diff --git a/arduino/beken-72xx/port/flashdb/fal_flash_bk72xx_port.c b/arduino/beken-72xx/port/flashdb/fal_flash_bk72xx_port.c index 9e2ace7..abca7a0 100644 --- a/arduino/beken-72xx/port/flashdb/fal_flash_bk72xx_port.c +++ b/arduino/beken-72xx/port/flashdb/fal_flash_bk72xx_port.c @@ -1,5 +1,6 @@ /* Copyright (c) Kuba SzczodrzyƄski 2022-06-19. */ +#include #include #include @@ -8,15 +9,27 @@ #define FLASH_ERASE_MIN_SIZE (4 * 1024) extern uint32_t flash_ctrl(uint32_t cmd, void *param); +extern PROTECT_TYPE get_flash_protect(); extern void flash_protection_op(uint8_t mode, PROTECT_TYPE type); +static void unprotect() { + PROTECT_TYPE type = get_flash_protect(); + if (type != FLASH_PROTECT_NONE) { + flash_protection_op(0, FLASH_PROTECT_NONE); +#if LT_LOGLEVEL <= LT_LEVEL_DEBUG + LT_D("Flash protect: %u -> %u", type, get_flash_protect()); + uint16_t sr = 0; + flash_ctrl(CMD_FLASH_READ_SR, &sr); + LT_D("SR = %04x", sr); +#endif + } +} + static int init() { __wrap_bk_printf_disable(); flash_init(); -#if CFG_SOC_NAME == SOC_BK7231N flash_ctrl(CMD_FLASH_WRITE_ENABLE, NULL); - flash_protection_op(0, FLASH_PROTECT_NONE); -#endif + unprotect(); __wrap_bk_printf_enable(); return 0; } @@ -27,11 +40,13 @@ static int read(long offset, uint8_t *buf, size_t size) { } static int write(long offset, const uint8_t *buf, size_t size) { + unprotect(); flash_write((char *)buf, size, offset); return size; } static int erase(long offset, size_t size) { + unprotect(); size = ((size - 1) / FLASH_ERASE_MIN_SIZE) + 1; for (uint16_t i = 0; i < size; i++) { uint32_t addr = offset + i * FLASH_ERASE_MIN_SIZE;