Subversion Repositories FlightCtrl

Rev

Rev 2543 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1171 hbuss 1
/*#######################################################################################
2
Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit
3
#######################################################################################*/
4
 
1426 ingob 5
#include "Spektrum.h"
1171 hbuss 6
#include "main.h"
1506 holgerb 7
 
1320 hbuss 8
unsigned char SpektrumTimer = 0;
1171 hbuss 9
 
2185 holgerb 10
// Achtung: RECEIVER_SPEKTRUM_DX7EXP oder RECEIVER_SPEKTRUM_DX8EXP wird in der main.h gesetzt
1928 holgerb 11
#if defined (RECEIVER_SPEKTRUM_DX7EXP) || defined (RECEIVER_SPEKTRUM_DX8EXP)
2185 holgerb 12
unsigned char s_excnt = 0;                   // Bitcounter for Spektrum-Expander
1680 holgerb 13
unsigned char s_exparity = 0;                // Parity Bit for Spektrum-Expander
2185 holgerb 14
signed char s_exdata[11];                    // Data for Spektrum-Expander
2543 holgerb 15
#endif
2185 holgerb 16
 
17
void s_update(unsigned char channel, signed int value)  // Channel-Diff numbercrunching and finally assign new stickvalue to PPM_in
18
{
19
        if(SenderOkay >= 180) PPM_diff[channel] = ((value - PPM_in[channel]) / 3) * 3;
20
        else PPM_diff[channel] = 0;
2590 holgerb 21
        if(value > 127) value = 127; else if(value < -127) value = -127;
2185 holgerb 22
        PPM_in[channel] = value;
23
}
24
 
1171 hbuss 25
//############################################################################
26
// USART1 initialisation from killagreg
1424 ingob 27
void SpektrumUartInit(void)
1171 hbuss 28
//############################################################################
29
    {
1680 holgerb 30
        // -- Start of USART1 initialisation for Spekturm seriell-mode
31
        // USART1 Control and Status Register A, B, C and baud rate register
32
        uint8_t sreg = SREG;
1928 holgerb 33
 
1680 holgerb 34
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1);
1928 holgerb 35
 
1680 holgerb 36
        // disable all interrupts before reconfiguration
37
        cli();
38
        // disable RX-Interrupt
39
        UCSR1B &= ~(1 << RXCIE1);
40
        // disable TX-Interrupt
41
        UCSR1B &= ~(1 << TXCIE1);
42
        // disable DRE-Interrupt
43
        UCSR1B &= ~(1 << UDRIE1);
2309 holgerb 44
/*
1680 holgerb 45
        // set direction of RXD1 and TXD1 pins
46
        // set RXD1 (PD2) as an input pin
47
        PORTD |= (1 << PORTD2);
48
        DDRD &= ~(1 << DDD2);
49
        // set TXD1 (PD3) as an output pin
50
        PORTD |= (1 << PORTD3);
51
        DDRD  |= (1 << DDD3);
2309 holgerb 52
*/      
1680 holgerb 53
        // USART0 Baud Rate Register
54
        // set clock divider
55
        UBRR1H = (uint8_t)(ubrr>>8);
56
        UBRR1L = (uint8_t)ubrr;
57
        // enable double speed operation
58
        UCSR1A |= (1 << U2X1);
59
        // enable receiver and transmitter
60
        //UCSR1B = (1<<RXEN1)|(1<<TXEN1);
1391 killagreg 61
 
1680 holgerb 62
        UCSR1B = (1<<RXEN1);
63
        // set asynchronous mode
64
        UCSR1C &= ~(1 << UMSEL11);
65
        UCSR1C &= ~(1 << UMSEL10);
66
        // no parity
67
        UCSR1C &= ~(1 << UPM11);
68
        UCSR1C &= ~(1 << UPM10);
69
        // 1 stop bit
70
        UCSR1C &= ~(1 << USBS1);
71
        // 8-bit
72
        UCSR1B &= ~(1 << UCSZ12);
73
        UCSR1C |=  (1 << UCSZ11);
74
        UCSR1C |=  (1 << UCSZ10);
75
        // flush receive buffer explicit
76
        while(UCSR1A & (1<<RXC1)) UDR1;
77
        // enable RX-interrupts at the end
78
        UCSR1B |= (1 << RXCIE1);
79
        // -- End of USART1 initialisation
80
        // restore global interrupt flags
1928 holgerb 81
 
