cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411RE stm32cube fatfs sdio sdcard always returns FR_DISK_ERR

Jakub Lasinski
Associate II
Posted on March 03, 2018 at 14:37

Hello.

I've tried to test sdcard with fatfs on STM32F411RE on NUCLEO-F411RE board.

I don't have a compleat 100% sure hardware or software, so I'm struggling to debug if its a software or hardware issue.

I've read trough some topics hear, one with issue with sd initialization (but it seams that in my version this issue is already resolved).

I've tripple cheked all the connections and when the card is not in the socket I get fast resposne.

I've generated the code from stm32cubemx, and added a custom code:

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_SDIO_SD_Init();

  MX_FATFS_Init();

  /* USER CODE BEGIN 2 */

  FRESULT res;

  FATFS SDFatFs;  /* File system object for SD disk logical drive */

  FIL MyFile;     /* File object */

  res =  f_mount(&SDFatFs, (TCHAR const*)SDPath, 0);

  if (res != FR_OK)

      while(1);

  res=  f_open(&MyFile, ''STM32.TXT'', FA_CREATE_ALWAYS | FA_WRITE);

  if (res != FR_OK)

        while(1);

  /* USER CODE END 2 */

f_mount is ok, but f_open after around 30s gives back an FR_DISK_ERR . When there is no card in the socket I'm getting fast FR_NOT_READY return.

When exacuting f_open the funcion stucks on line 3050 from ff.c  

fmt = check_fs(fs, bsect);            /* Load sector 0 and check if it is an FAT-VBR as SFD */

It check_fs returns 4 code back and then f_open returns with FR_DISK_ERR

The only thing I did is change some SDIO pins to other options, and setup the clock. I'v tried with SDIO divider up to 96 and it didn't resolve my issue.

Am i forgetting about something? I know that the socket is on jumper wires, but at 250khz clock it should work... Of course tried few cards, tried to use f_mkfs to format card....

I'm software I'm using:

Attolic True studio v9.0

STM32Cube FW_F4 V1.19.0

FatFs R0.12c

STM32Cube 4.240690X00000604RFQAY.jpg0690X00000609tJQAQ.png0690X00000609m9QAA.png0690X00000609tOQAQ.png0690X00000609tTQAQ.png

#fatfs-sdio #sdio #stm32f411re #nucleo-f411re #fatfs-r0.12c #nucleo-f411 #stm32f4
1 ACCEPTED SOLUTION

Accepted Solutions
Jakub Lasinski
Associate II
Posted on March 11, 2018 at 13:00

OK, I've resolved the issue.

The reason it not working was that I've didn't setup the DMA controller to work, and the standard SDIO communication works trough DMA.

Solution is for:

Attolic True studio v9.0

STM32Cube FW_F4 V1.0

FatFs R0.12c

STM32Cube 4.24

You have to manually in STM32Cube:

1. Add a DMA Stream for RX and TX

0690X0000060A5PQAU.png

2. Add IRQ for SDIO (not sure if its requierd)

0690X0000060A2mQAE.png

3. Optionally enable internally pull-ups on all lines except sdio_clk. The card works with only internal pull-ups without any problems at almost full speeds.

0690X00000609tjQAA.png

In you're code at the bottom of sd_diskio.c add:

0690X0000060A2QQAU.png

Those are proper names for week functions for DMA completed callbacks.

And card started to work. I've also tried to use a pooling mode (disable DMA in FatFs configuration in stm32cube), but that way only reading from the card worked, and writing resulted in some fatal errors I've didn't care to debug. Default is that DMA is enabled. You can switch it here:

0690X00000609zxQAA.png

My test code in main looks like this:

0690X00000609mxQAA.png

I've attached my 100% working project. You can get wiring from the first post, I've used an SD to MicroSD adapter as a socket. Works like a charm.

________________

Attachments :

sd_diskio.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hxim&d=%2Fa%2F0X0000000b24%2FCfrBCww.zTuYDZafHYoigwgocFRfOaruTl6_w0ZT9N4&asPdf=false

main.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hxt1&d=%2Fa%2F0X0000000b22%2F9.FrdqHYC556x8JTZti_xO1.VCvM97dRaDk3CTNpWhM&asPdf=false

