OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 12117|回复: 5

在做图片显示实验室FATFS文件系统中f_open()函数的使用问题

[复制链接]

12

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
123
金钱
123
注册时间
2014-9-25
在线时间
3 小时
发表于 2014-10-28 22:29:09 | 显示全部楼层 |阅读模式
5金钱
while(font_init()) //检查字库
{    
LCD_ShowString(60,50,16,"Font Error!");
delay_ms(200);  
LCD_Fill(60,50,240,66,WHITE);//清除显示     
delay_ms(200);  
}    
  while(f_opendir(&picdir,"0:/PICTURE"))//打开图片文件夹//这里是能打开的
  {    
Show_Str(60,170,240,16,"PICTURE文件夹错误!",16,0);
delay_ms(200);  
LCD_Fill(60,170,240,186,WHITE);//清除显示     
delay_ms(200);  
}
totpicnum=pic_get_tnum("0:/PICTURE"); //得到总有效文件数/totpicnum=0
  while(totpicnum==NULL)//图片文件为0
  {    
Show_Str(60,170,240,16,"没有图片文件!",16,0);//液晶上最总运行的结果是没有图片显示,也就是说totpicnum=0,即pic_get_tnum("0:/PICTURE")的返回值为0
delay_ms(200);  
LCD_Fill(60,170,240,186,WHITE);//清除显示     
delay_ms(200);  
}

