1 /*
2  * Copyright (c) 2021 Teslabs Engineering S.L.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/printk.h>
10 
11 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_backup_sram)
12 #define BACKUP_DEV_COMPAT st_stm32_backup_sram
13 #endif
14 
15 #define BACKUP_MAGIC 0x600DCE11
16 
17 struct backup_store {
18 	uint32_t value;
19 	uint32_t magic;
20 };
21 
22 /** Value stored in backup SRAM. */
23 __stm32_backup_sram_section struct backup_store backup;
24 
main(void)25 int main(void)
26 {
27 	const struct device *const dev = DEVICE_DT_GET_ONE(BACKUP_DEV_COMPAT);
28 
29 	if (!device_is_ready(dev)) {
30 		printk("ERROR: BackUp SRAM device is not ready\n");
31 		return 0;
32 	}
33 
34 	if (backup.magic != BACKUP_MAGIC) {
35 		backup.magic = BACKUP_MAGIC;
36 		backup.value = 0;
37 		printk("Invalid magic in backup SRAM structure - resetting value.\n");
38 	}
39 
40 	printk("Current value in backup SRAM (%p): %d\n", &backup.value, backup.value);
41 
42 	backup.value++;
43 #if __DCACHE_PRESENT
44 	SCB_CleanDCache_by_Addr(&backup, sizeof(backup));
45 #endif
46 
47 	printk("Next reported value should be: %d\n", backup.value);
48 	printk("Keep VBAT power source and reset the board now!\n");
49 	return 0;
50 }
51