Browse Source

Experimenting with FrSky code...

Thomas Buck 8 years ago
parent
commit
3d171e1bdc
1 changed files with 112 additions and 52 deletions
  1. 112
    52
      src/rx.c

+ 112
- 52
src/rx.c View File

13
 #include "spi.h"
13
 #include "spi.h"
14
 #include "timer.h"
14
 #include "timer.h"
15
 #include "rx.h"
15
 #include "rx.h"
16
-
17
-#ifdef DEBUG
18
-#include "serial.h"
19
-#endif
16
+#include "main.h"
20
 
17
 
21
 #define CHANNELS 8
18
 #define CHANNELS 8
22
 #define PPM_MIN 1000
19
 #define PPM_MIN 1000
47
 uint16_t c[8];
44
 uint16_t c[8];
48
 int rssi;
45
 int rssi;
49
 
46
 
47
+// original 0x5C 0x76 0x27
48
+uint8_t freq2 = 0x5C, freq1 = 0x70, freq0 = 0x00;
49
+
50
 static uint16_t ppmBuffer[CHANNELS];
50
 static uint16_t ppmBuffer[CHANNELS];
51
 
51
 
52
 static long map(long x, long in_min, long in_max, long out_min, long out_max);
52
 static long map(long x, long in_min, long in_max, long out_min, long out_max);
61
 static void writeBindingData(void);
61
 static void writeBindingData(void);
62
 
62
 
63
 void rxInit(void) {
63
 void rxInit(void) {
64
-#ifdef DEBUG
65
-    serialWriteString(0, "RX: binding\n");
66
-#endif
64
+    GDO_dir;
65
+
66
+    debugWrite("CC2500: initializing\n");
67
 
67
 
68
     initialize(1); // binding
68
     initialize(1); // binding
69
+
70
+    debugWrite("CC2500: binding\n");
71
+
69
     binding();
72
     binding();
70
 
73
 
71
-#ifdef DEBUG
72
-    serialWriteString(0, "RX: receiving\n");
73
-#endif
74
+    debugWrite("CC2500: receiving\n");
74
 
75
 
75
     initialize(0); // data
76
     initialize(0); // data
76
-    cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);//0A-hop
77
-    cc2500WriteReg(CC2500_23_FSCAL3, 0x89); //23-89
77
+    cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);// 0A - hop
78
+    cc2500WriteReg(CC2500_23_FSCAL3, 0x89); // 23 - 89
78
     cc2500Strobe(CC2500_SRX);
79
     cc2500Strobe(CC2500_SRX);
79
 }
80
 }
80
 
81
 
