lebment 发表于 2018-5-16 18:19:43

STM32F407用CubeMX创建SDIO+FatFs,f_Open失败

本帖最后由 lebment 于 2018-7-12 20:59 编辑

如题,具体环境是CubeMX最新版,HAL库最新版,MDK5.24a,STLINKv2-1,板子是STM32F407Vet6核心板(某宝四五十块钱)。
SDIO单独测试TF卡(4G卡肯定不是正版)成功,可以读出CSD,CID,卡的状态,卡的容量等,SDIO四线无DMA读写正常。


本人前前后后试过无数次,好几个月,现在不得不弄好!感谢大佬的帮助!
CubeMX:SDIO四线,无DMA,无SDIO全局中断,勾选FatFS文件系统,文件系统加入长名STACK,单片机HEAP-0x800,STACK-0x1000
具体代码:
主程序:

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
FRESULT res;                                          /* FatFs function common retSDult code */
uint32_t byteswritten, bytesread;                     /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext;                                 /* File read buffer */
/* USER CODE END PV */


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();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

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

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDIO_SD_Init();
MX_USART2_UART_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */

****************************************************
   从HAL库中F4Disco里抄来的代码
****************************************************
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
    /* FatFs Initialization Error */
    Error_Handler();
}
else
{
      /* Create and Open a new text file object with write access */
      if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
      {
      /* 'STM32.TXT' file Open for write Error */
      Error_Handler();
      }
      else
      {
      /* Write data to the text file */
      res = f_write(&SDFile, wtext, sizeof(wtext), (void *)&byteswritten);
      
      if((byteswritten == 0) || (retSD != FR_OK))
      {
          /* 'STM32.TXT' file Write or EOF Error */
          Error_Handler();
      }
      else
      {
          /* Close the open text file */
          f_close(&SDFile);
         
      /* Open the text file object with read access */
      if(f_open(&SDFile, "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(&SDFile, rtext, sizeof(rtext), (void *)&bytesread);
         
          if((bytesread == 0) || (retSD != FR_OK))
          {
            /* 'STM32.TXT' file Read or EOF Error */
            Error_Handler();
          }
          else
          {
            /* Close the open text file */
            f_close(&SDFile);
            
            /* 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 */
            HAL_GPIO_WritePin(GPIOA, D2_Pin|D3_Pin, GPIO_PIN_SET);
            }
          }
      }
      }
    }
}

/* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(SDPath);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

}


****************************************************
   单步调试结果 Sd_diskio.c中死循环
****************************************************
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
DRESULT res = RES_ERROR;
ReadStatus = 0;
uint32_t timeout;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
uint32_t alignedAddr;
#endif

if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,
                           (uint32_t) (sector),
                           count) == MSD_OK)
{
    /* Wait that the reading process is completed or a timeout occurs */
    timeout = HAL_GetTick();
    while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))//在此处无限循环
    {
    }
    /* incase of a timeout return error */
    if (ReadStatus == 0)
    {
      res = RES_ERROR;
    }
    else
    {
      ReadStatus = 0;
      timeout = HAL_GetTick();

      while((HAL_GetTick() - timeout) < SD_TIMEOUT)
      {
      if (BSP_SD_GetCardState() == SD_TRANSFER_OK)
      {
          res = RES_OK;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
            /*
               the SCB_InvalidateDCache_by_Addr() requires a 32-Byte aligned address,
               adjust the address and the D-Cache size to invalidate accordingly.
             */
            alignedAddr = (uint32_t)buff & ~0x1F;
            SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint32_t)buff - alignedAddr));
#endif
         break;
      }
      }
    }
}

return res;
}
由于MX更新,最新代码没有搞,怎么改,有空发。


feixiang20 发表于 2018-5-17 00:19:02

代码我一时也看不好,论坛里有那种【用STM32CubeMX快速生成一个SDIO+FATFS程序】【STM32CubeMx生成SDIO+SRAM+FATFS工程的记录和问题】等帖子么,可以参考下

jjbboox 发表于 2018-5-17 09:01:28

如果用DMA方式,现行的驱动是有问题的。
况且所谓的DMA方式也是要等待DMA读写完毕以后函数才返回的,和直接读取没太大的区别。
楼主还是老老实实用普通模式的文件读写吧。

Dandjinh 发表于 2018-5-17 09:16:45

应该这样写
// 初始化SDIO
MX_SDMMC1_SD_Init();
// 初始化FatFS
MX_FATFS_Init();

// SD卡初始化
BSP_SD_Init();

然后才能使用FatFS函数

lebment 发表于 2018-5-17 12:10:51

feixiang20 发表于 2018-5-17 00:19
代码我一时也看不好,论坛里有那种【用STM32CubeMX快速生成一个SDIO+FATFS程序】【STM32CubeMx生成SDIO+SRA ...

有,我也知道Cubemx生成肯定是有问题的

lebment 发表于 2018-5-17 12:11:19

Dandjinh 发表于 2018-5-17 09:16
应该这样写




SDIO早就初始化了,自动生成的

lebment 发表于 2018-5-17 12:11:50

jjbboox 发表于 2018-5-17 09:01
如果用DMA方式,现行的驱动是有问题的。
况且所谓的DMA方式也是要等待DMA读写完毕以后函数才返回的,和直接 ...

有没有DMA,SDIO读卡信息都是好的

lebment 发表于 2018-5-30 00:38:42

已解决,可以完成读写TXT!CubeMX生成的有问题,SDIO初始化里需要加入HAL SDIO初始化和四线初始化。

doesnt 发表于 2018-6-7 12:50:59

lebment 发表于 2018-5-30 00:38
已解决,可以完成读写TXT!CubeMX生成的有问题,SDIO初始化里需要加入HAL SDIO初始化和四线初始化。 ...

遇到相同的问题,请你您是怎么解决的?

sidnge 发表于 2018-10-11 21:30:53

怎么解决的,我也遇到这个问题,求助!
页: [1] 2
查看完整版本: STM32F407用CubeMX创建SDIO+FatFs,f_Open失败