cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 fatfs sdio

er3481
Associate III
Posted on August 18, 2016 at 15:44

Hi

I am trying to use fatfs with my 8gb microsd card on my own stm32f429 board. I use 1-bit sdio configuration and i use the cubemx fatfs example Project for eval board.

if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)

  {

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

    if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)

    {

      /* FatFs Initialization Error */

      //Error_Handler();

    }

else

    {

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

      {

        /* FatFs Format Error */

        //Error_Handler();

      }

      else

      {

        /*♯♯-4- Create and Open a new text file object with write access ♯♯♯♯♯*/

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

        {

          /* 'STM32.TXT' file Open for write Error */

          //Error_Handler();

        }

        else

        {

          /*♯♯-5- Write data to the text file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

          res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

         

          if((byteswritten == 0) || (res != FR_OK))

          {

            /* 'STM32.TXT' file Write or EOF Error */

            //Error_Handler();

          }

          else

          {

            /*♯♯-6- Close the open text file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

            f_close(&MyFile);

           

            /*♯♯-7- Open the text file object with read access ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

            if(f_open(&MyFile, ''STM32.TXT'', FA_READ) != FR_OK)

            {

              /* 'STM32.TXT' file Open for read Error */

              //Error_Handler();

            }

            else

            {

              /*♯♯-8- Read data from the text file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

              res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);

             

              if((bytesread == 0) || (res != FR_OK))

              {

                /* 'STM32.TXT' file Read or EOF Error */

                //Error_Handler();

              }

              else

              {

                /*♯♯-9- Close the open text file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

                f_close(&MyFile);

               

                /*♯♯-10- Compare read data with the expected data ♯♯♯♯♯♯♯♯♯♯♯♯*/

                if((bytesread != byteswritten))

                {               

                  /* Read data is different from the expected data */

                  //Error_Handler();

                }

                else

                {

                  /* Success of the demo: no error occurrence */

                  //BSP_LED_On(LED1);

                }

              }

            }

          }

        }

      }

    }

  }

FATFS_UnLinkDriver(SDPath);

When debugging, it returns from '' if(f_open(&MyFile, ''STM32.TXT'', FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)'' to error handler, so it can not open the file. What may the problem be? Any advise...

#no-hablo-hal
7 REPLIES 7
Posted on August 18, 2016 at 18:20

Any advise...

Most every SD card comes preformatted, don't use MKFS unless there is some compelling case to do so.

Don't debug it at this level. Validate your SDIO layer reading/writing sectors before trying to integrate with FatFS. If that works properly add instrumentation to the DISKIO layer so you can get some perspective on what the problem is.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
er3481
Associate III
Posted on August 18, 2016 at 18:26

Ok, this is my first trying about fatfs and sdcard writing and reading. So, how can i validate my SDIO layer reading/writing sectors ? And how can i add instrumentation to the DISKIO layer? I just want to understand the aim of these.

Posted on August 18, 2016 at 18:46

Ok so FatFs is a top level abstraction, it's not going to give you much insight into what exactly is wrong, just that it works vs doesn't work. You can waste hours at this level and get nowhere, and it is not as if you understand the File System Structures it is navigating. I'd start assuming that FatFs is pretty solid, and your issues are elsewhere.

The storage media has blocks/sectors, these a) have to work without errors, and b) return the same data you wrote to them previously. You need to prove this basic level of functionality is there. This code should be the SDIO Read/Write Block functionality on top of the SDIO peripheral register layer. Start with reading, and once you are sure that works move to writing, ie specific data patterns to specific sectors, reading back confirming as expected.

The DISKIO layer is how FatFs deals with the SDIO block/sector level. You can instrument this by showing what blocks it is attempting to read/write, and what errors or success it is having performing that task. If it gets an error, basically FatFs will pass that back to you and die. If you read the wrong data back successfully, it is going to try and process that and probably crash and die, or provide an error indicating it is unhappy or confused.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
er3481
Associate III
Posted on August 18, 2016 at 18:58

Is allocation unit size and Block size defined in fastfs library the same things? when sd cards are formatted on windows there is allocation unit size and there is a define called BLOCK_SIZE which is 512 in sd_diskio.c .

Posted on August 18, 2016 at 19:10

The most common sector/block size for storage media has been 512 bytes, FAT typically uses several of these to create a cluster, which is the minimum allocation unit for the file system.

You are going to want to access the SDIO via 512 byte blocks. The routines are expected to access one or more sequential blocks.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
er3481
Associate III
Posted on August 19, 2016 at 08:46

Hi,

When i debug for fopen, res returns FR_NOT_ENABLED. What does it mean ? and what is the solve? Any advise..

er3481
Associate III
Posted on August 19, 2016 at 13:40

I have solved it, there was not the VCC connection to sdcard socket. For now i am using the code below:

 // if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0) 

  //{   

    if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 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(SDPath);

It Works when i comment out this line :

//if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0) 

Why this line does not work for me and why the code Works when i comment it ?