Pārlūkot izejas kodu

Experimenting with FrSky code...

Thomas Buck 8 gadus atpakaļ
vecāks
revīzija
3d171e1bdc
1 mainītis faili ar 112 papildinājumiem un 52 dzēšanām
  1. 112
    52
      src/rx.c

+ 112
- 52
src/rx.c Parādīt failu

@@ -13,10 +13,7 @@
13 13
 #include "spi.h"
14 14
 #include "timer.h"
15 15
 #include "rx.h"
16
-
17
-#ifdef DEBUG
18
-#include "serial.h"
19
-#endif
16
+#include "main.h"
20 17
 
21 18
 #define CHANNELS 8
22 19
 #define PPM_MIN 1000
@@ -47,6 +44,9 @@ uint8_t frequencyOffsetHack = 0;
47 44
 uint16_t c[8];
48 45
 int rssi;
49 46
 
47
+// original 0x5C 0x76 0x27
48
+uint8_t freq2 = 0x5C, freq1 = 0x70, freq0 = 0x00;
49
+
50 50
 static uint16_t ppmBuffer[CHANNELS];
51 51
 
52 52
 static long map(long x, long in_min, long in_max, long out_min, long out_max);
@@ -61,36 +61,51 @@ static void readBindingData(void);
61 61
 static void writeBindingData(void);
62 62
 
63 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 68
     initialize(1); // binding
69
+
70
+    debugWrite("CC2500: binding\n");
71
+
69 72
     binding();
70 73
 
71
-#ifdef DEBUG
72
-    serialWriteString(0, "RX: receiving\n");
73
-#endif
74
+    debugWrite("CC2500: receiving\n");
74 75
 
75 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 79
     cc2500Strobe(CC2500_SRX);
79 80
 }
80 81
 