1680 holgerb 82
        SREG = sreg;
1171 hbuss 83
  return;
84
 }
1391 killagreg 85
 
1171 hbuss 86
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87
// + Copyright (c) Rainer Walther
88
// + RC-routines from original MK rc.c (c) H&I
89
// + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2
90
// + only for non-profit use
91
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
92
//
93
// 20080808 rw Modified for Spektrum AR6100 (PPM)
94
// 20080823 rw Add Spektrum satellite receiver on USART1 (644P only)
95
// 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels)
96
//             Replace AR6100-coding with original composit-signal routines
97
//
98
// ---
99
// Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben!
100
// Binding is not implemented. Bind with external Receiver.
101
// Servo output J3, J4, J5 not serviced
102
//
103
// Anschuß Spektrum Receiver
1680 holgerb 104
//              Orange:         3V von der FC (keinesfalls an 5V anschließen!)
105
//              Schwarz:        GND
106
//              Grau:           RXD1 (Pin 3) auf 10-Pol FC-Stecker
1171 hbuss 107
//
108
// ---
109
// Satellite-Reciever connected on USART1:
110
//
111
// DX7/DX6i: One data-frame at 115200 baud every 22ms.
112
// DX7se:    One data-frame at 115200 baud every 11ms.
1680 holgerb 113
//              byte1:  unknown
114
//      byte2:  unknown
115
//      byte3:  and byte4:  channel data        (FLT-Mode)
116
//      byte5:  and byte6:  channel data        (Roll)
117
//      byte7:  and byte8:  channel data        (Nick)
118
//      byte9:  and byte10: channel data        (Gier)
119
//      byte11: and byte12: channel data        (Gear Switch)
120
//      byte13: and byte14: channel data        (Gas)
121
//      byte15: and byte16: channel data        (AUX2)
1171 hbuss 122
//
123
// DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9
124
//  1st Frame:
1680 holgerb 125
//              byte1:  unknown
126
//      byte2:  unknown
127
//              byte3:  and byte4:  channel data
128
//      byte5:  and byte6:  channel data
129
//      byte7:  and byte8:  channel data
130
//      byte9:  and byte10: channel data
131
//      byte11: and byte12: channel data
132
//      byte13: and byte14: channel data
133
//      byte15: and byte16: channel data
1171 hbuss 134
//  2nd Frame:
1680 holgerb 135
//              byte1:  unknown
136
//      byte2:  unknown
137
//              byte3:  and byte4:  channel data
138
//      byte5:  and byte6:  channel data
139
//      byte7:  and byte8:  0xffff
140
//      byte9:  and byte10: 0xffff
141
//      byte11: and byte12: 0xffff
142
//      byte13: and byte14: 0xffff
143
//      byte15: and byte16: 0xffff
1171 hbuss 144
//
145
// Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as:
146
//
147
// Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
148
//
149
// 0 means a '0' bit
150
// F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only)
151
// C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter)
152
// D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel
153
//
154
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
155
 
1322 hbuss 156
#define MIN_FRAMEGAP 68  // 7ms
157
#define MAX_BYTEGAP  3   // 310us
1320 hbuss 158
 
1419 ingob 159
 
