From d09e1d6e8ff4f2b983415b50b5c4540c1611c6c4 Mon Sep 17 00:00:00 2001 From: Thomas Stranger Date: Thu, 22 Aug 2024 21:39:48 +0200 Subject: [PATCH] samples: boards: stm32: backup_sram: add magic value Stores a magic value togetter with the counter value, and resets the counter in case that magic does not match. A small magic value is not ideal, but should be good enough for the sample and avoids pulling in a additional dependency e.g. on crc. Signed-off-by: Thomas Stranger --- samples/boards/stm32/backup_sram/src/main.c | 23 ++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/samples/boards/stm32/backup_sram/src/main.c b/samples/boards/stm32/backup_sram/src/main.c index 833bf5ee4a5..3be30449d71 100644 --- a/samples/boards/stm32/backup_sram/src/main.c +++ b/samples/boards/stm32/backup_sram/src/main.c @@ -12,8 +12,15 @@ #define BACKUP_DEV_COMPAT st_stm32_backup_sram #endif +#define BACKUP_MAGIC 0x600DCE11 + +struct backup_store { + uint32_t value; + uint32_t magic; +}; + /** Value stored in backup SRAM. */ -__stm32_backup_sram_section uint32_t backup_value; +__stm32_backup_sram_section struct backup_store backup; int main(void) { @@ -24,14 +31,20 @@ int main(void) return 0; } - printk("Current value in backup SRAM (%p): %d\n", &backup_value, backup_value); + if (backup.magic != BACKUP_MAGIC) { + backup.magic = BACKUP_MAGIC; + backup.value = 0; + printk("Invalid magic in backup SRAM structure - resetting value.\n"); + } - backup_value++; + printk("Current value in backup SRAM (%p): %d\n", &backup.value, backup.value); + + backup.value++; #if __DCACHE_PRESENT - SCB_CleanDCache_by_Addr(&backup_value, sizeof(backup_value)); + SCB_CleanDCache_by_Addr(&backup, sizeof(backup)); #endif - printk("Next reported value should be: %d\n", backup_value); + printk("Next reported value should be: %d\n", backup.value); printk("Keep VBAT power source and reset the board now!\n"); return 0; }