forked from openLRSng/openLRSng
-
Notifications
You must be signed in to change notification settings - Fork 10
/
binary_com.h
499 lines (435 loc) · 12 KB
/
binary_com.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Implementation of PSP (Phoenix Serial Protocol)
Protocol data structure:
[SYNC1][SYNC2][CODE][LENGTH_L][LENGTH_H][DATA/DATA ARRAY][CRC]
*/
bool binary_mode_active = false;
#define PSP_SYNC1 0xB5
#define PSP_SYNC2 0x62
#define PSP_REQ_BIND_DATA 1
#define PSP_REQ_RX_CONFIG 2
#define PSP_REQ_RX_JOIN_CONFIGURATION 3
#define PSP_REQ_SCANNER_MODE 4
#define PSP_REQ_SPECIAL_PINS 5
#define PSP_REQ_FW_VERSION 6
#define PSP_REQ_NUMBER_OF_RX_OUTPUTS 7
#define PSP_REQ_ACTIVE_PROFILE 8
#define PSP_REQ_RX_FAILSAFE 9
#define PSP_REQ_TX_CONFIG 10
#define PSP_REQ_PPM_IN 11
#define PSP_REQ_DEFAULT_PROFILE 12
#define PSP_SET_BIND_DATA 101
#define PSP_SET_RX_CONFIG 102
#define PSP_SET_TX_SAVE_EEPROM 103
#define PSP_SET_RX_SAVE_EEPROM 104
#define PSP_SET_TX_RESTORE_DEFAULT 105
#define PSP_SET_RX_RESTORE_DEFAULT 106
#define PSP_SET_ACTIVE_PROFILE 107
#define PSP_SET_RX_FAILSAFE 108
#define PSP_SET_TX_CONFIG 109
#define PSP_SET_DEFAULT_PROFILE 110
#define PSP_SET_EXIT 199
#define PSP_INF_ACK 201
#define PSP_INF_REFUSED 202
#define PSP_INF_CRC_FAIL 203
#define PSP_INF_DATA_TOO_LONG 204
extern volatile uint8_t ppmAge;
extern struct rxSpecialPinMap rxcSpecialPins[];
extern uint8_t rxcSpecialPinCount;
extern uint8_t rxcNumberOfOutputs;
extern uint16_t rxcVersion;
uint8_t rxcConnect();
uint16_t getChannel(uint8_t ch);
uint8_t PSP_crc;
#define AS_U8ARRAY(x) ((uint8_t *)(x))
void PSP_serialize_uint8(uint8_t data)
{
Serial.write(data);
PSP_crc ^= data;
}
void PSP_serialize_uint16(uint16_t data)
{
PSP_serialize_uint8(lowByte(data));
PSP_serialize_uint8(highByte(data));
}
void PSP_serialize_uint32(uint32_t data)
{
for (uint8_t i = 0; i < 4; i++) {
PSP_serialize_uint8((uint8_t) (data >> (i * 8)));
}
}
void PSP_serialize_uint64(uint64_t data)
{
for (uint8_t i = 0; i < 8; i++) {
PSP_serialize_uint8((uint8_t) (data >> (i * 8)));
}
}
void PSP_serialize_float32(float f)
{
uint8_t *b = (uint8_t*) & f;
for (uint8_t i = 0; i < sizeof(f); i++) {
PSP_serialize_uint8(b[i]);
}
}
void PSP_protocol_head(uint8_t code, uint16_t length)
{
PSP_crc = 0; // reset crc
Serial.write(PSP_SYNC1);
Serial.write(PSP_SYNC2);
PSP_serialize_uint8(code);
PSP_serialize_uint16(length);
}
void PSP_protocol_tail()
{
Serial.write(PSP_crc);
}
void PSP_ACK()
{
PSP_protocol_head(PSP_INF_ACK, 1);
PSP_serialize_uint8(0x01);
}
void PSP_process_data(uint8_t code, uint16_t payload_length_received, uint8_t data_buffer[])
{
switch (code) {
case PSP_REQ_BIND_DATA:
PSP_protocol_head(PSP_REQ_BIND_DATA, sizeof(bind_data));
{
for (uint16_t i = 0; i < sizeof(bind_data); i++) {
PSP_serialize_uint8(AS_U8ARRAY(&bind_data)[i]);
}
}
break;
case PSP_REQ_RX_CONFIG:
PSP_protocol_head(PSP_REQ_RX_CONFIG, sizeof(rx_config));
{
for (uint16_t i = 0; i < sizeof(rx_config); i++) {
PSP_serialize_uint8(AS_U8ARRAY(&rx_config)[i]);
}
}
break;
case PSP_REQ_RX_JOIN_CONFIGURATION:
PSP_protocol_head(PSP_REQ_RX_JOIN_CONFIGURATION, 1);
// 1 success, 2 timeout, 3 failed response
PSP_serialize_uint8(rxcConnect());
break;
case PSP_REQ_SCANNER_MODE:
PSP_protocol_head(PSP_REQ_SCANNER_MODE, 1);
PSP_serialize_uint8(0x01);
PSP_protocol_tail();
scannerMode();
return;
break;
case PSP_REQ_SPECIAL_PINS:
PSP_protocol_head(PSP_REQ_SPECIAL_PINS, sizeof(struct rxSpecialPinMap) * rxcSpecialPinCount);
{
for (uint16_t i = 0; i < sizeof(struct rxSpecialPinMap) * rxcSpecialPinCount; i++) {
PSP_serialize_uint8(AS_U8ARRAY(&rxcSpecialPins)[i]);
}
}
break;
case PSP_REQ_FW_VERSION:
PSP_protocol_head(PSP_REQ_FW_VERSION, sizeof(version));
{
PSP_serialize_uint16(version);
}
break;
case PSP_REQ_NUMBER_OF_RX_OUTPUTS:
PSP_protocol_head(PSP_REQ_NUMBER_OF_RX_OUTPUTS, 1);
{
PSP_serialize_uint8(rxcNumberOfOutputs);
}
break;
case PSP_REQ_ACTIVE_PROFILE:
PSP_protocol_head(PSP_REQ_ACTIVE_PROFILE, 1);
{
PSP_serialize_uint8(activeProfile);
}
break;
case PSP_REQ_RX_FAILSAFE: {
uint8_t rxtx_buf;
rxtx_buf = 'f';
tx_packet(&rxtx_buf, 1);
rx_reset();
RF_Mode = Receive;
delay(200);
if (RF_Mode == Received) {
spiSendAddress(0x7f);
rxtx_buf = spiReadData();
if (rxtx_buf == 'F') {
PSP_protocol_head(PSP_REQ_RX_FAILSAFE, 32);
for (uint8_t i = 0; i < 32; i++) {
PSP_serialize_uint8(spiReadData()); // failsafe data
}
} else {
PSP_protocol_head(PSP_REQ_RX_FAILSAFE, 1);
PSP_serialize_uint8(0x01); // failsafe not set
}
} else {
PSP_protocol_head(PSP_REQ_RX_FAILSAFE, 1);
PSP_serialize_uint8(0x00); // fail
}
}
break;
case PSP_REQ_TX_CONFIG: {
PSP_protocol_head(PSP_REQ_TX_CONFIG, sizeof(tx_config));
{
// Force correct TX type based on firmware
#if (RFMTYPE == 868)
tx_config.rfm_type = 1;
#elif (RFMTYPE == 915)
tx_config.rfm_type = 2;
#else
tx_config.rfm_type = 0;
#endif
// Set current watchdog status
if (watchdogUsed) {
tx_config.flags |= WATCHDOG_USED;
} else {
tx_config.flags &=~ WATCHDOG_USED;
}
for (uint16_t i = 0; i < sizeof(tx_config); i++) {
PSP_serialize_uint8(AS_U8ARRAY(&tx_config)[i]);
}
}
}
break;
case PSP_REQ_PPM_IN: {
PSP_protocol_head(PSP_REQ_PPM_IN, 33);
PSP_serialize_uint8((ppmAge < 255) ? ppmAge++ : ppmAge);
for (uint8_t i = 0; i < 16; i++) {
PSP_serialize_uint16(servoBits2Us(getChannel(i)));
}
}
break;
case PSP_REQ_DEFAULT_PROFILE:
PSP_protocol_head(PSP_REQ_DEFAULT_PROFILE, 1);
{
PSP_serialize_uint8(defaultProfile);
}
break;
// SET
case PSP_SET_BIND_DATA:
PSP_protocol_head(PSP_SET_BIND_DATA, 1);
if (payload_length_received == sizeof(bind_data)) {
for (uint16_t i = 0; i < sizeof(bind_data); i++) {
AS_U8ARRAY(&bind_data)[i] = data_buffer[i];
}
PSP_serialize_uint8(0x01);
} else {
PSP_serialize_uint8(0x00);
}
break;
case PSP_SET_RX_CONFIG:
PSP_protocol_head(PSP_SET_RX_CONFIG, 1);
if (payload_length_received == sizeof(rx_config)) {
for (uint16_t i = 0; i < sizeof(rx_config); i++) {
AS_U8ARRAY(&rx_config)[i] = data_buffer[i];
}
PSP_serialize_uint8(0x01);
} else {
PSP_serialize_uint8(0x00);
}
break;
case PSP_SET_TX_SAVE_EEPROM:
PSP_protocol_head(PSP_SET_TX_SAVE_EEPROM, 1);
txWriteEeprom();
PSP_serialize_uint8(0x01);
break;
case PSP_SET_RX_SAVE_EEPROM:
PSP_protocol_head(PSP_SET_RX_SAVE_EEPROM, 1);
{
uint8_t tx_buf[1 + sizeof(rx_config)];
tx_buf[0] = 'u';
memcpy(tx_buf + 1, &rx_config, sizeof(rx_config));
tx_packet(tx_buf, sizeof(rx_config) + 1);
rx_reset();
RF_Mode = Receive;
delay(800);
if (RF_Mode == Received) {
spiSendAddress(0x7f); // Send the package read command
tx_buf[0] = spiReadData();
if (tx_buf[0]=='U') {
PSP_serialize_uint8(0x01); // success
} else {
PSP_serialize_uint8(0x00); // fail
}
} else {
PSP_serialize_uint8(0x00); // fail
}
}
break;
case PSP_SET_TX_RESTORE_DEFAULT:
PSP_protocol_head(PSP_SET_TX_RESTORE_DEFAULT, 1);
bindInitDefaults();
txInitDefaults();
PSP_serialize_uint8(0x01); // done
break;
case PSP_SET_RX_RESTORE_DEFAULT:
PSP_protocol_head(PSP_SET_RX_RESTORE_DEFAULT, 1);
uint8_t tx_buf[1 + sizeof(rx_config)];
tx_buf[0] = 'i';
tx_packet(tx_buf,1);
rx_reset();
RF_Mode = Receive;
delay(800);
if (RF_Mode == Received) {
spiSendAddress(0x7f); // Send the package read command
tx_buf[0] = spiReadData();
for (uint8_t i = 0; i < sizeof(rx_config); i++) {
tx_buf[i + 1] = spiReadData();
}
memcpy(&rx_config, tx_buf + 1, sizeof(rx_config));
if (tx_buf[0] == 'I') {
PSP_serialize_uint8(0x01); // success
} else {
PSP_serialize_uint8(0x00); // fail
}
} else {
PSP_serialize_uint8(0x00); // fail
}
break;
case PSP_SET_ACTIVE_PROFILE:
PSP_protocol_head(PSP_SET_ACTIVE_PROFILE, 1);
activeProfile=data_buffer[0];
txReadEeprom();
PSP_serialize_uint8(0x01); // done
break;
case PSP_SET_RX_FAILSAFE:
PSP_protocol_head(PSP_SET_RX_FAILSAFE, 1);
{
uint8_t rxtx_buf[33];
if (payload_length_received == 32) {
rxtx_buf[0] = 'g';
memcpy(rxtx_buf + 1, data_buffer, 32);
tx_packet(rxtx_buf, 33);
} else {
rxtx_buf[0] = 'G';
tx_packet(rxtx_buf, 1);
}
rx_reset();
RF_Mode = Receive;
delay(1100);
if (RF_Mode == Received) {
spiSendAddress(0x7f);
if (spiReadData() == 'G') {
PSP_serialize_uint8(0x01);
} else {
PSP_serialize_uint8(0x00);
}
} else {
PSP_serialize_uint8(0x00);
}
}
break;
case PSP_SET_TX_CONFIG:
PSP_protocol_head(PSP_SET_TX_CONFIG, 1);
if (payload_length_received == sizeof(tx_config)) {
for (uint16_t i = 0; i < sizeof(tx_config); i++) {
AS_U8ARRAY(&tx_config)[i] = data_buffer[i];
}
PSP_serialize_uint8(0x01);
} else {
PSP_serialize_uint8(0x00);
}
break;
case PSP_SET_DEFAULT_PROFILE:
PSP_protocol_head(PSP_SET_DEFAULT_PROFILE, 1);
setDefaultProfile(data_buffer[0]);
PSP_serialize_uint8(0x01); // done
break;
case PSP_SET_EXIT:
PSP_protocol_head(PSP_SET_EXIT, 1);
PSP_serialize_uint8(0x01);
PSP_protocol_tail();
binary_mode_active = false;
return;
break;
default: // Unrecognized code
PSP_protocol_head(PSP_INF_REFUSED, 1);
PSP_serialize_uint8(0x00);
}
// send over crc
PSP_protocol_tail();
}
void PSP_read(void)
{
static uint8_t data;
static uint8_t state;
static uint8_t code;
static uint8_t message_crc;
static uint16_t payload_length_expected;
static uint16_t payload_length_received;
static uint8_t data_buffer[100];
while (Serial.available()) {
data = Serial.read();
switch (state) {
case 0:
if (data == PSP_SYNC1) {
state++;
}
break;
case 1:
if (data == PSP_SYNC2) {
state++;
} else {
state = 0; // Restart and try again
}
break;
case 2:
code = data;
message_crc = data;
state++;
break;
case 3: // LSB
payload_length_expected = data;
message_crc ^= data;
state++;
break;
case 4: // MSB
payload_length_expected |= data << 8;
message_crc ^= data;
state++;
if (payload_length_expected > sizeof(data_buffer)) {
// Message too long, we won't accept
PSP_protocol_head(PSP_INF_DATA_TOO_LONG, 1);
PSP_serialize_uint8(0x01);
PSP_protocol_tail();
state = 0; // Restart
}
break;
case 5:
data_buffer[payload_length_received] = data;
message_crc ^= data;
payload_length_received++;
if (payload_length_received >= payload_length_expected) {
state++;
}
break;
case 6:
if (message_crc == data) {
// CRC is ok, process data
PSP_process_data(code, payload_length_received, data_buffer);
} else {
// respond that CRC failed
PSP_protocol_head(PSP_INF_CRC_FAIL, 2);
PSP_serialize_uint8(code);
PSP_serialize_uint8(PSP_crc);
}
// reset variables
memset(data_buffer, 0, sizeof(data_buffer));
payload_length_received = 0;
state = 0;
break;
}
}
}
void binaryMode()
{
// Just entered binary mode, flip the bool
binary_mode_active = true;
while (binary_mode_active == true) { // LOCK user here until exit command is received
if (Serial.available()) {
PSP_read();
}
}
}