移植FatFs到W25Q256

发布时间 2023-09-29 23:22:56作者: Yannnnnnn

1. 移植diskio.c

diskio.c文件用来连接硬件存储器和ff.c文件

a. 为W25Q256设置驱动编号

/*将W25Q256设为驱动1*/
#define W25Q256		1	/*Map Flash W25Q256 to physical drive 1 */

b.配置disk_status函数,获取W25Q256状态

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
    DSTATUS stat;
    int result;
    switch (pdrv) {
        case ATA :
            return stat;
            /*读取W25Q256状态*/
        case W25Q256 :  
            /*读取W25Q256设备ID,确认状态*/
            if(0xEF4019 == BSP_W25Q256_ReadID())
            {
                /* 设备ID正确 */
                stat = 0;
            }
            else
            {
                /* 设备ID错误 */
                stat = STA_NOINIT;
            }
            result = stat;
            return stat;
    }  
    return STA_NOINIT;
}

 c.连接初始化函数

DSTATUS disk_initialize (
    BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
    DSTATUS stat;
    int result;
    switch (pdrv) {
        case ATA :
            //		result = ATA_disk_initialize();
            // translate the reslut code here
            return stat;
        case W25Q256 :
            BSP_W25Q256_Init();/*初始化W25Q256*/
            stat=disk_status(W25Q256);/*获取W25Q256状态*/
            result = stat;
            return stat;
    }
    return STA_NOINIT;
}

 d.连接读取函数

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 */
)
{
    DRESULT res;
    int result;
    switch (pdrv) {
        case ATA :
            // translate the arguments here
            //		result = ATA_disk_read(buff, sector, count);
            // translate the reslut code here
            return res;
        case W25Q256 :
            /* 扇区偏移16MB,外部Flash文件系统空间放在Flash后面16MB空间 
        一共512Block*16 = 8192 Sector = 32MB
        sector+=4096 表示从第4096个sector开始
        第4096个sector地址:0x4096 000
        第4097个sector地址:0x4097 000
        相差0x1000*/
            sector+=4096;
            /*sector*0x1000 将第几个Sector转换为W25Q256地址
        count*4096 第二个参数是Byte数量,一个Sector有4096个Byte*/
            BSP_W25Q256_Read_Data(sector*0x1000,count*4096 ,buff);
            res = RES_OK;
            return res;
    }
    return RES_PARERR;
}

e.连接写函数

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 */
)
{
    DRESULT res;
    int result;
    switch (pdrv) {
        case ATA :
            // translate the arguments here
            //result = ATA_disk_write(buff, sector, count);
            // translate the reslut code here
            return res;
        case W25Q256 :
            /* 扇区偏移16MB,外部Flash文件系统空间放在Flash后面16MB空间 
        一共512Block*16 = 8192 Sector = 32MB*/
            sector+=4096;
            BSP_W25Q256_Sector_Erase(sector*0x1000);
            BSP_W25Q256_Write_General(sector*0x1000,(u8 *)buff,count*4096);
            res = RES_OK;
            result = res;
            return res;
    }
    return RES_PARERR;
}

f.其他命令函数

DRESULT disk_ioctl (
    BYTE pdrv,		/* Physical drive nmuber (0..) */
    BYTE cmd,		/* Control code */
    void *buff		/* Buffer to send/receive control data */
)
{
    DRESULT res;
    int result;
    switch (pdrv) {
        case ATA :
            // Process of the command for the ATA drive
            return res;
        case W25Q256 :
            switch (cmd) {
                    /* 扇区数量:4096sector*/
                case GET_SECTOR_COUNT:
                    *(DWORD * )buff = 4096;		
                    break;
                    /* 扇区大小 4096Byte */
                case GET_SECTOR_SIZE :
                    *(WORD * )buff = 4096;
                    break;
                    /* 同时擦除扇区个数 */
                case GET_BLOCK_SIZE :
                    *(DWORD * )buff = 1;
                    break;        
            }
            res = RES_OK;
            return res;
    }
    return RES_PARERR;
}

2.配置ffconf.h 配置各种状态

#define	_USE_MKFS		1 //格式化分区使能
#define _CODE_PAGE	    936 //选择简体中文,加入cc936.c
#define	_USE_LFN	    2    //打开LFN
#define	_MAX_LFN	    255
#define _VOLUMES	    2 //两个分卷ATA和W25Q256
#define	_MIN_SS		    512
#define	_MAX_SS		    4096      //sector大小设置

 3.测试FatFs

    {
        printf("\n******FATFS TEST******");
        /*注册一个FatFs工作区域*/
        f_mkfs("1:",0,0);
        if(0==f_mount(&Fs_1,"1:",1))
        {
            printf("\nW25Q256 注册成功");
        }
        else
        {
            printf("\nW25Q256 注册失败");
            printf("\n格式化文件系统");
            /*格式化文件系统*/
            f_mkfs("1:",0,0);
        }
        /*显示芯片ID*/
        printf("\nW25Q256 ID:0X%X",BSP_W25Q256_ReadID());
        /*新建变量保存操作状态*/
        uint8_t FatFsState;
        /*开始新建文件并写入数据*/
        printf("\n开始新建文件并写入数据!");
        /*新建文件File_1.txt,指针File_1*/
        FatFsState = f_open(&File_1, "1:File_1.txt",FA_CREATE_ALWAYS | FA_WRITE );
        printf("\nf_open 1:File_1.txt state:%d",FatFsState);
        /*写入数据*/
        FatFsState = f_write(&File_1,File_1Content,sizeof(File_1Content),&File1Num);
        printf("\nf_write 1:File_1.txt state:%d",FatFsState);
        /*关闭文件*/
        FatFsState = f_close(&File_1);
        printf("\nf_close 1:File_1.txt state:%d",FatFsState);
        /*写入完成*/
        printf("\n写入完成!");
        printf("\nWrite File1 Number:%d",File1Num);
        printf("\nWrite File1 Content:%s",File_1Content);
        /*打开文件*/
        printf("\n打开文件1:File_1.txt!读取内容");
        FatFsState = f_open(&File_1, "1:File_1.txt",FA_OPEN_EXISTING | FA_READ );
        printf("\nf_open 1:File_1.txt state:%d",FatFsState);
        /*读取数据*/
        FatFsState = f_read(&File_1, ReadFile_1Content, sizeof(ReadFile_1Content), &File1Num);
        printf("\nf_read 1:File_1.txt state:%d",FatFsState);
        /*关闭文件*/
        FatFsState = f_close(&File_1);
        printf("\nf_close 1:File_1.txt state:%d",FatFsState);
        /**读取的数据*/
        printf("\n读取完成!");
        printf("\nRead File1 Number:%d",File1Num);
        printf("\nRead File1 Content:%s",ReadFile_1Content);
        /*计算存储空间*/
        DWORD free_clust;
        FATFS *pFs_1;
        f_getfree("1:", &free_clust, &pFs_1);
        DWORD fre_sect, tot_sect;
        tot_sect = (pFs_1->n_fatent - 2) * pFs_1->csize;
        fre_sect = free_clust * pFs_1->csize;
        printf("\n设备总空间:%d\n可用空间:  %d\n", (int)tot_sect, (int)fre_sect);
        f_mount(NULL,"1:",1);
    }

 测试结果: