← index #6512PR #10490
Off-topic · high · value 1.902
QUERY · ISSUE

machine.SDCard doesn't release the SDSPI when deleted

openby aguavivaopened 2020-10-03updated 2026-03-24
port-esp32

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.

CANDIDATE · PULL REQUEST

ports/esp32/machine_sdcard.c: Fix ESP_ERR_INVALID_ARG on SDCard init on ESP32-S3 (Change DMA channel)

mergedby maripoopened 2023-01-13updated 2023-01-19
port-esp32

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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied