Forum Discussion
Altera_Forum
Honored Contributor
12 years agoYou really don't want to use C bitfields to access device registers, apart from the complete non-portability of the order in which the bits are assigned you have no control over the actual memory cycles generated by the compiler.
Any structures used to (directly) map io registers should also have all their fields marked 'volatile'. If the bitfield is only used for a normal progam variable, the following might work (not even compiled though):typedef struct {
alt_u32 a:2;
alt_u32 b:4;
alt_u32 c:26;
} PeripheralOne;
union {
PeripheralOne b;
alt_u32 i;
} PeripheralOne_u;
static inline void
write_PeripheralOne(PeripheralOne value)
{
PeripheralOne_u u;
u.b = value;
__builtin_stwio(address, u.i);
}