cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405 CAN RX interrupt halts?

i-make-robots
Associate II

Hello, everyone.  I hope you're doing well.

my work at this time: https://github.com/MarginallyClever/Daisy-Driver-2.0-firmware/commit/465ec3dbeb831ee2f49fd6a6648da8fd7377a56a

My code is written in Arduino 2.2.1 with https://github.com/stm32duino/BoardManagerFiles/raw/master/package_stmicroelectronics_index.json.  It is an order of magnitude faster than STM32CubeIDE.

I have an STM32F405 communicating with 4 of its sister boards via CAN 1.0. When the master node boots it calls everyone to transmit their CAN IDs to enumerate the network.  I get sometimes 2, sometimes 3 frames, meaning a few have been lost.  From the status lights on the other devices I know they all received and replied.  I never get the CAN overflow (CAN1->RF0R & CAN_RF0R_FOVR0) bit on either mailbox.

I have also tried to write an RX interrupt.  This appears to halt the board - the light turns on once and all serial updates stop.  (see code below)

Do you know what I'm doing wrong with the interrupt?  Can you give me a hint why I'm losing frames?

Thank you!

----------------------

 

#ifdef CAN_ENABLE_RX0_INTERRUPT
  // attach message pending interrupt method
  NVIC_SetPriority(CAN1_RX0_IRQn, 1);
  NVIC_EnableIRQ(CAN1_RX0_IRQn);
  // Enable FIFO Message Pending Interrupt
  CAN1->IER |= CAN_IER_FMPIE0 | CAN_IER_FMPIE1;
  DEBUGLN("CAN1 interrupt enabled.");
#endif

int CANstate2=0,CANstate1=0;

void CAN1_RX0_IRQHandler(void) {
  CANstate2 = (CANstate2==0? 255 : 0);
  light.setColor(0,CANstate1,CANstate2);
  CAN1->RF0R |= CAN_RF0R_RFOM0;  // release FIFO
  CAN1->IER |= CAN_IER_FMPIE0;  // enable interrupt
}

void CAN1_RX1_IRQHandler(void) {
  CANstate1 = (CANstate1==0? 255 : 0);
  light.setColor(0,CANstate2,CANstate2);
  CAN1->RF1R |= CAN_RF1R_RFOM1;  // release FIFO
  CAN1->IER |= CAN_IER_FMPIE1;  // enable interrupt
}
4 REPLIES 4
Karl Yamashita
Lead II

Did you check with a CAN bus analyzer to see if the other CAN nodes indeed send a reply? If not, then it could have been a bus collision and they are not set for auto retransmission so they tried to send once and aborted. Post your code for the other nodes when you initialize the CAN controller.

If you find my answers useful, click the accept button so that way others can see the solution.

Hi Karl,

Instead of the diff in the OP, here's a direct link to the canbus class, including initialization: https://github.com/MarginallyClever/Daisy-Driver-2.0-firmware/blob/465ec3dbeb831ee2f49fd6a6648da8fd7377a56a/daisyDriver/CANBus.ino#L187.  I call this as "init(500000,2)".  All the devices are initialized the same, excepting their CAN id (which is set from dip switches)

You will see https://github.com/MarginallyClever/Daisy-Driver-2.0-firmware/blob/465ec3dbeb831ee2f49fd6a6648da8fd7377a56a/daisyDriver/CANBus.ino#L239C3-L239C22 says retransmission is on for both CAN1 and CAN2.

The halting problem was first reported to me in https://github.com/nopnop2002/Arduino-STM32-CAN/issues/60 by a very experienced CAN and STM developer.

Make sure you don't have ANY blocking code in your Interrupt Handlers or Callbacks.

Qualify the interrupt source on entry, save reentry issues, and tail-chaining NVIC propagation delay issues.

Make sure all sources are cleared prior to departure, otherwise you'll get an interrupt storm and no lower priority or foreground execution will occur.

Make sure you don't have any errors or stalls that would preclude further reception.

I've done some CAN work on F2, F4, but it's not something I'm currently invested in.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

All I'm doing in blinking an LED, so I'm pretty sure that's not blocking.

> Qualify the interrupt source on entry, save reentry issues, and tail-chaining NVIC propagation delay issues. Make sure all sources are cleared prior to departure, otherwise you'll get an interrupt storm and no lower priority or foreground execution will occur. Make sure you don't have any errors or stalls that would preclude further reception.

Pardon my ignorance, I don't know what any of that means.  At this point I'm just blinking a light, it shouldn't halt.