野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17235|回复: 3

FATFS文件系统——sector+=512后,f_open()返回7

[复制链接]
发表于 2019-12-7 17:25:31 | 显示全部楼层 |阅读模式
求助。
FATFS文件系统移植视频中的例程,我自己在编写程序的过程中出现1个问题
sector 没有偏移512之前,程序可以正常运行。
运行结果:

扇区未偏移

扇区未偏移

sector 偏移512之后,程序再执行完f_open后返回7,FR_DENIED。

扇区偏移

扇区偏移









/***************************程序***************************/
disk_read:

write

write




disk_write:



disk_ioctl:

ioctl

ioctl






read

read
回复

使用道具 举报

 楼主| 发表于 2019-12-8 10:27:11 | 显示全部楼层
我用资料盘给的例程也是f_open返回7
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-8 10:32:39 | 显示全部楼层
main.c文件如下


  1. #include "stm32f10x.h"
  2. #include "./usart/bsp_usart.h"
  3. #include "./led/bsp_led.h"
  4. #include "./flash/bsp_spi_flash.h"
  5. #include "ff.h"                        /* Declarations of FatFs API */



  6. FATFS   fs;
  7. FIL     fp;
  8. UINT    bw;
  9. UINT    br;
  10. const char wData[]="欢迎使用野火开发板!";
  11. char rData[4096]="";
  12. /*
  13. * 函数名:main
  14. * 描述  :主函数
  15. * 输入  :无
  16. * 输出  :无
  17. */
  18. int main(void)
  19. {        
  20.         FRESULT res;
  21.         LED_GPIO_Config();
  22.         LED_BLUE;
  23.        
  24.         /* 配置串口为:115200 8-N-1 */
  25.         USART_Config();
  26.         printf("\r\n这是一个 FATFS文件系统移植实验 \r\n");
  27.                
  28.         res = f_mount(&fs,"1:",1);
  29.        
  30.         printf("f_mount res=%d\n",res);
  31.        
  32.         if(res==FR_NO_FILESYSTEM)
  33.         {
  34.                 res=f_mkfs("1:",0,0);
  35.                 printf("\r\nf_mkfs res=%d\n",res);
  36.                
  37.                 //格式化后要取消挂载再重新挂载
  38.                 res = f_mount(NULL,"1:",1);
  39.                 res = f_mount(&fs,"1:",1);
  40.                
  41.                 printf("\r\nsecond f_mount res=%d\n",res);
  42.         }
  43.        
  44.         res=f_open(&fp,"1:abcd.txt",FA_OPEN_ALWAYS|FA_READ|FA_WRITE );  
  45.         printf("f_open res=%d\n",res);
  46.         if(res==FR_OK )
  47.         {
  48.                 res=f_write(&fp,wData,sizeof(wData),&bw);
  49.                 printf("\r\nbw=%d",bw);
  50.                 if(res==FR_OK )
  51.                 {       
  52.                         f_lseek(&fp,0);
  53.                         res=f_read(&fp,rData,f_size(&fp),&br);
  54.                        
  55.                         if(res==FR_OK )
  56.                         {
  57.                                 printf("\r\n文件内容:%s br=%d",rData,br);
  58.                         }
  59.                 }       
  60.         }
  61.         f_close(&fp);  
  62.        
  63.         while(1);  
  64. }


