Skip to content

Commit

Permalink
feat: add freertos with usb demo
Browse files Browse the repository at this point in the history
  • Loading branch information
sakumisu committed May 10, 2024
1 parent 561058d commit 5362d68
Show file tree
Hide file tree
Showing 10 changed files with 1,207 additions and 4 deletions.
8 changes: 4 additions & 4 deletions samples/hpm/freertos_uart/src/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static chry_shell_t csh;
static UART_Type *shell_uart = NULL;
static volatile bool login = false;
static chry_ringbuffer_t shell_rb;
static ATTR_PLACE_AT_NONCACHEABLE uint8_t mempool[256];
static uint8_t mempool[1024];

static StaticTask_t task_buffer_repl;
static StaticTask_t task_buffer_exec;
Expand Down Expand Up @@ -67,7 +67,7 @@ static uint16_t csh_sget_cb(chry_readline_t *rl, void *data, uint16_t size)
static void wait_char(void)
{
wait:
EventBits_t event = xEventGroupWaitBits(event_hdl, 0x35, pdTRUE, pdFALSE, portMAX_DELAY);
EventBits_t event = xEventGroupWaitBits(event_hdl, (0x10 | 0x01 | 0x04), pdTRUE, pdFALSE, portMAX_DELAY);
if ((event & 0x10) == 0) {
if (event & 0x01) {
chry_readline_erase_line(&csh.rl);
Expand Down Expand Up @@ -271,8 +271,8 @@ int shell_init(UART_Type *uart, bool need_login)
csh_init.uid = 0;
csh_init.user[0] = "cherry";

/*!< The port hash function is required,
and the strcmp attribute is used weakly by default,
/*!< The port hash function is required,
and the strcmp attribute is used weakly by default,
int chry_shell_port_hash_strcmp(const char *hash, const char *str); */
csh_init.hash[0] = "12345678"; /*!< If there is no password, set to NULL */
csh_init.host = BOARD_NAME;
Expand Down
33 changes: 33 additions & 0 deletions samples/hpm/freertos_usb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2021 HPMicro
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.13)

set(CONFIG_CHERRYRB 1)
set(CONFIG_FREERTOS 1)
set(CONFIG_CHERRYUSB 1)
set(CONFIG_USB_DEVICE 1)
set(CONFIG_USB_DEVICE_CDC 1)

find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(cherryshell)

sdk_compile_options("-O2")
sdk_compile_definitions(-D__freertos_irq_stack_top=_stack)
sdk_compile_definitions(-DCONFIG_FREERTOS=1)
sdk_compile_definitions(-DUSE_NONVECTOR_MODE=1)
sdk_compile_definitions(-DDISABLE_IRQ_PREEMPTIVE=1)

sdk_inc(inc)
sdk_inc(../../../)

sdk_app_src(
src/main.c
src/shell.c
../../../chry_shell.c
../../../builtin/help.c
../../../builtin/shsize.c
../../../builtin/login.c
)

generate_ses_project()
63 changes: 63 additions & 0 deletions samples/hpm/freertos_usb/README_en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Cherry Shell FreeRTOS USB

## Overview

The Cherry Shell FreeRTOS example project demonstrates the basic usage of shell in multi-thread mode. In this project, you need to connect via serial port, and the shell requires a login. The default password is 12345678. After entering the password and pressing Enter, you will enter the shell. You can use the "help" command to view the available commands and variables.

Please refrain from using serial port assistants and consider downloading and using [MobaXterm](https://mobaxterm.mobatek.net/download.html)

## Board Setting

No special settings

## Running the example

- After the project runs successfully, the serial terminal will output the following information:

```console
login as: cherry
cherry@hpm5301evklite's password:
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S

```

- If you manually enter the correct password "12345678" and press Enter, the terminal will output the following information:

```console
login as: cherry
cherry@hpm5301evklite's password:
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S

welcome to cherry shell
cherry@hpm5301evklite:/$
```

- If you manually enter the command "help" and press Enter, the terminal will output the following information:

```console
cherry@hpm5301evklite:/$ help
total function 6
test -> /bin
toggle_led -> /bin
write_led -> /bin
exit -> /sbin
help -> /sbin
shsize -> /sbin

total variable 2
$PATH r- 11
$ZERO r- 1
cherry@hpm5301evklite:/$
```

- If you manually enter the command "toggle_led" and press Enter, the LED will toggle.
- If you manually enter the command "write_led 1" and press Enter, the LED will turn on.
- If you manually enter the command "write_led 0" and press Enter, the LED will turn off.
62 changes: 62 additions & 0 deletions samples/hpm/freertos_usb/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Cherry Shell FreeRTOS USB

## 概述

Cherry Shell FreeRTOS示例工程展示了shell的多线程模式基本使用。多线程模式下,命令的执行在独立的线程中,可以使用Ctrl+C中断命令执行。在这个工程中,需要连接串口,shell默认需要登陆,密码为12345678。输入密码后回车,进入shell,输入help可以查看命令和变量。
请勿使用串口助手,推荐下载使用 [MobaXterm](https://mobaxterm.mobatek.net/download.html)

## 硬件设置

无特殊设置

## 运行现象

- 当工程正确运行后,串口终端会输出如下信息:

```console
login as: cherry
cherry@hpm5301evklite's password:
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S

```

- 如果此时通过键盘手动输入正确密码"12345678"并回车,终端会输出如下信息:

```console
login as: cherry
cherry@hpm5301evklite's password:
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S
other task interval 5S

welcome to cherry shell
cherry@hpm5301evklite:/$
```

- 如果此时通过键盘手动输入命令"help"并回车,终端会输出如下信息:

```console
cherry@hpm5301evklite:/$ help
total function 6
test -> /bin
toggle_led -> /bin
write_led -> /bin
exit -> /sbin
help -> /sbin
shsize -> /sbin

total variable 2
$PATH r- 11
$ZERO r- 1
cherry@hpm5301evklite:/$
```

- 如果此时通过键盘手动输入命令"toggle_led"并回车,LED将会翻转
- 如果此时通过键盘手动输入命令"write_led 1"并回车,LED将会点亮
- 如果此时通过键盘手动输入命令"write_led 0"并回车,LED将会熄灭
149 changes: 149 additions & 0 deletions samples/hpm/freertos_usb/inc/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2021 HPMicro
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/*
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html.
*/

#include "board.h"

#if (portasmHAS_MTIME == 0)
#define configMTIME_BASE_ADDRESS (0)
#define configMTIMECMP_BASE_ADDRESS (0)
#else
#define configMTIME_BASE_ADDRESS (HPM_MCHTMR_BASE)
#define configMTIMECMP_BASE_ADDRESS (HPM_MCHTMR_BASE + 8UL)
#endif

#define configUSE_PREEMPTION 1
#define configCPU_CLOCK_HZ ((uint32_t) 24000000)
#define configTICK_RATE_HZ ((TickType_t) 1000)
#define configMAX_PRIORITIES (32)
#define configMINIMAL_STACK_SIZE (256)
#define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 0
#define configUSE_APPLICATION_TASK_TAG 0
#define configGENERATE_RUN_TIME_STATS 0

/* Memory allocation definitions. */
#define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configTOTAL_HEAP_SIZE ((size_t) (8 * 1024))

/* Hook function definitions. */
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configUSE_MALLOC_FAILED_HOOK 0
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0

/* Run time and task stats gathering definitions. */
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 0

/* Set the following definitions to 1 to include the API function, or zero to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTaskGetCurrentTaskHandle 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xSemaphoreGetMutexHolder 1

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES 2

/* Software timer definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1)
#define configTIMER_QUEUE_LENGTH 4
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE)

/* Task priorities.*/
#ifndef uartPRIMARY_PRIORITY
#define uartPRIMARY_PRIORITY (configMAX_PRIORITIES - 3)
#endif

/* Normal assert() semantics without relying on the provision of an assert.h header file. */
#define configASSERT(x) if ((x) == 0) { taskDISABLE_INTERRUPTS(); __asm volatile("ebreak"); for (;;); }

/*
* The size of the global output buffer that is available for use when there
* are multiple command interpreters running at once (for example, one on a UART
* and one on TCP/IP). This is done to prevent an output buffer being defined by
* each implementation - which would waste RAM. In this case, there is only one
* command interpreter running.
*/

/*
* The buffer into which output generated by FreeRTOS+CLI is placed. This must
* be at least big enough to contain the output of the task-stats command, as the
* example implementation does not include buffer overlow checking.
*/
#define configCOMMAND_INT_MAX_OUTPUT_SIZE 2096
#define configINCLUDE_QUERY_HEAP_COMMAND 1

/* This file is included from assembler files - make sure C code is not included in assembler files. */
#ifndef __ASSEMBLER__
void vAssertCalled(const char *pcFile, unsigned long ulLine);
void vConfigureTickInterrupt(void);
void vClearTickInterrupt(void);
void vPreSleepProcessing(unsigned long uxExpectedIdleTime);
void vPostSleepProcessing(unsigned long uxExpectedIdleTime);
#endif /* __ASSEMBLER__ */

/****** Hardware/compiler specific settings. *******/
/*
* The application must provide a function that configures a peripheral to
* create the FreeRTOS tick interrupt, then define configSETUP_TICK_INTERRUPT()
* in FreeRTOSConfig.h to call the function.
*/
#define configSETUP_TICK_INTERRUPT() vConfigureTickInterrupt()
#define configCLEAR_TICK_INTERRUPT() vClearTickInterrupt()

/*
* The configPRE_SLEEP_PROCESSING() and configPOST_SLEEP_PROCESSING() macros
* allow the application writer to add additional code before and after the MCU is
* placed into the low power state respectively. The empty implementations
* provided in this demo can be extended to save even more power.
*/
#define configPRE_SLEEP_PROCESSING(uxExpectedIdleTime) vPreSleepProcessing(uxExpectedIdleTime);
#define configPOST_SLEEP_PROCESSING(uxExpectedIdleTime) vPostSleepProcessing(uxExpectedIdleTime);


/* Compiler specifics. */
#define fabs(x) __builtin_fabs(x)

/* Enable Hardware Stack Protection and Recording mechanism. */
#define configHSP_ENABLE 0

/* Record the highest address of stack. */
#if (configHSP_ENABLE == 1 && configRECORD_STACK_HIGH_ADDRESS != 1)
#define configRECORD_STACK_HIGH_ADDRESS 1
#endif

#endif /* FREERTOS_CONFIG_H */
Loading

0 comments on commit 5362d68

Please sign in to comment.