Browse Source

Added much more Debug output

Thomas Buck 8 years ago
parent
commit
e462e9cf5d
5 changed files with 272 additions and 56 deletions
  1. 2
    0
      include/main.h
  2. 4
    2
      makefile
  3. 15
    11
      src/cc2500.c
  4. 5
    5
      src/main.c
  5. 246
    38
      src/rx.c

+ 2
- 0
include/main.h View File

9
 
9
 
10
 #include "serial.h"
10
 #include "serial.h"
11
 
11
 
12
+#define DEBUG_UART_MENU
13
+
12
 #define debugWrite(x) serialWriteString(0, x);
14
 #define debugWrite(x) serialWriteString(0, x);
13
 #define debugHex(x) serialWriteHex(0, x);
15
 #define debugHex(x) serialWriteHex(0, x);
14
 
16
 

+ 4
- 2
makefile View File

45
 
45
 
46
 #LINKER = -Wl,--relax
46
 #LINKER = -Wl,--relax
47
 
47
 
48
-PROGRAMMER = avrisp2
49
-ISPPORT = usb
48
+#PROGRAMMER = avrisp2
49
+#ISPPORT = usb
50
+PROGRAMMER = arduino
51
+ISPPORT = /dev/tty.usbserial-A100OZQ1
50
 ISPBAUD = 57600
52
 ISPBAUD = 57600
51
 
53
 
52
 TARGET = rx
54
 TARGET = rx

+ 15
- 11
src/cc2500.c View File

2
  * CC2500 helper routines
2
  * CC2500 helper routines
3
  */
3
  */
4
 
4
 
5
+#ifdef DEBUG
6
+//#define DEBUG_STATUS
7
+#endif
8
+
5
 #include <util/delay.h>
9
 #include <util/delay.h>
6
 
10
 
7
 #include "spi.h"
11
 #include "spi.h"
8
 #include "cc2500.h"
12
 #include "cc2500.h"
9
 #include "main.h"
13
 #include "main.h"
10
 
14
 
11
-#ifdef DEBUG
15
+#ifdef DEBUG_STATUS
12
 
16
 
13
 static void cc2500PrintStatusByte(uint8_t status) {
17
 static void cc2500PrintStatusByte(uint8_t status) {
14
     static int16_t lastStatusByte = -1;
18
     static int16_t lastStatusByte = -1;
74
 void cc2500ReadRegisterMulti(uint8_t address, uint8_t data[], uint8_t length) {
78
 void cc2500ReadRegisterMulti(uint8_t address, uint8_t data[], uint8_t length) {
75
     CS_off;
79
     CS_off;
76
 
80
 
77
-#ifdef DEBUG
81
+#ifdef DEBUG_STATUS
78
     uint8_t status = spiWrite(address);
82
     uint8_t status = spiWrite(address);
79
 #else
83
 #else
80
     spiWrite(address);
84
     spiWrite(address);
86
 
90
 
87
     CS_on;
91
     CS_on;
88
 
92
 
89
-#ifdef DEBUG
93
+#ifdef DEBUG_STATUS
90
     cc2500PrintStatusByte(status);
94
     cc2500PrintStatusByte(status);
91
 #endif
95
 #endif
92
 }
96
 }
94
 void cc2500WriteRegisterMulti(uint8_t address, const uint8_t data[], uint8_t length) {
98
 void cc2500WriteRegisterMulti(uint8_t address, const uint8_t data[], uint8_t length) {
95
     CS_off;
99
     CS_off;
96
 
100
 
97
-#ifdef DEBUG
101
+#ifdef DEBUG_STATUS
98
     uint8_t status = spiWrite(CC2500_WRITE_BURST | address);
102
     uint8_t status = spiWrite(CC2500_WRITE_BURST | address);
99
 #else
103
 #else
100
     spiWrite(CC2500_WRITE_BURST | address);
104
     spiWrite(CC2500_WRITE_BURST | address);
106
 
110
 
107
     CS_on;
111
     CS_on;
108
 
112
 
109
-#ifdef DEBUG
113
+#ifdef DEBUG_STATUS
110
     cc2500PrintStatusByte(status);
114
     cc2500PrintStatusByte(status);
111
 #endif
115
 #endif
112
 }
116
 }
114
 void cc2500WriteReg(uint8_t address, uint8_t data) {
118
 void cc2500WriteReg(uint8_t address, uint8_t data) {
115
     CS_off;
119
     CS_off;
116
 
120
 
117
-#ifdef DEBUG
121
+#ifdef DEBUG_STATUS
118
     uint8_t status = spiWrite(address);
122
     uint8_t status = spiWrite(address);
119
 #else
123
 #else
120
     spiWrite(address);
124
     spiWrite(address);
124
     spiWrite(data);
128
     spiWrite(data);
125
     CS_on;
129
     CS_on;
126
 
130
 
127
-#ifdef DEBUG
131
+#ifdef DEBUG_STATUS
128
     cc2500PrintStatusByte(status);
132
     cc2500PrintStatusByte(status);
129
 #endif
133
 #endif
130
 }
134
 }
139
         address |= CC2500_READ_SINGLE;
143
         address |= CC2500_READ_SINGLE;
140
     }
144
     }
