cancel
Showing results for 
Search instead for 
Did you mean: 

Using FreeRTOS with CAN interrupt

cleonb322
Associate III
Posted on October 05, 2017 at 19:21

FreeRTOS with CAN interrupt. It seems like im missing messages when using CAN with freertos. I know that the CAN bus is half duplex. Since i have no control over the Rx messages, how do i sync the tx task with the rx callback?

#can-rx
5 REPLIES 5
Posted on October 05, 2017 at 20:06

You don't have to task switch on the interrupt, and you don't need to use the HAL callback methodology.

If you use the callbacks, do very little and leave quickly, if you block or do unnecessary processing you will likely get data loss. Make sure you extract all the data from the peripheral.

The peripheral should manage the RX/TX on the bus.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead
Posted on October 06, 2017 at 01:36

I use the BxCan on MBED and RTOS and Bare Metal.

all in the same processor family, STM32F series. '072 '091 '767

Definitely, as Clive says above,  you want to exit any interrupt very quickly.

it is best to receive under interrupt, then write to a buffer and exit.

I use a 64 deep buffer, upto 64 message will be received under interrupt control before a buffer overrun will occur.

then in the foreground you can leisurely check each frame from the buffer

I have many posts here describing my work, maybe you can search for them

Posted on October 06, 2017 at 00:07

1. You don't have to task switch on the interrupt, and you don't need to

use the HAL callback methodology.

are you saying that i should call HAL_CAN_Receive_IT and

HAL_CAN_Transmit_IT from the same thread (task) so i don't have to worry

about them being call at the same time?

Currently In 'HAL_CAN_RxCpltCallback' I am using 'msg_CAN_rx =

(CanRxMsgTypeDef *) osMailAlloc(CAN_rx_mail_box, 0);' to malloc the memory

needed to add to 'osMailPut(CAN_rx_mail_box, msg_CAN_rx);'. it works fine

if i send CAN messages slowly from my PC CAN tool (about 3 pre sec).

Do you recommend using a single task to Rx and Tx CAN messages, then use a

separate task to parse the CAN rx data? I can use the mail box to queue the

messages then pass it to the CAN_Rx_Parse() task.

thanks for the help.

On Thu, Oct 5, 2017 at 2:06 PM, Clive One <st-microelectronics@jiveon.com>

Posted on October 06, 2017 at 02:10

It is important to understand the callback is under interrupt context, so additional ones are unlikely to preempt, allocating memory or printing also should be avoided.

I'm not a huge fan of the HAL paradigm, and I'm saying don't be bound by it, or assume it is thread safe. Walk the library code and understand how it interacts with itself and the peripheral.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 19, 2017 at 19:42

thanks