2 * lpcload.c - load firmware into ram of lpc2220 via uart0
4 * author: hackbard@hackdaworld.org
12 #include <sys/types.h>
17 #define VERBOSE (1<<0)
18 #define FIRMWARE (1<<1)
20 #define TXRX_TYPE_BAUD 0x01
21 #define TXRX_TYPE_SYNC 0x02
22 #define TXRX_TYPE_CMD 0x03
23 #define TXRX_TYPE_DATA 0x04
25 #define CMD_SUCCESS "0\r\n"
26 #define INVALID_COMMAND "1\r\n"
27 #define SRC_ADDR_ERROR "2\r\n"
28 #define DST_ADDR_ERROR "3\r\n"
29 #define SRC_ADDR_NOT_MAPPED "4\r\n"
30 #define DST_ADDR_NOT_MAPPED "5\r\n"
31 #define COUNT_ERROR "6\r\n"
32 #define COMPARE_ERROR "10\r\n"
34 #define PARAM_ERROR "12\r\n"
35 #define ADDR_ERROR "13\r\n"
36 #define ADDR_NOT_MAPPED "14\r\n"
37 #define CMD_LOCKED "15\r\n"
38 #define INVALID_CODE "16\r\n"
39 #define INVALID_BAUD_RATE "17\r\n"
40 #define INVALID_STOP_BIT "18\r\n"
42 #define CRYSTFREQ "10000"
46 typedef unsigned char u8;
48 typedef struct s_lpc {
49 int sfd; /* serial fd */
50 char sdev[128]; /* seriel device */
51 int fwfd; /* fimrware fd */
52 char fwfile[128]; /* firmware file */
53 u8 info; /* info/mode */
54 char freq[8]; /* frequency */
55 int partid; /* part id */
56 u8 bcv[2]; /* boot code version */
61 printf("possible argv:\n");
62 printf(" -d <serial device>\n");
63 printf(" -f <firmware>\n");
64 printf(" -c <crystal freq>\n");
69 int open_serial_device(t_lpc *lpc) {
73 //memset(&term,0,sizeof(struct termios));
75 /* open serial device */
77 lpc->sfd=open(lpc->sdev,O_RDWR);
83 /* configure the serial device */
85 tcgetattr(lpc->sfd,&term);
87 // input/output baudrate
89 cfsetispeed(&term,B9600);
90 cfsetospeed(&term,B9600);
92 // control options -> 8n1
94 term.c_cflag&=~PARENB; // no parity
95 term.c_cflag&=~CSTOPB; // only 1 stop bit
96 term.c_cflag&=~CSIZE; // no bit mask for data bits
97 term.c_cflag|=CS8; // 8 data bits
99 // line options -> raw input
101 term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
103 // input options -> disable flow control
105 term.c_iflag&=~(IXON|IXOFF|IXANY);
107 // more control options -> timeout
110 term.c_cc[VTIME]=10; // 1 second timeout
112 tcsetattr(lpc->sfd,TCSANOW,&term);
117 int open_firmware(t_lpc *lpc) {
119 /* open firmware file */
121 lpc->fwfd=open(lpc->fwfile,O_RDONLY);
129 int txrx(t_lpc *lpc,u8 *buf,int len,u8 type) {
136 if(lpc->info&VERBOSE)
140 ret=write(lpc->sfd,buf+cnt,len);
142 perror("txrx write");
145 if(lpc->info&VERBOSE)
148 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
153 if(lpc->info&VERBOSE)
154 printf(" (%d)\n",cnt);
156 /* cut the echo if not of type auto baud */
158 if(type!=TXRX_TYPE_BAUD) {
160 ret=read(lpc->sfd,buf,cnt);
162 perror("txrx echo cut");
169 /* return here if type is data */
171 if(type==TXRX_TYPE_DATA)
176 if(lpc->info&VERBOSE)
181 ret=read(lpc->sfd,buf+cnt,BUFSIZE-cnt);
186 if(ret+cnt>BUFSIZE) {
187 printf("txrx read: too small buf size (%d)!\n",BUFSIZE);
190 if(lpc->info&VERBOSE)
193 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
197 if(lpc->info&VERBOSE)
198 printf(" (%d)\n",cnt);
201 /* check/strip return code if type is data */
203 if(type==TXRX_TYPE_CMD) {
204 ret=strlen(CMD_SUCCESS);
205 if(!strncmp(buf,CMD_SUCCESS,ret)) {
211 printf("txrx bad return code!\n");
219 int bl_init(t_lpc *lpc) {
224 /* auto baud sequence */
226 txrx(lpc,buf,1,TXRX_TYPE_BAUD);
227 if(strncmp(buf,"Synchronized\r\n",14)) {
228 printf("auto baud detection failed\n");
232 /* tell bl that we are synchronized (it's allready in buf) */
233 txrx(lpc,buf,14,TXRX_TYPE_SYNC);
234 if(strncmp(buf,"OK\r\n",4)) {
235 printf("sync failed\n");
239 /* tell bl the crystal frequency */
240 len=strlen(lpc->freq)+2;
241 strncpy(buf,lpc->freq,BUFSIZE);
244 txrx(lpc,buf,len,TXRX_TYPE_SYNC);
245 if(strncmp(buf,"OK\r\n",4)) {
246 printf("freq set failed\n");
253 int read_part_id(t_lpc *lpc) {
257 memcpy(buf,"J\r\n",3);
258 txrx(lpc,buf,3,TXRX_TYPE_CMD);
259 lpc->partid=atoi(buf);
264 int read_bcv(t_lpc *lpc) {
269 memcpy(buf,"K\r\n",3);
270 txrx(lpc,buf,3,TXRX_TYPE_CMD);
271 ptr=strtok(buf,"\r\n");
272 lpc->bcv[0]=strtol(ptr,NULL,16);
273 ptr=strtok(NULL,"\r\n");
274 lpc->bcv[1]=strtol(ptr,NULL,16);
279 int uuencode(u8 *in,u8 *out) {
281 out[0]=0x20+((in[0]>>2)&0x3f);
282 out[1]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
283 out[2]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f);
284 out[3]=0x20+(in[2]&0x3f);
289 int write_to_ram(t_lpc *lpc,u8 *buf,int addr,int len) {
300 printf("ram write: not a multiple of 4\n");
304 /* make it a multiple of 3 (reason: uuencode) */
307 printf("ram write: too much data\n");
310 for(i=len;i<nlen;i++) buf[i]=0;
312 /* prepare write command */
313 snprintf(txrxbuf,BUFSIZE,"W %d %d",addr,len);
314 slen=strlen(txrxbuf);
316 txrxbuf[slen+1]='\0';
319 /* send command and check return code */
320 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
321 if(strncmp(txrxbuf,"OK\r\n",4)) {
322 printf("ram write: write command failed\n");
333 /* uuencode / prepare data bytes */
334 uuencode(buf+bcnt,txrxbuf);
339 checksum+=(buf[0]+buf[1]+buf[2]);
341 /* send a data line */
342 txrx(lpc,txrxbuf,6,TXRX_TYPE_DATA);
344 /* increase counters */
353 txrx(lpc,txrxbuf,1,TXRX_TYPE_CMD);
354 if(!strncmp(txrxbuf,"RESEND\r\n",8)) {
355 printf("ram write: resending ...\n");
358 if(strncmp(txrxbuf,"OK\r\n",4)) {
359 printf("ram write: bad response\n");
362 /* reset checksum & counter */
372 int firmware_to_ram(t_lpc *lpc) {
379 int main(int argc,char **argv) {
388 memset(&lpc,0,sizeof(t_lpc));
389 strncpy(lpc.freq,CRYSTFREQ,7);
393 for(i=1;i<argc;i++) {
395 if(argv[i][0]!='-') {
402 strncpy(lpc.sdev,argv[++i],127);
405 strncpy(lpc.fwfile,argv[++i],127);
412 strncpy(lpc.freq,argv[++i],7);
421 /* open serial port */
422 if(open_serial_device(&lpc)<0)
425 /* boot loader init */
426 printf("boot loader init ...\n");
431 printf("part id: %d\n",lpc.partid);
433 /* read boot code version */
435 printf("boot code version: %02x %02x\n",lpc.bcv[0],lpc.bcv[1]);
437 // to be continued ... (parsing fw file and poking it to ram)
438 if(open_firmware(&lpc)<0)
440 firmware_to_ram(&lpc);