STM32 SDIO FATFS文件系统 野火diskio.c文件解析

#define _FS_TINY  0 /* 0:Normal or 1:Tiny 完整的FATFS 精简版的是Tiny */

#define _FS_READONLY 0 /* 0:Read/Write or 1:Read only 能读能写*/

#define _FS_MINIMIZE 1 /* 0 to 3 简单的裁剪f_mkdir, f_chmod..这些功能没法用的*/
/* The _FS_MINIMIZE option defines minimization level to remove some functions.
/
/   0: Full function.
/   1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
/      are removed.
/   2: f_opendir and f_readdir are removed in addition to 1.
/   3: f_lseek is removed in addition to 2. */


#define _USE_STRFUNC 0 /* 0:Disable or 1/2:Enable是否使用字符串文件接口 */
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */


#define _USE_MKFS  0 /* 0:Disable or 1:Enable  制作文件系统我在PC上一般已经格式化好了*/
/* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */


#define _USE_FORWARD 0 /* 0:Disable or 1:Enable 发文件流?*/
/* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */


#define _USE_FASTSEEK 0 /* 0:Disable or 1:Enable 搜索*/
/* To enable fast seek feature, set _USE_FASTSEEK to 1. */

#define _CODE_PAGE 1 / /1    - ASCII only (Valid for non LFN cfg.)

#define _USE_LFN 0  /* 0 to 3 */
#define _MAX_LFN 255  /* Maximum LFN length to handle (12 to 255) 这些都是长文件名或是汉字文件支持很费资源所以不开启这些*/

#define _FS_SHARE 0 /* 0:Disable or >=1:Enable 不使用相对路径*/

#define _FS_SHARE 0 /* 0:Disable or >=1:Enable 文件共享多任务的操作系统会用到的*/

#define _FS_REENTRANT 0  /* 0:Disable or 1:Enable 这些是啥用?同步什么呢?默认就好了*/
#define _FS_TIMEOUT  1000 /* Timeout period in unit of time ticks */
#define _SYNC_t   HANDLE /* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */

integer.h主要定义了文件的类型 若是最新的可以不用修改。

都说要移植5个函数 。

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2011        */
/*-----------------------------------------------------------------------*/
/* This is a stub disk I/O module that acts as front end of the existing */
/* disk I/O modules and attach it to FatFs module with common interface. */
/*-----------------------------------------------------------------------*/

#include "diskio.h"
#include "stm32f10x.h"
#include "stm32_eval_sdio_sd.h"

#define BLOCK_SIZE            512 /* Block Size in Bytes */

 

/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */

DSTATUS disk_initialize (
 BYTE drv    /* Physical drive nmuber (0..) */
)
{
 SD_Error  Status;
 /* Supports only single driveFATFS支持多个设备 所以有个设备号drive nmuber当然了我就一个SD卡所以只有零号 */
 if (drv)
 {
  return STA_NOINIT;
 }
/*-------------------------- SD Init ----------------------------- */
  Status = SD_Init();
 if (Status!=SD_OK )
 {
  return STA_NOINIT;
 }
 else
 {
  return RES_OK;
 }

}

 

/*-----------------------------------------------------------------------*/
/* Return Disk Status                                                    */

DSTATUS disk_status (
 BYTE drv  /* Physical drive nmuber (0..) */
)
{
 return RES_OK; //懒的管了 有空写写 可以加个
}

 

/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */

DRESULT disk_read (
 BYTE drv,  /* Physical drive nmuber (0..) */
 BYTE *buff,  /* Data buffer to store read data */
 DWORD sector, /* Sector address (LBA) 注意这个是扇区地址也就是第几个扇区*/
 BYTE count  /* Number of sectors to read (1..255) 读取的扇区数*/
)
{
// SD_Error Status;
 if (count > 1)
 {
  SD_ReadMultiBlocks(buff, sector*BLOCK_SIZE, BLOCK_SIZE, count); //扇区地址*512就是实际地址 默认一个扇区就是512个字节
 }
 else
 {
  SD_ReadBlock(buff, sector*BLOCK_SIZE, BLOCK_SIZE);
 }
 return RES_OK;
}

 

/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */

#if _READONLY == 0
DRESULT disk_write (
 BYTE drv,   /* Physical drive nmuber (0..) */
 const BYTE *buff, /* Data to be written */
 DWORD sector,  /* Sector address (LBA) */
 BYTE count   /* Number of sectors to write (1..255) */
)
{
 if (count > 1)
 {
  SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);

/*这里大家看到了有个地址转换 因为DMA仅仅支持4字节指针的所以在函数内部还是有个转换的这里最好优化一下能提高效率*/
 }
 else
 {
  SD_WriteBlock((uint8_t *)buff,sector*BLOCK_SIZE, BLOCK_SIZE);
 }
 return RES_OK;
}
#endif /* _READONLY */

 


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */

DRESULT disk_ioctl (
 BYTE drv,  /* Physical drive nmuber (0..) */
 BYTE ctrl,  /* Control code */
 void *buff  /* Buffer to send/receive control data 若是用到擦除函数这个函数一定要补完的有空再写写吧我这简单的返回就好了*/
)
{
 return RES_OK;
}
        
/*-----------------------------------------------------------------------*/
/* Get current time                                                      */
/*-----------------------------------------------------------------------*/ 
DWORD get_fattime(void)
{
return ((2011UL-1980) << 25)       // Year = 2011
| (3UL << 21)       // Month = Mar
| (26UL << 16)       // Day = 26
| (13U << 11)       // Hour = 13
| (19U << 5)       // Min =19
| (0U >> 1)       // Sec = 0
;

你可能感兴趣的:(STM高级外设)