cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 HAL SDIO File operations with FATFS using DMA.

Kunaalkk1
Associate

I am trying to perform SD Card file operations using STM32 HAL in STM32L451RCT6. I have been successful in mounting the SD Card, but read and write operations are not working. Here are the details of my IOC Configuration:

1. FatFS Configuration

Kunaalkk1_7-1687260565631.png

Kunaalkk1_0-1687259486389.png

Kunaalkk1_1-1687259637020.png

Kunaalkk1_0-1687259755564.png

2. SDMMC Configuration

Kunaalkk1_1-1687260108807.png

Kunaalkk1_2-1687260188784.png

Kunaalkk1_3-1687260232942.png

Kunaalkk1_5-1687260361771.png

3. Clock configuration (Note: Since I am using UART and I2C as well, their clocks are also configured.

Kunaalkk1_8-1687260710874.png

Here is the code implementation:

 

 

MX_SDMMC1_SD_Init();
MX_FATFS_Init();

if (f_mount(&SDFatFS, "", 1) != FR_OK) {
Error_Handler();
}

if(HAL_SD_GetCardInfo(&hsd1, &card_info) != HAL_OK) {
Error_Handler();
}

if(HAL_SD_GetCardStatus(&hsd1, &card_status) != HAL_OK) {
Error_Handler();
}

 

 

Here are the corresponding values:

Kunaalkk1_9-1687261118513.png

The remaining code is where the problem occurs and it goes into the error handler:

 

if (f_open(&myFile, "sample.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ) != FR_OK) {
	Error_Handler();
}

if (f_write(&myFile, "sample text", strlen("sample text"), &byteCount) != FR_OK) {
	Error_Handler();
}

if (f_close(&myFile) != FR_OK) {
	Error_Handler();
}

 

When I call f_open() and f_write(), it is not implementing the sdmmc interrupt callback and gets stuck in the SD_Read or SD_Write() function until the timeout of 30 seconds. In the below code snippet, the WriteStatus is updated to 1 in the BSP_SD_WriteCpltCallback() function.

Kunaalkk1_0-1687261858936.png

Can someone please identify and tell me, what I am doing wrong?

 

Thanks in advance.
Regards,
Kunaal

3 REPLIES 3
Kunaalkk1
Associate

I have made some progress in this, I took some suggestions and since I was using a single DMA channel for SDMMC Tx and Rx, I was supposed to switch directions between Memory_to_Peripheral and Peripheral_to_Memory. After this, the f_open(), f_close() and f_read() functions return FR_OK, which means they are working. However:

1. The f_write is not working even after switching the DMA direction

2. If I modify the file on my windows PC, I am not able to open it due to some encoding problem that I am not able to figure out.

antfarmer
Associate III

I also had trouble with this and just got it working on an L4 chip, using this info. Read and write are ok and a bit faster now as well. I used Linux to verify the updates, but as a suggestion, you may want to try enabling hardware flow control on the SDMMC module. That allowed the SDIO clock to go from 3 to 48MHz for me. Also, try enabling USE_LFN under FatFS, with dynamic buffer on Heap. On that note, make sure you increase the heap/stack to something like 0x400/0x800 respectively, as in this ST video tutorial.

Embedded_Engineer
Associate II

Enable "USE_LFN ( Long File Name ) in FatFS Configuration>> Set Defines
then secondly in SDMMC Configuration  you just need to use 1 bit configuration as most of the SD cards above 8 GB will only work in 1 bit configuration

or you can can code as

Embedded_Engineer_0-1701672305486.png

by doing this your problem will be resolved

also you can enable FS_EXFAT to use linux partition SD cards