RTOS with CAN Bus

srcad wrote on Saturday, April 27, 2019:


I’m having problems unblocking a task.
First, the project consists of two MCU’s communicating over the CAN Bus, and works fine before conversion to RTOS.
I’m starting by converting one device to RTOS, but I’m having problems unblocking the CAN RX task.

For this conversion,
I create two tasks of priority 2:

xTaskCreate(vtask_can_tx_data_handler, “TASK_TX”, 500, NULL, 2, &xTaskHandle1);
xTaskCreate(vtask_can_rx_data_handler, “TASK_RX”, 500, NULL, 2, &xTaskHandle2);

Timer 6 interrupt unblocks the vtask_can_tx_data_handler task with this line of code:

xTaskNotifyFromISR(xTaskHandle1, 0x00, eIncrement, pxHigherPriorityTaskWoken); //Unblock task from ISR

vtask_can_tx_data_handler then transmits CAN messages, and this works with no problems.

The other CAN device then sends a message back.

The trouble is with the can rx part.

When CAN data is received, the CAN rx interrupt occurs in:

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

Here, I try to unblock the vtask_can_rx_data_handler task with a similar notification:

xTaskNotifyFromISR(xTaskHandle2, 0x00, eIncrement, pxHigherPriorityTaskWoken); //Unblock task from ISR

Putting a break point here, I know CAN data is being received and this line is being executed every time, but for some reason my vtask_can_rx_data_handler task never executes.

If I just create the tx task and put the rx routine directly into:

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan),

the program works fine with just the can tx task.

Also, as a test, if I unblock the rx task from a timer interrupt, it also executes. The problem seems to be related to unblocking it from void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan), and I can’t figure this out.

I tried increasing the stack size up to 8192 for each task and also making the priority of the rx task higher than the tx task, and using taskYIELD(); at the end of the tx task, but the rx task still does not execute.

When communication stops, I hit pause in the debugger to see where it might be hung up, but can’t really identify a problem as it always seems to be in a different part of the code. I’m not sure what to look for.

What do you think is the problem?


srcad wrote on Saturday, April 27, 2019:

Also, the rx task is as follows:

void vtask_can_rx_data_handler(void *params)
uint32_t current_notification_value = 0;
while(1) //Task should never return.
//Wait until receive notification (unblocked) from CAN rx interrupt.
if(xTaskNotifyWait(0,0,&current_notification_value,portMAX_DELAY) == pdTRUE)
//Process RX data, or just print a message for now…

rtel wrote on Saturday, April 27, 2019:

If the code works from the timer interrupt but not the CAN interrupt
then my immediate thought is that there is something wrong in the
implementation of the CAN interrupt outside of the code that is
unblocking the task (as you have proven that part by running it in the
timer interrupt). Are you clearing the CAN interrupt correctly? Maybe
it is just continuously re-entering the interrupt handler.

srcad wrote on Saturday, April 27, 2019:

Thanks for the response Richard.

I think the HAL CAN API is handling the interrupt clearing, in addition to calling:
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

As I mentioned, if instead of unblocking the task, I just put the contents of the task here, it works fine, and I don’t add any additional “clearing” function. Just like this:

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
//Process RX data, or just print a message for now, directly from here instead of unblocking a task. //This works fine, but I just can’t seem to unblock a task from here.

srcad wrote on Sunday, April 28, 2019:

Actually Richard, you are correct.
I realized it this morning that the interrupt gets cleared after the received data is read.
In my case, its the HAL_CAN_GetRxMessage().
So, I call this from HAL_CAN_RxFifo0MsgPendingCallback(), unblock the task, then process the data in the task. Working great.

Two thumbs up, Thanks!

andrea84 wrote on Monday, October 14, 2019:

Can you put the entire example code? I’m trying to do the same thing. Thanks