1171 hbuss 160
//############################################################################
1481 holgerb 161
// Wird im UART-Interrupt aufgerufen
1171 hbuss 162
//############################################################################
1419 ingob 163
void SpektrumParser(unsigned char c)
1171 hbuss 164
{
1320 hbuss 165
    static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0;
1680 holgerb 166
        unsigned int Channel, index = 0;
167
        signed int signal = 0, tmp;
168
        int bCheckDelay;
169
//      c = UDR1; // get data byte
170
        if(ReSync == 1)
171
            {
172
                // wait for beginning of new frame
173
                ReSync = 0;
174
                SpektrumTimer = MIN_FRAMEGAP;
175
                FrameCnt = 0;
176
                Sync = 0;
177
                ByteHigh = 0;
178
                }
1171 hbuss 179
  else
1391 killagreg 180
  {
1680 holgerb 181
        if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer);
182
        if ( Sync == 0 )
183
            {
184
                if(bCheckDelay)
185
                    {
186
                        // nach einer Pause von mind. 7ms erstes Sync-Character gefunden
187
                        // Zeichen ignorieren, da Bedeutung unbekannt
188
                        Sync = 1;
189
                        FrameCnt ++;
190
                    SpektrumTimer = MAX_BYTEGAP;
191
                        }
192
                else
193
                        {
194
                        // Zeichen kam vor Ablauf der 7ms Sync-Pause
195
                        // warten auf erstes Sync-Zeichen
196
                        SpektrumTimer = MIN_FRAMEGAP;
197
                    FrameCnt = 0;
198
                    Sync = 0;
199
                    ByteHigh = 0;
200
                        }
201
                }
202
        else if((Sync == 1) && !bCheckDelay)
203
            {
204
                // zweites Sync-Character ignorieren, Bedeutung unbekannt
205
                Sync = 2;
206
                FrameCnt ++;
207
                SpektrumTimer = MAX_BYTEGAP;
208
                }
209
        else if((Sync == 2) && !bCheckDelay)
210
            {
211
                SpektrumTimer = MAX_BYTEGAP;
212
                // Datenbyte high
213
                ByteHigh = c;
214
                if (FrameCnt == 2)
215
                    {
216
                    // is 1st Byte of Channel-data
217
                        // Frame 1 with Channel 1-7 comming next
218
                        Frame2 = 0;
219
                        if(ByteHigh & 0x80)
220
                            {
221
                                // DS9: Frame 2 with Channel 8-9 comming next
222
                                Frame2 = 1;
223
                                }
224
                        }
225
                Sync = 3;
226
                FrameCnt ++;
227
                }
228
        else if((Sync == 3) && !bCheckDelay)
229
            {
230
                // Datenbyte low
231
                // High-Byte for next channel comes next
232
                SpektrumTimer = MAX_BYTEGAP;
233
                Sync = 2;
234
                FrameCnt ++;
235
                Channel = ((unsigned int)ByteHigh << 8) | c;
236
                if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM)
237
                {
238
                        signal = Channel & 0x3ff;
239
                        signal -= 0x200;                // Offset, range 0x000..0x3ff?
240
                        signal = signal/3;              // scaling to fit PPM resolution
241
                        index = (ByteHigh >> 2) & 0x0f;
242
                }
243
                else  
244
                if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_HI_RES)
245
                {
246
                        signal = Channel & 0x7ff;
247
                        signal -= 0x400;                // Offset, range 0x000..0x7ff?
248
                        signal = signal/6;              // scaling to fit PPM resolution
249
                        index = (ByteHigh >> 3) & 0x0f;
1928 holgerb 250
                }      
1680 holgerb 251
                else  
252
                //if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_LOW_RES)
253
                {
254
                        signal = Channel & 0x3ff;
255
                        signal -= 360;          // Offset, range 0x000..0x3ff?
256
                        signal = signal/2;              // scaling to fit PPM resolution
257
                        index = (ByteHigh >> 2) & 0x0f;
258
                }
1391 killagreg 259
 
1680 holgerb 260
                index++;
261
                if(index < 13)
