cancel
Showing results for 
Search instead for 
Did you mean: 

CAN receive doesn't work

JKris.17
Associate II

Hello everyone, 

I'm currently doing some experiments with CAN communication with the STM32F302RB Nucleo board. The board is connected to a wcmcu-230 CAN transceiver which is connected to a Raspberry pi with a CAN header connected to it. The bus is terminated with a 120Ohm resistor on both sides.

I generated the HAL with CubeMX. RX1 interrupt is enabled and the pins are left as default:

JKris17_0-1711367038657.png

The bus is running on 125kbits/s and is configured as follows:

JKris17_1-1711367110408.png

I followed the instructions from this tutorial:

https://www.linkedin.com/pulse/unlocking-power-stm32-can-bus-step-by-step-tutorial-haresh-sondagar-whwcf/

I use the following code:

CAN_RxHeaderTypeDef rxHeader; //CAN Bus Transmit Header
CAN_TxHeaderTypeDef txHeader; //CAN Bus Receive Header
uint8_t canRX[8] = {0,0,0,0,0,0,0,0};  //CAN Bus Receive Buffer
CAN_FilterTypeDef canfil; //CAN Bus Filter
uint32_t canMailbox; //CAN Bus Mail box variable

//in main:
canfil.FilterBank = 0;
canfil.FilterMode = CAN_FILTERMODE_IDMASK;
canfil.FilterFIFOAssignment = CAN_RX_FIFO0;
canfil.FilterIdHigh = 0;
canfil.FilterIdLow = 0;
canfil.FilterMaskIdHigh = 0;
canfil.FilterMaskIdLow = 0;
canfil.FilterScale = CAN_FILTERSCALE_32BIT;
canfil.FilterActivation = ENABLE;
canfil.SlaveStartFilterBank = 14;

txHeader.DLC = 8; // Number of bites to be transmitted max- 8
txHeader.IDE = CAN_ID_STD;
txHeader.RTR = CAN_RTR_DATA;
txHeader.StdId = 0x030;
txHeader.ExtId = 0x02;
txHeader.TransmitGlobalTime = DISABLE;

HAL_StatusTypeDef res = HAL_CAN_ConfigFilter(&hcan,&canfil); //Initialize CAN Filter

res = HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING);// Initialize CAN Bus Rx Interrupt
res = HAL_CAN_Start(&hcan); //Initialize CAN Bus
uint8_t csend[] = {'H','E','L','L','O'}; // Tx Buffer
res =  HAL_CAN_AddTxMessage(&hcan,&txHeader,csend,&canMailbox); // Send Message

//At the end of main.c:
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan1)
{
    HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO0, &rxHeader, canRX); //Receive CAN bus message to canRX buffer
    HAL_GPIO_TogglePin(GPIOb, GPIO_PIN_13);// toggle LED

}

I receive the HELLO message on the pi, so I conclude that the hardware setup is properly done. However, when I send a message from the Pi, it is never received on the stm side. I played around but the interrupt was never received. I added a second Pi and there I could actually see the message. So I'm sure the message is sent from the Pi. 

Can someone please help me with this issue? I'm quite lost in this. Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Let's hope your scheduler works well 😎

 

 It's working well with some latencies 😁.

But looking at your code, I found the issue:

You're using FIFO0 while, the interrupt is configured for FIFO1. So you need to activate this interrupt for FIFO 0 (RX0):

SofLit_0-1711528746899.png

Hope it does answer your question.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
SofLit
ST Employee

Hello,

Just to confirm about your transceiver:

does the module wcmcu-230 has SN65HVD230 on it?

Could you please share your minimal project focusing on CAN? If you can't share it in public, send it by message.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
SofLit
ST Employee

Just to be sure that the message is well transmitted to the MCU, could you please check on CAN_RX pin with an oscilloscope if there is a CAN activity?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Hi SofLit,

 

Thanks for your support. Yes, it should be the SN65HVD230. It this module:  https://nl.aliexpress.com/item/32706875545.html

I created an empty project with only the can code. Hopefully it is helpful to you.

At this moment, I don't have a proper scope at hand. As the messages are well received by my Pi, My assumption is that the messages are sent correctly.  

Thanks again 🙂

SofLit
ST Employee

SN65HVD230 is perfect as a transceiver. 

You need to check if your transceiver is OK. So you need to check CAN_Rx to be sure that the frame arrives correctly to the MCU.

PS: I have at least 3 CAN threads to manage at the same time here in the community I have to "restore the context for each thread" to remember each thread config 😊

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Let's hope your scheduler works well 😎

I'll check the signals later this week. At this moment I don't have a scope available. 

Just to be sure some questions:
* Do you see any obvious issues in my project?
* When I disconnect the transceiver and set the mode to (silent) loopback, I should be able to receive an interrupt when I send a message, correct? I don't get this working, so this might point to an issue in the code itself.
* I currently assume that all ID's will be passed through the filter by default (so no call to HAL_CAN_ConfigFilter). Is this correct? If so, I'll leave this out for now as it cancels out the possible effect of the HW filters. 
* Is the configuration of the RX pin correct? I remember from earlier projects that the RX pin was configured as input, but this option is not available in the current cubeMx tool. Is a Pull-up required on the RX pin?

 

 

Let's hope your scheduler works well 😎

 

 It's working well with some latencies 😁.

But looking at your code, I found the issue:

You're using FIFO0 while, the interrupt is configured for FIFO1. So you need to activate this interrupt for FIFO 0 (RX0):

SofLit_0-1711528746899.png

Hope it does answer your question.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

HI SofLit,

That made my day! I took the settings from some other tutorial. I overlooked the configuration in the tutorial I was referring to. 

Many thanks!