cancel
Showing results for 
Search instead for 
Did you mean: 

f_mount fails using STM32L4 w/ SDMMC

Forest L
Associate III
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
16 REPLIES 16
Forest L
Associate III

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.

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

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 

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

Sorry, where/how would I 'dump' the block contents?

By the way, I'm attempting to use DMA, and realize now that there are two interrupts attached to the SDMMC interface -- can this cause problems if they're not correctly configured and are maybe overlapping?

void SDMMC1_IRQHandler(void)

void DMA2_Channel4_IRQHandler(void)

You'd instrument the disk_read and/or disk_write functions to call dumping routines that show you what's in the memory buffer.

0690X0000060QuCQAU.jpg

STM32Cube_FW_L4_V1.11.0\Projects\STM32L476G-EVAL\Applications\FatFs\FatFs_uSD_DMA_RTOS\Src\stm32l4xx_it.c

void SDMMC1_IRQHandler(void)
{
 HAL_SD_IRQHandler(&hsd_eval);
}
 
void DMA2_Channel4_IRQHandler(void)
{
  if((hsd_eval.Context == (SD_CONTEXT_DMA | SD_CONTEXT_READ_SINGLE_BLOCK)) ||
     (hsd_eval.Context == (SD_CONTEXT_DMA | SD_CONTEXT_READ_MULTIPLE_BLOCK)))
  {
    BSP_SD_DMA_Rx_IRQHandler();
  }
  else if((hsd_eval.Context == (SD_CONTEXT_DMA | SD_CONTEXT_WRITE_SINGLE_BLOCK)) ||
         (hsd_eval.Context == (SD_CONTEXT_DMA | SD_CONTEXT_WRITE_MULTIPLE_BLOCK)))
 {
   BSP_SD_DMA_Tx_IRQHandler();
 }
}

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

Sorry if I'm just being an idiot. Looking at this and related problems has fried my brain.

I'm trying to print the buffer like this inside f_write():

  	  	while (*buff){
  	  		msglen = snprintf(msgBuf, 255, "%02X/%c ",(unsigned int) *buff,(char) *buff);
  	  		buff++;
			serialDisplay(msgBuf,msglen);
  	  	}

for any write occurrence whether or not it fails. This just gives me the same value out, 'B'. What am I missing?