|
@@ -1,682 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Frsky RX 2-way
|
3
|
|
- * By Midelic
|
4
|
|
- * on RCGroups
|
5
|
|
- * an adaptation from Kyrre Aalerud(Kreature)
|
6
|
|
- * 2012 Frsky rx demo code
|
7
|
|
- * http://www.rcgroups.com/forums/showthread.php?t=1667453
|
8
|
|
- * Thanks also to Phracturedblue and his deviation firmware
|
9
|
|
- **********************************
|
10
|
|
- */
|
11
|
|
-
|
12
|
|
-#include <avr/interrupt.h>
|
13
|
|
-#include <EEPROM.h>
|
14
|
|
-#include "iface_cc2500.h"
|
15
|
|
-//#define DEBUG
|
16
|
|
-//#define DEBUG_RSSI
|
17
|
|
-//#define DEBUG0
|
18
|
|
-//#define DEBUG1
|
19
|
|
-//#define DEBUG2
|
20
|
|
-//#define DEBUG3
|
21
|
|
-//#define DEBUG4
|
22
|
|
-//#define DEBUG5
|
23
|
|
-//#define RSSI_OVER_PPM 7
|
24
|
|
-
|
25
|
|
-#define FAILSAFE
|
26
|
|
-#define SPIBB
|
27
|
|
-//#define SPIHW
|
28
|
|
-#if defined SPIHW
|
29
|
|
- #include <SPI.h>
|
30
|
|
-#endif
|
31
|
|
-
|
32
|
|
-// Used for RSSI_OVER_PPM
|
33
|
|
-int rssi;
|
34
|
|
-const int rssi_offset = 71;
|
35
|
|
-const int rssi_min = -103;
|
36
|
|
-const int rssi_max = -96;
|
37
|
|
-
|
38
|
|
-#define chanel_number 8 //set the number of chanels
|
39
|
|
-#define SEEK_CHANSKIP 13
|
40
|
|
-#define MAX_MISSING_PKT 20
|
41
|
|
-#define FAILSAFE_MISSING_PKT 170
|
42
|
|
-#define PPM_FrLen 22500
|
43
|
|
-#define PPM_PulseLen 300
|
44
|
|
-#define default_servo_value 1500
|
45
|
|
-#define onState 0 //set polarity of the pulses: 1 is positive, 0 is negative
|
46
|
|
-#define sigPin 10
|
47
|
|
-
|
48
|
|
-#if defined(SPIBB)
|
49
|
|
- #define MO_pin 5 //D5
|
50
|
|
- #define MI_pin 6 //D6
|
51
|
|
- #define SCLK_pin 4 //D4
|
52
|
|
- #define CS 2 //D2
|
53
|
|
- #define GDO_pin 3 //D3 GDO0 pin
|
54
|
|
- #define SCK_on PORTD |= 0x10 //D4
|
55
|
|
- #define SCK_off PORTD &= 0xEF //D4
|
56
|
|
- #define MO_on PORTD |= 0x20 //D5
|
57
|
|
- #define MO_off PORTD &= 0xDF //D5
|
58
|
|
- #define MI_1 (PIND & 0x40) == 0x40 //D6 input
|
59
|
|
- #define MI_0 (PIND & 0x40) == 0x00 //D6
|
60
|
|
- #define CS_on PORTD |= 0x04 //D2
|
61
|
|
- #define CS_off PORTD &= 0xFB //D2
|
62
|
|
- #define GDO_1 (PIND & 0x08) == 0x08 //D3 input
|
63
|
|
- #define GDO_0 (PIND & 0x08) == 0x00 //D3
|
64
|
|
-#endif
|
65
|
|
-
|
66
|
|
-#define bind_pin A0 //C0 bind plug also servo8
|
67
|
|
-
|
68
|
|
-#define Servo1_OUT 7 //Servo1(D7)
|
69
|
|
-#define Servo2_OUT 8 //Servo2(B0)
|
70
|
|
-#define Servo3_OUT 9 //Servo3(B1)
|
71
|
|
-#define Servo4_OUT 10 //Servo4(B2)//PPM pin
|
72
|
|
-#define Servo5_OUT 11 //Servo5(B3)
|
73
|
|
-#define Servo6_OUT 12 //Servo6(B4)
|
74
|
|
-#define Servo7_OUT 13 //Servo7(B5)
|
75
|
|
-#define Servo8_OUT A0 //Servo8(C0)
|
76
|
|
-
|
77
|
|
-#define Servo1_OUT_HIGH PORTD |= _BV(7) //Servo1(D7)
|
78
|
|
-#define Servo2_OUT_HIGH PORTB |= _BV(0) //Servo2(B0)
|
79
|
|
-#define Servo3_OUT_HIGH PORTB |= _BV(1) //Servo3(B1)
|
80
|
|
-#define Servo4_OUT_HIGH PORTB |= _BV(2) //Servo4(B2)
|
81
|
|
-#define Servo5_OUT_HIGH PORTB |= _BV(3) //Servo5(B3)
|
82
|
|
-#define Servo6_OUT_HIGH PORTB |= _BV(4) //Servo6(B4)
|
83
|
|
-#define Servo7_OUT_HIGH PORTB |= _BV(5) //Servo7(B5)
|
84
|
|
-#define Servo8_OUT_HIGH PORTC |= _BV(0) //Servo8(C0)
|
85
|
|
-#define Servo_Ports_LOW PORTD &= 0x7F ; PORTB &= 0xc0; PORTC &=0xFE //all servos low
|
86
|
|
-
|
87
|
|
-#define LED_pin A3
|
88
|
|
-#define LED_ON PORTC |= _BV(3)
|
89
|
|
-#define LED_OFF PORTC &= ~_BV(3)
|
90
|
|
-#define NOP() __asm__ __volatile__("nop")
|
91
|
|
-
|
92
|
|
-
|
93
|
|
-// Globals:
|
94
|
|
-static uint8_t ccData[27];
|
95
|
|
-static uint8_t ccLen;
|
96
|
|
-static boolean packet = false;
|
97
|
|
-//static uint16_t time;
|
98
|
|
-static uint8_t channr;
|
99
|
|
-static uint8_t missingPackets = 0;
|
100
|
|
-uint8_t calData[60];
|
101
|
|
-uint8_t hopData[60];
|
102
|
|
-uint8_t listLength;
|
103
|
|
-uint8_t txid[2];
|
104
|
|
-static uint8_t counter = 0;
|
105
|
|
-volatile uint16_t Servo_data[10] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
|
106
|
|
-volatile byte scale;
|
107
|
|
-static byte jumper1 = 0;
|
108
|
|
-static byte jumper2 = 0;
|
109
|
|
-volatile int ppm[chanel_number];
|
110
|
|
-volatile bool failed = false;
|
111
|
|
-static uint16_t total_servo_time = 0;
|
112
|
|
-static byte cur_chan_numb = 0;
|
113
|
|
-boolean debug = false;
|
114
|
|
-int count = 0;
|
115
|
|
-uint16_t c[8];
|
116
|
|
-boolean debug2 = true;
|
117
|
|
-boolean debug3 = false;
|
118
|
|
-
|
119
|
|
-void setup()
|
120
|
|
-{
|
121
|
|
-
|
122
|
|
- #if defined(SPIBB)
|
123
|
|
- pinMode(MO_pin, OUTPUT); //SI
|
124
|
|
- pinMode(MI_pin, INPUT); //SO
|
125
|
|
- pinMode(SCLK_pin, OUTPUT); //SCLK
|
126
|
|
- pinMode(CS, OUTPUT); //CS output
|
127
|
|
- pinMode(GDO_pin, INPUT); //GDO0 pin
|
128
|
|
- SCK_off; //start sck low
|
129
|
|
- MO_off; //low
|
130
|
|
- #endif
|
131
|
|
-
|
132
|
|
- pinMode(LED_pin, OUTPUT);
|
133
|
|
- CS_on;
|
134
|
|
-
|
135
|
|
- #if defined(SPIHW)
|
136
|
|
- pinMode(CS, OUTPUT);
|
137
|
|
- pinMode(GDO_pin, INPUT);
|
138
|
|
- SPI.setClockDivider(SPI_CLOCK_DIV2);
|
139
|
|
- SPI.setBitOrder( MSBFIRST);
|
140
|
|
- SPI.begin();
|
141
|
|
- #endif
|
142
|
|
-
|
143
|
|
- pinMode(Servo1_OUT, OUTPUT); //Servo1
|
144
|
|
- pinMode(Servo2_OUT, OUTPUT); //Servo2
|
145
|
|
- pinMode(Servo3_OUT, OUTPUT); //Servo3
|
146
|
|
- pinMode(Servo4_OUT, OUTPUT); //Servo4
|
147
|
|
- //
|
148
|
|
- pinMode(Servo6_OUT, OUTPUT); //Servo6
|
149
|
|
- pinMode(Servo7_OUT, OUTPUT); //Servo7
|
150
|
|
- pinMode(Servo8_OUT, OUTPUT); //Servo8
|
151
|
|
- //Servo8_OUT_HIGH;//bindpin pullup
|
152
|
|
-
|
153
|
|
- #if defined DEBUG
|
154
|
|
- Serial.begin(115200);
|
155
|
|
- int8_t i;
|
156
|
|
- Serial.print("PartNum ");
|
157
|
|
- i = cc2500_readReg(CC2500_30_PARTNUM + CC2500_READ_BURST);
|
158
|
|
- Serial.println(i);
|
159
|
|
- delay(10);
|
160
|
|
- Serial.print("Version ");
|
161
|
|
- i = cc2500_readReg(CC2500_31_VERSION + CC2500_READ_BURST);
|
162
|
|
- Serial.println(i);
|
163
|
|
- #endif
|
164
|
|
-
|
165
|
|
- #if F_CPU == 16000000
|
166
|
|
- scale = 2;
|
167
|
|
- #elif F_CPU == 8000000
|
168
|
|
- scale = 1;
|
169
|
|
- #else
|
170
|
|
- #error // 8 or 16MHz only !
|
171
|
|
- #endif
|
172
|
|
-
|
173
|
|
-
|
174
|
|
-
|
175
|
|
- initialize(1); //binding
|
176
|
|
- binding();
|
177
|
|
- pinMode(Servo8_OUT, OUTPUT); //Servo8.bind pin is set to output again.
|
178
|
|
- initialize(0); //data
|
179
|
|
-
|
180
|
|
- jumper1 = PPM_jumper(); // Check the possible jumper positions for changing the receiver mode.
|
181
|
|
-
|
182
|
|
- if (jumper1 == 1) {
|
183
|
|
- //initiallize default ppm values
|
184
|
|
- for (int i = 0; i < chanel_number; i++) {
|
185
|
|
- ppm[i] = default_servo_value;
|
186
|
|
- }
|
187
|
|
- pinMode(sigPin, OUTPUT);
|
188
|
|
- digitalWrite(sigPin, !onState); //set the PPM signal pin to the default state (off)
|
189
|
|
- }
|
190
|
|
- config_timer();
|
191
|
|
- channr = 0;
|
192
|
|
- cc2500_writeReg(CC2500_0A_CHANNR, hopData[channr]);//0A-hop
|
193
|
|
- cc2500_writeReg(CC2500_23_FSCAL3, 0x89); //23-89
|
194
|
|
- cc2500_strobe(CC2500_SRX);
|
195
|
|
-}
|
196
|
|
-
|
197
|
|
-void updateRSSI() {
|
198
|
|
-#if defined(RSSI_OVER_PPM)
|
199
|
|
- int rssi_dec = cc2500_readReg(CC2500_34_RSSI | CC2500_READ_BURST);
|
200
|
|
- if (rssi_dec < 128) {
|
201
|
|
- rssi = ((rssi_dec / 2) - rssi_offset) & 0x7f;
|
202
|
|
- } else {
|
203
|
|
- rssi = (((rssi_dec - 256) / 2)) - rssi_offset;
|
204
|
|
- }
|
205
|
|
-
|
206
|
|
- #if defined(DEBUG_RSSI2)
|
207
|
|
- Serial.print(millis());
|
208
|
|
- Serial.print("\t");
|
209
|
|
- Serial.println(rssi);
|
210
|
|
- #endif
|
211
|
|
-
|
212
|
|
- rssi = constrain(rssi, rssi_min, rssi_max);
|
213
|
|
-#endif
|
214
|
|
-}
|
215
|
|
-
|
216
|
|
-void loop()
|
217
|
|
-{
|
218
|
|
- unsigned long time = micros();
|
219
|
|
-
|
220
|
|
- #if defined(FAILSAFE)
|
221
|
|
- if (missingPackets > FAILSAFE_MISSING_PKT) {
|
222
|
|
- failed = true;
|
223
|
|
- missingPackets = 0;
|
224
|
|
- }
|
225
|
|
- #endif
|
226
|
|
-
|
227
|
|
- while (1) {
|
228
|
|
- if ((micros() - time) > 9000) {
|
229
|
|
- missingPackets++;
|
230
|
|
- cc2500_strobe(CC2500_SIDLE);
|
231
|
|
- if (missingPackets > MAX_MISSING_PKT) {
|
232
|
|
- nextChannel(SEEK_CHANSKIP);
|
233
|
|
- LED_OFF;
|
234
|
|
- counter++;
|
235
|
|
- if (counter > (MAX_MISSING_PKT << 1))
|
236
|
|
- LED_ON;
|
237
|
|
- if (counter == (MAX_MISSING_PKT << 2)) counter = 0;
|
238
|
|
- break;
|
239
|
|
- } else
|
240
|
|
- nextChannel(1);
|
241
|
|
- break;
|
242
|
|
- }
|
243
|
|
- if (GDO_1) {
|
244
|
|
- ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
|
245
|
|
- if (ccLen > 20)
|
246
|
|
- ccLen = 20;//
|
247
|
|
- if (ccLen) {
|
248
|
|
- cc2500_readFifo((uint8_t *)ccData, ccLen);
|
249
|
|
- if (ccData[ccLen - 1] & 0x80) { // Only if correct CRC
|
250
|
|
- missingPackets = 0;
|
251
|
|
- if (ccData[0] == 0x11) { // Correct length
|
252
|
|
- if ((ccData[1] == txid[0]) && (ccData[2] == txid[1])) { // Only if correct txid
|
253
|
|
- packet = true;
|
254
|
|
- //sei(); ///////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
- updateRSSI();
|
256
|
|
- cc2500_strobe(CC2500_SIDLE);
|
257
|
|
- nextChannel(1);
|
258
|
|
- LED_ON;
|
259
|
|
- failed = false;
|
260
|
|
- break;
|
261
|
|
- }
|
262
|
|
- }
|
263
|
|
- }
|
264
|
|
-
|
265
|
|
- }
|
266
|
|
-
|
267
|
|
- }
|
268
|
|
- }
|
269
|
|
-
|
270
|
|
- if (packet == true) {
|
271
|
|
- packet = false;
|
272
|
|
- debug = true;
|
273
|
|
- //cli();
|
274
|
|
- c[0] = (uint16_t)(ccData[10] & 0x0F) << 8 | ccData[6];
|
275
|
|
- c[1] = (uint16_t)(ccData[10] & 0xF0) << 4 | ccData[7];
|
276
|
|
- c[2] = (uint16_t)(ccData[11] & 0x0F) << 8 | ccData[8];
|
277
|
|
- c[3] = (uint16_t)(ccData[11] & 0xF0) << 4 | ccData[9];
|
278
|
|
- c[4] = (uint16_t)(ccData[16] & 0x0F) << 8 | ccData[12];
|
279
|
|
- c[5] = (uint16_t)(ccData[16] & 0xF0) << 4 | ccData[13];
|
280
|
|
- c[6] = (uint16_t)(ccData[17] & 0x0F) << 8 | ccData[14];
|
281
|
|
- c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
|
282
|
|
- //sei();
|
283
|
|
- for (int i = 0; i < 8; i++) {
|
284
|
|
- Servo_data[i] = 0.67 * c[i];
|
285
|
|
- if (Servo_data[i] < 900) { //added new
|
286
|
|
- Servo_data[i] = 1500; //added new
|
287
|
|
- Servo_data[2] = 1000;
|
288
|
|
- }
|
289
|
|
- ppm[i] = Servo_data[i];
|
290
|
|
- }
|
291
|
|
- #if defined(RSSI_OVER_PPM)
|
292
|
|
- ppm[RSSI_OVER_PPM] = map(rssi, rssi_min, rssi_max, 1000, 2000);
|
293
|
|
- #endif
|
294
|
|
- #if defined(DEBUG5)
|
295
|
|
- //Serial.println(rssi);
|
296
|
|
- #endif
|
297
|
|
- #if defined(DEBUG0)
|
298
|
|
- for (int i = 0; i < 8; i++) {
|
299
|
|
- Serial.print(" ");
|
300
|
|
- Serial.print(Servo_data[i]);
|
301
|
|
- Serial.print(" ");
|
302
|
|
- }
|
303
|
|
- Serial.println(" ");
|
304
|
|
- #endif
|
305
|
|
- }
|
306
|
|
-
|
307
|
|
- cc2500_strobe(CC2500_SRX);
|
308
|
|
- if (debug == true) {
|
309
|
|
- debug = false;
|
310
|
|
- #if defined(DEBUG2)
|
311
|
|
- Serial.println(ccData[3], HEX);
|
312
|
|
- #endif
|
313
|
|
- }
|
314
|
|
-}
|
315
|
|
-
|
316
|
|
-void initialize(int bind)
|
317
|
|
-{
|
318
|
|
- cc2500_resetChip();
|
319
|
|
- cc2500_writeReg(CC2500_02_IOCFG0, 0x01); // reg 0x02: RX complete interrupt(GDO0)
|
320
|
|
- cc2500_writeReg(CC2500_17_MCSM1, 0x0C); // reg 0x17:
|
321
|
|
- cc2500_writeReg(CC2500_18_MCSM0, 0x18); // reg 0x18:
|
322
|
|
- cc2500_writeReg(CC2500_06_PKTLEN, 0x19); // Leave room for appended status bytes
|
323
|
|
- cc2500_writeReg(CC2500_08_PKTCTRL0, 0x05); // reg 0x08:
|
324
|
|
- cc2500_writeReg(CC2500_3E_PATABLE, 0xFF); //
|
325
|
|
- cc2500_writeReg(CC2500_0B_FSCTRL1, 0x08); // reg 0x0B:
|
326
|
|
- cc2500_writeReg(CC2500_0C_FSCTRL0, 0x00); // reg 0x0C
|
327
|
|
- cc2500_writeReg(CC2500_0D_FREQ2, 0x5C); // reg 0x0D
|
328
|
|
- cc2500_writeReg(CC2500_0E_FREQ1, 0x76); // reg 0x0E
|
329
|
|
- cc2500_writeReg(CC2500_0F_FREQ0, 0x27); // reg 0x0F
|
330
|
|
- cc2500_writeReg(CC2500_10_MDMCFG4, 0xAA); // reg 0x10
|
331
|
|
- cc2500_writeReg(CC2500_11_MDMCFG3, 0x39); // reg 0x11
|
332
|
|
- cc2500_writeReg(CC2500_12_MDMCFG2, 0x11); // reg 0x12
|
333
|
|
- cc2500_writeReg(CC2500_13_MDMCFG1, 0x23); // reg 0x13
|
334
|
|
- cc2500_writeReg(CC2500_14_MDMCFG0, 0x7A); // reg 0x14
|
335
|
|
- cc2500_writeReg(CC2500_15_DEVIATN, 0x42); // reg 0x15
|
336
|
|
- cc2500_writeReg(CC2500_19_FOCCFG, 0x16); // reg 0x16
|
337
|
|
- cc2500_writeReg(CC2500_1A_BSCFG, 0x6C); // reg 0x1A
|
338
|
|
- cc2500_writeReg(CC2500_1B_AGCCTRL2, 0x03); // reg 0x1B
|
339
|
|
- cc2500_writeReg(CC2500_1C_AGCCTRL1, 0x40); // reg 0x1C
|
340
|
|
- cc2500_writeReg(CC2500_1D_AGCCTRL0, 0x91); // reg 0x1D
|
341
|
|
- cc2500_writeReg(CC2500_21_FREND1, 0x56); // reg 0x21:
|
342
|
|
- cc2500_writeReg(CC2500_22_FREND0, 0x10); // reg 0x22:
|
343
|
|
- cc2500_writeReg(CC2500_23_FSCAL3, 0xA9); // reg 0x23:
|
344
|
|
- cc2500_writeReg(CC2500_24_FSCAL2, 0x05); // reg 0x24:
|
345
|
|
- cc2500_writeReg(CC2500_25_FSCAL1, 0x00); // reg 0x25
|
346
|
|
- cc2500_writeReg(CC2500_26_FSCAL0, 0x11); // reg 0x26
|
347
|
|
- cc2500_writeReg(CC2500_29_FSTEST, 0x59); // reg 0x29
|
348
|
|
- cc2500_writeReg(CC2500_2C_TEST2, 0x88); // reg 0x2C
|
349
|
|
- cc2500_writeReg(CC2500_2D_TEST1, 0x31); // reg 0x2D
|
350
|
|
- cc2500_writeReg(CC2500_2E_TEST0, 0x0B); // reg 0x2E
|
351
|
|
- cc2500_writeReg(CC2500_03_FIFOTHR, 0x0F); // reg 0x03:
|
352
|
|
- cc2500_writeReg(CC2500_09_ADDR, bind ? 0x03 : txid[0]);
|
353
|
|
- cc2500_strobe(CC2500_SIDLE); // Go to idle...
|
354
|
|
- cc2500_writeReg(CC2500_07_PKTCTRL1, 0x0D); // reg 0x07 hack: Append status, filter by address, auto-flush on bad crc, PQT=0
|
355
|
|
- //cc2500_writeReg(CC2500_0C_FSCTRL0, 0); // Frequency offset...
|
356
|
|
- cc2500_writeReg(CC2500_0C_FSCTRL0, bind ? 0x00 : count); // Frequency offset hack
|
357
|
|
- cc2500_writeReg(CC2500_0A_CHANNR, 0x00);
|
358
|
|
-}
|
359
|
|
-// Receives complete bind setup
|
360
|
|
-void getBind(void)
|
361
|
|
-{
|
362
|
|
- cc2500_strobe(CC2500_SRX);//enter in rx mode
|
363
|
|
- listLength = 0;
|
364
|
|
- boolean eol = false;
|
365
|
|
- // len|bind |tx id|idx|h0|h1|h2|h3|h4|00|00|00|00|00|00|01
|
366
|
|
- // Start by getting bind packet 0 and the txid
|
367
|
|
- // 0 1 2 txid0(3) txid1()4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
368
|
|
- //ccdata //11 03 01 d7 2d 00 00 1e 3c 5b 78 00 00 00 00 00 00 01
|
369
|
|
- //11 03 01 19 3e 00 02 8e 2f bb 5c 00 00 00 00 00 00 01
|
370
|
|
- while (1) {
|
371
|
|
- if (GDO_1) {
|
372
|
|
- ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
|
373
|
|
- if (ccLen) {
|
374
|
|
- cc2500_readFifo((uint8_t *)ccData, ccLen);
|
375
|
|
- if (ccData[ccLen - 1] & 0x80) {
|
376
|
|
- if (ccData[2] == 0x01) {
|
377
|
|
- if (ccData[5] == 0x00) {
|
378
|
|
- txid[0] = ccData[3];
|
379
|
|
- txid[1] = ccData[4];
|
380
|
|
- for (uint8_t n = 0; n < 5; n++) {
|
381
|
|
- hopData[ccData[5] + n] = ccData[6 + n];
|
382
|
|
- }
|
383
|
|
- break;
|
384
|
|
- }
|
385
|
|
- }
|
386
|
|
- }
|
387
|
|
- }
|
388
|
|
- }
|
389
|
|
- }
|
390
|
|
-
|
391
|
|
- #if defined(DEBUG)
|
392
|
|
- Serial.print(txid[0], HEX);
|
393
|
|
- Serial.println(txid[1], HEX);
|
394
|
|
- #endif
|
395
|
|
-
|
396
|
|
- for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
|
397
|
|
- while (1) {
|
398
|
|
- if (GDO_1) {
|
399
|
|
- ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
|
400
|
|
- if (ccLen) {
|
401
|
|
- cc2500_readFifo((uint8_t *)ccData, ccLen);
|
402
|
|
- if (ccData[ccLen - 1] & 0x80) {
|
403
|
|
- if (ccData[2] == 0x01) {
|
404
|
|
- if(debug3) {
|
405
|
|
- Serial.print("ccLen = ");
|
406
|
|
- Serial.println(ccLen);
|
407
|
|
- }
|
408
|
|
- if ((ccData[3] == txid[0]) && (ccData[4] == txid[1])) {
|
409
|
|
- if(debug3)
|
410
|
|
- {
|
411
|
|
- Serial.print("ccData[5] = ");
|
412
|
|
- Serial.println(ccData[5]);
|
413
|
|
- Serial.print("bindIdx = ");
|
414
|
|
- Serial.println(bindIdx);
|
415
|
|
- }
|
416
|
|
- if (ccData[5] == bindIdx) {
|
417
|
|
- for (uint8_t n = 0; n < 5; n++) {
|
418
|
|
- if(debug3)
|
419
|
|
- {
|
420
|
|
- Serial.print("ccData[6 + n] = ");
|
421
|
|
- Serial.println(ccData[6 + n]);
|
422
|
|
- Serial.print("ccData[ccLen - 3] = ");
|
423
|
|
- Serial.println(ccData[ccLen - 3]);
|
424
|
|
- }
|
425
|
|
- //if (ccData[6 + n] == ccData[ccLen - 3]) {
|
426
|
|
- if (ccData[6 + n] <= 3) {
|
427
|
|
- eol = true;
|
428
|
|
- #if defined(DEBUG)
|
429
|
|
- Serial.print("listLength: ");
|
430
|
|
- Serial.println(listLength);
|
431
|
|
- #endif
|
432
|
|
- listLength = ccData[5] + n;
|
433
|
|
- break;
|
434
|
|
- }
|
435
|
|
- hopData[ccData[5] + n] = ccData[6 + n];
|
436
|
|
- }
|
437
|
|
- break;
|
438
|
|
- }
|
439
|
|
- }
|
440
|
|
- }
|
441
|
|
- }
|
442
|
|
- }
|
443
|
|
- }
|
444
|
|
- }
|
445
|
|
- #if defined(DEBUG)
|
446
|
|
- Serial.println(bindIdx / 5);
|
447
|
|
- #endif
|
448
|
|
- if (eol) break; // End of list found, stop!
|
449
|
|
- }
|
450
|
|
-
|
451
|
|
- #if defined(DEBUG)
|
452
|
|
- listLength = 47;
|
453
|
|
- Serial.println("jumpIdx list: ");
|
454
|
|
- for (uint8_t jumpIdx = 0; jumpIdx < (listLength); jumpIdx++) {
|
455
|
|
- Serial.print(" ");
|
456
|
|
- Serial.print(hopData[jumpIdx], HEX);
|
457
|
|
- Serial.print(" ");
|
458
|
|
- }
|
459
|
|
- Serial.println(" ");
|
460
|
|
- #endif
|
461
|
|
-
|
462
|
|
- Store_bind();
|
463
|
|
- cc2500_strobe(CC2500_SIDLE); // Back to idle
|
464
|
|
-}
|
465
|
|
-
|
466
|
|
-ISR(TIMER1_COMPA_vect)
|
467
|
|
-{
|
468
|
|
- if (failed) {
|
469
|
|
- digitalWrite(sigPin, HIGH);
|
470
|
|
- return;
|
471
|
|
- }
|
472
|
|
-
|
473
|
|
- TCNT1 = 0;
|
474
|
|
- if (jumper1 == 0) {
|
475
|
|
- pinMode(Servo5_OUT, OUTPUT);
|
476
|
|
- Servo_Ports_LOW;
|
477
|
|
- //code for servo.
|
478
|
|
- cur_chan_numb++; //next servo
|
479
|
|
- if (cur_chan_numb < chanel_number) {
|
480
|
|
- total_servo_time += Servo_data[cur_chan_numb] * scale;
|
481
|
|
- OCR1A = Servo_data[cur_chan_numb] * scale;
|
482
|
|
- } else {
|
483
|
|
- OCR1A = PPM_FrLen * scale - total_servo_time;
|
484
|
|
- cur_chan_numb = 0xff;
|
485
|
|
- total_servo_time = 0;
|
486
|
|
- }
|
487
|
|
-
|
488
|
|
- switch (cur_chan_numb) {
|
489
|
|
- case 0:
|
490
|
|
- Servo1_OUT_HIGH;
|
491
|
|
- break;
|
492
|
|
- case 1:
|
493
|
|
- Servo2_OUT_HIGH;
|
494
|
|
- break;
|
495
|
|
- case 2:
|
496
|
|
- Servo3_OUT_HIGH;
|
497
|
|
- break;
|
498
|
|
- case 3:
|
499
|
|
- Servo4_OUT_HIGH;
|
500
|
|
- break;
|
501
|
|
- case 4:
|
502
|
|
- Servo5_OUT_HIGH;
|
503
|
|
- break;
|
504
|
|
- case 5:
|
505
|
|
- Servo6_OUT_HIGH;
|
506
|
|
- break;
|
507
|
|
- case 6:
|
508
|
|
- Servo7_OUT_HIGH;
|
509
|
|
- break;
|
510
|
|
- case 7:
|
511
|
|
- Servo8_OUT_HIGH;
|
512
|
|
- break;
|
513
|
|
- }
|
514
|
|
- } else {
|
515
|
|
- static boolean state = true;
|
516
|
|
- pinMode(sigPin, OUTPUT);
|
517
|
|
- digitalWrite(sigPin, !onState);
|
518
|
|
-
|
519
|
|
- if (state) {
|
520
|
|
- digitalWrite(sigPin, onState);
|
521
|
|
- OCR1A = PPM_PulseLen * scale;
|
522
|
|
- state = false;
|
523
|
|
- } else {
|
524
|
|
- static byte cur_chan_numb;
|
525
|
|
- static unsigned int calc_rest;
|
526
|
|
- // digitalWrite(sigPin, !onState);//PPM on servo4 pin10
|
527
|
|
- state = true;
|
528
|
|
-
|
529
|
|
- if (cur_chan_numb >= chanel_number) {
|
530
|
|
- cur_chan_numb = 0;
|
531
|
|
- calc_rest = calc_rest + PPM_PulseLen;//
|
532
|
|
- OCR1A = (PPM_FrLen - calc_rest) * scale;
|
533
|
|
- calc_rest = 0;
|
534
|
|
- } else {
|
535
|
|
- OCR1A = (ppm[cur_chan_numb] - PPM_PulseLen) * scale;
|
536
|
|
- calc_rest = calc_rest + ppm[cur_chan_numb];
|
537
|
|
- cur_chan_numb++;
|
538
|
|
- }
|
539
|
|
- }
|
540
|
|
- }
|
541
|
|
-}
|
542
|
|
-
|
543
|
|
-void config_timer()
|
544
|
|
-{
|
545
|
|
- OCR1A = 50 * scale;
|
546
|
|
- cli();
|
547
|
|
- TCCR1A = 0; //
|
548
|
|
- TCCR1B = 0;
|
549
|
|
- TCCR1B |= (1 << WGM12);
|
550
|
|
- TCCR1B |= (1 << CS11);
|
551
|
|
- TIMSK1 |= (1 << OCIE1A);
|
552
|
|
- sei();
|
553
|
|
-}
|
554
|
|
-
|
555
|
|
-void nextChannel(uint8_t skip)
|
556
|
|
-{
|
557
|
|
- channr += skip;//
|
558
|
|
- if (channr >= listLength) channr -= listLength;
|
559
|
|
- cc2500_writeReg(CC2500_0A_CHANNR, hopData[channr]);
|
560
|
|
- cc2500_writeReg(CC2500_23_FSCAL3, 0x89);
|
561
|
|
-
|
562
|
|
-}
|
563
|
|
-
|
564
|
|
-void binding()
|
565
|
|
-{
|
566
|
|
- jumper2 = bind_jumper();
|
567
|
|
- while (1) {
|
568
|
|
- if (jumper2 == 0) { //bind complete or no bind
|
569
|
|
- uint8_t i;
|
570
|
|
- uint8_t adr = 100;
|
571
|
|
- for (i = 0; i < 2; i++) {
|
572
|
|
- txid[i] = EEPROM.read(adr + i);
|
573
|
|
- }
|
574
|
|
- if (txid[0] == 0xff && txid[1] == 0xff) {
|
575
|
|
- // No valid txid, forcing bind
|
576
|
|
- jumper2 = 1;
|
577
|
|
- continue;
|
578
|
|
- }
|
579
|
|
- for (i = 0; i < sizeof(hopData); i++) {
|
580
|
|
- hopData[i] = EEPROM.read(adr + 10 + i);
|
581
|
|
- }
|
582
|
|
- listLength = EEPROM.read(adr + 100);
|
583
|
|
- count = EEPROM.read(adr + 101);
|
584
|
|
- break;
|
585
|
|
- } else {
|
586
|
|
- LED_ON;
|
587
|
|
- tunning();
|
588
|
|
- //count=0xC8;//for test
|
589
|
|
- cc2500_writeReg(CC2500_0C_FSCTRL0, count);
|
590
|
|
- int adr = 100;
|
591
|
|
- EEPROM.write(adr + 101, count);
|
592
|
|
- getBind();
|
593
|
|
- while (1) {
|
594
|
|
- LED_ON;
|
595
|
|
- delay(500);
|
596
|
|
- LED_OFF;
|
597
|
|
- delay(500);
|
598
|
|
- }
|
599
|
|
- }
|
600
|
|
- }
|
601
|
|
-}
|
602
|
|
-
|
603
|
|
-
|
604
|
|
-void tunning()
|
605
|
|
-{
|
606
|
|
- cc2500_strobe(CC2500_SRX);//enter in rx mode
|
607
|
|
- int count1 = 0;
|
608
|
|
- while (1) {
|
609
|
|
- count1++;
|
610
|
|
- if (count >= 250) {
|
611
|
|
- count = 0;
|
612
|
|
- }
|
613
|
|
- if (count1 > 3000) {
|
614
|
|
- count1 = 0;
|
615
|
|
- cc2500_writeReg(CC2500_0C_FSCTRL0, count); // Frequency offset hack
|
616
|
|
- count = count + 10;
|
617
|
|
- //cc2500_strobe(CC2500_SRX);//enter in rx mode
|
618
|
|
- }
|
619
|
|
- if (GDO_1) {
|
620
|
|
- ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
|
621
|
|
- if (ccLen) {
|
622
|
|
- cc2500_readFifo((uint8_t *)ccData, ccLen);
|
623
|
|
- if (ccData[ccLen - 1] & 0x80) {
|
624
|
|
- if (ccData[2] == 0x01) {
|
625
|
|
- if (ccData[5] == 0x00) {
|
626
|
|
- break;
|
627
|
|
- }
|
628
|
|
- }
|
629
|
|
- }
|
630
|
|
- }
|
631
|
|
- }
|
632
|
|
- }
|
633
|
|
- #if defined(DEBUG1)
|
634
|
|
- Serial.println(count, HEX);
|
635
|
|
- #endif
|
636
|
|
-}
|
637
|
|
-
|
638
|
|
-
|
639
|
|
-
|
640
|
|
-void Store_bind()
|
641
|
|
-{
|
642
|
|
- uint8_t i;
|
643
|
|
- int adr = 100;
|
644
|
|
- for (i = 0; i < 2; i++) {
|
645
|
|
- EEPROM.write(adr + i, txid[i]);
|
646
|
|
- }
|
647
|
|
- for (i = 0; i < sizeof(hopData); i++) {
|
648
|
|
- EEPROM.write(adr + 10 + i, hopData[i]);
|
649
|
|
- }
|
650
|
|
- EEPROM.write(adr + 100, listLength);
|
651
|
|
-}
|
652
|
|
-
|
653
|
|
-unsigned char PPM_jumper(void)
|
654
|
|
-{
|
655
|
|
- // PPM Selection (jumper between Ch1 and ch3)
|
656
|
|
- pinMode(Servo3_OUT, INPUT); //CH3 input
|
657
|
|
- digitalWrite(Servo3_OUT, HIGH); // pull up
|
658
|
|
- digitalWrite(Servo1_OUT, HIGH); // CH1 is HIGH
|
659
|
|
- delayMicroseconds(1);
|
660
|
|
- if ( digitalRead(Servo3_OUT) == HIGH) {
|
661
|
|
- digitalWrite(Servo1_OUT, LOW); // CH1 is LOW
|
662
|
|
- delayMicroseconds(1);
|
663
|
|
- if (digitalRead(Servo3_OUT) == LOW) { // OK jumper plugged
|
664
|
|
- pinMode(Servo3_OUT, OUTPUT);
|
665
|
|
- return 1;
|
666
|
|
- }
|
667
|
|
- }
|
668
|
|
- pinMode(Servo3_OUT, OUTPUT);
|
669
|
|
-
|
670
|
|
- return 0; // servo PWM by default
|
671
|
|
-}
|
672
|
|
-
|
673
|
|
-//bind jumper
|
674
|
|
-unsigned char bind_jumper(void)
|
675
|
|
-{
|
676
|
|
- pinMode(bind_pin, INPUT_PULLUP);//pull up
|
677
|
|
- if ( digitalRead(bind_pin) == LOW) {
|
678
|
|
- delayMicroseconds(1);
|
679
|
|
- return 1;
|
680
|
|
- }
|
681
|
|
- return 0;
|
682
|
|
-}
|