对于像SPIFLASH、SD卡这类大容量的存储设备,如果你采用读写扇区的方式,那可真的要下点功夫去做存储空间分配了,呵呵,列一个大大的表格在里面表明变量的存储地址,停!有点头晕!我想静静!不扯淡了,切入正题,关于什么是文件系统,请各位直接找度娘或狗哥哥吧。老规矩,直接上代码!前几天已经在论坛发布了SPI和DSPI驱动W25QXX系列SPIFlash的帖子,需要的自己去看,本部分的用到了SPIFLASH的接口函数。在源码的基础上仅仅修改了diskio.c的几个文件,在main函数中包含ff.h即可使用,(注:没有配置支持中文文件名,需要的自己查资料配置)具体请大家自己看代码吧:- /*-----------------------------------------------------------------------*/
- /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
- /*-----------------------------------------------------------------------*/
- /* If a working storage control module is available, it should be */
- /* attached to the FatFs via a glue function rather than modifying it. */
- /* This is an example of glue functions to attach various exsisting */
- /* storage control modules to the FatFs module with a defined API. */
- /*-----------------------------------------------------------------------*/
- #include "diskio.h" /* FatFs lower layer API */
- //#include "u***disk.h" /* Example: Header file of existing USB MSD control module */
- //#include "atadrive.h" /* Example: Header file of existing ATA harddisk control module */
- //#include "sdcard.h" /* Example: Header file of existing MMC/SDC contorl module */
- #include "w25qxx.h"
- #define EX_FLASH 0 //外部flash,卷标为1
- #define FLASH_SECTOR_SIZE 512
- uint16_t FLASH_SECTOR_COUNT=2048*7; //W25Q64,前7M字节给FATFS占用
- #define FLASH_BLOCK_SIZE 8 //每个BLOCK有8个扇区
- //获得磁盘状态
- DSTATUS disk_status (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
- )
- {
- return RES_OK;
- }
- //初始化磁盘
- DSTATUS disk_initialize (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
- )
- {
- uint8_t res=0;
- switch(pdrv)
- {
- case EX_FLASH://外部flash
- W25QXX_Init();
- FLASH_SECTOR_COUNT=2048*12;//W25Q1218,前12M字节给FATFS占用
- break;
- default:
- res=1;
- }
- if(res)return STA_NOINIT;
- else return 0; //初始化成功
- }
- //读扇区
- //pdrv:磁盘编号0~9
- //*buff:数据接收缓冲首地址
- //sector:扇区地址
- //count:需要读取的扇区数
- DRESULT disk_read (
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- BYTE *buff, /* Data buffer to store read data */
- DWORD sector, /* Sector address in LBA */
- UINT count /* Number of sectors to read */
- )
- {
- uint8_t res=0;
- if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误
- switch(pdrv)
- {
- case EX_FLASH://外部flash
- for(;count>0;count--)
- {
- W25QXX_Read(buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
- sector++;
- buff+=FLASH_SECTOR_SIZE;
- }
- res=0;
- break;
- default:
- res=1;
- }
- //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
- if(res==0x00)return RES_OK;
- else return RES_ERROR;
- }
- //写扇区
- //pdrv:磁盘编号0~9
- //*buff:发送数据首地址
- //sector:扇区地址
- //count:需要写入的扇区数
- #if _USE_WRITE
- DRESULT disk_write (
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- const BYTE *buff, /* Data to be written */
- DWORD sector, /* Sector address in LBA */
- UINT count /* Number of sectors to write */
- )
- {
- uint8_t res=0;
- if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误
- switch(pdrv)
- {
- case EX_FLASH://外部flash
- for(;count>0;count--)
- {
- W25QXX_Write((uint8_t*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
- sector++;
- buff+=FLASH_SECTOR_SIZE;
- }
- res=0;
- break;
- default:
- res=1;
- }
- //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
- if(res == 0x00)return RES_OK;
- else return RES_ERROR;
- }
- #endif
- //其他表参数的获得
- //pdrv:磁盘编号0~9
- //ctrl:控制代码
- //*buff:发送/接收缓冲区指针
- #if _USE_IOCTL
- DRESULT disk_ioctl (
- BYTE pdrv, /* Physical drive nmuber (0..) */
- BYTE cmd, /* Control code */
- void *buff /* Buffer to send/receive control data */
- )
- {
- DRESULT res;
- if(pdrv==EX_FLASH) //外部FLASH
- {
- switch(cmd)
- {
- case CTRL_SYNC:
- res = RES_OK;
- break;
- case GET_SECTOR_SIZE:
- *(WORD*)buff = FLASH_SECTOR_SIZE;
- res = RES_OK;
- break;
- case GET_BLOCK_SIZE:
- *(WORD*)buff = FLASH_BLOCK_SIZE;
- res = RES_OK;
- break;
- case GET_SECTOR_COUNT:
- *(DWORD*)buff = FLASH_SECTOR_COUNT;
- res = RES_OK;
- break;
- default:
- res = RES_PARERR;
- break;
- }
- }else res=RES_ERROR;//其他的不支持
- return res;
- }
- #endif
- //获得时间
- //User defined function to give a current time to fatfs module */
- //31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
- //15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
- DWORD get_fattime (void)
- {
- return 0;
- }
[color=rgb(51, 102, 153) !important]复制代码
1
|
|
|
|