From 6f60661cca120a5d094badc3d7f497b27ff922f3 Mon Sep 17 00:00:00 2001 From: zhji Date: Sun, 6 Jul 2025 10:53:30 +0800 Subject: [PATCH] [feat] add shell color demo --- example/shell/shell_color/CMakeLists.txt | 11 ++++++++ example/shell/shell_color/Makefile | 18 ++++++++++++ example/shell/shell_color/main.c | 35 ++++++++++++++++++++++++ example/shell/shell_color/proj.conf | 1 + example/shell/shell_no_os/main.c | 27 ++++++++++++++---- 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 example/shell/shell_color/CMakeLists.txt create mode 100644 example/shell/shell_color/Makefile create mode 100644 example/shell/shell_color/main.c create mode 100644 example/shell/shell_color/proj.conf diff --git a/example/shell/shell_color/CMakeLists.txt b/example/shell/shell_color/CMakeLists.txt new file mode 100644 index 0000000..010b21e --- /dev/null +++ b/example/shell/shell_color/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) + +include(proj.conf) + +project(${EXAMPLE_NAME} VERSION 0.1) +add_executable(${EXAMPLE_NAME}.elf main.c) + +include_directories(.) + +add_subdirectory(${SDK_BASE_DIR} sdk) +target_link_libraries(${EXAMPLE_NAME}.elf sdk) diff --git a/example/shell/shell_color/Makefile b/example/shell/shell_color/Makefile new file mode 100644 index 0000000..94b737f --- /dev/null +++ b/example/shell/shell_color/Makefile @@ -0,0 +1,18 @@ +EXAMPLE_BASE_DIR ?= $(shell realpath .) +EXAMPLE_NAME := $(notdir $(patsubst %/,%,$(CURDIR))) +SDK_BASE_DIR ?= $(shell realpath ./../../..) + +export SDK_BASE_DIR +export EXAMPLE_NAME +export EXAMPLE_BASE_DIR + +GCC_PATH := $(shell which arm-none-eabi-gcc) +CROSS_COMPILE := $(patsubst %gcc,%,$(GCC_PATH)) +ifeq ($(GCC_PATH),) +$(error arm-none-eabi-gcc not found in PATH. Please install the ARM toolchain.) +endif + +# add custom cmake definition +#cmake_definition+=-Dxxx=sss + +include $(SDK_BASE_DIR)/project.build diff --git a/example/shell/shell_color/main.c b/example/shell/shell_color/main.c new file mode 100644 index 0000000..a1823f7 --- /dev/null +++ b/example/shell/shell_color/main.c @@ -0,0 +1,35 @@ +#include "resets.h" +#include "gpio.h" +#include "uart.h" +#include "timer.h" +#include "stdio.h" +#include "mem.h" +#include "shell.h" + +extern struct shell _shell; +struct shell *shell = &_shell; + +int main(void) +{ + reset_unreset_blocks_wait(RESETS_BLOCK_IO_BANK0 | RESETS_BLOCK_UART0 | RESETS_BLOCK_TIMER); + gpio_init(0, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_DRIVE_4MA); /* UART_TX pin */ + gpio_init(1, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_SCHMITT | GPIO_PAD_IE | GPIO_PAD_OD); /* UART_RX pin */ + uart_init(uart0_hw, 6 * 1000 * 1000, UART_DATABITS_8 | UART_PARITY_NONE | UART_STOPBITS_1); + printf("shell color example\r\n"); + + kmem_init((void *)0x20030000, 64 * 1024); + shell_init(); + printf("shell = %p\r\n", shell); + printf("printf = %p\r\n", shell->shell_printf); + while(1) { + static uint32_t count = 0; + SHELL_CMD("cmd: hello world, %lu\r\n", count++); + SHELL_PRINTF("printf: hello world, %lu\r\n", count++); + SHELL_DGB("dbg: hello world, %lu\r\n", count++); + SHELL_PROMPT("prompt: hello world, %lu\r\n", count++); + SHELL_E("e: hello world, %lu\r\n", count++); + timer_delay_ms(1000); + } + + return 0; +} diff --git a/example/shell/shell_color/proj.conf b/example/shell/shell_color/proj.conf new file mode 100644 index 0000000..c1a262e --- /dev/null +++ b/example/shell/shell_color/proj.conf @@ -0,0 +1 @@ +set(CONFIG_SHELL 1) diff --git a/example/shell/shell_no_os/main.c b/example/shell/shell_no_os/main.c index 80d3967..9a4bead 100644 --- a/example/shell/shell_no_os/main.c +++ b/example/shell/shell_no_os/main.c @@ -1,14 +1,12 @@ #include "resets.h" -#include "timer.h" #include "gpio.h" #include "uart.h" #include "stdio.h" -#include "stdlib.h" +#include "string.h" #include "mem.h" #include "shell.h" -#define LED_PIN (25) -#define LED_TIME (500 * 1000) /* 500ms */ +#define LED_PIN (25) int main(void) { @@ -20,6 +18,7 @@ int main(void) kmem_init((void *)0x20030000, 64 * 1024); shell_init(); + gpio_init(LED_PIN, GPIO_FUNC_NULL | GPIO_OVER_OUT_LOW | GPIO_OVER_OE_ENABLE | GPIO_PULL_DOWN | GPIO_DRIVE_4MA); /* LED pin */ while(1) { int c = uart_get_char(uart0_hw); if (c >= 0) { @@ -30,13 +29,29 @@ int main(void) return 0; } -int shell_test(int argc, char **argv) +void shell_test(int argc, char **argv) { printf("shell test, argc=%d:\r\n", argc); for (int i = 0; i < argc; i++) { printf("argv[%d] = %s\r\n", i, argv[i]); } printf("\r\n"); - return 0; } SHELL_CMD_EXPORT_ALIAS(shell_test, test, shell test.); + +void shell_led(int argc, char **argv) +{ + if (argc != 2) { + printf("Usage: led on/off\r\n"); + return; + } + if (strlen(argv[1]) == 3 && argv[1][0] == 'o' && argv[1][1] == 'f' && argv[1][2] == 'f') { + gpio_clear(LED_PIN); + } else if (strlen(argv[1]) == 2 && argv[1][0] == 'o' && argv[1][1] == 'n') { + gpio_set(LED_PIN); + } else { + printf("Usage: led on/off\r\n"); + return; + } +} +SHELL_CMD_EXPORT_ALIAS(shell_led, led, shell led.);