STM32 SDIO DMA Example

In this tutorial, we’ll learn how to use STM32 SDIO + DMA With FatFS Library For SD Card Interfacing. You’ll learn how to configure the STM32 SDIO With DMA enabled for faster data read/write operations with less CPU intervention. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 SDIO (Secure Digital IO)
  2. STM32 SDIO DMA Example
  3. Wrap Up

STM32 SDIO (Secure Digital IO)

In this previous tutorial, we discussed the STM32 SDIO interface in detail and created an example project for STM32 SD card interfacing using the SDIO interface with CPU-blocking read/write operations. Therefore, in this tutorial, we’ll expand more on the topic by adding DMA Rx/Tx channels for both read/write operations of the SDIO to accelerate the performance and also save as much CPU time as possible.

It’s highly recommended to check out the previous STM32 SDIO Tutorial as a starting point (if you’ve not already completed it).

STM32 SDIO SD Card Example With FatFS [Interfacing Tutorial]

This tutorial will give you more in-depth information about the STM32 SDIO interface and how to use it with FatFS for SD Card interfacing with a handful of test examples.


STM32 SDIO DMA Example

In this example project, we’ll test the STM32 SDIO + DMA 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.

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

In the SDIO DMA settings tab, add 2x DMA channels for (Rx & Tx) as shown below.

STM32-SDIO-DMA-Example-Enable-Configuration

In the SDIO NVIC tab, enable the SDIO global interrupt as shown below.

STM32-SDIO-DMA-Interrupt-Example

Step #4

Enable The FatFS Library

Edit the library configurations as shown below.

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

In the FatFS advanced settings tab, enable the DMA template.

STM32-SDIO-DMA-Enable-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 SDIO DMA 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 STM32F405 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-DMA-Example-Test-Result-1

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

STM32-SDIO-DMA-Example-Test-Result

❕ Note

It may not be very obvious that this example project is handling the SDIO (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

❕ Note

The advantage of using DMA over the polling version of SD read/write operations will be obviously visible when you’re attempting to read/write big chunks of data into large files. This is when you can notice major CPU time savings and CPU load reduction overall.


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 + DMA with the FatFS firmware library. The provided example project should be a very good starting point for your next STM32 SDIO + DMA 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