|
|
@ -17,13 +17,14 @@ |
|
|
|
|
|
|
|
|
******************************************************************************/ |
|
|
******************************************************************************/ |
|
|
#include "common.h" |
|
|
#include "common.h" |
|
|
|
|
|
#include "rd_thread.h" |
|
|
#ifdef USE_FREERTOS |
|
|
#ifdef USE_FREERTOS |
|
|
#include "cmsis_os.h" |
|
|
#include "cmsis_os.h" |
|
|
|
|
|
#include "cmsis_os2.h" |
|
|
#include "FreeRTOS.h" |
|
|
#include "FreeRTOS.h" |
|
|
#include "task.h" |
|
|
#include "task.h" |
|
|
#include "string.h" |
|
|
#include "string.h" |
|
|
#include "stdio.h" |
|
|
#include "stdio.h" |
|
|
#include "common.h" |
|
|
|
|
|
#include "lua_base.h" |
|
|
#include "lua_base.h" |
|
|
|
|
|
|
|
|
/*----------------------------------------------*
|
|
|
/*----------------------------------------------*
|
|
|
@ -79,81 +80,102 @@ void ShowHeapSize(void) |
|
|
lua_print("mempool free:\t%d\t%d\n", myMempool, myTotal); |
|
|
lua_print("mempool free:\t%d\t%d\n", myMempool, myTotal); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*void *pvPortCalloc(size_t xNum, size_t xSize)
|
|
|
|
|
|
{ |
|
|
|
|
|
size_t xTotalSize = xNum * xSize; |
|
|
|
|
|
|
|
|
|
|
|
// 防止乘法溢出(健壮性检查)
|
|
|
int Rd_SemInit(void *_sem, int _iPshared, int _iValue) |
|
|
if (xNum != 0 && xTotalSize / xNum != xSize) |
|
|
{ |
|
|
{ |
|
|
rd_sem_t *pSem = (rd_sem_t *)_sem; |
|
|
return NULL; |
|
|
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 *pvReturn = pvPortMalloc(xTotalSize); |
|
|
void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt) |
|
|
if (pvReturn != NULL) { |
|
|
{ |
|
|
// 将分配的内存块全部置零
|
|
|
rd_sem_t *pSem = (rd_sem_t *)_sem; |
|
|
memset(pvReturn, 0, xTotalSize); |
|
|
(void)_pcFunc; |
|
|
|
|
|
osSemaphoreAcquire(pSem->sem_id, osWaitForever); |
|
|
|
|
|
if (_piCnt != NULL) { |
|
|
|
|
|
(*_piCnt)++; |
|
|
} |
|
|
} |
|
|
return pvReturn; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
typedef struct A_BLOCK_LINK |
|
|
void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt) |
|
|
{ |
|
|
{ |
|
|
struct A_BLOCK_LINK *pxNextFreeBlock; |
|
|
rd_sem_t *pSem = (rd_sem_t *)_sem; |
|
|
size_t xBlockSize; |
|
|
(void)_pcFunc; |
|
|
} BlockLink_t; |
|
|
osSemaphoreRelease(pSem->sem_id); |
|
|
|
|
|
if (_piCnt != NULL) { |
|
|
|
|
|
(*_piCnt)++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 假设你使用的是 heap_4,通常 xHeapStructSize 等于 sizeof(BlockLink_t) 并做了对齐
|
|
|
int Rd_SemDestroy(void *_sem) |
|
|
// 你可以直接通过指针前移来获取旧大小
|
|
|
|
|
|
void *pvPortRealloc(void *pv, size_t xWantedSize) |
|
|
|
|
|
{ |
|
|
{ |
|
|
if (xWantedSize == 0) { vPortFree(pv); return NULL; } |
|
|
rd_sem_t *pSem = (rd_sem_t *)_sem; |
|
|
if (pv == NULL) { return pvPortMalloc(xWantedSize); } |
|
|
if (pSem->sem_id != NULL) { |
|
|
|
|
|
osSemaphoreDelete(pSem->sem_id); |
|
|
// 强行读取头部信息获取旧大小
|
|
|
pSem->sem_id = NULL; |
|
|
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; |
|
|
return RD_SUCCESS; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
SemaphoreHandle_t xCountingSemaphore; |
|
|
int Rd_MutexInit(void *_pMutex) |
|
|
StaticSemaphore_t xSemaphoreBuffer; |
|
|
|
|
|
|
|
|
|
|
|
int Rd_SemInit(void *_sem, int _iPshared, int _iValue) |
|
|
|
|
|
{ |
|
|
{ |
|
|
xCountingSemaphore = xSemaphoreCreateCountingStatic(5, 5, &xSemaphoreBuffer); |
|
|
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_FAILURE; |
|
|
|
|
|
} |
|
|
|
|
|
return RD_SUCCESS; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt) |
|
|
void Rd_MutexLock(void *_pMutex, const char *_pcFunc, int *_piCnt) |
|
|
{ |
|
|
{ |
|
|
xSemaphoreTake(sem_full, portMAX_DELAY) |
|
|
rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; |
|
|
return; |
|
|
(void)_pcFunc; |
|
|
|
|
|
if (*pMutex != NULL) { |
|
|
|
|
|
osMutexAcquire(*pMutex, osWaitForever); |
|
|
|
|
|
} |
|
|
|
|
|
if (_piCnt != NULL) { |
|
|
|
|
|
(*_piCnt)++; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt) |
|
|
void Rd_MutexUnlock(void *_pMutex, const char *_pcFunc, int *_piCnt) |
|
|
{ |
|
|
|
|
|
xSemaphoreGive(sem_empty); |
|
|
|
|
|
return; |
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
void Rd_MutexLock(void *_pMutex, const char *_pcFunc, int *_piCnt) |
|
|
|
|
|
{ |
|
|
{ |
|
|
vTaskSuspendAll(); |
|
|
rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; |
|
|
|
|
|
(void)_pcFunc; |
|
|
|
|
|
if (*pMutex != NULL) { |
|
|
|
|
|
osMutexRelease(*pMutex); |
|
|
|
|
|
} |
|
|
|
|
|
if (_piCnt != NULL) { |
|
|
|
|
|
(*_piCnt)++; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Rd_MutexUnlock(void *_pMutex, const char *_pcFunc, int *_piCnt) |
|
|
int Rd_MutexDestroy(void *_pMutex) |
|
|
{ |
|
|
{ |
|
|
(void)xTaskResumeAll(); |
|
|
rd_mutex_t *pMutex = (rd_mutex_t *)_pMutex; |
|
|
|
|
|
if (*pMutex != NULL) { |
|
|
|
|
|
osMutexDelete(*pMutex); |
|
|
|
|
|
*pMutex = NULL; |
|
|
|
|
|
} |
|
|
|
|
|
return RD_SUCCESS; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Rd_Delay(uint32_t Delay) |
|
|
void Rd_Delay(uint32_t Delay) |
|
|
|