Skip to main content
Associate III
Feb 28, 2025 9:51 AM UTC
Solved

STM32F407 and CAN protocol in Loopback mode

Hello everyone,
I'm trying to use the loopback mode on the  STM32F4DISCOVERY. I configured the CAN peripheral and i started the transmission by adding data but when i try to verify the reception process i can't find data in the RxHeader. 

This topic has been closed for replies.
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
Feb 28, 2025 1:30 PM UTC

Thanks for sharing.

In fact the Rx call back was called but just once as you transmit once a CAN frame.

If you set a breakpoint in the call back before running, the callback is called.

So put the transmit call in the while loop:

 

 while (1)
 {
	 while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0);/* Wait till a Tx mailbox is free */
	 HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox);
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
 }
To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.

7 replies

mƎALLEm
ST Technical Moderator
Feb 28, 2025 10:37 AM UTC

Hello,

You missed to call HAL_CAN_ConfigFilter() to set the filtrer configuration:

 if (HAL_CAN_ConfigFilter(&hcan1, &FilterConfig) != HAL_OK)
 {
 /* Filter configuration Error */
 Error_Handler();
 }

 

 

Don't forget to add this line to Set the slave start  filter bank:

 

FilterConfig.SlaveStartFilterBank = 14;

 

+ Configure your CAN_Rx pin with pull-up resistor.

An example for your reference: https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_LoopBack

See also this article: Troubleshooting bxCAN issues in Loop Back mode on STM32 MCUs

Hope that solves your issue.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
AzizzAuthor
Associate III
Feb 28, 2025 12:03 PM UTC

Hello,
I'm using only CAN1. Do i need to set the slave start filter bank ?

mƎALLEm
ST Technical Moderator
Feb 28, 2025 12:52 PM UTC

In you case slave start filter bank needs to be set at least to 1. It's true the default value is 14 but set it anyway. 

Did you succeed to receive with the loopback mode?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
AzizzAuthor
Associate III
Feb 28, 2025 12:58 PM UTC

No, i didn't. I still have no value sent to RxData. 

mƎALLEm
ST Technical Moderator
Feb 28, 2025 1:05 PM UTC

Please share you project as zip and attach it to the discussion.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
AzizzAuthor
Associate III
Feb 28, 2025 1:12 PM UTC
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
Feb 28, 2025 1:30 PM UTC

Thanks for sharing.

In fact the Rx call back was called but just once as you transmit once a CAN frame.

If you set a breakpoint in the call back before running, the callback is called.

So put the transmit call in the while loop:

 

 while (1)
 {
	 while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0);/* Wait till a Tx mailbox is free */
	 HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox);
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
 }
To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.