STM32F4 CAN Bus Receive Interrupts

Post Reply
Tazzi
Posts: 20
Joined: Wed Jan 08, 2020 12:45 am

STM32F4 CAN Bus Receive Interrupts

Post by Tazzi »

I am currently working with a STM32F446RET6 processor, I have both CAN1 and CAN2 working correctly for receiving and writing using polling method which works flawlessly.

Next is getting the receive interrupt working which unfortunately cannot seem to get going.
Looking at the stm32f4xx_hal_can.c file, it explains:
(++) In case of using interrupts (e.g. HAL_CAN_ActivateNotification())
(+++) Configure the CAN interrupt priority using
HAL_NVIC_SetPriority()
(+++) Enable the CAN IRQ handler using HAL_NVIC_EnableIRQ()
(+++) In CAN IRQ handler, call HAL_CAN_IRQHandler()

So, I have doe the following in my initialization code:

Code: Select all

HAL_NVIC_SetPriority(CAN2_TX_IRQn, 0, 0);
	HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
	HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 0, 0);
	HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
	HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);

A filter is set for CAN2 (I have called this CANBusHSCAN) on FIFO0 and the slavefilterstart set to 15.
Then when read I start the canbus and interrupts with:

Code: Select all

HAL_CAN_Start(&CANBusHSCAN);
HAL_CAN_ActivateNotification(&CANBusHSCAN, CAN_IT_RX_FIFO0_MSG_PENDING);
Next in my main ino file, I have implemented these:

Code: Select all

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	USBSerial.println("to Fifo1 callback");
}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	USBSerial.println("to Fifo0 callback");
}

void CAN2_RX1_IRQHandler(void) { 
HAL_CAN_IRQHandler(&CANBusHSCAN);
USBSerial.println("rx1");
}

void CAN2_RX0_IRQHandler(void) { 
HAL_CAN_IRQHandler(&CANBusHSCAN);
USBSerial.println("rx0");
}

I know I required the CallBack functions, which I have just thrown a quick serial print to tell if its fired or not. The bottom two functions I have found online and am unsure if this is the indicated "In CAN IRQ handler, call HAL_CAN_IRQHandler()".
Currently what happens is as soon as the can frame that meets the filter is found, the processor is completely freezing. I am unsure if I have missed something, or just completely done this incorrectly?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F4 CAN Bus Receive Interrupts

Post by fpiSTM »

Add before each functions definition:

Code: Select all

 extern "C" 
ino file is converted as cpp file.
Tazzi
Posts: 20
Joined: Wed Jan 08, 2020 12:45 am

Re: STM32F4 CAN Bus Receive Interrupts

Post by Tazzi »

Thanks for that,

the extern C is only required for the interrupt.

In saying that the "/* Aliases for __IRQHandler */" for canbus are not defined in the stm32f446xx.h file so it doesnt seem to ever call them. (https://github.com/stm32duino/Arduino_C ... xx/Include).

After adding those in, It finally called CAN2_RX0_IRQHandler(void)

To which you then call the HAL_CAN_IRQHandler

And finally inside this premade function, it will check all interrupt registers for which have been enabled and will process each one individually.

If one gets enabled that you have not made a function for, the processor will go into an infinite loop.

Also, when processing incoming messages, you MUST do a HAL_CAN_GetRxMessage() otherwise it will go into an infinite loop jumping in and out of the interrupt routine.
Post Reply

Return to “STM32F4 based boards”