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
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)