diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index d72a388662..69a516a095 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -145,7 +145,7 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d goto fail; } - int dev_id; + int dev_id = -1; esp_err_t err = esp_flash_init_os_functions(chip, config->host_id, &dev_id); if (err == ESP_ERR_NOT_SUPPORTED) { ESP_LOGE(TAG, "Init os functions failed! No free CS."); @@ -156,6 +156,12 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d ret = err; goto fail; } + // When `CONFIG_SPI_FLASH_SHARE_SPI1_BUS` is not enabled on SPI1 bus, the + // `esp_flash_init_os_functions` will not be able to assign a new device ID. In this case, we + // use the `cs_id` in the config structure. + if (dev_id == -1 && config->host_id == SPI_HOST) { + dev_id = config->cs_id; + } assert(dev_id < SOC_SPI_PERIPH_CS_NUM(config->host_id) && dev_id >= 0); bool use_iomux = spicommon_bus_using_iomux(config->host_id); diff --git a/components/spi_flash/include/esp_flash_spi_init.h b/components/spi_flash/include/esp_flash_spi_init.h index f3ac315eeb..85334d3ea5 100644 --- a/components/spi_flash/include/esp_flash_spi_init.h +++ b/components/spi_flash/include/esp_flash_spi_init.h @@ -28,8 +28,12 @@ typedef struct { esp_flash_io_mode_t io_mode; ///< IO mode to read from the Flash esp_flash_speed_t speed; ///< Speed of the Flash clock int input_delay_ns; ///< Input delay of the data pins, in ns. Set to 0 if unknown. - - int cs_id; ///< @deprecated CS pin (signal) to use + /** + * CS line ID, ignored when not `host_id` is not SPI1_HOST, or + * `CONFIG_SPI_FLASH_SHARE_SPI1_BUS` is enabled. In this case, the CS line used is + * automatically assigned by the SPI bus lock. + */ + int cs_id; } esp_flash_spi_device_config_t; /** diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index 07b8778aa5..f6cc038744 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -127,14 +127,30 @@ static const esp_flash_os_functions_t esp_flash_spi23_default_os_functions = { .delay_ms = delay_ms, }; -esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, int* out_dev_id) +static spi_bus_lock_dev_handle_t register_dev(int host_id) { spi_bus_lock_handle_t lock = spi_bus_lock_get_by_id(host_id); spi_bus_lock_dev_handle_t dev_handle; spi_bus_lock_dev_config_t config = {.flags = SPI_BUS_LOCK_DEV_FLAG_CS_REQUIRED}; esp_err_t err = spi_bus_lock_register_dev(lock, &config, &dev_handle); if (err != ESP_OK) { - return err; + return NULL; + } + return dev_handle; +} + +esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, int* out_dev_id) +{ + spi_bus_lock_dev_handle_t dev_handle = NULL; + + // Skip initializing the bus lock when the bus is SPI1 and the bus is not shared with SPI Master + // driver, leaving dev_handle = NULL + bool skip_register_dev = (host_id == SPI_HOST); +#if CONFIG_SPI_FLASH_SHARE_SPI1_BUS + skip_register_dev = false; +#endif + if (!skip_register_dev) { + dev_handle = register_dev(host_id); } if (host_id == SPI1_HOST) { @@ -166,7 +182,10 @@ esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, int* out_d return ESP_ERR_INVALID_ARG; } - *out_dev_id = spi_bus_lock_get_dev_id(dev_handle); + // Bus lock not initialized, the device ID should be directly given by application. + if (dev_handle) { + *out_dev_id = spi_bus_lock_get_dev_id(dev_handle); + } return ESP_OK; } @@ -174,7 +193,11 @@ esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, int* out_d esp_err_t esp_flash_deinit_os_functions(esp_flash_t* chip) { if (chip->os_func_data) { - spi_bus_lock_unregister_dev(((app_func_arg_t*)chip->os_func_data)->dev_lock); + spi_bus_lock_dev_handle_t dev_lock = ((app_func_arg_t*)chip->os_func_data)->dev_lock; + // SPI bus lock is possible not used on SPI1 bus + if (dev_lock) { + spi_bus_lock_unregister_dev(dev_lock); + } free(chip->os_func_data); } chip->os_func = NULL;