Skip to content

Problems with accessing SD CARD on ESP32-S3-GEEK board #10161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
donkiejkong opened this issue Mar 22, 2025 · 11 comments
Closed

Problems with accessing SD CARD on ESP32-S3-GEEK board #10161

donkiejkong opened this issue Mar 22, 2025 · 11 comments
Labels

Comments

@donkiejkong
Copy link

CircuitPython version and board name

Adafruit CircuitPython 9.2.4 on 2025-01-29; Waveshare ESP32-S3-GEEK with ESP32S3

Code/REPL

import os
import board
import sdioio
import storage
import bitbangio

CMD_PIN = 35
CLK_PIN = 36
D0_PIN  = 37
SD_SLOT_WIDTH = 1

# example 1
spi = bitbangio.SPI(board.GP36, board.GP35, board.GP37) # always fails
cs = digitalio.DigitalInOut(board.DAT3)
sd = adafruit_sdcard.SDCard(spi, cs) 

# example 2 (always fails)
sd = sdioio.SDCard(clock=board.GP36, command=board.GP35, data=[board.GP37], frequency=25000000)

# example 3 (alway fails)
spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO) 

# the usual things
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
os.listdir('/sd')

Behavior

the error is always the same:
Adafruit CircuitPython 9.2.4 on 2025-01-29; Waveshare ESP32-S3-GEEK with ESP32S3
Board ID:waveshare_esp32_s3_geek
UID:C34872620D8B
boot.py output:
Traceback (most recent call last):
File "boot.py", line 15, in
ValueError: GP36 in use # always same error for all of the examples

Description

Hello,

This is my device:
https://www.waveshare.com/wiki/ESP32-S3-GEEK

I've installed python circuit from here: https://circuitpython.org/board/waveshare_esp32_s3_geek/

I'm trying to port one of the examples from C to python:
https://github.com/user-attachments/files/19285971/waveshare_usb_msc_wireless_disk_example.zip

However I'm currently stuck trying to make the sd card working, this is my boot.py (i've tried multiple approaches):


import os
import board
import sdioio
import storage
import bitbangio

CMD_PIN = 35
CLK_PIN = 36
D0_PIN  = 37
SD_SLOT_WIDTH = 1

# example 1
spi = bitbangio.SPI(board.GP36, board.GP35, board.GP37) # always fails
cs = digitalio.DigitalInOut(board.DAT3)
sd = adafruit_sdcard.SDCard(spi, cs) 

# example 2 (always fails)
sd = sdioio.SDCard(clock=board.GP36, command=board.GP35, data=[board.GP37], frequency=25000000)

# example 3 (alway fails)
spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO) 

# the usual things
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
os.listdir('/sd')


the error is always the same:
Adafruit CircuitPython 9.2.4 on 2025-01-29; Waveshare ESP32-S3-GEEK with ESP32S3
Board ID:waveshare_esp32_s3_geek
UID:C34872620D8B
boot.py output:
Traceback (most recent call last):
File "boot.py", line 15, in
ValueError: GP36 in use

I want to use the SD card and make it visible to the operating system as mass storage device. (it works in the example in C). Anyway am i missing something here? why this error happens?

Additional information

No response

@Neradoc
Copy link

Neradoc commented Mar 22, 2025

Hi, I looked at the board definition and there seems to be mistakes in there: it instantiates the SPI bus of the SD card (also defined as board.SD_SPI) to use the display, despite the display being on a different SPI bus according to pins.c.
I don't have the board to test, can you check the following ?

  • Does the display show anything when the board is running ?
  • Try this code:
spi = board.SD_SPI()
cs = digitalio.DigitalInOut(board.SD_CS)
sd = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
os.listdir('/sd')

As for your ultimate goal of showing the SD card as mass storage, that's not gonna work over USB, it's not currently supported in Circuitpython.
For using it over wifi, you could use the web workflow for that, or write a server in python.
We have adafruit_httpserver, but I don't think that a file server has been done yet.

@donkiejkong
Copy link
Author

hello,

i've tried the code the error now is:

Adafruit CircuitPython 9.2.4 on 2025-01-29; Waveshare ESP32-S3-GEEK with ESP32S3 Board ID:waveshare_esp32_s3_geek UID:C34872620D8B boot.py output: Traceback (most recent call last): File "boot.py", line 19, in <module> File "adafruit_sdcard.py", line 112, in __init__ File "adafruit_sdcard.py", line 130, in _init_card File "adafruit_sdcard.py", line 252, in _cmd File "adafruit_sdcard.py", line 205, in _wait_for_ready ValueError: No miso pin

line 19 of boot.py is "sd = adafruit_sdcard.SDCard(spi, cs)"

i'm using adafruit_sdcard.mpy lib from adafruit-circuitpython-sd-9.x-mpy-3.3.25

@donkiejkong
Copy link
Author

donkiejkong commented Mar 22, 2025

There is a default SPI object defined for this board on GPIO35 to 37

circuitpython/ports/espressif/boards/waveshare_esp32_s3_geek/pins.c

Lines 67 to 69 in a0b482c

