Skip to content

Commit b38bdc4

Browse files
committed
drivers/flash: stm32wb: Add driver for stm32wb
Add flash driver for stm32wb Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 2b84917 commit b38bdc4

File tree

9 files changed

+305
-25
lines changed

9 files changed

+305
-25
lines changed

drivers/flash/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if(CONFIG_CLOCK_CONTROL_STM32_CUBE)
2020
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32F4X flash_stm32f4x.c)
2121
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32F7X flash_stm32f7x.c)
2222
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32L4X flash_stm32l4x.c)
23+
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32WBX flash_stm32wbx.c)
2324
endif()
2425

2526
zephyr_include_directories_ifdef(

drivers/flash/Kconfig.stm32

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@ if SOC_FAMILY_STM32
1010

1111
menuconfig SOC_FLASH_STM32
1212
bool "STM32 flash driver"
13-
depends on (SOC_SERIES_STM32F0X || SOC_SERIES_STM32F3X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32L4X)
13+
depends on (SOC_SERIES_STM32F0X || SOC_SERIES_STM32F3X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32L4X || SOC_SERIES_STM32WBX)
1414
select FLASH_HAS_DRIVER_ENABLED
15+
select USE_STM32_HAL_FLASH
16+
select USE_STM32_HAL_FLASH_EX
1517
default y
1618
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F0X
1719
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F3X
1820
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F4X
1921
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F7X
2022
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32L4X
23+
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32WBX
2124
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F0X
2225
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F3X
2326
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F4X
2427
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F7X
2528
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32L4X
29+
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32WBX
2630
help
27-
Enable STM32F0x, STM32F3x, STM32F4x, STM32F7x OR STM32L4x series flash driver.
31+
Enable STM32F0x, STM32F3x, STM32F4x, STM32F7x, STM32L4x or
32+
STM32WBx series flash driver.
2833

2934
endif

drivers/flash/flash_stm32.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
/* STM32L4: maximum erase time of 24.47ms for a 2K sector */
3030
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
3131
#define STM32_FLASH_TIMEOUT (K_MSEC(25))
32+
/* TBC: STM32WB: maximum erase time of 24.47ms for a 2K sector */
33+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
34+
#define STM32_FLASH_TIMEOUT (K_MSEC(25))
3235
#endif
3336

3437
/*
@@ -46,6 +49,7 @@ static inline void flash_stm32_sem_give(struct device *dev)
4649
k_sem_give(&FLASH_STM32_PRIV(dev)->sem);
4750
}
4851

52+
#if !defined(CONFIG_SOC_SERIES_STM32WBX)
4953
static int flash_stm32_check_status(struct device *dev)
5054
{
5155
u32_t const error =
@@ -75,6 +79,7 @@ static int flash_stm32_check_status(struct device *dev)
7579

7680
return 0;
7781
}
82+
#endif /* CONFIG_SOC_SERIES_STM32L5X */
7883

7984
int flash_stm32_wait_flash_idle(struct device *dev)
8085
{
@@ -86,7 +91,7 @@ int flash_stm32_wait_flash_idle(struct device *dev)
8691
return -EIO;
8792
}
8893

89-
while ((FLASH_STM32_REGS(dev)->sr & FLASH_SR_BSY)) {
94+
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {
9095
if (k_uptime_get() > timeout_time) {
9196
return -EIO;
9297
}
@@ -95,20 +100,25 @@ int flash_stm32_wait_flash_idle(struct device *dev)
95100
return 0;
96101
}
97102

103+
98104
static void flash_stm32_flush_caches(struct device *dev,
99105
off_t offset, size_t len)
100106
{
101107
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32F3X)
102108
ARG_UNUSED(dev);
103109
ARG_UNUSED(offset);
104110
ARG_UNUSED(len);
105-
#elif defined(CONFIG_SOC_SERIES_STM32F4X) || defined(CONFIG_SOC_SERIES_STM32L4X)
111+
#elif defined(CONFIG_SOC_SERIES_STM32F4X) || \
112+
defined(CONFIG_SOC_SERIES_STM32L4X) || \
113+
defined(CONFIG_SOC_SERIES_STM32WBX)
106114
ARG_UNUSED(offset);
107115
ARG_UNUSED(len);
108116
#if defined(CONFIG_SOC_SERIES_STM32F4X)
109117
struct stm32f4x_flash *regs = FLASH_STM32_REGS(dev);
110118
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
111119
struct stm32l4x_flash *regs = FLASH_STM32_REGS(dev);
120+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
121+
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
112122
#endif
113123
if (regs->acr.val & FLASH_ACR_DCEN) {
114124
regs->acr.val &= ~FLASH_ACR_DCEN;
@@ -185,17 +195,6 @@ static int flash_stm32_write(struct device *dev, off_t offset,
185195

186196
static int flash_stm32_write_protection(struct device *dev, bool enable)
187197
{
188-
#if defined(CONFIG_SOC_SERIES_STM32F4X)
189-
struct stm32f4x_flash *regs = FLASH_STM32_REGS(dev);
190-
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
191-
struct stm32f7x_flash *regs = FLASH_STM32_REGS(dev);
192-
#elif defined(CONFIG_SOC_SERIES_STM32F0X)
193-
struct stm32f0x_flash *regs = FLASH_STM32_REGS(dev);
194-
#elif defined(CONFIG_SOC_SERIES_STM32F3X)
195-
struct stm32f3x_flash *regs = FLASH_STM32_REGS(dev);
196-
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
197-
struct stm32l4x_flash *regs = FLASH_STM32_REGS(dev);
198-
#endif
199198
int rc = 0;
200199

201200
flash_stm32_sem_take(dev);
@@ -206,17 +205,9 @@ static int flash_stm32_write_protection(struct device *dev, bool enable)
206205
flash_stm32_sem_give(dev);
207206
return rc;
208207
}
209-
regs->cr |= FLASH_CR_LOCK;
208+
HAL_FLASH_Lock();
210209
} else {
211-
if (regs->cr & FLASH_CR_LOCK) {
212-
regs->keyr = FLASH_KEY1;
213-
#ifdef CONFIG_SOC_SERIES_STM32F3X
214-
/* On STM32F3 series some time is requested */
215-
/* before writing again on key register */
216-
flash_stm32_wait_flash_idle(dev);
217-
#endif /* CONFIG_SOC_SERIES_STM32F3X */
218-
regs->keyr = FLASH_KEY2;
219-
}
210+
HAL_FLASH_Unlock();
220211
}
221212

222213
flash_stm32_sem_give(dev);
@@ -241,6 +232,8 @@ static struct flash_stm32_priv flash_data = {
241232
.regs = (struct stm32l4x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
242233
.pclken = { .bus = STM32_CLOCK_BUS_AHB1,
243234
.enr = LL_AHB1_GRP1_PERIPH_FLASH },
235+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
236+
.regs = (struct stm32wbx_flash *) DT_FLASH_DEV_BASE_ADDRESS,
244237
#endif
245238
};
246239

drivers/flash/flash_stm32.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct flash_stm32_priv {
3434
struct stm32l4x_flash *regs;
3535
/* clock subsystem driving this peripheral */
3636
struct stm32_pclken pclken;
37+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
38+
struct stm32wbx_flash *regs;
3739
#endif
3840
struct k_sem sem;
3941
};
@@ -64,6 +66,10 @@ int flash_stm32_block_erase_loop(struct device *dev, unsigned int offset,
6466

6567
int flash_stm32_wait_flash_idle(struct device *dev);
6668

69+
#ifdef CONFIG_SOC_SERIES_STM32WBX
70+
int flash_stm32_check_status(struct device *dev);
71+
#endif /* CONFIG_SOC_SERIES_STM32WBX */
72+
6773
#ifdef CONFIG_FLASH_PAGE_LAYOUT
6874
void flash_stm32_page_layout(struct device *dev,
6975
const struct flash_pages_layout **layout,

drivers/flash/flash_stm32wbx.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
* Copyright (c) 2019 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define LOG_DOMAIN flash_stm32wb
8+
#define LOG_LEVEL CONFIG_FLASH_LOG_LEVEL
9+
#include <logging/log.h>
10+
LOG_MODULE_REGISTER(LOG_DOMAIN);
11+
12+
#include <kernel.h>
13+
#include <device.h>
14+
#include <string.h>
15+
#include <flash.h>
16+
#include <init.h>
17+
#include <soc.h>
18+
#include <misc/__assert.h>
19+
20+
#include "flash_stm32.h"
21+
22+
#define STM32WBX_PAGE_SHIFT 12
23+
24+
/* offset and len must be aligned on 8 for write
25+
* , positive and not beyond end of flash */
26+
bool flash_stm32_valid_range(struct device *dev, off_t offset, u32_t len,
27+
bool write)
28+
{
29+
return (!write || (offset % 8 == 0 && len % 8 == 0)) &&
30+
flash_stm32_range_exists(dev, offset, len);
31+
}
32+
33+
/*
34+
* Up to 255 4K pages
35+
*/
36+
static u32_t get_page(off_t offset)
37+
{
38+
return offset >> STM32WBX_PAGE_SHIFT;
39+
}
40+
41+
static int check_flash_unlock()
42+
{
43+
int rc = 0;
44+
45+
/* verify Flash is unlocked */
46+
if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) == 0u) {
47+
rc = -EIO;
48+
}
49+
50+
return rc;
51+
}
52+
53+
static int write_dword(struct device *dev, off_t offset, u64_t val)
54+
{
55+
u32_t address = (u32_t)(offset + CONFIG_FLASH_BASE_ADDRESS);
56+
HAL_StatusTypeDef rc;
57+
58+
/* if the control register is locked, do not fail silently */
59+
if (!check_flash_unlock()) {
60+
return -EIO;
61+
}
62+
63+
flash_stm32_check_status(dev);
64+
/* Perform the data write operation at the desired memory address */
65+
rc = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, val);
66+
67+
if (rc != HAL_OK) {
68+
return -EIO;
69+
}
70+
71+
return 0;
72+
}
73+
74+
static int erase_page(struct device *dev, u32_t page)
75+
{
76+
int rc;
77+
78+
/* if the control register is locked, do not fail silently */
79+
if (!check_flash_unlock()) {
80+
return -EIO;
81+
}
82+
83+
/* Check that no Flash memory operation is ongoing */
84+
rc = flash_stm32_wait_flash_idle(dev);
85+
if (rc < 0) {
86+
return rc;
87+
}
88+
89+
/* Check erase operation allowed */
90+
if (HAL_FLASHEx_IsOperationSuspended()) {
91+
return -EBUSY;
92+
}
93+
94+
/* Proceed to erase the page */
95+
MODIFY_REG(FLASH->CR, (FLASH_CR_PNB | FLASH_CR_PER), \
96+
((page << FLASH_CR_PNB_Pos) | FLASH_CR_PER));
97+
98+
SET_BIT(FLASH->CR, FLASH_CR_STRT);
99+
100+
/* Wait for the BSY bit */
101+
rc = flash_stm32_wait_flash_idle(dev);
102+
103+
CLEAR_BIT(FLASH->CR, FLASH_TYPEERASE_PAGES);
104+
105+
return rc;
106+
}
107+
108+
int flash_stm32_block_erase_loop(struct device *dev, unsigned int offset,
109+
unsigned int len)
110+
{
111+
int i, rc = 0;
112+
113+
i = get_page(offset);
114+
for (; i <= get_page(offset + len - 1) ; ++i) {
115+
rc = erase_page(dev, i);
116+
if (rc < 0) {
117+
break;
118+
}
119+
}
120+
121+
return rc;
122+
}
123+
124+
int flash_stm32_write_range(struct device *dev, unsigned int offset,
125+
const void *data, unsigned int len)
126+
{
127+
int i, rc = 0;
128+
129+
for (i = 0; i < len; i += 8, offset += 8) {
130+
rc = write_dword(dev, offset,
131+
UNALIGNED_GET((const u64_t *) data + (i >> 3)));
132+
if (rc < 0) {
133+
return rc;
134+
}
135+
}
136+
137+
return rc;
138+
}
139+
140+
void flash_stm32_page_layout(struct device *dev,
141+
const struct flash_pages_layout **layout,
142+
size_t *layout_size)
143+
{
144+
static struct flash_pages_layout stm32wb_flash_layout = {
145+
.pages_count = 0,
146+
.pages_size = 0,
147+
};
148+
149+
ARG_UNUSED(dev);
150+
151+
if (stm32wb_flash_layout.pages_count == 0) {
152+
stm32wb_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
153+
stm32wb_flash_layout.pages_size = FLASH_PAGE_SIZE;
154+
}
155+
156+
*layout = &stm32wb_flash_layout;
157+
*layout_size = 1;
158+
}
159+
160+
int flash_stm32_check_status(struct device *dev)
161+
{
162+
u32_t error = 0;
163+
164+
/* Save Flash errors */
165+
error = (FLASH->SR & FLASH_FLAG_SR_ERROR);
166+
error |= (FLASH->ECCR & FLASH_FLAG_ECCC);
167+
168+
/* TODO: Clean this */
169+
if (error & FLASH_FLAG_OPTVERR) {
170+
FLASH->SR |= FLASH_FLAG_SR_ERROR;
171+
return 0;
172+
}
173+
174+
if (error) {
175+
return -EIO;
176+
}
177+
178+
return 0;
179+
}

dts/arm/st/wb/stm32wb.dtsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
flash0: flash@8000000 {
4949
compatible = "soc-nv-flash";
5050
label = "FLASH_STM32";
51+
52+
write-block-size = <8>;
53+
erase-block-size = <4096>;
5154
};
5255
};
5356

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: STM32 WB Flash Controller
3+
version: 0.1
4+
5+
description: >
6+
This binding gives a base representation of the STM32 wb Flash Controller
7+
8+
inherits:
9+
!include flash-controller.yaml
10+
11+
properties:
12+
compatible:
13+
constraint: "st,stm32wb-flash-controller"
14+
15+
single-bank:
16+
type: boolean
17+
description: dual-bank mode not enabled (page erase 4096k)
18+
generation: define
19+
category: optionnal
20+
21+
dual-bank:
22+
type: boolean
23+
description: dual-bank mode enabled (page erase 2048k)
24+
generation: define
25+
category: optionnal
26+
...

soc/arm/st_stm32/stm32wb/dts_fixup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@
7878
#define DT_UART_STM32_LPUART_1_CLOCK_BITS DT_ST_STM32_LPUART_40008000_CLOCK_BITS
7979
#define DT_UART_STM32_LPUART_1_CLOCK_BUS DT_ST_STM32_LPUART_40008000_CLOCK_BUS
8080

81+
#define DT_FLASH_DEV_BASE_ADDRESS DT_ST_STM32WB_FLASH_CONTROLLER_58004000_BASE_ADDRESS
82+
#define DT_FLASH_DEV_NAME DT_ST_STM32WB_FLASH_CONTROLLER_58004000_LABEL
83+
84+
8185
/* End of SoC Level DTS fixup file */

0 commit comments

Comments
 (0)