comparison packages/hal/arm/cl7211/current/support/download_file.c @ 38:e7ba79f6d3a8 ecos-sw-1999-09-23

Merge from eCos master repository on 1999-09-23-20:10:25-BST
author jlarmour
date Thu, 23 Sep 1999 19:52:27 +0000
parents
children
comparison
equal deleted inserted replaced
37:0b25345c9c69 38:e7ba79f6d3a8
1 //****************************************************************************
2 //
3 // download_file.c - Send a raw data file to the EB72xx board.
4 //
5 // Adapted from the Cirrus Logic 'DOWNLOAD.C' program
6 //
7 // Copyright (c) 1999 Cirrus Logic, Inc.
8 //
9 // Adapted for Linux by Cygnus Solutions, Inc.
10 //
11 //****************************************************************************
12 #include <stdio.h>
13 #include "bootcode.h"
14
15 #define DEFAULT_PORT "/dev/ttyS0"
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: download <filename> {<baud rate> {<comm port>}}\n");
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.\n");
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\n");
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 // Open the port to the desired baud rate.
153 //
154 SetBaud(lPort, lRate);
155
156 //
157 // Empty out the input queue.
158 //
159 while(CharReady(lPort))
160 {
161 cChar = ReceiveChar(lPort);
162 }
163
164 //
165 // Tell the user to reset the board.
166 //
167 fprintf(stderr, "Waiting for the board to wakeup...");
168
169 //
170 // Send a '-' character until we receive back a '?' character.
171 //
172 while(1)
173 {
174 //
175 // Send a '-' character.
176 //
177 SendChar(lPort, '-');
178
179 //
180 // Wait a little bit.
181 //
182 for(lIdx = 0; lIdx < 1024 * 1024; lIdx++)
183 {
184 }
185
186 //
187 // See if there is a character waiting to be read.
188 //
189 if(CharReady(lPort))
190 {
191 //
192 // Read the character.
193 //
194 cChar = ReceiveChar(lPort);
195
196 //
197 // Quit waiting if this is a '?'.
198 //
199 if(cChar == '?')
200 {
201 break;
202 }
203 }
204 }
205
206 //
207 // Empty out the input queue.
208 //
209 while(CharReady(lPort))
210 {
211 cChar = ReceiveChar(lPort);
212 }
213
214 //
215 // Send the download file "command"
216 //
217 SendChar(lPort, 'D');
218
219 //
220 // Send the length of the data file.
221 //
222 SendChar(lPort, (char)(lFileSize & 0xFF));
223 SendChar(lPort, (char)((lFileSize >> 8) & 0xFF));
224 SendChar(lPort, (char)((lFileSize >> 16) & 0xFF));
225 SendChar(lPort, (char)((lFileSize >> 24) & 0xFF));
226
227 //
228 // Tell the user that we are downloading the file data.
229 //
230 fprintf(stderr, "\nDownloading file data...( 0%%)");
231
232 //
233 // Send the actual data in the file.
234 //
235 for(lIdx = 0; lIdx < lFileSize; lIdx++)
236 {
237 //
238 // Send this byte.
239 //
240 SendChar(lPort, pcFile[lIdx]);
241
242 //
243 // Periodically print out our progress.
244 //
245 if((lIdx & 127) == 127)
246 {
247 fprintf(stderr, "\b\b\b\b\b%3d%%)", ((lIdx + 1) * 100) / lFileSize);
248 //
249 // Read a character from the boot code.
250 //
251 cChar = ReceiveChar(lPort);
252 }
253 }
254
255 //
256 // Tell the user we are done.
257 //
258 fprintf(stderr, "\nSuccessfully downloaded '%s'.\n", argv[1]);
259 }
260