Skip to main content
Forest L
Associate II
July 13, 2018
Question

f_mount fails using STM32L4 w/ SDMMC

  • July 13, 2018
  • 13 replies
  • 4133 views
Posted on July 13, 2018 at 16:44

I'm having a lot of difficulty getting FatFS working with SDMMC on my STM32L476 (RG-Nucleo) board. I'm working from code generated by CubeMX and have followed a number of forum posts suggesting alterations to get SDMMC working -- for instance, 

https://community.st.com/message/203639-sdmmc-on-stm32-l476-l4

 . Currently I'm getting an infinite loop when calling f_mount which I can trace back to

stm32l4xx_hal_sd.c:

while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))

{

This loop goes on infinitely -- it seems none of these flags are being set? What are some likely causes of this problem?

#hal #sdmmc #f_mount #stm32l4+ #fatfs
This topic has been closed for replies.

13 replies

Tesla DeLorean
Guru
July 13, 2018
Posted on July 13, 2018 at 18:03

>> I'm working from code generated by CubeMX..

You need to stop doing that, the monkey inside is broken.

0690X0000060MAtQAM.jpg

I'll rebuild the demo in a minute.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Forest L
Forest LAuthor
Associate II
July 13, 2018
Posted on July 13, 2018 at 20:27

Thanks for the picture -- it seems our wiring is identical. 

Sadly I have 4-5 other peripherals that need configuration for my project so Cube seems like the path of least resistance. 

:(

I've tried adapting ST's non-Cube HAL example (for L476G, which has an onboard SD slot), but maybe I ought to try harder. So far, however, it seems I just can't get it working even without Cube.

If it's of note, I believe the one 'good' flag above is FLAG_DBCKEND, meaning a data block was received and the corresponding CRC passed. It hangs here until the timeout is reached, returning HAL_SD_ERROR_TIMEOUT. It's not failing the CRC either, so the data just isn't being received.

electronic tobi
Associate
July 16, 2018
electronic tobi
Associate
July 26, 2018

before stm locked the forum you wrote that it doesn't work and you get not_ready

this is because stm is inconsistent with the bsp files, in some files the bsp check if card detect is high, in some files it check if card detect is low.

So when you get not ready, you must change the line somewhere in sd init

Forest L
Forest LAuthor
Associate II
July 30, 2018

Oh, I didn't realize this post got re-uploaded yet. Thanks, I'll try that.

EDIT: I can confirm you're on to something. For future readers, the issue is in bsp_driver_sd.c and bsp_driver_sd.h. I believe I already corrected the definitions to be consistent. The error comes where I'm checking the card detect pin -- it always reads as disconnected. I'll try tying this to Vcc through a pullup resistor... Can anyone confirm if this is right or if I'm insane?

EDIT 2: I tried the above and it didn't work -- I then just made BSP_SD_IsDetected() always return SD_PRESENT for testing purposes, and at least I get a different error. Now I'm getting FR_DISK_ERR.

This seems to follow from

 while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))

in sd_diskio.c. Basically, ReadStatus is never set to 1 so we get a timeout here. Any advice?

EDIT 3: It seems the Cube generated code in sd_diskio.c didn't set a read or write bit for the functions

void BSP_SD_WriteCpltCallback(void)

and

void BSP_SD_ReadCpltCallback(void).

I changed this to set each appropriate bit to 1, and f_mount passes. I'm going to add in the other file operations and see if this fixed everything.

Forest L
Forest LAuthor
Associate II
July 30, 2018

So, now it's failing on f_write, which returns FR_DISK_ERR. This seems to go back to Middlewares/Third_Party/FatFs/src/ff.c: 1410.

(here line 1= line 1400 in the file.)

Here, cs = 0xFFFFFFFF, which means....

"an error occured" ?

Any advice?

		ncl = scl;	/* Start cluster */
		for (;;) {
			ncl++;							/* Next cluster */
			if (ncl >= fs->n_fatent) {		/* Check wrap-around */
				ncl = 2;
				if (ncl > scl) return 0;	/* No free cluster */
			}
			cs = get_fat(obj, ncl);			/* Get the cluster status */
			if (cs == 0) break;				/* Found a free cluster */
			if (cs == 1 || cs == 0xFFFFFFFF) return cs;	/* An error occurred */
			if (ncl == scl) return 0;		/* No free cluster */
		}

Tesla DeLorean
Guru
July 30, 2018

>>Any advice?

Go down toward the failure, not up away from it.

The HAL SDMMC is called via the DISKIO layer. Instrument there to understand the nature of the error before it propagates up in a pass/fail type FR_DISK_ERR, which tells you nothing.

In disk_write() add instrumentation to exit path

 if (res != RES_OK)

   printf("W %5d %2d (%08X)\n", sector, count, uSdHandle.ErrorCode);

Then look a bit/error flags for HAL SDMMC, likely a TIMEOUT or OVERRUN

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Forest L
Forest LAuthor
Associate II
July 30, 2018

Here, uSdHandle is the relevant SD_HandleTypeDef, correct? It's not visible in the disk_write() function (using the right name for my project). I know there are some easy fixes, but I want to double-check here to make sure I'm not barking up the wrong tree. Did you have to add or modify anything here to get the error code working?

I do have a print statement displaying the error code in my main loop, but it indicates no error so I'm assuming it gets reset some time in between.

Tesla DeLorean
Guru
July 30, 2018

extern SD_HandleTypeDef uSdHandle;

