Skip to content

Commit 284a1d5

Browse files
dgarskedanielinux
authored andcommitted
Peer review fixes
1 parent 980022d commit 284a1d5

5 files changed

Lines changed: 34 additions & 45 deletions

File tree

config/examples/stm32u3.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ V?=0
1818
SPMATH?=1
1919
RAM_CODE?=1
2020
DUALBANK_SWAP?=0
21-
# Flash layout for dual-bank (2x512KB, 8KB pages):
21+
# Flash layout for dual-bank (2x512KB, 4KB pages):
2222
# Bank 1 (0x08000000): wolfBoot (64KB) + BOOT partition (448KB)
23-
# Bank 2 (0x08080000): UPDATE partition (448KB) + SWAP (8KB)
23+
# Bank 2 (0x08080000): UPDATE partition (448KB) + SWAP (4KB)
2424
# Future: DUALBANK_SWAP=1 eliminates swap and uses HW bank-swap
2525
WOLFBOOT_SECTOR_SIZE?=0x1000
2626
WOLFBOOT_PARTITION_SIZE?=0x70000

docs/Targets.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,8 +1713,8 @@ arm-none-eabi-gdb
17131713
17141714
The STM32U3 family (for example the STM32U385RG on NUCLEO-U385RG-Q) is a
17151715
Cortex-M33 part **without TrustZone**, so the port is single-image only
1716-
(no `-tz` or `-ns` variants). 1 MB internal flash, 256 KB SRAM, 8 KB
1717-
pages, 128-bit (quad-word) flash write quantum.
1716+
(no `-tz` or `-ns` variants). 1 MB internal flash, 256 KB SRAM, 4 KB
1717+
pages, 64-bit (double-word) flash write quantum.
17181718
17191719
### Flash layout (stm32u3.config)
17201720
@@ -1764,17 +1764,11 @@ The test app blinks LD2 (PA5): slow on v1, fast on v2 (post-update).
17641764
17651765
### Testing an Update
17661766
1767-
Sign the test application as version 2 and write the update trigger
1768-
magic (`pBOOT`) at the tail of the partition:
1767+
Sign the test application as version 2, build the update image with the
1768+
`pBOOT` trigger magic, and flash it:
17691769
17701770
```sh
1771-
tools/keytools/sign --ecc384 --sha384 test-app/image.bin \
1772-
wolfboot_signing_private_key.der 2
1773-
echo -n "pBOOT" > trigger_magic.bin
1774-
./tools/bin-assemble/bin-assemble \
1775-
update.bin \
1776-
0x0 test-app/image_v2_signed.bin \
1777-
0x6FFFB trigger_magic.bin
1771+
./tools/scripts/prepare_update_u3.sh 2
17781772
STM32_Programmer_CLI -c port=SWD reset=HWrst \
17791773
-d update.bin 0x08080000 -v -rst
17801774
```

hal/stm32u3.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
/* STM32U3 family (e.g. NUCLEO-U385RG-Q). Cortex-M33 without TrustZone.
23-
* Always dual-bank 1 MB flash (2 x 512 KB), 8 KB pages, 64-bit
23+
* Always dual-bank 1 MB flash (2 x 512 KB), 4 KB pages, 64-bit
2424
* (double-word) write quantum.
2525
* No traditional PLL -- MSIS switches directly between MSIRC1 (24 MHz)
2626
* and MSIRC0 (96 MHz).
@@ -37,8 +37,12 @@
3737
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
3838
{
3939
uint32_t reg = FLASH_ACR;
40-
if ((reg & FLASH_ACR_LATENCY_MASK) != waitstates)
40+
if ((reg & FLASH_ACR_LATENCY_MASK) != waitstates) {
4141
FLASH_ACR = (reg & ~FLASH_ACR_LATENCY_MASK) | waitstates;
42+
/* RM: read-back to confirm LATENCY accepted before clock switch */
43+
while ((FLASH_ACR & FLASH_ACR_LATENCY_MASK) != waitstates)
44+
;
45+
}
4246
}
4347

4448
static RAMFUNCTION void flash_wait_complete(void)
@@ -83,7 +87,9 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
8387

8488
/* RM step 6: write first word, then second word */
8589
dst[i >> 2] = dword[0];
90+
ISB();
8691
dst[(i >> 2) + 1] = dword[1];
92+
ISB();
8793

