Skip to content

Commit 68344cd

Browse files
gmarullnashif
authored andcommitted
samples: boards: stm32: add backup SRAM sample
The sample illustrates how to define and use a variable in the backup SRAM. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 3a786c6 commit 68344cd

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(stm32_backup_sram)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. _stm32_backup_sram:
2+
3+
STM32 Backup SRAM
4+
#################
5+
6+
Overview
7+
********
8+
9+
Multiple STM32 microcontrollers have a small backup SRAM that can be used as a
10+
NVM when VBAT pin is supplied with a voltage source, e.g. a coin button cell.
11+
12+
This example shows how to define a variable on the Backup SRAM. Each time the
13+
application runs the current value is displayed and then incremented by one. If
14+
VBAT is preserved, the incremented value will be shown on the next power-cycle.
15+
16+
.. note::
17+
18+
On most boards VBAT is typically connected to VDD thanks to a jumper.
19+
To excercise this sample with an independent VBAT source, you will need to
20+
remove the jumper.
21+
22+
Building and Running
23+
********************
24+
25+
In order to run this sample, make sure to enable ``backup_sram`` node in your
26+
board DT file.
27+
28+
.. zephyr-app-commands::
29+
:zephyr-app: samples/memc/stm32_backup_sram
30+
:board: nucleo_h743zi
31+
:goals: build
32+
:compact:
33+
34+
Sample Output
35+
=============
36+
37+
.. code-block:: console
38+
39+
Current value in backup SRAM: 11
40+
Next reported value should be: 12
41+
Keep VBAT power source and reset the board now!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_STM32_BACKUP_SRAM=y
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sample:
2+
description: Usage of backup SRAM on STM32
3+
name: stm32_backup_sram
4+
tests:
5+
sample.boards.stm32.backup_sram:
6+
depends_on: backup_sram
7+
build_only: true
8+
filter: dt_compat_enabled("st,stm32-backup-sram")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <sys/printk.h>
9+
10+
/** Value stored in backup SRAM. */
11+
__stm32_backup_sram_section uint32_t backup_value;
12+
13+
void main(void)
14+
{
15+
printk("Current value in backup SRAM: %d\n", backup_value);
16+
17+
backup_value++;
18+
#if __DCACHE_PRESENT
19+
SCB_CleanDCache_by_Addr(&backup_value, sizeof(backup_value));
20+
#endif
21+
22+
printk("Next reported value should be: %d\n", backup_value);
23+
printk("Keep VBAT power source and reset the board now!\n");
24+
}

0 commit comments

Comments
 (0)