在一个ucosii线程运行测试fatfs的测试程序,总是莫名其妙的卡死,用jlink跟进了一下,系统运行一遍fatfs的测试程序后,进入了hardfault.麻烦大家帮助一下,感激不尽啊。
下面是测试fatfs的代码
int test_fatfs(void)
{
u32 total,free;
u8 res=0;
u16 tmp = 0;
DIR dir;
FATFS *pfs;
DWORD fre_clust, fre_sect, tot_sect;
FIL fnew; /* 文件对象 */
FRESULT res_flash; /* 文件操作结果 */
UINT fnum,size; /* 文件成功读写数量 */
char *writeF = "abcdefghijklmno";
static char readbuffer[1000];
//==============================================================================
printf("
******** 文件定位和读写测试 ********
");
res_flash = f_open(&fnew, "2:FatFs_test2.txt",FA_WRITE );
tmp = f_size(&fnew); printf("文件大小:tmp=%d
",tmp);
res_flash = f_lseek(&fnew,0);
f_write(&fnew,writeF,strlen(writeF),&fnum);//向文件中写入数据
f_sync(&fnew);
f_close(&fnew);
res_flash = f_open(&fnew, "2:FatFs_test2.txt",FA_READ );
res_flash = f_lseek(&fnew,0);
size = f_size(&fnew);
res_flash = f_read(&fnew,readbuffer,10,&fnum);//读取文件所有内容到缓存区
readbuffer[fnum] = ' ';
printf("读取的文件大小:%d+++%d
",size,fnum);
if(res_flash == FR_OK)
{
printf("文件内容:%s###
",readbuffer);
}
f_close(&fnew);
}
运行上述函数的线程的代码是:
static void msc_task(void* pdata)
{
static u8 cntr100ms=0;
static u8 cntr500ms=0;
static u8 cntr1000ms=0;
while(1)
{
cntr1000ms++;//1000ms任务.
if(cntr1000ms>=20)
{
cntr1000ms = 0;
test_fatfs(); //debug
}
OSTimeDlyHMSM(0,0,0,50);//延时50ms
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>