81
 static void initialize(uint8_t bind) {
82
 static void initialize(uint8_t bind) {
82
     cc2500ResetChip();
83
     cc2500ResetChip();
83
     cc2500WriteReg(CC2500_02_IOCFG0,   0x01); // RX complete interrupt(GDO0)
84
     cc2500WriteReg(CC2500_02_IOCFG0,   0x01); // RX complete interrupt(GDO0)
84
-    cc2500WriteReg(CC2500_17_MCSM1,    0x0C);
85
+    cc2500WriteReg(CC2500_17_MCSM1,    0x0F);
85
     cc2500WriteReg(CC2500_18_MCSM0,    0x18);
86
     cc2500WriteReg(CC2500_18_MCSM0,    0x18);
86
     cc2500WriteReg(CC2500_06_PKTLEN,   0x19); // Leave room for appended status bytes
87
     cc2500WriteReg(CC2500_06_PKTLEN,   0x19); // Leave room for appended status bytes
87
     cc2500WriteReg(CC2500_08_PKTCTRL0, 0x05);
88
     cc2500WriteReg(CC2500_08_PKTCTRL0, 0x05);
88
     cc2500WriteReg(CC2500_3E_PATABLE,  0xFF);
89
     cc2500WriteReg(CC2500_3E_PATABLE,  0xFF);
89
     cc2500WriteReg(CC2500_0B_FSCTRL1,  0x08);
90
     cc2500WriteReg(CC2500_0B_FSCTRL1,  0x08);
90
     cc2500WriteReg(CC2500_0C_FSCTRL0,  0x00);
91
     cc2500WriteReg(CC2500_0C_FSCTRL0,  0x00);
92
+
93
+#ifdef FREQ_ORIGINAL
94
+    /*
95
+     * According to the internet, these are the default FrSky
96
+     * FREQ values (0x5C7627) which should equal 2.404GHz.
97
+     * The datasheet mentions Fcarrier = Fxosc * FREQ / 2^16
98
+     * Therefore, FrSky seems to be using 26MHz too...?
99
+     */
91
     cc2500WriteReg(CC2500_0D_FREQ2,    0x5C);
100
     cc2500WriteReg(CC2500_0D_FREQ2,    0x5C);
92
     cc2500WriteReg(CC2500_0E_FREQ1,    0x76);
101
     cc2500WriteReg(CC2500_0E_FREQ1,    0x76);
93
     cc2500WriteReg(CC2500_0F_FREQ0,    0x27);
102
     cc2500WriteReg(CC2500_0F_FREQ0,    0x27);
103
+#else
104
+    cc2500WriteReg(CC2500_0D_FREQ2,    0x5B); // freq2
105
+    cc2500WriteReg(CC2500_0E_FREQ1,    0x00); // freq1
106
+    cc2500WriteReg(CC2500_0F_FREQ0,    0x00); // freq0
107
+#endif
108
+
94
     cc2500WriteReg(CC2500_10_MDMCFG4,  0xAA);
109
     cc2500WriteReg(CC2500_10_MDMCFG4,  0xAA);
95
     cc2500WriteReg(CC2500_11_MDMCFG3,  0x39);
110
     cc2500WriteReg(CC2500_11_MDMCFG3,  0x39);
96
     cc2500WriteReg(CC2500_12_MDMCFG2,  0x11);
111
     cc2500WriteReg(CC2500_12_MDMCFG2,  0x11);
108
     cc2500WriteReg(CC2500_24_FSCAL2,   0x05);
123
     cc2500WriteReg(CC2500_24_FSCAL2,   0x05);
109
     cc2500WriteReg(CC2500_25_FSCAL1,   0x00);
124
     cc2500WriteReg(CC2500_25_FSCAL1,   0x00);
110
     cc2500WriteReg(CC2500_26_FSCAL0,   0x11);
125
     cc2500WriteReg(CC2500_26_FSCAL0,   0x11);
111
-    cc2500WriteReg(CC2500_29_FSTEST,   0x59);
112
-    cc2500WriteReg(CC2500_2C_TEST2,    0x88);
113
-    cc2500WriteReg(CC2500_2D_TEST1,    0x31);
114
-    cc2500WriteReg(CC2500_2E_TEST0,    0x0B);
115
-    cc2500WriteReg(CC2500_03_FIFOTHR,  0x0F);
126
+    //cc2500WriteReg(CC2500_29_FSTEST,   0x59);
127
+    //cc2500WriteReg(CC2500_2C_TEST2,    0x88);
128
+    //cc2500WriteReg(CC2500_2D_TEST1,    0x31);
129
+    //cc2500WriteReg(CC2500_2E_TEST0,    0x0B);
130
+    //cc2500WriteReg(CC2500_03_FIFOTHR,  0x0F); // 0x07?
116
 
131
 
117
     cc2500WriteReg(CC2500_09_ADDR, bind ? 0x03 : txid[0]);
132
     cc2500WriteReg(CC2500_09_ADDR, bind ? 0x03 : txid[0]);
133
+    //cc2500WriteReg(CC2500_09_ADDR, bind ? 0x00 : txid[0]);
134
+
135
+    debugWrite("CC2500: Entering IDLE mode...\n");
118
 
136
 
119
     cc2500Strobe(CC2500_SIDLE); // Go to idle...
137
     cc2500Strobe(CC2500_SIDLE); // Go to idle...
120
 
138
 
122
     uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
140
     uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
123
     uint8_t version = cc2500ReadReg(CC2500_31_VERSION);
141
     uint8_t version = cc2500ReadReg(CC2500_31_VERSION);
124
 
142
 
125
-    serialWriteString(0, "CC2500 Part Number: 0x");
126
-    serialWriteHex(0, part);
127
-    serialWriteString(0, "\nCC2500 Version: 0x");
128
-    serialWriteHex(0, version);
129
-    serialWriteString(0, "\n");
143
+    debugWrite("CC2500: Part Number: 0x");
144
+    debugHex(part);
145
+    debugWrite("\nCC2500: Version: 0x");
146
+    debugHex(version);
147
+    debugWrite("\n");
148
+
149
+    if ((part != 0x80) || (version != 0x03)) {
150
+        debugWrite("INVALID CC2500 STATUS! IT MAY BE DEAD!\n");
151
+    }
130
 #endif
152
 #endif
131
 
153
 
132
     // hack: Append status, filter by address, auto-flush on bad crc, PQT=0
154
     // hack: Append status, filter by address, auto-flush on bad crc, PQT=0
142
     readBindingData();
164
     readBindingData();
143
     if ((txid[0] != 0xff) || (txid[1] != 0xff)) {
165
     if ((txid[0] != 0xff) || (txid[1] != 0xff)) {
144
         // valid binding data found
166
         // valid binding data found
145
-#ifdef DEBUG
146
-        serialWriteString(0, "RX: found data in EEPROM!\n");
147
-#endif
167
+        debugWrite("RX: found data in EEPROM!\n");
148
         return;
168
         return;
149
     }
169
     }
150
 
170
 
151
-#ifdef DEBUG
152
-    serialWriteString(0, "RX: no stored data, tuning...\n");
153
-#endif
171
+    debugWrite("RX: no stored data, tuning...\n");
154
 
172
 
155
     // No valid txid, forcing bind
173
     // No valid txid, forcing bind
156
     tuning();
174
     tuning();
159
     cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
177
     cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
160
     eeprom_write_byte(EEPROM_BASE_ADDRESS + 101, frequencyOffsetHack);
178
     eeprom_write_byte(EEPROM_BASE_ADDRESS + 101, frequencyOffsetHack);
161
 
179
 
162
-#ifdef DEBUG
163
-    serialWriteString(0, "RX: tuned, binding...\n");
164
-#endif
180
+    debugWrite("RX: tuned, binding...\n");
165
 
181
 
166
     performBind();
182
     performBind();
167
 }
183
 }
