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 */
+18 -20
View File
@@ -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 */