]> granicus.if.org Git - esp-idf/commitdiff
spi_flash_erase_range: Allow for 32KB blocks not 64KB blocks
authorAngus Gratton <angus@espressif.com>
Thu, 24 Nov 2016 05:58:36 +0000 (16:58 +1100)
committerAngus Gratton <angus@espressif.com>
Thu, 24 Nov 2016 07:05:06 +0000 (18:05 +1100)
SPI flash hardware sends 52h command, which is a 32KB erase.

There is a matching bug in the ROM SPIEraseArea code, unless
flashchip->block_size is modified first.

components/esp32/include/rom/spi_flash.h
components/spi_flash/flash_ops.c

index 1f14c6617ae79c62d621d2d45c8651adadbf714d..32f018477a605195506f26b98ceb33c54ed8a4bc 100644 (file)
@@ -384,7 +384,8 @@ SpiFlashOpResult SPIParamCfg(uint32_t deviceId, uint32_t chip_size, uint32_t blo
 SpiFlashOpResult SPIEraseChip(void);
 
 /**
-  * @brief Erase a block of flash.
+  * @brief Erase a 32KB block of flash
+  *        Uses SPI flash command 52h.
   *        Please do not call this function in SDK.
   *
   * @param  uint32_t block_num : Which block to erase.
@@ -411,6 +412,12 @@ SpiFlashOpResult SPIEraseSector(uint32_t sector_num);
   * @brief Erase some sectors.
   *        Please do not call this function in SDK.
   *
+  * @note If calling this function, first set
+  *       g_rom_flashchip.block_size = 32768; or call SPIParamCfg()
+  *       with appropriate parameters. This is due to a ROM bug, the
+  *       block erase command in use is a 32KB erase but after reset
+  *       the block_size field is incorrectly set to 65536.
+  *
   * @param  uint32_t start_addr : Start addr to erase, should be sector aligned.
   *
   * @param  uint32_t area_len : Length to erase, should be sector aligned.
index 3358c550f206fd174235702617bafcaf82be27b1..72ed1336c643c35e28591eb21793d0ddf3a111ef 100644 (file)
@@ -31,6 +31,9 @@
 #include "esp_log.h"
 #include "cache_utils.h"
 
+/* bytes erased by SPIEraseBlock() ROM function */
+#define BLOCK_ERASE_SIZE 32768
+
 #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
 static const char* TAG = "spi_flash";
 static spi_flash_counters_t s_flash_stats;
@@ -100,7 +103,7 @@ esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
     }
     size_t start = start_addr / SPI_FLASH_SEC_SIZE;
     size_t end = start + size / SPI_FLASH_SEC_SIZE;
-    const size_t sectors_per_block = 16;
+    const size_t sectors_per_block = BLOCK_ERASE_SIZE / SPI_FLASH_SEC_SIZE;
     COUNTER_START();
     spi_flash_disable_interrupts_caches_and_other_cpu();
     SpiFlashOpResult rc;