Skip to main content
Visitor II
September 23, 2024
Solved

FDCAN on STM32G0 Filter setup

  • September 23, 2024
  • 2 replies
  • 2594 views

Hello,

is there any simple example for (STM32G0B1CBT6) or similar where I can simply filter int genration for eg. only for 0x505 id in standart mode. I tested many samples and I believe that there must be some bug in IDE. This is 

 

static void MX_FDCAN2_Init(void)
{

 /* USER CODE BEGIN FDCAN2_Init 0 */

 /* USER CODE END FDCAN2_Init 0 */

 /* USER CODE BEGIN FDCAN2_Init 1 */

 /* USER CODE END FDCAN2_Init 1 */
 hfdcan2.Instance = FDCAN2;
 hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1;
 hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
 hfdcan2.Init.Mode = FDCAN_MODE_NORMAL;
 hfdcan2.Init.AutoRetransmission = DISABLE;
 hfdcan2.Init.TransmitPause = DISABLE;
 hfdcan2.Init.ProtocolException = DISABLE;
 hfdcan2.Init.NominalPrescaler = 16;
 hfdcan2.Init.NominalSyncJumpWidth = 1;
 hfdcan2.Init.NominalTimeSeg1 = 2;
 hfdcan2.Init.NominalTimeSeg2 = 2;
 hfdcan2.Init.DataPrescaler = 1;
 hfdcan2.Init.DataSyncJumpWidth = 1;
 hfdcan2.Init.DataTimeSeg1 = 1;
 hfdcan2.Init.DataTimeSeg2 = 1;
 hfdcan2.Init.StdFiltersNbr = 0;
 hfdcan2.Init.ExtFiltersNbr = 0;
 hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
 if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN FDCAN2_Init 2 */

 /* USER CODE END FDCAN2_Init 2 */

}
 // CAN initialization
 FDCAN_FilterTypeDef sFilterConfig;

 /* Nastavení počtu filtrů pro standardní ID */
 hfdcan1.Init.StdFiltersNbr = 1;
 hfdcan1.Init.ExtFiltersNbr = 0;

 sFilterConfig.IdType = FDCAN_STANDARD_ID;
 sFilterConfig.FilterIndex = 0; // Use the first filter bank
 sFilterConfig.FilterType = FDCAN_FILTER_MASK; // Specify the filter type
 sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; // Store matching messages in RX FIFO 0
 sFilterConfig.FilterID1 = 0x505; // filter
 sFilterConfig.FilterID2 = 0x1FF; // mask
 if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
 {
 Error_Handler();
 }

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

 /* Spuštění FDCAN modulu */
 if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
 {
 Error_Handler();
 }

 /* Aktivace přerušení pro FIFO 0 nové zprávy */
 if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
 {
 Error_Handler();
 }

 // CAN initialization END

 

I have played and tried many changes in  sFilterConfig.FilterID2 = 0x1FF; // mask 0x505 and other items.  If I comment or delete HAL_FDCAN_ConfigGlobalFilter then any ID generates interrupt data comes from all id  correctly but it does not filter for 0x505. if using  HAL_FDCAN_ConfigGlobalFilter then it does not generate any int doesnt receive.  

Instead of filter Iam using this method which is not very nice but working for now 

 

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{
	FDCAN_RxHeaderTypeDef 		FDCANRxHeader;
 if((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET)
 {
 /* Retrieve Rx messages from RX FIFO0 */
 if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &FDCANRxHeader, canrx_obj.data) != HAL_OK)
 {
 Error_Handler();
 }
	canrx_obj.id =FDCANRxHeader.Identifier;
	canrx_obj.len =FDCANRxHeader.DataLength;

 /* Display LEDx */
 if ((FDCANRxHeader.Identifier == 0x505) && (FDCANRxHeader.IdType == FDCAN_STANDARD_ID))
 {
 lt;
 //LED_Display(RxData[0]);
 //ubKeyNumber = RxData[0];
 }
 }
}

 

Where is the mistake whats wrong ?

Is it compiler bug somwhere?

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

    But here again Standard filter number is set to 0! for both FDCAN1 and FDCAN2:

     hfdcan1.Init.StdFiltersNbr = 0;
     hfdcan1.Init.ExtFiltersNbr = 0;

     


    @JD3 wrote:

    Is there any relation to filtering if using both?


    No.

    2 replies

    Technical Moderator
    September 23, 2024

    Hello @JD3 ,

    Your standard filter number is set to 0. Need to set it at least to 1.

    hfdcan2.Init.StdFiltersNbr = 0;

     

     

    JD3Author
    Visitor II
    September 23, 2024

    I think it is set to 1 as iam not using extended id, using only FDCAN1: 

     hfdcan1.Init.StdFiltersNbr = 1;
     hfdcan1.Init.ExtFiltersNbr = 0;

     

     

    Technical Moderator
    September 24, 2024

    Confusing code:


    @JD3 wrote:

    I think it is set to 1 as iam not using extended id, using only FDCAN1: 

     hfdcan1.Init.StdFiltersNbr = 1;
     hfdcan1.Init.ExtFiltersNbr = 0;

    Please review your original post:

    SofLit_0-1727167753044.png

    Also there are two CAN instances used. You need to clarify exactly what is the problem and on which instance and need to share your code.

    Graduate II
    September 23, 2024

    You've initiated for hfdcan2 but you're setting the filter for hfdcan1.

    Or, you're not showing all your code.