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 CRYSTFREQ "10000"
26 printf("possible argv:\n");
27 printf(" -d <serial device>\n");
28 printf(" -f <firmware>\n");
29 printf(" -c <crystal freq>\n");
34 int txrx(int fd,char *buf,int *len,unsigned char v) {
45 ret=write(fd,buf+cnt,*len);
53 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
59 printf(" (%d)\n",cnt);
62 // add more checking here!
64 cnt-=read(fd,buf,cnt);
73 ret=read(fd,buf+cnt,BUFSIZE-cnt);
79 printf("txrx read: too small buf size (%d)!\n",BUFSIZE);
85 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
90 printf(" (%d)\n",cnt);
96 int main(int argc,char **argv) {
114 memset(&term,0,sizeof(struct termios));
115 strncpy(cfreq,CRYSTFREQ,7);
119 for(i=1;i<argc;i++) {
121 if(argv[i][0]!='-') {
128 strncpy(tts,argv[++i],127);
131 strncpy(fw,argv[++i],127);
138 strncpy(cfreq,argv[++i],7);
151 tts_fd=open(tts,O_RDWR);
158 * configure serial device
161 tcgetattr(tts_fd,&term);
163 // input/output baudrate
165 cfsetispeed(&term,B9600);
166 cfsetospeed(&term,B9600);
168 // control options -> 8n1
170 term.c_cflag&=~PARENB; // no parity
171 term.c_cflag&=~CSTOPB; // only 1 stop bit
172 term.c_cflag&=~CSIZE; // no bit mask for data bits
173 term.c_cflag|=CS8; // 8 data bits
175 // line options -> raw input
177 term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
179 // input options -> disable flow control
181 term.c_iflag&=~(IXON|IXOFF|IXANY);
183 // more control options -> timeout
186 term.c_cc[VTIME]=10; // 1 second timeout
188 tcsetattr(tts_fd,TCSANOW,&term);
190 /* auto baud sequence */
194 txrx(tts_fd,buf,&len,info);
195 if(strncmp(buf,"Synchronized\r\n",14)) {
196 printf("auto baud detection failed\n");
200 /* tell bl that we are synchronized (it's allready in buf) */
202 txrx(tts_fd,buf,&len,info);
203 if(strncmp(buf,"OK\r\n",4)) {
204 printf("sync failed\n");
208 /* tell bl the crystal frequency */
210 strncpy(buf,cfreq,BUFSIZE);
213 txrx(tts_fd,buf,&len,info);
214 if(strncmp(buf,"OK\r\n",4)) {
215 printf("freq set failed\n");
220 // to be continued ... (parsing fw file and poking it to ram)