262
                {
263
                // Stabiles Signal
1928 holgerb 264
#if defined (RECEIVER_SPEKTRUM_DX7EXP) || defined (RECEIVER_SPEKTRUM_DX8EXP)
2185 holgerb 265
                                        if (index == 2) index = 4;                                                              // Analog channel reassigment (2 <-> 4) for logical numbering (1,2,3,4)
1928 holgerb 266
                                        else if (index == 4) index = 2;
1506 holgerb 267
#endif
2011 holgerb 268
                                if(abs(signal - PPM_in[index]) < 6)
269
                                   {
2185 holgerb 270
                                                                         if(EE_Parameter.FailsafeChannel == 0 || PPM_in[EE_Parameter.FailsafeChannel] < 100)  // forces Failsafe if the receiver doesn't have 'signal loss' on Failsafe
271
                                                                         {
2011 holgerb 272
                                        if(SenderOkay < 200) SenderOkay += 10;
273
                                         else
274
                                         {
1928 holgerb 275
                                                SenderOkay = 200;
276
                                                TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input
2011 holgerb 277
                                         }
2185 holgerb 278
                                                                         }    
279
                                                                   }
1928 holgerb 280
                                tmp = (3 * (PPM_in[index]) + signal) / 4;
281
                                if(tmp > signal+1) tmp--; else
282
                                if(tmp < signal-1) tmp++;
283
 
284
#ifdef RECEIVER_SPEKTRUM_DX7EXP
2185 holgerb 285
                                if(index == 6)                                                                                  // FLIGHT-MODE - The channel used for our data uplink
1928 holgerb 286
                                {
2185 holgerb 287
                                        if (signal > 100)                                                                       // SYNC received
1928 holgerb 288
                                        {
2185 holgerb 289
                                                if (s_exdata[s_excnt] == 125) s_exparity = ~s_exparity;                         // Bit = 1 -> Re-Invert parity bit
1928 holgerb 290
                                                if ((s_excnt == 6 && ((s_exparity != 0 && s_exdata[s_excnt] == -125) || (s_exparity == 0 && s_exdata[s_excnt] == 125))) || (s_excnt == 9 && ((s_exparity == 0 && s_exdata[s_excnt] == -125) || (s_exparity != 0 && s_exdata[s_excnt] == 125)))) // Parity check
291
                                                {
2185 holgerb 292
                                                        if (s_exdata[1] == 125 && s_exdata[2] == -125) s_update(5,-125);        // Reconstruct tripole Flight-Mode value (CH5)
293
                                                        else if (s_exdata[1] == -125 && s_exdata[2] == -125) s_update(5,0);     // Reconstruct tripole Flight-Mode value (CH5)
294
                                                        else if (s_exdata[1] == -125 && s_exdata[2] == 125) s_update(5,125);    // Reconstruct tripole Flight-Mode value (CH5)
295
                                                        s_update(6,s_exdata[3]);                                                // Elevator (CH6)
296
                                                        s_update(11,s_exdata[4]);                                               // Aileron (CH11)
297
                                                        s_update(12,s_exdata[5]);                                               // Rudder (CH12)
1680 holgerb 298
 
2185 holgerb 299
                                                        if (s_excnt == 9)                                                       // New Mode (12 Channels)
1928 holgerb 300
                                                        {
2185 holgerb 301
                                                                if (s_exdata[7] == 125) s_update(8,PPM_in[8]+5);                // Hover Pitch UP (CH8)
302
                                                                if (s_exdata[8] == 125) s_update(8,PPM_in[8]-5);                // Hover Pitch DN (CH8)
303
                                                                if (PPM_in[8] < -125) PPM_in[8] = -125;                         // Range-Limit
304
                                                                else if (PPM_in[8] > 125) PPM_in[8] = 125;                      // Range-Limit
305
                                                                s_update(10,s_exdata[6]);                                       // AUX2 (CH10)
1928 holgerb 306
                                                        }
307
                                                }
1680 holgerb 308
 
2185 holgerb 309
                                                s_excnt = 0;                                                                    // Reset bitcounter
310
                                                s_exparity = 0;                                                                 // Reset parity bit
1928 holgerb 311
                                        }
1506 holgerb 312
 
2185 holgerb 313
                                        if (signal < 10) s_exdata[++s_excnt] = -125;                                            // Bit = 0 -> value = -125 (min)
1928 holgerb 314
                                        if (s_excnt == 10) s_excnt = 0;                                                         // Overflow protection
315
                                        if (signal < -100)
316
                                        {
2185 holgerb 317
                                                s_exdata[s_excnt] = 125;                                                        // Bit = 1 -> value = 125 (max)
318
                                                s_exparity = ~s_exparity;                                                       // Bit = 1 -> Invert parity bit
1928 holgerb 319
                                        }
1680 holgerb 320
 
1928 holgerb 321
                                }
322
 
2185 holgerb 323
                                if (index < 5 ) s_update(index,tmp);                                                            // Update normal potis (CH1-4)
324
                                else if (index == 5) s_update(7,signal);                                                        // Gear (CH7)
325
                                else if (index == 7) s_update(9,signal);                                                        // Hover Throttle (CH9)
326
 
1928 holgerb 327
#elif defined RECEIVER_SPEKTRUM_DX8EXP
2185 holgerb 328
                                if(index == 6)                                                                                  // FLIGHT-MODE - The channel used for our data uplink
1928 holgerb 329
                                {
2185 holgerb 330
                                        if (signal > 100)                                                                       // SYNC received
1928 holgerb 331
                                        {
332
                                                if (s_exdata[s_excnt] == 125) s_exparity = ~s_exparity; // Bit = 1 -> Re-Invert parity bit
333
                                                if (s_excnt == 9 && ((s_exparity == 0 && s_exdata[s_excnt] == -125) || (s_exparity != 0 && s_exdata[s_excnt] == 125)))  // Parity check
334
                                                {
2185 holgerb 335
                                                        if (s_exdata[1] == 125 && s_exdata[2] == -125) s_update(5,-125);        // Reconstruct tripole Flight-Mode value (CH5)
336
                                                        else if (s_exdata[1] == -125 && s_exdata[2] == -125) s_update(5,0);     // Reconstruct tripole Flight-Mode value (CH5)
337
                                                        else if (s_exdata[1] == -125 && s_exdata[2] == 125) s_update(5,125);    // Reconstruct tripole Flight-Mode value (CH5)
1928 holgerb 338
 
2185 holgerb 339
                                                        if (s_exdata[3] == 125 && s_exdata[6] == -125) s_update(6,125);         // Reconstruct tripole Elev D/R value (CH6)
340
                                                        else if (s_exdata[3] == -125 && s_exdata[6] == -125) s_update(6,0);     // Reconstruct tripole Elev D/R value (CH6)
341
                                                        else if (s_exdata[3] == -125 && s_exdata[6] == 125) s_update(6,-125);   // Reconstruct tripole Elev D/R value (CH6)
1928 holgerb 342
 
343
 
2185 holgerb 344
                                                        if (s_exdata[7] == 125 && s_exdata[8] == -125) s_update(9,-125);        // Reconstruct tripole AIL D/R value (CH9)
345
                                                        else if (s_exdata[7] == -125 && s_exdata[8] == -125) s_update(9,0);     // Reconstruct tripole AIL D/R value (CH9)
346
                                                        else if (s_exdata[7] == -125 && s_exdata[8] == 125) s_update(9,125);    // Reconstruct tripole AIL D/R value (CH9)
1928 holgerb 347
 
2185 holgerb 348
                                                        s_update(10,s_exdata[5]);                                               // Gear (CH10)
349
                                                        s_update(12,s_exdata[4]);                                               // Mix (CH12)
1928 holgerb 350
                                                }
351
 
352
                                                s_excnt = 0;                                                                    // Reset bitcounter
353
                                                s_exparity = 0;                                                                 // Reset parity bit
354
                                        }
355
 
356
                                        if (signal < 10) s_exdata[++s_excnt] = -125;                                            // Bit = 0 -> value = -125 (min)
357
                                        if (s_excnt == 10) s_excnt = 0;                                                         // Overflow protection
358
                                        if (signal < -100)
359
                                        {
360
                                                s_exdata[s_excnt] = 125;                                                        // Bit = 1 -> value = 125 (max)
361
                                                s_exparity = ~s_exparity;                                                       // Bit = 1 -> Invert parity bit
362
                                        }
363
 
364
                                }
2185 holgerb 365
 
366
                                if (index < 5 ) s_update(index,tmp);                                                            // Update normal potis (CH1-4)
367
                                else if (index == 7) s_update(7,signal);                                                        // R Trim (CH7)
368
                                else if (index == 5) s_update(8,signal);                                                        // AUX2 (CH8)
369
                                else if (index == 8) s_update(11,signal);                                                       // AUX3 (CH11)
370
 
371
#else
1928 holgerb 372
                                if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
373
                                else PPM_diff[index] = 0;
2590 holgerb 374
                                                                if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
1928 holgerb 375
                                PPM_in[index] = tmp;
1506 holgerb 376
#endif
1928 holgerb 377
                        }
1506 holgerb 378
        else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren
1680 holgerb 379
                }
380
        else
381
                {
382
                // hier stimmt was nicht: neu synchronisieren
383
                ReSync = 1;
384
                FrameCnt = 0;
385
                Frame2 = 0;
386
                // new frame next, nach fruehestens 7ms erwartet
387
                SpektrumTimer = MIN_FRAMEGAP;
388
                }
1391 killagreg 389
 
1680 holgerb 390
        // 16 Bytes eingetroffen -> Komplett
391
        if(FrameCnt >= 16)
392
            {
393
                // Frame complete
394
                if(Frame2 == 0)
395
                        {
396
                        // Null bedeutet: Neue Daten
397
                        // nur beim ersten Frame (CH 0-7) setzen
398
                        if(!ReSync) NewPpmData = 0;
399
                        }
400
                FrameCnt = 0;
401
                Frame2 = 0;
402
                Sync = 0;
403
                SpektrumTimer = MIN_FRAMEGAP;
404
                }
1171 hbuss 405
   }
1391 killagreg 406
}
1928 holgerb 407
 
2185 holgerb 408