📄 loader.c
字号:
/* * loader.c -- Target-side bootstrap loader for EP7312 * * Copyright (C) 2004 WangXin <SecureCRT@eyou.com> * * This program is loaded into SRAM in bootstrap mode, where it waits * for commands on UART1 to read and write memory.And write datum from * UART1 ,then write all the datum to Flash. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "ioregs.h"#include "ep7211.h"#include "flash.h"#define BR_115200 1#define BR_57600 3#define BR_38400 5#define BR_19200 11#define BR_9600 23#define BR_2400 95#define BR_1200 191static unsigned char get_char(void){ while (IO_SYSFLG1 & URXFE1); return IO_UARTDR1 & 0xff;}static void drain(void){ while (IO_SYSFLG1 & UTXFF1);}static void put_char(unsigned char c){ drain(); IO_UARTDR1 = c;}static unsigned get_word(void){ unsigned w; w = get_char(); w |= get_char() << 8; w |= get_char() << 16; w |= get_char() << 24; return w;/* w = get_char() << 24; w |= get_char() << 16; w |= get_char() << 8; w |= get_char() ; return w;*/}static void put_word(unsigned w){ put_char(w >> 24); put_char(w >> 16); put_char(w >> 8); put_char(w);/* put_char(w); put_char(w >> 8); put_char(w >> 16); put_char(w >> 24);*/}static void chspeed(int br){ IO_UBRLCR1=IO_UBRLCR1&0xffff0000; switch (br){ case BR_115200 : IO_UBRLCR1=IO_UBRLCR1+BR_115200; break; case BR_57600 : IO_UBRLCR1=IO_UBRLCR1+BR_57600; break; case BR_38400 : IO_UBRLCR1=IO_UBRLCR1+BR_38400; break; case BR_19200 : IO_UBRLCR1=IO_UBRLCR1+BR_19200; break; case BR_9600 : IO_UBRLCR1=IO_UBRLCR1+BR_9600; break; case BR_2400 : IO_UBRLCR1=IO_UBRLCR1+BR_2400; break; case BR_1200 : IO_UBRLCR1=IO_UBRLCR1+BR_1200; break; default: IO_UBRLCR1=IO_UBRLCR1+BR_9600; break; }}static void getfile(unsigned length){int i;for(i=0;i<length;i++) { RAM_BYTE(i)=get_char(); }}static void readflash(int addr,int length){int i;for(i=0;i<length;i++) put_char(FLASH_BYTE(addr+i));}static void writeflash(int addr,int length){int point=0,ba=0,k,j,process=0;ba=addr;for(;ba<addr+length;ba=ba+0x20000) /* every time write a block */ { FLASH_BYTE(ba)=0x60; /* Unlock block */ FLASH_BYTE(ba)=0xD0; while(FLASH_BYTE(ba)!=0x80); FLASH_BYTE(ba)=0x50; /* Clear Status Bit*/ FLASH_BYTE(ba)=0x20; /* Erase Block */ FLASH_BYTE(ba)=0xD0; while(FLASH_BYTE(ba)!=0x80); for(j=0;j<0x20000;j+=32) { if(length-point==0) /* Done*/ { FLASH_BYTE(ba)=0x60; /* Lock the block */ FLASH_BYTE(ba)=0x01; while(FLASH_BYTE(ba)!=0x80); FLASH_BYTE(ba)=0xff; put_char('k'); return; } FLASH_BYTE(ba)=0x50; FLASH_BYTE(ba)=0xE8; while(FLASH_BYTE(ba)!=0x80); if(length-point>=32) { FLASH_BYTE(ba)=0x0F; for(k=0;k<16;k++) { FLASH_WORD(addr+point)=RAM_WORD(point); point=point+2; } FLASH_BYTE(ba+j)=0xD0; while(FLASH_BYTE(ba+j)!=0x80); process=process+32; } else /* the last data*/ { FLASH_BYTE(ba)=0x0F; k=16; while(k!=0) { if(length>point) { FLASH_WORD(addr+point)=RAM_WORD(point); point=point+2; } else { FLASH_WORD(addr+point)=0xFFFF; point=point+2; } k--; } FLASH_BYTE(ba+j)=0xD0; while(FLASH_BYTE(ba+j)!=0x80); FLASH_BYTE(ba)=0x60; FLASH_BYTE(ba)=0x01; while(FLASH_BYTE(ba)!=0x80); FLASH_BYTE(ba)=0xff; put_char('k'); return; } if(process>=1024) { process=0; put_char('.'); } } FLASH_BYTE(ba)=0x60; /* Lock the block */ FLASH_BYTE(ba)=0x01; while(FLASH_BYTE(ba)!=0x80); FLASH_BYTE(ba)=0xff; } FLASH_BYTE(ba)=0x60; /* Lock the block */ FLASH_BYTE(ba)=0x01; while(FLASH_BYTE(ba)!=0x80); FLASH_BYTE(ba)=0xff; put_char('k');}void armsleep(int s){int i;for(i=1;i<s*10000;i++){ i=i-1;i=i+1; }} int main(){ unsigned br; int filelength=0; /* file length */ unsigned address=0; /* defualt address is Flash 0 */ /* Enable the Flash LED */ IO_LEDFLSH=LEDFLSH_ENABLE; /* wait the host give the command */ while(1){ switch (get_char()) { case 'a': /* Ack please */ put_char('a'); /* we're fine, thanks */ break; case 's': /* Set speed */ put_char('s'); /* which speed ? */ br=get_word(); /* 4 bytes */ put_word(br); armsleep(10); chspeed(br); put_char('o'); break; case 'd': /* Set write address */ put_char('d'); address=get_word(); put_char('o'); break; case 'l': /* Set file length and get the file data */ put_char('l'); filelength=(int)get_word(); put_char('o'); break; case 'f': put_char('f'); getfile(filelength); put_char('o'); break; case 'w': put_char('w'); writeflash(address,filelength); break; case 'r': /* read data from flash */ readflash(address,filelength); break; default: put_char('?'); break; } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -