From cbe236c43080d95434433b6a5e9ca93d2a6e0503 Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Fri, 21 Aug 2026 13:21:25 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=B8=BB=E7=BA=BF=E5=BE=85=E5=90=88?= =?UTF-8?q?=E3=80=91freeRTOS=E5=A2=9E=E5=8A=A0=E4=B8=8D=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E7=9A=84=E5=B8=A6=E8=B6=85=E6=97=B6=E4=BF=A1=E5=8F=B7=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RBcore/msg_center.c | 7 +++++-- bspMCU/My_freeRTOS.c | 15 +++++++++++++++ library/common/include/rd_thread.h | 1 + library/common/rd_thread.c | 7 +++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/RBcore/msg_center.c b/RBcore/msg_center.c index aca5068..e0ca677 100644 --- a/RBcore/msg_center.c +++ b/RBcore/msg_center.c @@ -327,8 +327,11 @@ int MsgCenter_ProcessWait(uint32_t uiModuleID, uint32_t uiTimeoutMs) // 等待信号量(有新消息到达) if (uiTimeoutMs > 0) { - // 带超时等待 - Rd_SemWait(&g_astModules[iIndex].stSem, __func__, NULL); + // 带超时等待,超时返回 -1 让调用方继续轮询 + if (Rd_SemWaitTimeout(&g_astModules[iIndex].stSem, uiTimeoutMs) != 0) + { + return -1; + } } else { diff --git a/bspMCU/My_freeRTOS.c b/bspMCU/My_freeRTOS.c index 3e064d2..10b3b3a 100644 --- a/bspMCU/My_freeRTOS.c +++ b/bspMCU/My_freeRTOS.c @@ -114,6 +114,21 @@ void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt) } } +int Rd_SemWaitTimeout(void *_sem, uint32_t _uiTimeoutMs) +{ + rd_sem_t *pSem = (rd_sem_t *)_sem; + uint32_t ticks = (_uiTimeoutMs * osKernelGetTickFreq()) / 1000; + if (ticks == 0) ticks = 1; + osStatus_t status = osSemaphoreAcquire(pSem->sem_id, ticks); + if (status == osOK) { + return 0; + } else if (status == osErrorTimeout || status == osErrorResource) { + return -1; + } else { + return -2; + } +} + void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt) { rd_sem_t *pSem = (rd_sem_t *)_sem; diff --git a/library/common/include/rd_thread.h b/library/common/include/rd_thread.h index 9643ae3..5cc59c4 100644 --- a/library/common/include/rd_thread.h +++ b/library/common/include/rd_thread.h @@ -155,6 +155,7 @@ int Rd_MutexDestroy(void *_pMutex); int Rd_SemInit(void *_sem, int _iPshared, int _iValue); void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt); void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt); +int Rd_SemWaitTimeout(void *_sem, uint32_t _uiTimeoutMs); int Rd_SemDestroy(void *_sem); /** diff --git a/library/common/rd_thread.c b/library/common/rd_thread.c index 853aebc..49c7ea4 100644 --- a/library/common/rd_thread.c +++ b/library/common/rd_thread.c @@ -55,6 +55,13 @@ WEAK void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt) return; } +WEAK int Rd_SemWaitTimeout(void *_sem, uint32_t _uiTimeoutMs) +{ + (void)_sem; + (void)_uiTimeoutMs; + return -1; +} + WEAK void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt) { return;