forked from openLRSng/openLRSng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTX.h
535 lines (447 loc) · 12.5 KB
/
TX.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/****************************************************
* OpenLRSng transmitter code
****************************************************/
uint8_t RF_channel = 0;
uint8_t FSstate = 0; // 1 = waiting timer, 2 = send FS, 3 sent waiting btn release
uint32_t FStime = 0; // time when button went down...
uint32_t lastSent = 0;
uint32_t lastTelemetry = 0;
uint8_t RSSI_rx = 0;
uint8_t RSSI_tx = 0;
uint8_t RX_ain0 = 0;
uint8_t RX_ain1 = 0;
uint32_t sampleRSSI = 0;
volatile uint8_t ppmAge = 0; // age of PPM data
volatile uint8_t ppmCounter = PPM_CHANNELS; // ignore data until first sync pulse
volatile uint8_t ppmDetecting = 1; // countter for microPPM detection
volatile uint8_t ppmMicroPPM = 0; // status flag for 'Futaba microPPM mode'
#ifndef BZ_FREQ
#define BZ_FREQ 2000
#endif
/****************************************************
* Interrupt Vector
****************************************************/
static inline void processPulse(uint16_t pulse)
{
if (ppmDetecting) {
if (ppmDetecting>50) {
ppmDetecting=0;
if (ppmMicroPPM>10) {
ppmMicroPPM=1;
} else {
ppmMicroPPM=0;
}
// Serial.println(ppmMicroPPM?"Futaba micro mode":"Normal PPM mode");
} else {
if (pulse<1500) {
ppmMicroPPM++;
}
ppmDetecting++;
}
} else {
if (!ppmMicroPPM) {
pulse>>=1; // divide by 2 to get servo value on normal PPM
}
if (pulse > 2500) { // Verify if this is the sync pulse (2.5ms)
ppmCounter = 0; // -> restart the channel counter
ppmAge = 0; // brand new PPM data received
} else if ((pulse > 700) && (ppmCounter < PPM_CHANNELS)) { // extra channels will get ignored here
PPM[ppmCounter++] = servoUs2Bits(pulse); // Store measured pulse length (converted)
} else {
ppmCounter = PPM_CHANNELS; // glitch ignore rest of data
}
}
}
#ifdef USE_ICP1 // Use ICP1 in input capture mode
volatile uint16_t startPulse = 0;
ISR(TIMER1_CAPT_vect)
{
uint16_t stopPulse = ICR1;
processPulse(stopPulse - startPulse); // as top is 65535 uint16 math will take care of rollover
startPulse = stopPulse; // Save time at pulse start
}
void setupPPMinput()
{
ppmDetecting = 1;
ppmMicroPPM = 0;
// Setup timer1 for input capture (PSC=8 -> 0.5ms precision, falling edge)
TCCR1A = ((1 << WGM10) | (1 << WGM11));
TCCR1B = ((1 << WGM12) | (1 << WGM13) | (1 << CS11));
OCR1A = 65535;
TIMSK1 |= (1 << ICIE1); // Enable timer1 input capture interrupt
}
#else // sample PPM using pinchange interrupt
ISR(PPM_Signal_Interrupt)
{
uint16_t pulseWidth;
if (!PPM_Signal_Edge_Check) { // Falling edge detected
pulseWidth = TCNT1; // read the timer1 value
TCNT1 = 0; // reset the timer1 value for next
processPulse(pulseWidth);
}
}
void setupPPMinput(void)
{
ppmDetecting = 1;
ppmMicroPPM = 0;
// Setup timer1 for input capture (PSC=8 -> 0.5ms precision, top at 20ms)
TCCR1A = ((1 << WGM10) | (1 << WGM11));
TCCR1B = ((1 << WGM12) | (1 << WGM13) | (1 << CS11));
OCR1A = 65535;
TIMSK1 = 0;
PPM_Pin_Interrupt_Setup
}
#endif
void bindMode(void)
{
uint32_t prevsend = millis();
uint8_t tx_buf[sizeof(bind_data)+1];
boolean sendBinds = 1;
init_rfm(1);
while (Serial.available()) {
Serial.read(); // flush serial
}
while (1) {
if (sendBinds & (millis() - prevsend > 200)) {
prevsend = millis();
Green_LED_ON;
buzzerOn(BZ_FREQ);
tx_buf[0]='b';
memcpy(tx_buf+1,&bind_data, sizeof(bind_data));
tx_packet(tx_buf, sizeof(bind_data)+1);
Green_LED_OFF;
buzzerOff();
RF_Mode = Receive;
rx_reset();
delay(50);
if (RF_Mode == Received) {
RF_Mode = Receive;
spiSendAddress(0x7f); // Send the package read command
if ('B' == spiReadData()) {
sendBinds=0;
}
}
}
if (!digitalRead(BTN)) {
sendBinds=1;
}
while (Serial.available()) {
switch (Serial.read()) {
case '\n':
case '\r':
Serial.println(F("Enter menu..."));
handleCLI();
break;
case '#':
scannerMode();
break;
case 'B':
binaryMode();
break;
default:
break;
}
}
}
}
void checkButton(void)
{
uint32_t time, loop_time;
if (digitalRead(BTN) == 0) { // Check the button
delay(200); // wait for 200mS with buzzer ON
buzzerOff();
time = millis(); //set the current time
loop_time = time;
while (millis() < time + 4800) {
if (digitalRead(BTN)) {
goto just_bind;
}
}
// Check the button again, If it is still down reinitialize
if (0 == digitalRead(BTN)) {
int8_t bzstate = HIGH;
uint8_t doDefaults = 0;
buzzerOn(bzstate?BZ_FREQ:0);
loop_time = millis();
while (0 == digitalRead(BTN)) { // wait for button to release
if (loop_time > time + 9800) {
buzzerOn(BZ_FREQ);
doDefaults = 1;
} else {
if ((millis() - loop_time) > 200) {
loop_time = millis();
bzstate = !bzstate;
buzzerOn(bzstate?BZ_FREQ:0);
}
}
}
buzzerOff();
randomSeed(micros()); // button release time in us should give us enough seed
if (doDefaults) {
bindInitDefaults();
}
bindRandomize();
bindWriteEeprom();
bindPrint();
}
just_bind:
// Enter binding mode, automatically after recoding or when pressed for shorter time.
Serial.println("Entering binding mode\n");
bindMode();
}
}
void checkBND(void)
{
if ((Serial.available() > 3) &&
(Serial.read() == 'B') && (Serial.read() == 'N') &&
(Serial.read() == 'D') && (Serial.read() == '!')) {
buzzerOff();
bindMode();
}
}
void checkFS(void)
{
switch (FSstate) {
case 0:
if (!digitalRead(BTN)) {
FSstate = 1;
FStime = millis();
}
break;
case 1:
if (!digitalRead(BTN)) {
if ((millis() - FStime) > 1000) {
FSstate = 2;
buzzerOn(BZ_FREQ);
}
} else {
FSstate = 0;
}
break;
case 2:
if (digitalRead(BTN)) {
buzzerOff();
FSstate = 0;
}
break;
}
}
uint8_t tx_buf[21];
uint8_t rx_buf[9];
#define SERIAL_BUFSIZE 32
uint8_t serial_buffer[SERIAL_BUFSIZE];
uint8_t serial_resend[9];
uint8_t serial_head;
uint8_t serial_tail;
uint8_t serial_okToSend; // 2 if it is ok to send serial instead of servo
void setup(void)
{
uint32_t start;
setupSPI();
#ifdef SDN_pin
pinMode(SDN_pin, OUTPUT); //SDN
digitalWrite(SDN_pin, 0);
#endif
//LED and other interfaces
pinMode(Red_LED, OUTPUT); //RED LED
pinMode(Green_LED, OUTPUT); //GREEN LED
#ifdef Red_LED2
pinMode(Red_LED2, OUTPUT); //RED LED
pinMode(Green_LED2, OUTPUT); //GREEN LED
#endif
pinMode(BTN, INPUT); //Buton
pinMode(PPM_IN, INPUT); //PPM from TX
digitalWrite(PPM_IN, HIGH); // enable pullup for TX:s with open collector output
#if defined (RF_OUT_INDICATOR)
pinMode(RF_OUT_INDICATOR, OUTPUT);
digitalWrite(RF_OUT_INDICATOR, LOW);
#endif
buzzerInit();
Serial.begin(115200);
if (bindReadEeprom()) {
Serial.println("Loaded settings from EEPROM\n");
} else {
Serial.print("EEPROM data not valid, reiniting\n");
bindInitDefaults();
bindWriteEeprom();
}
setupPPMinput();
ppmAge = 255;
setupRfmInterrupt();
init_rfm(0);
rfmSetChannel(RF_channel);
sei();
start=millis();
while ((ppmAge==255) && ((millis()-start)<2000));
buzzerOn(BZ_FREQ);
digitalWrite(BTN, HIGH);
Red_LED_ON ;
while (Serial.available()) {
Serial.read();
}
Serial.println("OpenLRSng TX starting");
delay(200);
checkBND();
// switch to userdefined baudrate here
TelemetrySerial.begin(bind_data.serial_baudrate);
checkButton();
Red_LED_OFF;
buzzerOff();
rx_reset();
serial_head=0;
serial_tail=0;
serial_okToSend=0;
if (bind_data.flags & TELEMETRY_FRSKY) {
frskyInit((bind_data.flags & TELEMETRY_MASK) == TELEMETRY_SMARTPORT);
} else if (bind_data.flags & TELEMETRY_MASK) {
}
}
void loop(void)
{
if (spiReadRegister(0x0C) == 0) { // detect the locked module and reboot
Serial.println("module locked?");
Red_LED_ON;
init_rfm(0);
rx_reset();
Red_LED_OFF;
}
while (TelemetrySerial.available() && (((serial_tail + 1) % SERIAL_BUFSIZE) != serial_head)) {
serial_buffer[serial_tail] = TelemetrySerial.read();
serial_tail = (serial_tail + 1) % SERIAL_BUFSIZE;
}
if (RF_Mode == Received) {
// got telemetry packet
lastTelemetry = micros();
if (!lastTelemetry) {
lastTelemetry=1; //fixup rare case of zero
}
RF_Mode = Receive;
spiSendAddress(0x7f); // Send the package read command
for (int16_t i = 0; i < 9; i++) {
rx_buf[i] = spiReadData();
}
if ((tx_buf[0] ^ rx_buf[0]) & 0x40) {
tx_buf[0]^=0x40; // swap sequence to ack
if ((rx_buf[0] & 0x38) == 0x38) {
uint8_t i;
// transparent serial data...
for (i=0; i<=(rx_buf[0]&7);) {
i++;
if (bind_data.flags & TELEMETRY_FRSKY) {
frskyUserData(rx_buf[i]);
} else {
TelemetrySerial.write(rx_buf[i]);
}
}
} else if ((rx_buf[0] & 0x3F)==0) {
RSSI_rx = rx_buf[1];
RX_ain0 = rx_buf[2];
RX_ain1 = rx_buf[3];
}
}
if (serial_okToSend==1) {
serial_okToSend=2;
}
if (serial_okToSend==3) {
serial_okToSend=0;
}
}
uint32_t time = micros();
if ((sampleRSSI) && ((time - sampleRSSI) >= 3000)) {
RSSI_tx = rfmGetRSSI();
sampleRSSI=0;
}
if ((time - lastSent) >= getInterval(&bind_data)) {
lastSent = time;
if (ppmAge < 8) {
ppmAge++;
if (lastTelemetry) {
if ((time - lastTelemetry) > getInterval(&bind_data)) {
// telemetry lost
if (!(bind_data.flags & MUTE_TX)) {
buzzerOn(BZ_FREQ);
}
lastTelemetry=0;
} else {
// telemetry link re-established
buzzerOff();
}
}
// Construct packet to be sent
tx_buf[0] &= 0xc0; //preserve seq. bits
if ((serial_tail!=serial_head) && (serial_okToSend == 2)) {
tx_buf[0] ^= 0x80; // signal new data on line
uint8_t bytes=0;
uint8_t maxbytes = 8;
if (getPacketSize(&bind_data) < 9) {
maxbytes = getPacketSize(&bind_data)-1;
}
while ((bytes<maxbytes) && (serial_head!=serial_tail)) {
bytes++;
tx_buf[bytes]=serial_buffer[serial_head];
serial_resend[bytes]=serial_buffer[serial_head];
serial_head=(serial_head + 1) % SERIAL_BUFSIZE;
}
tx_buf[0] |= (0x37 + bytes);
serial_resend[0]= bytes;
serial_okToSend = 3; // sent but not acked
} else if (serial_okToSend == 4) {
uint8_t i;
for (i = 0; i < serial_resend[0]; i++) {
tx_buf[i+1] = serial_resend[i+1];
}
tx_buf[0] |= (0x37 + serial_resend[0]);
serial_okToSend = 3; // sent but not acked
} else {
if (FSstate == 2) {
tx_buf[0] |= 0x01; // save failsafe
Red_LED_ON
} else {
tx_buf[0] |= 0x00; // servo positions
Red_LED_OFF
if (serial_okToSend==0) {
serial_okToSend = 1;
}
if (serial_okToSend==3) {
serial_okToSend = 4; // resend
}
}
cli(); // disable interrupts when copying servo positions, to avoid race on 2 byte variable
packChannels(bind_data.flags & 7, PPM, tx_buf + 1);
sei();
}
//Green LED will be on during transmission
Green_LED_ON ;
// Send the data over RF
rfmSetChannel(RF_channel);
tx_packet(tx_buf, getPacketSize(&bind_data));
//Hop to the next frequency
RF_channel++;
if ((RF_channel == MAXHOPS) || (bind_data.hopchannel[RF_channel] == 0)) {
RF_channel = 0;
}
// do not switch channel as we may receive telemetry on the old channel
if (bind_data.flags & TELEMETRY_MASK) {
RF_Mode = Receive;
rx_reset();
// tell loop to sample downlink RSSI
sampleRSSI=micros();
if (sampleRSSI==0) {
sampleRSSI=1;
}
}
} else {
if (ppmAge == 8) {
Red_LED_ON
}
ppmAge = 9;
// PPM data outdated - do not send packets
}
}
if (bind_data.flags & TELEMETRY_FRSKY) {
frskyUpdate(RX_ain0,RX_ain1,lastTelemetry?RSSI_rx:0,lastTelemetry?RSSI_tx:0);
}
//Green LED will be OFF
Green_LED_OFF;
checkFS();
}