cancel
Showing results for 
Search instead for 
Did you mean: 

SD card example not working on STM32F746G-DISCO

johngj
Senior

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 ?

6 REPLIES 6
AScha.3
Chief II

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

 

If you feel a post has answered your question, please click "Accept as Solution".

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

 

 

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

If you feel a post has answered your question, please click "Accept as Solution".

I managed to get it working

I watched the video of the same guide and noticed the video has an extra step to add the DMA request for RX and TX...

johngj_0-1706020334426.png

Since doing that the code now writes to both SD cards BUT only when the SD mode is 1 bit.

It does not work when the SD mode is 4 bit.

I did try manually changing to 1 bit by setting...

hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B;

But this did not seem to work.

It only started working after I added the DMA requests as shown in the video, but not the written guide.

So the error is the missing step in the guide then ?

I assume SD mode 4 bit wide would be faster than 1 bit ?  In which case, how do I get 4 bit wide mode working ?

 

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

👍

About DMA : i had it always without dma (in fatfs : dma template off) , so the dma setting should be not important for working or not. (No dma: because i anyway wait for the data on a read etc , so no sense to make it more complicated.)

SD mode 4 bit wide is faster , but if you dont need maximum speed, why  make it more complicated. 🙂

But try - it should work now, just set it in Cube. (+ check: its connected 4bit on your board !!!)

+ First set correct speed for sdio: i use 100MHz , and clkdiv 1 in sdio parameter (in Cube) , = 50M for card.

If you feel a post has answered your question, please click "Accept as Solution".