diff --git a/components/esp_driver_spi/src/gpspi/spi_common.c b/components/esp_driver_spi/src/gpspi/spi_common.c index e1c9feb3dba5..3e54e769870a 100644 --- a/components/esp_driver_spi/src/gpspi/spi_common.c +++ b/components/esp_driver_spi/src/gpspi/spi_common.c @@ -921,6 +921,9 @@ esp_err_t spi_bus_free(spi_host_device_t host_id) if (ctx->destroy_func) { err = ctx->destroy_func(ctx->destroy_arg); + if (err != ESP_OK) { + return err; + } } spicommon_bus_free_io_cfg(&bus_attr->bus_cfg); diff --git a/components/esp_driver_spi/test_apps/master/main/test_spi_master.c b/components/esp_driver_spi/test_apps/master/main/test_spi_master.c index 504c448a5d6f..cde48af830bf 100644 --- a/components/esp_driver_spi/test_apps/master/main/test_spi_master.c +++ b/components/esp_driver_spi/test_apps/master/main/test_spi_master.c @@ -1757,3 +1757,30 @@ static void test_iram_slave_normal(void) TEST_CASE_MULTIPLE_DEVICES("SPI_Master:IRAM_safe", "[spi_ms]", test_master_iram, test_iram_slave_normal); #endif + +TEST_CASE("test_bus_free_safty_to_remain_devices", "[spi]") +{ + spi_bus_config_t buscfg = SPI_BUS_TEST_DEFAULT_CONFIG(); + TEST_ESP_OK(spi_bus_initialize(TEST_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); + + spi_device_handle_t dev0, dev1; + spi_device_interface_config_t devcfg = SPI_DEVICE_TEST_DEFAULT_CONFIG(); + TEST_ESP_OK(spi_bus_add_device(TEST_SPI_HOST, &devcfg, &dev0)); + devcfg.spics_io_num = PIN_NUM_MISO; + TEST_ESP_OK(spi_bus_add_device(TEST_SPI_HOST, &devcfg, &dev1)); + + int master_send; + spi_transaction_t trans_cfg = { + .tx_buffer = &master_send, + .length = sizeof(uint32_t) * 8, + }; + + TEST_ESP_OK(spi_bus_remove_device(dev0)); + TEST_ESP_ERR(ESP_ERR_INVALID_STATE, spi_bus_free(TEST_SPI_HOST)); + + //transaction should OK after a failed call to bus_free + TEST_ESP_OK(spi_device_transmit(dev1, (spi_transaction_t *)&trans_cfg)); + + TEST_ESP_OK(spi_bus_remove_device(dev1)); + TEST_ESP_OK(spi_bus_free(TEST_SPI_HOST)); +}