复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-8 10:36:14 | 显示全部楼层
diskio.c

  1. #include "diskio.h"                /* FatFs lower layer API */
  2. #include "./flash/bsp_spi_flash.h"
  3. //#include "usbdisk.h"        /* Example: Header file of existing USB MSD control module */
  4. //#include "atadrive.h"        /* Example: Header file of existing ATA harddisk control module */
  5. //#include "sdcard.h"                /* Example: Header file of existing MMC/SDC contorl module */

  6. /* Definitions of physical drive number for each drive */
  7. //#define ATA                0        /* Example: Map ATA harddisk to physical drive 0 */
  8. //#define MMC                1        /* Example: Map MMC/SD card to physical drive 1 */
  9. //#define USB                2        /* Example: Map USB MSD to physical drive 2 */

  10. #define SD_CARD                0
  11. #define SPI_FLASH        1
  12. /*-----------------------------------------------------------------------*/
  13. /* Get Drive Status                                                      */
  14. /*-----------------------------------------------------------------------*/

  15. DSTATUS disk_status (
  16.         BYTE pdrv                /* Physical drive nmuber to identify the drive */
  17. )
  18. {
  19.         DSTATUS stat;
  20. //        int result;

  21.         switch (pdrv) {
  22.         case SD_CARD :
  23.         //        result = ATA_disk_status();

  24.                 // translate the reslut code here

  25.                 return stat;

  26.         case SPI_FLASH :
  27.                
  28.         if(SPI_FLASH_ReadID()==sFLASH_ID)
  29.         {
  30.                  //状态正常
  31.                 stat = 0 ;
  32.         }
  33.         else
  34.         {
  35.                  //状态异常
  36.                 stat = STA_NOINIT ;
  37.         }
  38.                 return stat;

  39.         }
  40.         return STA_NOINIT;
  41. }



  42. /*-----------------------------------------------------------------------*/
  43. /* Inidialize a Drive                                                    */
  44. /*-----------------------------------------------------------------------*/

  45. DSTATUS disk_initialize (
  46.         BYTE pdrv                                /* Physical drive nmuber to identify the drive */
  47. )
  48. {
  49.         DSTATUS stat;
  50.         //int result;

  51.         switch (pdrv) {
  52.         case SD_CARD :
  53.         //        result = ATA_disk_initialize();

  54.                 // translate the reslut code here

  55.                 return stat;

  56.         case SPI_FLASH :
  57.                        
  58.          SPI_FLASH_Init();

  59.          SPI_Flash_WAKEUP();
  60.        
  61.                 return disk_status(SPI_FLASH);

  62.         }
  63.         return STA_NOINIT;
  64. }



  65. /*-----------------------------------------------------------------------*/
  66. /* Read Sector(s)                                                        */
  67. /*-----------------------------------------------------------------------*/

  68. DRESULT disk_read (
  69.         BYTE pdrv,                /* Physical drive nmuber to identify the drive */
  70.         BYTE *buff,                /* Data buffer to store read data */
  71.         DWORD sector,        /* Sector address in LBA */
  72.         UINT count                /* Number of sectors to read */
  73. )
  74. {
  75.         DRESULT res;
  76. //        int result;

  77.         switch (pdrv) {
  78.         case SD_CARD :
  79.                 // translate the arguments here

  80.                 //result = ATA_disk_read(buff, sector, count);

  81.                 // translate the reslut code here

  82.                 return res;

  83.         case SPI_FLASH :  
  84.                 /*扇区偏移2MB,外部FLASH文件系统空间放在SPI FLASH后面6MB空间*/
  85.         //        sector += 512;
  86.                 SPI_FLASH_BufferRead(buff,sector*4096,count*4096);
  87.        
  88.                 res=RES_OK ;
  89.        
  90.                 return res;

  91.         }

  92.         return RES_PARERR;
  93. }



  94. /*-----------------------------------------------------------------------*/
  95. /* Write Sector(s)                                                       */
  96. /*-----------------------------------------------------------------------*/

  97. #if _USE_WRITE
  98. DRESULT disk_write (
  99.         BYTE pdrv,                        /* Physical drive nmuber to identify the drive */
  100.         const BYTE *buff,        /* Data to be written */
  101.         DWORD sector,                /* Sector address in LBA */
  102.         UINT count                        /* Number of sectors to write */
  103. )
  104. {
  105.         DRESULT res;
  106.         //int result;
  107.   
  108.         switch (pdrv) {
  109.         case SD_CARD  :
  110.                 // translate the arguments here

  111.                 //result = ATA_disk_write(buff, sector, count);

  112.                 // translate the reslut code here

  113.                 return res;

  114.         case SPI_FLASH :
  115.                         /*扇区偏移2MB,外部FLASH文件系统空间放在SPI FLASH后面6MB空间*/
  116.                         //sector += 512;
  117.        
  118.                         //必须先擦除再写入
  119.                         SPI_FLASH_SectorErase(sector*4096);
  120.                         SPI_FLASH_BufferWrite((u8 *)buff,sector*4096,count*4096);
  121.             
  122.                         res=RES_OK ;
  123.        
  124.                 return res;

  125.         }

  126.         return RES_PARERR;
  127. }
  128. #endif


  129. /*-----------------------------------------------------------------------*/
  130. /* Miscellaneous Functions                                               */
  131. /*-----------------------------------------------------------------------*/

  132. #if _USE_IOCTL
  133. DRESULT disk_ioctl (
  134.         BYTE pdrv,                /* Physical drive nmuber (0..) */
  135.         BYTE cmd,                /* Control code */
  136.         void *buff                /* Buffer to send/receive control data */
  137. )
  138. {
  139.         DRESULT res;
  140. //        int result;

  141.         switch (pdrv) {
  142.         case SD_CARD  :

  143.                 // Process of the command for the ATA drive

  144.                 return res;

  145.         case SPI_FLASH  :
  146.                         switch(cmd)
  147.                         {
  148.                                 //返回扇区个数
  149.                                 case GET_SECTOR_COUNT:
  150.                                         /*扇区数量:1536*4096/1024/1024=6(MB)*/
  151.                                         *(DWORD * )buff = 2048;
  152.                                 break;
  153.                                
  154.                                 //返回扇区大小
  155.                                 case GET_SECTOR_SIZE:
  156.                                         *(WORD*)buff = 4096;
  157.                                 break;
  158.                                
  159.                                 //返回擦除扇区的最小个数 (单位是扇区)
  160.                                 case GET_BLOCK_SIZE:
  161.                                         *(DWORD*)buff = 1;
  162.                                 break;
  163.                         }

  164.                  res=RES_OK ;

  165.                 return res;

  166.         }

  167.         return RES_PARERR;
  168. }
  169. #endif



  170. //返回时间
  171. DWORD get_fattime (void)
  172. {
  173.         return  0;
  174. }

复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2025-12-6 09:42 , Processed in 0.039389 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表