# STM32 CAN bus Rx interrupt only triggers in loopback mode

I'm designing the CAN interface for a STM32F042C4T6 to be able to receive and send messages. I've tested my code and everything seems to work perfectly fine in loopback mode. However, when I hook up two STM32's together (with CAN transceivers on both ends as middle men) with one of them transmitting and the other receiving, the STM32 that's supposed to be receiving never enters the receive interrupt. I am sure that the correct CAN message is being sent using my Digilent Analog Discovery board that is able to receive digital CAN, and I probed the CAN Rx pin on the STM32 that is receiving and the message is correct. When I saw this, I thought there might be something wrong with my GPIO initialization code, but it looks like it should work fine. This is it :

//GPIO setup for CAN Tx and Rx
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; //Enable the GPIOB clock
GPIOB->AFR[1] |= (0b0100 << GPIO_AFRH_AFSEL8_Pos) | (0b0100 << GPIO_AFRH_AFSEL9_Pos); //Set PB8 and PB9 to AF4 for CAN
GPIOB->OTYPER &= ~(GPIO_OTYPER_OT_8 | GPIO_OTYPER_OT_9); //Set PB8 and PB9 to push pull
GPIOB->OSPEEDR |= (0b01 << GPIO_OSPEEDR_OSPEEDR8_Pos) | (0b01 << GPIO_OSPEEDR_OSPEEDR9_Pos); //Set PB8 and PB9 to medium speed
GPIOB->MODER |= (0b10 << GPIO_MODER_MODER8_Pos) | (0b10 << GPIO_MODER_MODER9_Pos); //Set PB8 and PB9 to alternate function mode


Another thing that I think might be wrong is that the CAN transceiver outputs 5V digital signals and this is a 3.3V microcontroller, STM32F042C4T6, but I checked, and it is stated in the documentation that these pins are 5V tolerant I/O, which means it should be fine right?

Another thing that I think is very unlikely to be wrong, but still a possibility because I can't really think of anything else is that maybe one the STM32s that is transmitting is sending data out at possibly 1 kHz different than the other STM32 that is receiving, and the CAN message is interpreted incorrectly. However, the bit timing register for both MCU's is the exact same so this is also unlikely.

Does anyone have any ideas as what I could be doing wrong here? If you need for me to post any code I would be glad too, as this is quite important.

## 2 Answers

I actually fixed the issue, thank you to Michel for trying to help me, the reason it wasn't working was because I didn't have the correct filter enabled because it seems the data sheet only that there are 14 filters to be enabled (incorrectly) but the header file specifies 28 correctly.

• Upvoted your answer (for helping others too), btw, you can accept your answer. – Michel Keijzers Mar 27 '19 at 8:03

I had the same problem as you, however, it seems you are not using HAL/CubeMX or do you? If so, make sure you have the latest version of the HAL library for the STM32F103C8T6 library, because a bug has been fixed that indeed it only worked in loopback mode. Update the HAL F1 library to 1.7 (or higher), where the CAN implementation has been fixed/changed.

If the above does not apply:

• Check if you use the correct resistors (120 ohm) at the right places
• That you use at least two (one receiver and one transmitter CAN); with one CAN device the CAN bus stops almost immediately.
• Thanks for the reply, I am not using the HAL library. I am using two 120 ohm resistors, (its just a 2 device network), each resistor is between the CANH and CANL lines before the CAN transceivers. Also, I am using one transmitter and one receiver and have verified that the CAN transmitter is working perfectly as the message that is continuously sending out is indeed arriving at the receiver's CAN Rx pin, just the Rx interrupt never triggers for some reason. – achilles7744 Mar 25 '19 at 18:04
• I also know that some STM devices (afaik STM32F103C8T6) cannot handle extended or the entire range of extended IDs. – Michel Keijzers Mar 25 '19 at 19:53