X-Git-Url: https://hackdaworld.org/gitweb/?a=blobdiff_plain;f=betty%2Flpcload.c;h=6c167e27082cfd355f1b47e884ed2f67e4e213b7;hb=50d38dd59a332ed9378386f89807a56eaa90c101;hp=e2bef7337992099544b0c96b6a61556f17fc739d;hpb=f99ed5c4faba98f0afda8d3f90d0481fc70258ee;p=my-code%2Farm.git diff --git a/betty/lpcload.c b/betty/lpcload.c index e2bef73..6c167e2 100644 --- a/betty/lpcload.c +++ b/betty/lpcload.c @@ -1,8 +1,10 @@ /* * lpcload.c - load firmware into ram of lpc2220 via uart0 * - * author: hackbard@hackdaworld.org + * author: hackbard@hackdaworld.org, rolf.anders@physik.uni-augsburg.de * + * build: make + * usage: sudo ./lpcload -d /dev/ttyS0 -f firmware.hex [-v] */ #include @@ -14,12 +16,51 @@ #include #include -#define VERBOSE (1<<0) -#define FIRMWARE (1<<1) - -#define CRYSTFREQ "10000" - -#define BUFSIZE 64 +#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_CMD 0x02 +#define TXRX_TYPE_DATA 0x03 +#define TXRX_TYPE_GO 0x04 + +#define CMD_SUCCESS "0\r\n" +#define INVALID_COMMAND "1\r\n" +#define SRC_ADDR_ERROR "2\r\n" +#define DST_ADDR_ERROR "3\r\n" +#define SRC_ADDR_NOT_MAPPED "4\r\n" +#define DST_ADDR_NOT_MAPPED "5\r\n" +#define COUNT_ERROR "6\r\n" +#define COMPARE_ERROR "10\r\n" +#define BUSY "11\r\n" +#define PARAM_ERROR "12\r\n" +#define ADDR_ERROR "13\r\n" +#define ADDR_NOT_MAPPED "14\r\n" +#define CMD_LOCKED "15\r\n" +#define INVALID_CODE "16\r\n" +#define INVALID_BAUD_RATE "17\r\n" +#define INVALID_STOP_BIT "18\r\n" + +#define CRYSTFREQ "10000" +#define RAMOFFSET 0x40000200 + +#define BUFSIZE 128 + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; + +typedef struct s_lpc { + int sfd; /* serial fd */ + char sdev[128]; /* seriel device */ + int fwfd; /* fimrware fd */ + char fwfile[128]; /* firmware file */ + u8 info; /* info/mode */ + char freq[8]; /* frequency */ + u32 roff; /* ram offset of uc */ +} t_lpc; void usage(void) { @@ -27,92 +68,436 @@ void usage(void) { printf(" -d \n"); printf(" -f \n"); printf(" -c \n"); + printf(" -r \n"); printf(" -v\n"); } -int txrx(int fd,char *buf,int *len,unsigned char v) { +int open_serial_device(t_lpc *lpc) { + + struct termios term; + + //memset(&term,0,sizeof(struct termios)); + + /* open serial device */ + + lpc->sfd=open(lpc->sdev,O_RDWR); + if(lpc->sfd<0) { + perror("tts open"); + return lpc->sfd; + } + + /* configure the serial device */ + + tcgetattr(lpc->sfd,&term); + + // input/output baudrate + + cfsetispeed(&term,B38400); + cfsetospeed(&term,B38400); + + // control options -> 8n1 + + term.c_cflag&=~PARENB; // no parity + term.c_cflag&=~CSTOPB; // only 1 stop bit + term.c_cflag&=~CSIZE; // no bit mask for data bits + term.c_cflag|=CS8; // 8 data bits + + // line options -> raw input + + term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG); + + // input options -> enable flow control + + //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 + + term.c_cc[VMIN]=0; + term.c_cc[VTIME]=10; // 1 second 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) { + + /* open firmware file */ + + lpc->fwfd=open(lpc->fwfile,O_RDONLY); + + if(lpc->fwfd<0) + perror("fw open"); + + return lpc->fwfd; +} + +int txrx(t_lpc *lpc,char *buf,int len,u8 type) { int ret,cnt; int i; /* write */ - if(v&VERBOSE) + if(lpc->info&VERBOSE) printf(" >> "); cnt=0; - while(*len) { - ret=write(fd,buf+cnt,*len); + while(len) { + ret=write(lpc->sfd,buf+cnt,len); if(ret<0) { perror("txrx write"); return ret; } - if(v&VERBOSE) + if(lpc->info&VERBOSE) for(i=0;i0x19)&(buf[cnt+i]<0x7f))? buf[cnt+i]:'.'); - *len-=ret; + len-=ret; cnt+=ret; } - if(v&VERBOSE) - printf(" (%d)\n",cnt); + if(lpc->info&VERBOSE) { + printf(" | "); + for(i=0;isfd,buf,cnt); + if(ret<0) { + perror("txrx echo cut"); + return ret; + } + cnt-=ret; + } + } + + /* return here if type is data */ + + if(type==TXRX_TYPE_DATA) + return cnt; /* read */ - if(v&VERBOSE) - printf(" << "); + ret=read(lpc->sfd,buf,1); + if(ret<0) { + perror("txrx read (first byte)"); + return ret; + } + + switch(buf[0]) { + case 'S': + cnt=13; + break; + case 'O': + cnt=3; + break; + case 'R': + cnt=7; + break; + case '0': + cnt=2; + break; + default: + printf("txrx read: bad return byte '%02x'\n",buf[0]); + break; + } + ret=1; - cnt=0; - while(ret>0) { - ret=read(fd,buf+cnt,BUFSIZE-cnt); + i=cnt; + while(i) { + ret=read(lpc->sfd,buf+1+cnt-i,i); if(ret<0) { - perror("txrx read"); + perror("txrx read (next bytes)"); return ret; } - if(ret+cnt>BUFSIZE) { - printf("txrx read: too small buf size (%d)!\n",BUFSIZE); + i-=ret; + } + if(lpc->info&VERBOSE) { + printf(" << "); + for(i=0;i0x19)&(buf[i]<0x7f))? + buf[i]:'.'); + printf(" | "); + for(i=0;i0x19)&(buf[cnt+i]<0x7f))? - buf[cnt+i]:'.'); - cnt+=ret; } - if(v&VERBOSE) - printf(" (%d)\n",cnt); - *len=cnt; return cnt; } -int main(int argc,char **argv) { +int bl_init(t_lpc *lpc) { - int i; - char tts[128]; - int tts_fd; - char fw[128]; - int fw_fd; - unsigned char info; - struct termios term; - char cfreq[8]; + char buf[BUFSIZE]; int len; + + /* auto baud sequence */ + buf[0]='?'; + txrx(lpc,buf,1,TXRX_TYPE_BAUD); + if(strncmp(buf,"Synchronized\r\n",14)) { + printf("auto baud detection failed\n"); + return -1; + } + + /* tell bl that we are synchronized (it's allready in buf) */ + txrx(lpc,buf,14,TXRX_TYPE_SYNC); + if(strncmp(buf,"OK\r\n",4)) { + printf("sync failed\n"); + return -1; + } + + /* tell bl the crystal frequency */ + len=strlen(lpc->freq)+2; + strncpy(buf,lpc->freq,BUFSIZE); + buf[len-2]='\r'; + buf[len-1]='\n'; + txrx(lpc,buf,len,TXRX_TYPE_SYNC); + if(strncmp(buf,"OK\r\n",4)) { + printf("freq set failed\n"); + return -1; + } + + return 0; +} + +int unlock_go(t_lpc *lpc) { + + char buf[BUFSIZE]; + int ret; + + memcpy(buf,"U 23130\r\n",9); + ret=txrx(lpc,buf,9,TXRX_TYPE_CMD); + + return ret; +} + +int go(t_lpc *lpc) { + + char buf[BUFSIZE]; + int ret,len; + + snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->roff); + len=strlen(buf); + ret=txrx(lpc,buf,len,TXRX_TYPE_GO); + + return ret; +} + +int uuencode(u8 *in,u8 *out,int len) { + + out[0]=0x20+len; + out[1]=0x20+((in[0]>>2)&0x3f); + out[2]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f); + out[3]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f); + out[4]=0x20+(in[2]&0x3f); + + return 0; +} + +int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) { + + int lcount; + u32 checksum; + char txrxbuf[BUFSIZE]; + int count,bcnt; + int nlen,slen; + int i; + + /* check length */ + if(len%4) { + printf("ram write: not a multiple of 4\n"); + return -1; + } + + /* make it a multiple of 3 (reason: uuencode) */ + nlen=(!(len%3))?len:((len/3+1)*3); + if(nlen>BUFSIZE) { + printf("ram write: too much data\n"); + return -1; + } + for(i=len;iroff; + + /* prepare write command */ + if(lpc->info&VERBOSE) + printf("writing 0x%02x bytes to 0x%08x\n",len,addr); + snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len); + slen=strlen(txrxbuf); + + /* send command and check return code */ + txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD); + + /* send data */ + lcount=0; + bcnt=0; + count=0; + checksum=0; + while(bcntsfd,txrxbuf+4,4); + printf("ram write: resending ...\n"); + bcnt-=count; + } + if(strncmp(txrxbuf,"OK\r\n",4)) { + printf("ram write: bad response\n"); + return -1; + } + /* reset checksum & counter */ + checksum=0; + count=0; + } + + } + + return 0; +} + +int firmware_to_ram(t_lpc *lpc) { + char buf[BUFSIZE]; + u32 addr,len,type; + int ret,temp; + + /* read a line */ + ret=1; + while(ret) { + /* sync line */ + ret=read(lpc->fwfd,buf,1); + switch(buf[0]) { + case '\r': + continue; + case '\n': + continue; + case ':': + /* start code */ + break; + default: + printf("fw to ram: no ihex format\n"); + return -1; + } + /* read len */ + ret=read(lpc->fwfd,buf,2); + sscanf(buf,"%02x",&len); + /* read addr */ + ret=read(lpc->fwfd,buf,4); + sscanf(buf,"%04x",&addr); + /* read type */ + ret=read(lpc->fwfd,buf,2); + sscanf(buf,"%02x",&type); + /* successfull return if type is end of file */ + if(type==0x01) + return 0; + /* read data (and cksum) */ + ret=read(lpc->fwfd,buf,2*(len+1)); + if(ret!=(2*(len+1))) { + printf("fw to ram: data missing\n"); + return -1; + } + for(ret=0;retroff=((buf[0]<<24)|(buf[1]<<16)); + break; + default: + printf("fw to ram: unknown type %02x\n",type); + return -1; + } + } + + return 0; +} + +int main(int argc,char **argv) { + + t_lpc lpc; + int i; + int ret; /* * initial ... */ - info=0; - memset(&term,0,sizeof(struct termios)); - strncpy(cfreq,CRYSTFREQ,7); + memset(&lpc,0,sizeof(t_lpc)); + strncpy(lpc.freq,CRYSTFREQ,7); + lpc.roff=RAMOFFSET; /* parse argv */ @@ -125,17 +510,17 @@ int main(int argc,char **argv) { switch(argv[i][1]) { case 'd': - strncpy(tts,argv[++i],127); + strncpy(lpc.sdev,argv[++i],127); break; case 'f': - strncpy(fw,argv[++i],127); - info|=FIRMWARE; + strncpy(lpc.fwfile,argv[++i],127); + lpc.info|=FIRMWARE; break; case 'v': - info|=VERBOSE; + lpc.info|=VERBOSE; break; case 'c': - strncpy(cfreq,argv[++i],7); + strncpy(lpc.freq,argv[++i],7); break; default: usage(); @@ -144,83 +529,40 @@ int main(int argc,char **argv) { } - /* - * open serial device - */ - - tts_fd=open(tts,O_RDWR); - if(tts_fd<0) { - perror("tts open"); - return tts_fd; - } - - /* - * configure serial device - */ - - tcgetattr(tts_fd,&term); - - // input/output baudrate - - cfsetispeed(&term,B9600); - cfsetospeed(&term,B9600); - - // control options -> 8n1 - - term.c_cflag&=~PARENB; // no parity - term.c_cflag&=~CSTOPB; // only 1 stop bit - term.c_cflag&=~CSIZE; // no bit mask for data bits - term.c_cflag|=CS8; // 8 data bits - - // line options -> raw input - - term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG); + /* open serial port */ + if(open_serial_device(&lpc)<0) + goto end; - // input options -> disable flow control - - term.c_iflag&=~(IXON|IXOFF|IXANY); - - // more control options -> timeout - - term.c_cc[VMIN]=0; - term.c_cc[VTIME]=10; // 1 second timeout - - tcsetattr(tts_fd,TCSANOW,&term); - - /* auto baud sequence */ - - write(tts_fd,"?",1); - len=0; - txrx(tts_fd,buf,&len,info); - if(strncmp(buf,"Synchronized\r\n",14)) { - printf("auto baud detection failed\n"); + /* boot loader init */ + printf("boot loader init ...\n"); + if(bl_init(&lpc)<0) return -1; - } - /* tell bl that we are synchronized (it's allready in buf) */ - len=14; - txrx(tts_fd,buf,&len,info); - if(strncmp(buf,"OK\r\n",4)) { - printf("sync failed\n"); - return -1; + /* quit if there is no hex file to process */ + if(!(lpc.info&FIRMWARE)) { + printf("no firmware -> aborting\n"); + goto end; } - /* tell bl the crystal frequency */ - len=strlen(cfreq)+2; - strncpy(buf,cfreq,BUFSIZE); - buf[len-2]='\r'; - buf[len-1]='\n'; - txrx(tts_fd,buf,&len,info); - if(strncmp(buf,"OK\r\n",4)) { - printf("freq set failed\n"); - return -1; - } + /* 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); - // to be continued ... (parsing fw file and poking it to ram) + /* go! */ + printf("go ...\n"); + ret=go(&lpc); end: - close(tts_fd); + close(lpc.sfd); + close(lpc.fwfd); return 0; }