{ MP_ROM_QSTR(MP_QSTR_SD_SCK), MP_ROM_PTR(&pin_GPIO36)},
{ MP_ROM_QSTR(MP_QSTR_SD_MOSI), MP_ROM_PTR(&pin_GPIO35)},
{ MP_ROM_QSTR(MP_QSTR_SD_MISO), MP_ROM_PTR(&pin_GPIO37)},
If you want to use the sd card over SPI, with the sdcardio module, calling it like this sd = sdcardio.SDCard(board.SPI(), board.SD_CS) should work.

Based on the board schematic, the SDIO interface is available too, so sd = sdioio.SDCard(clock=board.GP36, command=board.GP35, data=[board.GP37, board.GP33, board.GP38, board.GP34], frequency=25000000) should work as well, if you don't try to use the SPI at the same time.

For help with CircuitPython questions like these, it's better to ask in the Adafruit Discord server

thanks for the reply, i've modified the code to:

`
import os
import board
import sdioio
import storage
import bitbangio
import digitalio
import adafruit_sdcard

CMD_PIN = 35
CLK_PIN = 36
D0_PIN = 37
SD_SLOT_WIDTH = 1

sd = sdioio.SDCard(clock=board.GP36, command=board.GP35, data=[board.GP37, board.GP33, board.GP38, board.GP34], frequency=25000000)
`

the error is still the same:
Traceback (most recent call last):
File "boot.py", line 16, in
ValueError: GP36 in use

@Neradoc
Copy link

Neradoc commented Mar 22, 2025

Adafruit CircuitPython 9.2.4 on 2025-01-29; Waveshare ESP32-S3-GEEK with ESP32S3 Board ID:waveshare_esp32_s3_geek UID:C34872620D8B boot.py output: Traceback (most recent call last): File "boot.py", line 19, in <module> File "adafruit_sdcard.py", line 112, in __init__ File "adafruit_sdcard.py", line 130, in _init_card File "adafruit_sdcard.py", line 252, in _cmd File "adafruit_sdcard.py", line 205, in _wait_for_ready ValueError: No miso pin

Oh yeah that would actually not work...
The issue with the board definition is here:

busio_spi_obj_t *spi = common_hal_board_create_spi(0);
fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus;
common_hal_busio_spi_construct(
spi,
&pin_GPIO12, // CLK
&pin_GPIO11, // MOSI
NULL, // MISO not connected
false); // Not half-duplex

  • It creates board.SD_SPI() making the SD pins occupied.
  • It overwrites the spi variable with the LCD pins.

Therefore board.SD_SPI can't be used for the SD card, unlike I first thought, since it doesn't have the right pins anymore, despite the pins being busy. (And it doesn't have a MISO pin anymore). Plus the SD pins are unreachable at this point, you can't even deinit the SPI bus on them.

I believe I have a fix for that: #10162 can you test it from the artifacts once it's built ?

@Neradoc
Copy link

Neradoc commented Mar 22, 2025

FYI this is where it is:
Image

@RetiredWizard
Copy link

I'll also give the updated PR a test but for what it's worth the SDIO interface does seem to work on 9.2.5

import sdioio
import board
import storage
sd = sdioio.SDCard(clock=board.GP36,command=board.GP35,data=[board.GP37, board.GP33, board.GP38, board.GP34],frequency=25000000)
vfs=storage.VfsFat(sd)
storage.mount(vfs,'/sd')

@RetiredWizard
Copy link

  • Try this code:

spi = board.SD_SPI()
cs = digitalio.DigitalInOut(board.SD_CS)
sd = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
os.listdir('/sd')

This works with the artifact.....

@donkiejkong
Copy link
Author

FYI this is where it is: Image

thanks a lot this seems to work, at least i can now access the SD card!

any ideas how to mount the /sd to be visible by the operating system like a normal mass storage / USB thumb drive? (this works with flash memory "/", but not "/sd or "sd")

import os
import board
import sdioio
import storage
import bitbangio
import digitalio
import adafruit_sdcard
import supervisor


sd = sdioio.SDCard(clock=board.GP36, command=board.GP35, data=[board.GP37, board.GP33, board.GP38, board.GP34], frequency=25000000)
  
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')

print("### HELLO ###")
print(os.listdir('/sd'))

storage.remount('/sd', disable_concurrent_write_protection=True)
supervisor.runtime.autoreload = False

this line "storage.remount('/sd', disable_concurrent_write_protection=True)" fails with "OSError: [Errno 22] Invalid argument".

@RetiredWizard
Copy link

RetiredWizard commented Mar 22, 2025

The SDIO mount now doesn't work though...

>>> sd = sdioio.SDCard(clock=board.GP36,command=board.GP35,data=[board.GP37, board.GP33, board.GP38, board.GP34],frequency=25000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: SDIO Init Error 107

Nevermind, Neradoc reminded me that a power cycle is necessary to switch between SD and SDIO....

@Neradoc
Copy link

Neradoc commented Mar 22, 2025

this line "storage.remount('/sd', disable_concurrent_write_protection=True)" fails with "OSError: [Errno 22] Invalid argument".

At present time remount() only works on "/".
As I mentioned above the SD card is not mounted and visible via USB MSC, only from python code.
There's a PR for that though: #10129, it will probably be in Circuitpython 10.

@dhalbert
Copy link
Collaborator

This should be fixed by #10164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants