【问题标题】:f_mount() returns FA_DISK_ERRf_mount() 返回 FA_DISK_ERR
【发布时间】:2018-12-08 13:38:56
【问题描述】:

在收到大家的建议后,我对源代码进行了一些更改。为了将 FATFS API 与 SPI 接口连接,我已遵循此代码..

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

并相应地更改了我的user_diskio.c。

完成所有这些操作后,我的 SD 卡也返回 FA_DISK_ERROR。

我已经更换了 SD 卡插槽以及 SD 卡。但没有任何改变。

我的user_diskio.c 看起来像这样:

#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 */

而fatfs_sd.c 看起来像这样:

转到上面的 GITHUB 链接

不知道我哪里出错了。

有关此主题的任何内容都会有所帮助。

提前致谢。

【问题讨论】:

    标签: stm32 sd-card spi cubemx


    【解决方案1】:

    我之前使用 stm32f7 芯片为 FatFS 编写了自己的驱动程序,我想我可能知道发生了什么。我的猜测是您可能忘记在diskio.h 中附加disk_write 和disk_read 函数的实现。如果不这样做,FatFS 库就不知道如何与 SD 卡实际连接。

    您可以查看 FatFS 文档/手册作为一般资源:here

    非常密切关注此应用说明中的“必需功能”部分:here

    一个类似你的示例项目,但用于 STM32F4:here

    【讨论】:

    • 我想你快到了。我搜索了上面链接的源代码,找到了我之前提到的那些函数的所有存根定义。乍一看,它们看起来还不错。我找不到的是您将函数实际附加到 FatFS 驱动程序的位置。这是通过调用 `FATFS_LinkDriver(const Diskio_drvTypeDef *drv, char *path)' 来完成的。您在 user_diskio.c 的第 89 行定义了 Diskio_drvTypeDef 结构,并且似乎已经填写了函数句柄。现在你只需要通过调用链接函数让 FatFS 知道它的存在。
    • 我猜是在这个函数中调用void MX_FATFS_Init(void) { /*## FatFS: Link the USER driver ###########################*/ retUSER = FATFS_LinkDriver(&amp;USER_Driver, USERPath); /* USER CODE BEGIN Init */ /* additional user code for init */ /* USER CODE END Init */ }
    • 这个函数是从main调用的。
    • 嗯,是的,我知道我错过了。我的错。除了开始在调试器中逐行执行 FatFS 代码并查看错误返回的位置之外,我不确定要告诉您什么。 IMO 他们实际上在返回有用的错误方面做得相当好。一旦您在 FatFS 中找到错误的来源,检查周围的代码应该会告诉您失败的原因。另一个建议是使用逻辑分析仪来确保您的 SPI 输出实际工作。我无法告诉你它“应该”工作的次数,但只是发现它不是通过逻辑分析仪。
    • 好的,我会在调试器中逐行检查,谢谢。对主题有了更深入的了解。一旦我得到它的工作,我会发布答案。谢谢
    猜你喜欢
    • 2016-10-20
    • 2018-11-15
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2018-09-15
    • 2019-06-27
    相关资源
    最近更新 更多