adafruit_sdcard - SD card over SPI driver

CircuitPython driver for SD cards using SPI bus.

Requires an SPI bus and a CS pin. Provides readblocks and writeblocks methods so the device can be mounted as a filesystem.

  • Author(s): Scott Shawcroft

Implementation Notes

Hardware:

Software and Dependencies:

class adafruit_sdcard.SDCard(spi: SPI, cs: DigitalInOut, baudrate: int = 1320000)[source]

Controls an SD card over SPI.

Parameters:
  • spi (SPI) – The SPI bus

  • cs (DigitalInOut) – The chip select connected to the card

  • baudrate (int) – The SPI data rate to use after card setup

Example usage:

import busio
import storage
import adafruit_sdcard
import os
import board

spi = busio.SPI(SCK, MOSI, MISO)
sd = adafruit_sdcard.SDCard(spi, board.SD_CS)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, '/sd')
os.listdir('/')
count() int[source]

Returns the total number of sectors.

Returns:

The number of 512-byte blocks

Return type:

int

readblocks(start_block: int, buf: array | bytearray | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray) int[source]

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.

writeblocks(start_block: int, buf: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray) int[source]

Write one or more blocks to the card

Parameters:
  • start_block (int) – The block to start writing to

  • buf (ReadableBuffer) – The buffer to write into. Length must be multiple of 512.

adafruit_sdcard.calculate_crc(message: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray) int[source]

Calculate the CRC of message[0:5], using a precomputed table in CRC_TABLE.

Parameters:

message (bytearray) – Where each index is a byte