Skip to main content
Associate II
September 16, 2025
Solved

STM32F042G6U CAN - Can't receive frames

  • September 16, 2025
  • 2 replies
  • 684 views

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

 

Best answer by mƎALLEm

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.

2 replies

Andrew Neil
Super User
September 16, 2025
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate II
September 16, 2025

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

YannDiPadova_0-1758015095136.png

 

mƎALLEm
Technical Moderator
September 16, 2025

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)

"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."
Associate II
September 16, 2025

Thanks, I will try and come back to you if it works (or if it doesn't)