Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alignment of C structure in Internal FLASH memory

I have a configuration structure I would like to save on the internal flash of ARM cortex M3. According to the specifications, the data save in the internal flash, must be aligned to 32bit. Because I have lot's of boolean, and chars in my structure,I don't want to use 32bits to store 8 bits... I decided to pack the structure using the __packed preprocessor pragma, Then When I save it as a whole structure, I just have to make sure that the structure size is divisible by 4 (4 bytes = 32bits), I do it by adding padding bytes if needed. Currently, during development I alter the structure a lot, and to make it aligned with the 32 bits, I need to change the padding bytes all the time. Currently, the structure look slike this

typedef __packed struct
{
uint8_t status;
uint16_t delay;
uint32_t blabla;
uint8_t foo[5];
uint8_t padding[...] // this has to be changed every time I alter the structure.
} CONFIG;

Is there a better way to achieve what I'm doing ? I'm quite new in Embedded programming, and I want to make sure that I'm not doing mistakes.

Edit: Please note. The data is persisted in the end of the internal-flash, so omitting the padding will not work...

like image 863
stdcall Avatar asked Dec 15 '11 07:12

stdcall


2 Answers

Solution 1: You could put it inside a union containing your structure and an array of characters:

union
{
  CONFIG config;
  uint8_t total_size[32];
} my_union;
like image 34
Lindydancer Avatar answered Oct 03 '22 12:10

Lindydancer


Perhaps this is an idea:

typedef __packed struct {
    uint8_t status;
    uint16_t delay;
    uint32_t blabla;
    uint8_t foo[5];
} CONFIG;

typedef __packed struct {
    CONFIG cfg;
    uint8_t padding[4 - (sizeof(CONFIG) % 4)]
} CONFIGWRAPPER;
like image 50
orlp Avatar answered Oct 03 '22 14:10

orlp