Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_micro_error_SPI_HOST_MAX' into 'master'
Browse files Browse the repository at this point in the history
spi: fix micro SPI_HOST_MAX error

Closes IDFGH-8932

See merge request espressif/esp-idf!21557
  • Loading branch information
wanckl committed Jan 5, 2023
2 parents 28ccb3b + 3c37bcc commit 419544a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
2 changes: 2 additions & 0 deletions components/hal/include/hal/spi_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ typedef enum {
//SPI1 can be used as GPSPI only on ESP32
SPI1_HOST=0, ///< SPI1
SPI2_HOST=1, ///< SPI2
#if SOC_SPI_PERIPH_NUM > 2
SPI3_HOST=2, ///< SPI3
#endif
SPI_HOST_MAX, ///< invalid host value
} spi_host_device_t;

Expand Down
58 changes: 32 additions & 26 deletions components/spi_flash/spi_flash_os_func_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,33 +243,39 @@ esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, spi_bus_lo
return ESP_ERR_INVALID_ARG;
}

if (host_id == SPI1_HOST) {
//SPI1
chip->os_func = &esp_flash_spi1_default_os_functions;
chip->os_func_data = heap_caps_malloc(sizeof(spi1_app_func_arg_t),
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (chip->os_func_data == NULL) {
return ESP_ERR_NO_MEM;
}
*(spi1_app_func_arg_t*) chip->os_func_data = (spi1_app_func_arg_t) {
.common_arg = {
switch (host_id)
{
case SPI1_HOST:
//SPI1
chip->os_func = &esp_flash_spi1_default_os_functions;
chip->os_func_data = heap_caps_malloc(sizeof(spi1_app_func_arg_t),
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (chip->os_func_data == NULL) {
return ESP_ERR_NO_MEM;
}
*(spi1_app_func_arg_t*) chip->os_func_data = (spi1_app_func_arg_t) {
.common_arg = {
.dev_lock = dev_handle,
},
.no_protect = true,
};
break;
case SPI2_HOST:
#if SOC_SPI_PERIPH_NUM > 2
case SPI3_HOST:
#endif
//SPI2, SPI3
chip->os_func = &esp_flash_spi23_default_os_functions;
chip->os_func_data = heap_caps_malloc(sizeof(app_func_arg_t),
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (chip->os_func_data == NULL) {
return ESP_ERR_NO_MEM;
}
*(app_func_arg_t*) chip->os_func_data = (app_func_arg_t) {
.dev_lock = dev_handle,
},
.no_protect = true,
};
} else if (host_id == SPI2_HOST || host_id == SPI3_HOST) {
//SPI2, SPI3
chip->os_func = &esp_flash_spi23_default_os_functions;
chip->os_func_data = heap_caps_malloc(sizeof(app_func_arg_t),
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (chip->os_func_data == NULL) {
return ESP_ERR_NO_MEM;
}
*(app_func_arg_t*) chip->os_func_data = (app_func_arg_t) {
.dev_lock = dev_handle,
};
} else {
return ESP_ERR_INVALID_ARG;
};
break;
default: return ESP_ERR_INVALID_ARG;
}

return ESP_OK;
Expand Down
18 changes: 14 additions & 4 deletions components/spi_flash/test/test_esp_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ static void setup_bus(spi_host_device_t host_id)
gpio_iomux_out(hd_pin, spi_periph_signal[host_id].func, false);
#endif //CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT
//currently the SPI bus for main flash chip is initialized through GPIO matrix
} else if (host_id == SPI2_HOST) {
}
else if (host_id == SPI2_HOST) {
ESP_LOGI(TAG, "setup flash on SPI%d (FSPI) CS0...\n", host_id + 1);
spi_bus_config_t fspi_bus_cfg = {
.mosi_io_num = FSPI_PIN_NUM_MOSI,
Expand All @@ -358,7 +359,9 @@ static void setup_bus(spi_host_device_t host_id)
};
esp_err_t ret = spi_bus_initialize(host_id, &fspi_bus_cfg, 0);
TEST_ESP_OK(ret);
} else if (host_id == SPI3_HOST) {
}
#if SOC_SPI_PERIPH_NUM > 2
else if (host_id == SPI3_HOST) {
ESP_LOGI(TAG, "setup flash on SPI%d (HSPI) CS0...\n", host_id + 1);
spi_bus_config_t hspi_bus_cfg = {
.mosi_io_num = HSPI_PIN_NUM_MOSI,
Expand All @@ -377,7 +380,9 @@ static void setup_bus(spi_host_device_t host_id)

gpio_set_direction(HSPI_PIN_NUM_WP, GPIO_MODE_OUTPUT);
gpio_set_level(HSPI_PIN_NUM_WP, 1);
} else {
}
#endif
else {
ESP_LOGE(TAG, "invalid bus");
}
}
Expand All @@ -386,7 +391,12 @@ static void setup_bus(spi_host_device_t host_id)
static void release_bus(int host_id)
{
//SPI1 bus can't be deinitialized
if (host_id == SPI2_HOST || host_id == SPI3_HOST) {
#if SOC_SPI_PERIPH_NUM > 2
if (host_id == SPI2_HOST || host_id == SPI3_HOST)
#else
if (host_id == SPI2_HOST)
#endif
{
spi_bus_free(host_id);
}
}
Expand Down
15 changes: 12 additions & 3 deletions components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ static void setup_bus(spi_host_device_t host_id)
};
esp_err_t ret = spi_bus_initialize(host_id, &fspi_bus_cfg, 0);
TEST_ESP_OK(ret);
} else if (host_id == SPI3_HOST) {
}
#if SOC_SPI_PERIPH_NUM > 2
else if (host_id == SPI3_HOST) {
ESP_LOGI(TAG, "setup flash on SPI%u (HSPI) CS0...\n", host_id + 1);
spi_bus_config_t hspi_bus_cfg = {
.mosi_io_num = HSPI_PIN_NUM_MOSI,
Expand All @@ -159,7 +161,9 @@ static void setup_bus(spi_host_device_t host_id)

gpio_set_direction(HSPI_PIN_NUM_WP, GPIO_MODE_OUTPUT);
gpio_set_level(HSPI_PIN_NUM_WP, 1);
} else {
}
#endif
else {
ESP_LOGE(TAG, "invalid bus");
}
}
Expand All @@ -168,7 +172,12 @@ static void setup_bus(spi_host_device_t host_id)
static void release_bus(int host_id)
{
//SPI1 bus can't be deinitialized
if (host_id == SPI2_HOST || host_id == SPI3_HOST) {
#if SOC_SPI_PERIPH_NUM > 2
if (host_id == SPI2_HOST || host_id == SPI3_HOST)
#else
if (host_id == SPI2_HOST)
#endif
{
spi_bus_free(host_id);
}
}
Expand Down

0 comments on commit 419544a

Please sign in to comment.