Skip to content

esp32/machine_sdcard: Make default SPI pins board-configurable.#19407

Open
pavelrevak wants to merge 2 commits into
micropython:masterfrom
pavelrevak:esp32_sdcard
Open

esp32/machine_sdcard: Make default SPI pins board-configurable.#19407
pavelrevak wants to merge 2 commits into
micropython:masterfrom
pavelrevak:esp32_sdcard

Conversation

@pavelrevak

Copy link
Copy Markdown
Contributor

Summary

The default pins for the SPI-mode SD card bus (machine.SDCard(slot=2)) on the esp32 port are hard-coded per chip in machine_sdcard.c. This makes machine.SDCard(slot=2) unusable out of the box on many boards — most notably the ESP32-S3 defaults (GPIO 35/36/37) clash with the octal SPIRAM bus, so on an S3 board with octal PSRAM the call reconfigures the PSRAM pins and crashes.

This PR makes those pins board-configurable, consistent with how I2C (machine_i2c.h) and SPI (machine_hw_spi.c) already expose their default pins.

Changes

  • New ports/esp32/machine_sdcard.h defining MICROPY_HW_SDCARD_SPI_SCK / _MOSI / _MISO / _CS for the primary SPI SD bus (slot 2), guarded with #ifndef so a board can override them in mpconfigboard.h.
  • machine_sdcard.c uses these macros for the primary bus/device defaults.
  • Wires up the mechanism on four boards with an onboard SPI-mode microSD slot whose pins differ from the chip default: SEEED_XIAO_ESP32S3, LILYGO_T3_S3, LILYGO_TTGO_LORA32, SPARKFUN_THINGPLUS_ESP32C5. Pins are taken from each board's definition or official documentation.

Behaviour / compatibility

  • Default behaviour is unchanged. The macro defaults equal the previous hard-coded per-chip values (ESP32 18/23/19 + CS 5, S3 37/35/36 + CS 34, others GPIO_NUM_NC).
  • On chips with a single SPI SD bus (C3/C6/C5/H2/S2), the built-in IDF default CS is kept unless a board provides one.
  • Runtime sck=/miso=/mosi=/cs= arguments still take precedence.
  • Only SPI mode (slot 2) is affected. SDMMC mode (slots 0/1) is untouched.

Testing

Verified on a Seeed XIAO ESP32S3 (Sense) with a microSD card: machine.SDCard(slot=2) works with no arguments and sd.info() returns (1977614336, 512). The SPARKFUN_THINGPLUS_ESP32C5 board (single SPI bus) exercises the NUM_SD_SPI_BUS == 1 code path at build time in CI. The other boards' pins were confirmed against their schematics.

Affected ports

ESP32 only (all variants).

Generative AI

I used generative AI tools when creating this PR, but a human has checked the
code and is responsible for the code and the description above.

The default pins for the primary SPI-mode SD card bus (slot 2) were
hard-coded per chip, and the ESP32-S3 defaults (GPIO 35/36/37) clash with
the octal SPIRAM bus, so they are unusable on many S3 boards.

Add a machine_sdcard.h header defining MICROPY_HW_SDCARD_SPI_SCK, _MOSI,
_MISO and _CS, following the existing per-peripheral pin-define convention
(machine_i2c.h, machine_hw_spi.c). A board can override these in its
mpconfigboard.h so that machine.SDCard(slot=2) works without passing pins.
The defaults match the previous per-chip values, so behaviour is unchanged
unless a board overrides them.
SDMMC-mode pins (slots 0 and 1) are not affected.

Signed-off-by: Pavel Revak <pavelrevak@gmail.com>
Define MICROPY_HW_SDCARD_SPI_* so that machine.SDCard(slot=2) works
without explicit pins on boards that have an onboard microSD slot wired
for SPI mode, where the pins differ from the chip default:

- SEEED_XIAO_ESP32S3 (verified on hardware)
- LILYGO_T3_S3
- LILYGO_TTGO_LORA32
- SPARKFUN_THINGPLUS_ESP32C5

The pins are taken from each board's definition or official documentation.

Signed-off-by: Pavel Revak <pavelrevak@gmail.com>
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

Code size report:

Reference:  py/nlrpowerpc: Add r4 to the nlr_push asm clobber list. [562d6be]
Comparison: esp32/boards: Add default SD card SPI pins to boards with an SD slot. [merge of a019b9b]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants