【问题标题】:Cannot mount SD using FatFS on STM32在 STM32 上无法使用 FatFS 挂载 SD
【发布时间】:2017-10-05 17:11:34
【问题描述】:

我正在尝试使用 STM32F405 芯片写入 MicroSD 卡。
引脚连接正确,并且可以使用 HAL_GPIO_WritePin 写入 MicroSD 卡插槽上的每个引脚。 (用示波器测量)我正在使用 CubeMX 为 TrueStudio 生成初始化代码,所以希望那里一切正常。但是当我运行以下代码时,f_mount 返回FR_DISK_ERR。 MicroSD 卡可以写入和读取。如果我使用不同的设备编号,即“1:”,我会得到FR_INVALID_DRIVE

所以我的问题是:除了有故障的 MicroSD 卡之外,还有什么可能导致 FR_DISK_ERR

到目前为止,这是我的代码:

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();

  /* USER CODE BEGIN WHILE */
  FATFS fileSystem;
  FIL testFile;
  uint8_t testBuffer[16] = "SD write success";
  UINT testBytes;
  FRESULT res;

  while((res = f_mount(&fileSystem, SD_MOUNT_PATH, 1)) != FR_OK){
      printf("%d", res); //used to debug res, only for TrueStudio Debugger
  }


    uint8_t path[13] = "testfile.txt";
    path[12] = '\0';

    res = f_open(&testFile, (char*)path, FA_WRITE | FA_CREATE_ALWAYS);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
    res = f_write(&testFile, testBuffer, 16, &testBytes);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);

    res = f_close(&testFile);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);


}

MX_FATFS_Init() FATFS_LinkDriver(&SD_Driver, SD_Path) 中被调用并返回0。

【问题讨论】:

  • 您的问题解决了吗?我有同样的问题。你能帮帮我吗?

标签: sd-card stm32 fatfs


【解决方案1】:

如果没有 SD_MOUNT_PATH 宏,f_mount 调用是否如下所示: f_mount(&fileSystem, "0:", 1)?'

你是说f_open,f_write即使f_mount失败也返回FR_OK?!

FR_DISK_ERR 通常意味着 disk_read() 或 disk_write() 失败。尝试在使用 f_mount() 之前以及在 fatfs 函数调用之间给予 100 毫秒或 1 秒的延迟。

【讨论】:

  • 是的,SD_MOUNT_PATH"0:" 的宏,因为 f_mount 失败,while 循环将永远不会退出,所以不,f_openf_write 也不会返回 FR_OK .我应该说,GDB 调试器向我展示了它在disk_read 期间失败。看来,disk_writef_mount 期间从未执行过
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 2020-08-21
  • 2016-10-20
  • 2019-06-03
  • 2023-03-05
  • 2017-06-19
  • 2018-05-16
  • 2017-03-20
相关资源
最近更新 更多