blob: c04afccfc4842527f6672643943efba206a32169 [file] [log] [blame]
Stefan Reinauerc566d202015-08-25 09:52:42 -07001/*
2 * Copyright 2012-2015 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Stefan Reinauerc566d202015-08-25 09:52:42 -070012 */
13
14#include <stdio.h>
15#include <string.h>
16#include "em100.h"
17
Stefan Reinauer146eeba2020-12-17 19:26:02 -080018int trace_brief = 0;
19
Stefan Reinauerc566d202015-08-25 09:52:42 -070020/* SPI Trace related operations */
21
22/**
23 * reset_spi_trace: clear SPI trace buffer
24 * @param em100: em100 device structure
25 *
26 * out(16 bytes): 0xbd 0 .. 0
27 */
28int reset_spi_trace(struct em100 *em100)
29{
30 unsigned char cmd[16];
31 memset(cmd, 0, 16);
32 cmd[0] = 0xbd; /* reset SPI trace buffer*/
33 if (!send_cmd(em100->dev, cmd)) {
34 return 0;
35 }
36 return 1;
37}
38
39/**
40 * read_spi_trace: fetch SPI trace data
41 * @param em100: em100 device structure
42 * globals: curpos, counter, cmdid
43 *
44 * out(16 bytes): bc 00 00 00 08 00 00 00 00 15 00 00 00 00 00 00
45 * in(8x8192 bytes): 2 bytes (BE) number of records (0..0x3ff),
46 * then records of 8 bytes each
47 */
48static unsigned int counter = 0;
49static unsigned char curpos = 0;
50static unsigned char cmdid = 0xff; // timestamp, so never a valid command id
51
Martin Roth330de302015-09-17 16:33:07 -060052#define REPORT_BUFFER_LENGTH 8192
53#define REPORT_BUFFER_COUNT 8
54
Stefan Reinauerabd3e322019-12-04 18:29:24 -080055static int read_report_buffer(struct em100 *em100,
Martin Roth330de302015-09-17 16:33:07 -060056 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH])
Stefan Reinauerc566d202015-08-25 09:52:42 -070057{
Martin Roth330de302015-09-17 16:33:07 -060058 unsigned char cmd[16] = {0};
59 int len;
60 unsigned int report;
61
Stefan Reinauerc566d202015-08-25 09:52:42 -070062 cmd[0] = 0xbc; /* read SPI trace buffer*/
63
Stefan Reinauerabd3e322019-12-04 18:29:24 -080064 /*
Martin Roth330de302015-09-17 16:33:07 -060065 * Trace length, unit is 4k according to specs
66 *
67 * cmd1..cmd4 are probably u32BE on how many
68 * reports (8192 bytes each) to fetch
69 */
Stefan Reinauerc566d202015-08-25 09:52:42 -070070 cmd[1] = 0x00;
71 cmd[2] = 0x00;
72 cmd[3] = 0x00;
Martin Roth330de302015-09-17 16:33:07 -060073 cmd[4] = REPORT_BUFFER_COUNT;
Stefan Reinauerc566d202015-08-25 09:52:42 -070074 /* Timeout in ms */
75 cmd[5] = 0x00;
76 cmd[6] = 0x00;
77 cmd[7] = 0x00;
78 cmd[8] = 0x00;
79 /* Trace Config
80 * [1:0] 00 start/stop spi trace according to emulation status
81 * 01 start when TraceConfig[2] == 1
82 * 10 start when trig signal goes high
83 * 11 RFU
84 * [2] When TraceConfig[1:0] == 01 this bit starts the trace
85 * [7:3] RFU
86 */
87 cmd[9] = 0x15;
88
89 if (!send_cmd(em100->dev, cmd)) {
90 printf("sending trace command failed\n");
91 return 0;
92 }
Stefan Reinauerd7f959b2015-09-03 16:03:40 -070093
Martin Roth330de302015-09-17 16:33:07 -060094 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
Martin Rothb54d6ba2015-09-29 14:49:37 -060095 len = get_response(em100->dev, &reportdata[report][0],
96 REPORT_BUFFER_LENGTH);
Martin Roth330de302015-09-17 16:33:07 -060097 if (len != REPORT_BUFFER_LENGTH) {
98 printf("error, report length = %d instead of %d.\n\n",
99 len, REPORT_BUFFER_LENGTH);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700100 return 0;
101 }
Martin Roth330de302015-09-17 16:33:07 -0600102 }
103
104 return 1;
105}
106
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800107typedef enum { ADDR_NONE, ADDR_NO_OFF_3B, ADDR_3B, ADDR_4B, ADDR_DYNAMIC } address_t;
108
Martin Rothe8a72dd2015-09-17 17:14:48 -0600109struct spi_cmd_values {
Stefan Reinauer79533882019-11-22 00:57:29 -0800110 const char *cmd_name;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600111 uint8_t cmd;
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800112 address_t address_type;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600113 uint8_t pad_bytes;
114};
115
116struct spi_cmd_values spi_command_list[] = {
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800117 /* name cmd, addr, pad */
118 {"read SFDP", 0x5a, ADDR_NO_OFF_3B, 0},
119 {"write status register", 0x01, ADDR_NONE, 0},
120 {"page program", 0x02, ADDR_DYNAMIC, 0},
121 {"read", 0x03, ADDR_DYNAMIC, 0},
122 {"write disable", 0x04, ADDR_NONE, 0},
123 {"read status register", 0x05, ADDR_NONE, 0},
124 {"write enable", 0x06, ADDR_NONE, 0},
125 {"fast read", 0x0b, ADDR_DYNAMIC, 1},
126 {"EM100 specific", 0x11, ADDR_NONE, 0},
127 {"fast dual read", 0x3b, ADDR_DYNAMIC, 2},
128 {"chip erase", 0x60, ADDR_NONE, 0},
129 {"read JEDEC ID", 0x9f, ADDR_NONE, 0},
130 {"chip erase c7h", 0xc7, ADDR_NONE, 0},
131 {"chip erase 60h", 0x60, ADDR_NONE, 0},
132 {"sector erase", 0xd8, ADDR_DYNAMIC, 0},
133 {"dual I/O read", 0xbb, ADDR_DYNAMIC, 2},
134 {"quad I/O read", 0xeb, ADDR_DYNAMIC, 0},
135 {"quad read", 0x6b, ADDR_DYNAMIC, 0},
136 {"quad I/O dt read", 0xed, ADDR_DYNAMIC, 0},
137 {"quad page program", 0x38, ADDR_DYNAMIC, 0},
138 {"sector erase", 0x20, ADDR_DYNAMIC, 0},
139 {"block erase 32KB", 0x52, ADDR_DYNAMIC, 0},
140 {"block erase 64KB", 0xd8, ADDR_DYNAMIC, 0},
141 {"enter 4b mode", 0xb7, ADDR_NONE, 0},
142 {"exit 4b mode", 0xe9, ADDR_NONE, 0},
143 {"read 4b", 0x13, ADDR_4B, 0},
144 {"fast read 4b", 0x0c, ADDR_4B, 0},
145 {"dual I/O read 4b", 0xbc, ADDR_4B, 0},
146 {"dual out read 4b", 0x3c, ADDR_4B, 0},
147 {"quad I/O read 4b", 0xec, ADDR_4B, 0},
148 {"quad out read 4b", 0x6c, ADDR_4B, 0},
149 {"quad I/O dt read 4b", 0xee, ADDR_4B, 0},
150 {"page program 4b", 0x12, ADDR_4B, 0},
151 {"quad page program 4b", 0x3e, ADDR_4B, 0},
152 {"block erase 64KB 4b", 0xdc, ADDR_4B, 0},
153 {"block erase 32KB 4b", 0x5c, ADDR_4B, 0},
154 {"sector erase 4b", 0x21, ADDR_4B, 0},
155 {"enter quad I/O mode", 0x35, ADDR_NONE, 0},
156 {"exit quad I/O mode", 0xf5, ADDR_NONE, 0},
Martin Rothe8a72dd2015-09-17 17:14:48 -0600157
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800158 {"unknown command", 0xff, ADDR_NONE, 0}
159
Martin Rothe8a72dd2015-09-17 17:14:48 -0600160};
161
Stefan Reinauerabd3e322019-12-04 18:29:24 -0800162static struct spi_cmd_values * get_command_vals(uint8_t command)
163{
Martin Rothe8a72dd2015-09-17 17:14:48 -0600164 /* cache last command so a search isn't needed every time */
165 static struct spi_cmd_values *spi_cmd = &spi_command_list[3]; /* init to read */
166 int i;
167
168 if (spi_cmd->cmd != command) {
169 for (i = 0; spi_command_list[i].cmd != 0xff; i++) {
170 if (spi_command_list[i].cmd == command)
171 break;
172 }
173 spi_cmd = &spi_command_list[i];
174 }
175
176 return spi_cmd;
177}
178
179#define MAX_TRACE_BLOCKLENGTH 6
Martin Roth712262a2015-09-18 14:01:18 -0600180int read_spi_trace(struct em100 *em100, int display_terminal,
181 unsigned long addr_offset)
Martin Roth330de302015-09-17 16:33:07 -0600182{
Martin Rothb54d6ba2015-09-29 14:49:37 -0600183 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] =
184 {{0}};
Martin Roth330de302015-09-17 16:33:07 -0600185 unsigned char *data;
186 unsigned int count, i, report;
187 static int outbytes = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600188 static int additional_pad_bytes = 0;
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800189 static unsigned long address = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600190 static unsigned long long timestamp = 0;
191 static unsigned long long start_timestamp = 0;
192 static struct spi_cmd_values *spi_cmd_vals = &spi_command_list[3];
Martin Roth330de302015-09-17 16:33:07 -0600193
194 if (!read_report_buffer(em100, reportdata))
195 return 0;
196
197 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
198 data = &reportdata[report][0];
Stefan Reinauerc566d202015-08-25 09:52:42 -0700199 count = (data[0] << 8) | data[1];
Stefan Reinauerd6180e22020-12-11 14:20:57 -0800200 if (!count)
201 continue;
202 if (count > 1023) {
Stefan Reinauer2372d7a2015-10-10 10:46:45 +0000203 printf("Warning: EM100pro sends too much data.\n");
Stefan Reinauerd6180e22020-12-11 14:20:57 -0800204 count = 1023;
Stefan Reinauer2372d7a2015-10-10 10:46:45 +0000205 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700206 for (i = 0; i < count; i++) {
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800207 int address_bytes = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600208 unsigned int j = additional_pad_bytes;
209 additional_pad_bytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700210 unsigned char cmd = data[2 + i*8];
Stefan Reinauerdfb107d2020-12-10 19:22:40 -0800211
212 if (cmd == 0x00) {
213 /* packet without valid data */
214 continue;
215 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700216 if (cmd == 0xff) {
217 /* timestamp */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700218 timestamp = data[2 + i*8 + 2];
219 timestamp = (timestamp << 8) | data[2 + i*8 + 3];
220 timestamp = (timestamp << 8) | data[2 + i*8 + 4];
221 timestamp = (timestamp << 8) | data[2 + i*8 + 5];
222 timestamp = (timestamp << 8) | data[2 + i*8 + 6];
223 timestamp = (timestamp << 8) | data[2 + i*8 + 7];
Martin Rothdfdff3e2015-09-18 09:42:03 -0600224 if (display_terminal)
225 read_spi_terminal(em100, 1);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700226 continue;
227 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600228
Stefan Reinauerc566d202015-08-25 09:52:42 -0700229 /* from here, it must be data */
230 if (cmd != cmdid) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600231 unsigned char spi_command = data[i * 8 + 4];
232 spi_cmd_vals = get_command_vals(spi_command);
233
Stefan Reinauerc566d202015-08-25 09:52:42 -0700234 /* new command */
235 cmdid = cmd;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600236 if (counter == 0)
237 start_timestamp = timestamp;
238
Stefan Reinauerdfb107d2020-12-10 19:22:40 -0800239 /* Special commands */
240 switch (spi_command) {
241 case 0xb7:
242 address_mode = 4;
243 break;
244 case 0xe9:
245 address_mode = 3;
246 break;
247 }
248
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800249 /* skip command byte */
250 j = 1;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600251
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800252 /* Set up address if used by this command */
253 switch (spi_cmd_vals->address_type) {
254 case ADDR_DYNAMIC:
255 address_bytes = address_mode;
256 break;
257 case ADDR_NO_OFF_3B:
258 case ADDR_3B:
259 address_bytes = 3;
260 break;
261 case ADDR_4B:
262 address_bytes = 4;
263 break;
264 case ADDR_NONE:
265 break;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600266 }
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800267
268 if (address_bytes == 3)
269 address = (data[i * 8 + 5] << 16)
270 + (data[i * 8 + 6] << 8)
271 + data[i * 8 + 7];
272 else if (address_bytes == 4)
273 address = (data[i * 8 + 5] << 24)
274 + (data[i * 8 + 6] << 16)
275 + (data[i * 8 + 7] << 8)
276 + data[i * 8 + 8];
277
278 /* skip address bytes and padding */
279 j += address_bytes + spi_cmd_vals->pad_bytes;
280
281 if (j > MAX_TRACE_BLOCKLENGTH) {
282 additional_pad_bytes = j -
283 MAX_TRACE_BLOCKLENGTH;
284 j = MAX_TRACE_BLOCKLENGTH;
285 }
286
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800287 if (trace_brief) {
288 if (start_timestamp)
289 start_timestamp = 0;
290
291 if (spi_cmd_vals->address_type != ADDR_NONE)
292 printf("0x%02x @ 0x%08lx (%s)\n",
293 spi_command, address, spi_cmd_vals->cmd_name);
294 else
295 printf("0x%02x (%s)\n",
296 spi_command, spi_cmd_vals->cmd_name);
297 } else {
298 printf("\nTime: %06lld.%08lld",
Martin Rothb54d6ba2015-09-29 14:49:37 -0600299 (timestamp - start_timestamp) /
300 100000000,
301 (timestamp - start_timestamp) %
302 100000000);
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800303 printf(" command # %-6d : 0x%02x - %s",
Martin Rothb54d6ba2015-09-29 14:49:37 -0600304 ++counter, spi_command,
305 spi_cmd_vals->cmd_name);
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800306 }
307
Stefan Reinauerc566d202015-08-25 09:52:42 -0700308 curpos = 0;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700309 outbytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700310 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600311
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800312 if (trace_brief) {
313 if (outbytes)
314 outbytes++;
315 } else {
316 /* this exploits 8bit wrap around in curpos */
317 unsigned char blocklen = (data[2 + i*8 + 1] - curpos);
318 blocklen /= 8;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600319
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800320 for (; j < blocklen; j++) {
321 if (outbytes == 0) {
322 switch (spi_cmd_vals->address_type) {
323 case ADDR_DYNAMIC:
324 case ADDR_3B:
325 case ADDR_4B:
326 printf("\n%08lx : ",
327 addr_offset + address);
328 break;
329 case ADDR_NO_OFF_3B:
330 printf("\n%08lx : ", address);
331 break;
332 case ADDR_NONE:
333 printf("\n : ");
334 break;
335 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600336 }
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800337 printf("%02x ", data[i * 8 + 4 + j]);
338 outbytes++;
339 if (outbytes == 16) {
340 outbytes = 0;
341 address += 16;
342 }
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700343 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700344 }
Martin Rothb54d6ba2015-09-29 14:49:37 -0600345 // this is because the em100 counts funny
346 curpos = data[2 + i*8 + 1] + 0x10;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600347 fflush(stdout);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700348 }
349 }
350 return 1;
351}
Martin Rothdfdff3e2015-09-18 09:42:03 -0600352
Stefan Reinauercafd1512020-12-11 17:36:28 -0800353int read_spi_trace_console(struct em100 *em100, unsigned long addr_offset,
354 unsigned long addr_len)
355{
356 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] =
357 {{0}};
358 unsigned char *data;
359 unsigned int count, i, report;
360 static int additional_pad_bytes = 0;
361 static unsigned int address = 0;
362 static int do_write = 0;
363 static struct spi_cmd_values *spi_cmd_vals = &spi_command_list[3];
364 int addr_bytes = 0;
365
366 if (addr_offset == 0) {
367 printf("Address offset for console buffer required\n");
368 return -1;
369 }
370 if (addr_len == 0) {
371 printf("Console buffer length required\n");
372 return -1;
373 }
374
375 if (!read_report_buffer(em100, reportdata))
376 return -1;
377
378 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
379 data = &reportdata[report][0];
380 count = (data[0] << 8) | data[1];
381 if (!count)
382 continue;
383 if (count > 1023) {
384 printf("Warning: EM100pro sends too much data: %d.\n", count);
385 count = 1023;
386 }
387 for (i = 0; i < count; i++) {
388 unsigned int j = additional_pad_bytes;
389 unsigned int address_bytes = 0;
390 additional_pad_bytes = 0;
391 unsigned char cmd = data[2 + i*8];
392
393 /* from here, it must be data */
394 if (cmd != cmdid) {
395 unsigned char spi_command = data[i * 8 + 4];
396 spi_cmd_vals = get_command_vals(spi_command);
397
398 /* new command */
399 cmdid = cmd;
400
401 /* Special commands */
402 switch (spi_command) {
403 case 0xb7:
404 address_mode = 4;
405 break;
406 case 0xe9:
407 address_mode = 3;
408 break;
409 }
410
411 /* skip command byte */
412 j = 1;
413
414 /* Set up address if used by this command */
415 switch (spi_cmd_vals->address_type) {
416 case ADDR_DYNAMIC:
417 address_bytes = address_mode;
418 break;
419 case ADDR_NO_OFF_3B:
420 case ADDR_3B:
421 address_bytes = 3;
422 break;
423 case ADDR_4B:
424 address_bytes = 4;
425 break;
426 case ADDR_NONE:
427 break;
428 }
429
430 if (address_bytes == 3)
431 address = (data[i * 8 + 5] << 16)
432 + (data[i * 8 + 6] << 8)
433 + data[i * 8 + 7];
434 else if (address_bytes == 4)
435 address = (data[i * 8 + 5] << 24)
436 + (data[i * 8 + 6] << 16)
437 + (data[i * 8 + 7] << 8)
438 + data[i * 8 + 8];
439
440 /* skip address bytes and padding */
441 j += address_bytes + spi_cmd_vals->pad_bytes;
442
443 if (j > MAX_TRACE_BLOCKLENGTH) {
444 additional_pad_bytes = j -
445 MAX_TRACE_BLOCKLENGTH;
446 j = MAX_TRACE_BLOCKLENGTH;
447 }
448
449#if 0
450 if (spi_cmd_vals->address_type != ADDR_NONE)
451 printf("0x%02x @ 0x%08x (%s)\n",
452 spi_command, address, spi_cmd_vals->cmd_name);
453 else
454 printf("0x%02x (%s)\n",
455 spi_command, spi_cmd_vals->cmd_name);
456#endif
457
458 curpos = 0;
459 if (spi_command == 0x02)
460 do_write = 1;
461 else
462 do_write = 0;
463 }
464
465 if (do_write == 0 ||
466 (spi_cmd_vals->address_type == ADDR_NONE ||
467 address < addr_offset ||
468 address > (addr_offset + addr_len))) {
469 curpos = data[2 + i*8 + 1] + 0x10;
470 continue;
471 }
472
473 /* this exploits 8bit wrap around in curpos */
474 unsigned char blocklen = (data[2 + i*8 + 1] - curpos);
475 blocklen /= 8;
476
477 for (; j < blocklen; j++) {
478 printf("%c", data[i * 8 + addr_bytes + j]);
479 }
480
481 /* this is because the em100 counts funny */
482 curpos = data[2 + i*8 + 1] + 0x10;
483 fflush(stdout);
484 }
485 }
486 return 1;
487}
488
Martin Rothdfdff3e2015-09-18 09:42:03 -0600489#define UFIFO_SIZE 512
490#define UFIFO_TIMEOUT 0x00
491
492/*
493 * Polls the uFIFO buffer to see if there's any data. The HT registers don't
494 * seem to ever be updated to reflect that there's data present, and the
495 * Dediprog software doesn't use them either.
Martin Rothc69319c2015-09-29 14:09:40 -0600496 *
Martin Rothdfdff3e2015-09-18 09:42:03 -0600497 * Multiple messages can be in a single uFIFO transfer, so loop through
498 * the data looking for the signature.
499 */
Stefan Reinauerabd3e322019-12-04 18:29:24 -0800500int read_spi_terminal(struct em100 *em100, int show_counter)
501{
Martin Rothdfdff3e2015-09-18 09:42:03 -0600502 unsigned char data[UFIFO_SIZE] = { 0 };
503 static unsigned int msg_counter = 1; /* Number of messages */
Martin Rothc69319c2015-09-29 14:09:40 -0600504 uint16_t data_length;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600505 unsigned char *data_start;
506 unsigned int j, k;
507 struct em100_msg *msg = NULL;
508
509 if (!read_ufifo(em100, UFIFO_SIZE, UFIFO_TIMEOUT, &data[0]))
510 return 0;
511
512 /* the first two bytes are the amount of valid data */
Martin Rothc69319c2015-09-29 14:09:40 -0600513 data_length = (data[0] << 8) + data[1];
514 if (data_length == 0)
Martin Rothdfdff3e2015-09-18 09:42:03 -0600515 return 1;
516
517 /* actual data starts after the length */
518 data_start = &data[sizeof(uint16_t)];
519
520 /* examine data; stop when we run out of message or buffer */
Martin Rothc69319c2015-09-29 14:09:40 -0600521 for (j = 0; j < data_length &&
Martin Rothdfdff3e2015-09-18 09:42:03 -0600522 j < UFIFO_SIZE - sizeof(struct em100_msg_header); j++) {
523
524 msg = (struct em100_msg *)(data_start + j);
525 if (msg->header.signature == EM100_MSG_SIGNATURE) {
526
527 if (show_counter)
528 printf("\nHT%06d: ", msg_counter);
529
530 /* print message byte according to format */
531 for (k = 0; k < msg->header.data_length; k++) {
Martin Rothc69319c2015-09-29 14:09:40 -0600532 if (&msg->data[k] >= data_start + data_length)
Martin Rothdfdff3e2015-09-18 09:42:03 -0600533 break;
534 if (&msg->data[k] >= &data[0] + UFIFO_SIZE)
535 break;
536
537 switch (msg->header.data_type) {
538 case ht_checkpoint_1byte:
539 case ht_checkpoint_2bytes:
540 case ht_checkpoint_4bytes:
541 case ht_hexadecimal_data:
542 case ht_timestamp_data:
543 printf("%02x ", msg->data[k]);
544 break;
545 case ht_ascii_data:
546 printf("%c", msg->data[k]);
547 break;
548 case ht_lookup_table:
549 /* TODO - support lookup table */
550 printf("Lookup unsupported: %02x%02x",
551 msg->data[k], msg->data[k + 1]);
552 k++;
553 break;
554 }
555 }
556
557 /* advance to the end of the message */
Martin Rothb54d6ba2015-09-29 14:49:37 -0600558 j += msg->header.data_length +
559 sizeof(struct em100_msg_header) - 1;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600560 msg_counter++;
561 fflush(stdout);
562 }
563 }
564
565 return 1;
566}
567
568int init_spi_terminal (struct em100 *em100)
569{
570 int retval = 0x01;
571 uint16_t val;
572
573 retval &= write_ht_register(em100, ufifo_data_fmt_reg, 0);
574 retval &= write_ht_register(em100, status_reg, START_SPI_EMULATION);
575
576 /* set em100 to recognize spi command 0x11 */
577 retval &= write_fpga_register(em100, 0x82, EM100_SPECIFIC_CMD);
578 retval &= read_fpga_register(em100, 0x28, &val);
579
580 return retval;
581}