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 May 30, 2015 at 02:55

The version of the mx is lastest one, 4.7.0.

Keil 5.14.

Please help. I have been struggling this for a few days.

Reading microsd should be very simple task.

Posted on May 30, 2015 at 03:24

The failure almost certainly has nothing to do with the code you have presented.

You want to be looking at the code in DISKIO.C (or whatever) that has the abstraction code to the underlying SDIO drivers. Why it's failing is anyones guess, but you'd want to start by instrumenting the read functions. I would start by testing the SDCard independently of the Cube/HAL, and ideally free of the FatFs code.

The card interface needs short wires and pull-up resistors in the 33-47K. I've hand wired such socket arrangements to STM32F4-DISCO boards, and used break-out boards like the STM32F4-DIS-BB.

If you have an STM32F429I-DISCO, there are other issues to contend with.
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 May 30, 2015 at 03:36

Thanks for your quick reply.

I am using F407 chip, as in the discovery board. There is pull up resistor connected with breakup board. The code for diskio.c is generated by the cubemx. By the way would you please give a very short example to show how to access sd card without fat lib? Appreciate.

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 */
/* */
/* Portions COPYRIGHT 2014 STMicroelectronics */
/* Portions Copyright (C) 2012, ChaN, all right reserved */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control module to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
/**
******************************************************************************
* @file diskio.c 
* @author MCD Application Team
* @version V1.2.1
* @date 20-November-2014
* @brief FatFs low level disk I/O module.
******************************************************************************
* @attention
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the ''License'');
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an ''AS IS'' BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include ''diskio.h''
#include ''ff_gen_drv.h''
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern
Disk_drvTypeDef disk;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes a Drive
* @param pdrv: Physical drive number (0..)
* @retval DSTATUS: Operation status
*/
DSTATUS disk_initialize(BYTE pdrv)
{
DSTATUS stat = RES_OK;
if
(disk.is_initialized[pdrv] == 0)
{ 
disk.is_initialized[pdrv] = 1;
stat = disk.drv[pdrv]->disk_initialize();
}
return
stat;
}
/**
* @brief Gets Disk Status 
* @param pdrv: Physical drive number (0..)
* @retval DSTATUS: Operation status
*/
DSTATUS disk_status(BYTE pdrv)
{
DSTATUS stat;
stat = disk.drv[pdrv]->disk_status();
return
stat;
}
/**
* @brief Reads Sector(s) 
* @param pdrv: Physical drive number (0..)
* @param *buff: Data buffer to store read data
* @param sector: Sector address (LBA)
* @param count: Number of sectors to read (1..128)
* @retval DRESULT: Operation result
*/
DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
{
DRESULT res;
res = disk.drv[pdrv]->disk_read(buff, sector, count);
return
res;
}
/**
* @brief Writes Sector(s) 
* @param pdrv: Physical drive number (0..)
* @param *buff: Data to be written
* @param sector: Sector address (LBA)
* @param count: Number of sectors to write (1..128)
* @retval DRESULT: Operation result
*/
#if _USE_WRITE == 1
DRESULT disk_write(BYTE pdrv, 
const
BYTE *buff, DWORD sector, UINT count)
{
DRESULT res;
res = disk.drv[pdrv]->disk_write(buff, sector, count);
return
res;
}
#endif /* _USE_WRITE == 1 */
/**
* @brief I/O control operation 
* @param pdrv: Physical drive number (0..)
* @param cmd: Control code
* @param *buff: Buffer to send/receive control data
* @retval DRESULT: Operation result
*/
#if _USE_IOCTL == 1
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, 
void
*buff)
{
DRESULT res;
res = disk.drv[pdrv]->disk_ioctl(cmd, buff);
return
res;
}
#endif /* _USE_IOCTL == 1 */
/**
* @brief Gets Time from RTC 
* @param None
* @retval Time in DWORD
*/
DWORD get_fattime (
void
)
{
return
0;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Posted on May 30, 2015 at 05:10

You'll have to dig into the layers below this. Is it using DMA?

I posted some interface test code that outputs via the SWV screen in the ST-LINK Utilities.

About 50% down [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/sdio%20fatfs%20speed%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=1425]this thread. Try this as a method of validating your card interface.

I'm not a user of the HAL/Cube libraries. There is SDIO code within the SPL, and I've posted several source examples for the STM32F4-DISCO

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 May 30, 2015 at 07:26

Thanks Clivel

Let me check the interface this weekend. I don't have the board at my hand now.

rwmao
Senior
Posted on June 01, 2015 at 03:42

Clivel

I am using DMA.

The screenshot is as shown.

By the way I am using 16gb microsd.

There are pull up resistor connected for D0-D4,and Cmd. No resistor for clk.

Please give further advise.

thanks

0690X00000605hOQAQ.png

Posted on June 01, 2015 at 06:55

This would suggest to me that the interface is not functional, you need to double check what you've built, or use a STM32F4-DIS-BB board to confirm things are working with that.

The SD Slot/Socket is expected to be wired as follows
CARDDETECT = PC2 (Retargetable)
CLK = PC12
CMD = PD2
D0 = PC8
D1 = PC9
D2 = PC10
D3 = PC11
VDD = 3V
VSS = GND
PC10 (SCLK) and PC12 (SDIN) potentially conflict with the CS43L22
The CMD, D0, D1, D2, D3 pins should have 33K or 47K pull up resistors.

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 June 02, 2015 at 03:08

Clivel,

Wires are soldered and connection were double checked.

Detection pin was ignored because the adapter has only 8pins.

Still didn't work through.

Please help more.

0690X00000605gVQAQ.png

Posted on June 02, 2015 at 03:46

May be you can provide the part# for your socket, or a web page for it, along with a diagram of exactly how you have it wired.

The example code ignored the Card Detect GPIO. Watch the non-sequential D0..D3

0690X00000603SWQAY.jpg
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..