141
 
145
 
142
-#ifdef DEBUG
146
+#ifdef DEBUG_STATUS
143
     uint8_t status = spiWrite(address);
147
     uint8_t status = spiWrite(address);
144
 #else
148
 #else
145
     spiWrite(address);
149
     spiWrite(address);
148
     uint8_t result = spiRead();
152
     uint8_t result = spiRead();
149
     CS_on;
153
     CS_on;
150
 
154
 
151
-#ifdef DEBUG
155
+#ifdef DEBUG_STATUS
152
     cc2500PrintStatusByte(status);
156
     cc2500PrintStatusByte(status);
153
 #endif
157
 #endif
154
 
158
 
158
 void cc2500Strobe(uint8_t address) {
162
 void cc2500Strobe(uint8_t address) {
159
     CS_off;
163
     CS_off;
160
 
164
 
161
-#ifdef DEBUG
165
+#ifdef DEBUG_STATUS
162
     uint8_t status = spiWrite(address);
166
     uint8_t status = spiWrite(address);
163
 #else
167
 #else
164
     spiWrite(address);
168
     spiWrite(address);
166
 
170
 
167
     CS_on;
171
     CS_on;
168
 
172
 
169
-#ifdef DEBUG
173
+#ifdef DEBUG_STATUS
170
     cc2500PrintStatusByte(status);
174
     cc2500PrintStatusByte(status);
171
 #endif
175
 #endif
172
 }
176
 }

+ 5
- 5
src/main.c View File

17
     wdt_disable();
17
     wdt_disable();
18
 }
18
 }
19
 
19
 
