Aligning structure C in internal FLASH

I have a configuration structure that I would like to keep on the ARM cortex M3 internal flash. According to the specifications, the data storage in the internal flash should be aligned to 32 bits. Since I have a lot of logical and symbols in my structure, I don’t want to use 32 bits to store 8 bits ... I decided to pack the structure using the preprocessor pragma.Then __packed, when I save it in the whole structure, I just have to make sure that the size structure is divided by 4 (4 bytes = 32 bits), I do this by adding padding bytes, if necessary. Currently, during development, I change the structure a lot, and so that it aligns with 32 bits, I need to constantly change padding bytes. Currently the structure looks like 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 am doing? I am new to embedded programming and I want to make sure that I am not mistaken.

Edit: Please note. Data is saved at the end of the internal flash, so lowering the pad will not work ...

+5
source share
4 answers

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;
+4
source

Solution 1. You can put it in a union containing your structure and an array of characters:

union
{
  CONFIG config;
  uint8_t total_size[32];
} my_union;
+4
source

-, , , , , .

, (, , ). , 1- , , , , .

, , .

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

to

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

, 4 ( , , , 4 ). gcc attribute((__aligned__(4))

, , ( , sizeof() alignof() ), , , . /.

+3

2. IAR #pragma location , 32 . , - :

/* Fictitious end of flash location. */ 
#pragma location=0x1234FFE0
struct [... your struct goes here ...]
+1

All Articles