2024-03-25 04:59 AM
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:
The bus is running on 125kbits/s and is configured as follows:
I followed the instructions from this tutorial:
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!
Solved! Go to Solution.
2024-03-27 01:39 AM - edited 2024-03-27 01:42 AM
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):
Hope it does answer your question.
2024-03-25 06:42 AM - edited 2024-03-25 06:46 AM
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.
2024-03-25 06:48 AM - edited 2024-03-25 07:06 AM
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?
2024-03-25 08:05 AM
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
2024-03-25 08:23 AM
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
2024-03-25 11:23 AM
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?
2024-03-27 01:39 AM - edited 2024-03-27 01:42 AM
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):
Hope it does answer your question.
2024-03-27 03:43 AM
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!