-
Notifications
You must be signed in to change notification settings - Fork 48
/
i2crepl.ino
412 lines (397 loc) · 12.5 KB
/
i2crepl.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Get this from https://github.com/rambo/I2C
#define I2C_DEVICE_DEBUG
#include <I2C.h>
#define MAX_COMMAND_LENGTH 100 // null-terminated
char incoming_command[MAX_COMMAND_LENGTH + 2]; //Reserve space for CRLF too.
byte incoming_position;
/**
* Basic idea is to have "REPL" for low-level I2C operations
*
* [ -> start
* ] -> stop
* a1 -> hex numbers are bytes
* r -> read one byte
* = -> address calculator (for example "=4")
* S -> scan I2C address space
*
* On newline the line is parsed and corresponding actions taken, we need to know if sending a byte right after start since
* the slave address requires extra attention.
*
* Working:
* - start / stop
* - hex parsing (mostly) and sending bytes
* - reading
* - address scan
*
* TODO:
* - REPL so this can be used via plain serial port as well
* - Smarter number parsing (0x to signify hex, othewise suppose decimal)
*/
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(115200);
// Initialize I2C library manually
I2c.begin();
I2c.timeOut(500);
I2c.pullup(true);
// Scan the bus
//I2c.scan();
Serial.println(F("Remember that you need to send the 8-bit address (with R/W-bit set) when addressing a device"));
digitalWrite(13, LOW);
}
// Handle incoming Serial data, try to find a command in there
inline void read_command_bytes()
{
for (byte d = Serial.available(); d > 0; d--)
{
incoming_command[incoming_position] = Serial.read();
// Check for line end and in such case do special things
if (incoming_command[incoming_position] == 0xA // LF
|| incoming_command[incoming_position] == 0xD) // CR
{
incoming_command[incoming_position] = 0x0;
if (incoming_position > 0 && (incoming_command[incoming_position - 1] == 0xD // CR
|| incoming_command[incoming_position - 1] == 0xA) // LF
)
{
incoming_command[incoming_position - 1] = 0x0;
}
process_command();
// Clear the buffer and reset position to 0
memset(incoming_command, 0, MAX_COMMAND_LENGTH + 2);
incoming_position = 0;
return;
}
incoming_position++;
// Sanity check buffer sizes
if (incoming_position > MAX_COMMAND_LENGTH + 2)
{
Serial.println(0x15); // NACK
Serial.print(F("PANIC: No end-of-line seen and incoming_position="));
Serial.print(incoming_position, DEC);
Serial.println(F(" clearing buffers"));
memset(incoming_command, 0, MAX_COMMAND_LENGTH + 2);
incoming_position = 0;
}
}
}
enum parser_states
{
start_seen,
stop_seen,
in_hex,
p_idle,
calc_seen,
scan_seen,
};
inline boolean is_hex_char(byte current_char)
{
if ((current_char >= 0x30 && current_char <= 0x39) // 0-9
|| (current_char >= 0x61 && current_char <= 0x66) // a-f
|| (current_char >= 0x41 && current_char <= 0x46) // A-F
)
{
return true;
}
return false;
}
/**
* Parses ASCII [0-9A-F] hexadecimal to byte value
*/
inline byte ardubus_hex2byte(byte hexchar)
{
if (0x40 < hexchar && hexchar < 0x47) // A-F
{
return (hexchar - 0x41) + 10;
}
if (0x60 < hexchar && hexchar < 0x67) // a-f
{
return (hexchar - 0x61) + 10;
}
if (0x2f < hexchar && hexchar < 0x3a) // 0-9
{
return (hexchar - 0x30);
}
return 0x0; // Failure.
}
inline byte ardubus_hex2byte(byte hexchar0, byte hexchar1)
{
return (ardubus_hex2byte(hexchar0) << 4) | ardubus_hex2byte(hexchar1);
}
inline int ardubus_hex2int(byte hexchar0, byte hexchar1, byte hexchar2, byte hexchar3)
{
return ardubus_hex2byte(hexchar0, hexchar1) << 8 | ardubus_hex2byte(hexchar2, hexchar3);
}
inline byte parse_hex(char *parsebuffer)
{
byte len = strlen(parsebuffer);
/*
Serial.print(F("DEBUG: hexbuffer len="));
Serial.println(len, DEC);
*/
if (len > 2)
{
//Serial.println(parsebuffer);
return 0xff;
}
if (len == 2)
{
return ardubus_hex2byte(parsebuffer[0], parsebuffer[1]);
}
return ardubus_hex2byte(parsebuffer[0]);
}
void invalid_char(byte character, byte pos)
{
Serial.print(F("Invalid character '"));
Serial.write(character);
Serial.print(F("' (0x"));
Serial.print(character, HEX);
Serial.print(F(") in position "));
Serial.print(pos, DEC);
Serial.println(F(" when parsing command"));
}
inline void process_command()
{
char hexparsebuffer[5];
// Clear buffer
memset(hexparsebuffer, 0, sizeof(hexparsebuffer));
volatile byte hexparsebuffer_i = 0;
byte parser_state = p_idle;
byte next_parser_state = p_idle; // might not be needed
byte prev_parser_state = p_idle; // might not be needed
byte maxsize = strlen(incoming_command);
for (byte i = 0; i < maxsize; i++)
{
byte current_char = incoming_command[i];
switch (parser_state)
{
case calc_seen:
{
prev_parser_state = parser_state;
if (is_hex_char(current_char))
{
parser_state = in_hex;
i--; // muck i so we re-enter at some point to the hex parser
}
else
{
Serial.print(F("calc_seen: "));
invalid_char(current_char, i);
return;
}
}
break;
case start_seen:
{
prev_parser_state = parser_state;
if (current_char == 0x20) // space
{
parser_state = p_idle;
}
else if (is_hex_char(current_char)) // Allow also hex directly after start
{
parser_state = in_hex;
i--; // muck i so we re-enter at some point to the hex parser
}
else
{
Serial.print(F("start_seen: "));
invalid_char(current_char, i);
return;
}
}
break;
case stop_seen:
{
// after stop we only allow space to return to idle
if (current_char == 0x20) // space
{
prev_parser_state = parser_state;
parser_state = p_idle;
}
else
{
Serial.print(F("stop_seen: "));
invalid_char(current_char, i);
return;
}
}
break;
case in_hex:
{
boolean is_valid_char = false;
if (is_hex_char(current_char))
{
is_valid_char = true;
hexparsebuffer[hexparsebuffer_i++] = current_char;
if (hexparsebuffer_i > 2)
{
Serial.println(F("Can only have byte wide hex strings"));
return;
}
}
if (current_char == 0x20 // space
|| (i == (maxsize - 1))) // end of string
{
is_valid_char = true;
byte parsed_byte = parse_hex(hexparsebuffer);
// Clear buffer
memset(hexparsebuffer, 0, sizeof(hexparsebuffer));
hexparsebuffer_i = 0;
// I2C status code
byte stat;
boolean i2c_sent = true;
switch (prev_parser_state)
{
case calc_seen:
i2c_sent = false;
Serial.print(F("device 0x"));
Serial.print(parsed_byte, HEX);
Serial.print(F(": read 0x"));
Serial.print(((parsed_byte << 1) | 0x1), HEX);
Serial.print(F(" write 0x"));
Serial.println(((parsed_byte << 1) | 0x0), HEX);
break;
case start_seen:
stat = I2c._sendAddress(parsed_byte);
Serial.print(F("sendAddress"));
break;
default:
stat = I2c._sendByte(parsed_byte);
Serial.print(F("sendByte"));
break;
}
if (i2c_sent)
{
Serial.print(F("(0x"));
Serial.print(parsed_byte, HEX);
Serial.print(F("), stat=0x"));
Serial.println(stat, HEX);
}
// Return state to idle
prev_parser_state = parser_state;
parser_state = p_idle;
}
if (!is_valid_char)
{
Serial.print(F("in_hex: "));
invalid_char(current_char, i);
return;
}
}
break;
case p_idle:
{
boolean is_valid_char = false;
switch (current_char)
{
case 0x3d: // ASCII "="
{
is_valid_char = true;
parser_state = calc_seen;
}
break;
case 0x53: // ASCII "S"
{
is_valid_char = true;
if (prev_parser_state != p_idle)
{
Serial.println(F("Address scan cannot be done in the middle of I2C transaction"));
return;
}
I2c.scan();
Serial.println(F(""));
Serial.println(F("Scan done."));
}
break;
case 0x20: // space
is_valid_char = true;
break;
case 0x5b: // ASCII "[", our start signifier
{
is_valid_char = true;
byte stat = I2c._start();
parser_state = start_seen;
Serial.print(F("START sent, stat=0x"));
Serial.println(stat, HEX);
}
break;
case 0x5d: // ASCII "]", our stop signifier
{
is_valid_char = true;
byte stat = I2c._stop();
Serial.print(F("STOP sent, stat=0x"));
Serial.println(stat, HEX);
parser_state = stop_seen;
}
break;
case 0x72: // ASCII "r",
case 0x52: // ASCII "R", read byte
{
is_valid_char = true;
// peek ahead to see if this was last r -command
boolean is_last = true;
byte peek_i = i + 1;
while (peek_i < maxsize)
{
switch (incoming_command[peek_i])
{
case 0x72: // ASCII "r",
case 0x52: // ASCII "R", read byte
{
is_last = false;
}
break;
case 0x20: // space, command separator
// no-op
break;
case 0x5b: // ASCII "[", our start signifier
case 0x5d: // ASCII "]", our stop signifier
peek_i = maxsize - 1; // trigger break
break;
default:
// Any other command is in the wrong(est) place
Serial.print(F("r lookahead: "));
invalid_char(incoming_command[peek_i], peek_i);
return;
}
if (!is_last)
{
break;
}
peek_i++;
}
uint8_t tmpbuffer;
byte stat = I2c._receiveByte(!is_last, &tmpbuffer);
Serial.print(F("read 0x"));
Serial.print(tmpbuffer, HEX);
Serial.print(F(", stat=0x"));
Serial.println(stat, HEX);
}
break;
}
if (is_hex_char(current_char))
{
is_valid_char = true;
parser_state = in_hex;
i--; // muck i so we re-enter at some point to the hex parser
}
if (!is_valid_char)
{
Serial.print(F("p_idle: "));
invalid_char(current_char, i);
return;
}
}
break;
}
}
}
void loop()
{
read_command_bytes();
digitalWrite(13, !digitalRead(13));
delay(20);
}