[HAL library] STM32F407----CAN communication----filter configuration

[HAL library] STM32F407----CAN communication----basic principle
[HAL library] STM32F407----CAN communication----circuit diagram
[HAL library] STM32F407----CAN communication----interrupt detailed explanation

[HAL library] STM32CubeMX development----STM32F407----CAN communication experiment


1. STM32F407----CAN filter----Introduction

In the CAN protocol, the identifier of the message does not represent the address of the node, but is related to the content of the message. Therefore, the sender broadcasts the message to all receivers. When a node receives a message, it determines whether the software needs the message according to the value of the identifier (CAN ID); if necessary, it is copied to the SRAM; if not, the message is discarded without software intervention.

To meet this requirement, bxCAN provides 14 variable-bit-width, configurable filter groups (13~0) for the application program, so as to only receive the messages required by the software. The method of hardware filtering saves CPU overhead, otherwise it must be filtered by software to occupy a certain amount of CPU overhead. Each filter group x consists of 2 32-bit registers, CAN_FxR0 and CAN_FxR1.

1. CAN filter bank

STM32F407 has two CAN channels, so there are 28 filter groups, as shown in the following table:

CAN filter group
CAN1 0 ~ 13
CAN2 14 ~ 27

2. CAN filter mode

mask pattern

In mask mode, the identifier register is associated with the mask register to indicate which bits of the identifier must match and which bits don't .

list mode

All bits of the incoming identifier must match the bits specified in the filter register .

3. CAN filter bit width

The bit width of each filter group can be independently configured to meet the different needs of the application. Depending on the bit width, each filter group provides:

  • 1 x 32-bit filter , including: STDID[10:0], EXTID[17:0], IDE and RTR bits.
  • 2 16-bit filters including: STDID[10:0], IDE, RTR and EXTID[17:15] bits.

4. CAN filter matching serial number

Once a message is received in the FIFO, it is ready for use by the application.
Usually, the data in the message is copied to SRAM; in order to copy the data to the appropriate location, the application needs to distinguish different data according to the identifier of the message. bxCAN provides filter match numbers to simplify this identification process.
According to the filter priority rule, the filter matching sequence number is stored in the mailbox together with the message. Therefore, each received message has a filter matching sequence number associated with it.

There are two ways to use the filter matching serial number:

  1. compares the filter match number to a list of expected values
  2. Use the filter match number as an index to access the target address

For list-mode filters, the software no longer needs to compare identifiers.
For mask pattern filters, software only needs to compare the required mask bits (bits that must match).

The filter number is independent of the activation state of the filter group. In addition, two separate numbering schemes are used, one for each FIFO. Specific examples are as follows:
insert image description here
This figure is just an example, and the specific configuration does not need to follow this figure.

5. CAN filter priority rules

Depending on the configuration of the filter, it is possible that a message identifier can pass through multiple filters; in this case, the filter matching sequence number stored in the receiving mailbox is determined according to the following priority rules:

  • Filters with a bit width of 32 bits have priority over filters with a bit width of 16 bits.
  • For filters of the same bit width, identifier list patterns take precedence over mask bit patterns.
  • For filters with the same bit width and mode, the priority is determined by the filter number, and the priority of the filter with the smaller filter number is higher.

6. CAN filter configuration

Filter banks can be configured via the corresponding CAN_FMR register (CAN Filter Master Register). But it can not be configured directly at any time. Before configuring a filter group, it must be set to disabled by clearing the FACT bit of the CAN_FAR register (CAN filter activation register). before setting or setting the configuration of the filter group

  • By setting the FSCx bit of CAN_FS1R (CAN filter bit width register), the bit width of a filter bank can be configured .
  • By setting the FBMx bit of CAN_FM1R (CAN filter mode register), the identifier list mode or mask mode of the corresponding mask/identifier register can be configured .

The filter configuration is shown in the following figure:
insert image description here


2. STM32F407----CAN filter----configuration experiment

The following is the specific experimental content and code of STM32F407----CAN filter----configuration.
For preliminary preparations, please refer to this article: [HAL library] STM32CubeMX development----STM32F407----CAN communication experiment

Experimental content: The CAN communication baud rate is 500K bps, and the received CAN_ID is sent out, the sending frame ID is 0x18888888, and the first 4 bytes of data are the received CAN_ID.


Filter configuration ----- code template

In the [HAL library] program generated by STM32CubeMX, there is no configuration code related to the filter. If the filter is not configured, the data cannot be received , so we need to write the code to configure the filter by ourselves .

Below is the filter configuration code through which all CAN frame IDs can pass.

The specific filter configuration code template is as follows:

