STM32 SDIO SD Card Example With FatFS [Interfacing Tutorial]

This is a comprehensive guide for STM32 SDIO SD Card Interfacing With FatFS Library. You’ll learn how to use SD Cards with STM32 microcontrollers using the SDIO interface. We’ll create some STM32 SD Card Example Test Projects to verify what we’ll be learning in this tutorial.

Table of Contents

  1. STM32 SDIO (Secure Digital IO)
  2. STM32 SDIO SD Card Interfacing
  3. STM32 SDIO SD Card FatFS Example Project
  4. STM32 SDIO 4-Bit Example
  5. Wrap Up

STM32 SDIO (Secure Digital IO)

Some STM32 microcontroller series have an integrated SDIO (SDMMC) hardware peripheral that’s designed specifically to interface SD cards at the maximum speed.

However, SD cards can still be used over SPI communication which is available in all STM32 microcontrollers and pretty much every single microcontroller in the market. We’ve already focused on STM32 SD Card SPI interfacing in this previous tutorial. Therefore, in this tutorial, we’ll shift the attention to using the STM32 SDIO interface for SD Card handling.

STM32 SPI Vs SDIO Vs SDMMC

SPI is a generic serial peripheral interface and can still be used to interface SD cards with low-end microcontrollers at a relatively lower speed of communication and a much simpler software stack. That’s why SPI is the most commonly used interface for SD cards in a lot of projects.

SDIO is a hardware peripheral designed specifically for interfacing (SD Cards, SDIO Cards, and MultiMedia Cards “MMC”) with the APB2 peripheral bus in “some” of the STM32 microcontrollers. Given that it’s dedicated to SD card interfacing, it’s going to be a much faster way of communicating with SD cards (4x the speed you can get with an SPI interface).

SDMMC is a hardware peripheral designed specifically for interfacing (SD memory cards, SDIO cards, and eMMC devices) with the APB2 peripheral bus in “some” of the STM32 microcontrollers. It’s almost identical to the SDIO interface but it supports eMMC devices additionally and can go up to way higher transfer speeds (in 8-Bit mode).

STM32 SDIO Features

  • Full compliance with MultiMediaCard (MMC) System Specification Version 4.2. Card support for three different databus modes: 1-bit (default), 4-bit and 8-bit
  • Full compatibility with previous versions of MultiMediaCards (backward compatibility)
  • Data transfer up to 50 MHz for the 8-bit mode
❕ Note

The SDIO does not have an SPI-compatible communication mode.


STM32 SDIO SD Card Interfacing

In this section, we’ll discuss how to interface STM32 microcontrollers with SD Cards using the SDIO interface.

Micro SD Card Memory

Preparing The SD Card

Use an SD Card Reader for this step.

Before using your SD card, make sure you’ve Formatted it to the FAT (FAT32/FAT) file system (in your operating system of choice).

STM32 SDIO SD Card Hardware Design

If you’re designing your own STM32-based PCB board project that requires having an SD card memory slot onboard, you’ll need to connect your STM32 SDIO pins to the SD Card slot as shown below.

STM32-SDIO-Hardware-Circuit-SD-Card-PCB

The GPIO pin (PA8) in the schematic design shown above is used for “SD Card Presence Detection”, and you can read its state in software if needed.

Also keep in mind that all SDIO pins, except the CLK, need to be pulled up either in hardware or in software configurations of the STM32 GPIO pins. While routing the DATA lines during the PCB design, make sure the lines are matched to maximize signal integrity for high-speed communication.

Development Board

For quick prototyping and project idea testing, you can use any STM32 development board that has a target microcontroller with an internal SDIO interface as well as the hardware SD Card socket onboard. For me, I’ve used This STM32F405 Development Board from WeAct. But you can use any other STM32 development board that satisfies those two conditions.

However, if you’ve got an STM32 development board that doesn’t have an SD card slot but its target microcontroller still has an SDIO interface, you can use an SD Card breakout board like this and wire them up together as shown in the schematic diagram shown earlier.

SD-Card-SDIO-Breakout-Board

Buy an SD Card SDIO Breakout Board (on Amazon)


STM32 SDIO SD Card FatFS Example Project

