Showing results for 
Search instead for 
Did you mean: 

SDIO 4 bit mode with STM32CubeMX

Associate II

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: 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)
	// ...
	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_open(&myFile, "test.txt", FA_READ);
		f_read(&myFile, myRead, 5, &byteCount);
		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!


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 Up vote any posts that you find helpful, it shows what's working..
Peter Mather
Associate III

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

Associate II

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.

Associate II

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?

Associate II

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

Associate II

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
Associate II

@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 🙂

Associate II

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.

Associate II

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: