STM32F4 CAN Bus Receive Interrupts
Posted: Tue May 11, 2021 4:19 am
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:
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:
Next in my main ino file, I have implemented these:
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?
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);
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?