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:
+27
-27
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user