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 <dawidn@google.com>
This commit is contained in:
Dawid Niedzwiecki 2024-05-13 08:59:38 +02:00 committed by Alberto Escolar
commit c9649bdef8
3 changed files with 76 additions and 1 deletions

View file

@ -3,6 +3,5 @@ CONFIG_ZTEST=y
CONFIG_FLASH=y CONFIG_FLASH=y
CONFIG_FLASH_EX_OP_ENABLED=y CONFIG_FLASH_EX_OP_ENABLED=y
CONFIG_FLASH_STM32_BLOCK_REGISTERS=y
CONFIG_MAIN_STACK_SIZE=2048 CONFIG_MAIN_STACK_SIZE=2048

View file

@ -16,6 +16,7 @@
#define TEST_AREA_SIZE FIXED_PARTITION_SIZE(TEST_AREA) #define TEST_AREA_SIZE FIXED_PARTITION_SIZE(TEST_AREA)
#define TEST_AREA_MAX (TEST_AREA_OFFSET + TEST_AREA_SIZE) #define TEST_AREA_MAX (TEST_AREA_OFFSET + TEST_AREA_SIZE)
#define TEST_AREA_DEVICE FIXED_PARTITION_DEVICE(TEST_AREA) #define TEST_AREA_DEVICE FIXED_PARTITION_DEVICE(TEST_AREA)
#define TEST_AREA_DEVICE_REG DT_REG_ADDR(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(TEST_AREA)))
#define EXPECTED_SIZE 512 #define EXPECTED_SIZE 512
@ -179,4 +180,71 @@ ZTEST(flash_stm32, test_stm32_readout_protection_disabled)
} }
#endif #endif
#ifdef CONFIG_FLASH_STM32_BLOCK_REGISTERS
#if defined(CONFIG_FLASH_STM32_WRITE_PROTECT) || defined(CONFIG_FLASH_STM32_READOUT_PROTECTION)
#error Block Register tests unable to run other tests, because of locked registers.
#endif
int flash_stm32_option_bytes_lock(const struct device *dev, bool enable);
static bool flash_opt_locked(void)
{
FLASH_TypeDef *regs = (FLASH_TypeDef *)TEST_AREA_DEVICE_REG;
return regs->OPTCR & FLASH_OPTCR_OPTLOCK;
}
static void flash_cr_unlock(void)
{
FLASH_TypeDef *regs = (FLASH_TypeDef *)TEST_AREA_DEVICE_REG;
regs->KEYR = FLASH_KEY1;
regs->KEYR = FLASH_KEY2;
}
static bool flash_cr_locked(void)
{
FLASH_TypeDef *regs = (FLASH_TypeDef *)TEST_AREA_DEVICE_REG;
return regs->CR & FLASH_CR_LOCK;
}
ZTEST(flash_stm32, test_stm32_block_registers)
{
/* Test OPT lock. */
TC_PRINT("Unlocking OPT\n");
flash_stm32_option_bytes_lock(flash_dev, false);
zassert_false(flash_opt_locked(), "Unable to unlock OPT");
TC_PRINT("Blocking OPT\n");
flash_ex_op(flash_dev, FLASH_STM32_EX_OP_BLOCK_OPTION_REG, (uintptr_t)NULL, NULL);
zassert_true(flash_opt_locked(), "Blocking OPT didn't lock OPT");
TC_PRINT("Try to unlock blocked OPT\n");
__set_FAULTMASK(1);
flash_stm32_option_bytes_lock(flash_dev, false);
/* Clear Bus Fault pending bit */
SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk;
__set_FAULTMASK(0);
zassert_true(flash_opt_locked(), "OPT unlocked after being blocked");
/* Test CR lock. */
zassert_true(flash_cr_locked(), "CR should be locked by default");
TC_PRINT("Unlocking CR\n");
flash_cr_unlock();
zassert_false(flash_cr_locked(), "Unable to unlock CR");
TC_PRINT("Blocking CR\n");
flash_ex_op(flash_dev, FLASH_STM32_EX_OP_BLOCK_CONTROL_REG, (uintptr_t)NULL, NULL);
zassert_true(flash_cr_locked(), "Blocking CR didn't lock CR");
__set_FAULTMASK(1);
TC_PRINT("Try to unlock blocked CR\n");
flash_cr_unlock();
/* Clear Bus Fault pending bit */
SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk;
__set_FAULTMASK(0);
zassert_true(flash_cr_locked(), "CR unlocked after being blocked");
}
#endif
ZTEST_SUITE(flash_stm32, NULL, flash_stm32_setup, NULL, NULL, NULL); ZTEST_SUITE(flash_stm32, NULL, flash_stm32_setup, NULL, NULL, NULL);

View file

@ -12,6 +12,14 @@ tests:
- CONFIG_FLASH_STM32_READOUT_PROTECTION=y - CONFIG_FLASH_STM32_READOUT_PROTECTION=y
filter: dt_compat_enabled("st,stm32f4-flash-controller") and filter: dt_compat_enabled("st,stm32f4-flash-controller") and
dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions") dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions")
drivers.flash.stm32.f4.block_registers:
platform_allow:
- nucleo_f429zi
- google_dragonclaw
extra_configs:
- CONFIG_FLASH_STM32_BLOCK_REGISTERS=y
filter: dt_compat_enabled("st,stm32f4-flash-controller") and
dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions")
drivers.flash.stm32.l4: drivers.flash.stm32.l4:
platform_allow: platform_allow:
- nucleo_l452re/stm32l452xx/p - nucleo_l452re/stm32l452xx/p