//得到path路径下,目标文件的总个数
//path:路径    
//返回值:总有效文件数
u16 pic_get_tnum(u8 *path)
{  
u8 res=0;
u16 rval=0;
  DIR tdir; //临时目录
FILINFO tfileinfo; //临时文件信息
u8 *fn;       
    res=f_opendir(&tdir,(const TCHAR*)path); //打开目录//即f_opendir(&tdir,(const TCHAR*)path)的返回值不为FR_OK
  tfileinfo.lfsize=_MAX_LFN*2+1; //长文件名最大长度
tfileinfo.lfname=mymalloc(tfileinfo.lfsize);//为长文件缓存区分配内存
if(res==FR_OK&&tfileinfo.lfname!=NULL)//调试的结果是res不等于FR_OK
{
while(1)//查询总的有效文件数
{
       res=f_readdir(&tdir,&tfileinfo);       //读取目录下的一个文件
       if(res!=FR_OK||tfileinfo.fname[0]==0)break; //错误了/到末尾了,退出  
      fn=(u8*)(*tfileinfo.lfname?tfileinfo.lfname:tfileinfo.fname);  
res=f_typetell(fn);
//Show_Str(60,200,240,16,"测试!",16,0);
if((res&0XF0)==0X50)//取高四位,看看是不是图片文件
{
rval++;//有效文件数增加1
}    
}  

return rval;
}

FRESULT f_opendir (
DIR* dp, /* Pointer to directory object to create */
const TCHAR* path /* Pointer to the directory path */
)
{
FRESULT res;
FATFS* fs;
DEF_NAMEBUF;


if (!dp) return FR_INVALID_OBJECT;

/* Get logical drive number */
res = find_volume(&fs, &path, 0);
if (res == FR_OK) {
dp->fs = fs;
INIT_BUF(*dp);
res = follow_path(dp, path); /* Follow the path to the directory */
FREE_BUF();
if (res == FR_OK) { /* Follow completed */
if (dp->dir) { /* It is not the origin directory itself */
if (dp->dir[DIR_Attr] & AM_DIR) /* The object is a sub directory */
dp->sclust = ld_clust(fs, dp->dir);
else /* The object is a file */
res = FR_NO_PATH;
}
if (res == FR_OK) {
dp->id = fs->id;
res = dir_sdi(dp, 0); /* Rewind directory *///调试发现dir_sdir函数的返回值不为FR_OK
#if _FS_LOCK
if (res == FR_OK) {
if (dp->sclust) {
dp->lockid = inc_lock(dp, 0); /* Lock the sub directory */
if (!dp->lockid)
res = FR_TOO_MANY_OPEN_FILES;
} else {
dp->lockid = 0; /* Root directory need not to be locked */
}
}
#endif
}
}
if (res == FR_NO_FILE) res = FR_NO_PATH;
}
if (res != FR_OK) dp->fs = 0; /* Invalidate the directory object if function faild */
LEAVE_FF(fs, res);
}

/*-----------------------------------------------------------------------*/
/* Directory handling - Set directory index                              */
/*-----------------------------------------------------------------------*/
//去掉static方便其他地方调用
FRESULT dir_sdi (
DIR* dp, /* Pointer to directory object */
UINT idx /* Index of directory table */
)
{
DWORD clst, sect;
UINT ic;
dp->index = (WORD)idx; /* Current index */
clst = dp->sclust; /* Table start cluster (0:root) */
if (clst == 1 || clst >= dp->fs->n_fatent) /* Check start cluster range */
return FR_INT_ERR;
if (!clst && dp->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
clst = dp->fs->dirbase;

if (clst == 0) { /* Static table (root-directory in FAT12/16) */
if (idx >= dp->fs->n_rootdir) /* Is index out of range? */
return FR_INT_ERR;
sect = dp->fs->dirbase;
}
else { /* Dynamic table (root-directory in FAT32 or sub-directory) */
ic = SS(dp->fs) / SZ_DIR * dp->fs->csize; /* Entries per cluster */
while (idx >= ic) { /* Follow cluster chain */
clst = get_fat(dp->fs, clst); /* Get next cluster */
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
if (clst < 2 || clst >= dp->fs->n_fatent) /* Reached to end of table or internal error */
return FR_INT_ERR;
idx -= ic;
}
sect = clust2sect(dp->fs, clst);
}
dp->clust = clst; /* Current cluster# */
if (!sect) return FR_INT_ERR;
dp->sect = sect + idx / (SS(dp->fs) / SZ_DIR); /* Sector# of the directory entry */
dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */

return FR_OK;
}



红字是根据实验结果我自己理解的,如果我把

totpicnum=pic_get_tnum("0:/PICTURE"); //得到总有效文件数
  while(totpicnum==NULL)//图片文件为0
  {    
Show_Str(60,170,240,16,"没有图片文件!",16,0);
delay_ms(200);  
LCD_Fill(60,170,240,186,WHITE);//清除显示     
delay_ms(200);  
}
这部分注释掉直接领totpicnum=1;是能够显示SD卡中PICTURE文件夹中的所有图片的。。

原子哥这是为什么啊?


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-10-28 22:51:35 | 显示全部楼层
一般不会出现这个情况。
唯一可能出问题的地方,就是f_opendir貌似调用了2次,打开同一个文件夹,不知道是不是这个问题。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

12

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
123
金钱
123
注册时间
2014-9-25
在线时间
3 小时
 楼主| 发表于 2014-10-29 10:21:48 | 显示全部楼层
回复【2楼】正点原子:
---------------------------------
我用f_closedir(&dir)关闭上一个打开的文件夹后,再打开这个文件夹还是不行。。
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-10-29 23:06:13 | 显示全部楼层
回复【3楼】小小驴:
---------------------------------
那就不太清楚了,感觉是SD卡问题。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

2

主题

41

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
231
金钱
231
注册时间
2017-10-20
在线时间
56 小时
发表于 2017-11-23 19:55:20 | 显示全部楼层
Stack_Size      EQU     0x00001000
试下把栈改大一点
回复

使用道具 举报

17

主题

51

帖子

0

精华

高级会员

Rank: 4

积分
799
金钱
799
注册时间
2015-11-25
在线时间
126 小时
发表于 2018-12-11 18:27:10 | 显示全部楼层
SD卡问题,我也遇到过,将卡格式化下,我发现在格式化过程中,从第一扇区到10扇区会被写入一些东西(我也不知道是什么),但肯定是别破坏了造成SD卡出问题了,建议先格式化再重新装载图片
不积跬步无以至千里!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-22 13:21

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表