void CAN1_Filter_Bank(void)//过滤器
{
    
    
    CAN_FilterTypeDef FilterConfig;

    /*配置CAN过滤器*/
    FilterConfig.FilterBank = 0;                      //过滤器组号
    FilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;  //过滤模式:屏蔽位模式--CAN_FILTERMODE_IDMASK,列表模式--CAN_FILTERMODE_IDLIST
    FilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //过滤器位宽:32位
    FilterConfig.FilterIdHigh = 0x0000;               //32位ID
    FilterConfig.FilterIdLow = 0x0000;
    FilterConfig.FilterMaskIdHigh = 0x0000;           //32位MASK
    FilterConfig.FilterMaskIdLow = 0x0000;
    FilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; //过滤器0关联到FIFO0
    FilterConfig.FilterActivation = ENABLE;           //激活滤波器0
    FilterConfig.SlaveStartFilterBank = 14;           //单CAN此参数无意义
    
    //过滤器配置激活
    if (HAL_CAN_ConfigFilter(&hcan1, &FilterConfig) != HAL_OK)
    {
    
    
        Error_Handler();
    }
}

Description: Filter mask mode: 0 for no verification, 1 for verification. Therefore, if the mask bits are all 0, no verification is required, and all CAN frame IDs can be received.

list mode

Only the CAN_ID stored in the list can be received.

1. List mode - 16 bits

In the 16-bit list mode, only the standard frame CAN_ID can be received and processed .
The CAN_FxR1 and CAN_FxR2 registers have a total of 64 bits and can store four 16-bit CAN_IDs.

Specifically as shown in the figure below:
insert image description here

The specific description of the filter configuration is as follows:

bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
map SHAME[10:0] RTR IDE EXID[17:15]]
illustrate 11-bit standard frame CAN_ID Frame type:
0 data frame
1 remote frame
Identifier type:
0 standard frame
1 extended frame
Useless data
can be set to 0
example 0x101 0 0 0
0x102 1 0 0
0x103 0. 0 0
0x104 1 0 0

Because in the 16-bit list mode, only the standard frame CAN_ID can be received and processed , so the IDE bits are all set to 0 .

The specific code is as follows:

void CAN1_Filter_Bank(void)//过滤器、
{
    
    
	//标准帧ID
    uint16_t CanFilterListSTID0 = 0x101;
    uint8_t CanFilterListRTR0 = 0x00;   //帧类型:0x00数据帧,0x01远程帧
    
    uint16_t CanFilterListSTID1 = 0x102;
    uint8_t CanFilterListRTR1 = 0x01;   //帧类型:0x00数据帧,0x01远程帧
    
    uint16_t CanFilterListSTID2 = 0x103;
    uint8_t CanFilterListRTR2 = 0x00;   //帧类型:0x00数据帧,0x01远程帧
    
    uint16_t CanFilterListSTID3 = 0x104;
    uint8_t CanFilterListRTR3 = 0x01;   //帧类型:0x00数据帧,0x01远程帧
    
    CAN_FilterTypeDef FilterConfig;

    /*配置CAN过滤器*/
    FilterConfig.FilterBank = 0;                      //过滤器组号
    FilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;  //过滤模式:列表模式--CAN_FILTERMODE_IDLIST
    FilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; //过滤器位宽:16位
    FilterConfig.FilterIdHigh = (CanFilterListSTID0<<5)|(CanFilterListRTR0<<4);               //ID
    FilterConfig.FilterIdLow =  (CanFilterListSTID1<<5)|(CanFilterListRTR1<<4);
    FilterConfig.FilterMaskIdHigh = (CanFilterListSTID2<<5)|(CanFilterListRTR2<<4);           //MASK
    FilterConfig.FilterMaskIdLow = (CanFilterListSTID3<<5)|(CanFilterListRTR3<<4);
    FilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; //过滤器0关联到FIFO0
    FilterConfig.FilterActivation = ENABLE;           //激活滤波器
    FilterConfig.SlaveStartFilterBank = 14;           //单CAN此参数无意义
    //过滤器配置
    if (HAL_CAN_ConfigFilter(&hcan1, &FilterConfig) != HAL_OK)
    {
    
    
        Error_Handler();
    }
}

The experimental results are as follows:

insert image description here

2. List mode ---- 32 bits

In the 32-bit list mode, standard frame CAN_ID and extended frame CAN_ID can be received and processed .
The CAN_FxR1 and CAN_FxR2 registers have a total of 64 bits and can store two 32-bit CAN_IDs.

The details are shown in the figure below:
insert image description here
the filter configuration is described in detail as follows:

bit 31 ~ 21 20 ~3 2 1 0
map SHAME[10:0] EXID[17:0] IDE RTR 0
illustrate 29-bit extended frame CAN_ID Identifier type:
0 standard frame
1 extended frame
Frame type:
0 data frame
1 remote frame
0
11-bit standard frame CAN_ID 0
example 0x1B010F01 1 0 0
0x701 0 0 1 0

