FreeCalypso > hg > freecalypso-reveng
comparison frbl/test/frbl2.c @ 324:43c92df87ac6
frbl2test: meat filled in
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 06 Mar 2020 00:30:18 +0000 |
parents | cefa700d1b8f |
children | 7df08926a4e7 |
comparison
equal
deleted
inserted
replaced
323:cefa700d1b8f | 324:43c92df87ac6 |
---|---|
1 #include <sys/types.h> | 1 #include <sys/types.h> |
2 #include <sys/ioctl.h> | |
3 #include <sys/time.h> | |
4 #include <sys/errno.h> | |
5 #include <ctype.h> | |
2 #include <stdint.h> | 6 #include <stdint.h> |
3 #include <stdio.h> | 7 #include <stdio.h> |
4 #include <stdlib.h> | 8 #include <stdlib.h> |
5 #include <string.h> | 9 #include <string.h> |
6 #include <strings.h> | 10 #include <strings.h> |
11 #include <termios.h> | |
7 #include <unistd.h> | 12 #include <unistd.h> |
8 #include "srecreader.h" | 13 #include "srecreader.h" |
14 | |
15 extern int errno; | |
9 | 16 |
10 extern char *target_ttydev; | 17 extern char *target_ttydev; |
11 extern int target_fd; | 18 extern int target_fd; |
12 extern struct srecreader srimage; | 19 extern struct srecreader srimage; |
13 | 20 |
14 #define MAX_IMAGE_LEN 32768 | 21 #define MAX_IMAGE_LEN 32768 |
15 | 22 |
16 static u_char codeimage[MAX_IMAGE_LEN]; | 23 static u_char codeimage[MAX_IMAGE_LEN]; |
17 static unsigned codeimage_len; | 24 static unsigned codeimage_len; |
18 static uint32_t loadaddr; | 25 static uint32_t loadaddr; |
26 | |
27 static u_char beacon_cmd[3] = {0xAA, 0x01, 0xDD}; | |
28 static u_char fluid_baudrate_cmd[2] = {0x00, 0x07}; | |
29 static u_char fluid_version_cmd[1] = {'V'}; | |
30 static u_char fluid_chipquery_cmd[2] = {'Q', 'C'}; | |
31 static u_char fluid_download_cmd[7] = {'L'}; | |
32 | |
33 #define BEACON_INTERVAL 13 /* ms between beacons */ | |
34 #define INTERMEDIATE_TIMEOUT 500 /* ms to wait for responses */ | |
35 #define SERIAL_FLUSH_DELAY 200 /* also in ms */ | |
19 | 36 |
20 read_srec_image() | 37 read_srec_image() |
21 { | 38 { |
22 u_char *writep; | 39 u_char *writep; |
23 uint32_t endaddr; | 40 uint32_t endaddr; |
109 } | 126 } |
110 /* all good */ | 127 /* all good */ |
111 return(0); | 128 return(0); |
112 } | 129 } |
113 | 130 |
131 static void | |
132 fill_download_cmd() | |
133 { | |
134 unsigned nwords; | |
135 | |
136 nwords = codeimage_len >> 1; | |
137 fluid_download_cmd[1] = loadaddr >> 24; | |
138 fluid_download_cmd[2] = loadaddr >> 16; | |
139 fluid_download_cmd[3] = loadaddr >> 8; | |
140 fluid_download_cmd[4] = loadaddr; | |
141 fluid_download_cmd[5] = nwords >> 8; | |
142 fluid_download_cmd[6] = nwords; | |
143 } | |
144 | |
145 static int | |
146 expect_beacon_response(timeout) | |
147 { | |
148 u_char buf; | |
149 fd_set fds; | |
150 struct timeval tv; | |
151 int cc; | |
152 | |
153 for (;;) { | |
154 FD_ZERO(&fds); | |
155 FD_SET(target_fd, &fds); | |
156 tv.tv_sec = 0; | |
157 tv.tv_usec = timeout * 1000; | |
158 cc = select(target_fd+1, &fds, NULL, NULL, &tv); | |
159 if (cc < 0) { | |
160 if (errno == EINTR) | |
161 continue; | |
162 perror("select"); | |
163 exit(1); | |
164 } | |
165 if (cc < 1) | |
166 return(0); | |
167 cc = read(target_fd, &buf, 1); | |
168 if (cc <= 0) { | |
169 perror("read after successful select"); | |
170 exit(1); | |
171 } | |
172 if (buf == 'H') | |
173 return(1); | |
174 } | |
175 } | |
176 | |
177 static | |
178 send_beacons() | |
179 { | |
180 printf("Sending beacons to %s\n", target_ttydev); | |
181 do | |
182 write(target_fd, beacon_cmd, sizeof beacon_cmd); | |
183 while (!expect_beacon_response(BEACON_INTERVAL)); | |
184 return 0; | |
185 } | |
186 | |
187 static int | |
188 expect_add_response(buf, expect_bytes, timeout) | |
189 u_char *buf; | |
190 { | |
191 fd_set fds; | |
192 struct timeval tv; | |
193 int pass, cc; | |
194 | |
195 for (pass = 0; pass < expect_bytes; ) { | |
196 FD_ZERO(&fds); | |
197 FD_SET(target_fd, &fds); | |
198 tv.tv_sec = 0; | |
199 tv.tv_usec = timeout * 1000; | |
200 cc = select(target_fd+1, &fds, NULL, NULL, &tv); | |
201 if (cc < 0) { | |
202 if (errno == EINTR) | |
203 continue; | |
204 perror("select"); | |
205 exit(1); | |
206 } | |
207 if (cc < 1) | |
208 return(-1); | |
209 cc = read(target_fd, buf + pass, expect_bytes - pass); | |
210 if (cc <= 0) { | |
211 perror("read after successful select"); | |
212 exit(1); | |
213 } | |
214 pass += cc; | |
215 } | |
216 return(0); | |
217 } | |
218 | |
219 perform_frbl2_protocol() | |
220 { | |
221 u_char respbuf[4]; | |
222 static int zero = 0; | |
223 | |
224 ioctl(target_fd, FIONBIO, &zero); | |
225 send_beacons(); | |
226 printf("Got beacon response, attempting FLUID protocol\n"); | |
227 write(target_fd, fluid_baudrate_cmd, sizeof fluid_baudrate_cmd); | |
228 usleep(SERIAL_FLUSH_DELAY * 1000); | |
229 tcflush(target_fd, TCIFLUSH); | |
230 write(target_fd, fluid_version_cmd, sizeof fluid_version_cmd); | |
231 if (expect_add_response(respbuf, 1, INTERMEDIATE_TIMEOUT) < 0) { | |
232 fprintf(stderr, "no response to version query\n"); | |
233 exit(1); | |
234 } | |
235 if (!isdigit(respbuf[0])) { | |
236 fprintf(stderr, "bad response to version query: 0x%02X\n", | |
237 respbuf[0]); | |
238 exit(1); | |
239 } | |
240 printf("Version response: %d\n", respbuf[0] - '0'); | |
241 write(target_fd, fluid_chipquery_cmd, sizeof fluid_chipquery_cmd); | |
242 if (expect_add_response(respbuf, 4, INTERMEDIATE_TIMEOUT) < 0) { | |
243 fprintf(stderr, "no response to chip ID query\n"); | |
244 exit(1); | |
245 } | |
246 if (respbuf[2] || respbuf[3]) { | |
247 fprintf(stderr, | |
248 "bad response to chip ID query: %02X %02X %02X %02X\n", | |
249 respbuf[0], respbuf[1], respbuf[2], respbuf[3]); | |
250 exit(1); | |
251 } | |
252 printf("Chip ID response: %04X\n", (respbuf[1] << 8) | respbuf[0]); | |
253 printf("Sending download command\n"); | |
254 write(target_fd, fluid_download_cmd, sizeof fluid_download_cmd); | |
255 if (expect_add_response(respbuf, 1, INTERMEDIATE_TIMEOUT) < 0) { | |
256 fprintf(stderr, "no response to download command\n"); | |
257 exit(1); | |
258 } | |
259 if (respbuf[0] != 'R') { | |
260 fprintf(stderr, "bad response to download command: 0x%02X\n", | |
261 respbuf[0]); | |
262 exit(1); | |
263 } | |
264 if (write(target_fd, codeimage, codeimage_len) != codeimage_len) { | |
265 perror("write of download image bulk"); | |
266 exit(1); | |
267 } | |
268 printf("Code image sent!\n"); | |
269 } | |
270 | |
114 frbl_test_main() | 271 frbl_test_main() |
115 { | 272 { |
116 read_srec_image(); | 273 read_srec_image(); |
117 /* remainder to be implemented */ | 274 fill_download_cmd(); |
118 } | 275 perform_frbl2_protocol(); |
276 } |