bitfields


Erwin Rol
 

Hello,

maybe a bit of a weird coding style question, but for CAN support I need
a CAN ID "struct". The CAN ID is a 11 or 29 bit ID, a flags that says it
is 29 or 11 bit, a RTR flag and possible a ERROR flag. Which totals to
exactly 32 bit.

In Linux canid_t is just a typedef for a u32_t, and macros/defines are
used to access the flags and mask the ID's. something like;

typedef u32_t canid_t;

#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
#define CAN_ERR_FLAG 0x20000000U /* error message frame */

#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */

In Zephyr I also seen some use (dma for example) of the "u32_t flag:1;"
constructs. So a canid could be something like;

struct canid {
u32_t id:29;
u32_t eid:1;
u32_t rtr:1;
u32_t err:1;
};

Is there a preference for either of these constructs to encode
bitfields?

- Erwin

Join devel@lists.zephyrproject.org to automatically receive all group messages.