8894
/* RM step 8: wait for BSY clear */
8995
flash_wait_complete();
@@ -103,15 +109,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
103109
void RAMFUNCTION hal_flash_unlock(void)
104110
{
105111
flash_wait_complete();
106-
/* Unlock both NS and Secure flash controllers */
107-
if ((FLASH_NS_CR & FLASH_CR_LOCK) != 0) {
108-
FLASH_NS_KEYR = FLASH_KEY1;
109-
DMB();
110-
FLASH_NS_KEYR = FLASH_KEY2;
111-
DMB();
112-
while ((FLASH_NS_CR & FLASH_CR_LOCK) != 0)
113-
;
114-
}
112+
/* Unlock NS flash controller (TZEN=0, secure unlock not needed) */
115113
if ((FLASH_NS_CR & FLASH_CR_LOCK) != 0) {
116114
FLASH_NS_KEYR = FLASH_KEY1;
117115
DMB();
@@ -196,13 +194,6 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
196194
return 0;
197195
}
198196

199-
static void RAMFUNCTION stm32u3_reboot(void)
200-
{
201-
AIRCR = AIRCR_SYSRESETREQ | AIRCR_VKEY;
202-
while (1)
203-
;
204-
}
205-
206197
/* --- UART: USART1 on PA9 (TX) / PA10 (RX), AF7 --- */
207198

208199
#define USART1_BASE (0x40013800U)
@@ -335,7 +326,7 @@ void hal_init(void)
335326

336327
#if defined(DEBUG_UART) && defined(__WOLFBOOT)
337328
uart_init();
338-
uart_write("wolfBoot HAL Init\n", 18);
329+
uart_write("wolfBoot HAL Init\n", sizeof("wolfBoot HAL Init\n") - 1);
339330
#endif
340331
}
341332

@@ -360,9 +351,8 @@ void RAMFUNCTION hal_cache_invalidate(void)
360351
return;
361352
if ((ICACHE_SR & ICACHE_SR_BUSYF) == 0)
362353
ICACHE_CR |= ICACHE_CR_CACHEINV;
363-
if (ICACHE_SR & ICACHE_SR_BUSYF) {
364-
while ((ICACHE_SR & ICACHE_SR_BSYENDF) == 0)
365-
;
366-
}
354+
/* Wait unconditionally for invalidation to complete */
355+
while ((ICACHE_SR & ICACHE_SR_BSYENDF) == 0)
356+
;
367357
ICACHE_SR |= ICACHE_SR_BSYENDF;
368358
}

hal/stm32u3.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
/* STM32U3 family (e.g. STM32U385RG on NUCLEO-U385RG-Q).
23-
* Cortex-M33, no TrustZone. 1 MB dual-bank flash (2 × 512 KB, 8 KB pages).
23+
* Cortex-M33, no TrustZone. 1 MB dual-bank flash (2 × 512 KB, 4 KB pages).
2424
* Reference: RM0487, stm32u385xx.h from STM32Cube_FW_U3_V1.3.0.
2525
*
2626
* Peripheral base addresses differ from STM32U5:
@@ -66,7 +66,7 @@
6666
#define RCC_ICSCR1_MSISDIV_2 (1) /* /2 */
6767
#define RCC_ICSCR1_MSISDIV_4 (2) /* /4 */
6868
#define RCC_ICSCR1_MSISDIV_8 (3) /* /8 */
69-
/* MSIS RC source: bit 31 MSISSEL — 0 = MSIRC1 (24 MHz), 1 = MSIRC0 (96 MHz) */
69+
/* MSIS RC source: bit 31 MSISSEL — 0 = MSIRC0 (96 MHz), 1 = MSIRC1 (24 MHz) */
7070
#define RCC_ICSCR1_MSISSEL (1u << 31)
7171

7272
#define RCC_CFGR1 (*(volatile uint32_t *)(RCC_BASE + 0x1C))
@@ -130,7 +130,7 @@
130130
#define FLASH_NS_SR (*(volatile uint32_t *)(FLASH_BASE + 0x20))
131131
#define FLASH_NS_CR (*(volatile uint32_t *)(FLASH_BASE + 0x28))
132132

133-
/* Secure flash registers — required for erase/write even with TZEN=0 */
133+
/* Secure flash registers (unused when TZEN=0, kept for reference) */
134134
#define FLASH_SKEYR (*(volatile uint32_t *)(FLASH_BASE + 0x0C))
135135
#define FLASH_SSR (*(volatile uint32_t *)(FLASH_BASE + 0x24))
136136
#define FLASH_SCR (*(volatile uint32_t *)(FLASH_BASE + 0x2C))

test-app/app_stm32u3.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ static void busy_delay(uint32_t count)
8383
void main(void)
8484
{
8585
uint32_t version;
86+
uint32_t v;
8687
uint32_t on_ticks, off_ticks;
88+
char num[4];
89+
int idx = 0;
8790

8891
hal_init();
8992
led_init();
@@ -93,12 +96,14 @@ void main(void)
9396

9497
version = wolfBoot_current_firmware_version();
9598

96-
{
97-
char v = '0' + (char)(version & 0x7F);
98-
char msg[] = "App version: X\r\n";
99-
msg[13] = v;
100-
uart_write(msg, sizeof(msg) - 1);
101-
}
99+
v = version;
100+
if (v >= 100) { num[idx++] = '0' + (v / 100); v %= 100; }
101+
if (v >= 10 || idx > 0) { num[idx++] = '0' + (v / 10); v %= 10; }
102+
num[idx++] = '0' + v;
103+
num[idx] = '\0';
104+
uart_write("App version: ", sizeof("App version: ") - 1);
105+
uart_write(num, idx);
106+
uart_write("\r\n", 2);
102107

103108
/* v1: slow blink. v2+: fast blink. */
104109
if (version >= 2) {

0 commit comments

Comments
 (0)