169
 static void tuning() {
185
 static void tuning() {
170
     cc2500Strobe(CC2500_SRX); // enter in rx mode
186
     cc2500Strobe(CC2500_SRX); // enter in rx mode
171
 
187
 
172
-    int frequencyOffsetTimer = 0;
188
+    uint16_t frequencyOffsetTimer = 0;
173
     while (1) {
189
     while (1) {
174
         frequencyOffsetTimer++;
190
         frequencyOffsetTimer++;
175
         if (frequencyOffsetTimer > 3000) {
191
         if (frequencyOffsetTimer > 3000) {
176
             frequencyOffsetTimer = 0;
192
             frequencyOffsetTimer = 0;
177
             cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
193
             cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
178
-            frequencyOffsetHack += 10;
179
-            if (frequencyOffsetHack > 250) {
194
+            if (frequencyOffsetHack <= 250) {
195
+                frequencyOffsetHack += 5;
196
+            } else {
180
                 frequencyOffsetHack = 0;
197
                 frequencyOffsetHack = 0;
198
+
199
+                if (freq0 < 255) {
200
+                    freq0++;
201
+                } else {
202
+                    freq0 = 0;
203
+                    if (freq1 < 255) {
204
+                        freq1++;
205
+                    } else {
206
+                        freq1 = 0;
207
+                        if (freq2 < 255) {
208
+                            freq2++;
209
+                        } else {
210
+                            freq2 = 0;
211
+                        }
212
+                    }
213
+                }
214
+
215
+                debugWrite("0x");
216
+                debugHex(freq2);
217
+                debugHex(freq1);
218
+                debugHex(freq0);
219
+                debugWrite("\n");
220
+
221
+                cc2500WriteReg(CC2500_0D_FREQ2, freq2);
222
+                cc2500WriteReg(CC2500_0E_FREQ1, freq1);
223
+                cc2500WriteReg(CC2500_0F_FREQ0, freq0);
181
             }
224
             }
182
             //cc2500Strobe(CC2500_SRX); // enter in rx mode
225
             //cc2500Strobe(CC2500_SRX); // enter in rx mode
183
         }
226
         }
184
 
227
 
185
         if (GDO_1) {
228
         if (GDO_1) {
186
-            ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
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;
187
             if (ccLen) {
233
             if (ccLen) {
234
+                debugWrite("Tuning: got data!\n");
188
                 cc2500ReadFifo(ccData, ccLen);
235
                 cc2500ReadFifo(ccData, ccLen);
189
                 if ((ccData[ccLen - 1] & 0x80)
236
                 if ((ccData[ccLen - 1] & 0x80)
190
                         && (ccData[2] == 0x01)
237
                         && (ccData[2] == 0x01)
191
                         && (ccData[5] == 0x00)) {
238
                         && (ccData[5] == 0x00)) {
192
                     break;
239
                     break;
193
                 }
240
                 }
241
+            } else {
242
+                debugWrite("Tuning: no data?!\n");
194
             }
243
             }
195
         }
244
         }
196
 
245
 
197
         wdt_reset();
246
         wdt_reset();
247
+
248
+#ifdef DEBUG
249
+        uartMenu();
250
+#endif
198
     }
251
     }
199
 }
252
 }
200
 
253
 
203
 
256
 
