X-Git-Url: https://hackdaworld.org/gitweb/?a=blobdiff_plain;f=betty%2Flpcload.c;h=6c167e27082cfd355f1b47e884ed2f67e4e213b7;hb=50d38dd59a332ed9378386f89807a56eaa90c101;hp=6ec118628d38eb537e43662cc408256159427bce;hpb=14acad803dec0c6e18b1825f2b84e9bbcd409554;p=my-code%2Farm.git diff --git a/betty/lpcload.c b/betty/lpcload.c index 6ec1186..6c167e2 100644 --- a/betty/lpcload.c +++ b/betty/lpcload.c @@ -19,11 +19,12 @@ #define VERBOSE (1<<0) #define FIRMWARE (1<<1) +#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,7 +59,6 @@ typedef struct s_lpc { char fwfile[128]; /* firmware file */ u8 info; /* info/mode */ char freq[8]; /* frequency */ - u32 hoff; /* start addr of ihex file */ u32 roff; /* ram offset of uc */ } t_lpc; @@ -93,8 +93,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 @@ -112,6 +112,10 @@ int open_serial_device(t_lpc *lpc) { //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL); term.c_iflag&=~(INLCR|ICRNL|IXANY); term.c_iflag|=(IXON|IXOFF); + + // output options + + term.c_oflag=0; // more control options -> timeout / flow control @@ -127,9 +131,6 @@ int open_serial_device(t_lpc *lpc) { int open_firmware(t_lpc *lpc) { - int ret; - char buf[BUFSIZE]; - /* open firmware file */ lpc->fwfd=open(lpc->fwfile,O_RDONLY); @@ -137,16 +138,6 @@ int open_firmware(t_lpc *lpc) { if(lpc->fwfd<0) perror("fw open"); - /* read hex file offset */ - - ret=read(lpc->fwfd,buf,7); - if(buf[0]!=':') { - printf("fw open: not an intel hex file?\n"); - return -1; - } - sscanf(buf+3,"%04x",&(lpc->hoff)); - lseek(lpc->fwfd,0,SEEK_SET); - return lpc->fwfd; } @@ -247,6 +238,11 @@ int txrx(t_lpc *lpc,char *buf,int len,u8 type) { } buf[cnt+1]='\0'; + /* return if type is go */ + + if(type==TXRX_TYPE_GO) + return 0; + /* check/strip return code if type is cmd */ if(type==TXRX_TYPE_CMD) { @@ -317,7 +313,7 @@ int go(t_lpc *lpc) { snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->roff); len=strlen(buf); - ret=txrx(lpc,buf,ret,TXRX_TYPE_CMD); + ret=txrx(lpc,buf,len,TXRX_TYPE_GO); return ret; } @@ -349,7 +345,7 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) { } /* make it a multiple of 3 (reason: uuencode) */ - nlen=(len/3+1)*3; + nlen=(!(len%3))?len:((len/3+1)*3); if(nlen>BUFSIZE) { printf("ram write: too much data\n"); return -1; @@ -357,7 +353,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) @@ -376,7 +372,8 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) { while(bcntfwfd,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); @@ -470,12 +463,19 @@ int firmware_to_ram(t_lpc *lpc) { } /* act according to type */ switch(type) { - case 0x03: - /* get cs and ip */ - break; + //case 0x03: + // /* 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 0x04: + lpc->roff=((buf[0]<<24)|(buf[1]<<16)); + break; default: printf("fw to ram: unknown type %02x\n",type); return -1; @@ -489,6 +489,7 @@ int main(int argc,char **argv) { t_lpc lpc; int i; + int ret; /* * initial ... @@ -532,24 +533,32 @@ int main(int argc,char **argv) { if(open_serial_device(&lpc)<0) goto end; - /* open firmware file */ - if(open_firmware(&lpc)<0) - goto end; - /* boot loader init */ printf("boot loader init ...\n"); if(bl_init(&lpc)<0) return -1; + /* quit if there is no hex file to process */ + if(!(lpc.info&FIRMWARE)) { + printf("no firmware -> aborting\n"); + goto end; + } + + /* open firmware file */ + if(open_firmware(&lpc)<0) + goto end; + /* parse intel hex file and write to ram */ printf("write firmware to ram ...\n"); firmware_to_ram(&lpc); /* unlock go cmd */ + printf("unlock go command ...\n"); unlock_go(&lpc); /* go! */ - go(&lpc); + printf("go ...\n"); + ret=go(&lpc); end: close(lpc.sfd);