2 * lpcload.c - load firmware into ram of lpc2220 via uart0
4 * author: hackbard@hackdaworld.org, rolf.anders@physik.uni-augsburg.de
7 * usage: sudo ./lpcload -d /dev/ttyS0 -f firmware.hex [-v]
14 #include <sys/types.h>
19 #define VERBOSE (1<<0)
20 #define FIRMWARE (1<<1)
22 #define TXRX_TYPE_SYNC 0x00
23 #define TXRX_TYPE_CKSM 0x00
24 #define TXRX_TYPE_BAUD 0x01
25 #define TXRX_TYPE_CMD 0x02
26 #define TXRX_TYPE_DATA 0x03
27 #define TXRX_TYPE_GO 0x04
29 #define CMD_SUCCESS "0\r\n"
30 #define INVALID_COMMAND "1\r\n"
31 #define SRC_ADDR_ERROR "2\r\n"
32 #define DST_ADDR_ERROR "3\r\n"
33 #define SRC_ADDR_NOT_MAPPED "4\r\n"
34 #define DST_ADDR_NOT_MAPPED "5\r\n"
35 #define COUNT_ERROR "6\r\n"
36 #define COMPARE_ERROR "10\r\n"
38 #define PARAM_ERROR "12\r\n"
39 #define ADDR_ERROR "13\r\n"
40 #define ADDR_NOT_MAPPED "14\r\n"
41 #define CMD_LOCKED "15\r\n"
42 #define INVALID_CODE "16\r\n"
43 #define INVALID_BAUD_RATE "17\r\n"
44 #define INVALID_STOP_BIT "18\r\n"
46 #define CRYSTFREQ "10000"
47 #define RAMOFFSET 0x40000200
51 typedef unsigned char u8;
52 typedef unsigned short u16;
53 typedef unsigned int u32;
55 typedef struct s_lpc {
56 int sfd; /* serial fd */
57 char sdev[128]; /* seriel device */
58 int fwfd; /* fimrware fd */
59 char fwfile[128]; /* firmware file */
60 u8 info; /* info/mode */
61 char freq[8]; /* frequency */
62 u32 roff; /* ram offset of uc */
67 printf("possible argv:\n");
68 printf(" -d <serial device>\n");
69 printf(" -f <firmware>\n");
70 printf(" -c <crystal freq>\n");
71 printf(" -r <ram offset>\n");
76 int open_serial_device(t_lpc *lpc) {
80 //memset(&term,0,sizeof(struct termios));
82 /* open serial device */
84 lpc->sfd=open(lpc->sdev,O_RDWR);
90 /* configure the serial device */
92 tcgetattr(lpc->sfd,&term);
94 // input/output baudrate
96 cfsetispeed(&term,B38400);
97 cfsetospeed(&term,B38400);
99 // control options -> 8n1
101 term.c_cflag&=~PARENB; // no parity
102 term.c_cflag&=~CSTOPB; // only 1 stop bit
103 term.c_cflag&=~CSIZE; // no bit mask for data bits
104 term.c_cflag|=CS8; // 8 data bits
106 // line options -> raw input
108 term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
110 // input options -> enable flow control
112 //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
113 term.c_iflag&=~(INLCR|ICRNL|IXANY);
114 term.c_iflag|=(IXON|IXOFF);
120 // more control options -> timeout / flow control
123 term.c_cc[VTIME]=10; // 1 second timeout
124 term.c_cc[VSTART]=0x11;
125 term.c_cc[VSTOP]=0x13;
127 tcsetattr(lpc->sfd,TCSANOW,&term);
132 int open_firmware(t_lpc *lpc) {
134 /* open firmware file */
136 lpc->fwfd=open(lpc->fwfile,O_RDONLY);
144 int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
151 if(lpc->info&VERBOSE)
155 ret=write(lpc->sfd,buf+cnt,len);
157 perror("txrx write");
160 if(lpc->info&VERBOSE)
163 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
168 if(lpc->info&VERBOSE) {
171 printf("%02x ",buf[i]);
172 printf("| (%d)\n",cnt);
175 /* cut the echo if not of type auto baud */
177 if(type!=TXRX_TYPE_BAUD) {
179 ret=read(lpc->sfd,buf,cnt);
181 perror("txrx echo cut");
188 /* return here if type is data */
190 if(type==TXRX_TYPE_DATA)
195 ret=read(lpc->sfd,buf,1);
197 perror("txrx read (first byte)");
215 printf("txrx read: bad return byte '%02x'\n",buf[0]);
222 ret=read(lpc->sfd,buf+1+cnt-i,i);
224 perror("txrx read (next bytes)");
229 if(lpc->info&VERBOSE) {
232 printf("%c",((buf[i]>0x19)&(buf[i]<0x7f))?
236 printf("%02x ",buf[i]);
237 printf("| (%d)\n",cnt+1);
241 /* return if type is go */
243 if(type==TXRX_TYPE_GO)
246 /* check/strip return code if type is cmd */
248 if(type==TXRX_TYPE_CMD) {
249 ret=strlen(CMD_SUCCESS);
250 if(!strncmp(buf,CMD_SUCCESS,ret)) {
256 printf("txrx bad return code!\n");
264 int bl_init(t_lpc *lpc) {
269 /* auto baud sequence */
271 txrx(lpc,buf,1,TXRX_TYPE_BAUD);
272 if(strncmp(buf,"Synchronized\r\n",14)) {
273 printf("auto baud detection failed\n");
277 /* tell bl that we are synchronized (it's allready in buf) */
278 txrx(lpc,buf,14,TXRX_TYPE_SYNC);
279 if(strncmp(buf,"OK\r\n",4)) {
280 printf("sync failed\n");
284 /* tell bl the crystal frequency */
285 len=strlen(lpc->freq)+2;
286 strncpy(buf,lpc->freq,BUFSIZE);
289 txrx(lpc,buf,len,TXRX_TYPE_SYNC);
290 if(strncmp(buf,"OK\r\n",4)) {
291 printf("freq set failed\n");
298 int unlock_go(t_lpc *lpc) {
303 memcpy(buf,"U 23130\r\n",9);
304 ret=txrx(lpc,buf,9,TXRX_TYPE_CMD);
314 snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->roff);
316 ret=txrx(lpc,buf,len,TXRX_TYPE_GO);
321 int uuencode(u8 *in,u8 *out,int len) {
324 out[1]=0x20+((in[0]>>2)&0x3f);
325 out[2]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
326 out[3]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f);
327 out[4]=0x20+(in[2]&0x3f);
332 int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
336 char txrxbuf[BUFSIZE];
343 printf("ram write: not a multiple of 4\n");
347 /* make it a multiple of 3 (reason: uuencode) */
348 nlen=(!(len%3))?len:((len/3+1)*3);
350 printf("ram write: too much data\n");
353 for(i=len;i<nlen;i++) buf[i]=0;
358 /* prepare write command */
359 if(lpc->info&VERBOSE)
360 printf("writing 0x%02x bytes to 0x%08x\n",len,addr);
361 snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len);
362 slen=strlen(txrxbuf);
364 /* send command and check return code */
365 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
374 /* uuencode / prepare data bytes */
375 uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),
376 (bcnt==nlen-3)?(len%3?len%3:3):3);
381 checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
383 /* send a data line */
384 txrx(lpc,txrxbuf,7,TXRX_TYPE_DATA);
386 /* increase counters */
392 if((!(lcount%20))|(bcnt==nlen)) {
394 memcpy(txrxbuf,"`\r\n",3);
395 //txrx(lpc,txrxbuf,3,TXRX_TYPE_DATA);
397 snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
398 slen=strlen(txrxbuf);
399 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CKSM);
400 if(!strncmp(txrxbuf,"RESE",4)) {
401 read(lpc->sfd,txrxbuf+4,4);
402 printf("ram write: resending ...\n");
405 if(strncmp(txrxbuf,"OK\r\n",4)) {
406 printf("ram write: bad response\n");
409 /* reset checksum & counter */
419 int firmware_to_ram(t_lpc *lpc) {
429 ret=read(lpc->fwfd,buf,1);
439 printf("fw to ram: no ihex format\n");
443 ret=read(lpc->fwfd,buf,2);
444 sscanf(buf,"%02x",&len);
446 printf("fw to ram: len not a multiple of 4\n");
450 ret=read(lpc->fwfd,buf,4);
451 sscanf(buf,"%04x",&addr);
453 ret=read(lpc->fwfd,buf,2);
454 sscanf(buf,"%02x",&type);
455 /* successfull return if type is end of file */
458 /* read data (and cksum) */
459 ret=read(lpc->fwfd,buf,2*(len+1));
460 if(ret!=(2*(len+1))) {
461 printf("fw to ram: data missing\n");
464 for(ret=0;ret<len;ret++) {
465 sscanf(buf+2*ret,"%02x",&temp);
468 /* act according to type */
471 // /* get cs and ip */
475 printf("fw to ram: invalid len\n");
478 write_to_ram(lpc,buf,addr,len);
481 lpc->roff=((buf[0]<<28)|(buf[1]<<24));
482 lpc->roff|=((buf[2]<<20)|(buf[3]<<16));
485 printf("fw to ram: unknown type %02x\n",type);
493 int main(int argc,char **argv) {
503 memset(&lpc,0,sizeof(t_lpc));
504 strncpy(lpc.freq,CRYSTFREQ,7);
509 for(i=1;i<argc;i++) {
511 if(argv[i][0]!='-') {
518 strncpy(lpc.sdev,argv[++i],127);
521 strncpy(lpc.fwfile,argv[++i],127);
528 strncpy(lpc.freq,argv[++i],7);
537 /* open serial port */
538 if(open_serial_device(&lpc)<0)
541 /* boot loader init */
542 printf("boot loader init ...\n");
546 /* quit if there is no hex file to process */
547 if(!(lpc.info&FIRMWARE)) {
548 printf("no firmware -> aborting\n");
552 /* open firmware file */
553 if(open_firmware(&lpc)<0)
556 /* parse intel hex file and write to ram */
557 printf("write firmware to ram ...\n");
558 firmware_to_ram(&lpc);
561 printf("unlock go command ...\n");