machine.SDCard doesn't release the SDSPI when deleted
Note that sdspi_host_init_slot gets called on creation
https://github.com/micropython/micropython/blob/246f3f640dd635f67269bfdbebe23408f77cd2b0/ports/esp32/machine_sdcard.c#L227
but on deletion sdspi_host_deinit doesn't get called (only the mmc gets deinit)
https://github.com/micropython/micropython/blob/246f3f640dd635f67269bfdbebe23408f77cd2b0/ports/esp32/machine_sdcard.c#L261
As the SDCard object acquires it but never releases it, this bug prevents sharing the SPI with other modules.
ports/esp32/machine_sdcard.c: Fix ESP_ERR_INVALID_ARG on SDCard init on ESP32-S3 (Change DMA channel)
This pull request solves the problem that initialization of SDCard objects with SPI fails on ESP32-S3.
Problem
Creating SDCard object with SPI (slot=2 or 3) causes ESP_ERR_INVALID_ARG error.
[0;31mE (34340) spi: spi_bus_initialize(762): invalid dma channel, chip only support spi dma channel auto-alloc[0m
[0;31mE (34340) sdspi_host: spi_bus_initialize failed with rc=0x102[0m
The code below can reproduce this.
import os
from machine import Pin, SDCard
card = SDCard(slot=2, sck=Pin(17), miso=Pin(18), mosi=Pin(16), cs=Pin(15)) # <- ESP_ERR_INVALID_ARG
Reason
In ports/esp32/machine_sdcard.c, SPI slot configurations are defined. However, the DMA channel specified in the configuration for ESP32-S3 is inappropriate. In ESP-IDF, the only available channel for ESP32S3 is SPI_DMA_CH_AUTO=3.
esp-idf/components/driver/spi_common.c
Solution
Specifying DMA channel 3 instead of 2 fixes the problem. I have confirmed the problem and the solution on ESP32-S3-DevKitC-1 board.