From 29c6eab6dc93a0a9e4f00ca47fa311f663a84149 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 13 Sep 2016 15:16:36 +0800 Subject: [PATCH] components/spi_flash: call SPIUnlock only once 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 | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/components/spi_flash/esp_spi_flash.c b/components/spi_flash/esp_spi_flash.c index 65d4c709dd..b0a31e8201 100644 --- a/components/spi_flash/esp_spi_flash.c +++ b/components/spi_flash/esp_spi_flash.c @@ -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); }