Skip to main content
djhtml
Associate
August 19, 2018
Question

SDIO 4 bit mode with STM32CubeMX

  • August 19, 2018
  • 16 replies
  • 17999 views

I would like to use SDIO with 4 bit mode with FatFS. I use STM32F429I Discovery board and I wired a micro SD card board to it. The SD card is a 16 GB SanDisk Ultra A1 card formatted to FAT32.

This is the micro SD card board: https://image.dhgate.com/0x0s/f2-albu-g4-M01-BC-F9-rBVaEVmcHSeAdh6CAAKj2N4sVnA447.jpg/3-3v-5v-micro-sd-tf-card-reader-module-spi.jpg all lines are pulled up to 3V with 10k res (even the CLK).

Version of STM32CubeMX is 4.26.1, firmware version is STM32Cube FW_F4 V1.21.0, I use Atollic TrueSTUDIO 9.0.0 and FatFS version is R0.12c.

If I generate SDIO with 1 bit mode with CubeMX it works fine, I can read, write the card.

When I generate SDIO with 4 bit mode it is not working. Function 'f_mount' is returning with error code: 13 which is 'FR_NO_FILESYSTEM'. But there is a working file system on the card, I can use it with PC and it works with 1 bit SDIO mode also.

This is the relevant part of the code (all other part is generated with CubeMX):

// ...
 
uint8_t myWrite[30] = "TEST STRING";
uint8_t myRead[30];
 
// ...
 
int main(void)
{
	
	// ...
	
	printf("Start!\r\n");
 
	FATFS myFAT;
	FIL myFile;
	UINT byteCount;
 
	printf("path: '%s'\r\n", SDPath);
 
	FRESULT fRet = 0;
 
	fRet = f_mount(&myFAT, SDPath, 1);
	if(fRet == FR_OK){
		f_open(&myFile, "test.txt", FA_WRITE | FA_CREATE_ALWAYS);
		f_write(&myFile, myWrite, 30, &byteCount);
		f_close(&myFile);
 
		f_open(&myFile, "test.txt", FA_READ);
		f_read(&myFile, myRead, 5, &byteCount);
		f_close(&myFile);
	}else{
		printf("mount fail :(\r\n");
		printf("err. code: %d\r\n", fRet);
	}
	
	// ...
	
	while (1)
	{
		// ...
	}
}

The same code works if I generate SDIO 1 bit mode with CubeMX, but I get the above mentioned 'FR_NO_FILESYSTEM' error with 4 bit SDIO mode.

Please help me how can I solve this.

Thank you in advance!

This topic has been closed for replies.

16 replies

Tesla DeLorean
Guru
August 19, 2018

Doesn't the STM32F429I-DISCO have pin level clashes with the display?

The SDIO can sense if all bits on the bus aren't working properly at the beginning of a transfer, and there is a CRC at the end. FatFs will fail when data returned doesn't meet expectations, or gets a low level error.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Peter Mather
Associate III
August 19, 2018

in CubeMX, in the configuration of FATFS, the IP instances tab, make sure "use DMA template" is set to disable. This isn't the default and is a known issue

djhtml
djhtmlAuthor
Associate
August 19, 2018

Thank you for the answers!

@Community member​  Yes, STM's PC9 is SDIO D1 and also is SDA for the thouch sensor. But the other pins of the display is not shared with SDIO pins. Since there is no clock on SCL, SDA shouldn't be interfering with SDIO, should it? And I also don't get a low level error but a no file system error.

If I disconnect SDIO CMD or CLK (only one of them) I get error code 3, which is 'FR_NOT_READY' it means: 'The physical drive cannot work'. When I disconnect D0 or D1 or D2 or D3 (only one of them) the program prints: Start!... path: '0:/' and waits forever: if I pause it, it is in waiting for SD_TIMEOUT.

That's why I assume it is not low level problem.

@Community member​ I tried it with 'USE dma template: Disable' and no DMA in SDIO but did not work, also did not work with 1 bit mode. SDIO with DMA and 'USE dma template: Enable' it worked with 1 bit mode and did not work with 4 bit mode.

