#STM32学习# 使用文件系统FAT

1、我在使用stm32进行文件操作时,学习和使用了FatFs文件系统。如下图所示,关键在于 FatFs module 和 LowLevel disk I/O layer。

dependency diagram

2、在项目中需要将FatFs module 和 LowLevel disk I/O layer 各自 的内容分别定义好。我所涉及的项目,就包含了这样的文件。如下图所示:



下方绿色文字,表示  LowLevel disk I/O layer   涉及的内容:

2.1.1 在diskio.h中,定义了很多的变量和宏,也包括几个关键的函数声明:

DSTATUS disk_initialize (BYTE);    //底层初始化
DSTATUS disk_status (BYTE);
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
#if	_READONLY == 0
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
#endif
DRESULT disk_ioctl (BYTE, BYTE, void*);

2.1.2 在diskio.c中,是对上述函数的实现,等等(包括一些中间函数)。而其中,用户在使用时需要注意的是:

DSTATUS disk_initialize (BYTE drv)    /* Physical drive nmuber (0..) */
{
        switch( drv ){	/* Supports only single drive */
        case FS_SPI_FLASH:
        Flash_Init();
        return RES_OK;
  }
  return STA_NOINIT;

}

2.1.3 Flash_Init();被定义在Flash.c文件中。内部的SPI_FLASH_Init(); 是使用SPI与Flash沟通的引脚定义(GPIO)。

u8 Flash_Init(void) 
{ 
	u8 Device_Id;
	u32 Produce_Id;
	SPI_FLASH_Init();
	Device_Id = SPI_FLASH_ReadDeviceID();
	Produce_Id = SPI_FLASH_ReadID();		
	printf("Produce_Id = %x\r\n",Produce_Id);
	if(Device_Id != Device_ID_Name){	
		return 1;
	}
	return 0;
}

2.1.4 SPI_FLASH_Init(); 定义在SPI.c中。


/*PB13,14,15   PB12是片选信号*/
void SPI_FLASH_Init(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
。。。
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  /*clock enable */
。。。
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /*MISO */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
。。。
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /* CS pin */
。。。
  GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /*Chip Select high */
  SPI_FLASH_CS_HIGH();
  /* SPI1 configuration */
  // W25X16: data input on the DIO pin is sampled on the rising edge of the CLK. 
  // Data on the DO and DIO pins are clocked out on the falling edge of CLK.
。。。
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI2, &SPI_InitStructure);
  /* Enable SPI1  */
  SPI_Cmd(SPI2, ENABLE);
}


下方蓝色文字表示 FatFs module  涉及的内容

2.2 首先要明确,FatFs module 使用的基础函数 就是在  LowLevel disk I/O layer 中定义的函数。

FatFs module 将 基础函数 封装成 对应功能的 各种文件操作函数:

FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode);				/* Open or create a file */
FRESULT f_close (FIL* fp);											/* Close an open file object */
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);			/* Read data from the file */
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw);	/* Write data to the file */
FRESULT f_lseek (FIL* fp, FSIZE_t ofs);								/* Move file pointer of the file object */
FRESULT f_truncate (FIL* fp);										/* Truncate the file */
FRESULT f_sync (FIL* fp);											/* Flush cached data of the writing file */
FRESULT f_opendir (DIR* dp, const TCHAR* path);						/* Open a directory */
FRESULT f_closedir (DIR* dp);										/* Close an open directory */
FRESULT f_readdir (DIR* dp, FILINFO* fno);							/* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern);	/* Find first file */
FRESULT f_findnext (DIR* dp, FILINFO* fno);							/* Find next file */
FRESULT f_mkdir (const TCHAR* path);								/* Create a sub directory */
FRESULT f_unlink (const TCHAR* path);								/* Delete an existing file or directory */
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new);	/* Rename/Move a file or directory */
FRESULT f_stat (const TCHAR* path, FILINFO* fno);					/* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask);			/* Change attribute of a file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno);			/* Change timestamp of a file/dir */
FRESULT f_chdir (const TCHAR* path);								/* Change current directory */
FRESULT f_chdrive (const TCHAR* path);								/* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len);							/* Get current directory */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs);	/* Get number of free clusters on the drive */
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn);	/* Get volume label */
FRESULT f_setlabel (const TCHAR* label);							/* Set volume label */
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf);	/* Forward data to the stream */
FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt);					/* Allocate a contiguous block to the file */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt);			/* Mount/Unmount a logical drive */
FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len);	/* Create a FAT volume */
FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work);			/* Divide a physical drive into some partitions */
int f_putc (TCHAR c, FIL* fp);										/* Put a character to the file */
int f_puts (const TCHAR* str, FIL* cp);								/* Put a string to the file */
int f_printf (FIL* fp, const TCHAR* str, ...);						/* Put a formatted string to the file */
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);						/* Get a string from the file */

2.2.1 相关文件操作函数的具体功能请自行查找和理解。

学习FatFs的网址:

http://elm-chan.org/fsw/ff/00index_e.html


3. 未完待续


猜你喜欢

转载自blog.csdn.net/kshine2017/article/details/80060097
今日推荐