问题
I want to understand below code, why the filter 0x100 between 1xFF address. Mask value is 1110000000000000 (16 bit) and filter value is 10000000000000. That mean my ids could be 100100000000000(16bit),10000100000000(16 bit) etc. (100.. side must be constant all ids.
But the 0x100 value is 100000000 (9 bit) how this possible I can not understand.How it compares 9-bit and 16-bit id ?
HAL_StatusTypeDef CAN_Filter_Init(CAN_HandleTypeDef *hcanxx)
{
CAN_FilterTypeDef can_filter_config;
//100 to 1FF in other words block all messages after 0x0200
can_filter_config.FilterActivation = CAN_FILTER_ENABLE;
can_filter_config.FilterBank = 0;
can_filter_config.FilterIdHigh = 0x100 << 5;
// can_filter_config.FilterIdHigh = 0x000;
can_filter_config.FilterIdLow = 0x0000;
can_filter_config.FilterMode = CAN_FILTERMODE_IDMASK;
can_filter_config.FilterScale = CAN_FILTERSCALE_32BIT;
can_filter_config.FilterMaskIdHigh = 0x700 << 5;
// can_filter_config.FilterMaskIdHigh = 0X000;
can_filter_config.FilterMaskIdLow = 0x0000;
can_filter_config.FilterFIFOAssignment = CAN_FILTER_FIFO0;
can_filter_config.SlaveStartFilterBank = 0;
if (HAL_CAN_ConfigFilter(hcanxx, &can_filter_config) != HAL_OK)
{
Error_Handler();
}
return HAL_OK;
}
回答1:
The CAN bus has 11-bit identifiers (base frame format) and 29-bit identifiers (extended frame format).
So if FilterIdHigh
is 16 bits, and the 11-bit identifier is in the upper bits of the register, then you need to left shift the identifier by 5 bits to put it into the 11 MSBs of the register. That is why the code has the left shift (<< 5
) for both the FilterIdHigh
and the FilterMaskIdHigh
.
The 0x100 value isn't a 9-bit value, it's an 11-bit value 001 0000 0000
.
The mask 0x700 value is also an 11-bit value 111 0000 0000
.
As a result, the upper three bits of the 11-bit identifier must be 001, and any identifier of the form001 xxxx xxxx
will pass the filter. So identifiers from 0x100 thru 0x1ff will pass the filter.
来源:https://stackoverflow.com/questions/57686650/can-bus-filter-and-mask-logic