Selaa lähdekoodia

Added much more Debug output

Thomas Buck 8 vuotta sitten
vanhempi
commit
e462e9cf5d
5 muutettua tiedostoa jossa 272 lisäystä ja 56 poistoa
  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 Näytä tiedosto

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

+ 4
- 2
makefile Näytä tiedosto

@@ -45,8 +45,10 @@ endif
45 45
 
46 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 52
 ISPBAUD = 57600
51 53
 
52 54
 TARGET = rx

+ 15
- 11
src/cc2500.c Näytä tiedosto

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

+ 5
- 5
src/main.c Näytä tiedosto

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

+ 246
- 38
src/rx.c Näytä tiedosto

@@ -2,6 +2,19 @@
2 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 18
 #include <stdint.h>
6 19
 #include <avr/io.h>
7 20
 #include <avr/interrupt.h>
@@ -24,11 +37,27 @@
24 37
 #define SEEK_CHANNEL_SKIP   13
25 38
 #define MAX_MISSING_PACKET 20
26 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 46
 #define RSSI_OFFSET 71
29 47
 #define RSSI_MIN -103
30 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 62
 uint8_t ccData[27];
34 63
 uint8_t ccLen;
@@ -41,16 +70,22 @@ uint8_t txid[2];
41 70
 uint8_t counter = 0;
42 71
 uint8_t failed = 0;
43 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 81
 static uint16_t ppmBuffer[CHANNELS];
51 82
 
83
+#ifdef RSSI_OVER_PPM
84
+static int rssi;
85
+
52 86
 static long map(long x, long in_min, long in_max, long out_min, long out_max);
53 87
 static long constrain(long x, long min, long max);
88
+#endif
54 89
 
55 90
 static void initialize(uint8_t bind);
56 91
 static void binding(void);
@@ -63,15 +98,15 @@ static void writeBindingData(void);
63 98
 void rxInit(void) {
64 99
     GDO_dir;
65 100
 
66
-    debugWrite("CC2500: initializing\n");
101
+    debugWrite("RX: initializing\n");
67 102
 
68 103
     initialize(1); // binding
69 104
 
70
-    debugWrite("CC2500: binding\n");
105
+    debugWrite("RX: binding\n");
71 106
 
72 107
     binding();
73 108
 
74
-    debugWrite("CC2500: receiving\n");
109
+    debugWrite("RX: receiving\n");
75 110
 
76 111
     initialize(0); // data
77 112
     cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);// 0A - hop
