X-Git-Url: https://hackdaworld.org/gitweb/?a=blobdiff_plain;f=betty%2Flpcload.c;h=0d2f9c85ddd9bfd9f5394c556a38ee666d707aa8;hb=feece393fb07514608a7ad21eaa376ea3375e945;hp=bdb39482c205ae97db92a3a89c7b7f6b760e0e12;hpb=a2e8f16f2d595f007ebf1bfe041927702e161c9c;p=my-code%2Farm.git diff --git a/betty/lpcload.c b/betty/lpcload.c index bdb3948..0d2f9c8 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_BAUD 0x01 +#define TXRX_TYPE_SYNC 0x02 +#define TXRX_TYPE_CMD 0x03 +#define TXRX_TYPE_DATA 0x04 +#define TXRX_TYPE_CKSM 0x05 + +#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 hoff; /* start addr of ihex file */ + 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,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); + + // input options -> enable flow control + + //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL); + term.c_iflag&=~(INLCR|ICRNL|IXANY); + term.c_iflag|=(IXON|IXOFF); + + // 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) { + + int ret; + char buf[BUFSIZE]; + + /* open firmware file */ + + lpc->fwfd=open(lpc->fwfile,O_RDONLY); + + 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; +} + +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 */ - /* cut the echo */ - // add more checking here! - while(cnt) - cnt-=read(fd,buf,cnt); + 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_CMD); + + 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-lpc->hoff); + + /* 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); + 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); + /* 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;ret 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 -> 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 */ - - printf("auto baud sequence ...\n"); - 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"); - return -1; - } - - /* tell bl that we are synchronized (it's allready in buf) */ - - printf("sync sequence ...\n"); - len=14; - txrx(tts_fd,buf,&len,info); - if(strncmp(buf,"OK\r\n",4)) { - printf("sync failed\n"); + /* boot loader init */ + printf("boot loader init ...\n"); + if(bl_init(&lpc)<0) return -1; - } - - /* tell bl the crystal frequency */ - - printf("frequency setting sequence ...\n"); - 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; - } - - /* read part id */ - printf("read part id ...\n"); - len=3; - memcpy(buf,"J\r\n",3); - txrx(tts_fd,buf,&len,info); - buf[len]='\0'; - printf("part id: %d (0x%x)\n",atoi(buf),atoi(buf+3)); - - /* read boot code version */ - printf("read boot code version ...\n"); - len=3; - memcpy(buf,"K\r\n",3); - txrx(tts_fd,buf,&len,info); - buf[len]='\0'; - printf("boot code version: %c %c\n",(buf),atoi(buf)); - + /* 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"); + go(&lpc); + printf("\n"); end: - close(tts_fd); + close(lpc.sfd); + close(lpc.fwfd); return 0; }