This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

HAL design pattern?

Hi,
I'm trying to write a hardware abstraction layer.
I'm trying to figure out how to write predefined values (for all needed register) to the 8-bit registers.
I had some ideas and they were all bad and caused a lot of stupid mistakes that wasted a lot of time...
I dont want any numbers in my program.Everything must be predefined.
Now I thought of something new but I'm not sure it will work or maybe will work but will cause me future problems due to issues I'm unaware of...
The idea is that for each different register I'll define a structure with bitfields.Something like this i.e.:

#define REG ((unsigned char volatile xdata *) 0)[0x400]

typedef struct
{ BYTE field1 :3, BYTE field2 :1, BYTE field3 :4
}sReg;

((sReg) REG).field1 = PREDEFINED_VAL1;
((sReg) REG).field2 = PREDEFINED_VAL2;
((sReg) REG).field3 = PREDEFINED_VAL3;

will this work or will cause me problems by overriding
bits I don't want to be changed?or any other problem you can think of....

Does anyone have any other good idea?

Thanks!!

Parents
  • Bitfields in general can raise more questions than they solve - especially for single bits;
    They are (often) no more effecient that using the normal bitwise operators, and may even be less efficient.

    So it's probably easier to just stick with "masks" and the good ol' bitwise operators...

Reply
  • Bitfields in general can raise more questions than they solve - especially for single bits;
    They are (often) no more effecient that using the normal bitwise operators, and may even be less efficient.

    So it's probably easier to just stick with "masks" and the good ol' bitwise operators...

Children