comparison packages/hal/arm/edb7xxx/current/support/dl_edb7xxx.c @ 54:f10165814f1e ecos-sw-1999-11-19

Merge from eCos master repository on 1999-11-19-22:24:39-GMT
author jlarmour
date Fri, 19 Nov 1999 23:37:56 +0000
parents
children 3836136e7743
comparison
equal deleted inserted replaced
53:bf22a672b71b 54:f10165814f1e
1 //****************************************************************************
2 //
3 // DOWNLOAD.C - Automates the download of code into the flash on the
4 // CL-PS7111-DK-D2 board.
5 //
6 // Copyright (c) 1999 Cirrus Logic, Inc.
7 //
8 // Adapted for Linux by Cygnus Solutions, Inc.
9 // Renamed 'dl_edb7xxx' to indicate support for all Cirrus Logic eval boards.
10 //
11 //****************************************************************************
12 #include <stdio.h>
13 #include "bootcode.h"
14
15 #define DEFAULT_PORT "/dev/ttyS1"
16 extern char ReceiveChar(long);
17 extern void SendChar(long, char);
18 extern void SetBaud(long, long);
19 extern int CharRead(long port);
20 extern void WaitForOutputReady(long port);
21 extern long OpenPort(char *);
22
23 //****************************************************************************
24 //
25 // WaitFor waits until a specific character is read from the comm port.
26 //
27 //****************************************************************************
28 void
29 WaitFor(long lPort, char cWaitChar)
30 {
31 char cChar;
32
33 //
34 // Wait until we read a specific character from the comm port.
35 //
36 while(1)
37 {
38 //
39 // Read a character.
40 //
41 cChar = ReceiveChar(lPort);
42
43 //
44 // Stop waiting if we received the character.
45 //
46 if(cChar == cWaitChar)
47 {
48 break;
49 }
50 }
51 }
52
53 //****************************************************************************
54 //
55 // This program waits for the '<' character from the boot ROM, sends the boot
56 // code, waits for the '>' from the boot ROM, waits for the '?' from the boot
57 // code, changes the serial port rate (preferably to 115200), downloads the
58 // user data file, and then prints out progress status as the boot code writes
59 // the user data file to the flash.
60 //
61 //****************************************************************************
62 void
63 main(int argc, char *argv[])
64 {
65 long lPort;
66 long lRate = 38400;
67 long lFileSize, lIdx;
68 char cChar, cFirstChar, *pcFile, cRateChar;
69 FILE *pFile;
70
71 //
72 // Make sure that a filename was specified.
73 //
74 if(argc < 2)
75 {
76 fprintf(stderr, "Usage: %s <filename> {<baud rate> {<comm port>}}\n", argv[0]);
77 return;
78 }
79
80 //
81 // If a baud rate was specified, then read it and make sure it is valid.
82 //
83 if(argc > 2)
84 {
85 lRate = atoi(argv[2]);
86 if((lRate != 9600) && (lRate != 19200) && (lRate != 28800) &&
87 (lRate != 38400) && (lRate != 57600) && (lRate != 115200))
88 {
89 fprintf(stderr, "Invalid baud rate: %d(%s).\n", lRate, argv[2]);
90 return;
91 }
92 }
93
94 //
95 // If a comm port was specified, then read it and make sure it is valid.
96 //
97 if(argc > 3)
98 {
99 lPort = OpenPort(argv[3]);
100 } else
101 {
102 lPort = OpenPort(DEFAULT_PORT);
103 }
104 if (lPort < 0)
105 {
106 fprintf(stderr, "Can't open port: %s\n", argv[3]);
107 return;
108 }
109
110 //
111 // Open the file to be downloaded.
112 //
113 pFile = fopen(argv[1], "rb");
114 if(!pFile)
115 {
116 fprintf(stderr, "Could not open file '%s'.\n", argv[1]);
117 return;
118 }
119
120 //
121 // Get the size of the file.
122 //
123 fseek(pFile, 0, SEEK_END);
124 lFileSize = ftell(pFile);
125 fseek(pFile, 0, SEEK_SET);
126
127 //
128 // Allocate memory to hold the file contents.
129 //
130 pcFile = (char *)malloc(lFileSize);
131 if(!pcFile)
132 {
133 fprintf(stderr, "Failed to allocate memory for the file.\n");
134 return;
135 }
136
137 //
138 // Read the contents of the file into memory.
139 //
140 if(fread(pcFile, 1, lFileSize, pFile) != lFileSize)
141 {
142 fprintf(stderr, "Failed to read file '%s'.\n", argv[1]);
143 return;
144 }
145
146 //
147 // Close the file.
148 //
149 fclose(pFile);
150
151 //
152 // Get the baud rate divisor for the given baud rate.
153 //
154 SetBaud(lPort, 9600);
155 switch(lRate)
156 {
157 case 9600:
158 {
159 cRateChar = '0';
160 break;
161 }
162
163 case 19200:
164 {
165 cRateChar = '1';
166 break;
167 }
168
169 case 28800:
170 {
171 cRateChar = '2';
172 break;
173 }
174
175 case 38400:
176 {
177 cRateChar = '3';
178 break;
179 }
180
181 case 57600:
182 {
183 cRateChar = '4';
184 break;
185 }
186
187 case 115200:
188 {
189 cRateChar = '5';
190 break;
191 }
192 }
193
194 //
195 // Empty out the input queue.
196 //
197 while(CharReady(lPort))
198 {
199 cChar = ReceiveChar(lPort);
200 }
201
202 //
203 // Tell the user to reset the board.
204 //
205 fprintf(stderr, "Waiting for the board to wakeup...");
206
207 //
208 // Wait until we read a '<' from the comm port.
209 //
210 WaitFor(lPort, '<');
211
212 //
213 // Tell the user that we are downloading the boot code.
214 //
215 fprintf(stderr, "\nDownloading boot code...( 0%%)");
216
217 //
218 // Write the boot code to the comm port.
219 //
220 for(lIdx = 0; lIdx < 2048; lIdx++)
221 {
222 //
223 // Write this character.
224 //
225 SendChar(lPort, pcBoot[lIdx]);
226
227 //
228 // Periodically print out our progress.
229 //
230 if((lIdx & 127) == 127)
231 {
232 fprintf(stderr, "\b\b\b\b\b%3d%%)", ((lIdx + 1) * 100) / 2048);
233 }
234 }
235
236 //
237 // Wait until we read a '>' from the comm port.
238 //
239 WaitFor(lPort, '>');
240
241 //
242 // Wait until we read a '?' from the comm port.
243 //
244 WaitFor(lPort, '?');
245
246 //
247 // Tell the boot code to switch to the desired baud rate.
248 //
249 SendChar(lPort, 'B');
250 SendChar(lPort, cRateChar);
251
252 //
253 // Wait until the output buffer is empty.
254 //
255 WaitForOutputEmpty();
256
257 //
258 // Switch our baud rate to the desired rate.
259 //
260 SetBaud(lPort, lRate);
261
262 //
263 // Send a '-' character until we receive back a '?' character.
264 //
265 while(1)
266 {
267 //
268 // Send a '-' character.
269 //
270 SendChar(lPort, '-');
271
272 //
273 // Wait a little bit.
274 //
275 for(lIdx = 0; lIdx < 1024 * 1024; lIdx++)
276 {
277 }
278
279 //
280 // See if there is a character waiting to be read.
281 //
282 if(CharReady(lPort))
283 {
284 //
285 // Read the character.
286 //
287 cChar = ReceiveChar(lPort);
288
289 //
290 // Quit waiting if this is a '?'.
291 //
292 if(cChar == '?')
293 {
294 break;
295 }
296 }
297 }
298
299 //
300 // Empty out the input queue.
301 //
302 while(CharReady(lPort))
303 {
304 cChar = ReceiveChar(lPort);
305 }
306
307 //
308 // Send the program flash command.
309 //
310 SendChar(lPort, 'F');
311
312 //
313 // We always program the flash at location 0.
314 //
315 SendChar(lPort, 0);
316 SendChar(lPort, 0);
317 SendChar(lPort, 0);
318 SendChar(lPort, 0);
319
320 //
321 // Send the length of the data file.
322 //
323 SendChar(lPort, (char)(lFileSize & 0xFF));
324 SendChar(lPort, (char)((lFileSize >> 8) & 0xFF));
325 SendChar(lPort, (char)((lFileSize >> 16) & 0xFF));
326 SendChar(lPort, (char)((lFileSize >> 24) & 0xFF));
327
328 //
329 // Tell the user that we are downloading the file data.
330 //
331 fprintf(stderr, "\nDownloading file data...( 0%%)");
332
333 //
334 // Send the actual data in the file.
335 //
336 for(lIdx = 0; lIdx < lFileSize; lIdx++)
337 {
338 //
339 // Send this byte.
340 //
341 SendChar(lPort, pcFile[lIdx]);
342
343 //
344 // Periodically print out our progress.
345 //
346 if((lIdx & 127) == 127)
347 {
348 fprintf(stderr, "\b\b\b\b\b%3d%%)", ((lIdx + 1) * 100) / lFileSize);
349 }
350 }
351
352 //
353 // Tell the user that we are erasing the flash.
354 //
355 fprintf(stderr, "\nErasing the flash...( 0%%)");
356
357 //
358 // Wait until the flash has been erased.
359 //
360 cFirstChar = cChar = ReceiveChar(lPort);
361 while(cChar != '1')
362 {
363 //
364 // Print out our progress.
365 //
366 fprintf(stderr, "\b\b\b\b\b%3d%%)",
367 ((cFirstChar - cChar + 1) * 100) / (cFirstChar - '0'));
368
369 //
370 // Read a character from the boot code.
371 //
372 cChar = ReceiveChar(lPort);
373 }
374
375 //
376 // Tell the user that we are programming the flash.
377 //
378 fprintf(stderr, "\nProgramming the flash...( 0%%)");
379
380 //
381 // Wait until the flash has been programmed.
382 //
383 lIdx = 0;
384 while(1)
385 {
386 //
387 // Read a character from the boot code.
388 //
389 cChar = ReceiveChar(lPort);
390
391 //
392 // If the character is a '?', then we are done.
393 //
394 if(cChar == '?')
395 {
396 break;
397 }
398
399 //
400 // Print out our progress.
401 //
402 fprintf(stderr, "\b\b\b\b\b%3d%%)",
403 (++lIdx * 100) / ((lFileSize + 1023) / 1024));
404 }
405
406 //
407 // Tell the user we are done.
408 //
409 fprintf(stderr, "\nSuccessfully downloaded '%s'.\n", argv[1]);
410 }
411