Skip to main content
Graduate
March 14, 2025
Solved

STM32G431 FDCAN filter doesn't work.

  • March 14, 2025
  • 3 replies
  • 737 views

Hi guys.

I am using STM32G431KBT MCU. I am using 2 MCU (master / slave) and they communicate via canbus. Canbus is working good but canbus filter is not working. Master canbus address is 0x200 ans slave address is 0x101. I changing master ID (0x150) but the slave still continues to receive data and go to the canbus callback program. Cubemx settings are as follows;

Cubemx1.pngCubemx2.png

And program is here;

 

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_ADC1_Init();
 MX_FDCAN1_Init();
 MX_TIM2_Init();
 /* USER CODE BEGIN 2 */
	Configure_FDCAN_Filter();
	if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK) {
			Error_Handler();
	}
	if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK) {
			Error_Handler();
	}
	Config_CANTX();
void Configure_FDCAN_Filter(void)
{
 FDCAN_FilterTypeDef sFilterConfig;
 sFilterConfig.IdType = FDCAN_STANDARD_ID; // Standart 11-bit ID
 sFilterConfig.FilterIndex = 0;
 sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
 sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
 sFilterConfig.FilterID1 = 0x200; // Filtre ID
 sFilterConfig.FilterID2 = 0x200; // Maske (sadece 0x200 adresini kabul et.)
 if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK) {
 Error_Handler();
 }
}
void Config_CANTX(void)
{
	TxHeader.Identifier = CANBUS_ID;
	TxHeader.IdType = FDCAN_STANDARD_ID;
	TxHeader.TxFrameType = FDCAN_DATA_FRAME;
	TxHeader.DataLength = FDCAN_DLC_BYTES_8; // 8 bayt veri
	TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
	TxHeader.BitRateSwitch = FDCAN_BRS_OFF; // CAN FD degil, klasik CAN
	TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
	TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
	TxHeader.MessageMarker = 0;
}

 

I tried the every filter type (mask, dual, range) but Canbus filter is doesn't work and accepts all addresses. I am watching RxHeader in debug watch. RxHeader.Identifier is 0x00000150 and program goes to HAL_FDCAN_RxFifo0Callback with every new message. Whats is the problem?

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    1 - There is no Master-Slave concept in CAN bus all nodes have the same privilege. 

    2-  You forgot to call HAL_FDCAN_ConfigGlobalFilter() after the filter config:

     

     if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
     {
     Error_Handler();
     }

     

    3- To receive only the ID 0x101:

     

     sFilterConfig.FilterType = FDCAN_FILTER_MASK;
     sFilterConfig.FilterID1 = 0x101;
     sFilterConfig.FilterID2 = 0x7FF;

     

      Hope that helps.

    3 replies

    Explorer
    March 14, 2025

    Sorry, but you totally misunderstood the CAN bus.

    > I am using STM32G431KBT MCU. I am using 2 MCU (master / slave) and they communicate via canbus.

    There are no "masters" and "slaves" on the CAN bus.
    Only messages have priorities, not nodes.

    > Master canbus address is 0x200 ans slave address is 0x101. I changing master ID (0x150) but the slave still continues to receive data and go to the canbus callback program.

    These are "message IDs", not CAN bus addresses.
    In fact, there is no such thing as "CAN bus addresses" - consult the CAN standard.

    Every node will always receive every valid/correct CAN frame.
    A node might drop those messages which do not match the filter settings. But only after the peripheral unit received and acknowledget it.

    The STM32 CAN filter settings are somewhat tricky, I never dealt with CAN implementations on STM32.
    But you will probably find some helpful examples.

    mƎALLEmAnswer
    Technical Moderator
    March 14, 2025

    Hello,

    1 - There is no Master-Slave concept in CAN bus all nodes have the same privilege. 

    2-  You forgot to call HAL_FDCAN_ConfigGlobalFilter() after the filter config:

     

     if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
     {
     Error_Handler();
     }

     

    3- To receive only the ID 0x101:

     

     sFilterConfig.FilterType = FDCAN_FILTER_MASK;
     sFilterConfig.FilterID1 = 0x101;
     sFilterConfig.FilterID2 = 0x7FF;

     

      Hope that helps.

    ABURMAuthor
    Graduate
    March 14, 2025

    Hi guys.

    I know how canbus works. In my previous experiments, I used 2 MCUs. That's why I didn't need filtering. However, in the project I'm working on, 1 MCU communicates with 20 MCUs over the canbus line and sends the data it collects to the PC via UART. The other MCUs only need to process the data coming from the MCU that communicates with the PC. That's why filtering is very important in this project. That's why I used the terms master and slave. 
    I was beginning to think that there was a bug in cubemx or the HAL libraries, so I manually added the following lines to the program. It worked when I added these lines.

     volatile uint32_t *rxgfc = (volatile uint32_t *)(0x40006400 + 0x0080); // 0x40006480
     *rxgfc = (1 << 5) | (1 << 16); // F0OM = 1, ANFS = 2, LSS = 1


    But I missed the "HAL_FDCAN_ConfigGlobalFilter".Thanks @mƎALLEm, It's working.