Skip to main content
Graduate II
January 23, 2024
Question

SD card example not working on STM32F746G-DISCO

  • January 23, 2024
  • 2 replies
  • 2468 views

I purchased the STM32F746G-DISCO in the hope of being able to read and write to an SD card.

I followed the guide below...

https://community.st.com/t5/stm32-mcus/how-to-create-a-file-system-on-a-sd-card-using-stm32cubeide/ta-p/49830 

However, when stepping through the code I can see it is going to the Error_Handler when the function f_mkfs is called...

 

 

if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext)) != FR_OK)
{
 Error_Handler();
}

 

 

 

I have tried two different SD cards:

1. Kingston 16GB microSDHC CL10 

2. Transcend 2 GB MicroSD Micro SD Card, Class 30 (TS2GUSD)

When reading the comments at the bottom of the guide, I noticed that others also had the same issue .

I tried the suggestion made by TKana.1 (i.e. moved variables to global and changed 4B wide operation to 1B operation in MX_SDMMC1_SD_Init) but the error still occurs.

Why does the code in this guide not work and how do I fix it ?

    This topic has been closed for replies.

    2 replies

    Super User
    January 23, 2024

    1. Never do mkfs (=format) or write at first , until you know, mount + read is working fine !

    2. At beginning, use 1bit mode. (4bit more difficult, just faster; can try, after 1bit mode working perfect.)

    read about...:

    https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/m-p/625603#M26901

    +

    https://community.st.com/t5/stm32-mcus-embedded-software/stm32h743xi-sdmmc1-sd-card-hal-sd-error-unsupported-feature/m-p/63500#M1178

     

    johngjAuthor
    Graduate II
    January 23, 2024

    I used the code from the guide (shown below for reference) where it does f_mount and then f_mkfs.

    Are you saying the code in this guide is wrong ?  If so is the an example I can use that does work ?

    I already tried changing to 1 bit mode (as suggested by TKana.1 in the comment at the bottom of the guide), but it made no difference.

    Shouldn't these example projects be plug and play and just work ? 

    I've worked with many other examples for all sorts of processors, but I havent been able to find an SD card example that works (I have also tried NXP without success). 

    For some reason it seems that SD card drivers are very hit and miss and a black art ?

     

    /* USER CODE BEGIN 1 */
    FRESULT res; /* FatFs function common result code */
    uint32_t byteswritten, bytesread; /* File write/read counts */
    uint8_t wtext[] = "STM32 FATFS works great!"; /* File write buffer */
    uint8_t rtext[_MAX_SS];/* File read buffer */
    /* USER CODE END 1 */
     ...
    /* USER CODE BEGIN 2 */
    	if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
    	{
    		Error_Handler();
    	}
    	else
    	{
    		if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext)) != FR_OK)
    	 {
    			Error_Handler();
    	 }
    		else
    		{
    			//Open file for writing (Create)
    			if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
    			{
    				Error_Handler();
    			}
    			else
    			{
    
    				//Write to the text file
    				res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
    				if((byteswritten == 0) || (res != FR_OK))
    				{
    					Error_Handler();
    				}
    				else
    				{
    
    					f_close(&SDFile);
    				}
    			}
    		}
    	}
    	f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
    /* USER CODE END 2 */

     

     

    Super User
    January 23, 2024

    >Are you saying the code in this guide is wrong ?

    No, not wrong - but a bad idea for beginner. 

     

    >Shouldn't these example projects be plug and play and just work ?

    ... and the world should be a peaceful place with only nice people - same.

     

    For some reason it seems that SD card drivers are very hit and miss and a black art ?

    No , not rocket science - but close. :)

     

    Now, you should get it working, without big problems, on a STM32F746G-DISCO board, because here at least hardware is ok. ( Many others building something new, with many more problems by hardware design.) 

    So lets begin: 

    - set in Cube sdio for 1 bit , + fatfs , + fatfs->sdcard . ok ?

    - then in main:   

    extern char SDPath[];

    FRESULT fresult ;          // result

     

    fresult = f_mount(&SDfs, (TCHAR const*)SDPath, 1); // SD card mount

    look in debug : is fresult ok (=0) ?  then mount ok. Use a working sdcard with some files on it, but nothing important.

    Graduate II
    January 23, 2024

    Cards come formatted, they don't need reformatting unless you trash the file system.

    Best NOT to use f_mkfs() unless doing so as a "format" for an end user via a dialog / user interface option.

    Also doing any writes until you've fully validated the DISKIO implementation is unwise.

    Demo's tend just to be a proof-of-concept, in reality a lot of different cards exist with different levels of features and functionality. Cheap / bootleg quality ones generally giving the most trouble for tech support, etc.

    Demo's in the context of ST Staff are really "Hey look it works, check that off the list, next"

    ST also keeps changing it's strategy with SDIO/SDMMC, at one time you put the 4-bit mode in the init structure, other times you put it in 1-bit mode, and then call the "WideMode" function later to negotiate a higher speed.

    4-Bit mode is far less tolerant of issues with the electrical connectivity, so termination, reflections and signal skewing get to be things as the clock rate increases.

    Cards also have zero tolerance of you wandering off-task mid transfer, there is a FIFO, but it's depth is limited. ST's pollled-mode operation is also flawed, it deals with everything as misaligned, which is far from optimal.

    DMA is preferred, but that brings it's own issues of coherency. For the F7 using the DTCM for aligned buffers is strongly recommended. The DCache methods also have their own set of issues, most particularly the 32-byte width, and the ability to corrupt abutting structures/data.