Transition from timer based to task based LED signal

- Remove RTOS timers
- Create LED task
- Avoid polling, instead wait for semaphore
This commit is contained in:
2026-06-15 13:46:31 +02:00
parent 70cead28d7
commit 4b2cd53f3b
3 changed files with 45 additions and 48 deletions
+27 -27
View File
@@ -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 */