Subversion Repositories FlightCtrl

Rev

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

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