Skip to content

Commit 519f5ff

Browse files
str4t0mcfriedt
authored andcommitted
drivers: flash: stm32: mv security-mode dependent defines to header
An attempt to simplify the ifdef-ery around FLASH_SR is made. Define Registers and flags in the header file instead of including several individual operations in the driver. FLASH_FLAG_BSY is not only defined for STM32L5, but also other series. Therefore use this flag instead of FLASH_SR_BSY. Only the g0 series definition is not valid in our context, therefore use FLASH_SR_BSY1 instead. No functional changes, only refactoring. Signed-off-by: Thomas Stranger <[email protected]>
1 parent d184181 commit 519f5ff

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

drivers/flash/flash_stm32.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ LOG_MODULE_REGISTER(flash_stm32, CONFIG_FLASH_LOG_LEVEL);
3030
#define STM32_FLASH_TIMEOUT \
3131
(2 * DT_PROP(DT_INST(0, st_stm32_nv_flash), max_erase_time))
3232

33-
#if defined(FLASH_NSSR_NSBSY) /* For STM32L5x in non-secure mode */
34-
#define FLASH_SECURITY_NS
35-
#elif defined(FLASH_SECSR_SECBSY) /* For STM32L5x in secured mode */
36-
#error Flash is not supported in secure mode
37-
#define FLASH_SECURITY_SEC
38-
#else
39-
#define FLASH_SECURITY_NA /* For series which does not have
40-
* secured or non-secured mode
41-
*/
42-
#endif
43-
4433
static const struct flash_parameters flash_stm32_parameters = {
4534
.write_block_size = FLASH_STM32_WRITE_BLOCK_SIZE,
4635
/* Some SoCs (L0/L1) use an EEPROM under the hood. Distinguish
@@ -113,13 +102,9 @@ static int flash_stm32_check_status(const struct device *dev)
113102
#endif
114103
FLASH_FLAG_WRPERR;
115104

116-
#if defined(FLASH_SECURITY_NS)
117-
if (FLASH_STM32_REGS(dev)->NSSR & error) {
118-
LOG_DBG("Status: 0x%08x", FLASH_STM32_REGS(dev)->NSSR & error);
119-
#else /* FLASH_SECURITY_SEC | FLASH_SECURITY_NA */
120-
if (FLASH_STM32_REGS(dev)->SR & error) {
121-
LOG_DBG("Status: 0x%08x", FLASH_STM32_REGS(dev)->SR & error);
122-
#endif /* FLASH_SECURITY_NS */
105+
if (FLASH_STM32_REGS(dev)->FLASH_STM32_SR & error) {
106+
LOG_DBG("Status: 0x%08x",
107+
FLASH_STM32_REGS(dev)->FLASH_STM32_SR & error);
123108
return -EIO;
124109
}
125110

@@ -131,21 +116,16 @@ int flash_stm32_wait_flash_idle(const struct device *dev)
131116
{
132117
int64_t timeout_time = k_uptime_get() + STM32_FLASH_TIMEOUT;
133118
int rc;
119+
uint32_t busy_flags;
134120

135121
rc = flash_stm32_check_status(dev);
136122
if (rc < 0) {
137123
return -EIO;
138124
}
139-
#if defined(FLASH_SECURITY_NS)
140-
while ((FLASH_STM32_REGS(dev)->NSSR & FLASH_FLAG_BSY)) {
141-
#else /* FLASH_SECURITY_SEC | FLASH_SECURITY_NA */
142-
#if defined(FLASH_SR_BSY1)
143-
/* Applicable for STM32G0 series */
144-
while ((FLASH_STM32_REGS(dev)->SR & FLASH_SR_BSY1)) {
145-
#else
146-
while ((FLASH_STM32_REGS(dev)->SR & FLASH_SR_BSY)) {
147-
#endif
148-
#endif /* FLASH_SECURITY_NS */
125+
126+
busy_flags = FLASH_STM32_SR_BUSY;
127+
128+
while ((FLASH_STM32_REGS(dev)->FLASH_STM32_SR & busy_flags)) {
149129
if (k_uptime_get() > timeout_time) {
150130
LOG_ERR("Timeout! val: %d", STM32_FLASH_TIMEOUT);
151131
return -EIO;

drivers/flash/flash_stm32.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,33 @@ struct flash_stm32_priv {
3333
/* as flash node property 'write-block-size' */
3434
#endif
3535

36+
/* Differentiate between arm trust-zone non-secure/secure, and others. */
37+
#if defined(FLASH_NSSR_NSBSY) /* For mcu w. TZ in non-secure mode */
38+
#define FLASH_SECURITY_NS
39+
#define FLASH_STM32_SR NSSR
40+
#elif defined(FLASH_SECSR_SECBSY) /* For mcu w. TZ in secured mode */
41+
#error Flash is not supported in secure mode
42+
#define FLASH_SECURITY_SEC
43+
#else
44+
#define FLASH_SECURITY_NA /* For series which does not have
45+
* secured or non-secured mode
46+
*/
47+
#define FLASH_STM32_SR SR
48+
#endif
49+
50+
3651
#define FLASH_STM32_PRIV(dev) ((struct flash_stm32_priv *)((dev)->data))
3752
#define FLASH_STM32_REGS(dev) (FLASH_STM32_PRIV(dev)->regs)
3853

54+
55+
/* Redefintions of flags and masks to harmonize stm32 series: */
56+
#if defined(CONFIG_SOC_SERIES_STM32G0X)
57+
#define FLASH_STM32_SR_BUSY (FLASH_SR_BSY1)
58+
#else
59+
#define FLASH_STM32_SR_BUSY (FLASH_FLAG_BSY)
60+
#endif
61+
62+
3963
#ifdef CONFIG_FLASH_PAGE_LAYOUT
4064
static inline bool flash_stm32_range_exists(const struct device *dev,
4165
off_t offset,

0 commit comments

Comments
 (0)