forked from gioblu/PJON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThroughSerial.h
434 lines (352 loc) · 12.4 KB
/
ThroughSerial.h
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* ThroughSerial data link layer
used as a Strategy by the PJON framework (included in version v11.2)
Compliant with TSDL (Tardy Serial Data Link) specification v2.0
Contributors:
- sticilface async reception development
- Fred Larsen, Development, testing and debugging
- Zbigniew Zasieczny, collision avoidance multi-drop RS485 (latency)
and SoftwareSerial compatibility
- Franketto (Arduino forum user) RS485 TX enable pin compatibility
- Endre Karlson separate RS485 enable pins handling, flush timing hack
___________________________________________________________________________
ThroughSerial, based on ThroughSerial, developed by sticilface
copyright 2018 by Giovanni Blu Mitolo All rights reserved
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
// START symbol 10010101 - 0x95 -
#define TS_START 149
// END symbol 11101010 - 0xea - ê
#define TS_END 234
// ESCAPE symbol 10111011 - 0xBB - »
#define TS_ESC 187
// Used to signal communication failure
#define TS_FAIL 65535
// Used for unused pin handling
#define TS_NOT_ASSIGNED 255
#include "Timing.h"
enum TS_state_t : uint8_t {
TS_WAITING,
TS_RECEIVING,
TS_WAITING_ESCAPE,
TS_WAITING_END,
TS_DONE
};
// Recommended receive time for this strategy, in microseconds
#ifndef TS_RECEIVE_TIME
#define TS_RECEIVE_TIME 0
#endif
class ThroughSerial {
public:
uint8_t buffer[PJON_PACKET_MAX_LENGTH] = {0};
uint16_t position = 0;
TS_state_t state = TS_WAITING;
PJON_SERIAL_TYPE serial;
/* Returns suggested delay related to the attempts passed as parameter: */
uint32_t back_off(uint8_t attempts) {
uint32_t result = attempts;
for(uint8_t d = 0; d < TS_BACK_OFF_DEGREE; d++)
result *= (uint32_t)(attempts);
return result + PJON_RANDOM(TS_COLLISION_DELAY);
};
/* Begin method, to be called on initialization:
(returns always true) */
bool begin(uint8_t did = 0) {
PJON_DELAY(PJON_RANDOM(TS_INITIAL_DELAY) + did);
_last_reception_time = PJON_MICROS();
return true;
};
/* Check if the channel is free for transmission: */
bool can_start() {
PJON_DELAY_MICROSECONDS(PJON_RANDOM(TS_COLLISION_DELAY));
if(
(state != TS_WAITING) ||
PJON_SERIAL_AVAILABLE(serial) ||
((uint32_t)(PJON_MICROS() - _last_reception_time) < TS_TIME_IN)
) return false;
return true;
};
/* Function called when a frame reception fails */
uint16_t fail(TS_state_t s) {
state = s;
return (uint16_t)TS_FAIL;
}
/* Returns the maximum number of attempts for each transmission: */
static uint8_t get_max_attempts() {
return TS_MAX_ATTEMPTS;
};
/* Returns the recommended receive time for this strategy: */
static uint16_t get_receive_time() {
return TS_RECEIVE_TIME;
};
/* Handle a collision: */
void handle_collision() {
PJON_DELAY_MICROSECONDS(PJON_RANDOM(TS_COLLISION_DELAY));
};
/* Receive Byte */
int16_t receive_byte() {
int16_t value = PJON_SERIAL_READ(serial);
if(value == -1) return -1;
_last_reception_time = PJON_MICROS();
return value;
};
/* It returns the state of the previous transmission: */
uint16_t receive_response() {
if(_fail) return TS_FAIL;
uint32_t time = PJON_MICROS();
uint8_t i = 0;
while((uint32_t)(PJON_MICROS() - time) < TS_RESPONSE_TIME_OUT) {
if(PJON_SERIAL_AVAILABLE(serial)) {
int16_t read = PJON_SERIAL_READ(serial);
_last_reception_time = PJON_MICROS();
if(read >= 0) {
if(_response[i++] != read) return TS_FAIL;
if(i == TS_RESPONSE_LENGTH) return PJON_ACK;
}
}
#if defined(_WIN32)
PJON_DELAY_MICROSECONDS(TS_RESPONSE_TIME_OUT / 10);
#endif
}
return TS_FAIL;
};
/* Receive a string: */
uint16_t receive_frame(uint8_t *data, uint16_t max_length) {
if( // Reception attempts are spaced by an interval
_last_call_time &&
(uint32_t)(PJON_MICROS() - _last_call_time) < _read_interval
) return TS_FAIL;
_last_call_time = PJON_MICROS();
if( // If reception timeout is reached discard data
(
(state == TS_RECEIVING) ||
(state == TS_WAITING_END) ||
(state == TS_WAITING_ESCAPE)
) &&
((uint32_t)(PJON_MICROS() - _last_reception_time) > TS_BYTE_TIME_OUT)
) return fail(TS_WAITING);
switch(state) {
case TS_WAITING: {
while(PJON_SERIAL_AVAILABLE(serial)) {
int16_t value = receive_byte();
if(value == -1) return TS_FAIL;
if(value == TS_START) {
position = 0;
return fail(TS_RECEIVING);
}
};
break;
}
case TS_RECEIVING: {
while(PJON_SERIAL_AVAILABLE(serial)) {
int16_t value = receive_byte();
if((value == TS_START) || (value == -1)) return fail(TS_WAITING);
if(value == TS_ESC) {
if(!PJON_SERIAL_AVAILABLE(serial))
return fail(TS_WAITING_ESCAPE);
else {
value = receive_byte();
if(value == -1) return fail(TS_WAITING);
value = value ^ TS_ESC;
if(
(value != TS_START) &&
(value != TS_ESC) &&
(value != TS_END)
) return fail(TS_WAITING);
buffer[position++] = (uint8_t)value;
continue;
}
}
if(max_length == 1)
return fail(TS_WAITING_END);
if(position + 1 >= PJON_PACKET_MAX_LENGTH)
return fail(TS_WAITING);
if(value == TS_END)
return fail(TS_DONE);
buffer[position++] = (uint8_t)value;
}
return TS_FAIL;
}
case TS_WAITING_ESCAPE: {
if(PJON_SERIAL_AVAILABLE(serial)) {
int16_t value = receive_byte();
if(value == -1) return fail(TS_WAITING);
value = value ^ TS_ESC;
if(
(value != TS_START) &&
(value != TS_ESC) &&
(value != TS_END)
) return fail(TS_WAITING);
buffer[position++] = (uint8_t)value;
return fail(TS_RECEIVING);
}
break;
}
case TS_WAITING_END: {
if(PJON_SERIAL_AVAILABLE(serial)) {
int16_t value = receive_byte();
if(value == -1) return fail(TS_WAITING);
if(value == TS_END) return fail(TS_DONE);
else return fail(TS_WAITING);
}
break;
}
case TS_DONE: {
memcpy(&data[0], &buffer[0], position);
prepare_response(buffer, position);
state = TS_WAITING;
return position;
}
};
return TS_FAIL;
};
/* Send a byte and wait for its transmission end */
void send_byte(uint8_t b) {
uint32_t time = PJON_MICROS();
int16_t result = 0;
while(
((result = PJON_SERIAL_WRITE(serial, b)) != 1) &&
((uint32_t)(PJON_MICROS() - time) < TS_BYTE_TIME_OUT)
);
if(result != 1) _fail = true;
};
/* The last 5 bytes of the frame are used as a unique identifier within
the response. PJON has CRC8 or CRC32 at the end of the packet, encoding
a CRC (that is a good hashing algorithm) and using 40 bits looks enough
to provide a relatively safe response that should be nearly flawless
(yield few false positives per millennia). */
void prepare_response(const uint8_t *buffer, uint16_t position) {
uint8_t raw = 0;
for(int8_t i = 0; i < TS_RESPONSE_LENGTH; i++) {
raw = buffer[(position - ((TS_RESPONSE_LENGTH - 1) - i)) - 1];
_response[i] = (
(raw == TS_START) || (raw == TS_ESC) || (raw == TS_END)
) ? (raw - 1) : raw; // Avoid encoding symbols
}
};
/* Send byte response to the packet's transmitter */
void send_response(uint8_t response) {
if(response == PJON_ACK) {
start_tx();
wait_RS485_pin_change();
for(uint8_t i = 0; i < TS_RESPONSE_LENGTH; i++)
send_byte(_response[i]);
PJON_SERIAL_FLUSH(serial);
wait_RS485_pin_change();
end_tx();
}
};
/* Send a string: */
void send_frame(uint8_t *data, uint16_t length) {
_fail = false;
start_tx();
uint16_t overhead = 2;
// Add frame flag
send_byte(TS_START);
for(uint16_t b = 0; b < length; b++) {
if(_fail) return;
// Byte-stuffing
if(
(data[b] == TS_START) ||
(data[b] == TS_ESC) ||
(data[b] == TS_END)
) {
send_byte(TS_ESC);
send_byte(data[b] ^ TS_ESC);
overhead++;
} else send_byte(data[b]);
}
send_byte(TS_END);
/* On RPI flush fails to wait until all bytes are transmitted
here RPI forced to wait blocking using delayMicroseconds */
#if defined(RPI) || defined(LINUX)
if(_bd)
PJON_DELAY_MICROSECONDS(
((1000000 / (_bd / 8)) + _flush_offset) * (overhead + length)
);
#endif
PJON_SERIAL_FLUSH(serial);
end_tx();
// Prepare expected response for the receive_response call
prepare_response(data, length);
};
/* Pass the Serial port where you want to operate with */
void set_serial(PJON_SERIAL_TYPE serial_port) {
serial = serial_port;
};
/* RS485 enable pins handling: */
void start_tx() {
if(_enable_RS485_txe_pin != TS_NOT_ASSIGNED) {
PJON_IO_WRITE(_enable_RS485_txe_pin, HIGH);
if(_enable_RS485_rxe_pin != TS_NOT_ASSIGNED)
PJON_IO_WRITE(_enable_RS485_rxe_pin, HIGH);
wait_RS485_pin_change();
}
};
void end_tx() {
if(_enable_RS485_txe_pin != TS_NOT_ASSIGNED) {
wait_RS485_pin_change();
PJON_IO_WRITE(_enable_RS485_txe_pin, LOW);
if(_enable_RS485_rxe_pin != TS_NOT_ASSIGNED)
PJON_IO_WRITE(_enable_RS485_rxe_pin, LOW);
}
};
#if defined(RPI) || defined(LINUX)
/* Pass baudrate to ThroughSerial
(needed only for RPI flush hack): */
void set_baud_rate(uint32_t baud) {
_bd = baud;
};
/* Set flush timing offset in microseconds between expected and real
serial byte transmission: */
void set_flush_offset(uint16_t offset) {
_flush_offset = offset;
};
#endif
/* Sets the interval between each read attempt from serial
(TS_READ_INTERVAL or 100 microseconds by default) to allow the buffer
to fill and to reduce the computation time consumed while polling for
incoming data. */
uint32_t get_read_interval() {
return _read_interval;
};
void set_read_interval(uint32_t t) {
_read_interval = t;
};
/* RS485 enable pins setters: */
void set_enable_RS485_pin(uint8_t pin) {
set_RS485_txe_pin(pin);
};
void set_RS485_rxe_pin(uint8_t pin) {
_enable_RS485_rxe_pin = pin;
PJON_IO_MODE(_enable_RS485_rxe_pin, OUTPUT);
}
void set_RS485_txe_pin(uint8_t pin) {
_enable_RS485_txe_pin = pin;
PJON_IO_MODE(_enable_RS485_txe_pin, OUTPUT);
}
void wait_RS485_pin_change() {
if(_enable_RS485_txe_pin != TS_NOT_ASSIGNED)
PJON_DELAY(_RS485_delay);
};
private:
#if defined(RPI) || defined(LINUX)
uint16_t _flush_offset = TS_FLUSH_OFFSET;
uint32_t _bd;
#endif
bool _fail = false;
uint8_t _response[TS_RESPONSE_LENGTH];
uint32_t _last_reception_time = 0;
uint32_t _last_call_time = 0;
uint8_t _enable_RS485_rxe_pin = TS_NOT_ASSIGNED;
uint8_t _enable_RS485_txe_pin = TS_NOT_ASSIGNED;
uint32_t _RS485_delay = TS_RS485_DELAY;
uint32_t _read_interval = TS_READ_INTERVAL;
};