wiring.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxuD&d=%2Fa%2F0X0000000b21%2FDjAlqwnWO2VeZOkx2C4QUJ71sNlLdQeIoCjxBQnSRCA&asPdf=false

sdiodma.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxuI&d=%2Fa%2F0X0000000b25%2F.t4D8Xaq6h0rLIMciJeL54GVhIX50YGYB7cp_OSz24I&asPdf=false

View solution in original post

16 REPLIES 16
waseem shahzad
Associate
Posted on March 05, 2018 at 16:01

Hi!

     I faced a similar problem when working with an F407 board. Initially i thought that there was something wrong with the code generation but i was wrong. I solved the problem once i slowed down the clocks after reading this in the manual.

0690X00000609vyQAA.png

I think you should decrease the SDIOCLK to around 25MHz (16 MHz worked for me) and then give it a try. 

Hope this helps.

Jakub Lasinski
Associate II
Posted on March 11, 2018 at 13:00

OK, I've resolved the issue.

The reason it not working was that I've didn't setup the DMA controller to work, and the standard SDIO communication works trough DMA.

Solution is for:

Attolic True studio v9.0

STM32Cube FW_F4 V1.0

FatFs R0.12c

STM32Cube 4.24

You have to manually in STM32Cube:

1. Add a DMA Stream for RX and TX

0690X0000060A5PQAU.png

2. Add IRQ for SDIO (not sure if its requierd)

0690X0000060A2mQAE.png

3. Optionally enable internally pull-ups on all lines except sdio_clk. The card works with only internal pull-ups without any problems at almost full speeds.

0690X00000609tjQAA.png

In you're code at the bottom of sd_diskio.c add:

0690X0000060A2QQAU.png

Those are proper names for week functions for DMA completed callbacks.

And card started to work. I've also tried to use a pooling mode (disable DMA in FatFs configuration in stm32cube), but that way only reading from the card worked, and writing resulted in some fatal errors I've didn't care to debug. Default is that DMA is enabled. You can switch it here:

0690X00000609zxQAA.png

My test code in main looks like this:

0690X00000609mxQAA.png

I've attached my 100% working project. You can get wiring from the first post, I've used an SD to MicroSD adapter as a socket. Works like a charm.

________________

Attachments :

sd_diskio.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hxim&d=%2Fa%2F0X0000000b24%2FCfrBCww.zTuYDZafHYoigwgocFRfOaruTl6_w0ZT9N4&asPdf=false

main.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hxt1&d=%2Fa%2F0X0000000b22%2F9.FrdqHYC556x8JTZti_xO1.VCvM97dRaDk3CTNpWhM&asPdf=false

wiring.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxuD&d=%2Fa%2F0X0000000b21%2FDjAlqwnWO2VeZOkx2C4QUJ71sNlLdQeIoCjxBQnSRCA&asPdf=false

sdiodma.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxuI&d=%2Fa%2F0X0000000b25%2F.t4D8Xaq6h0rLIMciJeL54GVhIX50YGYB7cp_OSz24I&asPdf=false
Posted on March 11, 2018 at 12:19

Thank You, but as I've mentioned I've tried to lower the clock of SDIO. Tried values to 250kHz. But I've resolved the issue, I'll post the solution within few minutes.

@Jakub: Thanks for not only asking a good question, but also being so nice to post the solution.

Your screenshots helped me compare the differences between my configuration and a working configuration, and with that I got it to work. Using DMA was the key :)

ooztu.1139
Associate II

Thanks! Jakub Lasinski 

I just change pins to pull-up and all my problems solved.

unfortunately it doesn't work on STM32Cube_FW_F4_V1.25.2 and standard example STM324xG_EVAL\Applications\FatFs\FatFs_uSD\Src (2017 year) is without stm32cubeide support too...

Mr ZEE
Associate II

Very helpful post. Thanks alot.

unfortunately for stm32cubeide 1.5.0 this solution doesn't work. And FatFS via CubeMX on STM32Cube_FW_F4_V1.25.2 too. But very old example STM324xG_EVAL\Applications\FatFs\FatFs_uSD\Src from 2017 year works via Keil.

I have cube 1.5.0 and it works perfectly