cancel
Showing results for 
Search instead for 
Did you mean: 

Correct way to send CAN message inside interrupt

Rui Loureiro
Associate II
Posted on June 14, 2017 at 12:45

I'm using an STM32F4 Discovery and an MCP2562 transceiver. I have CAN working using the CAN2 port.

In my implementation, I'm sending several CAN messages inside a timer interrupt. I'm using the HAL function HAL_CAN_TRANSMIT_IT.  It works fine until I try to send 3 or more messages in a row, in that case HAL_CAN_TRANSMIT_IT returns an error. How should I work around this?

I thought about using HAL_CAN_TRANSMIT, which has the option to use a timeout, but since this is called inside an interrupt, this should be avoided.

Any help will be greatly appreciated.

#hal #can #stm32f4-discovery
5 REPLIES 5
Nicolas Cmb
Associate III
Posted on June 14, 2017 at 13:23

Hi

Loureiro.Rui

,

Did you try to see some exmples on theSTM32_F4_*** files, wich probably has projects using the CAN communication ?

Regards ,

Nicolas

Kraal
Senior III
Posted on June 14, 2017 at 13:34

Hi !

I can't comment on the F4 but if the peripheral is the same as F0, then you have only 3 Tx mailboxes. So if you want to send more than 3 CAN messages in a row, you can either poll the mailboxes until one becomes free again or use a circular buffer with interrupts if you don't want to waste CPU cycles.

Posted on June 14, 2017 at 13:55

I'm using the STM32F1 family and it's the same things (3 Tx Mailboxes), so maybe the STM32 F4 has the same abilities.

Posted on June 14, 2017 at 14:24

Didn't check it, but I'm pretty sure it's the same peripheral.

IMHO another case where the Cube SW structure clashes with the application requirements.

I would organize CAN send receive and send in a SW queue, and just trigger the send from an interrupt (i.e. setting a flag for the queue to start the send).

The queue could be polled/processed at regular intervals (timer interrupt), or event driven, depending on your requirements.

Advantage is, this SW queue can be arbitrary long, not restricted to the implemented hardware.

I have no implementation for STM32 here, though.

Jeroen3
Senior
Posted on June 15, 2017 at 09:31

Add more mailboxes in software. See if there are any software mailboxes marked for transmit on tx complete irq.