📄 main.c
字号:
typedef unsigned char uchar;
typedef unsigned int uint;
#include "config.h"
#include "PIN.h"
#include "var.h"
#include "ini.h"
#include "beep.h"
#include "PubFunc.h"
#include "Lcd.h"
#include "key.h"
#include "define.h"
#include "flash.h"
#include "comm.h"
#define RECV_DELAY 1000000
#define DELAY_COUNT 1500 / 100
#define MAX_DATA_LEN 1024
#define PER_DATA 512
#define BYTE_PER_SECTOR_4 4 //32
#define BYTE_PER_SECTOR_32 32 //32
#define APP_BASE_ADDRESS 0x00008000 //0x00008000
#define START_SECTOR (APP_BASE_ADDRESS / 0x1000)
#define BOOT_STR_ADDRESS (APP_BASE_ADDRESS - 0x200) // 0x00005E00 //0x00007E00
#define WRITE_STR_LEN 256
#define BOOT_STR_LEN 8
#define SLEEP_MS 10
#define SLEEP_TIMES 100
void loader(void) ;
void jump_app(unsigned int) ;
int rcvData(void) ;
void sendData(unsigned char , unsigned char * , unsigned char ) ;
int CompBootStr(void) ;
int writeBootStr(void) ;
unsigned char bootBuf[WRITE_STR_LEN + 4] ;
unsigned int bootStr ;
int main (void)
{
int sleep_count = 0 ;
mcu_init();
ClearBeep();
TurnBacklight(1);
LCDInit();
InitIAP() ;
ClearScreen();
DisplayStr(0,0,8,"SmarTec Boot");
Beep(1);
bootStr = (unsigned int) bootBuf + (4 - (unsigned int) bootBuf % 4) ;
((unsigned char *)bootStr)[0] = '\x12' ;
((unsigned char *)bootStr)[1] = '\x34' ;
((unsigned char *)bootStr)[2] = '\x56' ;
((unsigned char *)bootStr)[3] = '\x78' ;
((unsigned char *)bootStr)[4] = '\x90' ;
((unsigned char *)bootStr)[5] = '\x12' ;
((unsigned char *)bootStr)[6] = '\x34' ;
((unsigned char *)bootStr)[7] = '\x56' ;
#if 0
while(1)
{
DelayMs(SLEEP_MS) ;
sleep_count ++ ;
if(sleep_count > SLEEP_TIMES)
{
Beep(1) ;
if(CompBootStr() != SUCCESS)
{
loader() ;
}
else
{
jump_app(APP_BASE_ADDRESS) ;
}
}
if(GetKey() == KEY_01)
{
loader();
}
}
#else
memset(bootStr , 0x00 , 16) ;
strcpy(bootStr , "11223344");
writeBootStr() ;
#endif
return 0;
}
void loader(void)
{
ClearScreen();
DisplayStr(0,0,8,"SmarTec Loader");
while(1)
{
if(GetKey() == KEY_03)
{
if(CompBootStr() != SUCCESS)
{
loader() ;
}
else
{
jump_app(APP_BASE_ADDRESS) ;
}
}
if(rcvData() != SUCCESS)
{
Beep(2) ;
ClearScreen();
DelayMs(2000) ;
DisplayStr(0,0,8,"SmarTec Loader");
continue ;
}
else
{
ClearScreen();
DisplayStr(0,0,8,"Download OK !");
Beep(1) ;
DelayMs(500) ;
ClearScreen();
DisplayStr(0,0,8,"Restarting...");
DelayMs(500) ;
jump_app(APP_BASE_ADDRESS) ;
}
}
}
void eraseAppSector()
{
unsigned int address ;
int startSector , endSector ;
//write rom
address = APP_BASE_ADDRESS ;
startSector = APP_BASE_ADDRESS / (4 * 1024 ) ;
endSector = 128 / 4 ; //total end
SelectSector(startSector , endSector) ;
EraseSector(startSector , endSector) ;
}
int CompBootStr(void)
{
if(Compare((unsigned int)BOOT_STR_ADDRESS , bootStr , BOOT_STR_LEN) == SUCCESS)
{
// Beep(1) ;
return(SUCCESS) ;
}
else
{
Beep(1) ;
// aseAppSector() ;
return(ERROR) ;
}
}
int writeBootStr(void)
{
unsigned int address ;
int startSector , endSector ;
address = BOOT_STR_ADDRESS ;
startSector = BOOT_STR_ADDRESS / (4 * 1024 ) ;
endSector = (BOOT_STR_ADDRESS + WRITE_STR_LEN) / (4 * 1024 ) ;
SelectSector(startSector , endSector) ;
EraseSector(startSector , endSector) ;
SelectSector(startSector , endSector) ;
if(RamToFlash(address , bootStr , WRITE_STR_LEN) == SUCCESS)
{
return(SUCCESS) ;
}
else
{
return(ERROR) ;
}
}
int clearBootStr(void)
{
unsigned int address ;
int startSector , endSector ;
int ret ;
address = BOOT_STR_ADDRESS ;
startSector = BOOT_STR_ADDRESS / (4 * 1024 ) ;
endSector = (BOOT_STR_ADDRESS + WRITE_STR_LEN) / (4 * 1024 ) ;
ret = SelectSector(startSector , endSector) ;
if(ret != SUCCESS)
{
return(ret) ;
}
ret = EraseSector(startSector , endSector) ;
return(ret) ;
}
int rcvData(void)
{
unsigned char ch ;
unsigned char rcvbuf[MAX_DATA_LEN + 4] ;
unsigned int rcv = (unsigned int) rcvbuf + (4 - (unsigned int) rcvbuf % 4) ;
//2 byte head(len) , then 1byte transcode , then 2 byte No , then data ;
unsigned char len_buf[2+1] ;
unsigned char no_buf[2+1] ;
unsigned char end_buf[2+1] ;
unsigned char transCode ;
int bytePerSector ;
int startSector ;
int len ;
unsigned int address ;
unsigned char LRC ;
int i ;
int partNo ;
while(1)
{
if(UART0_GetByte(&ch) != SUCCESS)
{
return(ERROR) ;
}
else if(ch == STX)
{
len = 0 ;
LRC = 0x00 ;
//len buf
if(UART0_GetStr( len_buf , 2 , 0) != SUCCESS)
{
return(ERROR) ;
}
len = ((unsigned int) (len_buf[0])) * 256 + (unsigned int)(len_buf[1]);
//trans code
if(UART0_GetStr(&transCode , 1 , 0) != SUCCESS)
{
return(ERROR) ;
}
//no buf
if(UART0_GetStr(no_buf , 2 , 0) != SUCCESS)
{
return(ERROR) ;
}
partNo = ((unsigned int) (no_buf[0])) * 256 + (unsigned int) no_buf[1] ;
if(UART0_GetStr( (unsigned char *) rcv , len - 3 , 1) != SUCCESS)
{
return(ERROR) ;
}
//ETX , LRC
if(UART0_GetStr(end_buf , 2 , 0) != SUCCESS) //0x03 , LRC
{
return(ERROR) ;
}
#if 0
//LRC
for(i = 2 ; i < len + 2 ; i++)
{
LRC ^= ((unsigned char *)rcv)[i] ;
}
if(LRC != ((unsigned char *)rcv)[rcv + 2 + count + 1] )
{
sendData( ((unsigned char *)rcv)[2] , &((unsigned char *)rcv)[3] , 0x01) ;
}
#endif
address = APP_BASE_ADDRESS + partNo * PER_DATA ;
if(address >= 0x00008000)
{
bytePerSector = BYTE_PER_SECTOR_32 ;
startSector = 8 + (partNo * PER_DATA) / (bytePerSector * 1024) ;
}
else
{
bytePerSector = BYTE_PER_SECTOR_4 ;
startSector = START_SECTOR + (partNo * PER_DATA) / (bytePerSector * 1024) ;
}
if(partNo == 0)
{
clearBootStr() ;
}
if(partNo % ((bytePerSector * 1024)/PER_DATA) == 0)
{
if(SelectSector(startSector , startSector) != SUCCESS)
{
ClearScreen() ;
DisplayStr(0,8,8,"here Error1 !");
DelayMs(2000) ;
return(ERROR) ;
}
if(EraseSector(startSector , startSector) != SUCCESS)
{
ClearScreen() ;
DisplayStr(0,8,8,"here Error2 !");
DelayMs(2000) ;
return(ERROR) ;
}
}
if(SelectSector(startSector , startSector) != SUCCESS)
{
ClearScreen() ;
DisplayStr(0,8,8,"Select Error !");
DelayMs(1000) ;
continue ;
}
if(RamToFlash(address , rcv , PER_DATA) == SUCCESS)
{
sendData( transCode , no_buf , 0x00) ;
}
else
{
sendData( transCode , no_buf , 0x02) ;
ClearScreen() ;
DisplayStr(0,8,8,"Flash Error !");
DelayMs(2000) ;
continue ;
}
if(transCode == 0x02)
{
//write rom
if(writeBootStr() != SUCCESS)
{
return(ERROR) ;
}
else
{
return(SUCCESS) ;
}
}
else //not finished ...
{
continue ;
}
}
else //not STX
{
continue ;
}
}
}
void sendData(unsigned char transCode , unsigned char *no , unsigned char respCode)
{
unsigned char sndBuf[10 + 1] ;
int i ;
unsigned char LRC = 0x00 ;
sndBuf[0] = 0x02 ;
sndBuf[1] = 0x00 ;
sndBuf[2] = 0x04 ;
sndBuf[3] = transCode ;
sndBuf[4] = no[0] ;
sndBuf[5] = no[1] ;
sndBuf[6] = respCode ;
sndBuf[7] = 0x03 ;
for(i = 1 ; i < 8 ; i++)
{
LRC ^= sndBuf[i] ;
}
sndBuf[8] ^= LRC ;
UART0_SendStr(sndBuf , 9) ;
}
void jump_app(unsigned int address)
{
JumpTo(address);
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -