Initial commit: CCU621_M firmware project with BLE debug link support.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-08 17:28:36 +08:00
commit 9ceb218f80
1597 changed files with 724159 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
/*
* FreeRTOS Kernel V10.3.1
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#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
*----------------------------------------------------------*/
/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__TASKING__) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 1
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ ( SystemCoreClock )
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES ( 32 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 130 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 150 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 2
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 1
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configGENERATE_RUN_TIME_STATS 0
/* 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 ( 2 )
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 )
/* 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 0
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 4 /* 15 priority levels */
#endif
/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf
/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
/* Interrupt priorities used by the kernel port layer itself. These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
#endif /* FREERTOS_CONFIG_H */
+282
View File
@@ -0,0 +1,282 @@
/**
* @file app_freertos.c
* @brief FreeRTOS 运行信息查看(任务/堆/CPU占用)
*
* 参考实现:`CCU601E_RUN/BSP/freertos/app_freertos.c`
*
* 本工程适配说明:
* - 任务“栈大小/周期”从 `app_init/app_init.c` 的任务表 `my_task_data[]` 获取
* - 看门狗计数 `wdtCount` 通过 `u16_get_wdt_cnt()` 获取实时值
* - 通过 letter_shell 导出 `ps` 命令打印任务信息
*/
#include "freertos/app_freertos.h"
#include <string.h>
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "app_init/app_init.h"
#include "wdt_task/wdt_task.h"
#include "publicdata/public_define.h"
#if (MY_SHELL_EN)
#include "letter_shell/shell_port.h"
#define APP_RTOS_OUTPUT(str_) shell_send_String((str_))
#else
static void app_rtos_output(const char *str)
{
(void)str;
}
#define APP_RTOS_OUTPUT(str_) app_rtos_output((str_))
#endif
/* 按任务名查表:type=0 栈大小(字节)type=1 周期(ms) */
static uint32_t get_task_cfg_by_name(const char *name, uint8_t type)
{
if (name == NULL) {
return 0U;
}
for (uint32_t i = 0U; i < (uint32_t)MY_TASK_NUM; i++) {
if (strcmp(name, my_task_data[i].task_name) == 0) {
return (type == 0U) ? (uint32_t)my_task_data[i].task_size
: (uint32_t)my_task_data[i].task_timer;
}
}
return 0U;
}
/* 按任务名查任务ID:成功返回1,并通过 task_id 输出;失败返回0 */
static uint8_t get_task_id_by_name(const char *name, TASK_ID *task_id)
{
if ((name == NULL) || (task_id == NULL)) {
return 0U;
}
for (uint32_t i = 0U; i < (uint32_t)MY_TASK_NUM; i++) {
if (strcmp(name, my_task_data[i].task_name) == 0) {
*task_id = (TASK_ID)i;
return 1U;
}
}
return 0U;
}
/**
* @brief 按优先级对任务状态数组进行排序,返回排序后的原始数组下标
* @param pxTaskStatusArray 任务状态数组指针
* @param uxArraySize 数组长度
* @param rtos_pri_index 输出缓冲区(最大30个元素),存储排序后的原始数组下标
* @return 实际存储数量(≤uxArraySize
*/
static UBaseType_t SortTasksByPriorityIndex(const TaskStatus_t *pxTaskStatusArray,
UBaseType_t uxArraySize,
UBaseType_t rtos_pri_index[30])
{
if (pxTaskStatusArray == NULL || rtos_pri_index == NULL) {
return 0U;
}
UBaseType_t valid_size = (uxArraySize > 30U) ? 30U : uxArraySize;
UBaseType_t indexes[30];
for (UBaseType_t i = 0U; i < valid_size; i++) {
indexes[i] = i;
}
for (UBaseType_t i = 0U; i + 1U < valid_size; i++) {
for (UBaseType_t j = 0U; j + 1U < (valid_size - i); j++) {
if (pxTaskStatusArray[indexes[j]].uxCurrentPriority <=
pxTaskStatusArray[indexes[j + 1U]].uxCurrentPriority) {
UBaseType_t tmp = indexes[j];
indexes[j] = indexes[j + 1U];
indexes[j + 1U] = tmp;
}
}
}
for (UBaseType_t i = 0U; i < valid_size; i++) {
rtos_pri_index[i] = indexes[i];
}
return valid_size;
}
SystemInfo_t *getSystemInfo(void)
{
TaskStatus_t *pxTaskStatusArray = NULL;
UBaseType_t uxArraySize;
UBaseType_t rtos_pri[30] = {0};
const char states[] = {'X', 'R', 'B', 'S', 'D'};
uint32_t ulTotalTime = 0U;
uxArraySize = uxTaskGetNumberOfTasks();
pxTaskStatusArray = (TaskStatus_t *)pvPortMalloc(uxArraySize * sizeof(TaskStatus_t));
if (pxTaskStatusArray == NULL) {
return NULL;
}
uxArraySize = uxTaskGetSystemState(pxTaskStatusArray, uxArraySize, &ulTotalTime);
UBaseType_t sorted_count = SortTasksByPriorityIndex(pxTaskStatusArray, uxArraySize, rtos_pri);
SystemInfo_t *sysInfo = (SystemInfo_t *)pvPortMalloc(sizeof(SystemInfo_t));
if (sysInfo == NULL) {
vPortFree(pxTaskStatusArray);
return NULL;
}
memset(sysInfo, 0, sizeof(SystemInfo_t));
sysInfo->tasks = (TaskInfo_t *)pvPortMalloc(sorted_count * sizeof(TaskInfo_t));
if (sysInfo->tasks == NULL) {
vPortFree(pxTaskStatusArray);
vPortFree(sysInfo);
return NULL;
}
memset(sysInfo->tasks, 0, sorted_count * sizeof(TaskInfo_t));
sysInfo->taskCount = (unsigned int)sorted_count;
for (UBaseType_t i = 0U; i < sorted_count; i++) {
UBaseType_t x = rtos_pri[i];
if (x >= uxArraySize) {
break;
}
TaskInfo_t *task = &sysInfo->tasks[i];
strncpy(task->taskName, pxTaskStatusArray[x].pcTaskName, sizeof(task->taskName) - 1U);
task->taskName[sizeof(task->taskName) - 1U] = '\0';
if (pxTaskStatusArray[x].eCurrentState <= eDeleted) {
task->state = states[pxTaskStatusArray[x].eCurrentState];
} else {
task->state = '?';
}
task->id = (unsigned int)i;
task->priority = (unsigned int)pxTaskStatusArray[x].uxCurrentPriority;
task->allocatedStack = (unsigned int)get_task_cfg_by_name(pxTaskStatusArray[x].pcTaskName, 0U);
task->cycle = (unsigned int)get_task_cfg_by_name(pxTaskStatusArray[x].pcTaskName, 1U);
{
TASK_ID task_id;
task->wdtCount = (get_task_id_by_name(pxTaskStatusArray[x].pcTaskName, &task_id) == 1U)
? (unsigned int)u16_get_wdt_cnt((uint8_t)task_id)
: 0U;
}
task->minFreeStack = (unsigned int)pxTaskStatusArray[x].usStackHighWaterMark * 4U;
sysInfo->totalAllocatedStack += task->allocatedStack;
if (task->allocatedStack > 0U) {
unsigned int freePct = (task->minFreeStack * 100U) / task->allocatedStack;
task->stackUsagePercentage = (freePct == 0U) ? 0U : (100U - freePct);
} else {
task->stackUsagePercentage = 0U;
}
task->cpuUsagePercentage = (ulTotalTime > 0U)
? (unsigned int)((pxTaskStatusArray[x].ulRunTimeCounter * 100UL) / ulTotalTime)
: 0U;
}
sysInfo->freeHeapSize = (unsigned int)xPortGetFreeHeapSize();
sysInfo->minEverFreeHeapSize = (unsigned int)xPortGetMinimumEverFreeHeapSize();
sysInfo->totalHeapSize = (unsigned int)configTOTAL_HEAP_SIZE;
vPortFree(pxTaskStatusArray);
return sysInfo;
}
void freeSystemInfo(SystemInfo_t *sysInfo)
{
if (sysInfo != NULL) {
if (sysInfo->tasks != NULL) {
vPortFree(sysInfo->tasks);
}
vPortFree(sysInfo);
}
}
void printSystemInfo(void)
{
SystemInfo_t *sysInfo = getSystemInfo();
if (sysInfo == NULL) {
APP_RTOS_OUTPUT("Error: Failed to get system information!\r\n");
return;
}
char buffer[256];
(void)snprintf(buffer, sizeof(buffer),
"%-3s %-12s %-2s %-4s %-6s %-6s %-6s %-6s %-5s %-5s\r\n",
"ID", "TaskName", "St", "Pri", "Cycle", "Stack", "Wdt", "Free", "Use%", "CPU%");
APP_RTOS_OUTPUT(buffer);
for (unsigned int i = 0U; i < sysInfo->taskCount; i++) {
TaskInfo_t *task = &sysInfo->tasks[i];
const char *cpuStr = (task->cpuUsagePercentage > 0U) ? "" : "<1";
if (task->cpuUsagePercentage > 0U) {
(void)snprintf(buffer, sizeof(buffer),
"%-3u %-12.12s %-2c %-4u %-6u %-6u %-6u %-6u %-5u %-5u%%\r\n",
task->id,
task->taskName,
task->state,
task->priority,
task->cycle,
task->allocatedStack,
task->wdtCount,
task->minFreeStack,
task->stackUsagePercentage,
task->cpuUsagePercentage);
} else {
(void)snprintf(buffer, sizeof(buffer),
"%-3u %-12.12s %-2c %-4u %-6u %-6u %-6u %-6u %-5u %-5s%%\r\n",
task->id,
task->taskName,
task->state,
task->priority,
task->cycle,
task->allocatedStack,
task->wdtCount,
task->minFreeStack,
task->stackUsagePercentage,
cpuStr);
}
APP_RTOS_OUTPUT(buffer);
}
float free_percent = (sysInfo->totalHeapSize > 0U) ? ((float)sysInfo->freeHeapSize * 100.0f) / (float)sysInfo->totalHeapSize : 0.0f;
float min_percent = (sysInfo->totalHeapSize > 0U) ? ((float)sysInfo->minEverFreeHeapSize * 100.0f) / (float)sysInfo->totalHeapSize : 0.0f;
(void)snprintf(buffer, sizeof(buffer),
"\r\nHeap:\r\n"
"Free: %u bytes (%.1f%%)\r\n"
"Min: %u bytes (%.1f%%)\r\n"
"Stacks: %u bytes\r\n"
"Total: %u bytes\r\n"
"----------------------------------------\r\n",
sysInfo->freeHeapSize, free_percent,
sysInfo->minEverFreeHeapSize, min_percent,
sysInfo->totalAllocatedStack,
sysInfo->totalHeapSize);
APP_RTOS_OUTPUT(buffer);
freeSystemInfo(sysInfo);
}
#if (MY_SHELL_EN)
/* 兼容入口:对齐参考工程函数名 */
static void printTaskStackUsage(void)
{
printSystemInfo();
}
/* Shell 命令:ps */
static void v_task_show(void)
{
printTaskStackUsage();
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
ps, v_task_show, myshlle-- task runing state show);
#endif
+44
View File
@@ -0,0 +1,44 @@
/**
* @file app_freertos.h
* @brief FreeRTOS 运行信息查看(任务/堆/CPU占用)
*
* 说明:
* - 参考 `CCU601E_RUN/BSP/freertos/app_freertos.c` 的用途,在 Shell 中提供 `ps` 命令
* - 本工程接入任务看门狗统计,`wdtCount` 为各任务实时看门狗计数值
*/
#ifndef APP_FREERTOS_H
#define APP_FREERTOS_H
#include "FreeRTOS.h"
/* 单任务信息快照 */
typedef struct {
char taskName[configMAX_TASK_NAME_LEN];
char state;
unsigned int id;
unsigned int priority;
unsigned int cycle; /* 任务周期(ms) - 来自任务表 */
unsigned int allocatedStack; /* 分配堆栈大小(字节) - 来自任务表 */
unsigned int wdtCount; /* 看门狗计数(秒级累加,实时读取) */
unsigned int minFreeStack; /* 最小剩余堆栈(字节) */
unsigned int stackUsagePercentage; /* 堆栈使用百分比 */
unsigned int cpuUsagePercentage; /* CPU使用百分比 */
} TaskInfo_t;
/* 系统信息快照 */
typedef struct {
TaskInfo_t *tasks; /* 任务数组 */
unsigned int taskCount; /* 任务数量 */
unsigned int freeHeapSize; /* 当前剩余堆空间 */
unsigned int minEverFreeHeapSize; /* 历史最小剩余堆 */
unsigned int totalAllocatedStack; /* 已知分配任务空间(字节) */
unsigned int totalHeapSize; /* 总堆空间大小(字节) */
} SystemInfo_t;
SystemInfo_t *getSystemInfo(void);
void freeSystemInfo(SystemInfo_t *sysInfo);
void printSystemInfo(void);
#endif /* APP_FREERTOS_H */