Guten Morgen,
wie würdet ihr folgendes Problem sauber in c lösen?
Ich habe eine Konfigurationsstruktur die im FLASH gespeichert werden
soll; sowohl beim Programmieren als auch im Betrieb.
Ich möchte zur Sicherheit noch eine CRC hinzufügen.
Das Problem ist, dass die Berechnung der CRC ohne den Speicherbereich
für die CRC durchführen will/muss.
Der erste Ansatz mit
1 | typedef struct
|
2 | {
|
3 | uint8_t param_1;
|
4 | uint16_t param_2;
|
5 | uint8_t param_3[9];
|
6 | uint16_t crc;
|
7 | } conf_t
|
8 | conf_t config;
|
9 | get_default_config(&config);
|
10 | uint8_t crc_size = sizeof(config) - sizeof(config.crc);
|
11 | config.crc = calc_crc(&config, crc_size);
|
scheiterte, da auch am Ende Padding-Bytes eingefügt wurden.
Die zweite Lösung empfinde ich als unsauber, funktioniert aber erstmal.
1 | uint8_t crc_size = (uint8_t)(&(config.crc)) - (uint8_t)&config;
|
aktuell habe ich die Idee, die Struktur so aufzubauen:
1 | typedef struct
|
2 | {
|
3 | uint8_t param_1;
|
4 | uint16_t param_2;
|
5 | uint8_t param_3[9];
|
6 | } conf_t;
|
7 | typedef struct
|
8 | {
|
9 | config_t conf;
|
10 | uint16_t crc;
|
11 | } conf_rcr_t;
|
Habt ihr eine bessere und vor allem sauberere Lösung?
Simon