下面的代码,是在ucosii以及emwin下,实现一个frame,然后判断U盘是否挂载好,最后将flash上的数据写入到U盘中。
但是点击按钮写入1次,我点击3次,便会出现HardFault。
调试后发现:CFSR 00 00 82 00,HFSR 40 00 00 00
查手册,发现CFSR寄存器对应的错误为BFARVALID以及PRECISERR
我只知道大概是这段代码引起的错误,但是却不知道具体的问题,我把代码贴上来,请各位大神瞅瞅
[mw_shl_code=c,true]//u盘写入
void Write_usb(void)
{
FIL fsrc;
FIL fusb;
FRESULT ret;
UINT bw ;
UINT br ;
u8 buf[50];
u8 i=0;
// OS_CPU_SR cpu_sr=0;
nber = 0;
dcm10 = 0;
dcm100 = 0;
ret = f_open(&fsrc,sfname,FA_READ); //读取flash 的数据 //打开flash //FR_NOT_ENOUGH_CORE
if(ret==FR_OK) //flash 文件打开成功
{
ret = f_open(&fusb,suname,FA_CREATE_NEW | FA_WRITE); //在U盘上新建文件夹
while (ret!=FR_OK)
{
nber=nber+1; //当nber加到255后怎么办
if(nber==0xFF)break;
if(nber%100)
{
if(nber%10)
{
i=nber%10;
suname[7]=i+0x30;
}
else
{
dcm10=dcm10+1;
suname[6]=dcm10+0x30;
suname[7]=0x30;
if(dcm10>8)
dcm10=0xFF;
}
}
else
{
dcm100=dcm100+1;
suname[5]=dcm100+0x30;
suname[6]=0x30;
suname[7]=0x30;
if(dcm100>8)
dcm100=0xFF;
}
ret = f_open(&fusb,suname,FA_CREATE_NEW | FA_WRITE);
}
if(ret==FR_OK) //如果新建成功 便进行写入
{
while(1)
{
// OS_ENTER_CRITICAL(); //进入临界区,关闭中断
f_read(&fsrc,buf,30,&br);
if(br>0)
{
for(i=0;i<30;i++)
{
if(buf==0x20) buf=',';
}
f_write(&fusb,buf,br,&bw);
}
else
{
TEXT_SetText(Text_2,suname);
TEXT_SetText(Text_3,"Save Success"); // Show_Str(30,250,200,16,"数据已经输完",16,0);
break;
}
}
// OS_EXIT_CRITICAL(); //退出临界区,开中断
f_close(&fusb); //关闭优盘
}
else
TEXT_SetText(Text_3,"New File Failed!");
f_close(&fsrc); //关闭flash
}
else
TEXT_SetText(Text_3,"Flash Failed!");
}
//U盘操作界面
void Usb_Interface(void) // usb 显示
{
FRAMEWIN_Handle hFrame;
BUTTON_Handle hButton_TRANSMISSION;
BUTTON_Handle hButton_CLOSE;
TEXT_Handle Text_1;
// TEXT_Handle Text_2;
// OS_CPU_SR cpu_sr=0;
int tem=0;
FRESULT res;
u8 sign=0;
u8 i=0;
char warn_show[5]="/0";
//U盘 界面初始化
hFrame = FRAMEWIN_CreateEx(30, 200, 420,460,WM_HBKWIN ,WM_CF_SHOW,0,14,"USB Flash Disk",0); //桌面窗口 句柄?WM_HBKWIN
FRAMEWIN_SetActive(hFrame,1);
FRAMEWIN_SetTitleHeight(hFrame,30);
FRAMEWIN_SetFont(hFrame,&GUI_FontComic24B_ASCII);
// FRAMEWIN_SetText(hFrame,"USB Flash Disk");
Text_1 = TEXT_CreateAsChild(0,30,420,100,hFrame,17,WM_CF_SHOW,"",TEXT_CF_HCENTER|TEXT_CF_VCENTER);
TEXT_SetFont(Text_1,&GUI_FontIcpFont48B);
Text_2 = TEXT_CreateAsChild(0,30,420,300,hFrame,18,WM_CF_SHOW,"",TEXT_CF_HCENTER|TEXT_CF_VCENTER);
TEXT_SetFont(Text_2,&GUI_FontIcpFont48B);
Text_3 = TEXT_CreateAsChild(0,30,420,500,hFrame,19,WM_CF_SHOW,"",TEXT_CF_HCENTER|TEXT_CF_VCENTER);
TEXT_SetFont(Text_3,&GUI_FontIcpFont48B);
hButton_CLOSE = BUTTON_CreateAsChild(320, 350, 80, 70, hFrame, 16, WM_CF_SHOW);
BUTTON_SetFont(hButton_CLOSE,&GUI_FontComic24B_ASCII);
BUTTON_SetText(hButton_CLOSE,"Exit");
while(1)
{
GUI_TOUCH_Exec();
tem=GUI_GetKey();
USBH_Process(&USB_OTG_Core, &USB_Host);
if(HCD_IsDeviceConnected(&USB_OTG_Core)) //插U盘
{
if(usb_flag==1)
{
TEXT_SetText(Text_1,"USB Connected");
if(sign==0)
{
// TEXT_SetText(Text_2,"");
// TEXT_SetText(Text_3,"");
hButton_TRANSMISSION = BUTTON_CreateAsChild(20, 350, 80, 70, hFrame, 15, WM_CF_SHOW);
BUTTON_SetFont(hButton_TRANSMISSION,&GUI_FontComic24B_ASCII);
BUTTON_SetText(hButton_TRANSMISSION,"Save");
sign=1;
}
if(tem==15) //SEND
{
Sound(50);
Write_usb();
}
}
}
else //未插U盘
{
if(sign==1)
{
BUTTON_Delete(hButton_TRANSMISSION);
// GUI_Clear();
sign = 0;
}
TEXT_SetText(Text_1,"USB Unconnected");
TEXT_SetText(Text_2,"");
TEXT_SetText(Text_3,"");
}
if(tem==16) //Exit
{
Sound(50);
// res=f_mount(0,"2:",1); //卸载 U 盘
// if(res==FR_OK)
// {
FRAMEWIN_Delete(hFrame);// WM_SetDesktopColor P292 桌面窗口 删除控件以后 需要重绘
WM_SetDesktopColor(GUI_DBLUE);
GUI_Delay(1); //为了刷新界面 也让ICP能够显示 一定是放這裡
GUI_SetFont(&GUI_FontICPfont150);
GUI_DispStringAt("ICP",60,10);
if(data[0]=='a')
Wave_init();
GUI_SetColor(GUI_WHITE);
GUI_SetFont(&GUI_Font24B_1);
sprintf(warn_show,"%d",warn_high); //显示high
GUI_DispStringAt("Top Limit: ",100,470); //11 121
GUI_DispStringAtCEOL(warn_show,205,470); //
sprintf(warn_show,"%d",warn_low); //显示low
GUI_DispStringAt("Lower Limit: ",280,470); //13 240
GUI_DispStringAtCEOL(warn_show,405,470); // 365
break;
// }
}
// OS_ENTER_CRITICAL();
GUI_Delay(10);
// OS_EXIT_CRITICAL();
}
}[/mw_shl_code]
一周热门 更多>