From 4b2cd53f3b8ca72fbcfed6381f082817c4cd115a Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 15 Jun 2026 13:46:31 +0200 Subject: [PATCH] Transition from timer based to task based LED signal - Remove RTOS timers - Create LED task - Avoid polling, instead wait for semaphore --- Core/Inc/canlog.h | 1 - Core/Src/canlog.c | 54 +++++++++++++++++++++++------------------------ Core/Src/main.c | 38 ++++++++++++++++----------------- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Core/Inc/canlog.h b/Core/Inc/canlog.h index d741aff..72ea736 100644 --- a/Core/Inc/canlog.h +++ b/Core/Inc/canlog.h @@ -45,7 +45,6 @@ typedef struct { typedef struct { LED_Config *led; osSemaphoreId_t semaphore; - osTimerId_t timer; } LEDContext; /* USER CODE END PTD */ diff --git a/Core/Src/canlog.c b/Core/Src/canlog.c index 45b2935..739ee47 100644 --- a/Core/Src/canlog.c +++ b/Core/Src/canlog.c @@ -97,22 +97,13 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) */ void vCANLoggerListen(void *argument) { - /* CODE BEGIN */ - CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)argument; - osMessageQueueId_t queue; - CanMessage_t message; + /* CODE BEGIN */ + LEDContext *context = (LEDContext*)argument; - queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue; - - for (;;) - { - if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK) - { - osSemaphoreRelease( (hcan->Instance == CAN1) ? xSemaphoreCAN1 : xSemaphoreCAN2 ); - // TODO: use this semaphore - } - - } + 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 */ } /* END vCANLoggerListen */ @@ -125,19 +116,28 @@ void vCANLoggerListen(void *argument) */ void vLEDHeartbeat(void *argument) { - /* CODE BEGIN */ - LEDContext *context = (LEDContext*)argument; + /* CODE BEGIN */ + LEDContext *context = (LEDContext*)argument; - if (osSemaphoreAcquire(context->semaphore, 0U) == osOK) - { - HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_SET); - osTimerStart(context->timer, 25U); - } - else - { - HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_RESET); - } - /* CODE END */ + for (;;) + { + // BLOCK for UP TO 25ms. + // - Returns osOK instantly if CAN traffic arrives. + // - Returns osTimeout if 25ms pass with NO traffic. + if (osSemaphoreAcquire(context->semaphore, 25U) == osOK) + { + // Traffic detected! Turn ON LED. + // Loop immediately to wait for the next message. + HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_SET); + } + else + { + // Timeout! No CAN traffic for 25ms. Turn OFF LED. + // Loop and block again. + 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 ff7bb39..26287f1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -55,9 +55,6 @@ LEDContext ledContextCAN2; osSemaphoreId_t xSemaphoreCAN1; osSemaphoreId_t xSemaphoreCAN2; -osTimerId_t xHeartbeatTimerCAN1; -osTimerId_t xHeartbeatTimerCAN2; - osMessageQueueId_t xCAN1RxQueue; osMessageQueueId_t xCAN2RxQueue; /* USER CODE END PV */ @@ -126,12 +123,27 @@ int main(void) .stack_size = 128 * 4, .priority = (osPriority_t) osPriorityRealtime1, }; + osThreadId_t xCAN2rx; const osThreadAttr_t CAN2rxAttributes = { .name = "CAN2rx", .stack_size = 128 * 4, .priority = (osPriority_t) osPriorityRealtime, }; + + osThreadId_t xLEDHeartbeatCAN1; + const osThreadAttr_t LEDHeartbeatCAN1Attributes = { + .name = "LED_HB_CAN1", + .stack_size = 128 * 4, + .priority = (osPriority_t) osPriorityVeryLow1, + }; + + osThreadId_t xLEDHeartbeatCAN2; + const osThreadAttr_t LEDHeartbeatCAN2Attributes = { + .name = "LED_HB_CAN2", + .stack_size = 128 * 4, + .priority = (osPriority_t) osPriorityVeryLow, + }; /* USER END RTOS_TASKS */ /* USER CODE BEGIN RTOS_MUTEX */ @@ -151,21 +163,14 @@ int main(void) /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ - xHeartbeatTimerCAN1 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN1, NULL); - xHeartbeatTimerCAN2 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN2, NULL); - - osTimerStart(xHeartbeatTimerCAN1, 25U); - osTimerStart(xHeartbeatTimerCAN2, 25U); - - if (xHeartbeatTimerCAN1 == NULL || xHeartbeatTimerCAN2 == NULL) Error_Handler(); - - ledContextCAN1.timer = xHeartbeatTimerCAN1; - ledContextCAN2.timer = xHeartbeatTimerCAN2; + /* add timers, ... */ /* USER CODE END RTOS_TIMERS */ /* USER CODE BEGIN RTOS_QUEUES */ xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL); xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL); + xLEDHeartbeatCAN1 = osThreadNew(vLEDHeartbeat, &ledContextCAN1, &LEDHeartbeatCAN1Attributes); + xLEDHeartbeatCAN2 = osThreadNew(vLEDHeartbeat, &ledContextCAN2, &LEDHeartbeatCAN2Attributes); if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler(); /* USER CODE END RTOS_QUEUES */ @@ -180,13 +185,6 @@ int main(void) /* USER CODE END RTOS_EVENTS */ /* USER CODE BEGIN 3 */ - ledContextCAN1.led = &led_can1; - ledContextCAN1.semaphore = xSemaphoreCAN1; - ledContextCAN1.timer = xHeartbeatTimerCAN1; - - ledContextCAN2.led = &led_can2; - ledContextCAN2.semaphore = xSemaphoreCAN2; - ledContextCAN2.timer = xHeartbeatTimerCAN2; /* USER CODE END 3 */ /* Start scheduler */