diff options
Diffstat (limited to 'ports/esp32/machine_sdcard.c')
-rw-r--r-- | ports/esp32/machine_sdcard.c | 137 |
1 files changed, 95 insertions, 42 deletions
diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 5b495f8494..a2d133442f 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -69,6 +69,68 @@ typedef struct _sdcard_obj_t { #define _SECTOR_SIZE(self) (self->card.csd.sector_size) +// SPI bus default bus and device configuration. + +static const spi_bus_config_t spi_bus_defaults[2] = { + { + #if CONFIG_IDF_TARGET_ESP32 + .miso_io_num = GPIO_NUM_19, + .mosi_io_num = GPIO_NUM_23, + .sclk_io_num = GPIO_NUM_18, + #else + .miso_io_num = GPIO_NUM_36, + .mosi_io_num = GPIO_NUM_35, + .sclk_io_num = GPIO_NUM_37, + #endif + .data2_io_num = GPIO_NUM_NC, + .data3_io_num = GPIO_NUM_NC, + .data4_io_num = GPIO_NUM_NC, + .data5_io_num = GPIO_NUM_NC, + .data6_io_num = GPIO_NUM_NC, + .data7_io_num = GPIO_NUM_NC, + .max_transfer_sz = 4000, + .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK | SPICOMMON_BUSFLAG_MISO | SPICOMMON_BUSFLAG_MOSI, + .intr_flags = 0, + }, + { + .miso_io_num = GPIO_NUM_2, + .mosi_io_num = GPIO_NUM_15, + .sclk_io_num = GPIO_NUM_14, + .data2_io_num = GPIO_NUM_NC, + .data3_io_num = GPIO_NUM_NC, + .data4_io_num = GPIO_NUM_NC, + .data5_io_num = GPIO_NUM_NC, + .data6_io_num = GPIO_NUM_NC, + .data7_io_num = GPIO_NUM_NC, + .max_transfer_sz = 4000, + .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK | SPICOMMON_BUSFLAG_MISO | SPICOMMON_BUSFLAG_MOSI, + .intr_flags = 0, + }, +}; + +#if CONFIG_IDF_TARGET_ESP32 +static const uint8_t spi_dma_channel_defaults[2] = { + 2, + 1, +}; +#endif + +static const sdspi_device_config_t spi_dev_defaults[2] = { + { + #if CONFIG_IDF_TARGET_ESP32 + .host_id = VSPI_HOST, + .gpio_cs = GPIO_NUM_5, + #else + .host_id = SPI3_HOST, + .gpio_cs = GPIO_NUM_34, + #endif + .gpio_cd = SDSPI_SLOT_NO_CD, + .gpio_wp = SDSPI_SLOT_NO_WP, + .gpio_int = SDSPI_SLOT_NO_INT, + }, + SDSPI_DEVICE_CONFIG_DEFAULT(), // HSPI (ESP32) / SPI2 (ESP32S3) +}; + STATIC gpio_num_t pin_or_int(const mp_obj_t arg) { if (mp_obj_is_small_int(arg)) { return MP_OBJ_SMALL_INT_VALUE(arg); @@ -188,10 +250,11 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args } if (is_spi) { - #if CONFIG_IDF_TARGET_ESP32S3 - self->host.slot = slot_num ? SPI3_HOST : SPI2_HOST; - #else + // Needs to match spi_dev_defaults above. + #if CONFIG_IDF_TARGET_ESP32 self->host.slot = slot_num ? HSPI_HOST : VSPI_HOST; + #else + self->host.slot = slot_num ? SPI2_HOST : SPI3_HOST; #endif } @@ -202,46 +265,39 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args if (is_spi) { // SPI interface - #if CONFIG_IDF_TARGET_ESP32S3 - STATIC const sdspi_slot_config_t slot_defaults[2] = { - { - .gpio_miso = GPIO_NUM_36, - .gpio_mosi = GPIO_NUM_35, - .gpio_sck = GPIO_NUM_37, - .gpio_cs = GPIO_NUM_34, - .gpio_cd = SDSPI_SLOT_NO_CD, - .gpio_wp = SDSPI_SLOT_NO_WP, - .dma_channel = SPI_DMA_CH_AUTO - }, - SDSPI_SLOT_CONFIG_DEFAULT() - }; + DEBUG_printf(" Setting up SPI slot configuration"); + spi_host_device_t spi_host_id = self->host.slot; + spi_bus_config_t bus_config = spi_bus_defaults[slot_num]; + #if CONFIG_IDF_TARGET_ESP32 + spi_dma_chan_t dma_channel = spi_dma_channel_defaults[slot_num]; #else - STATIC const sdspi_slot_config_t slot_defaults[2] = { - { - .gpio_miso = GPIO_NUM_19, - .gpio_mosi = GPIO_NUM_23, - .gpio_sck = GPIO_NUM_18, - .gpio_cs = GPIO_NUM_5, - .gpio_cd = SDSPI_SLOT_NO_CD, - .gpio_wp = SDSPI_SLOT_NO_WP, - .dma_channel = 2 - }, - SDSPI_SLOT_CONFIG_DEFAULT() - }; + spi_dma_chan_t dma_channel = SPI_DMA_CH_AUTO; #endif + sdspi_device_config_t dev_config = spi_dev_defaults[slot_num]; - DEBUG_printf(" Setting up SPI slot configuration"); - sdspi_slot_config_t slot_config = slot_defaults[slot_num]; + SET_CONFIG_PIN(bus_config, miso_io_num, ARG_miso); + SET_CONFIG_PIN(bus_config, mosi_io_num, ARG_mosi); + SET_CONFIG_PIN(bus_config, sclk_io_num, ARG_sck); - SET_CONFIG_PIN(slot_config, gpio_cd, ARG_cd); - SET_CONFIG_PIN(slot_config, gpio_wp, ARG_wp); - SET_CONFIG_PIN(slot_config, gpio_miso, ARG_miso); - SET_CONFIG_PIN(slot_config, gpio_mosi, ARG_mosi); - SET_CONFIG_PIN(slot_config, gpio_sck, ARG_sck); - SET_CONFIG_PIN(slot_config, gpio_cs, ARG_cs); + SET_CONFIG_PIN(dev_config, gpio_cs, ARG_cs); + SET_CONFIG_PIN(dev_config, gpio_cd, ARG_cd); + SET_CONFIG_PIN(dev_config, gpio_wp, ARG_wp); - DEBUG_printf(" Calling init_slot()"); - check_esp_err(sdspi_host_init_slot(self->host.slot, &slot_config)); + DEBUG_printf(" Calling spi_bus_initialize()"); + check_esp_err(spi_bus_initialize(spi_host_id, &bus_config, dma_channel)); + + DEBUG_printf(" Calling sdspi_host_init_device()"); + sdspi_dev_handle_t sdspi_handle; + esp_err_t ret = sdspi_host_init_device(&dev_config, &sdspi_handle); + if (ret != ESP_OK) { + spi_bus_free(spi_host_id); + check_esp_err(ret); + } + if (self->host.slot != sdspi_handle) { + // MicroPython restriction: the SPI bus must be exclusively for the SD card. + spi_bus_free(spi_host_id); + mp_raise_ValueError(MP_ERROR_TEXT("SPI bus already in use")); + } } else { // SD/MMC interface DEBUG_printf(" Setting up SDMMC slot configuration"); @@ -275,12 +331,9 @@ STATIC mp_obj_t sd_deinit(mp_obj_t self_in) { DEBUG_printf("De-init host\n"); if (self->flags & SDCARD_CARD_FLAGS_HOST_INIT_DONE) { - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) if (self->host.flags & SDMMC_HOST_FLAG_DEINIT_ARG) { self->host.deinit_p(self->host.slot); - } else - #endif - { + } else { self->host.deinit(); } if (self->host.flags & SDMMC_HOST_FLAG_SPI) { |