0

I have made few changes in my source code after suggestion form everyone.For connecting FATFS API with SPI interface i have followed this code..

https://github.com/eziya/STM32_SPI_SDCARD/blob/master/Src/fatfs_sd.c

and changed my user_diskio.c accordingly.

After doing all those things then also my SD Card returns FA_DISK_ERROR.

I have changed SD Card slot as well as SD Card. But nothing Changed.

My user_diskio.c look like this:

#include <string.h>
#include "ff_gen_drv.h"
#include "fatfs_sd.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
/* Disk status */
static volatile DSTATUS Stat = STA_NOINIT;

/* USER CODE END DECL */

/* Private function prototypes -----------------------------------------------*/
DSTATUS USER_initialize (BYTE pdrv);
DSTATUS USER_status (BYTE pdrv);
DRESULT USER_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
#if _USE_WRITE == 1
  DRESULT USER_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);  
#endif /* _USE_WRITE == 1 */
#if _USE_IOCTL == 1
  DRESULT USER_ioctl (BYTE pdrv, BYTE cmd, void *buff);
#endif /* _USE_IOCTL == 1 */

Diskio_drvTypeDef  USER_Driver =
{
  USER_initialize,
  USER_status,
  USER_read, 
#if  _USE_WRITE
  USER_write,
#endif  /* _USE_WRITE == 1 */  
#if  _USE_IOCTL == 1
  USER_ioctl,
#endif /* _USE_IOCTL == 1 */
};

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes a Drive
  * @param  pdrv: Physical drive number (0..)
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_initialize (
    BYTE pdrv           /* Physical drive nmuber to identify the drive */
)
{
  /* USER CODE BEGIN INIT */
    return SD_disk_initialize(pdrv);
  /* USER CODE END INIT */
}

/**
  * @brief  Gets Disk Status 
  * @param  pdrv: Physical drive number (0..)
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_status (
    BYTE pdrv       /* Physical drive number to identify the drive */
)
{
  /* USER CODE BEGIN STATUS */
    return SD_disk_status(pdrv);
  /* USER CODE END STATUS */
}

/**
  * @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 USER_read (
    BYTE pdrv,      /* Physical drive nmuber to identify the drive */
    BYTE *buff,     /* Data buffer to store read data */
    DWORD sector,   /* Sector address in LBA */
    UINT count      /* Number of sectors to read */
)
{
  /* USER CODE BEGIN READ */
    return SD_disk_read(pdrv, buff, sector, count);
  /* USER CODE END READ */
}

/**
  * @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 USER_write (
    BYTE pdrv,          /* Physical drive nmuber to identify the drive */
    const BYTE *buff,   /* Data to be written */
    DWORD sector,       /* Sector address in LBA */
    UINT count          /* Number of sectors to write */
)
{ 
  /* USER CODE BEGIN WRITE */
  /* USER CODE HERE */
    return SD_disk_write(pdrv, buff, sector, count);
  /* USER CODE END WRITE */
}
#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 USER_ioctl (
    BYTE pdrv,      /* Physical drive nmuber (0..) */
    BYTE cmd,       /* Control code */
    void *buff      /* Buffer to send/receive control data */
)
{
  /* USER CODE BEGIN IOCTL */
    return SD_disk_ioctl(pdrv, cmd, buff);
  /* USER CODE END IOCTL */
}
#endif /* _USE_IOCTL == 1 */

and fatfs_sd.c looks like this:

GO TO THE ABOVE GITHUB LINK

Have no idea where i am going wrong.

Anything on this topic will be helpful.

Thanks in advance.

Devjeet Mandal
  • 345
  • 1
  • 4
  • 23

1 Answers1

1

I've written my own driver using an stm32f7 chip for FatFS before and think I might know what's going on. My guess is that you likely forgot to attach implementations for the disk_write and disk_read functions in diskio.h. Without doing that, the FatFS library has no clue on how to actually interface with the SD card.

You could check out the FatFS documentation/manual as a general resource: here

Pay very close attention to the "Required Functions" section in this app note: here

An example project like yours but for STM32F4: here

Community
  • 1
  • 1
Brandon Braun
  • 316
  • 2
  • 12
  • I think you are almost there. I searched through your source code linked above and found all your stub definitions for those functions I mentioned earlier. At a glance they look ok. What I was unable to find was where you actually attached your functions to the FatFS driver. This is done with a call to `FATFS_LinkDriver(const Diskio_drvTypeDef *drv, char *path)' . You have your definition of the Diskio_drvTypeDef struct on line 89 of user_diskio.c, and appear to have filled out the function handles. Now you just need to let FatFS know it exists by calling that link function. – Brandon Braun Jul 01 '18 at 12:56
  • I guess it's call in this function `void MX_FATFS_Init(void) { /*## FatFS: Link the USER driver ###########################*/ retUSER = FATFS_LinkDriver(&USER_Driver, USERPath); /* USER CODE BEGIN Init */ /* additional user code for init */ /* USER CODE END Init */ }` – Devjeet Mandal Jul 01 '18 at 13:10
  • and this function is called from main. – Devjeet Mandal Jul 01 '18 at 13:11
  • Hmm yeah I see I missed that. My bad. I'm not sure what to tell you other than to start stepping through the FatFS code line by line in the debugger and seeing where the error is returned. IMO they actually do a fairly good job of returning useful errors. Once you find where the error is issued from in FatFS, inspecting the surrounding code should tell you why it failed. Another suggestion is to get a logic analyzer to make sure your SPI output is actually working. I can't tell you the number of times where it "should" be working, but only discovered it wasn't through a logic analyzer. – Brandon Braun Jul 01 '18 at 13:18
  • Ok i will check line by line in debugger thanks anyway. Got a deeper view on topic. Once i get it working i will post the answer. Thanks – Devjeet Mandal Jul 01 '18 at 13:37