From 6446f5209bfcebb1b9f8cc2e308cf6e0ba409ca4 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 16 Jun 2026 17:56:11 +0200 Subject: [PATCH] Remove semaphores in favor of task notifications - Less overhead, simpler to manage - Semaphores are apparently overkill for a simple blink timer --- Core/Inc/canlog.h | 1 - Core/Src/canlog.c | 27 +++++++++++++++++---------- Core/Src/main.c | 24 +++++++----------------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Core/Inc/canlog.h b/Core/Inc/canlog.h index 72ea736..4267270 100644 --- a/Core/Inc/canlog.h +++ b/Core/Inc/canlog.h @@ -44,7 +44,6 @@ typedef struct { typedef struct { LED_Config *led; - osSemaphoreId_t semaphore; } LEDContext; /* USER CODE END PTD */ diff --git a/Core/Src/canlog.c b/Core/Src/canlog.c index b7ebeee..c5366ca 100644 --- a/Core/Src/canlog.c +++ b/Core/Src/canlog.c @@ -24,6 +24,9 @@ extern CAN_HandleTypeDef hcan1; extern CAN_HandleTypeDef hcan2; +extern osThreadId_t xLedTaskCAN1; +extern osThreadId_t xLedTaskCAN2; + /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /* BEGIN CAN_Logger_Init */ @@ -76,15 +79,17 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) memcpy(message.payload, data, rxHeader.DLC); osMessageQueueId_t queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue; - if (osMessageQueuePut(queue, &message, 0U, 0U) != osOK) + if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK) + { + if (hcan->Instance == CAN1) osThreadFlagsSet(xLedTaskCAN1, 0x01); + else osThreadFlagsSet(xLedTaskCAN2, 0x01); + } + else { // TODO: better handling: flash error_led on osTimeout, call Error_Handler() when other errors // send errors like queue full via UART } - - osSemaphoreId_t semaphore = (hcan->Instance == CAN1) ? xSemaphoreCAN1 : xSemaphoreCAN2; - osSemaphoreRelease(semaphore); /* CODE END */ } /* END HAL_CAN_RxFifo0MsgPendingCallback */ @@ -126,12 +131,14 @@ void vLEDHeartbeat(void *argument) /* CODE BEGIN */ LEDContext *context = (LEDContext*)argument; - for (;;) - { - if (osSemaphoreAcquire(context->semaphore, 25U) == osOK) HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_SET); - else HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_RESET); - } - /* CODE END */ + for (;;) + { + uint32_t ret = osThreadFlagsWait(0x01, osFlagsWaitAny, 25U); + + if (ret & 0x01) HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_SET); + else HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_RESET); + } + /* CODE END */ } /* END vLEDHeartbeat */ /* USER CODE END FunctionPrototypes */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 5e307f8..d0172f2 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -54,9 +54,6 @@ LED_Config led_error = {GPIOB, GPIO_PIN_3}; LEDContext ledContextCAN1; LEDContext ledContextCAN2; -osSemaphoreId_t xSemaphoreCAN1; -osSemaphoreId_t xSemaphoreCAN2; - osMessageQueueId_t xCAN1RxQueue; osMessageQueueId_t xCAN2RxQueue; /* USER CODE END PV */ @@ -134,14 +131,14 @@ int main(void) .priority = (osPriority_t) osPriorityRealtime, }; - osThreadId_t xLEDHeartbeatCAN1; + osThreadId_t xLedTaskCAN1; const osThreadAttr_t LEDHeartbeatCAN1Attributes = { .name = "LED_HB_CAN1", .stack_size = 128 * 4, .priority = (osPriority_t) osPriorityVeryLow1, }; - osThreadId_t xLEDHeartbeatCAN2; + osThreadId_t xLedTaskCAN2; const osThreadAttr_t LEDHeartbeatCAN2Attributes = { .name = "LED_HB_CAN2", .stack_size = 128 * 4, @@ -154,15 +151,6 @@ int main(void) /* USER CODE END RTOS_MUTEX */ /* USER CODE BEGIN RTOS_SEMAPHORES */ - xSemaphoreCAN1 = osSemaphoreNew(32, 0, NULL); - xSemaphoreCAN2 = osSemaphoreNew(32, 0, NULL); - - if (xSemaphoreCAN1 == NULL || xSemaphoreCAN2 == NULL) Error_Handler(); - - ledContextCAN1.led = &led_can1; - ledContextCAN1.semaphore = xSemaphoreCAN1; - ledContextCAN2.led = &led_can2; - ledContextCAN2.semaphore = xSemaphoreCAN2; /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ @@ -179,8 +167,11 @@ int main(void) /* USER CODE BEGIN RTOS_THREADS */ xCAN1rx = osThreadNew(vCANLoggerListen, &hcan1, &CAN1rxAttributes); xCAN2rx = osThreadNew(vCANLoggerListen, &hcan2, &CAN2rxAttributes); - xLEDHeartbeatCAN1 = osThreadNew(vLEDHeartbeat, &ledContextCAN1, &LEDHeartbeatCAN1Attributes); - xLEDHeartbeatCAN2 = osThreadNew(vLEDHeartbeat, &ledContextCAN2, &LEDHeartbeatCAN2Attributes); + xLedTaskCAN1 = osThreadNew(vLEDHeartbeat, &ledContextCAN1, &LEDHeartbeatCAN1Attributes); + xLedTaskCAN2 = osThreadNew(vLEDHeartbeat, &ledContextCAN2, &LEDHeartbeatCAN2Attributes); + + ledContextCAN1.led = &led_can1; + ledContextCAN2.led = &led_can2; /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ @@ -483,7 +474,6 @@ static void MX_GPIO_Init(void) } /* USER CODE BEGIN 4 */ - } /* USER CODE END 4 */