cancel
Showing results for 
Search instead for 
Did you mean: 

SDIO (SDMMC) and FatFS with CubeMX

dennis23
Associate II
Posted on May 12, 2016 at 13:55

Hello!

I try to use the SDMMC1 interface together with FatFS. Initialization is created with CubeMX.

My init is something like this:

...

MX_LTDC_Init();

MX_SDMMC1_SD_Init();

MX_FATFS_Init();

...

Then I try to use the SD card. The CardInfo is right, I get data from. But with the FatFS I got some problems. Initialization and f_mount is no problem but then I get uSD: FatFs format Error with the Error code 3 -> NOT_READY

UG_ConsoleSetForecolor(C_YELLOW);

UG_ConsolePutString(''System start uSD debugging...!\n'');

if(HAL_SD_Get_CardInfo(&hsd1, &SDCardInfo1) != SD_OK)

{

    UG_ConsoleSetForecolor(C_RED);

    UG_ConsolePutString(''uSD: HAL_SD_Get_CardInfo != SD_OK!\n'');

    Error_Handler();

}

// Put infos about SD card

UG_ConsoleSetForecolor(C_WHITE);

sprintf(itoaBuffer, ''SD capacity: %d\n'',SDCardInfo1.CardCapacity);

UG_ConsolePutString(itoaBuffer);

sprintf(itoaBuffer, ''SD block size: %d\n'',SDCardInfo1.CardBlockSize);

UG_ConsolePutString(itoaBuffer);

sprintf(itoaBuffer, ''SD type: %d\n'',SDCardInfo1.CardType);

UG_ConsolePutString(itoaBuffer);

/*♯♯-2- Register the file system object to the FatFs module ♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

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

if(tempRes != FR_OK)

{

    /* FatFs Initialization Error */

    UG_ConsoleSetForecolor(C_RED);

    sprintf(itoaBuffer, ''uSD: FatFs Initialization Error (%d)\n'', tempRes);

    UG_ConsolePutString(itoaBuffer);

    Error_Handler();

}

else

{

/*♯♯-3- Create a FAT file system (format) on the logical drive ♯♯♯♯♯♯♯♯♯*/

/* WARNING: Formatting the uSD card will delete all content on the device */

tempRes = f_mkfs((TCHAR const*)SDPath, 0, 0);

if(tempRes != FR_OK)

{

    /* FatFs Format Error */

    UG_ConsoleSetForecolor(C_RED);

    sprintf(itoaBuffer, ''uSD: FatFs format Error (%d)\n'', tempRes);

    UG_ConsolePutString(itoaBuffer);

    Error_Handler();

}

Do I have to do something else after the init from Cube? I am a bit in trouble with the FatFS...

Would be great if someone has an answer for me!

#sdio #stm32 #f4 #sdmmc #f7
3 REPLIES 3
Nesrine M_O
Lead II
Posted on May 13, 2016 at 11:55

Hi debegr,

STM32Cube_FW_F4_V1.11.0\Projects\STM324x9I_EVAL\Applications\FatFs\FatFs_uSD

This directory contains example that provide a description on how to use STM32Cube firmware with FatFs middleware component as a generic FAT file system module,

in order to develop an application exploiting FatFs offered features with microSD drive configuration

For more details about FatFs implementation on STM32Cube, please refer to

http://www2.st.com/content/ccc/resource/technical/document/user_manual/61/79/2b/96/c8/b4/48/19/DM00105259.pdf/files/DM00105259.pdf/jcr:content/translations/en.DM00105259.pdf

''Developing Applications on STM32Cube with FatFs''.

-Syrine-

dennis23
Associate II
Posted on May 14, 2016 at 17:32

Hi Syrine,

thank you for your advice. I found exactly this example code for the STM32F7xx and copy it into my code. The if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0) was created from CubeMX init (FatFS_Init function), this is why I deleted the first if case with this line of code.

Now I get the error in this line:

if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK)

I got the error code 3 -> Not ready. Ok there is always a FAT16/32 file system on the two cards tested, I also deleted this function and try to open the file direct. But there I also get the error 3...

I am not sure, but the code in the example is not really the same like the created one from CubeMX. CubeMX calls first a function to set the SDIO (or later on STM32F7xx SDMMC) interface but do not init this, this is done later from the driver layer in FatFS.

Do I miss anything or is there a hidden secret feature I have to enable?! I think with the right settings and the example code I am good to go but it still do not work and in the example code I can't see any initialization of SD or FatFS?!

Posted on September 13, 2017 at 00:59

CubeMX is a nice tool but it lags behind CubeF7. Version 4.22.1 as well.

Learn to switch like I did. You spend more hours in debugging as in 

building a new project without a code generator.