204
     while (1) {
257
     while (1) {
205
         if (GDO_1) {
258
         if (GDO_1) {
206
-            ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
259
+            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
260
+            ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
207
             if (ccLen) {
261
             if (ccLen) {
208
                 cc2500ReadFifo(ccData, ccLen);
262
                 cc2500ReadFifo(ccData, ccLen);
209
                 if ((ccData[ccLen - 1] & 0x80)
263
                 if ((ccData[ccLen - 1] & 0x80)
220
         }
274
         }
221
 
275
 
222
         wdt_reset();
276
         wdt_reset();
223
-    }
224
 
277
 
225
 #ifdef DEBUG
278
 #ifdef DEBUG
226
-    serialWriteString(0, "RX: got hop data, reading list...\n");
279
+        uartMenu();
227
 #endif
280
 #endif
281
+    }
282
+
283
+    debugWrite("RX: got hop data, reading list...\n");
228
 
284
 
229
     listLength = 0;
285
     listLength = 0;
230
     uint8_t eol = 0;
286
     uint8_t eol = 0;
231
     for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
287
     for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
232
         while (1) {
288
         while (1) {
233
             if (GDO_1) {
289
             if (GDO_1) {
234
-                ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
290
+                //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
291
+                ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
235
                 if (ccLen) {
292
                 if (ccLen) {
236
                     cc2500ReadFifo(ccData, ccLen);
293
                     cc2500ReadFifo(ccData, ccLen);
237
                     if ((ccData[ccLen - 1] & 0x80)
294
                     if ((ccData[ccLen - 1] & 0x80)
254
             }
311
             }
255
 
312
 
256
             wdt_reset();
313
             wdt_reset();
314
+
315
+#ifdef DEBUG
316
+            uartMenu();
317
+#endif
257
         }
318
         }
258
 
319
 
259
         if (eol) {
320
         if (eol) {
261
         }
322
         }
262
     }
323
     }
263
 
324
 
264
-#ifdef DEBUG
265
-    serialWriteString(0, "RX: binding finished!\n");
266
-#endif
325
+    debugWrite("RX: binding finished!\n");
267
 
326
 
268
     writeBindingData();
327
     writeBindingData();
269
     cc2500Strobe(CC2500_SIDLE); // Back to idle
328
     cc2500Strobe(CC2500_SIDLE); // Back to idle
320
                 nextChannel(SEEK_CHANNEL_SKIP);
379
                 nextChannel(SEEK_CHANNEL_SKIP);
321
                 counter++;
380
                 counter++;
322
 
381
 
323
-#ifdef DEBUG
324
                 if (counter > (MAX_MISSING_PACKET << 1)) {
382
                 if (counter > (MAX_MISSING_PACKET << 1)) {
325
-                    serialWriteString(0, "RX: missing packet notification!\n");
383
+                    debugWrite("RX: missing packet notification!\n");
326
                 }
384
                 }
327
-#endif
328
 
385
 
329
                 if (counter == (MAX_MISSING_PACKET << 2)) counter = 0;
386
                 if (counter == (MAX_MISSING_PACKET << 2)) counter = 0;
330
                 break;
387
                 break;
334
         }
391
         }
335
 
392
 
336
         if (GDO_1) {
393
         if (GDO_1) {
337
-            ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
394
+            //ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
395
+            ccLen = cc2500ReadReg(CC2500_3B_RXBYTES) & 0x7F;
338
             if (ccLen > 20) {
396
             if (ccLen > 20) {
339
                 ccLen = 20;
397
                 ccLen = 20;
340
             }
398
             }
348
                         packet = 1;
406
                         packet = 1;
349
 
407
 
350
 #ifdef RSSI_OVER_PPM
408
 #ifdef RSSI_OVER_PPM
351
-                        int rssi_dec = cc2500ReadReg(CC2500_34_RSSI | CC2500_READ_BURST);
409
+                        //int rssi_dec = cc2500ReadReg(CC2500_34_RSSI | CC2500_READ_BURST);
410
+                        int rssi_dec = cc2500ReadReg(CC2500_34_RSSI);
352
                         if (rssi_dec < 128) {
411
                         if (rssi_dec < 128) {
353
                             rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
412
                             rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
354
                         } else {
413
                         } else {
367
         }
426
         }
368
 
427
 
369
         wdt_reset();
428
         wdt_reset();
429
+
430
+#ifdef DEBUG
431
+        uartMenu();
432
+#endif
370
     }
433
     }
371
 
434
 
372
     if (packet != 0) {
435
     if (packet != 0) {
391
         ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
454
         ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
392
 #endif
455
 #endif
393
 
456
 
394
-#ifdef DEBUG
395
-        serialWriteString(0, "RX: packet received, sending CPPM!\n");
396
-#endif
397
-
457
+        debugWrite("RX: packet received, sending CPPM!\n");
398
         cppmCopy(ppmBuffer);
458
         cppmCopy(ppmBuffer);
399
     }
459
     }
400
 
460
 

Loading…
Cancel
Save