added lpcload tool to upload firmware into lpc2220 ram vi uart0
[my-code/arm.git] / betty / lpcload.c
1 /*
2  * lpcload.c - load firmware into ram of lpc2220 via uart0
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <termios.h>
16
17 #define VERBOSE         (1<<0)
18 #define FIRMWARE        (1<<1)
19
20 #define CRYSTFREQ       "10000"
21
22 #define BUFSIZE         64
23
24 void usage(void) {
25
26         printf("possible argv:\n");
27         printf("  -d <serial device>\n");
28         printf("  -f <firmware>\n");
29         printf("  -c <crystal freq>\n");
30         printf("  -v\n");
31
32 }
33
34 int txrx(int fd,char *buf,int *len,unsigned char v) {
35
36         int ret,cnt;
37         int i;
38
39         /* write */
40
41         if(v&VERBOSE)
42                 printf("  >> ");
43         cnt=0;
44         while(*len) {
45                 ret=write(fd,buf+cnt,*len);
46                 if(ret<0) {
47                         perror("txrx write");
48                         return ret;
49                 }
50                 if(v&VERBOSE)
51                         for(i=0;i<ret;i++)
52                                 printf("%c",
53                                        ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
54                                        buf[cnt+i]:'.');
55                 *len-=ret;
56                 cnt+=ret;
57         }
58         if(v&VERBOSE)
59                 printf(" (%d)\n",cnt);
60
61         /* cut the echo */
62         // add more checking here!
63         while(cnt)
64                 cnt-=read(fd,buf,cnt);
65
66         /* read */
67
68         if(v&VERBOSE)
69                 printf("  << ");
70         ret=1;
71         cnt=0;
72         while(ret>0) {
73                 ret=read(fd,buf+cnt,BUFSIZE-cnt);
74                 if(ret<0) {
75                         perror("txrx read");
76                         return ret;
77                 }
78                 if(ret+cnt>BUFSIZE) {
79                         printf("txrx read: too small buf size (%d)!\n",BUFSIZE);
80                         return -1;
81                 }
82                 if(v&VERBOSE)
83                         for(i=0;i<ret;i++)
84                                 printf("%c",
85                                        ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
86                                        buf[cnt+i]:'.');
87                 cnt+=ret;
88         }
89         if(v&VERBOSE)
90                 printf(" (%d)\n",cnt);
91         *len=cnt;
92
93         return cnt;
94 }
95
96 int main(int argc,char **argv) {
97
98         int i;
99         char tts[128];
100         int tts_fd;
101         char fw[128];
102         int fw_fd;
103         unsigned char info;
104         struct termios term;
105         char cfreq[8];
106         int len;
107         char buf[BUFSIZE];
108
109         /*
110          * initial ... 
111          */
112
113         info=0;
114         memset(&term,0,sizeof(struct termios));
115         strncpy(cfreq,CRYSTFREQ,7);
116
117         /* parse argv */
118
119         for(i=1;i<argc;i++) {
120
121                 if(argv[i][0]!='-') {
122                         usage();
123                         return -1;
124                 }
125
126                 switch(argv[i][1]) {
127                         case 'd':
128                                 strncpy(tts,argv[++i],127);
129                                 break;
130                         case 'f':
131                                 strncpy(fw,argv[++i],127);
132                                 info|=FIRMWARE;
133                                 break;
134                         case 'v':
135                                 info|=VERBOSE;
136                                 break;
137                         case 'c':
138                                 strncpy(cfreq,argv[++i],7);
139                                 break;
140                         default:
141                                 usage();
142                                 return -1;
143                 }
144
145         }
146
147         /*
148          * open serial device
149          */
150
151         tts_fd=open(tts,O_RDWR);
152         if(tts_fd<0) {
153                 perror("tts open");
154                 return tts_fd;
155         }
156
157         /*
158          * configure serial device
159          */
160
161         tcgetattr(tts_fd,&term);
162
163         // input/output baudrate
164
165         cfsetispeed(&term,B9600);
166         cfsetospeed(&term,B9600);
167
168         // control options -> 8n1
169
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
174
175         // line options -> raw input
176         
177         term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
178
179         // input options -> disable flow control
180         
181         term.c_iflag&=~(IXON|IXOFF|IXANY);
182
183         // more control options -> timeout
184         
185         term.c_cc[VMIN]=0;
186         term.c_cc[VTIME]=10;    // 1 second timeout
187
188         tcsetattr(tts_fd,TCSANOW,&term);
189
190         /* auto baud sequence */
191
192         write(tts_fd,"?",1);
193         len=0;
194         txrx(tts_fd,buf,&len,info);
195         if(strncmp(buf,"Synchronized\r\n",14)) {
196                 printf("auto baud detection failed\n");
197                 return -1;
198         }
199
200         /* tell bl that we are synchronized (it's allready in buf) */
201         len=14;
202         txrx(tts_fd,buf,&len,info);
203         if(strncmp(buf,"OK\r\n",4)) {
204                 printf("sync failed\n");
205                 return -1;
206         }
207
208         /* tell bl the crystal frequency */
209         len=strlen(cfreq)+2;
210         strncpy(buf,cfreq,BUFSIZE);
211         buf[len-2]='\r';
212         buf[len-1]='\n';
213         txrx(tts_fd,buf,&len,info);
214         if(strncmp(buf,"OK\r\n",4)) {
215                 printf("freq set failed\n");
216                 return -1;
217         }
218
219
220         // to be continued ... (parsing fw file and poking it to ram)
221
222 end:
223         close(tts_fd);
224
225         return 0;
226 }
227