2025-09-16 2:13 AM - last edited on 2025-09-16 2:23 AM by Andrew Neil
Hello,
I have an issue with the CAN bus on STM32F042G6 µC.
For the moment I just want to receive Frames from an other controller (STM32G0B1) and it doesn't work at all.
I am using PA11 as Rx Pin and PA12 as Tx Pin Pins.
Here is the configuration part with a 12MHz external clock :
static void MX_CAN_Init(void)
{
/* USER CODE BEGIN CAN_Init 0 */
/* USER CODE END CAN_Init 0 */
/* USER CODE BEGIN CAN_Init 1 */
/* USER CODE END CAN_Init 1 */
hcan.Instance = CAN;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = DISABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = DISABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN_Init 2 */
/* USER CODE END CAN_Init 2 */
}
I tried to read the frame sent with this code :
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_CAN_Init();
if(HAL_CAN_Start(&hcan) != HAL_OK)
Error_Handler();
if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
Error_Handler();
}
while (1)
{
HAL_Delay(500);
}
}
I don't understand why but it is impossible for me to go in the HAL_CAN_RxFifo0MsgPendingCallback function when a message is received. I checkes the RX Pin with an oscilloscope and the CAN frame is correct on this Pin.
I already found this topic https://community.st.com/t5/stm32-mcus-products/stm32f042g6ux-can-issue/td-p/675078 and follow what was said but nothing works. I don't have any filters, I enabled interrupts for CAN on NVIC menu and in CAN menu. My Rx pin is set to Pull-Up and both my µC have the same exact configuration.
I send a the configuration of my CAN, NVIC and GPIO in .ioc file on attached image.
Do you have any idea why it doesn't work ?
Thank you in advance for your reply,
Yann Di Padova
Solved! Go to Solution.
2025-09-16 4:49 AM - edited 2025-09-16 4:51 AM
Ok,
1- I don't see any filter config in the F0 code. If there is no filter config you can't receive any message even if all the settings are fine. At least a very basic filter passing all the frames needs to be set. As you are using FIFO0, this is a basic filter set for it:
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
2- If the filter is not the issue and to avoid unnecessary ping pongs, I propose you to attach both projects. If you can't publish them in public, send them in private. Otherwise I can't help you efficiently.
2025-09-16 2:25 AM
Please give hardware details - see:
How to write your question to maximize your chances to find a solution
2025-09-16 2:29 AM - edited 2025-09-16 2:36 AM
Hello,
At a first glance:
These are bad timing parameters:
hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
You need to increase BS1 and BS2 as much as possible with a sample point at 87%.
I invite you to use this web tool for CAN timing computation: http://www.bittiming.can-wiki.info/.
I also recommend to read these articles:
Using CAN (bxCAN) in Normal mode with STM32 microcontrollers (Part 1)
Using CAN (bxCAN) in Normal mode with STM32 microcontrollers (Part 2)
2025-09-16 2:32 AM
Hello, I'm using a STM32F042G6U6.
I'm also using STM32CubeIDE with STM32Cube MCU Package for STM32F0 : Version 1.11.5
My other µC used to send the frames is a STM32G0B1CBU6
In attachment, here's my hardware connection. CAN TX is directly connected to PA12 and CAN RX to PA11
2025-09-16 2:34 AM
Thanks, I will try and come back to you if it works (or if it doesn't)
2025-09-16 4:23 AM
I tried to change the Time Quanta in Segment 1 to 13 and 2 on Segment 2 following the recommandation of your web tool (12MHz clock, 250kbit/s). Unfortunately it doesn't work.
I also changed these parameters for my STM32G0B1CBU6 that send the frames for the exact same parameters.
I read the articles you sended but I discovered nothing more that I didn't tried.
I really don't know what do to make it work.
Here's my configuration for my STM32G0B1CBU6, in case there's something wrong with it (I checked on an oscilloscope and everything seems fine but maybe I'm wrong, also FDCAN1 and FDCAN2 have exactly the same parameters, I'm using both and both seems to send frames).
2025-09-16 4:35 AM
Which node is not receiving the CAN frame? from the title I understand that STM32F042G6U is having that issue,
But from your screenshot it seems you have an issue from STM32G0 side since both filter numbers are set to 0:
You need to set at least 1 filter to receive (standard or extended ID)!
2025-09-16 4:43 AM
The node not receiving the can frame is the STM32F042G6U6
The node sending the can frame is the STM32G0B1CBU6. I send to you an oscilloscope screenshot proving it sends a Frame (a bit seems to be 4µs which is correct for a 250kb/s frequency i think ? that's why i supposed STM32F042G6U6 was the issue here)
2025-09-16 4:49 AM - edited 2025-09-16 4:51 AM
Ok,
1- I don't see any filter config in the F0 code. If there is no filter config you can't receive any message even if all the settings are fine. At least a very basic filter passing all the frames needs to be set. As you are using FIFO0, this is a basic filter set for it:
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
2- If the filter is not the issue and to avoid unnecessary ping pongs, I propose you to attach both projects. If you can't publish them in public, send them in private. Otherwise I can't help you efficiently.
2025-09-16 4:55 AM
It seems both filters and quantas were the issue. It works well now.
Thanks a lot for your help