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).
3737static 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
4448static 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)
103109void 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}
0 commit comments