emWin里按下按钮,先关闭中断,对SD操作,返回以后,emWin就触发HardFault_Handler
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
[mw_shl_code=c,true] case ID_BUTTON_4505: // Notifications sent by 'Button'
switch(NCode) {
case WM_NOTIFICATION_CLICKED:
break;
case WM_NOTIFICATION_RELEASED:
hItem = WM_GetDialogItem(hWin, ID_TEXT_4502);
TEXT_SetText(hItem,"Saving Result to SD!");
GUI_Delay(200);
__disable_irq();
if(check_SD()==0)
{
if(write_result_to_SD()==0)
{
hItem = WM_GetDialogItem(hWin, ID_TEXT_4502);
TEXT_SetText(hItem,"Save successfully!");
}
else
{
TEXT_SetText(hItem,"Test result writes error!");
}
}
else
{
TEXT_SetText(hItem,"SD Error");
}
__enable_irq();
GUI_Delay(500);
break;[/mw_shl_code]
下面上面调用的两个函数。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
[mw_shl_code=c,true]int check_SD(void)
{
u32 total,free;
u8 i;
int flag=0;
i=5;
while(i) //检测SD卡
{
if(SD_Initialize())
{
delay_ms(100);
i--;
flag=1;
}
else
i=0;
}
if(flag!=0) return flag;
exfuns_init(); //为fatfs相关变量申请内存
f_mount(fs[0],"0:",1); //挂载SD卡
//f_mount(fs[1],"1:",1); //挂载FLASH.
i=5;
while(i) //得到SD卡的总容量和剩余容量
{
if(exf_getfree("0",&total,&free))
{
delay_ms(200);
i--;
flag=2;
}
else
i=0;
}
if(flag!=0)
{LED_B=1;LED_R=0;return flag; }
else
{
LCD_ShowString(30,180,200,16,16,"SD Test Ok. Free Size: MB");
LCD_ShowNum(220,180,free>>10,5,16); //显示SD卡剩余容量 MB
delay_ms(200);
f_mount(NULL, "", 0);
return flag;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
int write_result_to_SD(void)
{
int flag=0;
uint8_t res;
u8 i,k;
FATFS fs;
uint32_t index = 0x00;
uint32_t reindex = 0x00;
char buf[2000] = "\0";
uint8_t str[15];
char name[50] = "TestData_";
pht_read_test_result();
delay_ms(1000);
RTC_get_date(str,0);
for(i=0;i<10;i++)
name[9+i]=str;
name[19]='_';
RTC_get_time(str,1);
for(i=0;i<6;i++)
name[20+i]=str;
name[26]='.';
name[27]='t';
name[28]='x';
name[29]='t';
name[30]='\0'; //TestData_2016-07-17_202512.dat
res = f_mount(&fs, "", 0);
if(res != FR_OK)
{
LCD_ShowString(30,180,200,16,16,"file mount error!");
flag=1;
delay_ms(1000);
return flag;
}
LCD_ShowString(30,180,200,16,16, "file mount successful!");
delay_ms(1000);
res = f_open(file,(const TCHAR *)name,FA_READ | FA_WRITE | FA_CREATE_NEW );
if(res == FR_OK)
{
LCD_ShowString(30,180,200,16,16, "file open sucessfully!");
delay_ms(1000);
}
else
{
LCD_ShowString(30,180,200,16,16,"file open error!");
flag=2;
delay_ms(1000);
return flag;
}
index=2000;
for(i=0;i<50;i++)
{
sprintf(buf,"%s","--Record-");
if(rec.RecFlag==0x88)
{
sprintf(buf,"%s%2d%s",buf,i,"--\r\n");
sprintf(buf,"%s%s",buf,rec.TestNr);
sprintf(buf,"%s%s",buf,"\r\n");
sprintf(buf,"%s%4d",buf,rec.Pres);
sprintf(buf,"%s%s",buf,"\r\n");
sprintf(buf,"%s%2d",buf,rec.PresDrL);
sprintf(buf,"%s%s",buf,"\r\n");
sprintf(buf,"%s%1d",buf,rec.TDur);
sprintf(buf,"%s%s",buf,"\r\n");
sprintf(buf,"%s%8d%4d",buf,rec.RDate,rec.RTime);
sprintf(buf,"%s%s",buf,"\r\n");
sprintf(buf,"%s%1d",buf,rec.TResult);
sprintf(buf,"%s%s",buf,"\r\n");
sprintf(buf,"%s%s",buf,rec.Tester);
sprintf(buf,"%s%s",buf,"\r\n");
for(k=0;k<21;k++)
sprintf(buf,"%s%4d%s",buf,rec.PresRec[k]," ");
sprintf(buf,"%s%s",buf,"\r\n\r\n\0");
}
else
{
sprintf(buf,"%s%2d%s",buf,i,"--\r\n");
sprintf(buf,"%s%s",buf,"This record is blank!\r\n");
sprintf(buf,"%s%s",buf,"\r\n\0");
}
//index=sizeof(buf)-1;
index=strlen(buf);
res = f_write(file,buf,index, &reindex);
}
if((res == FR_OK)&&(reindex == index))
{
LCD_ShowString(30,180,200,16,16,"file saved successfully!");
delay_ms(1000);
f_close(file);
}
else
{
LCD_ShowString(30,180,200,16,16, "file save failed!");
flag=3;
delay_ms(1000);
f_mount(NULL, "", 0);
return flag;
}
/* 解除 SD 卡的逻辑驱动挂载 */
delay_ms(1000);
f_mount(NULL, "", 0);
return flag;
}[/mw_shl_code]
|