Skip to content

Commit f9f10cb

Browse files
danielinuxdgarske
authored andcommitted
Added support for STM32WB
1 parent ff96298 commit f9f10cb

File tree

5 files changed

+442
-2
lines changed

5 files changed

+442
-2
lines changed

docs/Targets.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Example 192KB partitioning on STM32-L073
4444
4545
This device is capable of erasing single flash pages (256B each).
4646
47-
However, we choose to use a logic sector size of 4KB for the swaps, to limit the amount of
47+
However, we choose to use a logic sector size of 4KB for the swaps, to limit the amount of
4848
writes to the swap partition.
4949
5050
The proposed geometry in this example `target.h` uses 32KB for wolfBoot, and two
@@ -88,12 +88,39 @@ Compile with:
8888

8989
`make TARGET=stm32g0 NVM_FLASH_WRITEONCE=1`
9090

91+
## STM32WB55
92+
93+
Example partitioning on Nucleo-68 board:
94+
95+
- Sector size: 4KB
96+
- Wolfboot partition size: 32 KB
97+
- Applicatiobn partition size: 128 KB
98+
99+
```C
100+
#define WOLFBOOT_SECTOR_SIZE 0x1000 /* 4 KB */
101+
#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0x8000
102+
#define WOLFBOOT_PARTITION_SIZE 0x20000 /* 128 KB */
103+
#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0x28000
104+
#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0x48000
105+
```
106+
107+
### Building
108+
109+
Use `make TARGET=stm32wb`.
110+
111+
The option `NVM_FLASH_WRITEONCE=1` is mandatory on this target, since the IAP driver does not support
112+
multiple writes after each erase operation.
113+
114+
Compile with:
115+
116+
`make TARGET=stm32wb NVM_FLASH_WRITEONCE=1`
117+
91118
## SiFive HiFive1 RISC-V
92119

93120
### Features
94121
* E31 RISC-V 320MHz 32-bit processor
95122
* Onboard 16KB scratchpad RAM
96-
* External 4MB QSPI Flash
123+
* External 4MB QSPI Flash
97124

98125
### Default Linker Settings
99126
* FLASH: Address 0x20000000, Len 0x6a120 (424 KB)

