/****************************************************************************** 版权所有 (C), 2018-2099, Radkil ****************************************************************************** 文 件 名 : My_freeRTOS.c 版 本 号 : 初稿 作 者 : Radkil 生成日期 : 2026年5月17日 最近修改 : 功能描述 : freeRTOS中间层 修改历史 : 1.日 期 : 2026年5月17日 作 者 : Radkil 修改内容 : 创建文件 ******************************************************************************/ #include "common.h" #include "rd_thread.h" #ifdef USE_FREERTOS #include "cmsis_os.h" #include "cmsis_os2.h" #include "FreeRTOS.h" #include "task.h" #include "string.h" #include "stdio.h" #include "lua_base.h" /*----------------------------------------------* * 外部变量说明 * *----------------------------------------------*/ /*----------------------------------------------* * 外部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 内部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 全局变量 * *----------------------------------------------*/ /*----------------------------------------------* * 模块级变量 * *----------------------------------------------*/ /*----------------------------------------------* * 常量定义 * *----------------------------------------------*/ /*----------------------------------------------* * 宏定义 * *----------------------------------------------*/ void StackOverflowHook(TaskHandle_t xTask, char *pcTaskName) { log_a("!!! Stack Overflow Detected in Task: %s !!!\n", pcTaskName); while(1) {osDelay(1);} } void ShowTaskList(void) { char taskInfoBuffer[256]; vTaskList(taskInfoBuffer); lua_print("Name\t\tState\tPrior\tStack\tNum\n"); lua_print("%s", taskInfoBuffer); } void ShowHeapSize(void) { size_t iSize = xPortGetFreeHeapSize(); size_t iMin = xPortGetMinimumEverFreeHeapSize(); size_t Rd_MemPoolGetFreeCount(void); size_t myMempool = Rd_MemPoolGetFreeCount(); size_t Rd_MemPoolGetTotalCount(void); size_t myTotal = Rd_MemPoolGetTotalCount(); lua_print("freeRTOS used:\t%d\t%d\n", iSize, iMin); lua_print("mempool free:\t%d\t%d\n", myMempool, myTotal); } int Rd_SemInit(void *_sem, int _iPshared, int _iValue) { rd_sem_t *pSem = (rd_sem_t *)_sem; osSemaphoreAttr_t attr = { .name = NULL, .attr_bits = 0, .cb_mem = pSem->cb_buf, .cb_size = sizeof(pSem->cb_buf) }; pSem->sem_id = osSemaphoreNew((uint32_t)_iValue + 1, (uint32_t)_iValue, &attr); if (pSem->sem_id == NULL) { return RD_FAILURE; } pSem->count = _iValue; return RD_SUCCESS; } void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt) { rd_sem_t *pSem = (rd_sem_t *)_sem; (void)_pcFunc; osSemaphoreAcquire(pSem->sem_id, osWaitForever); if (_piCnt != NULL) { (*_piCnt)++; } } void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt) { rd_sem_t *pSem = (rd_sem_t *)_sem; (void)_pcFunc; osSemaphoreRelease(pSem->sem_id); if (_piCnt != NULL) { (*_piCnt)++; } } int Rd_SemDestroy(void *_sem) { rd_sem_t *pSem = (rd_sem_t *)_sem; if (pSem->sem_id != NULL) { osSemaphoreDelete(pSem->sem_id); pSem->sem_id = NULL; } return RD_SUCCESS; } int Rd_MutexInit(void *_pMutex) { rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; osMutexAttr_t attr = { .name = NULL, .attr_bits = osMutexRecursive | osMutexPrioInherit, .cb_mem = NULL, .cb_size = 0 }; *pMutex = osMutexNew(&attr); if (*pMutex == NULL) { return RD_FAILURE; } return RD_SUCCESS; } void Rd_MutexLock(void *_pMutex, const char *_pcFunc, int *_piCnt) { rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; (void)_pcFunc; if (*pMutex != NULL) { osMutexAcquire(*pMutex, osWaitForever); } if (_piCnt != NULL) { (*_piCnt)++; } } void Rd_MutexUnlock(void *_pMutex, const char *_pcFunc, int *_piCnt) { rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; (void)_pcFunc; if (*pMutex != NULL) { osMutexRelease(*pMutex); } if (_piCnt != NULL) { (*_piCnt)++; } } int Rd_MutexDestroy(void *_pMutex) { rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; if (*pMutex != NULL) { osMutexDelete(*pMutex); *pMutex = NULL; } return RD_SUCCESS; } void Rd_Delay(uint32_t Delay) { osDelay(Delay); } void MyTask_Init(void) { #ifndef USE_TALNET const osThreadAttr_t Lua_attributes = { .name = "LuaREPL", .stack_size = 4 * 1024, .priority = (osPriority_t) osPriorityLow7, }; void StartLuaREPL(void *argument); (void)osThreadNew(StartLuaREPL, NULL, &Lua_attributes); #endif #ifdef USE_LWIP const osThreadAttr_t talnet_attributes = { .name = "talnet", .stack_size = 6 * 1024, .priority = (osPriority_t) osPriorityLow6, }; void vTCPServer_Task(void *pvParameters); (void)osThreadNew(vTCPServer_Task, NULL, &talnet_attributes); #endif } #endif