Configuring STM32L431 SDMMC for high speed data transfer using DMA
Hello everyone,
I have been working with the STM32L431RCTx on a project where I need to log audio data to an SD card. I have attempted to do multiple things unsuccessfully and would really appreciate your help to see where I am going wrong. I am new to working with SD cards, so I recognise that I may have missed something in my research. For all of the below, I have tested with SDHC and SDXC cards with the same results.
First; I have tried to setup the SDMMC interface with at least 20MHz clock speed (clock divide factor 0)
- The behaviour is very intermittent. It sometimes mounts, opens, writes, closes, then unmounts, but other times will not open or write without an FR_DISK_ERR. My error handling attempts to correct any errors, but I believe there is no fix for a FR_DISK_ERR right? After a while a Hard Fault occurs.
- I tried again with 40MHz clock speed and clock divide factor of 4. With these settings it goes for longer without errors, however now and then there are still errors that creap in. After a while a Hard Fault occurs too
Second; I have tried to set up 4-bit wide bus communication
- This failed due to the code getting stuck in a timeout during the SD card initialisation. The timeout value is 0xFFFFFFFF so it is not an infinite loop, but definitely as good as infinite for how long the device will be stuck.
Third; I have tried to enable the DMA transfer using the DMA template in the FatFS settings
- I won't go into this here (I'll post another question for this later), but at this stage I had no luck with that either. Once I have resolved these other issues I will look more into the DMA. However if any of you know of any resources for using the DMA with the STM32L431, I would really appreciate it. What I did was to enable the DMA template in FatFs, with the DMA settings in SDMMC enabled.
I got it working using 1-bit wide bus, 2MHz clock speed, 0 clock divide factor, FatFs enabled, DMA disabled, SDMMC1 global interrupt enabled.





The Detect_SDIO pin is selected (in this case input on PC6)
Using this configuration I can read, write, and format etc. However if I try to keep the settings the same, while increasing the speed above 2MHz; or changing the bus width to 4-bit, I get the errors described above. This seems to be the only stable combination of settings. This is unfortunately way too slow for my application, considering that I believe it should be able to operate at 32MHz, 4-bit wide bus; that would mean a 64 times increase in speed from 2MHz 1-bit wide bus.
I have attached my slow but working configuration code below. My system clock speed is 20MHz, derived from an 8MHz crystal.
I would really appreciate any assistance. If you notice any fundamental flaws in my understanding of working with SD card I'd welcome the feedback. Thank you in advance.
