How to use SD card with stm32 and SdFat library - GitMasterNikanjam/Arduino_WiKi GitHub Wiki
It’s fundamental for data logging the interfacing with SD cards. STM32 doesn’t support well native SD library, so we will use the SdFat library, which has an Adafruit fork also that we’ll use for other purposes.
Protocol SD card has a native host interface apart from the SPI mode for communicating with master devices. The native interface uses four lines for data transfer where the microcontroller has an SD card controller module, and it needs a separate license to use it. Since the SPI is a widely used protocol and is available in most low-cost microcontrollers, the SPI mode is the widely used interface in low-cost embedded systems. The operating voltage range of the SD family is 2.7V to 3.6V, and this is indicated in the operation condition register (OCR). Exist a low-power SD Card that operates at 1.8V but isn’t so used.
SD Pinout Exists various form factors, but the base pinout is the same.
Pin Number | Pin Name | In SD Mode | In SPI Mode |
---|---|---|---|
1 | DAT2/X | Connector Data line 2 | No use |
2 | DAT3/CS | Connector Data line 3 | Chip Select |
3 | CMD/DI | Command / Response Line | Data Input |
4 | VDD/VDD | Power supply (+3.3V) | Power supply (+3.3V) |
5 | CLK/SCLK | Clock | Serial Clock |
6 | VSS/VSS | Ground | Ground |
7 | DAT0/D0 | Connector Data line 0 | Data Out |
8 | DAT1/X | Connector Data line 1 | No use |
Library We are going to use the SdFat library for this test, the original one from greiman,
but exists a fork from Adafruit, and for a lot of my projects, I use that.
You can install the library from the Arduino libraries manager.
Wiring
Wiring primary SPI The most common wiring is to use primary SPI, but It isn’t always the better choice, especially when we have SPI Flash or Ethernet on the same SPI interface.
STM32F1 (blue-pill) Here is the raw wiring schema on primary SPI.
Examples
For more information and example see sdcard projects in bluepill repo.