sdcardio – Interface to an SD card via the SPI bus
Available on these boards
- class sdcardio.SDCard(bus: busio.SPI, cs: microcontroller.Pin, baudrate: int = 8000000)
SD Card Block Interface
Controls an SD card over SPI. This built-in module has higher read performance than the library adafruit_sdcard, but it is only compatible with
busio.SPI, notbitbangio.SPI. Usually an SDCard object is used withstorage.VfsFatto allow file I/O to an SD card.Construct an SPI SD Card object with the given properties
- Parameters:
spi (busio.SPI) – The SPI bus
cs (microcontroller.Pin) – The chip select connected to the card
baudrate (int) – The SPI data rate to use after card setup
Note that during detection and configuration, a hard-coded low baudrate is used. Data transfers use the specified baurate (rounded down to one that is supported by the microcontroller)
Important
When the SPI bus is shared with other peripherals, every CS pin on the bus must be in a known HIGH (deselected) state before any SPI transaction occurs. This is normally guaranteed by a hardware pull-up on each CS line, but on boards where a co-resident peripheral’s CS floats (for example the Feather RP2040 RFM, whose
RFM_CShas no pull-up), that CS must be driven HIGH in software before the SD card is initialized. If any CS is allowed to float low, the SPI bus can be corrupted during SD card init and the card may not be recognized until it is powered off or re-inserted. The order in which peripherals are constructed is secondary; what matters is that all CS lines are HIGH (deselected) before any SPI transaction.Example usage:
import os import board import sdcardio import storage # Make sure to make an "sd" folder on CIRCUITPY sd = sdcardio.SDCard(board.SPI(), board.SD_CS) vfs = storage.VfsFat(sd) storage.mount(vfs, '/sd') print(os.listdir('/sd'))
- count() int
Returns the total number of sectors
Due to technical limitations, this is a function and not a property.
- Returns:
The number of 512-byte blocks, as a number
- readblocks(start_block: int, buf: circuitpython_typing.WriteableBuffer) None
Read one or more blocks from the card
- Parameters:
start_block (int) – The block to start reading from
buf (WriteableBuffer) – The buffer to write into. Length must be multiple of 512.
- Returns:
None
- writeblocks(start_block: int, buf: circuitpython_typing.ReadableBuffer) None
Write one or more blocks to the card
- Parameters:
start_block (int) – The block to start writing from
buf (ReadableBuffer) – The buffer to read from. Length must be multiple of 512.
- Returns:
None