From 63e24a9ff9a969ea286652c90ec8cecbde561dce Mon Sep 17 00:00:00 2001 From: Armando Date: Sun, 25 Jun 2023 15:20:15 +0800 Subject: [PATCH 1/3] spiflash: fix not calling on_spi_acquired when CONFIG_SPI_FLASH_SHARE_SPI1_BUS issue --- components/spi_flash/spi_flash_os_func_app.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index 2cdce6c2c4..0f910a4c23 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -91,15 +91,16 @@ static IRAM_ATTR esp_err_t spi_end(void *arg) static IRAM_ATTR esp_err_t spi1_start(void *arg) { + esp_err_t ret = ESP_OK; #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS //use the lock to disable the cache and interrupts before using the SPI bus - return spi_start(arg); + ret = spi_start(arg); #else //directly disable the cache and interrupts when lock is not used cache_disable(NULL); - on_spi1_acquired((spi1_app_func_arg_t*)arg); - return ESP_OK; #endif + on_spi1_acquired((spi1_app_func_arg_t*)arg); + return ret; } static IRAM_ATTR esp_err_t spi1_end(void *arg) From 0fe635f11161a6257e7abccc2b43a850ef466f85 Mon Sep 17 00:00:00 2001 From: Armando Date: Sun, 25 Jun 2023 15:22:42 +0800 Subject: [PATCH 2/3] spi_flash: fix concurrency issue when calling esp_flash apis under xip_psram or auto_suspen --- components/spi_flash/spi_flash_os_func_app.c | 25 ++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index 0f910a4c23..23bdd06e38 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -21,8 +21,13 @@ #include "driver/spi_common_internal.h" +#define SPI_FLASH_CACHE_NO_DISABLE (CONFIG_SPI_FLASH_AUTO_SUSPEND || CONFIG_APP_BUILD_TYPE_ELF_RAM) static const char TAG[] = "spi_flash"; +#if SPI_FLASH_CACHE_NO_DISABLE +static _lock_t s_spi1_flash_mutex; +#endif // #if SPI_FLASH_CACHE_NO_DISABLE + /* * OS functions providing delay service and arbitration among chips, and with the cache. * @@ -59,16 +64,12 @@ static inline IRAM_ATTR bool on_spi1_check_yield(spi1_app_func_arg_t* ctx); IRAM_ATTR static void cache_enable(void* arg) { -#ifndef CONFIG_SPI_FLASH_AUTO_SUSPEND g_flash_guard_default_ops.end(); -#endif } IRAM_ATTR static void cache_disable(void* arg) { -#ifndef CONFIG_SPI_FLASH_AUTO_SUSPEND g_flash_guard_default_ops.start(); -#endif } static IRAM_ATTR esp_err_t spi_start(void *arg) @@ -92,9 +93,19 @@ static IRAM_ATTR esp_err_t spi_end(void *arg) static IRAM_ATTR esp_err_t spi1_start(void *arg) { esp_err_t ret = ESP_OK; + /** + * There are three ways for ESP Flash API lock: + * 1. spi bus lock, this is used when SPI1 is shared with GPSPI Master Driver + * 2. mutex, this is used when the Cache isn't need to be disabled. + * 3. cache lock (from cache_utils.h), this is used when we need to disable Cache to avoid access from SPI0 + * + * From 1 to 3, the lock efficiency decreases. + */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS //use the lock to disable the cache and interrupts before using the SPI bus ret = spi_start(arg); +#elif SPI_FLASH_CACHE_NO_DISABLE + _lock_acquire(&s_spi1_flash_mutex); #else //directly disable the cache and interrupts when lock is not used cache_disable(NULL); @@ -106,8 +117,14 @@ static IRAM_ATTR esp_err_t spi1_start(void *arg) static IRAM_ATTR esp_err_t spi1_end(void *arg) { esp_err_t ret = ESP_OK; + + /** + * There are three ways for ESP Flash API lock, see `spi1_start` + */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS ret = spi_end(arg); +#elif SPI_FLASH_CACHE_NO_DISABLE + _lock_release(&s_spi1_flash_mutex); #else cache_enable(NULL); #endif From fb231a26d9816770aad3196ade25da2f99071872 Mon Sep 17 00:00:00 2001 From: Armando Date: Mon, 26 Jun 2023 19:37:06 +0800 Subject: [PATCH 3/3] spi_flash: rename spi_flash_os_func_app: spi_start, spi_end spi_start -> s_acquire_spi_bus_lock spi_end -> s_release_spi_bus_lock --- components/spi_flash/spi_flash_os_func_app.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index 23bdd06e38..71012e5f36 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -72,7 +72,7 @@ IRAM_ATTR static void cache_disable(void* arg) g_flash_guard_default_ops.start(); } -static IRAM_ATTR esp_err_t spi_start(void *arg) +static IRAM_ATTR esp_err_t acquire_spi_bus_lock(void *arg) { spi_bus_lock_dev_handle_t dev_lock = ((app_func_arg_t *)arg)->dev_lock; @@ -85,7 +85,7 @@ static IRAM_ATTR esp_err_t spi_start(void *arg) return ESP_OK; } -static IRAM_ATTR esp_err_t spi_end(void *arg) +static IRAM_ATTR esp_err_t release_spi_bus_lock(void *arg) { return spi_bus_lock_acquire_end(((app_func_arg_t *)arg)->dev_lock); } @@ -103,7 +103,7 @@ static IRAM_ATTR esp_err_t spi1_start(void *arg) */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS //use the lock to disable the cache and interrupts before using the SPI bus - ret = spi_start(arg); + ret = acquire_spi_bus_lock(arg); #elif SPI_FLASH_CACHE_NO_DISABLE _lock_acquire(&s_spi1_flash_mutex); #else @@ -122,7 +122,7 @@ static IRAM_ATTR esp_err_t spi1_end(void *arg) * There are three ways for ESP Flash API lock, see `spi1_start` */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS - ret = spi_end(arg); + ret = release_spi_bus_lock(arg); #elif SPI_FLASH_CACHE_NO_DISABLE _lock_release(&s_spi1_flash_mutex); #else @@ -218,8 +218,8 @@ static const DRAM_ATTR esp_flash_os_functions_t esp_flash_spi1_default_os_functi }; static const esp_flash_os_functions_t esp_flash_spi23_default_os_functions = { - .start = spi_start, - .end = spi_end, + .start = acquire_spi_bus_lock, + .end = release_spi_bus_lock, .delay_us = delay_us, .get_temp_buffer = get_buffer_malloc, .release_temp_buffer = release_buffer_malloc,