cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F4] Multiple CAN filters using HAL

ValentinG
Associate II

Good morning,

I'm using a CAN bus on my STM32F446RE. Right, now, the CAN communication is working with only one rx filter. I saw in datasheet that we can use up to 128 filters. I don't need so many filters, but I'm trying to configure a second filter. Unfortunately, when I do this, the MCU does not receive any message (although it compiles without error).

So my question is : how can I config two CAN filters with HAL library ?

Here is my code ( sFilterConfig2 is commented because it is the part that breaks my application) :

void Ifremer_MX_CAN_Init(void)
{
 
	/* USER CODE BEGIN CAN_Init 0 */
 
	/* USER CODE END CAN_Init 0 */
 
	/* USER CODE BEGIN CAN_Init 1 */
 
	/* USER CODE END CAN_Init 1 */
	hcan2.Instance = CAN2;
	hcan2.Init.Prescaler = 3;
	hcan2.Init.Mode = CAN_MODE_NORMAL;
	hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
	hcan2.Init.TimeSeg1 = CAN_BS1_11TQ;
	hcan2.Init.TimeSeg2 = CAN_BS2_2TQ;
	hcan2.Init.TimeTriggeredMode = DISABLE;
	hcan2.Init.AutoBusOff = DISABLE;
	hcan2.Init.AutoWakeUp = DISABLE;
	hcan2.Init.AutoRetransmission = DISABLE;
	hcan2.Init.ReceiveFifoLocked = DISABLE;
	hcan2.Init.TransmitFifoPriority = DISABLE;
	if (HAL_CAN_Init(&hcan2) != HAL_OK)
	{
		Error_Handler();
	}
	/* USER CODE BEGIN CAN_Init 2 */
	/*##-2- Configure the CAN Filter ###########################################*/
	CAN_FilterTypeDef  sFilterConfig;
	sFilterConfig.FilterIdHigh= (nodeID << 5);// ID 11-bit in top bits
	sFilterConfig.FilterIdLow= 0;
	sFilterConfig.FilterMaskIdHigh= 0b111111100000;// ID 11-bit in top bits,
	sFilterConfig.FilterMaskIdLow= 0;
	sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
	sFilterConfig.FilterBank= 0;//0
	sFilterConfig.FilterMode= CAN_FILTERMODE_IDMASK;
	sFilterConfig.FilterScale= CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterActivation= ENABLE;
	sFilterConfig.SlaveStartFilterBank = 0;//0
 
	HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig);
 
 /* CAN_FilterTypeDef  sFilterConfig2;
	sFilterConfig2.FilterIdHigh= (nodeID << 5);// ID 11-bit in top bits
	sFilterConfig2.FilterIdLow= 0;
	sFilterConfig2.FilterMaskIdHigh= 0b111111100000;// ID 11-bit in top bits,, mask for comparison FFE0
	sFilterConfig2.FilterMaskIdLow= 0;
	sFilterConfig2.FilterFIFOAssignment = CAN_FILTER_FIFO1;
	sFilterConfig2.FilterBank= 0;//0
	sFilterConfig2.FilterMode= CAN_FILTERMODE_IDMASK;
	sFilterConfig2.FilterScale= CAN_FILTERSCALE_32BIT;
	sFilterConfig2.FilterActivation= ENABLE;
	sFilterConfig2.SlaveStartFilterBank = 0;//0
 
	HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig2);*/
 
 
	/* Start the FDCAN module */
	if (HAL_CAN_Start(&hcan2) != HAL_OK)
	{
	/* Start Error */
	Error_Handler();
	}
 
 
	HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
	//HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
 
 
	/*##-5- Configure Transmission process #####################################*/
	// Configure header (standard id)
	TxHeader.StdId = address;
	TxHeader.IDE = CAN_ID_STD;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.DLC = 8;
 
  /* USER CODE END CAN_Init 2 */
 
}

0693W000006ESbbQAG.png 

Additionnal infos :

  • I use STM32Cube IDE
  • NUCLEO-F446 RE board

Thanks for reading?

Best regards,

Valentin

0 REPLIES 0