81 82
 static void initialize(uint8_t bind) {
82 83
     cc2500ResetChip();
83 84
     cc2500WriteReg(CC2500_02_IOCFG0,   0x01); // RX complete interrupt(GDO0)
84
-    cc2500WriteReg(CC2500_17_MCSM1,    0x0C);
85
+    cc2500WriteReg(CC2500_17_MCSM1,    0x0F);
85 86
     cc2500WriteReg(CC2500_18_MCSM0,    0x18);
86 87
     cc2500WriteReg(CC2500_06_PKTLEN,   0x19); // Leave room for appended status bytes
87 88
     cc2500WriteReg(CC2500_08_PKTCTRL0, 0x05);
88 89
     cc2500WriteReg(CC2500_3E_PATABLE,  0xFF);
89 90
     cc2500WriteReg(CC2500_0B_FSCTRL1,  0x08);
90 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 100
     cc2500WriteReg(CC2500_0D_FREQ2,    0x5C);
92 101
     cc2500WriteReg(CC2500_0E_FREQ1,    0x76);
93 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 109
     cc2500WriteReg(CC2500_10_MDMCFG4,  0xAA);
95 110
     cc2500WriteReg(CC2500_11_MDMCFG3,  0x39);
96 111
     cc2500WriteReg(CC2500_12_MDMCFG2,  0x11);
@@ -108,13 +123,16 @@ static void initialize(uint8_t bind) {
108 123
     cc2500WriteReg(CC2500_24_FSCAL2,   0x05);
109 124
     cc2500WriteReg(CC2500_25_FSCAL1,   0x00);
110 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 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 137
     cc2500Strobe(CC2500_SIDLE); // Go to idle...
120 138
 
@@ -122,11 +140,15 @@ static void initialize(uint8_t bind) {
122 140
     uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
123 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 152
 #endif
131 153
 
132 154
     // hack: Append status, filter by address, auto-flush on bad crc, PQT=0
@@ -142,15 +164,11 @@ static void binding(void) {
142 164
     readBindingData();
143 165
     if ((txid[0] != 0xff) || (txid[1] != 0xff)) {
144 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 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 173
     // No valid txid, forcing bind
156 174
     tuning();
@@ -159,9 +177,7 @@ static void binding(void) {
159 177
     cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
160 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 182
     performBind();
167 183
 }
@@ -169,32 +185,69 @@ static void binding(void) {
169 185
 static void tuning() {
170 186
     cc2500Strobe(CC2500_SRX); // enter in rx mode
171 187
 
172
-    int frequencyOffsetTimer = 0;
188
+    uint16_t frequencyOffsetTimer = 0;
173 189
     while (1) {
174 190
         frequencyOffsetTimer++;
175 191
         if (frequencyOffsetTimer > 3000) {
176 192
             frequencyOffsetTimer = 0;
177 193
             cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
178
-            frequencyOffsetHack += 10;
179
-            if (frequencyOffsetHack > 250) {
194
+            if (frequencyOffsetHack <= 250) {
195
+                frequencyOffsetHack += 5;
196
+            } else {
180 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 225
             //cc2500Strobe(CC2500_SRX); // enter in rx mode
183 226
         }
184 227
 
185 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 233
             if (ccLen) {
234
+                debugWrite("Tuning: got data!\n");
188 235
                 cc2500ReadFifo(ccData, ccLen);
189 236
                 if ((ccData[ccLen - 1] & 0x80)
190 237
                         && (ccData[2] == 0x01)
191 238
                         && (ccData[5] == 0x00)) {
192 239
                     break;
193 240
                 }
241
+            } else {
242
+                debugWrite("Tuning: no data?!\n");
194 243
             }
195 244
         }
196 245
 
197 246
         wdt_reset();
247
+
248
+#ifdef DEBUG
249
+        uartMenu();
250
+#endif
198 251
     }
199 252
 }
200 253
 
@@ -203,7 +256,8 @@ static void performBind(void) {
203 256
 
204 257
     while (1) {
205 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 261
             if (ccLen) {
208 262
                 cc2500ReadFifo(ccData, ccLen);
209 263
                 if ((ccData[ccLen - 1] & 0x80)
@@ -220,18 +274,21 @@ static void performBind(void) {
220 274
         }
221 275
 
222 276
         wdt_reset();
223
-    }
224 277
 
225 278
 #ifdef DEBUG
226
-    serialWriteString(0, "RX: got hop data, reading list...\n");
279
+        uartMenu();
227 280
 #endif
281
+    }
282
+
283
+    debugWrite("RX: got hop data, reading list...\n");
228 284
 
229 285
     listLength = 0;
230 286
     uint8_t eol = 0;
231 287
     for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
232 288
         while (1) {
233 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 292
                 if (ccLen) {
236 293
                     cc2500ReadFifo(ccData, ccLen);
237 294
                     if ((ccData[ccLen - 1] & 0x80)
@@ -254,6 +311,10 @@ static void performBind(void) {
254 311
             }
255 312
 
256 313
             wdt_reset();
314
+
315
+#ifdef DEBUG
316
+            uartMenu();
317
+#endif
257 318
         }
258 319
 
259 320
         if (eol) {
@@ -261,9 +322,7 @@ static void performBind(void) {
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 327
     writeBindingData();
269 328
     cc2500Strobe(CC2500_SIDLE); // Back to idle
@@ -320,11 +379,9 @@ void rxReceivePacket() {
320 379
                 nextChannel(SEEK_CHANNEL_SKIP);
321 380
                 counter++;
322 381
 
323
-#ifdef DEBUG
324 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 386
                 if (counter == (MAX_MISSING_PACKET << 2)) counter = 0;
330 387
                 break;
@@ -334,7 +391,8 @@ void rxReceivePacket() {
334 391
         }
335 392
 
336 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 396
             if (ccLen > 20) {
339 397
                 ccLen = 20;
340 398
             }
@@ -348,7 +406,8 @@ void rxReceivePacket() {
348 406
                         packet = 1;
349 407
 
350 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 411
                         if (rssi_dec < 128) {
353 412
                             rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
354 413
                         } else {
@@ -367,6 +426,10 @@ void rxReceivePacket() {
367 426
         }
368 427
 
369 428
         wdt_reset();
429
+
430
+#ifdef DEBUG
431
+        uartMenu();
432
+#endif
370 433
     }
371 434
 
372 435
     if (packet != 0) {
@@ -391,10 +454,7 @@ void rxReceivePacket() {
391 454
         ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
392 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 458
         cppmCopy(ppmBuffer);
399 459
     }
400 460
 

Notiek ielāde…
Atcelt
Saglabāt