X-Git-Url: https://hackdaworld.org/gitweb/?a=blobdiff_plain;f=betty%2Flpcload.c;h=19d5a5b2904aebc9dcdc242090ecd506331b4c95;hb=ed474ffa00e2e35babccb09d039a7a65939097a0;hp=80af804153dc5697da32ba6ae3f30a598cfcd45d;hpb=d1ece6f866fffde0a4322a3819056fce3d4123b8;p=my-code%2Farm.git diff --git a/betty/lpcload.c b/betty/lpcload.c index 80af804..19d5a5b 100644 --- a/betty/lpcload.c +++ b/betty/lpcload.c @@ -18,12 +18,24 @@ #define VERBOSE (1<<0) #define FIRMWARE (1<<1) +#define BANK0 (1<<2) +#define BANK2 (1<<3) +#define BL (1<<4) +#define BANK0_ADDR 0x80000000 +#define BANK2_ADDR 0x82000000 +#define BANK_SIZE 0x00100000 +#define BL_ADDR 0x7fffe000 +#define BL_SIZE 0x00002000 + +#define CMD_READ 'R' // stay compatible to fwflash! + +#define TXRX_TYPE_SYNC 0x00 +#define TXRX_TYPE_CKSM 0x00 #define TXRX_TYPE_BAUD 0x01 -#define TXRX_TYPE_SYNC 0x02 -#define TXRX_TYPE_CMD 0x03 -#define TXRX_TYPE_DATA 0x04 -#define TXRX_TYPE_CKSM 0x05 +#define TXRX_TYPE_CMD 0x02 +#define TXRX_TYPE_DATA 0x03 +#define TXRX_TYPE_GO 0x04 #define CMD_SUCCESS "0\r\n" #define INVALID_COMMAND "1\r\n" @@ -58,17 +70,24 @@ typedef struct s_lpc { char fwfile[128]; /* firmware file */ u8 info; /* info/mode */ char freq[8]; /* frequency */ - u32 hoff; /* start addr of ihex file */ + char bank0[127]; /* flash dump bank0 */ + int b0fd; /* dumpfile fd bank0 */ + char bank2[127]; /* flash dump bank2 */ + int b2fd; /* dumpfile fd bank0 */ + char bl[127]; /* flash dump bootloader */ + int blfd; /* dumpfile fd bootloader */ u32 roff; /* ram offset of uc */ + u32 jaddr; /* addr for the jump */ } t_lpc; void usage(void) { printf("possible argv:\n"); - printf(" -d \n"); - printf(" -f \n"); - printf(" -c \n"); - printf(" -r \n"); + printf(" -d \n"); + printf(" -f \n"); + printf(" -c \n"); + printf(" -Dx \n"); + printf(" x=0: bank0, x=2: bank2, x=b: bootloader\n"); printf(" -v\n"); } @@ -93,8 +112,8 @@ int open_serial_device(t_lpc *lpc) { // input/output baudrate - cfsetispeed(&term,B9600); - cfsetospeed(&term,B9600); + cfsetispeed(&term,B38400); + cfsetospeed(&term,B38400); // control options -> 8n1 @@ -109,7 +128,6 @@ int open_serial_device(t_lpc *lpc) { // input options -> enable flow control - //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL); term.c_iflag&=~(INLCR|ICRNL|IXANY); term.c_iflag|=(IXON|IXOFF); @@ -120,19 +138,39 @@ int open_serial_device(t_lpc *lpc) { // more control options -> timeout / flow control term.c_cc[VMIN]=0; - term.c_cc[VTIME]=40; // 4 second timeout - term.c_cc[VSTART]=0x11; - term.c_cc[VSTOP]=0x13; + term.c_cc[VTIME]=20; // 2 seconds timeout + //term.c_cc[VSTART]=0x11; + //term.c_cc[VSTOP]=0x13; tcsetattr(lpc->sfd,TCSANOW,&term); return lpc->sfd; } -int open_firmware(t_lpc *lpc) { +int reconfig_serial_device(t_lpc *lpc) { + struct termios term; int ret; - char buf[BUFSIZE]; + + /* reconfigure the serial device for our lousy loader tool */ + + tcgetattr(lpc->sfd,&term); + + // disable flow control + + term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL); + + // change baudrate + + cfsetispeed(&term,B115200); + cfsetospeed(&term,B115200); + + ret=tcsetattr(lpc->sfd,TCSANOW,&term); + + return ret; +} + +int open_firmware(t_lpc *lpc) { /* open firmware file */ @@ -141,19 +179,40 @@ int open_firmware(t_lpc *lpc) { if(lpc->fwfd<0) perror("fw open"); - /* read hex file offset */ + return lpc->fwfd; +} - ret=read(lpc->fwfd,buf,7); - if(buf[0]!=':') { - printf("fw open: not an intel hex file?\n"); - return -1; +int open_dumpfiles(t_lpc *lpc) { + + /* open dumpfiles */ + + if(lpc->info&BANK0) { + lpc->b0fd=open(lpc->bank0,O_WRONLY|O_CREAT); + if(lpc->b0fd<0) { + perror("bank0 dump file open"); + return lpc->b0fd; + } } - sscanf(buf+3,"%04x",&(lpc->hoff)); - lseek(lpc->fwfd,0,SEEK_SET); - return lpc->fwfd; -} + if(lpc->info&BANK2) { + lpc->b2fd=open(lpc->bank2,O_WRONLY|O_CREAT); + if(lpc->b2fd<0) { + perror("bank2 dump file open"); + return lpc->b2fd; + } + } + + if(lpc->info&BL) { + lpc->blfd=open(lpc->bl,O_WRONLY|O_CREAT); + if(lpc->blfd<0) { + perror("bootloader dump file open"); + return lpc->blfd; + } + } + + return 0; +} int txrx(t_lpc *lpc,char *buf,int len,u8 type) { int ret,cnt; @@ -185,6 +244,8 @@ int txrx(t_lpc *lpc,char *buf,int len,u8 type) { printf("| (%d)\n",cnt); } + + /* cut the echo if not of type auto baud */ if(type!=TXRX_TYPE_BAUD) { @@ -198,6 +259,11 @@ int txrx(t_lpc *lpc,char *buf,int len,u8 type) { } } + /* return if type is go */ + + if(type==TXRX_TYPE_GO) + return cnt; + /* return here if type is data */ if(type==TXRX_TYPE_DATA) @@ -319,9 +385,9 @@ int go(t_lpc *lpc) { char buf[BUFSIZE]; int ret,len; - snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->roff); + snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->jaddr); len=strlen(buf); - ret=txrx(lpc,buf,len,TXRX_TYPE_CMD); + ret=txrx(lpc,buf,len,TXRX_TYPE_GO); return ret; } @@ -361,7 +427,7 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) { for(i=len;iroff-lpc->hoff); + addr+=lpc->roff; /* prepare write command */ if(lpc->info&VERBOSE) @@ -450,10 +516,6 @@ int firmware_to_ram(t_lpc *lpc) { /* read len */ ret=read(lpc->fwfd,buf,2); sscanf(buf,"%02x",&len); - if(len%4) { - printf("fw to ram: len not a multiple of 4\n"); - return -1; - } /* read addr */ ret=read(lpc->fwfd,buf,4); sscanf(buf,"%04x",&addr); @@ -479,10 +541,18 @@ int firmware_to_ram(t_lpc *lpc) { // /* get cs and ip */ // break; case 0x00: + if(len%4) { + printf("fw to ram: invalid len\n"); + return -1; + } write_to_ram(lpc,buf,addr,len); break; - case 0x01: - write_to_ram(lpc,buf,addr,len); + case 0x04: + lpc->roff=((buf[0]<<24)|(buf[1]<<16)); + break; + case 0x05: + lpc->jaddr=((buf[0]<<24)|(buf[1]<<16)); + lpc->jaddr|=((buf[2]<<8)|buf[3]); break; default: printf("fw to ram: unknown type %02x\n",type); @@ -493,11 +563,89 @@ int firmware_to_ram(t_lpc *lpc) { return 0; } +int lpc_txbuf_flush(t_lpc *lpc) { + + int i,ret; + u8 buf[16]; + + ret=1; + printf("flushing lpc tx buffer: "); + while(ret) { + ret=read(lpc->sfd,buf,16); + for(i=0;i>24)&0xff; + buf[2]=(addr>>16)&0xff; + buf[3]=(addr>>8)&0xff; + buf[4]=addr&0xff; + buf[5]=(len>>24)&0xff; + buf[6]=(len>>16)&0xff; + buf[7]=(len>>8)&0xff; + buf[8]=len&0xff; + printf(" sending cmd: "); + while(size) { + ret=write(sfd,buf+cnt,size); + for(i=cnt;i