forked from openLRSng/openLRSng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTX.h
376 lines (300 loc) · 9.16 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
/****************************************************
* 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;
volatile uint8_t ppmAge = 0; // age of PPM data
volatile uint16_t startPulse = 0;
volatile uint8_t ppmCounter = PPM_CHANNELS; // ignore data until first sync pulse
#define TIMER1_FREQUENCY_HZ 50
#define TIMER1_PRESCALER 8
#define TIMER1_PERIOD (F_CPU/TIMER1_PRESCALER/TIMER1_FREQUENCY_HZ)
#define BZ_FREQ 2000
#ifdef USE_ICP1 // Use ICP1 in input capture mode
/****************************************************
* Interrupt Vector
****************************************************/
ISR(TIMER1_CAPT_vect)
{
uint16_t stopPulse = ICR1;
// Compensate for timer overflow if needed
uint16_t pulseWidth = ((startPulse > stopPulse) ? TIMER1_PERIOD : 0) + stopPulse - startPulse;
if (pulseWidth > 5000) { // Verify if this is the sync pulse (2.5ms)
ppmCounter = 0; // -> restart the channel counter
ppmAge = 0; // brand new PPM data received
} else if ((pulseWidth > 1400) && (ppmCounter < PPM_CHANNELS)) { // extra channels will get ignored here
PPM[ppmCounter] = servoUs2Bits(pulseWidth / 2); // Store measured pulse length (converted)
ppmCounter++; // Advance to next channel
} else {
ppmCounter = PPM_CHANNELS; // glitch ignore rest of data
}
startPulse = stopPulse; // Save time at pulse start
}
void setupPPMinput()
{
// 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) | (1 << ICES1));
OCR1A = TIMER1_PERIOD;
TIMSK1 |= (1 << ICIE1); // Enable timer1 input capture interrupt
}
#else // sample PPM using pinchange interrupt
ISR(PPM_Signal_Interrupt)
{
uint16_t time_temp;
if (PPM_Signal_Edge_Check) { // Only works with rising edge of the signal
time_temp = TCNT1; // read the timer1 value
TCNT1 = 0; // reset the timer1 value for next
if (time_temp > 5000) { // new frame detection (>2.5ms)
ppmCounter = 0; // -> restart the channel counter
ppmAge = 0; // brand new PPM data received
} else if ((time_temp > 1400) && (ppmCounter < PPM_CHANNELS)) {
PPM[ppmCounter] = servoUs2Bits(time_temp / 2); // Store measured pulse length (converted)
ppmCounter++; // Advance to next channel
} else {
ppmCounter = PPM_CHANNELS; // glitch ignore rest of data
}
}
}
void setupPPMinput(void)
{
// 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 = TIMER1_PERIOD;
TIMSK1 = 0;
PPM_Pin_Interrupt_Setup
}
#endif
void bindMode(void)
{
uint32_t prevsend = millis();
init_rfm(1);
while (Serial.available()) {
Serial.read(); // flush serial
}
while (1) {
if (millis() - prevsend > 200) {
prevsend = millis();
Green_LED_ON;
buzzerOn(BZ_FREQ);
tx_packet((uint8_t*)&bind_data, sizeof(bind_data));
Green_LED_OFF;
buzzerOff();
}
while (Serial.available()) {
switch (Serial.read()) {
case '\n':
case '\r':
Serial.println(F("Enter menu..."));
handleCLI();
break;
case '#':
scannerMode();
break;
default:
break;
}
}
}
}
void checkButton(void)
{
uint32_t time, loop_time;
if (digitalRead(BTN) == 0) { // Check the button
delay(200); // wait for 200mS when buzzer ON
buzzerOff();
time = millis(); //set the current time
loop_time = time;
while ((digitalRead(BTN) == 0) && (loop_time < time + 4800)) {
// wait for button reelase if it is already pressed.
loop_time = millis();
}
// 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();
}
// Enter binding mode, automatically after recoding or when pressed for shorter time.
Serial.println("Entering binding mode\n");
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;
}
}
void setup(void)
{
//RF module pins
pinMode(SDO_pin, INPUT); //SDO
pinMode(SDI_pin, OUTPUT); //SDI
pinMode(SCLK_pin, OUTPUT); //SCLK
pinMode(IRQ_pin, INPUT); //IRQ
pinMode(nSel_pin, OUTPUT); //nSEL
//LED and other interfaces
pinMode(Red_LED, OUTPUT); //RED LED
pinMode(Green_LED, OUTPUT); //GREEN LED
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(SERIAL_BAUD_RATE);
if (bindReadEeprom()) {
Serial.println("Loaded settings from EEPROM\n");
} else {
Serial.print("EEPROM data not valid, reiniting\n");
bindInitDefaults();
bindWriteEeprom();
Serial.print("EEPROM data saved\n");
}
setupPPMinput();
attachInterrupt(IRQ_interrupt, RFM22B_Int, FALLING);
init_rfm(0);
rfmSetChannel(bind_data.hopchannel[RF_channel]);
sei();
buzzerOn(BZ_FREQ);
digitalWrite(BTN, HIGH);
Red_LED_ON ;
delay(100);
checkButton();
Red_LED_OFF;
buzzerOff();
ppmAge = 255;
rx_reset();
}
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;
}
if (RF_Mode == Received) {
uint8_t rx_buf[4];
// got telemetry packet
lastTelemetry = micros();
RF_Mode = Receive;
spiSendAddress(0x7f); // Send the package read command
for (int16_t i = 0; i < 4; i++) {
rx_buf[i] = spiReadData();
}
// Serial.println(rx_buf[0]); // print rssi value
}
uint32_t time = micros();
if ((time - lastSent) >= modem_params[bind_data.modem_params].interval) {
lastSent = time;
if (ppmAge < 8) {
uint8_t tx_buf[11];
ppmAge++;
if (lastTelemetry) {
if ((time - lastTelemetry) > modem_params[bind_data.modem_params].interval) {
// telemetry lost
buzzerOn(BZ_FREQ);
lastTelemetry=0;
} else {
// telemetry link re-established
buzzerOff();
}
}
// Construct packet to be sent
if (FSstate == 2) {
tx_buf[0] = 0xF5; // save failsafe
Red_LED_ON
} else {
tx_buf[0] = 0x5E; // servo positions
Red_LED_OFF
}
cli(); // disable interrupts when copying servo positions, to avoid race on 2 byte variable
tx_buf[1] = (PPM[0] & 0xff);
tx_buf[2] = (PPM[1] & 0xff);
tx_buf[3] = (PPM[2] & 0xff);
tx_buf[4] = (PPM[3] & 0xff);
tx_buf[5] = ((PPM[0] >> 8) & 3) | (((PPM[1] >> 8) & 3) << 2) | (((PPM[2] >> 8) & 3) << 4) | (((PPM[3] >> 8) & 3) << 6);
tx_buf[6] = (PPM[4] & 0xff);
tx_buf[7] = (PPM[5] & 0xff);
tx_buf[8] = (PPM[6] & 0xff);
tx_buf[9] = (PPM[7] & 0xff);
tx_buf[10] = ((PPM[4] >> 8) & 3) | (((PPM[5] >> 8) & 3) << 2) | (((PPM[6] >> 8) & 3) << 4) | (((PPM[7] >> 8) & 3) << 6);
sei();
//Green LED will be on during transmission
Green_LED_ON ;
// Send the data over RF
rfmSetChannel(bind_data.hopchannel[RF_channel]);
tx_packet(tx_buf, 11);
//Hop to the next frequency
RF_channel++;
if (RF_channel >= bind_data.hopcount) {
RF_channel = 0;
}
// do not switch channel as we may receive telemetry on the old channel
if (modem_params[bind_data.modem_params].flags & TELEMETRY_ENABLED) {
RF_Mode = Receive;
rx_reset();
}
} else {
if (ppmAge == 8) {
Red_LED_ON
}
ppmAge = 9;
// PPM data outdated - do not send packets
}
}
//Green LED will be OFF
Green_LED_OFF;
checkFS();
}