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
  • what happens if you put an overflow value to a bit field?

    As an example:
    MSB
    BYTE bitfield2 :7
    BYTE bitfield1 :1
    LSB

    bitfield1 = 5;
    bitfield2 = 3;

    will the byte look like this? (assuming little endian)
    0000011 1 (cause 5 = 1 0 1...and the processor will take just the last bit?)

    do you where can I read about this issues?most C references don't cover this subject very deeply...

Reply
  • what happens if you put an overflow value to a bit field?

    As an example:
    MSB
    BYTE bitfield2 :7
    BYTE bitfield1 :1
    LSB

    bitfield1 = 5;
    bitfield2 = 3;

    will the byte look like this? (assuming little endian)
    0000011 1 (cause 5 = 1 0 1...and the processor will take just the last bit?)

    do you where can I read about this issues?most C references don't cover this subject very deeply...

Children
  • "do you where can I read about this issues?"

    The same place you would read about any of C's integer conversions. Your C reference should cover integer conversions and if not, get a better reference and/or get the language standard and read about the topic there.