Depends on where the error is coming from. Instrumenting the failure paths of disk_read/disk_write is a start. You might also want to look at disk_status.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Forest L
Forest LAuthor
Associate II
July 31, 2018

I added similar debug statements to each function and got the following output from disk_write( ) (see below):

Among other things, I'm probably not referencing the SD_HandleTypeDef right. I declare it extern in a header file which is included in multiple source files, diskio.c included. This is because I figure we're referencing the same struct across these source files.

Another thing I'm not too sure about is the value of that array "buff". I'm trying to write the string "This is a test\r\n", so where does a B come from? I think I'm misunderstanding something...

Thanks again for the help so far everyone.

[WRITE FAILURE]
sector 8672
count 1
errorCode 0x00000000 (HAL_SD_ERROR_NONE)
pdrv 0
buff B 

Tesla DeLorean
Guru
July 31, 2018

It is a file system on top of a block storage system. There are partition tables, boot sectors, directories, and block tables associated with writing your text to a file.

You should dump the 512 bytes in the block to better understand what is in this one. My guess would be your test string is in a preceding block that was written.

READ 0 1
00000000 : EB 58 90 4D 53 44 4F 53-35 2E 30 00 02 08 B8 0B .X.MSDOS5.0.....
00000010 : 02 00 00 00 00 F8 00 00-3F 00 FF 00 00 00 00 00 ........?.......
00000020 : 80 0E E9 00 24 3A 00 00-00 00 00 00 02 00 00 00 ....$:..........
00000030 : 01 00 06 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000040 : 80 00 29 49 1C 8B 7E 4E-4F 20 4E 41 4D 45 20 20 ..)I..~NO NAME 
00000050 : 20 20 46 41 54 33 32 20-20 20 33 C9 8E D1 BC F4 FAT32 3.....
00000060 : 7B 8E C1 8E D9 BD 00 7C-88 56 40 88 4E 02 8A 56 {......|.V@.N..V
00000070 : 40 B4 41 BB AA 55 CD 13-72 10 81 FB 55 AA 75 0A @.A..U..r...U.u.
00000080 : F6 C1 01 74 05 FE 46 02-EB 2D 8A 56 40 B4 08 CD ...t..F..-.V@...
00000090 : 13 73 05 B9 FF FF 8A F1-66 0F B6 C6 40 66 0F B6 .s......f...@f..
000000A0 : D1 80 E2 3F F7 E2 86 CD-C0 ED 06 41 66 0F B7 C9 ...?.......Af...
000000B0 : 66 F7 E1 66 89 46 F8 83-7E 16 00 75 39 83 7E 2A f..f.F..~..u9.~*
000000C0 : 00 77 33 66 8B 46 1C 66-83 C0 0C BB 00 80 B9 01 .w3f.F.f........
000000D0 : 00 E8 2C 00 E9 A8 03 A1-F8 7D 80 C4 7C 8B F0 AC ..,......}..|...
000000E0 : 84 C0 74 17 3C FF 74 09-B4 0E BB 07 00 CD 10 EB ..t.<.t.........
000000F0 : EE A1 FA 7D EB E4 A1 7D-80 EB DF 98 CD 16 CD 19 ...}...}........
00000100 : 66 60 80 7E 02 00 0F 84-20 00 66 6A 00 66 50 06 f`.~.... .fj.fP.
00000110 : 53 66 68 10 00 01 00 B4-42 8A 56 40 8B F4 CD 13 Sfh.....B.V@....
00000120 : 66 58 66 58 66 58 66 58-EB 33 66 3B 46 F8 72 03 fXfXfXfX.3f;F.r.
00000130 : F9 EB 2A 66 33 D2 66 0F-B7 4E 18 66 F7 F1 FE C2 ..*f3.f..N.f....
00000140 : 8A CA 66 8B D0 66 C1 EA-10 F7 76 1A 86 D6 8A 56 ..f..f....v....V
00000150 : 40 8A E8 C0 E4 06 0A CC-B8 01 02 CD 13 66 61 0F @............fa.
00000160 : 82 74 FF 81 C3 00 02 66-40 49 75 94 C3 42 4F 4F .t.....f@Iu..BOO
00000170 : 54 4D 47 52 20 20 20 20-00 00 00 00 00 00 00 00 TMGR ........
00000180 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000190 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001A0 : 00 00 00 00 00 00 00 00-00 00 00 00 0D 0A 44 69 ..............Di
000001B0 : 73 6B 20 65 72 72 6F 72-FF 0D 0A 50 72 65 73 73 sk error...Press
000001C0 : 20 61 6E 79 20 6B 65 79-20 74 6F 20 72 65 73 74 any key to rest
000001D0 : 61 72 74 0D 0A 00 00 00-00 00 00 00 00 00 00 00 art.............
000001E0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001F0 : 00 00 00 00 00 00 00 00-AC 01 B9 01 00 00 55 AA ..............U.
READ 1 1
00000000 : 52 52 61 41 00 00 00 00-00 00 00 00 00 00 00 00 RRaA............
00000010 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000020 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000030 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000040 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000050 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000060 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000070 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000080 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000090 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000000A0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000000B0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000000C0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000000D0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000000E0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000000F0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000100 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000110 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000120 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000130 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000140 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000150 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000160 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000170 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000180 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000190 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001A0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001B0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001C0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001D0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
000001E0 : 00 00 00 00 72 72 41 61-8D 68 1A 00 46 A9 02 00 ....rrAa.h..F...
000001F0 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 55 AA ..............U.

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