cancel
Showing results for 
Search instead for 
Did you mean: 

FDCAN on STM32G0 Filter setup

JD3
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

8 REPLIES 8
SofLit
ST Employee

Hello @JD3 ,

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

hfdcan2.Init.StdFiltersNbr = 0;

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
JD3
Associate II

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

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

 

 

Karl Yamashita
Lead III

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

Or, you're not showing all your code.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
JD3
Associate II

Thanks my mistake, i have posted wrong one but i initialize both FDCAN1 and 2 but using only FDCAN1. Is there any relation to filtering if using both?

 

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_FDCAN1_Init();
  MX_I2C1_Init();
  MX_I2C3_Init();
  MX_TIM1_Init();
  MX_TIM3_Init();
  MX_USART3_UART_Init();
  MX_SPI2_Init();
  MX_ADC1_Init();
  MX_FDCAN2_Init();
  MX_TIM2_Init();
  MX_USB_DRD_FS_PCD_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
/**
  * @brief FDCAN1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_FDCAN1_Init(void)
{

  /* USER CODE BEGIN FDCAN1_Init 0 */

  /* USER CODE END FDCAN1_Init 0 */

  /* USER CODE BEGIN FDCAN1_Init 1 */

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

  /* USER CODE END FDCAN1_Init 2 */

}

/**
  * @brief FDCAN2 Initialization Function
  * @PAram None
  * @retval None
  */
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 */

}

 

 

I think there must be something with this if uncomented nothing passes:

// if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1,

// FDCAN_REJECT,

// FDCAN_REJECT,

// FDCAN_REJECT_REMOTE,

// FDCAN_REJECT_REMOTE) != HAL_OK)

// {

// Error_Handler();

// }

 

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

You need to configure the HAL driver for the number of filters you are going to use

 

// You can add up to 28 filters for STD ID, and up to 8 filters for EXT ID.


hfdcan1.Init.StdFiltersNbr = 28;
hfdcan1.Init.ExtFiltersNbr = 8;

hfdcan2.Init.StdFiltersNbr = 28; 
hfdcan2.Init.ExtFiltersNbr = 8;


// Or, if you have only 2 STD ID and 0 EXT ID
hfdcan1.Init.StdFiltersNbr = 2;
hfdcan1.Init.ExtFiltersNbr = 0;

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
// Or, if you have only 2 STD ID and 0 EXT ID
hfdcan1.Init.StdFiltersNbr = 2;
hfdcan1.Init.ExtFiltersNbr = 0;

REason was that i did not include it to 

static void MX_FDCAN1_Init(void)
{

  /* USER CODE BEGIN FDCAN1_Init 0 */

  /* USER CODE END FDCAN1_Init 0 */

  /* USER CODE BEGIN FDCAN1_Init 1 */

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

  /* USER CODE END FDCAN1_Init 2 */

}

respectively did not set it in .ioc  .. now everything works fine