10
10
#include <zephyr/drivers/flash/stm32_flash_api_extensions.h>
11
11
#include <zephyr/devicetree.h>
12
12
#include <zephyr/storage/flash_map.h>
13
+ #include <zephyr/sys/barrier.h>
13
14
14
15
#define TEST_AREA storage_partition
15
16
#define TEST_AREA_OFFSET FIXED_PARTITION_OFFSET(TEST_AREA)
@@ -200,15 +201,46 @@ static void flash_cr_unlock(void)
200
201
{
201
202
FLASH_TypeDef * regs = (FLASH_TypeDef * )TEST_AREA_DEVICE_REG ;
202
203
204
+ #ifdef CONFIG_SOC_SERIES_STM32H7X
205
+ regs -> KEYR1 = FLASH_KEY1 ;
206
+ regs -> KEYR1 = FLASH_KEY2 ;
207
+ #ifdef DUAL_BANK
208
+ regs -> KEYR2 = FLASH_KEY1 ;
209
+ regs -> KEYR2 = FLASH_KEY2 ;
210
+ #endif /* DUAL_BANK */
211
+ #else /* CONFIG_SOC_SERIES_STM32H7X */
203
212
regs -> KEYR = FLASH_KEY1 ;
204
213
regs -> KEYR = FLASH_KEY2 ;
214
+ #endif /* CONFIG_SOC_SERIES_STM32H7X */
215
+ barrier_dsync_fence_full ();
205
216
}
206
217
207
- static bool flash_cr_locked (void )
218
+ static bool flash_cr_is_locked (void )
208
219
{
209
220
FLASH_TypeDef * regs = (FLASH_TypeDef * )TEST_AREA_DEVICE_REG ;
210
221
222
+ #ifdef CONFIG_SOC_SERIES_STM32H7X
223
+ return regs -> CR1 & FLASH_CR_LOCK ;
224
+ #ifdef DUAL_BANK
225
+ return (regs -> CR1 & FLASH_CR_LOCK ) && (regs -> CR2 & FLASH_CR_LOCK );
226
+ #endif /* DUAL_BANK */
227
+ #else /* CONFIG_SOC_SERIES_STM32H7X */
211
228
return regs -> CR & FLASH_CR_LOCK ;
229
+ #endif /* CONFIG_SOC_SERIES_STM32H7X */
230
+ }
231
+
232
+ static bool flash_cr_is_unlocked (void )
233
+ {
234
+ FLASH_TypeDef * regs = (FLASH_TypeDef * )TEST_AREA_DEVICE_REG ;
235
+
236
+ #ifdef CONFIG_SOC_SERIES_STM32H7X
237
+ return !(regs -> CR1 & FLASH_CR_LOCK );
238
+ #ifdef DUAL_BANK
239
+ return !((regs -> CR1 & FLASH_CR_LOCK ) || (regs -> CR2 & FLASH_CR_LOCK ));
240
+ #endif /* DUAL_BANK */
241
+ #else /* CONFIG_SOC_SERIES_STM32H7X */
242
+ return !(regs -> CR & FLASH_CR_LOCK );
243
+ #endif /* CONFIG_SOC_SERIES_STM32H7X */
212
244
}
213
245
214
246
ZTEST (flash_stm32 , test_stm32_block_registers )
@@ -227,24 +259,27 @@ ZTEST(flash_stm32, test_stm32_block_registers)
227
259
flash_stm32_option_bytes_lock (flash_dev , false);
228
260
/* Clear Bus Fault pending bit */
229
261
SCB -> SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk ;
262
+ barrier_dsync_fence_full ();
230
263
__set_FAULTMASK (0 );
231
264
zassert_true (flash_opt_locked (), "OPT unlocked after being blocked" );
232
265
233
266
/* Test CR lock. */
234
- zassert_true (flash_cr_locked (), "CR should be locked by default" );
267
+ zassert_true (flash_cr_is_locked (), "CR should be locked by default" );
235
268
TC_PRINT ("Unlocking CR\n" );
236
269
flash_cr_unlock ();
237
- zassert_false ( flash_cr_locked (), "Unable to unlock CR" );
270
+ zassert_true ( flash_cr_is_unlocked (), "Unable to unlock CR" );
238
271
TC_PRINT ("Blocking CR\n" );
239
272
flash_ex_op (flash_dev , FLASH_STM32_EX_OP_BLOCK_CONTROL_REG , (uintptr_t )NULL , NULL );
240
- zassert_true (flash_cr_locked (), "Blocking CR didn't lock CR" );
273
+ zassert_true (flash_cr_is_locked (), "Blocking CR didn't lock CR" );
241
274
__set_FAULTMASK (1 );
242
275
TC_PRINT ("Try to unlock blocked CR\n" );
243
276
flash_cr_unlock ();
244
277
/* Clear Bus Fault pending bit */
245
278
SCB -> SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk ;
279
+ barrier_dsync_fence_full ();
246
280
__set_FAULTMASK (0 );
247
- zassert_true (flash_cr_locked (), "CR unlocked after being blocked" );
281
+ /* Make sure previous write is completed. */
282
+ zassert_true (flash_cr_is_locked (), "CR unlocked after being blocked" );
248
283
}
249
284
#endif
250
285
0 commit comments