Skip to content

Commit c9649bd

Browse files
niedzwiecki-dawidaescolar
authored andcommitted
tests: flash: stm32: add block registers tests
Add a test of FLASH_STM32_EX_OP_BLOCK_OPTION_REG and FLASH_STM32_EX_OP_BLOCK_CONTROL_REG extended operations for stm32f4. It verifies that the Option Byte and Control registers are blocked correctly. The registers can be unlock after reboot, so it is needed to separate this test from other tests. Signed-off-by: Dawid Niedzwiecki <[email protected]>
1 parent 27c3378 commit c9649bd

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

tests/drivers/flash/stm32/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ CONFIG_ZTEST=y
33

44
CONFIG_FLASH=y
55
CONFIG_FLASH_EX_OP_ENABLED=y
6-
CONFIG_FLASH_STM32_BLOCK_REGISTERS=y
76

87
CONFIG_MAIN_STACK_SIZE=2048

tests/drivers/flash/stm32/src/main.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define TEST_AREA_SIZE FIXED_PARTITION_SIZE(TEST_AREA)
1717
#define TEST_AREA_MAX (TEST_AREA_OFFSET + TEST_AREA_SIZE)
1818
#define TEST_AREA_DEVICE FIXED_PARTITION_DEVICE(TEST_AREA)
19+
#define TEST_AREA_DEVICE_REG DT_REG_ADDR(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(TEST_AREA)))
1920

2021
#define EXPECTED_SIZE 512
2122

@@ -179,4 +180,71 @@ ZTEST(flash_stm32, test_stm32_readout_protection_disabled)
179180
}
180181
#endif
181182

183+
#ifdef CONFIG_FLASH_STM32_BLOCK_REGISTERS
184+
185+
#if defined(CONFIG_FLASH_STM32_WRITE_PROTECT) || defined(CONFIG_FLASH_STM32_READOUT_PROTECTION)
186+
#error Block Register tests unable to run other tests, because of locked registers.
187+
#endif
188+
189+
int flash_stm32_option_bytes_lock(const struct device *dev, bool enable);
190+
191+
static bool flash_opt_locked(void)
192+
{
193+
FLASH_TypeDef *regs = (FLASH_TypeDef *)TEST_AREA_DEVICE_REG;
194+
195+
return regs->OPTCR & FLASH_OPTCR_OPTLOCK;
196+
}
197+
198+
static void flash_cr_unlock(void)
199+
{
200+
FLASH_TypeDef *regs = (FLASH_TypeDef *)TEST_AREA_DEVICE_REG;
201+
202+
regs->KEYR = FLASH_KEY1;
203+
regs->KEYR = FLASH_KEY2;
204+
}
205+
206+
static bool flash_cr_locked(void)
207+
{
208+
FLASH_TypeDef *regs = (FLASH_TypeDef *)TEST_AREA_DEVICE_REG;
209+
210+
return regs->CR & FLASH_CR_LOCK;
211+
}
212+
213+
ZTEST(flash_stm32, test_stm32_block_registers)
214+
{
215+
/* Test OPT lock. */
216+
TC_PRINT("Unlocking OPT\n");
217+
flash_stm32_option_bytes_lock(flash_dev, false);
218+
zassert_false(flash_opt_locked(), "Unable to unlock OPT");
219+
220+
TC_PRINT("Blocking OPT\n");
221+
flash_ex_op(flash_dev, FLASH_STM32_EX_OP_BLOCK_OPTION_REG, (uintptr_t)NULL, NULL);
222+
223+
zassert_true(flash_opt_locked(), "Blocking OPT didn't lock OPT");
224+
TC_PRINT("Try to unlock blocked OPT\n");
225+
__set_FAULTMASK(1);
226+
flash_stm32_option_bytes_lock(flash_dev, false);
227+
/* Clear Bus Fault pending bit */
228+
SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk;
229+
__set_FAULTMASK(0);
230+
zassert_true(flash_opt_locked(), "OPT unlocked after being blocked");
231+
232+
/* Test CR lock. */
233+
zassert_true(flash_cr_locked(), "CR should be locked by default");
234+
TC_PRINT("Unlocking CR\n");
235+
flash_cr_unlock();
236+
zassert_false(flash_cr_locked(), "Unable to unlock CR");
237+
TC_PRINT("Blocking CR\n");
238+
flash_ex_op(flash_dev, FLASH_STM32_EX_OP_BLOCK_CONTROL_REG, (uintptr_t)NULL, NULL);
239+
zassert_true(flash_cr_locked(), "Blocking CR didn't lock CR");
240+
__set_FAULTMASK(1);
241+
TC_PRINT("Try to unlock blocked CR\n");
242+
flash_cr_unlock();
243+
/* Clear Bus Fault pending bit */
244+
SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk;
245+
__set_FAULTMASK(0);
246+
zassert_true(flash_cr_locked(), "CR unlocked after being blocked");
247+
}
248+
#endif
249+
182250
ZTEST_SUITE(flash_stm32, NULL, flash_stm32_setup, NULL, NULL, NULL);

tests/drivers/flash/stm32/testcase.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ tests:
1212
- CONFIG_FLASH_STM32_READOUT_PROTECTION=y
1313
filter: dt_compat_enabled("st,stm32f4-flash-controller") and
1414
dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions")
15+
drivers.flash.stm32.f4.block_registers:
16+
platform_allow:
17+
- nucleo_f429zi
18+
- google_dragonclaw
19+
extra_configs:
20+
- CONFIG_FLASH_STM32_BLOCK_REGISTERS=y
21+
filter: dt_compat_enabled("st,stm32f4-flash-controller") and
22+
dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions")
1523
drivers.flash.stm32.l4:
1624
platform_allow:
1725
- nucleo_l452re/stm32l452xx/p

0 commit comments

Comments
 (0)