djhtml
djhtmlAuthor
Associate
August 21, 2018

I took a look with oscilloscope at all lines (D0-D3, CMD). They look fine, there is communication in every lines. All signals are 0-3V and looks "sharp".

How can I determine if there is a CRC error due to the clashes with the display signal, as @Community member​ said?

MWATS
Visitor II
September 20, 2018

I had same issue, dropped SDMMC clock speed to 25Mhz and all good.

URane
Associate
October 3, 2018

dear all

Can any one share complete project even with or without dma to get sdio 4 bit with cubemx 4.26 or 4.27 version ?

 Or at least step by step procedure either in the form of video or pictures.

Ade J
Visitor II
October 4, 2018

@MWATS​  I tried dropping clock to 24MHz but was not my problem, just using 1 bit mode without DMA. Checked wiring and hardware, somewhere in the generated CubeMX code there is a problem, when mounting the device.

@djhtml​ I saw a few posts but was removed when migrated to new ST site, will give link to reddit "solution" eventhough I still can't seem to make head/tail with it and solve my FR_NOT_READY/FR_DISK_ERR problem when calling f_mount(&SDFATfs, "", 1) - also some people think they solve it when replacing "1" with "0", but that only delays the fail up until you try to f_open(&MyFile, "Hello.txt", FA_CREATE_ALWAYS | FA_WRITE).

@Community member​ or @Community member​ if you maybe have any suggestions/examples I would appreciate it very much, will even reference you in my Final year Engineering project if it helps :)

MWATS
Visitor II
October 10, 2018

Check SD present in generated code, my hardware goes high when present but generated code has the opposite.

DMA-- set NVIC and code generation for both DMA channels and the SDIO general interrupt.

http://mkwsolutions.com/Blog/STM32F4_SDIO

JCorn.2
Associate III
July 24, 2019

I had the same problem, 1 bit mode was working fine, 4 bit mode wasn´t.

In the end i got it working by setting the SDIO clock divide factor to 3 and set the clock configuration as the following:

0690X000009Z3W2QAK.png

Tesla DeLorean
Guru
July 24, 2019

If you have overly aggressive slew-rate settings on the GPIO pins you might want to back them off.

You might consider series resistors.

The SDIO is sensitive to bit skewing between the pins, make sure the trace lengths are equivalent.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JCorn.2
Associate III
July 25, 2019

hmm, i just found out that 2GB SD card is working in 4 bit mode, 4GB or even higher isn´t. Any ideas how to solve that?

DOsbo
Associate III
July 25, 2019

Hi Guys,

My experience with SDIO is probably not relevant to your micros, but it might help anyone using the STM32H7 evaluation board. My problems revolved around the DMA transfer to the SD card. I was constantly getting DMA Tx Underruns. That forced me to slow the SD Clock to less than 20MHz. Even though that ridiculously slow speed avoided the underruns, I noticed the DMA was just transferring zeros instead of real data. That issue manifested as a FR_NO_FILESYSTEM because it failed to write the boot sector in the previous step.

The solution was to move the DMA buffers to the internal SRAM section (located at 0x24000000 on STM32H7) and disable the cache for that region. The buffers include the FATFS structure passed to f_mount() and the buffer passed to f_mkfs(). I suspect the buffer passed to f_write() and f_read() should also be located there if they are large. Once I made that change I was able to increase the SD clock to a more respectable 100MHz.

I still don't know why the other RAMs (DTCMRAM, RAM_D2, RAM_D3 and external SDRAM) didn't work with the DMA. It's probably something to do with the infinitely complex internal bus structure.

Here's one for the gurus: Why does the BSD SD example not call HAL_SD_ConfigSpeedBusOperation(), but it calls HAL_SD_ConfigWideBusOperation() twice? Methinks something is not quite right with HAL_SD_Init(), but who cares - it works (usually).

Cheers,

David