Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include "em100.h" |
| 21 | |
| 22 | /* SPI Trace related operations */ |
| 23 | |
| 24 | /** |
| 25 | * reset_spi_trace: clear SPI trace buffer |
| 26 | * @param em100: em100 device structure |
| 27 | * |
| 28 | * out(16 bytes): 0xbd 0 .. 0 |
| 29 | */ |
| 30 | int reset_spi_trace(struct em100 *em100) |
| 31 | { |
| 32 | unsigned char cmd[16]; |
| 33 | memset(cmd, 0, 16); |
| 34 | cmd[0] = 0xbd; /* reset SPI trace buffer*/ |
| 35 | if (!send_cmd(em100->dev, cmd)) { |
| 36 | return 0; |
| 37 | } |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * read_spi_trace: fetch SPI trace data |
| 43 | * @param em100: em100 device structure |
| 44 | * globals: curpos, counter, cmdid |
| 45 | * |
| 46 | * out(16 bytes): bc 00 00 00 08 00 00 00 00 15 00 00 00 00 00 00 |
| 47 | * in(8x8192 bytes): 2 bytes (BE) number of records (0..0x3ff), |
| 48 | * then records of 8 bytes each |
| 49 | */ |
| 50 | static unsigned int counter = 0; |
| 51 | static unsigned char curpos = 0; |
| 52 | static unsigned char cmdid = 0xff; // timestamp, so never a valid command id |
| 53 | |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame^] | 54 | #define REPORT_BUFFER_LENGTH 8192 |
| 55 | #define REPORT_BUFFER_COUNT 8 |
| 56 | |
| 57 | static int read_report_buffer(struct em100 *em100, |
| 58 | unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH]) |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 59 | { |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame^] | 60 | unsigned char cmd[16] = {0}; |
| 61 | int len; |
| 62 | unsigned int report; |
| 63 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 64 | cmd[0] = 0xbc; /* read SPI trace buffer*/ |
| 65 | |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame^] | 66 | /* |
| 67 | * Trace length, unit is 4k according to specs |
| 68 | * |
| 69 | * cmd1..cmd4 are probably u32BE on how many |
| 70 | * reports (8192 bytes each) to fetch |
| 71 | */ |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 72 | cmd[1] = 0x00; |
| 73 | cmd[2] = 0x00; |
| 74 | cmd[3] = 0x00; |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame^] | 75 | cmd[4] = REPORT_BUFFER_COUNT; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 76 | /* Timeout in ms */ |
| 77 | cmd[5] = 0x00; |
| 78 | cmd[6] = 0x00; |
| 79 | cmd[7] = 0x00; |
| 80 | cmd[8] = 0x00; |
| 81 | /* Trace Config |
| 82 | * [1:0] 00 start/stop spi trace according to emulation status |
| 83 | * 01 start when TraceConfig[2] == 1 |
| 84 | * 10 start when trig signal goes high |
| 85 | * 11 RFU |
| 86 | * [2] When TraceConfig[1:0] == 01 this bit starts the trace |
| 87 | * [7:3] RFU |
| 88 | */ |
| 89 | cmd[9] = 0x15; |
| 90 | |
| 91 | if (!send_cmd(em100->dev, cmd)) { |
| 92 | printf("sending trace command failed\n"); |
| 93 | return 0; |
| 94 | } |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 95 | |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame^] | 96 | for (report = 0; report < REPORT_BUFFER_COUNT; report++) { |
| 97 | len = get_response(em100->dev, &reportdata[report][0], REPORT_BUFFER_LENGTH); |
| 98 | if (len != REPORT_BUFFER_LENGTH) { |
| 99 | printf("error, report length = %d instead of %d.\n\n", |
| 100 | len, REPORT_BUFFER_LENGTH); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame^] | 103 | } |
| 104 | |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | int read_spi_trace(struct em100 *em100) |
| 109 | { |
| 110 | unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] = {{0}}; |
| 111 | unsigned char *data; |
| 112 | unsigned int count, i, report; |
| 113 | static int outbytes = 0; |
| 114 | |
| 115 | if (!read_report_buffer(em100, reportdata)) |
| 116 | return 0; |
| 117 | |
| 118 | for (report = 0; report < REPORT_BUFFER_COUNT; report++) { |
| 119 | data = &reportdata[report][0]; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 120 | count = (data[0] << 8) | data[1]; |
| 121 | for (i = 0; i < count; i++) { |
| 122 | unsigned int j; |
| 123 | unsigned char cmd = data[2 + i*8]; |
| 124 | if (cmd == 0xff) { |
| 125 | /* timestamp */ |
| 126 | unsigned long long timestamp = 0; |
| 127 | timestamp = data[2 + i*8 + 2]; |
| 128 | timestamp = (timestamp << 8) | data[2 + i*8 + 3]; |
| 129 | timestamp = (timestamp << 8) | data[2 + i*8 + 4]; |
| 130 | timestamp = (timestamp << 8) | data[2 + i*8 + 5]; |
| 131 | timestamp = (timestamp << 8) | data[2 + i*8 + 6]; |
| 132 | timestamp = (timestamp << 8) | data[2 + i*8 + 7]; |
| 133 | printf("\ntimestamp: %lld.%lld", timestamp / 100000000, timestamp % 100000000); |
| 134 | continue; |
| 135 | } |
| 136 | #if 0 |
| 137 | printf("{(%d)", curpos); |
| 138 | for (j = 0; j < 8; j++) { |
| 139 | printf("%02x ", data[2 + i*8 + j]); |
| 140 | } |
| 141 | printf("}"); |
| 142 | #endif |
| 143 | /* from here, it must be data */ |
| 144 | if (cmd != cmdid) { |
| 145 | /* new command */ |
| 146 | cmdid = cmd; |
| 147 | printf("\nspi command %6d: ", ++counter); |
| 148 | curpos = 0; |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 149 | outbytes = 0; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 150 | } |
| 151 | /* this exploits 8bit wrap around in curpos */ |
| 152 | unsigned char blocklen = (data[2 + i*8 + 1] - curpos); |
| 153 | blocklen /= 8; |
| 154 | for (j = 0; j < blocklen; j++) { |
| 155 | printf("%02x ", data[2 + i*8 + 2 + j]); |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 156 | outbytes++; |
| 157 | if (outbytes == 16) { |
| 158 | outbytes=0; |
| 159 | printf("\n "); |
| 160 | } |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 161 | } |
| 162 | curpos = data[2 + i*8 + 1] + 0x10; // this is because the em100 counts funny |
| 163 | } |
| 164 | } |
| 165 | return 1; |
| 166 | } |