The specific code is as follows:

void CAN1_Filter_Bank(void)//过滤器
{
    
    
	//扩展帧ID
    uint32_t CanFilterListEXID0 = 0x1B010F01;
    uint8_t CanFilterListIDE0 = 0x01;       //帧类型:0x00标准帧,0x01扩展帧
    uint8_t CanFilterListRTR0 = 0x00;       //帧类型:0x00数据帧,0x01远程帧

    //标准帧ID
    uint16_t CanFilterListSTID1 = 0x701;
    uint8_t CanFilterListIDE1 = 0x00;//帧类型:0x00标准帧,0x01扩展帧
    uint8_t CanFilterListRTR1 = 0x01;//帧类型:0x00数据帧,0x01远程帧

    CAN_FilterTypeDef FilterConfig;

    /*配置CAN过滤器*/
    FilterConfig.FilterBank = 0;                      //过滤器组号
    FilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;  //过滤模式:列表模式--CAN_FILTERMODE_IDLIST
    FilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //过滤器位宽:32位
    FilterConfig.FilterIdHigh = (CanFilterListEXID0<<3)>>16;         //ID
    FilterConfig.FilterIdLow =  (CanFilterListEXID0<<3)|(CanFilterListIDE0<<2)|(CanFilterListRTR0<<1);
    FilterConfig.FilterMaskIdHigh = CanFilterListSTID1<<5;           //MASK
    FilterConfig.FilterMaskIdLow = (CanFilterListIDE1<<2)|(CanFilterListRTR1<<1);
    FilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; //过滤器0关联到FIFO0
    FilterConfig.FilterActivation = ENABLE;           //激活滤波器
    FilterConfig.SlaveStartFilterBank = 14;           //单CAN此参数无意义
    //过滤器配置
    if (HAL_CAN_ConfigFilter(&hcan1, &FilterConfig) != HAL_OK)
    {
    
    
        Error_Handler();
    }
}

The experimental results are as follows:

insert image description here

mask pattern

It is determined by the mask code whether to compare with which digits of the verified CAN_ID, and it can only be received if it is consistent with the verified CAN_ID. Mask code: 0 for no verification, 1 for verification.

Examples of mask patterns:

Hexadecimal 7 6 5 4 3 2 1 0
masking code 0xFC 1 1 1 1 1 1 0 0
verification code 0x79 0 1 1 1 1 0 0 1
available data 0x78,0x79,0x7A,0x7B 0 1 1 1 1 0 x x

The mask code is a bit of 1, which must be consistent with the verification code to pass.
The mask code is the bit of 0, both 0 and 1 are allowed to pass.

1. Mask mode - 16 bits

In the 16-bit mask mode, only the standard frame CAN_ID can be received and processed .
The CAN_FxR1 and CAN_FxR2 registers have a total of 64 bits, which can store two 16-bit verification codes and two 16-bit mask codes.

The details are shown in the figure below:
insert image description here
the filter configuration is described in detail as follows:

bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
map SHAME[10:0] RTR IDE EXID[17:15]]
illustrate 11-bit standard frame CAN_ID Frame type:
0 data frame
1 remote frame
Identifier type:
0 standard frame
1 extended frame
Useless data
can be set to 0
example Mask code 0: 0x7FC 1 0 0
Verification code 0: 0x201 0 0 0
Receivable frames: 0x200, 0x201, 0x202, 0x203
can only receive data frames
Mask code 1: 0x7CF 0 0 0
Verification code 1: 0x301 0 0 0
Receivable frames: 0x301, 0x311, 0x321, 0x331
can receive data frames and remote frames

Because in the 16-bit list mode, only the standard frame CAN_ID can be received and processed , so the IDE bits are all set to 0 .

The specific code is as follows:

