@@ -97,7 +97,8 @@ PKA_HandleTypeDef hpka = { };
9797#define FLASH_ACR_LATENCY_MASK (0x07)
9898
9999#ifndef WOLFSSL_STM32_PKA
100- #define FLASH_SR_BSY (1 << 16)
100+ #define FLASH_SR_BSY (1 << 16)
101+ #define FLASH_SR_CFGBSY (1 << 18)
101102#define FLASH_SR_SIZERR (1 << 6)
102103#define FLASH_SR_PGAERR (1 << 5)
103104#define FLASH_SR_WRPERR (1 << 4)
@@ -109,11 +110,12 @@ PKA_HandleTypeDef hpka = { };
109110
110111#define FLASH_CR_PER (1 << 1)
111112#define FLASH_CR_PG (1 << 0)
113+ #define FLASH_CR_FSTPG (1 << 18)
112114
113115#endif /* !WOLFSSL_STM32_PKA */
114116
115117#define FLASH_CR_PNB_SHIFT 3
116- #define FLASH_CR_PNB_MASK 0x3f
118+ #define FLASH_CR_PNB_MASK 0xFF
117119
118120#define FLASH_KEY1 (0x45670123)
119121#define FLASH_KEY2 (0xCDEF89AB)
@@ -128,7 +130,7 @@ static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
128130
129131static RAMFUNCTION void flash_wait_complete (void )
130132{
131- while ((FLASH_SR & FLASH_SR_BSY ) == FLASH_SR_BSY )
133+ while ((FLASH_SR & ( FLASH_SR_BSY | FLASH_SR_CFGBSY )) != 0 )
132134 ;
133135}
134136
@@ -137,21 +139,50 @@ static void RAMFUNCTION flash_clear_errors(void)
137139 FLASH_SR |= ( FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_PROGERR );
138140}
139141
142+
143+
144+ void RAMFUNCTION hal_flash_unlock (void )
145+ {
146+ flash_wait_complete ();
147+ if ((FLASH_CR & FLASH_CR_LOCK ) != 0 ) {
148+ FLASH_KEY = FLASH_KEY1 ;
149+ DMB ();
150+ FLASH_KEY = FLASH_KEY2 ;
151+ DMB ();
152+ while ((FLASH_CR & FLASH_CR_LOCK ) != 0 )
153+ ;
154+ }
155+ }
156+
157+ void RAMFUNCTION hal_flash_lock (void )
158+ {
159+ flash_wait_complete ();
160+ if ((FLASH_CR & FLASH_CR_LOCK ) == 0 )
161+ FLASH_CR |= FLASH_CR_LOCK ;
162+ }
163+
140164int RAMFUNCTION hal_flash_write (uint32_t address , const uint8_t * data , int len )
141165{
142166 int i = 0 ;
143167 uint32_t * src , * dst ;
168+ uint32_t pdword [2 ] __attribute__((aligned (16 )));
169+ uint32_t reg ;
170+
144171 flash_clear_errors ();
145- FLASH_CR |= FLASH_CR_PG ;
172+ reg = FLASH_CR & (~FLASH_CR_FSTPG );
173+ FLASH_CR = reg | FLASH_CR_PG ;
146174
147175 while (i < len ) {
148176 flash_clear_errors ();
149177 if ((len - i > 3 ) && ((((address + i ) & 0x07 ) == 0 ) && ((((uint32_t )data ) + i ) & 0x07 ) == 0 )) {
178+ uint32_t idx = i >> 2 ;
150179 src = (uint32_t * )data ;
151- dst = (uint32_t * )(address + FLASHMEM_ADDRESS_SPACE );
180+ dst = (uint32_t * )(address );
181+ pdword [0 ] = src [idx ];
182+ pdword [1 ] = src [idx + 1 ];
152183 flash_wait_complete ();
153- dst [i >> 2 ] = src [ i >> 2 ];
154- dst [( i >> 2 ) + 1 ] = src [( i >> 2 ) + 1 ];
184+ dst [idx ] = pdword [ 0 ];
185+ dst [idx + 1 ] = pdword [ 1 ];
155186 flash_wait_complete ();
156187 i += 8 ;
157188 } else {
@@ -176,42 +207,26 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
176207 return 0 ;
177208}
178209
179- void RAMFUNCTION hal_flash_unlock (void )
180- {
181- flash_wait_complete ();
182- if ((FLASH_CR & FLASH_CR_LOCK ) != 0 ) {
183- FLASH_KEY = FLASH_KEY1 ;
184- DMB ();
185- FLASH_KEY = FLASH_KEY2 ;
186- DMB ();
187- while ((FLASH_CR & FLASH_CR_LOCK ) != 0 )
188- ;
189- }
190- }
191-
192- void RAMFUNCTION hal_flash_lock (void )
193- {
194- flash_wait_complete ();
195- if ((FLASH_CR & FLASH_CR_LOCK ) == 0 )
196- FLASH_CR |= FLASH_CR_LOCK ;
197- }
198-
199210
200211int RAMFUNCTION hal_flash_erase (uint32_t address , int len )
201212{
202- int start = -1 , end = -1 ;
203213 uint32_t end_address ;
204214 uint32_t p ;
205215 if (len == 0 )
206216 return -1 ;
217+ address -= FLASHMEM_ADDRESS_SPACE ;
207218 end_address = address + len - 1 ;
219+ flash_wait_complete ();
208220 for (p = address ; p < end_address ; p += FLASH_PAGE_SIZE ) {
209- uint32_t reg = FLASH_CR & (~(FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT ));
210- FLASH_CR = reg | ((p >> 12 ) << FLASH_CR_PNB_SHIFT ) | FLASH_CR_PER | FLASH_CR_PG ;
221+ uint32_t reg ;
222+ flash_clear_errors ();
223+ reg = FLASH_CR & ~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT ) | FLASH_CR_FSTPG | FLASH_CR_PG );
224+ FLASH_CR = reg | ((p >> 12 ) << FLASH_CR_PNB_SHIFT ) | FLASH_CR_PER ;
211225 DMB ();
212226 FLASH_CR |= FLASH_CR_STRT ;
227+ DMB ();
213228 flash_wait_complete ();
214- FLASH_CR &= ~(FLASH_CR_PER | FLASH_CR_PG );
229+ FLASH_CR &= ~(FLASH_CR_PER );
215230 }
216231 return 0 ;
217232}
@@ -310,7 +325,6 @@ void hal_prepare_boot(void)
310325#ifdef SPI_FLASH
311326 spi_release ();
312327#endif
313- hal_flash_lock ();
314328 clock_pll_off ();
315329}
316330
0 commit comments