移植Fatfs遇到f_open返回FR_NO_FILE等问题


1、函数disk_ioctl()内部*(FFDWORD *)buff = FATFS_FLASH_SECTOR_SIZE;这句代码在有些M0的arm核上会硬件错误,改为*(FFWORD *)buff = FATFS_FLASH_SECTOR_SIZE;即可,是因为字对齐访问的问题;


2、写测试函数时,FATFS定义的变量最好是全局变量,否则可能因为栈空间不足出现奇怪问题,如f_open返回FR_NO_FILE,读到的数据为0;


3、第一次挂载读写测试要先调用f_mkfs()函数格式化下,后面可去掉,直接f_mount、f_open、f_write、f_read就行了。


测试代码:

FATFS Fatfs_test;

void FatfsTest()
{
 //FATFS Fatfs_test;//定义成全局
 FIL fil;
 UINT nWriteLen = 0;
 UINT nReadLen = 0;
 unsigned char buf[64] = {0};
 unsigned char buf_write[] = "1234567890";
 int res = 0;


 res = f_mount(0, &Fatfs_test);
 
 //res = f_mkfs(0,0,256*4096);// 1MB大小 第一次运行需要格式化扇区
 //return;

    res = f_open(&fil, "0:/testfile.txt", FA_CREATE_NEW | FA_WRITE);
 if(res == FR_OK)
 {
  res = f_write(&fil,buf_write,strlen(buf_write),&nWriteLen);
  f_close(&fil);
 }
 else
 {
  if(res == FR_EXIST)
  {
   printf("文件存在\r\n");
  }
  else
  {
   printf("文件创建失败,res:%d\r\n",res);
  }
 }
 
 res = f_open(&fil, "0:/testfile.txt", FA_OPEN_EXISTING | FA_READ);
 printf("res:%d\r\n",res);
 for(;;)
 {
  res = f_read(&fil,buf,sizeof(buf),&nReadLen);
  printf("res:%d,nReadLen:%d\r\n",res,nReadLen);  
  if(res||nReadLen == 0)/*错误或者读到文件尾*/
   break;
 }
 f_close(&fil);
 f_mount(0,0);
 

}

猜你喜欢

转载自blog.csdn.net/hxl5955/article/details/53322944
今日推荐