void CAN1_Filter_Bank(void)//过滤器
{
    
    
	uint16_t CanFilterMaskSTID0 = 0x7FC;//屏蔽码:0不验证,1验证
    uint8_t  CanFilterMaskRTR0 = 0x01;//屏蔽码:0不验证,1验证
    uint16_t CanFilterSTID0 = 0x201;
    uint8_t  CanFilterRTR0 = 0x00;//帧类型:0x00数据帧,0x01远程帧
    
    uint16_t CanFilterMaskSTID1 = 0x7CF;//屏蔽码:0不验证,1验证
    uint8_t CanFilterMaskRTR1 = 0x00;//屏蔽码:0不验证,1验证
    uint16_t CanFilterSTID1 = 0x301;
    uint8_t CanFilterRTR1 = 0x00;//帧类型:0x00数据帧,0x01远程帧

    CAN_FilterTypeDef FilterConfig;

    /*配置CAN过滤器*/
    FilterConfig.FilterBank = 0;                      //过滤器组号
    FilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;  //过滤模式:掩码模式--CAN_FILTERMODE_IDMASK
    FilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; //过滤器位宽:16位
    FilterConfig.FilterIdHigh = (CanFilterSTID0<<5)|(CanFilterRTR0<<4);               //ID
    FilterConfig.FilterIdLow =  (CanFilterSTID1<<5)|(CanFilterRTR1<<4);
    FilterConfig.FilterMaskIdHigh = (CanFilterMaskSTID0<<5)|(CanFilterMaskRTR0<<4);           //MASK
    FilterConfig.FilterMaskIdLow = (CanFilterMaskSTID1<<5)|(CanFilterMaskRTR1<<4);
    FilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; //过滤器0关联到FIFO0
    FilterConfig.FilterActivation = ENABLE;           //激活滤波器
    FilterConfig.SlaveStartFilterBank = 14;           //单CAN此参数无意义
    //过滤器配置
    if (HAL_CAN_ConfigFilter(&hcan1, &FilterConfig) != HAL_OK)
    {
    
    
        Error_Handler();
    }
}

Experimental result 1 is as follows:
insert image description here
Acceptable frames: 0x200, 0x201, 0x202, 0x203, only data frames can be received.

Experimental result 2 is as follows:
insert image description here
Receivable frames: 0x301, 0x311, 0x321, 0x331, data frames and remote frames can be received.

2. Mask mode ---- 32 bits

In the 32-bit list mode, standard frame CAN_ID and extended frame CAN_ID can be received and processed .
The CAN_FxR1 and CAN_FxR2 registers have a total of 64 bits, which can store a 32-bit verification code and a 32-bit mask code.

The details are shown in the figure below:
insert image description here
the filter configuration is described in detail as follows:

bit 31 ~ 21 20 ~3 2 1 0
map SHAME[10:0] EXID[17:0] IDE RTR 0
illustrate 29-bit extended frame CAN_ID Identifier type:
0 standard frame
1 extended frame
Frame type:
0 data frame
1 remote frame
0
11-bit standard frame CAN_ID 0
example Mask code: 0x1FFFFFFC 1 1 0
Verification code: 0x1B030F01 1 0 0
Receivable frame: 0x1B030F00, 0x1B030F01, 0x1B030F02, 0x1B030F03
can only receive data frame extension frame

The specific code is as follows:

void CAN1_Filter_Bank(void)//过滤器
{
    
    
    uint32_t CanFilterMaskEXID = 0x1FFFFFFC;//屏蔽码:0不验证,1验证
    uint8_t CanFilterMaskIDE = 0x01;//屏蔽码:0不验证,1验证
    uint8_t CanFilterMaskRTR = 0x01;//屏蔽码:0不验证,1验证

    uint32_t CanFilterEXID = 0x1B030F01;
    uint8_t CanFilterIDE= 0x01;     //帧类型:0x00标准帧,0x01扩展帧
    uint8_t CanFilterRTR= 0x00;     //帧类型:0x00数据帧,0x01远程帧

    CAN_FilterTypeDef FilterConfig;

    /*配置CAN过滤器*/
    FilterConfig.FilterBank = 0;                      //过滤器组号
    FilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;  //过滤模式:掩码模式--CAN_FILTERMODE_IDMASK
    FilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //过滤器位宽:32位
    FilterConfig.FilterIdHigh = (CanFilterEXID<<3)>>16;               //ID
    FilterConfig.FilterIdLow =  (CanFilterEXID<<3)|(CanFilterIDE<<2)|(CanFilterRTR<<1);
    FilterConfig.FilterMaskIdHigh = (CanFilterMaskEXID<<3)>>16;           //MASK
    FilterConfig.FilterMaskIdLow = (CanFilterMaskEXID<<3)|(CanFilterMaskIDE<<2)|(CanFilterMaskRTR<<1);
    FilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; //过滤器0关联到FIFO0
    FilterConfig.FilterActivation = ENABLE;           //激活滤波器
    FilterConfig.SlaveStartFilterBank = 14;           //单CAN此参数无意义
    //过滤器配置
    if (HAL_CAN_ConfigFilter(&hcan1, &FilterConfig) != HAL_OK)
    {
    
    
        Error_Handler();
    }

The experimental results are as follows:
insert image description here
Acceptable frames: 0x1B030F00, 0x1B030F01, 0x1B030F02, 0x1B030F03, only data frame extension frames can be received.


Guess you like

Origin blog.csdn.net/MQ0522/article/details/130067130