Let's suppose, I have a following structure:
struct my_struct {
uint32_t bf1 : 3;
uint32_t bf2 : 5;
uint32_t bf3 : 16;
uint32_t bf4 : 8;
};
and the following enum:
enum bf1_values {
Val1 = 0x0;
Val2 = 0x4;
Val3 = 0x7;
};
in addition, getter and setter functions for bf1:
uint32_t bf1_getter() {
return global_struct.bf1; // cast value to (uint32_t)?
}
void bf1_setter(enum bf1_values val) {
global_struct.bf1 = val; // cast enum to (uint32_t)?
}
Should I use the typecasting in getter and setter functions for safety?
EDIT:
The structure is supposed to be sent to HW.
EDIT2:
What I want to achieve is to be really sure that enum
will be correctly written to a bitfield, and correctly read from bitfield.