fatfs R0.09移植,卡了我半天,fatf_getfree传递drv参数一定要加:(冒号)

2019-08-14 15:42发布

本帖最后由 EDA3rd 于 2017-8-13 17:40 编辑

移植原子的fatfs实验,版本的R0.09a,SD卡能正常读取容量,SPI flash读取到的容量居然和SD一样,都是32G:

[mw_shl_code=c,true]
if(exfuns_init()){
printf("exfuns_init failed...");
while(1);
};        //为fatfs相关变量申请内存        
f_mount(0, efs[0]); //挂载SD卡
res=f_mount(1, efs[1]); //挂载FLASH.
printf("res of f_mount = %d ", res);        
if(res==FR_NO_FILESYSTEM)//FLASH磁盘,FAT文件系统错误,重新格式化FLASH
{
LCD_ShowString(30,150,200,16,16,"Flash Disk Formatting...");        //格式化FLASH
printf("Flash Disk Formatting... ");
res=f_mkfs(1, 1, 4096);//格式化FLASH,1,盘符;1,不需要引导区,8个扇区为1个簇
if(res==0)
{
printf("SPI Flash Format Finish! ");
//        f_setlabel((const TCHAR *)"1:ALIENTEK");        //设置Flash磁盘的名字为:ALIENTEK
LCD_ShowString(30,150,200,16,16,"Flash Disk Format Finish");        //格式化完成
}else LCD_ShowString(30,150,200,16,16,"Flash Disk Format Error ");        //格式化失败
delay_ms(1000);
}        
LCD_Fill(30,150,240,150+16,WHITE);        //清除显示        
while(exf_getfree("0",&total,&free))        //得到SD卡的总容量和剩余容量
{
LCD_ShowString(30,150,200,16,16,"SD Card Fatfs Error!");
delay_ms(200);
LCD_Fill(30,150,240,150+16,WHITE);        //清除显示        
delay_ms(200);
LED0=!LED0;//DS0闪烁
}        
POINT_COLOR=BLUE;//设置字体为蓝 {MOD}        
LCD_ShowString(30,150,200,16,16,"FATFS OK!");         
LCD_ShowString(30,170,200,16,16,"SD Total Size: MB");         
LCD_ShowString(30,190,200,16,16,"SD Free Size: MB");
LCD_ShowNum(30+8*14,170,total>>10,5,16);        //显示SD卡总容量 MB
LCD_ShowNum(30+8*14,190,free>>10,5,16);        //显示SD卡剩余容量 MB        
printf("SD card Fatfs OK! ");         
printf("SD card Total Size: %d MB ", total>>10);         
printf("SD card Free Size: %d MB ", free>>10);

while(exf_getfree("1",&total,&free))        //得到flash的总容量和剩余容量
{
LCD_ShowString(30,150,200,16,16,"SPI Flash Fatfs Error!");
delay_ms(200);
LCD_Fill(30,150,240,150+16,WHITE);        //清除显示        
delay_ms(200);
LED0=!LED0;//DS0闪烁
}        
POINT_COLOR=BLUE;//设置字体为蓝 {MOD}        
printf("SPI Flash Fatfs OK! ");         
printf("Flash Total Size: %d MB ", total>>10);         
printf("Flash Free Size: %d MB ", free>>10);
[/mw_shl_code]

查了半天才发现是参数问题,原子的代码默认exf_getfree("0",&total,&free)是不带冒号的,执行exf_getfree会调用chk_mounted函数,chk_mounted开头会判断vol,如果不带冒号,就会赋值vol = 0,所以spi flash读容量得到的是SD卡的容量。

        /* Get logical drive number from the path name */
        vol = p[0] - '0';                                        /* Is there a drive number? */
        if (vol <= 9 && p[1] == ':') {                /* Found a drive number, get and strip it */
                p += 2; *path = p;                                /* Return pointer to the path name */
        } else {                                                        /* No drive number is given */
#if _FS_RPATH
                vol = CurrVol;                                        /* Use current drive */
#else
                vol = 0;                                                /* Use drive 0 */
#endif
        }

        /* Check if the file system object is valid or not */
        *rfs = 0;
        if (vol >= _VOLUMES)                                 /* Is the drive number valid? */
                return FR_INVALID_DRIVE;
        fs = FatFs[vol];                                        /* Get corresponding file system object */
        if (!fs) return FR_NOT_ENABLED;                /* Is the file system object available? */


0条回答

一周热门 更多>