freeRTOS操作系统机器人实现
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

182 lines
5.2 KiB

/******************************************************************************
版权所有 (C), 2018-2099, Radkil
******************************************************************************
文 件 名 : My_freeRTOS.c
版 本 号 : 初稿
作 者 : Radkil
生成日期 : 2026年5月17日
最近修改 :
功能描述 : freeRTOS中间层
修改历史 :
1.日 期 : 2026年5月17日
作 者 : Radkil
修改内容 : 创建文件
******************************************************************************/
#include "bsp_config.h"
#ifdef USE_FREERTOS
#include "cmsis_os.h"
#include "FreeRTOS.h"
#include "task.h"
#include "string.h"
#include "stdio.h"
#include "common.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);
}
/*void *pvPortCalloc(size_t xNum, size_t xSize)
{
size_t xTotalSize = xNum * xSize;
// 防止乘法溢出(健壮性检查)
if (xNum != 0 && xTotalSize / xNum != xSize)
{
return NULL;
}
void *pvReturn = pvPortMalloc(xTotalSize);
if (pvReturn != NULL) {
// 将分配的内存块全部置零
memset(pvReturn, 0, xTotalSize);
}
return pvReturn;
}
typedef struct A_BLOCK_LINK
{
struct A_BLOCK_LINK *pxNextFreeBlock;
size_t xBlockSize;
} BlockLink_t;
// 假设你使用的是 heap_4,通常 xHeapStructSize 等于 sizeof(BlockLink_t) 并做了对齐
// 你可以直接通过指针前移来获取旧大小
void *pvPortRealloc(void *pv, size_t xWantedSize)
{
if (xWantedSize == 0) { vPortFree(pv); return NULL; }
if (pv == NULL) { return pvPortMalloc(xWantedSize); }
// 强行读取头部信息获取旧大小
uint8_t *puc = (uint8_t *)pv;
puc -= sizeof(BlockLink_t); // 减去头部大小(需注意字节对齐,通常 heap_4 默认是 8 字节对齐)
BlockLink_t *pxLink = (BlockLink_t *)puc;
size_t xOldSize = pxLink->xBlockSize & ~((size_t)0x01); // 去掉最低位的已分配标志位
void *pvNew = pvPortMalloc(xWantedSize);
if (pvNew != NULL)
{
memcpy(pvNew, pv, xOldSize);
vPortFree(pv);
}
return pvNew;
}
SemaphoreHandle_t xCountingSemaphore;
StaticSemaphore_t xSemaphoreBuffer;
int Rd_SemInit(void *_sem, int _iPshared, int _iValue)
{
xCountingSemaphore = xSemaphoreCreateCountingStatic(5, 5, &xSemaphoreBuffer);
return RD_FAILURE;
}
void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt)
{
xSemaphoreTake(sem_full, portMAX_DELAY)
return;
}
void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt)
{
xSemaphoreGive(sem_empty);
return;
}*/
void Rd_MutexLock(void *_pMutex, const char *_pcFunc, int *_piCnt)
{
vTaskSuspendAll();
}
void Rd_MutexUnlock(void *_pMutex, const char *_pcFunc, int *_piCnt)
{
(void)xTaskResumeAll();
}
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