Memory Bits To Bitfield Definition
-
I would like to address several bytes of memory but different bits from different locations. The program I am using is on an embedded system and I need access to different pieces of the peripherals. Specifically when dealing with ports where something connected crosses multiple ports and I need a modular way to access them through a simple 'maskport = value' and you only need to modify some macros or a enum to change things instead of each functions code. IS this even allowed or am I out to lunch? (I am using GCC) For example: (this doesn't work btw but it was my attempt)
struct bits {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
} __attribute__((__packed__));#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)
struct FIELD {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
};struct FIELD test;
test.b0 = SBIT(0xE2, 0);
test.b1 = SBIT(0xE2, 4);
test.b2 = SBIT(0xE2, 7);
test.b3 = SBIT(0xEF, 2);
test.b4 = SBIT(0xEF, 3);
test.b5 = SBIT(0x07, 1);
test.b6 = SBIT(0x07, 5);
test.b7 = SBIT(0x07, 6);test = 0x7A; // 0111 1010
That way by assigning a value to test:
test = 0x7A; // 0111 1010
0xE2:0001 0000
0xEF:0000 1100
0x07:0010 0010
-
I would like to address several bytes of memory but different bits from different locations. The program I am using is on an embedded system and I need access to different pieces of the peripherals. Specifically when dealing with ports where something connected crosses multiple ports and I need a modular way to access them through a simple 'maskport = value' and you only need to modify some macros or a enum to change things instead of each functions code. IS this even allowed or am I out to lunch? (I am using GCC) For example: (this doesn't work btw but it was my attempt)
struct bits {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
} __attribute__((__packed__));#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)
struct FIELD {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
};struct FIELD test;
test.b0 = SBIT(0xE2, 0);
test.b1 = SBIT(0xE2, 4);
test.b2 = SBIT(0xE2, 7);
test.b3 = SBIT(0xEF, 2);
test.b4 = SBIT(0xEF, 3);
test.b5 = SBIT(0x07, 1);
test.b6 = SBIT(0x07, 5);
test.b7 = SBIT(0x07, 6);test = 0x7A; // 0111 1010
That way by assigning a value to test:
test = 0x7A; // 0111 1010
0xE2:0001 0000
0xEF:0000 1100
0x07:0010 0010
If I understood, you can try with a union: union FIELDEX { struct FIELD { uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; } fld; uint8_t val; }; FIELDEX test; test.val = 0x7A; // 0111 1010 test.fld.b0 = 1; // set bit 0
-
If I understood, you can try with a union: union FIELDEX { struct FIELD { uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; } fld; uint8_t val; }; FIELDEX test; test.val = 0x7A; // 0111 1010 test.fld.b0 = 1; // set bit 0
I would still have the problem of setting separate address bits, however your example would make a good compact bitfield definition. I am trying to take bits from separate registers and create my own 'masked register' so when I write to my register it sends each bit to its specified bits in other registers.
test.b0 = SBIT(0xE2, 0); // 0xE2: 0000000x
test.b1 = SBIT(0xE2, 4); // 0xE2: 000x0000
test.b2 = SBIT(0xE2, 7); // 0xE2: x0000000test.b3 = SBIT(0xEF, 2); // 0xEF: 000000x0
test.b4 = SBIT(0xEF, 3); // 0xEF: 00000x00test.b5 = SBIT(0x07, 1); // 0x07: 000000x0
test.b6 = SBIT(0x07, 5); // 0x07: 00x00000
test.b7 = SBIT(0x07, 6); // 0x07: 0x000000Hopefully this will show more clearly what I am after:
test = 0x7A; // 0111 1010
// 0xE2 gets bits xxxxx010
// xx0 = 0xE2 bit 0
// x1x = 0xE2 bit 4
// 0xx = 0xE2 bit 7
// 0xE2 = 00010000// 0xEF gets bits xxx11xxx
// x1 = 0xEF bit 2
// 1x = 0xEF bit 3
// 0xEF = 00001100// 0x07 gets bits 011xxxxx
// xx1 = 0x07 bit 1
// x1x = 0x07 bit 5
// 0xx = 0x07 bit 6
// 0x07 = 00100010