2024-11-26 02:12 AM - last edited on 2024-11-26 07:50 AM by SofLit
I have developed a custom board with an STM32G474RE controller. Since the controller only features FDCAN, I am using it in normal mode. Connected to this is a CAN controller (not FD). Sending and receiving messages work without any issues. However, as several nodes will be connected to the bus later, I use extended IDs and want to filter the received messages using a mask, as some information is encoded bitwise in the identifier. The controller should only react to messages intended for it. Unfortunately, I can't get the filters to work. I must be doing something wrong or misunderstanding something.
I created a very simple program to upload here. I would expect that when receiving, FilterIndex 0 allows the data through. But it is always Filter 63. When I set HAL_FDCAN_ConfigGlobalFilter, nothing happens in the HAL_FDCAN_RxFifo0Callback, which makes sense in that case.
The CAN is configured in FDCAN_MODE_INTERNAL_LOOPBACK for testing. Even when I configure the filter like this, FilterIndex 0 doesn't catch the message:
sFilterConfig.FilterID1 = 0x12346578;
sFilterConfig.FilterID2 = 0x1FFFFFFF;
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
FDCAN_HandleTypeDef hfdcan2;
/* USER CODE BEGIN PV */
FDCAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[8];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_FDCAN2_Init(void);
/* USER CODE BEGIN PFP */
void FDCAN2_FilterConfig_and_Start(void);
void Send(uint32_t id, uint64_t data);
void FDCAN2_FilterConfig_and_Start() {
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x12346578;
sFilterConfig.FilterID2 = 0x1FFFFFFF;
if (HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig) != HAL_OK) {
Error_Handler();
}
/*
if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan2, FDCAN_REJECT, FDCAN_REJECT, FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE) != HAL_OK) {
Error_Handler();
}
*/
if (HAL_FDCAN_Start(&hfdcan2) != HAL_OK) {
Error_Handler();
}
if (HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK) {
Error_Handler();
}
}
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
FDCAN_RxHeaderTypeDef rxHeader;
uint8_t rxData[8];
if (HAL_FDCAN_GetRxMessage(&hfdcan2, FDCAN_RX_FIFO0, &rxHeader, rxData) == HAL_OK)
{
uint32_t filter_index = rxHeader.FilterIndex;
uint32_t RxId = rxHeader.Identifier;
}
}
void Send(uint32_t id, uint64_t data) {
uint8_t TxData[8];
for (int i = 0; i < 8; i++) {
TxData[i] = (data >> (56 - (8 * i))) & 0xFF;
}
TxHeader.Identifier = id;
if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FDCAN2_Init();
/* USER CODE BEGIN 2 */
FDCAN2_FilterConfig_and_Start();
Send(0x12345678, 0xAAAAAAAAAAAA);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief FDCAN2 Initialization Function
* 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_INTERNAL_LOOPBACK;
hfdcan2.Init.AutoRetransmission = DISABLE;
hfdcan2.Init.TransmitPause = DISABLE;
hfdcan2.Init.ProtocolException = DISABLE;
hfdcan2.Init.NominalPrescaler = 2;
hfdcan2.Init.NominalSyncJumpWidth = 1;
hfdcan2.Init.NominalTimeSeg1 = 3;
hfdcan2.Init.NominalTimeSeg2 = 4;
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 */
TxHeader.IdType = FDCAN_EXTENDED_ID;
TxHeader.TxFrameType = FDCAN_DATA_FRAME;
TxHeader.DataLength = FDCAN_DLC_BYTES_8;
TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
TxHeader.MessageMarker = 0;
/* USER CODE END FDCAN2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* file: pointer to the source file name
* line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
I would appreciate some help!
Solved! Go to Solution.
2024-11-26 08:56 AM
Hello,
You didn't set the Extended filter number:
hfdcan2.Init.ExtFiltersNbr = 0;
The number is set to 0. You need to set it at least to 1.
I've created a simple project where extended ID and standard ID are filtered.
You need just to comment / uncomment
#define USE_EXTENDED_ID
You can also play with the different parameters:
#define USE_EXTENDED_ID
#define FILTER_INDEX_NB 0
#if defined (USE_EXTENDED_ID)
#define CAN_EXT_ID_TO_SEND 0x12346578
#define CAN_EXT_ID_TO_FILTER 0x12346578
#else
#define CAN_STD_ID_TO_SEND 0x123
#define CAN_STD_ID_TO_FILTER 0x123
#endif
You can modify the ID to send to let it different from the ID to filter to validate that the frame will not be received.
Hope it helps.
2024-11-26 07:10 AM
Hello,
I didn't understand this statement: "FilterIndex 0 allows the data through. But it is always Filter 63."
What do you want to achieve? receiving only a frame with the extended ID 0x12345678?
2024-11-26 08:01 AM
I configured FilterIndex 0 to filter the Id. I tried this, that should filter if exact 0x12345678 is received:
sFilterConfig.FilterID1 = 0x12346578;
sFilterConfig.FilterID2 = 0x1FFFFFFF;
and this, that should match any Id:
sFilterConfig.FilterID1 = 0x00000000;
sFilterConfig.FilterID2 = 0x00000000;
In both cases HAL_FDCAN_RxFifo0Callback is called and rxHeader.FilterIndex (at line 94) is always 63 and not 0. FilterIndex 63 means no filter matched the Id. In this example I just want:
If identifier 0x12345678 is received, that FilterIndex 0 matches. But it doesn't. In the final software I'll need multiple filters. But my problem is, that this simple filter doesn't work as I expect.
2024-11-26 08:56 AM
Hello,
You didn't set the Extended filter number:
hfdcan2.Init.ExtFiltersNbr = 0;
The number is set to 0. You need to set it at least to 1.
I've created a simple project where extended ID and standard ID are filtered.
You need just to comment / uncomment
#define USE_EXTENDED_ID
You can also play with the different parameters:
#define USE_EXTENDED_ID
#define FILTER_INDEX_NB 0
#if defined (USE_EXTENDED_ID)
#define CAN_EXT_ID_TO_SEND 0x12346578
#define CAN_EXT_ID_TO_FILTER 0x12346578
#else
#define CAN_STD_ID_TO_SEND 0x123
#define CAN_STD_ID_TO_FILTER 0x123
#endif
You can modify the ID to send to let it different from the ID to filter to validate that the frame will not be received.
Hope it helps.
2024-11-27 01:00 AM
Thank you very much!
I didn't set
hfdcan2.Init.ExtFiltersNbr
I have 4 Filters and set it to 4. Now it works perfectly!
Why do I have to set it, only when I'm using extended IDs?
Is there any documentation, apart from this two sentences in the HAL documentation?
uint32_t FDCAN_InitTypeDef::ExtFiltersNbr
//Specifies the number of extended Message ID filters. This parameter must be a number between 0 and 8
2024-11-27 01:04 AM
@RoLie wrote:
Is there any documentation, apart from this two sentences in the HAL documentation?
The reference manual section "44 FD controller area network (FDCAN)"