From 7d932b88f81c2472abeb13b364d05bde8dfe6262 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Sun, 1 Aug 2021 16:25:27 -0400 Subject: [PATCH] flash_stm32: don't unlock flash if already unlocked On stm32f4 after reboot from DFU it was observed that the flash is already unlocked. In that case, attempting to unlock it again causes a data abort. --- tmk_core/common/chibios/flash_stm32.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tmk_core/common/chibios/flash_stm32.c b/tmk_core/common/chibios/flash_stm32.c index 0bead21039..35c6a433d8 100644 --- a/tmk_core/common/chibios/flash_stm32.c +++ b/tmk_core/common/chibios/flash_stm32.c @@ -175,9 +175,11 @@ FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { * @retval None */ void FLASH_Unlock(void) { - /* Authorize the FPEC Access */ - FLASH->KEYR = FLASH_KEY1; - FLASH->KEYR = FLASH_KEY2; + if (FLASH->CR & FLASH_CR_LOCK) { + /* Authorize the FPEC Access */ + FLASH->KEYR = FLASH_KEY1; + FLASH->KEYR = FLASH_KEY2; + } } /**