📄 flash0.c
字号:
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051F020.h>
#include <Uart.h>
#include <Const.h>
#include <Delay.h>
#include <FLASH.h>
#include <key.h>
WORD Flash_PHead,Flash_PEnd,Flash_Pointer,PCFlash_Pointer;
extern X_BYTE ADC_Buffer[35][2];
extern unsigned char code HEXStr[16];
bit bFlash_Save_Timer_Flag,bNeed_Update_Flag;
bit bFlash_Work_Flag;
bool Flash_Init(void)
{
bFlash_Save_Timer_Flag=0;
bNeed_Update_Flag=1;
Flash_PHead=0;
Flash_PEnd=0;
Flash_Pointer=0;
PCFlash_Pointer=0;
if(Flash_IndexRange_Update())
{
bFlash_Work_Flag=1;
#ifdef DBUG
Uart1_SendString("Test Flash Communication Succeed!\r\n");
#endif
return true;
}
bFlash_Work_Flag=0;
#ifdef DBUG
Uart1_SendString("Test Flash Communication Fail!\r\n");
#endif
return false;
}
bool Flash_Write_Data(void)
{
R_BYTE i;
FLEXI_INT16 data temp;
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
temp=Flash_Write_Check_He();
Uart1_SendByte(0xaa);
Uart1_SendByte(0x55);
Uart1_SendByte('W');
Uart1_SendByte(temp.chars.c1);
Uart1_SendByte(temp.chars.c0);
i=Uart1_ReceiveData(); // Receive data
if (i!=0x55)
return false;
i=Uart1_ReceiveData();
if (i!=0xaa)
return false;
i=Uart1_ReceiveData();
if (i!='W')
return false;
i=Uart1_ReceiveData();
if (i=='S')
{
return true;
}
else
return false;
}
bool Flash_Read_Data(WORD index)
{
R_BYTE i,temp;
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
Uart1_SendByte(0xaa); // Send read data command
Uart1_SendByte(0x55);
Uart1_SendByte('R');
Uart1_SendByte(index/256);
Uart1_SendByte(index%256);
temp=Uart1_ReceiveData(); // Receive data
if (temp!=0x55)
return false;
temp=Uart1_ReceiveData();
if (temp!=0xaa)
return false;
temp=Uart1_ReceiveData();
if (temp!='R')
return false;
for(i=0; i<4; i++) //read check data
temp=Uart1_ReceiveData();
return true;
}
bool Flash_IndexRange_Update(void)
{
R_BYTE temp;
R_WORD Check_CV;
R_WORD Check_RV;
WDT_CLR;
Uart1_SendByte(0xaa); //Send read data index command
Uart1_SendByte(0x55);
Uart1_SendByte('I'); //'I'=0x49
temp=Uart1_ReceiveData();//Receive data
if (temp!=0x55)
return false;
temp=Uart1_ReceiveData();
if (temp!=0xaa)
return false;
temp=Uart1_ReceiveData();
if (temp!='I')
return false;
temp=Uart1_ReceiveData();
Check_CV=temp;
Flash_PHead=256*temp;
temp=Uart1_ReceiveData();
Check_CV+=temp;
Flash_PHead+=temp;
temp=Uart1_ReceiveData();
Check_CV+=temp;
Flash_PEnd=256*temp;
temp=Uart1_ReceiveData();
Check_CV+=temp;
Flash_PEnd+=temp;
Check_RV=256*Uart1_ReceiveData();
Check_RV+=Uart1_ReceiveData();
///Check_CV=Flash_PHead+Flash_PEnd;
if (Check_CV!=Check_RV)
return false;
return true;
}
bool Flash_Search_Pointer(void)
{
R_BYTE temp;
R_WORD DataHe,DataHe_A;
//unsigned int data Check_CV;
//FLEXI_INT16 data Check_RV;
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
#ifdef DBUG
Uart1_SendString("DateTime.DataBuf[");
DataHe=0;
for (temp=0; temp<5; temp++)
{
Uart1_SendByte(HEXStr[(DateTime.DataBuf[temp]>>4)&0xf]);
Uart1_SendByte(HEXStr[DateTime.DataBuf[temp]&0x0f]);
Uart1_SendByte(' ');
DataHe+=DateTime.DataBuf[temp];
}
Uart1_SendByte(HEXStr[((DataHe/256)>>4)&0xf]);
Uart1_SendByte(HEXStr[(DataHe/256)&0x0f]);
Uart1_SendByte(' ');
Uart1_SendByte(HEXStr[((DataHe%256)>>4)&0xf]);
Uart1_SendByte(HEXStr[(DataHe%256)&0x0f]);
Uart1_SendString("]\n");
#endif
Uart1_SendByte(0xaa); //Send read data index command
Uart1_SendByte(0x55);
Uart1_SendByte('Q'); //'Q'=0x49
DataHe=0;
Uart1_SendByte(DataHe/256);
Uart1_SendByte(DataHe%256);
temp=Uart1_ReceiveData();//Receive data
if (temp!=0x55)
return false;
temp=Uart1_ReceiveData();
if (temp!=0xaa)
return false;
temp=Uart1_ReceiveData();
if (temp!='Q')
return false;
temp=Uart1_ReceiveData();
Flash_Pointer=temp;
Flash_Pointer*=256;
DataHe_A=temp;
temp=Uart1_ReceiveData();
Flash_Pointer+=temp;
DataHe_A+=temp;
DataHe=Uart1_ReceiveData();
DataHe*=256;
DataHe+=Uart1_ReceiveData();
if (DataHe!=DataHe_A)
return false;
return true;
}
FLEXI_INT16 Flash_Write_Check_He(void)
{
FLEXI_INT16 data temp;
unsigned char data i;
for (i=0; i<35; i++)
{
temp.u0+=ADC_Buffer[i][0];
temp.u0+=ADC_Buffer[i][1];
}
return temp;
}
bool Flash_Proc(void)
{
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
if (bFlash_Save_Timer_Flag)
{
bFlash_Save_Timer_Flag=0x00;
if (!Flash_Write_Data())
{
if (!Flash_Write_Data())
{
return false;
}
}
bNeed_Update_Flag=1;
}
return true;
}
bool Flash_Read_Proc(BYTE Offset)
{
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
if (bNeed_Update_Flag) // Update flash data Start&end pointer
{
Delay_s(1);
if (!Flash_IndexRange_Update())
return false;
bNeed_Update_Flag=0;
Delay_ms(10);
}
if (Offset==Flash_Next)
{
Flash_Pointer++;
if (Flash_Pointer==Flash_PHead)
Flash_Pointer=Flash_PEnd+1;
}
else if (Offset==Flash_Pri)
{
Flash_Pointer--;
if (Flash_Pointer==Flash_PEnd)
Flash_Pointer=Flash_PHead-1;
}
else if (Offset==Flash_Current)
{
Flash_Pointer=Flash_Pointer;
}
else
return false;
#ifdef DBUG
Uart1_SendString("Flash_PHead=[");
Uart1_SendByte(HEXStr[Flash_PHead>>12]);
Uart1_SendByte(HEXStr[(Flash_PHead>>8)&0xf]);
Uart1_SendByte(HEXStr[(Flash_PHead>>4)&0xf]);
Uart1_SendByte(HEXStr[Flash_PHead&0x0f]);
Uart1_SendString("]\n");
Uart1_SendString("Flash_PEnd=[");
Uart1_SendByte(HEXStr[Flash_PEnd>>12]);
Uart1_SendByte(HEXStr[(Flash_PEnd>>8)&0xf]);
Uart1_SendByte(HEXStr[(Flash_PEnd>>4)&0xf]);
Uart1_SendByte(HEXStr[Flash_PEnd&0x0f]);
Uart1_SendString("]\n");
Uart1_SendString("Flash_Pointer=[");
Uart1_SendByte(HEXStr[Flash_Pointer>>12]);
Uart1_SendByte(HEXStr[(Flash_Pointer>>8)&0xf]);
Uart1_SendByte(HEXStr[(Flash_Pointer>>4)&0xf]);
Uart1_SendByte(HEXStr[Flash_Pointer&0x0f]);
Uart1_SendString("]\n");
#endif
if (Flash_Read_Data(Flash_Pointer))
return true;
else
return false;
}
bool Flash_PCRead_Proc(BYTE Offset)
{
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
if (bNeed_Update_Flag) // Update flash data Start&end pointer
{
Delay_s(1);
if (!Flash_IndexRange_Update())
return false;
bNeed_Update_Flag=0;
Delay_ms(10);
}
if (Offset==Flash_Next)
{
PCFlash_Pointer++;
if (PCFlash_Pointer==Flash_PHead)
{
PCFlash_Pointer--;
return false;
}
//PCFlash_Pointer=Flash_PEnd+1;
}
else if (Offset==Flash_Pri)
{
PCFlash_Pointer--;
if (PCFlash_Pointer==Flash_PEnd)
{
PCFlash_Pointer--;
return false;
}
//PCFlash_Pointer=Flash_PHead-1;
}
else if (Offset==Flash_Current)
{
PCFlash_Pointer=PCFlash_Pointer;
}
else
return false;
#ifdef DBUG
Uart1_SendString("Flash_PHead=[");
Uart1_SendByte(HEXStr[Flash_PHead>>12]);
Uart1_SendByte(HEXStr[(Flash_PHead>>8)&0xf]);
Uart1_SendByte(HEXStr[(Flash_PHead>>4)&0xf]);
Uart1_SendByte(HEXStr[Flash_PHead&0x0f]);
Uart1_SendString("]\n");
Uart1_SendString("Flash_PEnd=[");
Uart1_SendByte(HEXStr[Flash_PEnd>>12]);
Uart1_SendByte(HEXStr[(Flash_PEnd>>8)&0xf]);
Uart1_SendByte(HEXStr[(Flash_PEnd>>4)&0xf]);
Uart1_SendByte(HEXStr[Flash_PEnd&0x0f]);
Uart1_SendString("]\n");
Uart1_SendString("Flash_Pointer=[");
Uart1_SendByte(HEXStr[PCFlash_Pointer>>12]);
Uart1_SendByte(HEXStr[(PCFlash_Pointer>>8)&0xf]);
Uart1_SendByte(HEXStr[(PCFlash_Pointer>>4)&0xf]);
Uart1_SendByte(HEXStr[PCFlash_Pointer&0x0f]);
Uart1_SendString("]\n");
#endif
if (Flash_Read_Data(PCFlash_Pointer))
return true;
else
return false;
}
bool Flash_PCSearch_Pointer(void)
{
R_BYTE temp;
R_WORD DataHe,DataHe_A;
WDT_CLR;
if (!bFlash_Work_Flag)
return false;
#ifdef DBUG
Uart1_SendString("DateTime.DataBuf[");
DataHe=0;
for (temp=0; temp<5; temp++)
{
Uart1_SendByte(HEXStr[(DateTime.DataBuf[temp]>>4)&0xf]);
Uart1_SendByte(HEXStr[DateTime.DataBuf[temp]&0x0f]);
Uart1_SendByte(' ');
DataHe+=DateTime.DataBuf[temp];
}
Uart1_SendByte(HEXStr[((DataHe/256)>>4)&0xf]);
Uart1_SendByte(HEXStr[(DataHe/256)&0x0f]);
Uart1_SendByte(' ');
Uart1_SendByte(HEXStr[((DataHe%256)>>4)&0xf]);
Uart1_SendByte(HEXStr[(DataHe%256)&0x0f]);
Uart1_SendString("]\n");
#endif
Uart1_SendByte(0xaa); //Send read data index command
Uart1_SendByte(0x55);
Uart1_SendByte('Q'); //'Q'=0x49
DataHe=0;
Uart1_SendByte(DataHe/256);
Uart1_SendByte(DataHe%256);
temp=Uart1_ReceiveData();//Receive data
if (temp!=0x55)
return false;
temp=Uart1_ReceiveData();
if (temp!=0xaa)
return false;
temp=Uart1_ReceiveData();
if (temp!='Q')
return false;
temp=Uart1_ReceiveData();
PCFlash_Pointer=temp;
PCFlash_Pointer*=256;
DataHe_A=temp;
temp=Uart1_ReceiveData();
PCFlash_Pointer+=temp;
DataHe_A+=temp;
DataHe=Uart1_ReceiveData();
DataHe*=256;
DataHe+=Uart1_ReceiveData();
if (DataHe!=DataHe_A)
return false;
return true;
}
void flash_test(void)
{
// R_BYTE key;
// key=GetScanKey();
// if (key==0x11)//0
// {
if (Flash_IndexRange_Update())
{
#ifdef DBUG
Uart1_SendString("Flash_IndexRange_Update Succeed!\r\n");
Uart1_SendString("Flash_PHead=");
Uart1_SendByte(HEXStr[Flash_PHead/(256*16)]);
Uart1_SendByte(HEXStr[(Flash_PHead%(256*16))/256]);
Uart1_SendByte(HEXStr[(Flash_PHead%256)/16]);
Uart1_SendByte(HEXStr[Flash_PHead%16]);
Uart1_SendString(" Flash_PEnd=");
Uart1_SendByte(HEXStr[Flash_PEnd/(256*16)]);
Uart1_SendByte(HEXStr[(Flash_PEnd%(256*16))/256]);
Uart1_SendByte(HEXStr[(Flash_PEnd%256)/16]);
Uart1_SendByte(HEXStr[Flash_PEnd%16]);
Uart1_SendString("\r\n");
#endif
}
/*
else
{
#ifdef DBUG
Uart1_SendString("Flash_IndexRange_Update fail!\r\n");
#endif
}
}
else if (key==0x21)
{
if (Flash_Search_Pointer())
{
#ifdef DBUG
Uart1_SendString("Flash_Search_Pointer Succeed!\r\n");
#endif
}
else
{
#ifdef DBUG
Uart1_SendString("Flash_Search_Pointer fail!\r\n");
#endif
}
}
else if (key==0x12)
{
if (Flash_Write_Data())
{
#ifdef DBUG
Uart1_SendString("Flash_Write_Data Succeed!\r\n");
#endif
}
else
{
#ifdef DBUG
Uart1_SendString("Flash_Write_Data fail!\r\n");
#endif
}
}
else if (key==0x22)
{
if (Flash_Read_Data(Flash_PHead))
{
#ifdef DBUG
Uart1_SendString("Flash_Read_Data Succeed!\r\n");
#endif
}
else
{
#ifdef DBUG
Uart1_SendString("Flash_Read_Data fail!\r\n");
#endif
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -