From 024c3ff81479a605d3e94b53f5a69fc246497be2 Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Wed, 25 Nov 2026 22:27:01 +0800 Subject: [PATCH] [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/include/drivers/dev_can.h | 37 +++++++++++++------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/components/drivers/include/drivers/dev_can.h b/components/drivers/include/drivers/dev_can.h index 4ec5103e7ba3..e6bbaf4342f5 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 @@ -315,17 +328,17 @@ 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) + 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,0xFFFFFFFF,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,0xFFFFFFFF,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,0xFFFFFFFF,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,0xFFFFFFFF,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,0xFFFFFFFF,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