From 75b53af8ccd14047b836488869e75a3f0be27f4e Mon Sep 17 00:00:00 2001 From: ymj0440 <23681351@qq.com> Date: Tue, 1 Dec 2026 10:19:18 +0800 Subject: [components][drivers][can] limit filter masks to the identifier width (#12089) * [components][drivers][can] limit filter masks to the identifier width RT_CAN_FILTER_STD_INIT() and the other helpers initialized their mask with 0xFFFFFFFF, which carries bits outside the identifier width: 11 bits for a standard frame and 29 bits for an extended one. A mask in Mask Mode selects the identifier bits a filter compares, so bits above the width do not belong there. Add RT_CAN_STD_ID_MASK (0x7FF) and RT_CAN_EXT_ID_MASK (0x1FFFFFFF) and use them in all twelve helper macros. Drivers that shift the mask into the hardware filter registers are unaffected, because the shifted value is the same for the old and the new mask; the change makes the API correct for drivers that use the mask in software and removes the ambiguity reported in #11576. Signed-off-by: ymj0440 * [components][drivers][can] clang-format the filter helper macros The helper macros changed in the previous commit did not follow the repository clang-format style: arguments are separated by a space and the continuation line is indented by four spaces. Signed-off-by: ymj0440 --------- Signed-off-by: ymj0440 Co-authored-by: ymj0440 --- components/drivers/include/drivers/dev_can.h | 49 ++++++++++++++++++---------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'components') diff --git a/components/drivers/include/drivers/dev_can.h b/components/drivers/include/drivers/dev_can.h index 4ec5103e7b..56adb78b0b 100644 --- a/components/drivers/include/drivers/dev_can.h +++ b/components/drivers/include/drivers/dev_can.h @@ -299,6 +299,19 @@ struct rt_can_filter_item #endif /*RT_CAN_USING_HDR*/ }; +/** + * @def RT_CAN_STD_ID_MASK + * @brief Filter mask that matches every bit of a standard (11-bit) identifier. + * @def RT_CAN_EXT_ID_MASK + * @brief Filter mask that matches every bit of an extended (29-bit) identifier. + * + * A mask in Mask Mode selects the identifier bits a filter compares, so it + * must never carry bits outside the identifier width of the frame type. The + * helper macros below use these values instead of a full 32-bit mask. + */ +#define RT_CAN_STD_ID_MASK 0x000007FFUL +#define RT_CAN_EXT_ID_MASK 0x1FFFFFFFUL + #ifdef RT_CAN_USING_HDR /** * @def RT_CAN_FILTER_ITEM_INIT @@ -314,18 +327,18 @@ struct rt_can_filter_item */ #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \ {(id), (ide), (rtr), (mode),(mask), -1, CAN_RX_FIFO0,(ind), (args)} -#define RT_CAN_FILTER_STD_INIT(id,ind,args) \ - RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args) -#define RT_CAN_FILTER_EXT_INIT(id,ind,args) \ - RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args) -#define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \ - RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args) -#define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \ - RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args) -#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \ - RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args) -#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \ - RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args) +#define RT_CAN_FILTER_STD_INIT(id, ind, args) \ + RT_CAN_FILTER_ITEM_INIT(id, 0, 0, 0, RT_CAN_STD_ID_MASK, ind, args) +#define RT_CAN_FILTER_EXT_INIT(id, ind, args) \ + RT_CAN_FILTER_ITEM_INIT(id, 1, 0, 0, RT_CAN_EXT_ID_MASK, ind, args) +#define RT_CAN_STD_RMT_FILTER_INIT(id, ind, args) \ + RT_CAN_FILTER_ITEM_INIT(id, 0, 1, 0, RT_CAN_STD_ID_MASK, ind, args) +#define RT_CAN_EXT_RMT_FILTER_INIT(id, ind, args) \ + RT_CAN_FILTER_ITEM_INIT(id, 1, 1, 0, RT_CAN_EXT_ID_MASK, ind, args) +#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id, ind, args) \ + RT_CAN_FILTER_ITEM_INIT(id, 0, 0, 1, RT_CAN_STD_ID_MASK, ind, args) +#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id, ind, args) \ + RT_CAN_FILTER_ITEM_INIT(id, 1, 0, 1, RT_CAN_EXT_ID_MASK, ind, args) #else /** * @def RT_CAN_FILTER_ITEM_INIT @@ -340,17 +353,17 @@ struct rt_can_filter_item #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask) \ {(id), (ide), (rtr), (mode), (mask), -1, CAN_RX_FIFO0 } #define RT_CAN_FILTER_STD_INIT(id) \ - RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF) + RT_CAN_FILTER_ITEM_INIT(id, 0, 0, 0, RT_CAN_STD_ID_MASK) #define RT_CAN_FILTER_EXT_INIT(id) \ - RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF) + RT_CAN_FILTER_ITEM_INIT(id, 1, 0, 0, RT_CAN_EXT_ID_MASK) #define RT_CAN_STD_RMT_FILTER_INIT(id) \ - RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF) + RT_CAN_FILTER_ITEM_INIT(id, 0, 1, 0, RT_CAN_STD_ID_MASK) #define RT_CAN_EXT_RMT_FILTER_INIT(id) \ - RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF) + RT_CAN_FILTER_ITEM_INIT(id, 1, 1, 0, RT_CAN_EXT_ID_MASK) #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \ - RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF) + RT_CAN_FILTER_ITEM_INIT(id, 0, 0, 1, RT_CAN_STD_ID_MASK) #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \ - RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF) + RT_CAN_FILTER_ITEM_INIT(id, 1, 0, 1, RT_CAN_EXT_ID_MASK) #endif -- cgit v1.3.1