Skip to main content
Explorer
January 3, 2025
Solved

CAN1 RX issue

  • January 3, 2025
  • 3 replies
  • 1867 views

Hi

I am using STM32F407 evb in which I have configured both CAN1 and CAN2 ,I am sendind CAN1(3 ids 501,502,503) and vice versa from CAN2 same I am sending but I am ble to recive all three id on CAN2 Rx but only one ID iam receving on CAN1(501) .I am sahring my filter configuration I am using mask mode 

 

if (can_controller==0){

		for (int i=0; i <CAN_MAXTABLEID_0; i++)
		{
			Can_Configfilter[i].FilterIdHigh = (0x501 << 5);
			Can_Configfilter[i].FilterMaskIdHigh =(0xFFC << 5);
			Can_Configfilter[i].FilterIdLow = 0x00000000; // Initialize if EXTID used
			Can_Configfilter[i].FilterMaskIdLow = 0x00000000; // Initialize if EXTID used
			Can_Configfilter[i].FilterFIFOAssignment = CAN_FILTER_FIFO_0;
			Can_Configfilter[i].FilterBank = i; //Selection of filterBANk
			Can_Configfilter[i].FilterMode = CAN_FILTERMODE_IDMASK; // Identifier mode
			Can_Configfilter[i].FilterScale = CAN_FILTERSCALE_32BIT; // Scale selection
			Can_Configfilter[i].FilterActivation = CAN_FILTER_ENABLE;// Example activation
			Can_Configfilter[i].SlaveStartFilterBank=1; //0-2 CAN1 filter

			Can_IPW_Can_ConfigFilter(&hcan,&Can_Configfilter[i]); //Filter configuration
		}
}


	if (can_controller==1){

			for (int i=1; i <CAN2_MAXTFILTER; i++)
			{
				Can_Configfilter[i].FilterIdHigh = (0x501 << 5);
				Can_Configfilter[i].FilterMaskIdHigh =( 0xFFC << 5);
				Can_Configfilter[i].FilterIdLow = 0x00000000; // Initialize if EXTID used
				Can_Configfilter[i].FilterMaskIdLow = 0x00000000; // Initialize if EXTID used
				Can_Configfilter[i].FilterFIFOAssignment = CAN_FILTER_FIFO_0;
				Can_Configfilter[i].FilterBank = i; //Selection of filterBANk
				Can_Configfilter[i].FilterMode = CAN_FILTERMODE_IDMASK; // Identifier mode
				Can_Configfilter[i].FilterScale = CAN_FILTERSCALE_32BIT; // Scale selection
				Can_Configfilter[i].FilterActivation = CAN_FILTER_ENABLE;// Example activation
				Can_Configfilter[i].SlaveStartFilterBank=1; //2-28 CAN2 filter

				Can_IPW_Can_ConfigFilter(&hcan,&Can_Configfilter[i]); //Filter configuration
			}
	}

 

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

    I've created a simple project where CAN1 and CAN2 are in loopback mode using STM32F407 Discovery board and I'm receiving well all the sent IDs: 0x502 (1281), 0x502 (1282), 0x503 (1283) on both sides CAN1 and CAN2:

    CAN1 Rx:

    SofLit_0-1735914523248.png

    CAN2 Rx:

    SofLit_1-1735914574561.png

    I've attached the project.

    I've also used that configuration and worked fine:

     

     sFilterConfig.FilterBank = 0;
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = (0x501 << 5);
     sFilterConfig.FilterIdLow = 0x0000;
     sFilterConfig.FilterMaskIdHigh = (0xFFC << 5);
     sFilterConfig.FilterMaskIdLow = 0x0000;
     sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
     sFilterConfig.FilterActivation = ENABLE;
     sFilterConfig.SlaveStartFilterBank = 1;
     
     if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
     {
     /* Filter configuration Error */
     Error_Handler();
     }
     
     sFilterConfig.FilterBank = 1;
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = (0x501 << 5);
     sFilterConfig.FilterIdLow = 0x0000;
     sFilterConfig.FilterMaskIdHigh = (0xFFC << 5);
     sFilterConfig.FilterMaskIdLow = 0x0000;
     sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
     sFilterConfig.FilterActivation = ENABLE;
     sFilterConfig.SlaveStartFilterBank = 1;
     
     if(HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
     {
     /* Filter configuration Error */
     Error_Handler();
     } 

     

    So I suspect something wrong in your specific implementation, either in your Can_IPW_Can_ConfigFilter() or with the handler hcan that was not set correctly in your software.

    Hope it helps.

    3 replies

    Technical Moderator
    January 3, 2025

    Hello,

    For "controller 1" (I think CAN1). Here i starts from 0:

    for (int i=0; i <CAN_MAXTABLEID_0; i++)

    For "controller 2" (I think CAN2). Here i starts from 1:

    for (int i=1; i <CAN2_MAXTFILTER; i++)

    So what are the values of CAN_MAXTABLEID_0 and CAN2_MAXTFILTER?

    I suspect an overlap of Filter config between CAN1 and CAN2!  -> CAN_MAXTABLEID_0 should not exceed 1

    Ash1Author
    Explorer
    January 3, 2025

    hi  

    value as follows 

    CAN_MAXTABLEID_0 =1, and CAN2_MAXTFILTER=2

    1. I Think filter configuration is fine .

    First I am sending 3 id from CAN1 then 3 id From CAN2 in this case CAn2 is receving all three but CAn1 only one id.

    2.than I interchange sending way this time I sent CAN2 first and CAN1 after ,in this case CAN2 receving only one and CAN1 receving all three IDs,

    I am sending in 10 ms function.

    Technical Moderator
    January 3, 2025

    For the moment use "pass all" IDs filters for CAN1:

    Can_Configfilter[i].FilterIdHigh = 0x0000;
    Can_Configfilter[i].FilterMaskIdHigh = 0x0000;
    Can_Configfilter[i].FilterIdLow = 0x0000; 
    Can_Configfilter[i].FilterMaskIdLow = 0x0000; 

    And see if you get all the messages intended to be received.

    mƎALLEmAnswer
    Technical Moderator
    January 3, 2025

    I've created a simple project where CAN1 and CAN2 are in loopback mode using STM32F407 Discovery board and I'm receiving well all the sent IDs: 0x502 (1281), 0x502 (1282), 0x503 (1283) on both sides CAN1 and CAN2:

    CAN1 Rx:

    SofLit_0-1735914523248.png

    CAN2 Rx:

    SofLit_1-1735914574561.png

    I've attached the project.

    I've also used that configuration and worked fine:

     

     sFilterConfig.FilterBank = 0;
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = (0x501 << 5);
     sFilterConfig.FilterIdLow = 0x0000;
     sFilterConfig.FilterMaskIdHigh = (0xFFC << 5);
     sFilterConfig.FilterMaskIdLow = 0x0000;
     sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
     sFilterConfig.FilterActivation = ENABLE;
     sFilterConfig.SlaveStartFilterBank = 1;
     
     if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
     {
     /* Filter configuration Error */
     Error_Handler();
     }
     
     sFilterConfig.FilterBank = 1;
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = (0x501 << 5);
     sFilterConfig.FilterIdLow = 0x0000;
     sFilterConfig.FilterMaskIdHigh = (0xFFC << 5);
     sFilterConfig.FilterMaskIdLow = 0x0000;
     sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
     sFilterConfig.FilterActivation = ENABLE;
     sFilterConfig.SlaveStartFilterBank = 1;
     
     if(HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
     {
     /* Filter configuration Error */
     Error_Handler();
     } 

     

    So I suspect something wrong in your specific implementation, either in your Can_IPW_Can_ConfigFilter() or with the handler hcan that was not set correctly in your software.

    Hope it helps.

    Ash1Author
    Explorer
    January 4, 2025

    Hi

    Can you attached your project so that I can too test on my board.

    I am running both can on 500kbps so will it be baudrate issue or not ?

    Doubt1: I have attached same code that you have tested I have one doubt you have added delay(10ms) in between HAL_CAN_ADDTXmessage. what if there is no 10ms delay in between  HAL_CAN_ADDTXmessage I mean to say CAn you try with delay at the end of both .will it work the same? Like I want to transmit both CAN1TX and CAN2TX in very 10ms delay

    what is the purpose of adding delay in between HAL_CAN_ADDTXmessage. 

    I have attached modified code CAN you test that too.

    Doubt2:: If I try to send 6 ID in single delay (10ms) without any delay between HAL_CAN_ADDTXmessage from CAN1 to CAN2  will all id will transmit or only first three I will receive on CAN2RX

     

     

     

     {
     CAN1_TxHeader.StdId = CAN1_Tx_ID_List[i];
     CAN1_TxData[0] ++;
     CAN1_TxData[1] --;
     if (HAL_CAN_AddTxMessage(&hcan1, &CAN1_TxHeader, CAN1_TxData, &CAN1_TxMailbox) != HAL_OK)
     {
     /* Transmission request Error */
     Error_Handler();
     } 
     CAN2_TxHeader.StdId = CAN2_Tx_ID_List[i];
     if (HAL_CAN_AddTxMessage(&hcan2, &CAN2_TxHeader, CAN2_TxData, &CAN2_TxMailbox) != HAL_OK)
     {
     /* Transmission request Error */
     Error_Handler();
     }
     HAL_Delay(10); 
     i++;
     if (i == 3) i = 0; 
    
     /* USER CODE END WHILE */
    
     /* USER CODE BEGIN 3 */
     }

     

     

     

     

     

     

    Technical Moderator
    January 6, 2025

    @Ash1 wrote:

    Hi

    Can you attached your project so that I can too test on my board.


    It's already attached:

    SofLit_0-1736152100679.png

    But this is tested on STM32F4Discovery board. You need to adapt it on your board, mainly the clocks and the CAN GPIOs.

    If you don't insert that delay you risk to get a non-empty Tx mailbox. Note that you have only 3 mailboxes on transmit and if you keep calling HAL_CAN_AddTxMessage() and the CAN has no enough time to drain the messages you send for a given reason (arbitration reason for example), you will risk to loose messages.

    Technical Moderator
    January 6, 2025

    You accepted the wrong comment:

    SofLit_0-1736163869198.png

    I will mark the nearest comment that answered your question as "Accepted as Solution".