Skip to main content
antonius
Associate III
February 20, 2017
Question

STM32CubeMX, FatFS and SDIO?

  • February 20, 2017
  • 54 replies
  • 12425 views
Posted on February 20, 2017 at 22:57

Everyone,

How can I use cubeMX for SDcard ? I have generated for MDK but :

this function doesn't work , from UM1721

User manual

Developing Applications on STM32Cube with FatFs page 21.

:

uint32_t wbytes; /* File write counts */

uint8_t wtext[] = 'text to write logical disk'; /* File write buffer */

if(FATFS_LinkDriver(&mynewdisk_Driver, mynewdiskPath) == 0)

{

if(f_mount(&mynewdiskFatFs, (TCHAR const*)mynewdiskPath, 0) == FR_OK)

{

if(f_open(&MyFile, 'STM32.TXT', FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

{

if(f_write(&MyFile, wtext, sizeof(wtext), (void *)&wbytes) == FR_OK);

{

f_close(&MyFile);

}

}

}

}

FATFS_UnLinkDriver(mynewdiskPath);

This variable :

&mynewdisk_Driver doesn't exist, which file is related ?

Thank you

    This topic has been closed for replies.

    54 replies

    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 11:18

    Yes I tried analyzing myself, but it seems that STM32CubeMX is not straighfoward, and,

    Some question,

    1. After I initialized SDIO with SDcard option, is it initialized already ?

    2. I saw FATFs init as well on the code ? so if it's already initialized, SD card must be initialized, otherwise FATFs can not be initialized, I got until f_mount but can not do f_open ?

    Confusing me...it would be nice if I used SDIO and FatFS in straightforward.

    Thanks

    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 13:11

    what's this function for ??

    MX_SDIO_SD_Init(); ??

    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 13:37

    I tried to follow the step on UM1718 page 193, but I think it's not working properly.. ??

    Does it work at all ? Has it it been tested on Eval board from ST ? or just an abstract and confusing ST end user like me ?

    Tesla DeLorean
    Guru
    February 28, 2017
    Posted on February 28, 2017 at 17:07

    >>I tried to follow the step on UM1718 page 193, but I think it's not working properly.. ??

    Top level functionality of FatFs is predicated on the correct functionality of the block read/write layer below. If that can't perform a basic job of returning blocks previously written accurately and repeatedly then anything built on top is going to fail in some form or fashion. The manual describes FatFs usage on a board you aren't using, and is dependent on drivers below to be functioning correctly.

    >>Does it work at all ? Has it it been tested on Eval board from ST ? or just an abstract and confusing ST end user like me ?

    You are wasting your time debugging the FatFs level, this isn't the root of your problem, and doesn't have any chronic bugs. The confusion is mostly of your own making, validate the SDIO layer FIRST, the clocking and connectivity is important, but it is CRITICAL that blocks are read/written correctly, to the correct spot on the media, and with 100% reliability. It is like delivering mail, if you keep delivering to the wrong houses in the wrong streets, you are going to get fired. Or a taxi driver who always gets lost and has no sense of direction.

    Do you have an ST EVAL board?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 22:31

    1.I'm currently using STM32F103VET6 and it's my own board, so I need to trace the SDIO timing and connection ?

    Any ideas how ?

    I have USART1 as diagnostic

    2. I have STM32F4 discovery and STM32F429 discovery board, will they help ? but I wanna make it work on STM32F103, because I have connected SDCARD module on it and tested with old STM32F103 library and it worked ok.

    Making me confused, it's working with old code but not working with STM32CubeMX ??

    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 15:52

    I diagnosed until ff.c :

    FRESULT f_open (

        FIL* fp,            /* Pointer to the blank file object */

        const TCHAR* path,    /* Pointer to the file name */

        BYTE mode            /* Access mode and file open mode flags */

    )

    {

        FRESULT res;

        DIR dj;

        BYTE *dir;

        DEFINE_NAMEBUF;

        printf('f_open function Line 2533 ff.c! \n'); ===> stop here for now

    #if !_FS_READONLY

        DWORD dw, cl;

    #endif
    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 22:36

    If this board doesn't work, it will not working with my old code for SDIO and USB mass storage,

    It's working now with 16GB SDcard and USB as mass storage I can read and write file correctly...

    I don't understand at all, probably I put a wrong speed for SDIO ?? any clues ? really confusing me..

    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 22:38

    0690X00000603hWQAQ.jpg

    Probably I put this clock too fast ? 0 = 24 MHz ??

    antonius
    antoniusAuthor
    Associate III
    February 28, 2017
    Posted on February 28, 2017 at 22:40

    This is from my Old code :

    ♯ define SDIO_TRANSFER_CLK_DIV            ((uint8_t)0x3)

    ♯ define SD_DETECT_PIN                    GPIO_Pin_14                 /* PB.14 */

    ♯ define SD_DETECT_GPIO_PORT              GPIOB                       /* GPIOB */

    ♯ define SD_DETECT_GPIO_CLK               RCC_APB2Periph_GPIOB

    /**

      * @brief  SDIO Intialization Frequency (400KHz max)

      */

    ♯ define SDIO_INIT_CLK_DIV                ((uint8_t)0xB2)

    ♯ define SDIO_FIFO_ADDRESS                ((uint32_t)0x40018080)

    Is there anything different with STM32CubeMX, where can I find this setting on STM32CubeMX generated code ? thanks Clive..

    rwmao
    Senior
    March 1, 2017
    Posted on March 01, 2017 at 01:25

    h.Rick

    Clive is right! please follow his advise.

    You have to start from the low level sdio testing. Forget about whatever ff, fmount, fopen etc.

    YOu can try to use this to test.

    int main(void)
    {
     char usbbuffer[500];
     uint8_t SD_state = MSD_OK;
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */
    /* MCU Configuration----------------------------------------------------------*/
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
     HAL_Init();
    /* Configure the system clock */
     SystemClock_Config();
    /* Initialize all configured peripherals */
     MX_GPIO_Init();
     MX_DMA_Init();
     MX_SDIO_SD_Init();
    
    /* USER CODE BEGIN 2 */
    
     /* USER CODE BEGIN 2 */
    SD_Test_LowLevel(); 
    while(1){;}
     }

    SD_Test_LowLevel() is defined as:

    the function sendtextmsgtousb is for debugging only. you don't need that.

    Error_Handler() is showing error by LED and halt system.

    void SD_Test_LowLevel(void)
    { 
     uint8_t SD_state = MSD_OK;
     static uint8_t prev_status = 0; 
     char usbbuffer[200];
    SD_state = BSP_SD_Init();
     
     if(SD_state != MSD_OK)
     {
     sprintf(usbbuffer,'
    ---initialize failed ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     Error_Handler(); 
     }
    //initialization is done. Now go ahead for SD operation in low level
     
     
     SD_state = BSP_SD_Erase(BLOCK_START_ADDR, (BLOCKSIZE * NUM_OF_BLOCKS));
     
     if(SD_state != MSD_OK)
     {
     sprintf(usbbuffer,'
    ---erase failed ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     Error_Handler();
     }
     
     /* Fill the buffer to write */
     Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
     SD_state = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS);
     
     if(SD_state != MSD_OK)
     {
     sprintf(usbbuffer,'
    ---writing failed ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     Error_Handler();
     }
    SD_state = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS);
     if(SD_state != MSD_OK)
     {
     sprintf(usbbuffer,'
    ---reading failed ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     Error_Handler();
     }
     else
     {
     sprintf(usbbuffer,'
    ---Reading OK ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0)
     {
     sprintf(usbbuffer,'
    ---reading/writing compare failed ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     Error_Handler(); 
     }
     else
     {
     sprintf(usbbuffer,'
    ---Testing is done, OK ---
    ');//export it to char buffer first. 
     SendTextMsgToUSB(usbbuffer); //send the txt back 
     }
     }
     
     
     
     
     GREEN_LED_ON;//Good
     
     
     while (1)
     {
    }
    }

    Helpful

    https://community.st.com/message/149707?commentID=149707

    https://community.st.com/message/149707?commentID=149707

    Like

    Show 0 Likes

    https://community.st.com/message/149707?commentID=149707

    antonius
    antoniusAuthor
    Associate III
    March 1, 2017
    Posted on March 01, 2017 at 11:37

    Can it be because my Init SDIO clock too fast ? Why can't I read this message properly ? Always got unexpected error ? server problem here ?

    antonius
    antoniusAuthor
    Associate III
    March 1, 2017
    Posted on March 01, 2017 at 11:46

    I reduce the SDIO clock, and it can write properly already, thanks a lot guys

    antonius
    antoniusAuthor
    Associate III
    March 1, 2017
    Posted on March 01, 2017 at 12:25

    Where  can I fidn SDIO_CK variable in the code ?

    antonius
    antoniusAuthor
    Associate III
    March 1, 2017
    Posted on March 01, 2017 at 22:14

    I changed this configuration in this file : stm32f1xx_II_sdmmc.h

    And now it's working properly

    /* SDIO Intialization Frequency (400KHz max) */

    #define SDIO_INIT_CLK_DIV ((uint8_t)0xC3)

    /* SDIO Data Transfer Frequency */

    #define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x3)

    Seton Schroeder
    Visitor II
    April 26, 2017
    Posted on April 26, 2017 at 22:27

    Hi h.rick,

    Could you provide your setting on each tab on CubeMX, including the 'FATFS' and 'SDIO' windows under the 'Configuration' tab?

    Did you use these setting for allocated memory: CSTACK: 0x800 & HEAP: 0x400?

    Did you connect an SD breakout board with 4-pin SDIO mode directly to your STM32F103 board, or do you need level shifters in between?

    I'm trying to do the same thing with my Nucleo-STM32F401RE board with connected SD breakout board. That eval board has no external crystal, so my clock setting have to be from the internal clock.

    Thanks.

    Tesla DeLorean
    Guru
    April 27, 2017
    Posted on April 27, 2017 at 00:46

    You don't need level converters, I would however recommend very short connections and pull-up resistors.

    The Nucleo has an 8 MHz HSE clock provided by the ST-LINK.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Seton Schroeder
    Visitor II
    April 26, 2017
    Posted on April 27, 2017 at 01:43

    Thanks. After posting, I actually re-read the Nucleo user manual realized, yes, by default it connects to the STLINK 8MHz external crystal. So I made the changes and when I build I still get these issues:

    Warnings:

    - implicit declaration of function 'strcmp'

    - passing arguments 4 of 'f_read' from incompatible pointer type

    Infos:

    - expected 'UINT *{aka unsigned int *}' but argument is of type 'unint32_t *{aka long unsigned int *}'

    Still, I read nothing to the SD card using the Example you pointed out in the STM32CubeMX manual. 

    I'll try adding the pull-up resistors and try again.

    Tesla DeLorean
    Guru
    April 27, 2017
    Posted on April 27, 2017 at 02:32

    You should be able to enable pull-ups on the pins internally.

    The FatFs code will not work if the underlying SDIO code isn't functional.

    What pin is being used on the socket's card detect?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    April 27, 2017
    Posted on April 27, 2017 at 02:34

    That's right, connect SD Detect as well, it's inside the code PB14 if

    I'm not wrong..

    cory
    Visitor II
    May 26, 2017
    Posted on May 26, 2017 at 21:33

    Hey guys, I think the issue is that Cube is generating sdio.c init code that has the hardware flow control option set to off. This is different than the examples provided in the cube driver and example distro, which has flow control enabled:

    hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE;

    Lowering the clock speed does appear to work also, because flow control is not needed when you get low enough.

    John Craven
    Senior
    August 15, 2017
    Posted on August 15, 2017 at 15:47

    I have SDIO 4bit FatFS working, but only with delays between block writes.

    https://community.st.com/0D50X00009XkYXYSA3

    I foundyour topic. MyCUBE code also had;

    hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;�?�?�?�?�?

    I thought it was worth enabling the flow control, to see if it would allow getting rid of my delays.

    With it enabled, my app fails with and without thewithout the delays that i am trying to eliminate.

    In my case, it appearsto make things worse.

    Tesla DeLorean
    Guru
    July 12, 2018
    Posted on July 12, 2018 at 11:35

    Going to note that the F2 part I'm working on today doesn't function properly with flow control enabled, just locks up on writes. Looks to be an errata issue. Reads were working. Just wrote ~15GB with it disabled.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..