STM32 SDMMC Tutorial With Examples + DMA

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

Table of Contents

  1. STM32 SDMMC
  2. STM32 SDMMC SD Card Interfacing
  3. STM32 SDMMC (4-Bit Mode) FatFS Example Project
  4. STM32 SDMMC DMA Example
  5. Wrap Up

STM32 SDMMC

Some STM32 microcontroller series have an integrated SDMMC hardware peripheral that’s designed specifically to interface SD cards at the maximum operating speed. The SDMMC interface provides an interface between the AHB bus and (SD memory cards, SDIO cards, and eMMC devices).

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, and STM32 SDIO Interface in this other previous tutorial. Therefore, in today’s tutorial, we’ll shift the attention to using the STM32 SDMMC 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 SDMMC Features

  • Compliance with Embedded MultiMediaCard (eMMC) System Specification Version 4.51
  • 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)
  • Full compliance with SDIO card specification version 4.0
  • Data transfer up to 208 Mbytes/s for the 8-bit mode
❕ Note

The SDMMC does not have an SPI-compatible communication mode. And it only supports one (SD/SDIO/eMMC) card at a time.


STM32 SDMMC SD Card Interfacing

In this section, we’ll discuss how to interface STM32 microcontrollers with SD Cards using the SDMMC 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 SDMMC 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 SDMMC pins to the SD Card slot as shown below.

STM32-SDMMC-Schematic-Hardware-Circuit-PCB

The MicroSD_SW pin 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 SDMMC 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 SDMMC interface as well as the hardware SD Card socket onboard. For me, I’ve used This STM32H750 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 SDMMC 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 SDMMC Breakout Board (on Amazon)


STM32 SDMMC (4-Bit Mode) FatFS Example Project

In this example project, our ultimate goal is to test the STM32 SDMMC 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, 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.

Make sure that the USB clock & SDMMC clock are both 48MHz.

Step #2

Configure 1x USB CDC Device

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

Step #3

Configure The SDMMC Module To Be Used For SD Card Interfacing

Here, I’m using the SDMMC 4-Bit mode, and increase the SDMMC clock divider factor a bit to stay below the speed limit of your SD card’s slass.

STM32-SDMMC-4-Bit-Mode-Example-Configuration

Step #4

Enable The FatFS Library

Edit the library configurations as shown below.

STM32-SDMMC-FatFS-Example-Configuration

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 SDMMC 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 16GB 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 top side of my STM32H750 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!

STM32H750-Development-Board-WeAct

Testing Result

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

STM32-SDMMC-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-SDMMC-4-Bit-Mode-DMA-Example-Testing-Result


STM32 SDMMC DMA Example

Setting up the STM32 SDMMC DMA is very similar to what we’ve done in the STM32 SDIO DMA Tutorial previously. You can refer to the SDIO + DMA tutorial to see how it’s configured and use the exact same code provided in the example of this tutorial and it should work just as fine.

STM32 SDIO DMA Example

This article will give more in-depth information about configuring an STM32 SDIO interface with DMA channels for Rx/Tx operations and test its functionality in an example project.

❕ Note

It may not be very obvious that your final project will be handling the SDMMC (read/write) operations using DMA. However, you can check the following files to make sure that the application-level drivers are using the DMA channels under the hood.

FATFS/Target/sd_diskio.c

You’ll find the  SD_read() &  SD_write() functions are using the DMA versions of the BSP_SD_ReadBlocks_DMA() & BSP_SD_WriteBlocks_DMA() functions. You can also find their implementations in the source file below.

FATFS/Target/bsp_driver_sd.c


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 SDMMC hardware interface with the FatFS firmware library. You can build on top of the example provided in this tutorial and/or 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