From 2848cc1cae494f9f75f5e725aeac2e3c93d63355 Mon Sep 17 00:00:00 2001 From: richardclli Date: Tue, 11 Aug 2026 06:12:23 +0800 Subject: [PATCH] chore(spiflash): improve the efficiency of "is block erased" detection (#7582) --- .../targets/common/arm/stm32/spi_flash.cpp | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/radio/src/targets/common/arm/stm32/spi_flash.cpp b/radio/src/targets/common/arm/stm32/spi_flash.cpp index c14a65325..7e8f77210 100644 --- a/radio/src/targets/common/arm/stm32/spi_flash.cpp +++ b/radio/src/targets/common/arm/stm32/spi_flash.cpp @@ -270,8 +270,10 @@ uint32_t flashSpiGetSize() bool flashSpiIsErased(uint32_t address) { - // verify sector alignment - if((address & FLASH_SECTOR_MASK) != 0) + static uint8_t temp_buffer[FLASH_SECTOR_SIZE] __DMA_NO_CACHE; + + // Verify sector alignment + if ((address & FLASH_SECTOR_MASK) != 0) return false; flash_wait_for_not_busy(); @@ -283,17 +285,25 @@ bool flashSpiIsErased(uint32_t address) flash_put_cmd_addr(FLASH_CMD_READ, address); } - bool ret = true; - for(uint32_t i = 0; i < FLASH_SECTOR_SIZE; i++) { - uint8_t byte = flash_rw_byte(0xFF); - if (byte != 0xff) { - ret = false; - break; + // Check first 4 bytes + for (uint32_t i = 0; i < 4; i++) { + if (flash_rw_byte(0xFF) != 0xff) { + flash_unselect(); + return false; } } + // First 4 bytes all 0xFF, now need to check the remaining using DMA + flash_dma_read_bytes(temp_buffer, FLASH_SECTOR_SIZE - 4); flash_unselect(); - return ret; + + for (uint32_t i = 0; i < FLASH_SECTOR_SIZE - 4; i++) { + if (temp_buffer[i] != 0xff) { + return false; + } + } + + return true; } uint32_t flashSpiRead(uint32_t address, uint8_t* data, uint32_t size)