20
-#ifdef DEBUG
20
+#ifdef DEBUG_UART_MENU
21
 void uartMenu(void) {
21
 void uartMenu(void) {
22
     if (!serialHasChar(0))  {
22
     if (!serialHasChar(0))  {
23
         return;
23
         return;
56
 #endif
56
 #endif
57
 
57
 
58
 void main(void) {
58
 void main(void) {
59
-    cppmInit();
59
+    //cppmInit();
60
     timerInit();
60
     timerInit();
61
 
61
 
62
 #ifdef DEBUG
62
 #ifdef DEBUG
67
     //wdt_enable(WDTO_250MS); // Trigger Watchdog after 250ms
67
     //wdt_enable(WDTO_250MS); // Trigger Watchdog after 250ms
68
     wdt_enable(WDTO_2S); // Trigger Watchdog after 2s
68
     wdt_enable(WDTO_2S); // Trigger Watchdog after 2s
69
 
69
 
70
-    debugWrite("RX reset.\n");
70
+    //debugWrite("RX reset.\n");
71
 
71
 
72
     spiInit();
72
     spiInit();
73
     rxInit();
73
     rxInit();
74
 
74
 
75
-    debugWrite("RX ready!\n");
75
+    //debugWrite("RX ready!\n");
76
 
76
 
77
     for(;;) {
77
     for(;;) {
78
         wdt_reset();
78
         wdt_reset();
79
         rxReceivePacket();
79
         rxReceivePacket();
80
 
80
 
81
-#ifdef DEBUG
81
+#ifdef DEBUG_UART_MENU
82
         uartMenu();
82
         uartMenu();
83
 #endif
83
 #endif
84
     }
84
     }

+ 246
- 38
src/rx.c View File

2
  * FrSky RX 2-way protocol
2
  * FrSky RX 2-way protocol
3
  */
3
  */
4
 
4
 
5
+#ifdef DEBUG
6
+//#define DEBUG_TEST_PART_VERSION
7
+//#define DEBUG_EEPROM_DATA_READ
8
+//#define DEBUG_MISSING_PACKETS
9
+//#define DEBUG_PACKET_DUMP
10
+//#define DEBUG_PACKET_DUMP_TUNE
11
+//#define DEBUG_PACKET_DUMP_BIND
12
+//#define DEBUG_PACKET_DUMP_LIST
13
+//#define DEBUG_PPM_VALUES
14
+//#define DEBUG_ERROR_MESSAGES
15
+#define DEBUG_PACKET_DOT
16
+#endif
17
+
5
 #include <stdint.h>
18
 #include <stdint.h>
6
 #include <avr/io.h>
19
 #include <avr/io.h>
7
 #include <avr/interrupt.h>
20
 #include <avr/interrupt.h>
24
 #define SEEK_CHANNEL_SKIP   13
37
 #define SEEK_CHANNEL_SKIP   13
25
 #define MAX_MISSING_PACKET 20
38
 #define MAX_MISSING_PACKET 20
26
 #define FAILSAFE_MISSING_PACKET 170
39
 #define FAILSAFE_MISSING_PACKET 170
27
-#define RSSI_OVER_PPM 7
40
+
41
+//#define FAILSAFE_INVALID_VALUE 850
42
+#define FAILSAFE_INVALID_VALUE_MINIMUM 900
43
+#define FAILSAFE_INVALID_VALUE_MAXIMUM 2100
44
+
45
+//#define RSSI_OVER_PPM 7
28
 #define RSSI_OFFSET 71
46
 #define RSSI_OFFSET 71
29
 #define RSSI_MIN -103
47
 #define RSSI_MIN -103
30
 #define RSSI_MAX -96
48
 #define RSSI_MAX -96
31
-#define PPM_SCALE 0.67
49
+
50
+#define PPM_SCALE 2 / 3
51
+
52
+#define FRSKY_PACKET_LENGTH 0x11
53
+
54
+#define FREQ_ORIGINAL
55
+//#define FREQUENCY_SCAN
56
+
57
+// Original 0x5C 0x76 0x27
58
+#define FREQ_SCAN_START_2 0x5C
59
+#define FREQ_SCAN_START_1 0x76
60
+#define FREQ_SCAN_START_0 0x00
32
 
61
 
33
 uint8_t ccData[27];
62
 uint8_t ccData[27];
34
 uint8_t ccLen;
63
 uint8_t ccLen;
41
 uint8_t counter = 0;
70
 uint8_t counter = 0;
42
 uint8_t failed = 0;
71
 uint8_t failed = 0;
43
 uint8_t frequencyOffsetHack = 0;
72
 uint8_t frequencyOffsetHack = 0;
44
-uint16_t c[8];
45
-int rssi;
73
+uint16_t c[CHANNELS];
46
 
74
 
47
-// original 0x5C 0x76 0x27
48
-uint8_t freq2 = 0x5C, freq1 = 0x70, freq0 = 0x00;
75
+#ifdef FREQUENCY_SCAN
76
+uint8_t freq2 = FREQ_SCAN_START_2;
77
+uint8_t freq1 = FREQ_SCAN_START_1;
78
+uint8_t freq0 = FREQ_SCAN_START_0;
79
+#endif
49
 
80
 
50
 static uint16_t ppmBuffer[CHANNELS];
81
 static uint16_t ppmBuffer[CHANNELS];
51
 
82
 
83
+#ifdef RSSI_OVER_PPM
84
+static int rssi;
85
+
52
 static long map(long x, long in_min, long in_max, long out_min, long out_max);
86
 static long map(long x, long in_min, long in_max, long out_min, long out_max);
53
 static long constrain(long x, long min, long max);
87
 static long constrain(long x, long min, long max);
88
+#endif
54
 
89
 
55
 static void initialize(uint8_t bind);
90
 static void initialize(uint8_t bind);
56
 static void binding(void);
91
 static void binding(void);
63
 void rxInit(void) {
98
 void rxInit(void) {
64
     GDO_dir;
99
     GDO_dir;
65
 
100
 
66
-    debugWrite("CC2500: initializing\n");
101
+    debugWrite("RX: initializing\n");
67
 
102
 
68
     initialize(1); // binding
103
     initialize(1); // binding
69
 
104
 
70
-    debugWrite("CC2500: binding\n");
105
+    debugWrite("RX: binding\n");
71
 
106
 
72
     binding();
107
     binding();
73
 
108
 
74
-    debugWrite("CC2500: receiving\n");
109
+    debugWrite("RX: receiving\n");
75
 
110
 
76
     initialize(0); // data
111
     initialize(0); // data
77
     cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);// 0A - hop
112
     cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);// 0A - hop
95
      * According to the internet, these are the default FrSky
130
      * According to the internet, these are the default FrSky
96
      * FREQ values (0x5C7627) which should equal 2.404GHz.
131
      * FREQ values (0x5C7627) which should equal 2.404GHz.
97
      * The datasheet mentions Fcarrier = Fxosc * FREQ / 2^16
132
      * The datasheet mentions Fcarrier = Fxosc * FREQ / 2^16
98
-     * Therefore, FrSky seems to be using 26MHz too...?
133
+     * Therefore, FrSky seems to be using 26MHz too.
99
      */
134
      */
100
     cc2500WriteReg(CC2500_0D_FREQ2,    0x5C);
135
     cc2500WriteReg(CC2500_0D_FREQ2,    0x5C);
101
     cc2500WriteReg(CC2500_0E_FREQ1,    0x76);
136
     cc2500WriteReg(CC2500_0E_FREQ1,    0x76);
102
     cc2500WriteReg(CC2500_0F_FREQ0,    0x27);
137
     cc2500WriteReg(CC2500_0F_FREQ0,    0x27);
103
 #else
138
 #else
104
-    cc2500WriteReg(CC2500_0D_FREQ2,    0x5B); // freq2
105
-    cc2500WriteReg(CC2500_0E_FREQ1,    0x00); // freq1
106
-    cc2500WriteReg(CC2500_0F_FREQ0,    0x00); // freq0
139
+    cc2500WriteReg(CC2500_0D_FREQ2,    FREQ_SCAN_START_2);
140
+    cc2500WriteReg(CC2500_0E_FREQ1,    FREQ_SCAN_START_1);
141
+    cc2500WriteReg(CC2500_0F_FREQ0,    FREQ_SCAN_START_0);
107
 #endif
142
 #endif
108
 
143
 
109
     cc2500WriteReg(CC2500_10_MDMCFG4,  0xAA);
144
     cc2500WriteReg(CC2500_10_MDMCFG4,  0xAA);
136
 
171
 
137
     cc2500Strobe(CC2500_SIDLE); // Go to idle...
172
     cc2500Strobe(CC2500_SIDLE); // Go to idle...
138
 
173
 
139
-#ifdef DEBUG
174
+#ifdef DEBUG_TEST_PART_VERSION
140
     uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
175
     uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
141
     uint8_t version = cc2500ReadReg(CC2500_31_VERSION);
176
     uint8_t version = cc2500ReadReg(CC2500_31_VERSION);
142
 
177
 
196
             } else {
231
             } else {
197
                 frequencyOffsetHack = 0;
232
                 frequencyOffsetHack = 0;
198
 
233
 
234
+#ifdef FREQUENCY_SCAN
199
                 if (freq0 < 255) {
235
                 if (freq0 < 255) {
200
                     freq0++;
236
                     freq0++;
201
                 } else {
237
                 } else {
221
                 cc2500WriteReg(CC2500_0D_FREQ2, freq2);
257
                 cc2500WriteReg(CC2500_0D_FREQ2, freq2);
222
                 cc2500WriteReg(CC2500_0E_FREQ1, freq1);
258
                 cc2500WriteReg(CC2500_0E_FREQ1, freq1);
223
                 cc2500WriteReg(CC2500_0F_FREQ0, freq0);
259
                 cc2500WriteReg(CC2500_0F_FREQ0, freq0);
260
+#endif
224
             }
261
             }
225
             //cc2500Strobe(CC2500_SRX); // enter in rx mode
262
             //cc2500Strobe(CC2500_SRX); // enter in rx mode
226
         }
263
         }
227
 
264
 
228
         if (GDO_1) {
265
         if (GDO_1) {
229
-            debugWrite("Tuning: data flag has been set!\n");
230
-
231
-            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
232
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
266
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
233
             if (ccLen) {
267
             if (ccLen) {
234
-                debugWrite("Tuning: got data!\n");
235
                 cc2500ReadFifo(ccData, ccLen);
268
                 cc2500ReadFifo(ccData, ccLen);
269
+
270
+#ifdef DEBUG_PACKET_DUMP_TUNE
271
+                debugWrite("RX: Tune Packet Dump (");
272
+                serialWriteUnsigned8(0, ccLen);
273
+                debugWrite("):\n");
274
+                for (uint8_t i = 0; i < ccLen; i++) {
275
+                    debugWrite("0x")
276
+                    serialWriteHex(0, ccData[i]);
277
+                    if (i < (ccLen - 1)) {
278
+                        debugWrite(" ");
279
+                    } else {
280
+                        debugWrite("\n");
281
+                    }
282
+                }
283
+#endif
284
+
236
                 if ((ccData[ccLen - 1] & 0x80)
285
                 if ((ccData[ccLen - 1] & 0x80)
237
                         && (ccData[2] == 0x01)
286
                         && (ccData[2] == 0x01)
238
                         && (ccData[5] == 0x00)) {
287
                         && (ccData[5] == 0x00)) {
239
                     break;
288
                     break;
240
                 }
289
                 }
241
-            } else {
242
-                debugWrite("Tuning: no data?!\n");
243
             }
290
             }
244
         }
291
         }
245
 
292
 
246
         wdt_reset();
293
         wdt_reset();
247
 
294
 
248
-#ifdef DEBUG
295
+#ifdef DEBUG_UART_MENU
249
         uartMenu();
296
         uartMenu();
250
 #endif
297
 #endif
251
     }
298
     }
256
 
303
 
257
     while (1) {
304
     while (1) {
258
         if (GDO_1) {
305
         if (GDO_1) {
259
-            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
260
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
306
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
261
             if (ccLen) {
307
             if (ccLen) {
262
                 cc2500ReadFifo(ccData, ccLen);
308
                 cc2500ReadFifo(ccData, ccLen);
309
+
310
+#ifdef DEBUG_PACKET_DUMP_BIND
311
+                debugWrite("RX: Bind Packet Dump (");
312
+                serialWriteUnsigned8(0, ccLen);
313
+                debugWrite("):\n");
314
+                for (uint8_t i = 0; i < ccLen; i++) {
315
+                    debugWrite("0x")
316
+                    serialWriteHex(0, ccData[i]);
317
+                    if (i < (ccLen - 1)) {
318
+                        debugWrite(" ");
319
+                    } else {
320
+                        debugWrite("\n");
321
+                    }
322
+                }
323
+#endif
324
+
263
                 if ((ccData[ccLen - 1] & 0x80)
325
                 if ((ccData[ccLen - 1] & 0x80)
264
                         && (ccData[2] == 0x01)
326
                         && (ccData[2] == 0x01)
265
                         && (ccData[5] == 0x00)) {
327
                         && (ccData[5] == 0x00)) {
275
 
337
 
276
         wdt_reset();
338
         wdt_reset();
277
 
339
 
278
-#ifdef DEBUG
340
+#ifdef DEBUG_UART_MENU
279
         uartMenu();
341
         uartMenu();
280
 #endif
342
 #endif
281
     }
343
     }
287
     for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
349
     for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
288
         while (1) {
350
         while (1) {
289
             if (GDO_1) {
351
             if (GDO_1) {
290
-                //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
291
                 ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
352
                 ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
292
                 if (ccLen) {
353
                 if (ccLen) {
293
                     cc2500ReadFifo(ccData, ccLen);
354
                     cc2500ReadFifo(ccData, ccLen);
355
+
356
+#ifdef DEBUG_PACKET_DUMP_LIST
357
+                    debugWrite("RX: List Packet Dump (");
358
+                    serialWriteUnsigned8(0, ccLen);
359
+                    debugWrite("):\n");
360
+                    for (uint8_t i = 0; i < ccLen; i++) {
361
+                        debugWrite("0x")
362
+                        serialWriteHex(0, ccData[i]);
363
+                        if (i < (ccLen - 1)) {
364
+                            debugWrite(" ");
365
+                        } else {
366
+                            debugWrite("\n");
367
+                        }
368
+                    }
369
+#endif
370
+
294
                     if ((ccData[ccLen - 1] & 0x80)
371
                     if ((ccData[ccLen - 1] & 0x80)
295
                             && (ccData[2] == 0x01)
372
                             && (ccData[2] == 0x01)
296
                             && (ccData[3] == txid[0])
373
                             && (ccData[3] == txid[0])
312
 
389
 
313
             wdt_reset();
390
             wdt_reset();
314
 
391
 
315
-#ifdef DEBUG
392
+#ifdef DEBUG_UART_MENU
316
             uartMenu();
393
             uartMenu();
317
 #endif
394
 #endif
318
         }
395
         }
339
 }
416
 }
340
 
417
 
341
 static void readBindingData() {
418
 static void readBindingData() {
419
+#ifdef DEBUG_EEPROM_DATA_READ
420
+    debugWrite("RX: EEPROM TXID: ");
421
+#endif
342
     for (uint8_t i = 0; i < 2; i++) {
422
     for (uint8_t i = 0; i < 2; i++) {
343
         txid[i] = eeprom_read_byte(EEPROM_BASE_ADDRESS + i);
423
         txid[i] = eeprom_read_byte(EEPROM_BASE_ADDRESS + i);
424
+#ifdef DEBUG_EEPROM_DATA_READ
425
+        debugWrite("0x")
426
+        serialWriteHex(0, txid[i]);
427
+        debugWrite(" ");
428
+#endif
344
     }
429
     }
430
+#ifdef DEBUG_EEPROM_DATA_READ
431
+    debugWrite("\n");
432
+#endif
345
 
433
 
434
+#ifdef DEBUG_EEPROM_DATA_READ
435
+    debugWrite("RX: EEPROM Hop Data (");
436
+    serialWriteUnsigned8(0, HOP_DATA_LENGTH);
437
+    debugWrite(")\n");
438
+#endif
346
     for (uint8_t i = 0; i < HOP_DATA_LENGTH; i++) {
439
     for (uint8_t i = 0; i < HOP_DATA_LENGTH; i++) {
347
         hopData[i] = eeprom_read_byte(EEPROM_BASE_ADDRESS + 10 + i);
440
         hopData[i] = eeprom_read_byte(EEPROM_BASE_ADDRESS + 10 + i);
441
+#ifdef DEBUG_EEPROM_DATA_READ
442
+        debugWrite("0x")
443
+        serialWriteHex(0, hopData[i]);
444
+        debugWrite(" ");
445
+#endif
348
     }
446
     }
447
+#ifdef DEBUG_EEPROM_DATA_READ
448
+    debugWrite("\n");
449
+#endif
349
 
450
 
350
     listLength = eeprom_read_byte(EEPROM_BASE_ADDRESS + 100);
451
     listLength = eeprom_read_byte(EEPROM_BASE_ADDRESS + 100);
351
     frequencyOffsetHack = eeprom_read_byte(EEPROM_BASE_ADDRESS + 101);
452
     frequencyOffsetHack = eeprom_read_byte(EEPROM_BASE_ADDRESS + 101);
453
+
454
+#ifdef DEBUG_EEPROM_DATA_READ
455
+    debugWrite("RX: EEPROM List Length: ");
456
+    serialWriteUnsigned8(0, listLength);
457
+    debugWrite("\nRX: EEPROM Frequency Offset Hack: ");
458
+    serialWriteUnsigned8(0, frequencyOffsetHack);
459
+    debugWrite("\n");
460
+#endif
352
 }
461
 }
353
 
462
 
354
 static void writeBindingData() {
463
 static void writeBindingData() {
366
 void rxReceivePacket() {
475
 void rxReceivePacket() {
367
     time_t time = timerGet();
476
     time_t time = timerGet();
368
 
477
 
478
+#ifdef DEBUG_MISSING_PACKETS
479
+    static uint8_t packetMissingNotification = 0;
480
+#endif
481
+
369
     if (missingPackets > FAILSAFE_MISSING_PACKET) {
482
     if (missingPackets > FAILSAFE_MISSING_PACKET) {
370
         failed = 1;
483
         failed = 1;
371
         missingPackets = 0;
484
         missingPackets = 0;
380
                 counter++;
493
                 counter++;
381
 
494
 
382
                 if (counter > (MAX_MISSING_PACKET << 1)) {
495
                 if (counter > (MAX_MISSING_PACKET << 1)) {
383
-                    debugWrite("RX: missing packet notification!\n");
496
+#ifdef DEBUG_MISSING_PACKETS
497
+                    if (packetMissingNotification < 1) {
498
+                        debugWrite("RX: missing packet notification!\n");
499
+                        packetMissingNotification = 1;
500
+                    }
501
+#endif
384
                 }
502
                 }
385
 
503
 
386
-                if (counter == (MAX_MISSING_PACKET << 2)) counter = 0;
387
-                break;
388
-            } else
504
+                if (counter == (MAX_MISSING_PACKET << 2)) {
505
+#ifdef DEBUG_MISSING_PACKETS
506
+                    if (packetMissingNotification < 2) {
507
+                        debugWrite("RX: missing packet notification 2!\n");
508
+                        packetMissingNotification = 2;
509
+                    }
510
+#endif
511
+                    counter = 0;
512
+                }
513
+            } else {
389
                 nextChannel(1);
514
                 nextChannel(1);
515
+            }
390
             break;
516
             break;
391
         }
517
         }
392
 
518
 
393
         if (GDO_1) {
519
         if (GDO_1) {
394
-            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
395
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
520
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
396
             if (ccLen > 20) {
521
             if (ccLen > 20) {
522
+#ifdef DEBUG_ERROR_MESSAGES
523
+                debugWrite("RX: packet too long: ");
524
+                serialWriteUnsigned8(0, ccLen);
525
+                debugWrite("\n");
526
+#endif
397
                 ccLen = 20;
527
                 ccLen = 20;
398
             }
528
             }
399
             if (ccLen) {
529
             if (ccLen) {
400
                 cc2500ReadFifo((uint8_t *)ccData, ccLen);
530
                 cc2500ReadFifo((uint8_t *)ccData, ccLen);
401
-                if (ccData[ccLen - 1] & 0x80) { // Only if correct CRC
531
+                if (ccData[ccLen - 1] & CC2500_LQI_CRC_OK_BM) { // Only if correct CRC
532
+
533
+#ifdef DEBUG_PACKET_DUMP
534
+                    debugWrite("RX: Packet Dump (");
535
+                    serialWriteUnsigned8(0, ccLen);
536
+                    debugWrite("):\n");
537
+                    for (uint8_t i = 0; i < ccLen; i++) {
538
+                        debugWrite("0x")
539
+                        serialWriteHex(0, ccData[i]);
540
+                        if (i < (ccLen - 1)) {
541
+                            debugWrite(" ");
542
+                        } else {
543
+                            debugWrite("\n");
544
+                        }
545
+                    }
546
+#endif
547
+
402
                     missingPackets = 0;
548
                     missingPackets = 0;
403
-                    if ((ccData[0] == 0x11) // Correct length
549
+                    if ((ccData[0] == FRSKY_PACKET_LENGTH) // Correct length
404
                             && (ccData[1] == txid[0]) // Correct txid
550
                             && (ccData[1] == txid[0]) // Correct txid
405
                             && (ccData[2] == txid[1])) {
551
                             && (ccData[2] == txid[1])) {
552
+#ifdef DEBUG_MISSING_PACKETS
553
+                        packetMissingNotification = 0;
554
+#endif
555
+
406
                         packet = 1;
556
                         packet = 1;
407
 
557
 
408
 #ifdef RSSI_OVER_PPM
558
 #ifdef RSSI_OVER_PPM
409
-                        //int rssi_dec = cc2500ReadReg(CC2500_34_RSSI | CC2500_READ_BURST);
410
                         int rssi_dec = cc2500ReadReg(CC2500_34_RSSI);
559
                         int rssi_dec = cc2500ReadReg(CC2500_34_RSSI);
411
                         if (rssi_dec < 128) {
560
                         if (rssi_dec < 128) {
412
                             rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
561
                             rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
420
                         nextChannel(1);
569
                         nextChannel(1);
421
                         failed = 0;
570
                         failed = 0;
422
                         break;
571
                         break;
572
+                    } else {
573
+#ifdef DEBUG_ERROR_MESSAGES
574
+                        if (ccData[0] != FRSKY_PACKET_LENGTH) {
575
+                            debugWrite("RX: invalid packet length: ");
576
+                            serialWriteUnsigned8(0, ccData[0]);
577
+                            debugWrite(" != ");
578
+                            serialWriteUnsigned8(0, FRSKY_PACKET_LENGTH);
579
+                            debugWrite("\n");
580
+                        }
581
+                        if (ccData[1] != txid[0]) {
582
+                            debugWrite("RX: invalid txid[0]: ");
583
+                            serialWriteUnsigned8(0, ccData[1]);
584
+                            debugWrite(" != ");
585
+                            serialWriteUnsigned8(0, txid[0]);
586
+                            debugWrite("\n");
587
+                        }
588
+                        if (ccData[2] == txid[1]) {
589
+                            debugWrite("RX: invalid txid[1]: ");
590
+                            serialWriteUnsigned8(0, ccData[2]);
591
+                            debugWrite(" != ");
592
+                            serialWriteUnsigned8(0, txid[1]);
593
+                            debugWrite("\n");
594
+                        }
595
+#endif
423
                     }
596
                     }
597
+                } else {
598
+#ifdef DEBUG_ERROR_MESSAGES
599
+                    debugWrite("RX: invalid CRC!\n");
600
+#endif
424
                 }
601
                 }
425
             }
602
             }
426
         }
603
         }
427
 
604
 
428
         wdt_reset();
605
         wdt_reset();
429
 
606
 
430
-#ifdef DEBUG
607
+#ifdef DEBUG_UART_MENU
431
         uartMenu();
608
         uartMenu();
432
 #endif
609
 #endif
433
     }
610
     }
444
         c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
621
         c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
445
 
622
 
446
         for (int i = 0; i < CHANNELS; i++) {
623
         for (int i = 0; i < CHANNELS; i++) {
447
-            ppmBuffer[i] = PPM_SCALE * c[i];
448
-            if ((ppmBuffer[i] < 900) || (ppmBuffer[i] > 2100)) {
449
-                ppmBuffer[i] = 850;
624
+            ppmBuffer[i] = (uint16_t)((uint32_t)(c[i]) * PPM_SCALE);
625
+            if (ppmBuffer[i] < FAILSAFE_INVALID_VALUE_MINIMUM) {
626
+#ifdef FAILSAFE_INVALID_VALUE
627
+                ppmBuffer[i] = FAILSAFE_INVALID_VALUE;
628
+#else
629
+                ppmBuffer[i] = FAILSAFE_INVALID_VALUE_MINIMUM;
630
+#endif
631
+            }
632
+
633
+            if (ppmBuffer[i] > FAILSAFE_INVALID_VALUE_MAXIMUM) {
634
+#ifdef FAILSAFE_INVALID_VALUE
635
+                ppmBuffer[i] = FAILSAFE_INVALID_VALUE;
636
+#else
637
+                ppmBuffer[i] = FAILSAFE_INVALID_VALUE_MAXIMUM;
638
+#endif
450
             }
639
             }
451
         }
640
         }
452
 
641
 
454
         ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
643
         ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
455
 #endif
644
 #endif
456
 
645
 
457
-        debugWrite("RX: packet received, sending CPPM!\n");
646
+#ifdef DEBUG_PPM_VALUES
647
+        debugWrite("RX: got packet @ ");
648
+        serialWriteUnsigned64(0, timerGet() / 1000);
649
+        debugWrite("s:\n");
650
+        for (uint8_t i = 0; i < CHANNELS; i++) {
651
+            serialWriteUnsigned8(0, i);
652
+            debugWrite(": ");
653
+            serialWriteUnsigned16(0, ppmBuffer[i]);
654
+            debugWrite(" - ");
655
+            serialWriteUnsigned16(0, c[i]);
656
+            debugWrite("\n");
657
+        }
658
+#endif
659
+
660
+#ifdef DEBUG_PACKET_DOT
661
+        serialWrite(0, '.');
662
+#endif
663
+
458
         cppmCopy(ppmBuffer);
664
         cppmCopy(ppmBuffer);
459
     }
665
     }
460
 
666
 
461
     cc2500Strobe(CC2500_SRX);
667
     cc2500Strobe(CC2500_SRX);
462
 }
668
 }
463
 
669
 
670
+#ifdef RSSI_OVER_PPM
464
 static long map(long x, long in_min, long in_max, long out_min, long out_max) {
671
 static long map(long x, long in_min, long in_max, long out_min, long out_max) {
465
     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
672
     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
466
 }
673
 }
474
     }
681
     }
475
     return x;
682
     return x;
476
 }
683
 }
684
+#endif
477
 
685
 

Loading…
Cancel
Save