A FAT filesystem with SPI driver for SD card on Raspberry Pi Pico
A FAT filesystem with SPI driver for SD card on Raspberry Pi Pico
Note:
This repository is more or less frozen. Exceptions will be made for bug fixes and compatibility with new releases of the Pico SDK and FatFS.
Any new development will take place in the new repository: no-OS-FatFS-SD-SDIO-SPI-RPi-Pico.
This repository supports SPI-attached SD cards only. The new repository supports SPI- and/or SDIO-attached SD cards.
At the heart of this library is ChaN's FatFs - Generic FAT Filesystem Module. It also contains a Serial Peripheral Interface (SPI) SD Card block driver for the Raspberry Pi Pico, derived from SDBlockDevice from Mbed OS 5. It is wrapped up in a complete runnable project, with a little command line interface, some self tests, and an example data logging application.
dma_claim_unused_channel.irq_add_shared_handler and enabled.Using a Debug build: Writing and reading a file of 0xC0000000 (3,221,225,472) random bytes (3 GiB) on a SanDisk 32GB card with SPI baud rate 12,500,000:
On a SanDisk Class 4 16 GB card, I have been able to push the SPI baud rate as far as 20,833,333 which increases the transfer speed proportionately (but SDIO would be faster!).
Raspberry Pi Pico
Something like the Adafruit Micro SD SPI or SDIO Card Breakout Board[^3] or SparkFun microSD Transflash Breakout
Warning: Avoid Aduino breakout boards like these: Micro SD Storage Board Micro SD Card Modules. They are designed for 5 V Arduino signals. Many use simple resistor dividers to drop the signal voltage, and will not work properly with the 3.3 V Raspberry Pi Pico. However, see The 5V Arduino SD modules might work with a simple trick.
Some people have made micro SD card sockets by soldering pins onto old SD Card adapters.
| SPI0 | GPIO | Pin | SPI | MicroSD 0 | Description | |
|---|---|---|---|---|---|---|
| MISO | RX | 16 | 21 | DO | DO | Master In, Slave Out |
| CS0 | CSn | 17 | 22 | SS or CS | CS | Slave (or Chip) Select |
| SCK | SCK | 18 | 24 | SCLK | CLK | SPI clock |
| MOSI | TX | 19 | 25 | DI | DI | Master Out, Slave In |
| CD | 22 | 29 | CD | Card Detect | ||
| GND | 18,23 | GND | Ground | |||
| 3v3 | 36 | 3v3 | 3.3 volt power |
I don't think the Pimoroni Pico VGA Demo Base can work with a built in RP2040 SPI controller. It looks like RP20040 SPI0 SCK needs to be on GPIO 2, 6, or 18 (pin 4, 9, or 24, respectively), but Pimoroni wired it to GPIO 5 (pin 7).
The SparkFun RP2040 Thing Plus works well, on SPI1. The only downside to this board is that it's difficult to access the signal lines if you want to look at them with, say, a logic analyzer or an oscilloscope.
For SparkFun RP2040 Thing Plus:
| SPI0 | GPIO | Description | |
|---|---|---|---|
| MISO | RX | 12 | Master In, Slave Out |
| CS0 | CSn | 09 | Slave (or Chip) Select |
| SCK | SCK | 14 | SPI clock |
| MOSI | TX | 15 | Master Out, Slave In |
| CD | Card Detect |
Maker Pi Pico looks like it could work on SPI1. It has CS on GPIO 15, which is not a pin that the RP2040 built in SPI1 controller would drive as CS, but this driver controls CS explicitly with gpio_put, so it doesn't matter.
git clone --recurse-submodules [email protected]:carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git no-OS-FatFSff14a/source/ffconf.h as desiredpico_enable_stdio_uart and pico_enable_stdio_usb in CMakeLists.txt as you prefer.
(See 4.1. Serial input and output on Raspberry Pi Pico in Getting started with Raspberry Pi Pico and 2.7.1. Standard Input/Output (stdio) Support in Raspberry Pi Pico C/C++ SDK.) cd no-OS-FatFS/example
mkdir build
cd build
cmake ..
makeThis library can support many different hardware configurations.
Therefore, the hardware configuration is not defined in the library[^1].
Instead, the application must provide it.
The configuration is defined in "objects" of type spi_t (see sd_driver/spi.h) and sd_card_t (see sd_driver/sd_card.h).
There can be one or more objects of both types.
These objects specify which pins to use for what, SPI baud rate, features like Card Detect, etc.
sd_card_t describes the configuration of one SD card socket.…pcName FatFs Logical Drive name (or "number")ss_gpio Slave Select (or Chip Select [CS]) for this SD carduse_card_detect Whether or not to use Card Detectcard_detect_gpio GPIO number of the Card Detect, connected to the SD card socket's Card Detect switch (sometimes marked DET)card_detected_true What the GPIO read returns when a card is present (Some sockets use active high, some low)set_drive_strength Whether or not to set the drive strNo open issues yet, or sync has not completed.