-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgeneric_debugger.ino
385 lines (295 loc) · 6.59 KB
/
generic_debugger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#define PIN_C2CK PB3
#define PIN_C2D PB4
#define PIN_TRIG PB5
#define C2_DEVID 0x00
#define C2_REVID 0x01
#define C2_FPCTL 0x02
#define C2_FPDAT 0xAD
#define TRY_COUNT_WAIT 100
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // HIGH - LED disabled
pinMode(PIN_C2CK, INPUT);
pinMode(PIN_C2D, INPUT);
pinMode(PIN_TRIG, OUTPUT);
digitalWrite(PIN_TRIG, LOW);
Serial.begin(115200);
Serial.setTimeout(10);
}
static u8 g_trig_counter = 0;
static u8 g_disable_interrupts = 1;
void enable_C2CK() {
if (g_disable_interrupts)
noInterrupts();
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(PIN_C2CK, HIGH);
pinMode(PIN_C2CK, OUTPUT);
if (g_trig_counter)
digitalWrite(PIN_TRIG, HIGH);
}
void disable_C2CK() {
if (g_trig_counter) {
digitalWrite(PIN_TRIG, LOW);
g_trig_counter--;
}
digitalWrite(PIN_C2CK, HIGH);
pinMode(PIN_C2CK, INPUT);
digitalWrite(LED_BUILTIN, HIGH);
if (g_disable_interrupts)
interrupts();
}
void enable_C2D() {
digitalWrite(PIN_C2D, HIGH);
pinMode(PIN_C2D, OUTPUT);
}
void disable_C2D() {
digitalWrite(PIN_C2D, HIGH);
pinMode(PIN_C2D, INPUT);
}
void strobe() {
digitalWrite(PIN_C2CK, LOW);
digitalWrite(PIN_C2CK, HIGH);
digitalWrite(PIN_C2CK, HIGH);
}
void reset() {
enable_C2CK();
digitalWrite(PIN_C2CK, LOW);
delayMicroseconds(25);
digitalWrite(PIN_C2CK, HIGH);
disable_C2CK();
// delayMicroseconds(5);
}
void halt() {
enable_C2CK();
strobe();
disable_C2CK();
}
void send_INS(u8 ins) {
digitalWrite(PIN_C2D, (ins & 0x01) ? HIGH : LOW);
strobe();
digitalWrite(PIN_C2D, (ins & 0x02) ? HIGH : LOW);
strobe();
}
void send_LENGTH(u8 len) {
digitalWrite(PIN_C2D, (len & 0x01) ? HIGH : LOW);
strobe();
digitalWrite(PIN_C2D, (len & 0x02) ? HIGH : LOW);
strobe();
}
bool addr_write(u8 addr) {
enable_C2CK();
// START field
strobe();
enable_C2D();
// INS field
send_INS(0x03);
// ADDRESS field
for(u8 i = 0; i < 8; ++i) {
digitalWrite(PIN_C2D, (addr & 0x01) ? HIGH : LOW);
strobe();
addr >>= 1;
}
disable_C2D();
// STOP field
strobe();
disable_C2CK();
return true;
}
bool addr_read(u8 *dst) {
enable_C2CK();
// START field
strobe();
enable_C2D();
// INS field
send_INS(0x02);
disable_C2D();
strobe();
// ADDRESS field
u8 addr = 0;
for(u8 i = 0; i < 8; ++i) {
addr >>= 1;
if (digitalRead(PIN_C2D) == HIGH)
addr |= 0x80;
strobe();
}
*dst = addr;
// STOP field
disable_C2CK();
return true;
}
bool data_write(u8 data) {
enable_C2CK();
// START field
strobe();
enable_C2D();
// INS field
send_INS(0x01);
// LENGTH field
send_LENGTH(0x00); // 1 byte
// DATA field
for(u8 i = 0; i < 8; ++i) {
digitalWrite(PIN_C2D, (data & 0x01) ? HIGH : LOW);
strobe();
data = data >> 1;
}
// WAIT field
disable_C2D();
strobe();
u32 try_count = 0;
while (digitalRead(PIN_C2D) == LOW) {
if (try_count++ >= TRY_COUNT_WAIT) {
disable_C2CK();
return false;
}
strobe();
}
// STOP field
strobe();
disable_C2CK();
return true;
}
bool data_read(u8 *dst) {
enable_C2CK();
// START field
strobe();
enable_C2D();
// INS field
send_INS(0x00);
// LENGTH field
send_LENGTH(0x00); // 1 byte
disable_C2D();
strobe();
// WAIT field
u32 try_count = 0;
while (digitalRead(PIN_C2D) == LOW) {
if (try_count++ >= TRY_COUNT_WAIT) {
disable_C2CK();
return false;
}
strobe();
}
strobe(); // last WAIT strobe
// DATA field
u8 data = 0;
for(u8 i = 0; i < 8; ++i) {
data >>= 1;
if (digitalRead(PIN_C2D) == HIGH)
data |= 0x80;
strobe();
}
*dst = data;
// STOP field
disable_C2CK();
return true;
}
void do_reset() {
reset();
Serial.println("OK");
}
void do_halt() {
halt();
Serial.println("OK");
}
void do_addr_write(const char *cmd) {
u8 addr = (hex2num(cmd[3]) << 4) + hex2num(cmd[4]);
bool res = addr_write(addr);
Serial.println(res ? "OK" : "ERR");
}
void do_addr_read() {
u8 addr;
bool res = addr_read(&addr);
if (res) {
char tmp[0x10];
snprintf(tmp, sizeof(tmp), "OK %02X", addr);
Serial.println(tmp);
} else
Serial.println("ERR");
}
void do_data_write(const char *cmd) {
u8 data = (hex2num(cmd[3]) << 4) + hex2num(cmd[4]);
bool res = data_write(data);
Serial.println(res ? "OK" : "ERR");
}
void do_data_read() {
u8 data;
bool res = data_read(&data);
if (res) {
char tmp[0x10];
snprintf(tmp, sizeof(tmp), "OK %02X", data);
Serial.println(tmp);
} else
Serial.println("ERR");
}
u8 hex2num(char ch) {
u8 r = 0;
if ((ch >= '0') && (ch <= '9'))
r = (u8)(ch - '0');
if ((ch >= 'a') && (ch <= 'f'))
r = (u8)(ch - 'a' + 10);
if ((ch >= 'A') && (ch <= 'F'))
r = (u8)(ch - 'A' + 10);
return r;
}
void do_set_trig(const char *cmd) {
g_trig_counter = hex2num(cmd[4]);
Serial.println("OK");
}
void do_hack() {
// Don't trust the name
// Just a custom command when you need to do something time-dependent
g_disable_interrupts = 0;
g_trig_counter = 0;
noInterrupts();
reset();
addr_write(C2_FPCTL);
data_write(0x02);
data_write(0x04);
digitalWrite(PIN_TRIG, HIGH);
data_write(0x01);
digitalWrite(PIN_TRIG, LOW);
interrupts();
g_disable_interrupts = 1;
Serial.println("OK");
}
void exec_cmd(const char *cmd) {
if ((cmd[0] == 'R') && (cmd[1] == 'S') && (cmd[2] == 'T')) {
do_reset();
} else if ((cmd[0] == 'H') && (cmd[1] == 'L') && (cmd[2] == 'T')) {
do_halt();
} else if ((cmd[0] == 'A') && (cmd[1] == 'W') && (cmd[2] == ' ')) {
do_addr_write(cmd);
} else if ((cmd[0] == 'A') && (cmd[1] == 'R')) {
do_addr_read();
} else if ((cmd[0] == 'D') && (cmd[1] == 'W') && (cmd[2] == ' ')) {
do_data_write(cmd);
} else if ((cmd[0] == 'D') && (cmd[1] == 'R')) {
do_data_read();
} else if ((cmd[0] == 'T') && (cmd[1] == 'R') && (cmd[2] == 'G') && (cmd[3] == ' ')) {
do_set_trig(cmd);
} else if ((cmd[0] == 'H') && (cmd[1] == 'A') && (cmd[2] == 'C') && (cmd[3] == 'K')) {
do_hack();
} else {
Serial.println("Unknown cmd");
}
}
void loop() {
// RST
// HLT
// AW AA
// AR
// DW DD
// DR
// TRG N
// HACK
char cmd_buf[6];
while (Serial.available() > 0) {
int n = Serial.readBytes(cmd_buf, sizeof(cmd_buf));
if ((n == sizeof(cmd_buf)) && (cmd_buf[sizeof(cmd_buf)-1] == '\n')) {
exec_cmd(cmd_buf);
} else
Serial.println("Wrong cmd format");
}
}