Well, another non-hacky way you could do this for multiple variables is to put all variables in a struct, define a variable according to this struct with the initial values in it as const so it's put in RO memory, then manually memcpy those values into a 2nd variable in RAM. This also has the advantage of namespacing all those 'special' variables into a struct.
Code: Select all
typedef struct my_variables_t {
int var1;
int var2;
};
const my_variables_t initial_values={ //will live in flash because 'const'
.var1=123,
.var2=456,
};
my_variables_t values;
void on_startup() {
memcpy(&values, &initial_values, sizeof(my_variables_t));
}
void check_same() {
if (values.var1!=initial_values.var1) printf("Changed var1!\n");
if (values.var2!=initial_values.var2) printf("Changed var2!\n");
}
If you still insist on doing it by digging through the app code manually, it's really a lot more difficult than on the STM32, as the flash doesn't contain exactly what's mapped in memory, to make things like OTA easier. The steps would roughly be:
- Find out which application partition in flash you're running from.
- Read the application header and
parse it.
- Read out and parse the chunks the application consists of. Find the chunk that contains the address of your variable.
- Read out the chunk at the correct offset
- Compare with the variable.
Note that as the flash sections containing all of this by default are not memory mapped, you'd either have to manually memory map them or read the data using the spiflash api.