In this example project, our ultimate goal is to test the STM32 SDIO interface with an SD Card and also test the functionalities provided by the FatFS library and use it to create a text file, write to it, read the file, modify the existing file, and delete the file. We’ll monitor the progress of this test sequence using USB CDC (VCP) messages printed to the serial monitor on the PC.

SD Card Tests Included in This Project:

  • Mount The SD Card
  • Find and print the card size & free space
  • Create a new Text File
  • Write to the text file using the f_puts() function
  • Write to the text file using the f_write() function
  • Read from the text file using the f_gets() function
  • Read from the text file using the f_read() function
  • Modify (update) an existing file
  • Delete the file
  • Unmount the SD Card

Please, follow The step-by-step guide below to create the project, configure the needed hardware peripherals in CubeMX, add the FATFS_SD driver folder, and run the test project on your STM32 dev board.

Step #1

Create a New Project in STM32CubeMX

The first step is to head over to STM32CubeMX, create a new project, enable the SWD (serial wire debug), enable the external HSE oscillator, and configure the RCC clock.

Step #2

Configure 1x USB CDC Device

Enable a USB CDC device to be used as a serial communication with the PC through Windows virtual COM port (VCP). Leave the default settings as is, no need to change them.

Step #3

Configure The SDIO Module To Be Used For SD Card Interfacing

Here, I’m using the SDIO 1-Bit mode while the rest of the settings are left as default.

STM32-SDIO-Example-Configurations-CubeMX

Make sure that all SDIO GPIO pins are internally, or externally, pulled-up except for the CLK line.

STM32-SDIO-GPIO-Pull-Up

Step #4

Enable The FatFS Library

Edit the library configurations as shown below.

STM32-SDIO-FatFS-SD-Card-Example-Configurations

Once you’re done with CubeMX configurations, generate the project code and head over to STM32CubeIDE.

Step #5

Copy The Project’s Code Below into Your Main.c File

Build The Project, Flash The Code To The STM32 Board, and Start Testing!

STM32 SDIO SD Card FatFS Example Code

The Application Code For This Example (main.c)

Test Setup

This is my test setup for this example project. I’m using a 2GB SD card as well as a USB cable to connect the STM32 USB CDC to my PC as a virtual COM port. The SD card socket is already soldered to the bottom of my development board as stated earlier according to the schematic design figure that I did also show earlier in this tutorial. That’s all about it!

STM32F405-Development-Board

Testing Result

Here is the test result that’s printed on the serial monitor.

STM32-SDIO-SD-Card-Example-Testing-Result

And this is the content of the SD card after running this example project as shown on my PC file manager.

STM32-SDIO-SD-Card-Example-TextFile-Testing-Result


STM32 SDIO 4-Bit Example

You can use the STM32 SDIO 4-Bit mode by choosing it in the CubeMX configurations as shown below.

STM32-SDIO-4-Bit-Mode-Example-Configurations-CubeMX

❕ Note

Do not forget to activate internal pull-ups for all SDIO lines except for the CLK line (if they are not already externally pulled up in your board hardware schematic design).

You can use the exact same code of the previous example project with no extra changes, only the CubeMX configurations need adjustment and you may also need to reduce the clock speed of SDIO as illustrated hereafter in the following section.

STM32 SDIO Clock Configuration & Divider

The SDIO peripheral uses the 48MHz clock just like the USB and it divides it by 2 (by default) according to the following equation.

SDIO_CK = SDIOCLK / [CLKDIV + 2]

Leaving the CLKDIV factor as 0 will result in an SDIO_CK = 48 / [0 + 2] = 24MHz. Which is the maximum operating speed for the SDIO interface.

You can slow it down by increasing the CLKDIV factor if you’re getting errors while mounting the SD card.


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1SD Card SDIO Breakout BoardAmazonAliExpresseBay
1SD Card Reader (For PC)AmazonAliExpresseBay
1SD Card 8GBAmazonAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve discussed how to interface STM32 microcontrollers with SD Card memory using the SDIO hardware interface with the FatFS firmware library. The provided example project should be a very good starting point for your next STM32 SDIO SD Card interfacing project. You can also explore the other parts of the STM32 SD Card tutorials series for more information about the other STM32 SD Card interfacing options.

This Tutorial is Part of The Following Multi-Part Tutorial Series:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment