百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

no-OS-FatFS-SD-SPI-RPi-Pico

> 编程语言
开源

一个使用 SPI 驱动的 FAT 文件系统,用于 Raspberry Pi Pico 上的 SD 卡

350 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个使用 SPI 驱动的 FAT 文件系统,用于 Raspberry Pi Pico 上的 SD 卡


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.


no-OS-FatFS-SD-SPI-RPi-Pico

Simple library for SD Cards on the Pico

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.

Features:

  • Supports multiple SD Cards
  • Supports multiple SPIs
  • Supports multiple SD Cards per SPI
  • Supports Real Time Clock for maintaining file and directory time stamps
  • Supports Cyclic Redundancy Check (CRC)
  • Plus all the neat features provided by FatFS

Resources Used

  • At least one (depending on configuration) of the two Serial Peripheral Interface (SPI) controllers is used.
  • For each SPI controller used, two DMA channels are claimed with dma_claim_unused_channel.
  • DMA_IRQ_0 is hooked with irq_add_shared_handler and enabled.
  • For each SPI controller used, one GPIO is needed for each of RX, TX, and SCK. Note: each SPI controller can only use a limited set of GPIOs for these functions.
  • For each SD card attached to an SPI controller, a GPIO is needed for CS, and, optionally, another for CD (Card Detect).

Performance

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:

  • Writing
    • Elapsed seconds 4113.8
    • Transfer rate 764.7 KiB/s
  • Reading (and verifying)
    • Elapsed seconds 3396.9
    • Transfer rate 926.1 KiB/s

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!).

Prerequisites:

  • 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.

  • Breadboard and wires
  • Raspberry Pi Pico C/C++ SDK
  • (Optional) A couple of ~5-10kΩ resistors for pull-ups
  • (Optional) A couple of capacitors for decoupling. Ideally, use three capacitors: 10 uF, 0.1 uF (100 nF), and .001 uF (1 nF) on the Vdd line to the of the SD card, located close to the socket. (The Adafruit board already has the 10 uF and 0.1 uF: Schematic.)
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

Construction:

  • The wiring is so simple that I didn't bother with a schematic. I just referred to the table above, wiring point-to-point from the Pin column on the Pico to the MicroSD 0 column on the Transflash.
  • Card Detect is optional. Some SD card sockets have no provision for it. Even if it is provided by the hardware, if you have no requirement for it you can skip it and save a Pico I/O pin.
  • You can choose to use either or both of the Pico's SPIs.
  • Wires should be kept short and direct. SPI operates at HF radio frequencies.

Pull Up Resistors and other electrical considerations

  • The SPI MISO (DO on SD card, SPIx RX on Pico) is open collector (or tristate). It should be pulled up. The Pico internal gpio_pull_up is weak: around 56uA or 60kΩ. It's best to add an external pull up resistor of around 5kΩ to 3.3v. You might get away without one if you only run one SD card and don't push the SPI baud rate too high.
  • The SPI Slave Select (SS), or Chip Select (CS) line enables one SPI slave of possibly multiple slaves on the bus. This is what enables the tristate buffer for Data Out (DO), among other things. It's best to pull CS up so that it doesn't float before the Pico GPIO is initialized. It is imperative to pull it up for any devices on the bus that aren't initialized. For example, if you have two SD cards on one bus but the firmware is aware of only one card (see hw_config); you can't let the CS float on the unused one.
  • Driving the SD card directly with the GPIOs is not ideal. Take a look at the CM1624 (https://www.onsemi.com/pdf/datasheet/cm1624-d.pdf). Unfortunately, it's a tiny little surface mount part -- not so easy to work with, but the schematic in the data sheet is still instructive. Besides the pull up resistors, it's probably not a bad idea to have 40 - 100 Ω series terminating resistors at the SD card end of CS, SCK, MISO, MOSI.
  • It can be helpful to add a decoupling capacitor or two (e.g., 10, 100 nF) between 3.3 V and GND on the SD card.
  • Note: the Adafruit Breakout Board takes care of the pull ups and decoupling caps, but the Sparkfun one doesn't.

Notes about Card Detect

  • There is one case in which Card Detect can be important: when the user can hot swap the physical card while the file system is mounted. In this case, the application might have no way of knowing that the card was swapped, and so it will continue to assume that its prior knowledge of the FATs and directories is still valid. File system corruption and data loss are the likely result.
  • If Card Detect is used, in order to detect a card swap there needs to be a way for the application to be made aware of a change in state when the card is removed. This could take the form of a GPIO interrupt (see FatFS_SPI_example.cpp), or polling.
  • Some workarounds:
    • Periodically poll sd_test_com() which can be called any time after sd_init_driver() is called. This function minimally accesses the SPI interface to check for the presence of an SD card. The internals of this call automatically flags the SD interface for reinitialization when false is returned. If false is returned when the previous call returned true, it is important to invalidate any file handles that are still opened, unmount, and reset any mounted flags. Then don't try to remount until sd_test_com() returns true once again.
    • If you don't care much about performance or battery life, you could mount the card before each access and unmount it after. This might be a good strategy for a slow data logging application, for example.
    • Some other form of polling: if the card is periodically accessed at rate faster than the user can swap cards, then the temporary absence of a card will be noticed, so a swap will be detected. For example, if a data logging application writes a log record to the card once per second, it is unlikely that the user could swap cards between accesses.

Notes about prewired boards with SD card sockets:

  • 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.

Firmware:

  • Follow instructions in Getting started with Raspberry Pi Pico to set up the development environment.
  • Install source code: git clone --recurse-submodules [email protected]:carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git no-OS-FatFS
  • Customize:
    • Configure the code to match the hardware: see section Customizing for the Hardware Configuration, below.
    • Customize ff14a/source/ffconf.h as desired
    • Customize pico_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.)
  • Build:
bash
   cd no-OS-FatFS/example
   mkdir build
   cd build
   cmake ..
   make
  • Program the device

Customizing for the Hardware Configuration

This 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.

An instance of 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 card
  • use_card_detect Whether or not to use Card Detect
  • card_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 str

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言