Skip to main content
Graduate II
February 28, 2025
Solved

STM32F407 and CAN protocol in Loopback mode

  • February 28, 2025
  • 4 replies
  • 1401 views

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.
    Best answer by mƎALLEm

    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 */
     }

    4 replies

    Technical Moderator
    February 28, 2025

    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.

    AzizzAuthor
    Graduate II
    February 28, 2025

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

    Technical Moderator
    February 28, 2025

    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?

    AzizzAuthor
    Graduate II
    February 28, 2025

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

    Technical Moderator
    February 28, 2025

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

    mƎALLEmAnswer
    Technical Moderator
    February 28, 2025

    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 */
     }