@@ -95,15 +130,15 @@ static void initialize(uint8_t bind) {
95 130
      * According to the internet, these are the default FrSky
96 131
      * FREQ values (0x5C7627) which should equal 2.404GHz.
97 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 135
     cc2500WriteReg(CC2500_0D_FREQ2,    0x5C);
101 136
     cc2500WriteReg(CC2500_0E_FREQ1,    0x76);
102 137
     cc2500WriteReg(CC2500_0F_FREQ0,    0x27);
103 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 142
 #endif
108 143
 
109 144
     cc2500WriteReg(CC2500_10_MDMCFG4,  0xAA);
@@ -136,7 +171,7 @@ static void initialize(uint8_t bind) {
136 171
 
137 172
     cc2500Strobe(CC2500_SIDLE); // Go to idle...
138 173
 
139
-#ifdef DEBUG
174
+#ifdef DEBUG_TEST_PART_VERSION
140 175
     uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
141 176
     uint8_t version = cc2500ReadReg(CC2500_31_VERSION);
142 177
 
@@ -196,6 +231,7 @@ static void tuning() {
196 231
             } else {
197 232
                 frequencyOffsetHack = 0;
198 233
 
234
+#ifdef FREQUENCY_SCAN
199 235
                 if (freq0 < 255) {
200 236
                     freq0++;
201 237
                 } else {
@@ -221,31 +257,42 @@ static void tuning() {
221 257
                 cc2500WriteReg(CC2500_0D_FREQ2, freq2);
222 258
                 cc2500WriteReg(CC2500_0E_FREQ1, freq1);
223 259
                 cc2500WriteReg(CC2500_0F_FREQ0, freq0);
260
+#endif
224 261
             }
225 262
             //cc2500Strobe(CC2500_SRX); // enter in rx mode
226 263
         }
227 264
 
228 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 266
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
233 267
             if (ccLen) {
234
-                debugWrite("Tuning: got data!\n");
235 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 285
                 if ((ccData[ccLen - 1] & 0x80)
237 286
                         && (ccData[2] == 0x01)
238 287
                         && (ccData[5] == 0x00)) {
239 288
                     break;
240 289
                 }
241
-            } else {
242
-                debugWrite("Tuning: no data?!\n");
243 290
             }
244 291
         }
245 292
 
246 293
         wdt_reset();
247 294
 
248
-#ifdef DEBUG
295
+#ifdef DEBUG_UART_MENU
249 296
         uartMenu();
250 297
 #endif
251 298
     }
@@ -256,10 +303,25 @@ static void performBind(void) {
256 303
 
257 304
     while (1) {
258 305
         if (GDO_1) {
259
-            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
260 306
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
261 307
             if (ccLen) {
262 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 325
                 if ((ccData[ccLen - 1] & 0x80)
264 326
                         && (ccData[2] == 0x01)
265 327
                         && (ccData[5] == 0x00)) {
@@ -275,7 +337,7 @@ static void performBind(void) {
275 337
 
276 338
         wdt_reset();
277 339
 
278
-#ifdef DEBUG
340
+#ifdef DEBUG_UART_MENU
279 341
         uartMenu();
280 342
 #endif
281 343
     }
@@ -287,10 +349,25 @@ static void performBind(void) {
287 349
     for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
288 350
         while (1) {
289 351
             if (GDO_1) {
290
-                //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
291 352
                 ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
292 353
                 if (ccLen) {
293 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 371
                     if ((ccData[ccLen - 1] & 0x80)
295 372
                             && (ccData[2] == 0x01)
296 373
                             && (ccData[3] == txid[0])
@@ -312,7 +389,7 @@ static void performBind(void) {
312 389
 
313 390
             wdt_reset();
314 391
 
315
-#ifdef DEBUG
392
+#ifdef DEBUG_UART_MENU
316 393
             uartMenu();
317 394
 #endif
318 395
         }
@@ -339,16 +416,48 @@ static void nextChannel(uint8_t skip) {
339 416
 }
340 417
 
341 418
 static void readBindingData() {
419
+#ifdef DEBUG_EEPROM_DATA_READ
420
+    debugWrite("RX: EEPROM TXID: ");
421
+#endif
342 422
     for (uint8_t i = 0; i < 2; i++) {
343 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 439
     for (uint8_t i = 0; i < HOP_DATA_LENGTH; i++) {
347 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 451
     listLength = eeprom_read_byte(EEPROM_BASE_ADDRESS + 100);
351 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 463
 static void writeBindingData() {
@@ -366,6 +475,10 @@ static void writeBindingData() {
366 475
 void rxReceivePacket() {
367 476
     time_t time = timerGet();
368 477
 
478
+#ifdef DEBUG_MISSING_PACKETS
479
+    static uint8_t packetMissingNotification = 0;
480
+#endif
481
+
369 482
     if (missingPackets > FAILSAFE_MISSING_PACKET) {
370 483
         failed = 1;
371 484
         missingPackets = 0;
@@ -380,33 +493,69 @@ void rxReceivePacket() {
380 493
                 counter++;
381 494
 
382 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 514
                 nextChannel(1);
515
+            }
390 516
             break;
391 517
         }
392 518
 
393 519
         if (GDO_1) {
394
-            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
395 520
             ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
396 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 527
                 ccLen = 20;
398 528
             }
399 529
             if (ccLen) {
400 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 548
                     missingPackets = 0;
403
-                    if ((ccData[0] == 0x11) // Correct length
549
+                    if ((ccData[0] == FRSKY_PACKET_LENGTH) // Correct length
404 550
                             && (ccData[1] == txid[0]) // Correct txid
405 551
                             && (ccData[2] == txid[1])) {
552
+#ifdef DEBUG_MISSING_PACKETS
553
+                        packetMissingNotification = 0;
554
+#endif
555
+
406 556
                         packet = 1;
407 557
 
408 558
 #ifdef RSSI_OVER_PPM
409
-                        //int rssi_dec = cc2500ReadReg(CC2500_34_RSSI | CC2500_READ_BURST);
410 559
                         int rssi_dec = cc2500ReadReg(CC2500_34_RSSI);
411 560
                         if (rssi_dec < 128) {
412 561
                             rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
@@ -420,14 +569,42 @@ void rxReceivePacket() {
420 569
                         nextChannel(1);
421 570
                         failed = 0;
422 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 605
         wdt_reset();
429 606
 
430
-#ifdef DEBUG
607
+#ifdef DEBUG_UART_MENU
431 608
         uartMenu();
432 609
 #endif
433 610
     }
@@ -444,9 +621,21 @@ void rxReceivePacket() {
444 621
         c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
445 622
 
446 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,13 +643,31 @@ void rxReceivePacket() {
454 643
         ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
455 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 664
         cppmCopy(ppmBuffer);
459 665
     }
460 666
 
461 667
     cc2500Strobe(CC2500_SRX);
462 668
 }
463 669
 
670
+#ifdef RSSI_OVER_PPM
464 671
 static long map(long x, long in_min, long in_max, long out_min, long out_max) {
465 672
     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
466 673
 }
@@ -474,4 +681,5 @@ static long constrain(long x, long min, long max) {
474 681
     }
475 682
     return x;
476 683
 }
684
+#endif
477 685
 

Loading…
Peruuta
Tallenna