cancel
Showing results for 
Search instead for 
Did you mean: 

Problem of writing/reading microsd card using the code generated by stm32f4cubemx

rwmao
Senior
Posted on May 30, 2015 at 02:53

This is is very simple project, using stm32f4 discovery and wired microsd socket to read/write data to microsd card.

the code is generated by the stm32f4cubemx.

However error occurs at f_open, tracking down it shows FS_DISK_ERR in ff.c

Please help..

The code for reading/write is:

  /* Register the file system object to the FatFs module */

  if(f_mount(&SDDISKFatFs, (TCHAR const*)SD_Path, 0) != FR_OK)

  {

    /* FatFs Initialization Error */

    Error_Handler();

  }

  else

  {

LED_ON(ORANGE,1);

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

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

      {

LED_ON(RED,1);

HAL_Delay(2000);

LED_ON(RED,0);HAL_Delay(1000);

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

        Error_Handler();

      }

      else

      {

LED_ON(BLUE,1);

        /* Write data to the text file */

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

        

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

        {

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

          Error_Handler();

        }

        else

        {

LED_ON(GREEN,1);

          /* Close the open text file */

          f_close(&MySdFile);

          

        /* Open the text file object with read access */

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

        {

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

          Error_Handler();

        }

        else

        {

          /* Read data from the text file */

          res = f_read(&MySdFile, rtext, sizeof(rtext), (void *)&bytesread);

          

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

          {

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

            Error_Handler();

          }

          else

          {

            /* Close the open text file */

            f_close(&MySdFile);

            

            /* 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(LED4);

            }

          }

        }

      }

    }

  }

23 REPLIES 23
rwmao
Senior
Posted on June 12, 2015 at 01:10

Hi Clivel

Update of the board.

I got a board  STM32F4DIS-BB from

http://www.newark.com/stmicroelectronics/stm32f4dis-bb/expansion-stm32f4-cortex-m4-for/dp/47W1731

Tested the connection. Clearly the error is caused by the connection problem,maybe signal interference .

I will move forward to test my own code.

Thanks Clivel.

0690X00000605c5QAA.png

rwmao
Senior
Posted on June 16, 2015 at 05:35

Hi Clivel,

My code still doesn't go through.

The connection is definitely fine because I tested your hex file.

The program at problem when loading sector 0 of sd card.

I have much experiences on pic but not stm32. 

I can get the LED blinking but really lost on the uSD.

Please give me some advise.

Thanks

Posted on June 16, 2015 at 17:29

I've published projects to the forum, there are a couple in the thread cited earlier.

I'm not sure what your sector 0 issue is, the sector should typically contain the MBR (Master Boot Record), which is nominally some boot code and a partition table.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rwmao
Senior
Posted on July 03, 2015 at 07:56

Hi Clivel,

With the help of you and burton.mark , I have a better understanding of cubemx.

Also I studied the example provided in STM32Cube\Repository\STM32Cube_FW_F4_V1.5.0\Projects\STM324xG_EVAL\Applications\FatFs\FatFs_uSD.

Slightly revised project and produced an standalone project.

Maybe there is someone with entry level needs the code to start with.

Therefore I uploaded it to github (bitbucket)

https://bitbucket.org/rwmao/microsd_stmf407test/wiki/Home

The source code can be downloaded there.

Thanks