fatfs系统的移植

integer.h     FATFS的数据类型定义(一般不需要更改,其他的文件都需要引用这个文件的内容)

ffcon.h    FATFS的配置文件,配置项的各个参数都需要在这里修改

 

一个细致的讲解fatfs文件系统的原理

https://www.cnblogs.com/amanlikethis/p/3793077.html

 

fatfs(API)函数的使用

FRESULT error;//定义fatfs操作返回结果
    FIL fsrc;   //FIL掌控一个已打开的文件,由f_open()创建,f_close()抛弃
    UINT bytesWritten;
    UINT bytesRead;  
    UINT readBuffer[50];
     error=f_mount(fs[1],"1:",1);                 //挂载FLASH.
    if(!error){
        printf("fs[1] load sd is ok\r\n");
    }else if(error==0X0D){//FLASH磁盘,FAT文件系统错误,重新格式化FLASH
        error=f_mkfs("1:",1,4096);//格式化FLASH,1,盘符;1,不需要引导区,8个扇区为1个簇
        if(error==0){
            printf("Flash Disk Format Finish");    //格式化完成
        }else{
            printf("Flash Disk Format Error ");    //格式化失败
        }
    }else{
        printf("fs[1] load sd err ,the error num is %d\r\n",error);
    }                                                        
#if 0    //这里是txt文件的创立和使用          
                                                      
    error = f_setlabel((const TCHAR *)"1:START");    //设置Flash磁盘的名字为:START
    if(!error){
        printf("set sd's name is ok\r\n");
    }else{
        printf("set sd's name err ,the error num is %d\r\n",error);
    }
    
    
    error = f_mkdir("1:/523");//创立文件夹(创建一次后再次创建会失败)          1:/523  盘符 + 名称        
    if(!error){
        printf("dir create ok\r\n");
    }else{
        printf("dir create err ,the error num is %d\r\n",error);
    }
    
    error = f_open(&fsrc, "1:523.txt",  FA_OPEN_ALWAYS|FA_WRITE|FA_READ  );  //创立txt文件  1:523.txt         FA_OPEN_ALWAYS(打开一个文件,如果不存在就创建该文件)
    if(!error){
        printf("file create ok\r\n");
    }else{
        printf("file create err ,the error num is %d\r\n",error);
    }
    
    error = f_write (&fsrc , "test" , 5 , &bytesWritten); //在 fsrc 文件内写入test字符串
    if(!error){
        printf("write 'test' is ok\r\n");
    }else{
        printf("write 'test' err ,the error num is %d\r\n",error);
    }
    
    error = f_close(&fsrc);    //关闭 fsrc 文件(打开之后必须关闭)
    if(!error){
        printf("close file ok\r\n");
    }else{
        printf("close file err ,the error num is %d\r\n",error);
    }
    
    error = f_open(&fsrc, "1:523.txt",  FA_READ  );  //打开一个可以读取的文件
    if(!error){
        printf("open a text for read is  ok\r\n");
    }else{
        printf("open a text for read err ,the error num is %d\r\n",error);
    }
    
    error = f_read(&fsrc, &readBuffer ,50,   &bytesRead);  //读取一个文件
    if(!error){
        printf("read a text is  ok\r\n");
    }else{
        printf("read a text err ,the error num is %d\r\n",error);
    }
    
    error = f_sync(&fsrc);    //刷新一个缓存
    if(!error){
        printf("crush a text is  ok\r\n");
    }else{
        printf("crush a text err ,the error num is %d\r\n",error);
    }
    
    error = f_rename("1:523.txt","1:524.txt");    //修改一个文件名(oldname  newname--不能跟其他文件名冲突  )
    if(!error){
        printf("rename a text is  ok\r\n");
    }else{
        printf("rename a text err ,the error num is %d\r\n",error);
    }
    
    
    error = f_unlink("1:524.txt");    //删除一个文件
    if(!error){
        printf("delete a text is  ok\r\n");
    }else{
        printf("detele a text err ,the error num is %d\r\n",error);
    }

posted @ 2019-06-06 13:39  大黄蜂_001  阅读(941)  评论(0编辑  收藏  举报