2023-06-20 05:00 AM
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
2. SDMMC Configuration
3. Clock configuration (Note: Since I am using UART and I2C as well, their clocks are also configured.
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();
}
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.
Can someone please identify and tell me, what I am doing wrong?
Thanks in advance.
Regards,
Kunaal
2023-06-22 02:05 AM - edited 2023-06-22 09:34 PM
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.
2023-12-03 10:34 PM
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.
2023-12-03 10:48 PM - edited 2023-12-03 10:48 PM
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
by doing this your problem will be resolved
also you can enable FS_EXFAT to use linux partition SD cards