From d43de5fa7e7e01a93ae48a2b1df678f88bf76e91 Mon Sep 17 00:00:00 2001 From: wanckl Date: Thu, 9 Jul 2026 19:33:03 +0800 Subject: [PATCH] fix(hw_support): spi buslock fix unregister dev api issue --- components/esp_driver_spi/src/gpspi/spi_master.c | 1 + components/esp_hw_support/spi_bus_lock.c | 11 +++++++++++ components/spi_flash/esp_flash_spi_init.c | 8 ++++++-- components/spi_flash/include/esp_flash_internal.h | 4 +++- components/spi_flash/include/esp_flash_spi_init.h | 1 + components/spi_flash/spi_flash_os_func_app.c | 8 +++++++- 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/components/esp_driver_spi/src/gpspi/spi_master.c b/components/esp_driver_spi/src/gpspi/spi_master.c index 6dc2729a2e8..f10d569efd4 100644 --- a/components/esp_driver_spi/src/gpspi/spi_master.c +++ b/components/esp_driver_spi/src/gpspi/spi_master.c @@ -577,6 +577,7 @@ esp_err_t spi_bus_remove_device(spi_device_handle_t handle) //catch design errors and aren't meant to be triggered during normal operation. SPI_CHECK(uxQueueMessagesWaiting(handle->trans_queue) == 0, "Have unfinished transactions", ESP_ERR_INVALID_STATE); SPI_CHECK(handle->host->cur_cs == DEV_NUM_MAX || handle->host->device[handle->host->cur_cs] != handle, "Have unfinished transactions", ESP_ERR_INVALID_STATE); + SPI_CHECK(handle->host->device_acquiring_lock != handle, "Device has acquired the bus", ESP_ERR_INVALID_STATE); if (handle->ret_queue) { SPI_CHECK(uxQueueMessagesWaiting(handle->ret_queue) == 0, "Have unfinished transactions", ESP_ERR_INVALID_STATE); } diff --git a/components/esp_hw_support/spi_bus_lock.c b/components/esp_hw_support/spi_bus_lock.c index 1fe9cb502a3..6463bfde085 100644 --- a/components/esp_hw_support/spi_bus_lock.c +++ b/components/esp_hw_support/spi_bus_lock.c @@ -703,10 +703,16 @@ void spi_bus_lock_unregister_dev(spi_bus_lock_dev_handle_t dev_handle) spi_bus_lock_t* lock = dev_handle->parent; BUS_LOCK_DEBUG_EXECUTE_CHECK(atomic_load(&lock->dev[id]) == (intptr_t)dev_handle); + BUS_LOCK_DEBUG_EXECUTE_CHECK(lock->acquiring_dev != dev_handle); + BUS_LOCK_DEBUG_EXECUTE_CHECK((lock_status_fetch(lock) & dev_handle->mask) == 0); if (lock->last_dev == dev_handle) { lock->last_dev = NULL; } + if (lock->acquiring_dev == dev_handle) { + lock->acquiring_dev = NULL; + lock->acq_dev_bg_active = false; + } atomic_store(&lock->dev[id], (intptr_t)NULL); if (dev_handle->semphr) { @@ -733,6 +739,11 @@ void spi_bus_lock_set_bg_control(spi_bus_lock_handle_t lock, bg_ctrl_func_t bg_e lock->bg_arg = arg; } +IRAM_ATTR spi_bus_lock_handle_t spi_bus_lock_get_parent(spi_bus_lock_dev_handle_t dev_handle) +{ + return (dev_handle ? dev_handle->parent : NULL); +} + IRAM_ATTR int spi_bus_lock_get_dev_id(spi_bus_lock_dev_handle_t dev_handle) { return (dev_handle ? dev_lock_get_id(dev_handle) : -1); diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index abebbb3e83f..1f1b277ed5a 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -301,13 +301,17 @@ esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip) } spi_bus_lock_dev_handle_t dev_handle = NULL; - esp_flash_deinit_os_functions(chip, &dev_handle); + esp_err_t ret = esp_flash_deinit_os_functions(chip, &dev_handle); + if (ret != ESP_OK) { + return ret; + } + if (dev_handle) { spi_bus_lock_unregister_dev(dev_handle); } free(chip->host); free(chip); - return ESP_OK; + return ret; } /* The default (ie initial boot) no-OS ROM esp_flash_os_functions_t */ diff --git a/components/spi_flash/include/esp_flash_internal.h b/components/spi_flash/include/esp_flash_internal.h index 0060eafe8da..d20b15fb243 100644 --- a/components/spi_flash/include/esp_flash_internal.h +++ b/components/spi_flash/include/esp_flash_internal.h @@ -63,7 +63,9 @@ esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, spi_bus_lo * @param chip The chip to deinit os functions * @param out_dev_handle The SPI bus lock passed from `esp_flash_init_os_functions`. The caller should deinitialize * the lock. - * @return always ESP_OK. + * @return + * - ESP_ERR_INVALID_STATE: the chip is still acquiring the SPI bus lock. + * - ESP_OK: success. */ esp_err_t esp_flash_deinit_os_functions(esp_flash_t* chip, spi_bus_lock_dev_handle_t* out_dev_handle); diff --git a/components/spi_flash/include/esp_flash_spi_init.h b/components/spi_flash/include/esp_flash_spi_init.h index 493bd937eb6..85dfb6cb193 100644 --- a/components/spi_flash/include/esp_flash_spi_init.h +++ b/components/spi_flash/include/esp_flash_spi_init.h @@ -51,6 +51,7 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d * * @return * - ESP_ERR_INVALID_ARG: The chip is invalid. + * - ESP_ERR_INVALID_STATE: The chip is still acquiring the SPI bus lock. * - ESP_OK: success. */ esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip); diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index 700a2309508..fc6decc571c 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -336,9 +336,15 @@ esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, spi_bus_lo esp_err_t esp_flash_deinit_os_functions(esp_flash_t* chip, spi_bus_lock_dev_handle_t* out_dev_handle) { + *out_dev_handle = NULL; if (chip->os_func_data) { + app_func_arg_t *ctx = (app_func_arg_t*)chip->os_func_data; + spi_bus_lock_dev_handle_t dev_handle = ctx->dev_lock; + if (dev_handle && spi_bus_lock_get_acquiring_dev(spi_bus_lock_get_parent(dev_handle)) == dev_handle) { + return ESP_ERR_INVALID_STATE; + } // SPI bus lock is possibly not used on SPI1 bus - *out_dev_handle = ((app_func_arg_t*)chip->os_func_data)->dev_lock; + *out_dev_handle = dev_handle; free(chip->os_func_data); } chip->os_func = NULL;