存储在 EEPROM 中的数据结构的卷影副本

Shadow copy for a data structure stored in EEPROM

我正在为 STM32F105RC 处理器使用 ARM GCC。
在我的应用程序中,我现在有类似的东西:

typedef struct
{
  uint16_t coinValue;
  uint8_t  minimumCoins;
} MIN_COIN_RENDERING;

typedef struct
{
  uint16_t coinValue;
  uint8_t  hopperType;
  uint8_t  motorDelay;
} CONFIG_HOPPER;

typedef struct
{
  MIN_COIN_RENDERING minCoinRendering[10];
  CONFIG_HOPPER hopper[5];
  uint8_t reservedFFU[X];
  //
  uint16_t crc;
} APPLICATION_CONFIG; // MUST have 128 bytes!

第一个问题是如何正确判断预留FFU字节数(上面标有X)
如果你说:X = 128 - (10 x 3 + 5 x 4 + 2) = 76,那么这不是真的!
X 的正确值为 66,因为编译器对齐结构中的字段(至少使用编译器的默认设置)。
整个结构必须有 128 个字节,因为它将存储到 EEPROM 或从 EEPROM 恢复。该结构用作我们在 EEPROM 中的影子副本...

我的问题:有没有一种方法(更好、更灵活)来拥有一个卷影副本(用于 EEPROM),而不必在每次我在 APPLICATION_CONFIG 结构 ?

就这样写吧:

typedef struct
{
    MIN_COIN_RENDERING minCoinRendering[10];
    CONFIG_HOPPER hopper[5];
} APPLICATION_CONFIG;

typedef struct
{
    APPLICATION_CONFIG config;
    uint8_t reserved[128 - sizeof(APPLICATION_CONFIG) - 2];
    uint16_t crc;
} EEPROM_LAYOUT;

The first problem is how to determine correctly the number of bytes reserved FFU (marked above with an X).

正如您自己提到的,编译器对齐结构中的字段并且实际上是实现定义的,那么您必须确保通过编译器设置来弄清楚它是如何对齐的数据。也许它具有一些更改结构对齐设置的语句。(我在 ARM GCC 中还没有深入挖掘以肯定地说)。

My question: Is there a way (better, flexible) to have a shadow copy (for the EEPROM) without having to mess with the reservedFFU size each time I add a new field (or change something) in the APPLICATION_CONFIG structure ?

很遗憾,我无法直接回答这个问题。在我看来,这取决于你问题第一部分的解决方案。

您想要具有特定偏移量数据的固定大小的东西听起来很像您想要部分结构、部分数组的东西。如果您准备好以稍微不同的方式对待 crc,为什么不完全一样!

typedef union
{
  struct {
    MIN_COIN_RENDERING minCoinRendering[10];
    CONFIG_HOPPER hopper[5];
  };
  uint16_t raw[64];
} APPLICATION_CONFIG;

// Then e.g.
APPLICATION_CONFIG config;
config.hopper[3].motorDelay = 7; // Thanks to anonymous structures
uint16_t *crcptr = &config.raw[63];