From c6cd49d913f0a57bed0168c08521f41373fada7f Mon Sep 17 00:00:00 2001 From: M-Byte Date: Wed, 8 Jun 2022 09:42:54 +0200 Subject: [PATCH] GD32 flash workaround Addressing #181 At least some GD32 devices seem to require unlocking the flash twice. As the unlock function exits with `HAL_OK` if the flash is already unlocked, STM32 devices can run the same code without issues. --- src/stm32_platform.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/stm32_platform.cpp b/src/stm32_platform.cpp index a0d9fca..fd6ea37 100644 --- a/src/stm32_platform.cpp +++ b/src/stm32_platform.cpp @@ -60,6 +60,11 @@ void Stm32Platform::commitToEeprom() return; for (uint16_t i = 0; i < _eepromSize; ++i) eeprom_buffered_write_byte(i, _eepromPtr[i]); + // For some GD32 chips, the flash needs to be unlocked twice + // and the first call will fail. If the first call is + // successful, the second one (inside eeprom_buffer_flush) + // does nothing. + HAL_FLASH_Unlock(); eeprom_buffer_flush(); }