hal/stm32wb.c

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
/* stm32wb.c
2+
*
3+
* Copyright (C) 2019 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#include <stdint.h>
23+
#include <image.h>
24+
/* STM32 WB register configuration */
25+
26+
/* Assembly helpers */
27+
#define DMB() __asm__ volatile ("dmb")
28+
29+
/*** RCC ***/
30+
31+
#define RCC_BASE (0x58000000)
32+
#define RCC_CR (*(volatile uint32_t *)(RCC_BASE + 0x00))
33+
#define RCC_CFGR (*(volatile uint32_t *)(RCC_BASE + 0x08))
34+
#define RCC_PLLCFGR (*(volatile uint32_t *)(RCC_BASE + 0x0C))
35+
36+
#define RCC_CR_PLLRDY (1 << 25)
37+
#define RCC_CR_PLLON (1 << 24)
38+
#define RCC_CR_MSIRDY (1 << 1)
39+
#define RCC_CR_MSION (1 << 0)
40+
#define RCC_CR_HSIRDY (1 << 10)
41+
#define RCC_CR_HSION (1 << 8)
42+
#define RCC_CR_MSIRANGE_SHIFT 4
43+
#define RCC_CR_MSIRANGE_MASK (0x0F << 4)
44+
#define RCC_CR_MSIRANGE_6 (0x06 << 4)
45+
46+
#define RCC_CFGR_SW_MSI 0x0
47+
#define RCC_CFGR_SW_PLL 0x3
48+
#define RCC_CFGR_SW_MASK 0x3
49+
50+
#define RCC_CFGR_HPRE_MASK 0x0F
51+
#define RCC_CFGR_PPRE1_MASK 0x07
52+
#define RCC_CFGR_PPRE2_MASK 0x07
53+
#define RCC_CFGR_HPRE_SHIFT 4
54+
#define RCC_CFGR_PPRE1_SHIFT 8
55+
#define RCC_CFGR_PPRE2_SHIFT 11
56+
57+
#define RCC_PLLCFGR_SRC_SHIFT 0
58+
#define RCC_PLLCFGR_PLLSRC_MSI 0x1
59+
#define RCC_PLLCFGR_PLLSRC_MASK 0x3
60+
#define RCC_PLLCFGR_PLLM_DIV2 (0x1 << 4)
61+
#define RCC_PLLCFGR_PLLM_MASK (0x3 << 4)
62+
#define RCC_PLLCFGR_PLLN_32 (32 << 8)
63+
#define RCC_PLLCFGR_PLLN_MASK (0x7f << 8)
64+
#define RCC_PLLCFGR_PLLP_DIV5 (4 << 17)
65+
#define RCC_PLLCFGR_PLLP_MASK (0x7 << 17)
66+
#define RCC_PLLCFGR_PLLQ_DIV4 (3 << 25)
67+
#define RCC_PLLCFGR_PLLQ_MASK (0x7 << 25)
68+
#define RCC_PLLCFGR_PLLR_DIV2 (1 << 29)
69+
#define RCC_PLLCFGR_PLLR_MASK (0x7 << 29)
70+
#define RCC_PLLCFGR_PLLP_EN (1 << 16)
71+
#define RCC_PLLCFGR_PLLQ_EN (1 << 24)
72+
#define RCC_PLLCFGR_PLLR_EN (1 << 28)
73+
74+
#define RCC_PRESCALER_DIV_NONE 0
75+
76+
77+
/*** FLASH ***/
78+
#define FLASH_BASE (0x58004000)
79+
#define FLASH_ACR (*(volatile uint32_t *)(FLASH_BASE + 0x00)) //RM0444 - 3.7.1 - FLASH_ACR
80+
#define FLASH_KEY (*(volatile uint32_t *)(FLASH_BASE + 0x08)) //RM0444 - 3.7.2 - FLASH_KEYR
81+
#define FLASH_SR (*(volatile uint32_t *)(FLASH_BASE + 0x10)) //RM0444 - 3.7.4 - FLASH_SR
82+
#define FLASH_CR (*(volatile uint32_t *)(FLASH_BASE + 0x14)) //RM0444 - 3.7.5 - FLASH_CR
83+
84+
#define FLASHMEM_ADDRESS_SPACE (0x08000000)
85+
#define FLASH_PAGE_SIZE (0x800) /* 2KB */
86+
87+
/* Register values */
88+
#define FLASH_ACR_LATENCY_MASK (0x07)
89+
90+
#define FLASH_SR_BSY (1 << 16)
91+
#define FLASH_SR_SIZERR (1 << 6)
92+
#define FLASH_SR_PGAERR (1 << 5)
93+
#define FLASH_SR_WRPERR (1 << 4)
94+
#define FLASH_SR_PROGERR (1 << 3)
95+
#define FLASH_SR_EOP (1 << 0)
96+
97+
#define FLASH_CR_LOCK (1 << 31)
98+
#define FLASH_CR_STRT (1 << 16)
99+
100+
#define FLASH_CR_PER (1 << 1)
101+
#define FLASH_CR_PG (1 << 0)
102+
103+
#define FLASH_CR_PNB_SHIFT 3
104+
#define FLASH_CR_PNB_MASK 0x3f
105+
106+
#define FLASH_KEY1 (0x45670123)
107+
#define FLASH_KEY2 (0xCDEF89AB)
108+
109+
110+
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
111+
{
112+
uint32_t reg = FLASH_ACR;
113+
if ((reg & FLASH_ACR_LATENCY_MASK) != waitstates)
114+
FLASH_ACR |= ((reg & ~FLASH_ACR_LATENCY_MASK) | waitstates);
115+
}
116+
117+
static RAMFUNCTION void flash_wait_complete(void)
118+
{
119+
while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY)
120+
;
121+
}
122+
123+
static void RAMFUNCTION flash_clear_errors(void)
124+
{
125+
FLASH_SR |= ( FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_PROGERR);
126+
}
127+
128+
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
129+
{
130+
int i = 0;
131+
uint32_t *src, *dst;
132+
flash_clear_errors();
133+
FLASH_CR |= FLASH_CR_PG;
134+
135+
while (i < len) {
136+
flash_clear_errors();
137+
if ((len - i > 3) && ((((address + i) & 0x07) == 0) && ((((uint32_t)data) + i) & 0x07) == 0)) {
138+
src = (uint32_t *)data;
139+
dst = (uint32_t *)(address + FLASHMEM_ADDRESS_SPACE);
140+
flash_wait_complete();
141+
dst[i >> 2] = src[i >> 2];
142+
dst[(i >> 2) + 1] = src[(i >> 2) + 1];
143+
flash_wait_complete();
144+
i+=8;
145+
} else {
146+
uint32_t val[2];
147+
uint8_t *vbytes = (uint8_t *)(val);
148+
int off = (address + i) - (((address + i) >> 3) << 3);
149+
uint32_t base_addr = address & (~0x07); /* aligned to 64 bit */
150+
int u32_idx = (i >> 2);
151+
dst = (uint32_t *)(base_addr);
152+
val[0] = dst[u32_idx];
153+
val[1] = dst[u32_idx + 1];
154+
while ((off < 8) && (i < len))
155+
vbytes[off++] = data[i++];
156+
dst[u32_idx] = val[0];
157+
dst[u32_idx + 1] = val[1];
158+
flash_wait_complete();
159+
}
160+
}
161+
if ((FLASH_SR & FLASH_SR_EOP) == FLASH_SR_EOP)
162+
FLASH_SR |= FLASH_SR_EOP;
163+
FLASH_CR &= ~FLASH_CR_PG;
164+
return 0;
165+
}
166+
167+
void RAMFUNCTION hal_flash_unlock(void)
168+
{
169+
flash_wait_complete();
170+
if ((FLASH_CR & FLASH_CR_LOCK) != 0) {
171+
FLASH_KEY = FLASH_KEY1;
172+
DMB();
173+
FLASH_KEY = FLASH_KEY2;
174+
DMB();
175+
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
176+
;
177+
}
178+
}
179+
180+
void RAMFUNCTION hal_flash_lock(void)
181+
{
182+
flash_wait_complete();
183+
if ((FLASH_CR & FLASH_CR_LOCK) == 0)
184+
FLASH_CR |= FLASH_CR_LOCK;
185+
}
186+
187+
188+
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
189+
{
190+
int start = -1, end = -1;
191+
uint32_t end_address;
192+
uint32_t p;
193+
if (len == 0)
194+
return -1;
195+
end_address = address + len - 1;
196+
for (p = address; p < end_address; p += FLASH_PAGE_SIZE) {
197+
uint32_t reg = FLASH_CR & (~(FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT));
198+
FLASH_CR = reg | ((p >> 11) << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER;
199+
DMB();
200+
FLASH_CR |= FLASH_CR_STRT;
201+
flash_wait_complete();
202+
FLASH_CR &= ~FLASH_CR_PER;
203+
}
204+
return 0;
205+
}
206+
207+
static void clock_pll_off(void)
208+
{
209+
uint32_t reg32;
210+
/* Enable internal high-speed oscillator. */
211+
RCC_CR |= RCC_CR_MSION;
212+
DMB();
213+
while ((RCC_CFGR & RCC_CR_MSIRDY) == 0) {};
214+
/* Select MSI as SYSCLK source. */
215+
reg32 = RCC_CFGR;
216+
reg32 &= ~(RCC_CFGR_SW_MASK);
217+
DMB();
218+
/* Turn off PLL */
219+
RCC_CR &= ~RCC_CR_PLLON;
220+
DMB();
221+
}
222+
223+
static void clock_pll_on(void)
224+
{
225+
uint32_t reg32;
226+
uint32_t cpu_freq, pllm, plln, pllp,pllq, pllr;
227+
uint32_t hpre, ppre1, ppre2;
228+
uint32_t flash_waitstates;
229+
230+
/* Select clock parameters (CPU Speed = 64MHz) */
231+
cpu_freq = 64000000;
232+
flash_waitstates = 4;
233+
flash_set_waitstates(flash_waitstates);
234+
235+
/* Configure + enable internal high-speed oscillator. */
236+
RCC_CR = (RCC_CR & (~RCC_CR_MSIRANGE_MASK)) | RCC_CR_MSIRANGE_6;
237+
RCC_CR |= RCC_CR_MSION;
238+
DMB();
239+
while ((RCC_CR & RCC_CR_MSIRDY) == 0)
240+
;
241+
/* Select MSI as SYSCLK source. */
242+
reg32 = RCC_CFGR;
243+
reg32 &= ~(RCC_CFGR_SW_MASK);
244+
RCC_CFGR = (reg32 | RCC_CFGR_SW_MSI);
245+
DMB();
246+
/*
247+
* Set prescalers
248+
*/
249+
hpre = RCC_PRESCALER_DIV_NONE;
250+
ppre1 = RCC_PRESCALER_DIV_NONE;
251+
ppre2 = RCC_PRESCALER_DIV_NONE;
252+
reg32 = RCC_CFGR;
253+
reg32 &= ~(RCC_CFGR_HPRE_MASK << RCC_CFGR_HPRE_SHIFT);
254+
RCC_CFGR = (hpre & RCC_CFGR_HPRE_MASK) << RCC_CFGR_HPRE_SHIFT;
255+
DMB();
256+
reg32 = RCC_CFGR;
257+
reg32 &= ~(RCC_CFGR_PPRE1_MASK << RCC_CFGR_PPRE1_SHIFT);
258+
RCC_CFGR = (reg32 | (ppre1 << RCC_CFGR_PPRE1_SHIFT));
259+
DMB();
260+
reg32 &= ~(RCC_CFGR_PPRE2_MASK << RCC_CFGR_PPRE2_SHIFT);
261+
RCC_CFGR = (reg32 | (ppre2 << RCC_CFGR_PPRE2_SHIFT));
262+
DMB();
263+
/* Set PLLCFGR parameter */
264+
RCC_PLLCFGR = RCC_PLLCFGR_PLLM_DIV2 | RCC_PLLCFGR_PLLN_32 |
265+
RCC_PLLCFGR_PLLP_DIV5 | RCC_PLLCFGR_PLLQ_DIV4 |
266+
RCC_PLLCFGR_PLLR_DIV2 | RCC_PLLCFGR_PLLP_EN |
267+
RCC_PLLCFGR_PLLQ_EN | RCC_PLLCFGR_PLLR_EN |
268+
RCC_PLLCFGR_PLLSRC_MSI;
269+
270+
/* Enable PLL oscillator and wait for it to stabilize. */
271+
RCC_CR |= RCC_CR_PLLON;
272+
DMB();
273+
while ((RCC_CR & RCC_CR_PLLRDY) == 0)
274+
;
275+
276+
/* Select PLL as SYSCLK source. */
277+
reg32 = RCC_CFGR;
278+
reg32 &= ~(RCC_CFGR_SW_MASK);
279+
RCC_CFGR = (reg32 | RCC_CFGR_SW_PLL);
280+
DMB();
281+
/* Wait for PLL clock to be selected (via SWS, bits 3:2) */
282+
while (((RCC_CFGR >> 2) & RCC_CFGR_SW_MASK) != RCC_CFGR_SW_PLL)
283+
;
284+
}
285+
286+
void hal_init(void)
287+
{
288+
clock_pll_on();
289+
}
290+
291+
void hal_prepare_boot(void)
292+
{
293+
#ifdef SPI_FLASH
294+
spi_release();
295+
#endif
296+
hal_flash_lock();
297+
clock_pll_off();
298+
}
299+

hal/stm32wb.ld

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
MEMORY
2+
{
3+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x8000
4+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00030000
5+
}
6+
7+
SECTIONS
8+
{
9+
.text :
10+
{
11+
_start_text = .;
12+
KEEP(*(.isr_vector))
13+
*(.text*)
14+
*(.rodata*)
15+
. = ALIGN(4);
16+
_end_text = .;
17+
} > FLASH
18+
19+
_stored_data = .;
20+
.data : AT (_stored_data)
21+
{
22+
_start_data = .;
23+
KEEP(*(.data*))
24+
. = ALIGN(4);
25+
KEEP(*(.ramcode))
26+
. = ALIGN(4);
27+
_end_data = .;
28+
} > RAM
29+
30+
.bss (NOLOAD) :
31+
{
32+
_start_bss = .;
33+
__bss_start__ = .;
34+
*(.bss*)
35+
*(COMMON)
36+
. = ALIGN(4);
37+
_end_bss = .;
38+
__bss_end__ = .;
39+
_end = .;
40+
} > RAM
41+
. = ALIGN(4);
42+
}
43+
44+
END_STACK = ORIGIN(RAM) + LENGTH(RAM);

0 commit comments

Comments
 (0)