Skip to content

Commit 10532a5

Browse files
gmarullnashif
authored andcommitted
soc: arm: st_stm32: add support for STM32 backup SRAM
Add support for backup SRAM initialization found in multiple STM32 microcontrollers. Linker script facilities are also provided to make it easy to define variables in the backup SRAM. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 1e9abde commit 10532a5

File tree

8 files changed

+113
-0
lines changed

8 files changed

+113
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2021, Teslabs Engineering S.L.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
STM32 Backup SRAM.
6+
7+
With a battery connected to the VBAT pin, the backup SRAM can be used to
8+
retain data during low-power mode (Standby and VBAT mode).
9+
10+
compatible: "st,stm32-backup-sram"
11+
12+
include: base.yaml
13+
14+
properties:
15+
label:
16+
required: true
17+
18+
reg:
19+
required: true
20+
21+
clocks:
22+
required: true

include/arch/arm/aarch32/cortex_m/scripts/linker.ld

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ MEMORY
131131
#if DT_NODE_HAS_STATUS(DT_NODELABEL(sdram2), okay)
132132
SDRAM2 (rw) : ORIGIN = DT_REG_ADDR(DT_NODELABEL(sdram2)), LENGTH = DT_REG_SIZE(DT_NODELABEL(sdram2))
133133
#endif
134+
#endif
135+
#ifdef CONFIG_STM32_BACKUP_SRAM
136+
BACKUP_SRAM (rw) : ORIGIN = DT_REG_ADDR(DT_NODELABEL(backup_sram)), LENGTH = DT_REG_SIZE(DT_NODELABEL(backup_sram))
134137
#endif
135138
/* Used by and documented in include/linker/intlist.ld */
136139
IDT_LIST (wx) : ORIGIN = (RAM_ADDR + RAM_SIZE), LENGTH = 2K

include/linker/section_tags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define __imx_boot_dcd_section Z_GENERIC_SECTION(_IMX_BOOT_DCD_SECTION_NAME)
3434
#define __stm32_sdram1_section Z_GENERIC_SECTION(_STM32_SDRAM1_SECTION_NAME)
3535
#define __stm32_sdram2_section Z_GENERIC_SECTION(_STM32_SDRAM2_SECTION_NAME)
36+
#define __stm32_backup_sram_section Z_GENERIC_SECTION(_STM32_BACKUP_SRAM_SECTION_NAME)
3637
#endif /* CONFIG_ARM */
3738

3839
#if defined(CONFIG_NOCACHE_MEMORY)

include/linker/sections.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#define _STM32_SDRAM1_SECTION_NAME .stm32_sdram1
6262
#define _STM32_SDRAM2_SECTION_NAME .stm32_sdram2
6363

64+
#define _STM32_BACKUP_SRAM_SECTION_NAME .stm32_backup_sram
65+
6466
#ifdef CONFIG_NOCACHE_MEMORY
6567
#define _NOCACHE_SECTION_NAME nocache
6668
#endif

soc/arm/st_stm32/common/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ zephyr_include_directories(.)
55
zephyr_sources(stm32cube_hal.c)
66

77
zephyr_linker_sources_ifdef(CONFIG_STM32_CCM SECTIONS ccm.ld)
8+
9+
zephyr_sources_ifdef(CONFIG_STM32_BACKUP_SRAM stm32_backup_sram.c)
10+
zephyr_linker_sources_ifdef(CONFIG_STM32_BACKUP_SRAM SECTIONS stm32_backup_sram.ld)

soc/arm/st_stm32/common/Kconfig.soc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ DT_CHOSEN_Z_CCM := zephyr,ccm
88

99
config STM32_CCM
1010
def_bool $(dt_chosen_enabled,$(DT_CHOSEN_Z_CCM))
11+
12+
config STM32_BACKUP_SRAM
13+
bool "Enable STM32 Backup SRAM"
14+
depends on SOC_SERIES_STM32F2X || SOC_SERIES_STM32F4X || \
15+
SOC_SERIES_STM32F7X || SOC_SERIES_STM32H7X
16+
help
17+
Enable support for STM32 backup SRAM.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT st_stm32_backup_sram
8+
9+
#include <device.h>
10+
#include <drivers/clock_control/stm32_clock_control.h>
11+
12+
#include <stm32_ll_pwr.h>
13+
14+
#include <logging/log.h>
15+
LOG_MODULE_REGISTER(stm32_backup_sram, CONFIG_SOC_LOG_LEVEL);
16+
17+
struct stm32_backup_sram_config {
18+
struct stm32_pclken pclken;
19+
};
20+
21+
static int stm32_backup_sram_init(const struct device *dev)
22+
{
23+
const struct stm32_backup_sram_config *config = dev->config;
24+
25+
int ret;
26+
const struct device *clk;
27+
28+
/* enable backup SRAM clock */
29+
clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
30+
__ASSERT_NO_MSG(clk);
31+
32+
ret = clock_control_on(clk, (clock_control_subsys_t *)&config->pclken);
33+
if (ret < 0) {
34+
LOG_ERR("Could not initialize backup SRAM clock (%d)", ret);
35+
return ret;
36+
}
37+
38+
/* enable write access to backup domain */
39+
LL_PWR_EnableBkUpAccess();
40+
while (!LL_PWR_IsEnabledBkUpAccess()) {
41+
}
42+
43+
/* enable backup sram regulator (required to retain backup SRAM content
44+
* while in standby or VBAT modes).
45+
*/
46+
LL_PWR_EnableBkUpRegulator();
47+
while (!LL_PWR_IsEnabledBkUpRegulator()) {
48+
}
49+
50+
return 0;
51+
}
52+
53+
static const struct stm32_backup_sram_config config = {
54+
.pclken = { .bus = DT_INST_CLOCKS_CELL(0, bus),
55+
.enr = DT_INST_CLOCKS_CELL(0, bits) },
56+
};
57+
58+
DEVICE_DT_INST_DEFINE(0, stm32_backup_sram_init, device_pm_control_nop,
59+
NULL, &config, POST_KERNEL,
60+
CONFIG_APPLICATION_INIT_PRIORITY, NULL);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
GROUP_START(BACKUP_SRAM)
8+
9+
SECTION_PROLOGUE(_STM32_BACKUP_SRAM_SECTION_NAME, (NOLOAD),)
10+
{
11+
*(.stm32_backup_sram)
12+
*(".stm32_backup_sram.*")
13+
} GROUP_LINK_IN(BACKUP_SRAM)
14+
15+
GROUP_END(BACKUP_SRAM)

0 commit comments

Comments
 (0)