]> granicus.if.org Git - esp-idf/commitdiff
components/spi_flash: call SPIUnlock only once
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 13 Sep 2016 07:16:36 +0000 (15:16 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 13 Sep 2016 07:16:36 +0000 (15:16 +0800)
This fixes the performance impact for spi_flash_write and spi_flash_erase.
With this change, NVS init in single core mode takes about 50ms (compared to >2seconds before that).

components/spi_flash/esp_spi_flash.c

index 65d4c709ddecd0f72bc52f2b21b6c39f34a14d17..b0a31e8201c9026cc5199749cf6f3187605bfb95 100644 (file)
@@ -178,11 +178,24 @@ static void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
 #endif // CONFIG_FREERTOS_UNICORE
 
 
+SpiFlashOpResult IRAM_ATTR spi_flash_unlock()
+{
+    static bool unlocked = false;
+    if (!unlocked) {
+        SpiFlashOpResult rc = SPIUnlock();
+        if (rc != SPI_FLASH_RESULT_OK) {
+            return rc;
+        }
+        unlocked = true;
+    }
+    return SPI_FLASH_RESULT_OK;
+}
+
 esp_err_t IRAM_ATTR spi_flash_erase_sector(uint16_t sec)
 {
     spi_flash_disable_interrupts_caches_and_other_cpu();
     SpiFlashOpResult rc;
-    rc = SPIUnlock();
+    rc = spi_flash_unlock();
     if (rc == SPI_FLASH_RESULT_OK) {
         rc = SPIEraseSector(sec);
     }
@@ -194,7 +207,7 @@ esp_err_t IRAM_ATTR spi_flash_write(uint32_t dest_addr, const uint32_t *src, uin
 {
     spi_flash_disable_interrupts_caches_and_other_cpu();
     SpiFlashOpResult rc;
-    rc = SPIUnlock();
+    rc = spi_flash_unlock();
     if (rc == SPI_FLASH_RESULT_OK) {
         rc = SPIWrite(dest_addr, src, (int32_t) size);
     }