[Programming tips] - Structs and EEPROM serialization (Part 1 of 3)

This post refers to the ATMega328P on which the code was tested, but should also apply to many/most Atmel mcus. The code is in plain C and is (obviously) compatible with, but not specific to the Arduino IDE. Inspired by recent discussions in this forum. Learn from it what you didn't know, add to it if you can.

Part 1 | Part 2-> | Part 3-->

#struct

It is often handy (even necessary) to persist information or settings between power-ups. For this very simple example a hypothetical 'Temperature Alarm System' is considered. It is a battery-operated device that sounds an alarm and turns on a light when its built-in sensor measures a certain temperature. For this example, the temperature sensor's raw, pre-converted output is in the range 0 to 4096.

The device has the following user-configurable settings:

  • The temperature at which the alarm should trigger can be set by the user Integer value (16 bits)
  • The temperature can be expressed in either Celsius or Fahrenheit boolean value (1 bit)
  • A light can accompany the audible alarm boolean value (1 bit)
  • The audible alarm has a soft/loud setting boolean value (1 bit)

The naive way to express it in code:

List 1.1

uint16_t triggerTemp;//16 bits (2 bytes)
bool tempIsDegreesC;//1 bit (1 byte)
bool lightWithAlarm;//1 bit (1 byte)
bool alarmIsLoud;//1 bit (1 byte)

The above arrangement uses 5 bytes of memory: 2 for the uint16_t and one byte each for the bools (despite the fact that they represent a single bit, 1 or 0). There is a better way: the struct

List 1.2

uint16_t triggerTemp;//16 bits (2 bytes)

struct booleanSettings {
    uint8_t tempIsDegreesC : 1;//1 bit (of 1 byte)
    uint8_t lightWithAlarm : 1;//1 bit (of 1 byte)
    uint8_t alarmIsLoud : 1;//1 bit (of 1 byte)
    uint8_t : 0;//pad the rest of the byte with 0's
};

This approach consumes only 3 bytes (40% less than previously): 2 for the uint16_t and one for the struct into which the booleans are packed. Each member of the struct (named booleanSettings) appears to be an unsigned byte uint8_t, but the : 1 notation tells the compiler to associate just one bit of a single byte with the member. One byte is thereby shared among all three members, so that sizeof(booleanSettings) is 1 byte. The : 0 notation tells the compiler to pad the rest of the byte with 0's, but it can alternatively be padded with 1's. The uint8_t : 0; line is not strictly necessary, but useful.

Including the uint16_t triggerTemp into the struct exposes an important consideration: struct members are packed according to the widest of its members. This is called its alignment. Achar is 1 byte 'wide', an int is 2 bytes, a long is 4 byes, and so on.

List 1.3

struct alarmSettings {
    uint16_t triggerTemp;//16 bits (2 bytes)
    uint8_t tempIsDegreesC : 1;//1 bit (of 1 byte)
    uint8_t lightWithAlarm : 1;//1 bit (of 1 byte)
    uint8_t alarmIsLoud : 1;//1 bit (of 1 byte)
};

Contrary to expectations, sizeof(alarmSettings) is 4 bytes (20% less), and not 3. The reason for this is that the struct is 2 bytes wide due to the uint16_t that it contains, but the single byte containing all the boolean value's bits is not wide enough, so the compiler adds another (hidden) 'dummy' byte. This is called padding. To illustrate the point, sizeof(alarmSettings_TEMP) (see below) is also 4 bytes, despite it having one more 1-byte member than struct alarmSettings above:

List 1.4

struct alarmSettings_TEMP {
    uint16_t triggerTemp;
    uint8_t tempIsDegreesC : 1;
    uint8_t lightWithAlarm : 1;
    uint8_t alarmIsLoud : 1;
    uint8_t  extraMember;//<--Makes no difference to the size
};

Considering the raw output of the temperature sensor, the maximum value it outputs is 4096, which is 0b111111111111 in binary. That's 12 bits maximum, whereas a uint16_t is 16 bits wide, which leaves 4 bits 'unused'. We can indeed use them:

List 1.5

struct alarmSettings {
    uint16_t triggerTemp : 12;
    uint16_t tempIsDegreesC : 1;
    uint16_t lightWithAlarm : 1;
    uint16_t alarmIsLoud : 1;
};

The sizeof(alarmSettings) is now 2 bytes with an extra bit to spare (>60% less). All of the variables are packed into a single 16-bit wide struct!

For reference, a struct is normally declared globally (outside of any functions), and the struct members are accessed in functions:

List 1.6

...
alarmSettings.triggerTemp = 1234;
alarmSettings.tempIsDegreesC = true;
...
if (alarmSettings.alarmIsLoud) {...}
...

Posted Apr 18, 2019, 12:44 AM UTC · edited

85
dsalychev•

Thanks for sharing!

I didn't even know that the language itself allows such bit-field declarations inside a structure, but checked in K&R :) That's true. However, it can be fair to mention that endianess should be taken into account to create portable programs.

2
asking_scienceOP•

Cool, happy to share.

endianess

I'm addressing that in part 2

2
Tythagoras•

Im just beginning embedded programming, and this was so helpful. In pc/desktop programming, I never gave a second thought as to how structs occupied space, and wouldnt have cared if it took a few more bytes anyway. Thank you so much for this.

2
asking_scienceOP•

Welcome to the world of cycles and bits!

1
TotesMessenger•

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 ^(If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads.) ^(Info ^/ ^Contact)

1