Subversion Repositories FlightCtrl

Rev

Rev 1221 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1221 Rev 1222
1
/*#######################################################################################
1
/*#######################################################################################
2
Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit
2
Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit
3
#######################################################################################*/
3
#######################################################################################*/
4
 
4
 
5
#include "spectrum.h"
5
#include "Spectrum.h"
-
 
6
#include "main.h"
-
 
7
 
-
 
8
//--------------------------------------------------------------//
-
 
9
 
-
 
10
//--------------------------------------------------------------//
-
 
11
void SpektrumBinding(void)
-
 
12
{
-
 
13
  unsigned int timerTimeout = SetDelay(10000);  // Timeout 10 sec.
-
 
14
  unsigned char connected = 0;
-
 
15
  unsigned int delaycounter;
-
 
16
 
-
 
17
  UCSR1B &= ~(1 << RXCIE1);  // disable rx-interrupt
-
 
18
  UCSR1B &= ~(1<<RXEN1);     // disable Uart-Rx
-
 
19
  PORTD &= ~(1 << PORTD2);   // disable pull-up
-
 
20
 
-
 
21
  printf("\n\rPlease connect Spektrum receiver for binding NOW...");
-
 
22
 
-
 
23
  while(!CheckDelay(timerTimeout))
-
 
24
  {
-
 
25
    if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; }
-
 
26
  }
-
 
27
 
-
 
28
  if (connected)
-
 
29
  {
-
 
30
   
-
 
31
    printf("ok.\n\r");
-
 
32
    DDRD |= (1 << DDD2);     // Rx as output
-
 
33
 
-
 
34
        while(!CheckDelay(timerTimeout));  // delay after startup of RX
-
 
35
        for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2);
-
 
36
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
37
       
-
 
38
        for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2);
-
 
39
    for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
40
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2);
-
 
41
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
42
     
-
 
43
        for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2);
-
 
44
    for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
45
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2);
-
 
46
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
47
 
-
 
48
        for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2);
-
 
49
    for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
50
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2);
-
 
51
 
-
 
52
  }
-
 
53
   else
-
 
54
  { printf("Timeout.\n\r");
-
 
55
 
-
 
56
 
-
 
57
  }
-
 
58
   
-
 
59
  DDRD &= ~(1 << DDD2);      // RX as input
-
 
60
  PORTD &= ~(1 << PORTD2);  
-
 
61
 
-
 
62
  Uart1Init();    // init Uart again
-
 
63
}
-
 
64
 
-
 
65
//############################################################################
-
 
66
// zum Decodieren des Spektrum Satelliten wird USART1 benutzt.
-
 
67
// USART1 initialisation from killagreg
-
 
68
void Uart1Init(void)
-
 
69
//############################################################################
-
 
70
    {
-
 
71
        // -- Start of USART1 initialisation for Spekturm seriell-mode
-
 
72
        // USART1 Control and Status Register A, B, C and baud rate register
-
 
73
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1);
-
 
74
        // disable RX-Interrupt
-
 
75
        UCSR1B &= ~(1 << RXCIE1);
-
 
76
        // disable TX-Interrupt
-
 
77
        UCSR1B &= ~(1 << TXCIE1);
-
 
78
        // disable DRE-Interrupt
-
 
79
        UCSR1B &= ~(1 << UDRIE1);
-
 
80
        // set direction of RXD1 and TXD1 pins
-
 
81
        // set RXD1 (PD2) as an input pin
-
 
82
        PORTD |= (1 << PORTD2);
-
 
83
        DDRD &= ~(1 << DDD2);
-
 
84
        // USART0 Baud Rate Register
-
 
85
        // set clock divider
-
 
86
       
-
 
87
        UBRR1H = (uint8_t)(ubrr>>8);
-
 
88
        UBRR1L = (uint8_t)ubrr;
-
 
89
        // enable double speed operation
-
 
90
        UCSR1A |= (1 << U2X1);
-
 
91
        // enable receiver and transmitter
-
 
92
        //UCSR1B = (1<<RXEN1)|(1<<TXEN1);
-
 
93
 
-
 
94
 
-
 
95
       
-
 
96
       
-
 
97
        UCSR1B = (1<<RXEN1);
-
 
98
        // set asynchronous mode
-
 
99
        UCSR1C &= ~(1 << UMSEL11);
-
 
100
        UCSR1C &= ~(1 << UMSEL10);
-
 
101
        // no parity
-
 
102
        UCSR1C &= ~(1 << UPM11);
-
 
103
        UCSR1C &= ~(1 << UPM10);
-
 
104
        // 1 stop bit
-
 
105
        UCSR1C &= ~(1 << USBS1);
-
 
106
        // 8-bit
-
 
107
        UCSR1B &= ~(1 << UCSZ12);
-
 
108
        UCSR1C |=  (1 << UCSZ11);
-
 
109
        UCSR1C |=  (1 << UCSZ10);
-
 
110
        // flush receive buffer explicit
-
 
111
        while(UCSR1A & (1<<RXC1)) UDR1;
-
 
112
        // enable RX-interrupts at the end
-
 
113
        UCSR1B |= (1 << RXCIE1);
-
 
114
        // -- End of USART1 initialisation
6
#include rc.h
115
  return;
7
 
116
 }
8
 
117
       
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
118
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Copyright (c) Rainer Walther
119
// + Copyright (c) Rainer Walther
11
// + RC-routines from original MK rc.c (c) H&I
120
// + RC-routines from original MK rc.c (c) H&I
12
// + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2
121
// + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2
13
// + only for non-profit use
122
// + only for non-profit use
14
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
//
124
//
16
// 20080808 rw Modified for Spektrum AR6100 (PPM)
125
// 20080808 rw Modified for Spektrum AR6100 (PPM)
17
// 20080823 rw Add Spektrum satellite receiver on USART1 (644P only)
126
// 20080823 rw Add Spektrum satellite receiver on USART1 (644P only)
18
// 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels)
127
// 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels)
19
//             Replace AR6100-coding with original composit-signal routines
128
//             Replace AR6100-coding with original composit-signal routines
20
//
129
//
21
// ---
130
// ---
22
// Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben!
131
// Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben!
23
// Binding is not implemented. Bind with external Receiver.
132
// Binding is not implemented. Bind with external Receiver.
24
// Servo output J3, J4, J5 not serviced
133
// Servo output J3, J4, J5 not serviced
25
//
134
//
26
// Anschuß Spektrum Receiver
135
// Anschuß Spektrum Receiver
27
//              Orange:         3V von der FC (keinesfalls an 5V anschließen!)
136
//              Orange:         3V von der FC (keinesfalls an 5V anschließen!)
28
//              Schwarz:        GND
137
//              Schwarz:        GND
29
//              Grau:           RXD1 (Pin 3) auf 10-Pol FC-Stecker
138
//              Grau:           RXD1 (Pin 3) auf 10-Pol FC-Stecker
30
//
139
//
31
// ---
140
// ---
32
// Satellite-Reciever connected on USART1:
141
// Satellite-Reciever connected on USART1:
33
//
142
//
34
// DX7/DX6i: One data-frame at 115200 baud every 22ms.
143
// DX7/DX6i: One data-frame at 115200 baud every 22ms.
35
// DX7se:    One data-frame at 115200 baud every 11ms.
144
// DX7se:    One data-frame at 115200 baud every 11ms.
36
//              byte1:  unknown
145
//              byte1:  unknown
37
//      byte2:  unknown
146
//      byte2:  unknown
38
//      byte3:  and byte4:  channel data        (FLT-Mode)
147
//      byte3:  and byte4:  channel data        (FLT-Mode)
39
//      byte5:  and byte6:  channel data        (Roll)
148
//      byte5:  and byte6:  channel data        (Roll)
40
//      byte7:  and byte8:  channel data        (Nick)
149
//      byte7:  and byte8:  channel data        (Nick)
41
//      byte9:  and byte10: channel data        (Gier)
150
//      byte9:  and byte10: channel data        (Gier)
42
//      byte11: and byte12: channel data        (Gear Switch)
151
//      byte11: and byte12: channel data        (Gear Switch)
43
//      byte13: and byte14: channel data        (Gas)
152
//      byte13: and byte14: channel data        (Gas)
44
//      byte15: and byte16: channel data        (AUX2)
153
//      byte15: and byte16: channel data        (AUX2)
45
//
154
//
46
// DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9
155
// DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9
47
//  1st Frame:
156
//  1st Frame:
48
//              byte1:  unknown
157
//              byte1:  unknown
49
//      byte2:  unknown
158
//      byte2:  unknown
50
//              byte3:  and byte4:  channel data
159
//              byte3:  and byte4:  channel data
51
//      byte5:  and byte6:  channel data
160
//      byte5:  and byte6:  channel data
52
//      byte7:  and byte8:  channel data
161
//      byte7:  and byte8:  channel data
53
//      byte9:  and byte10: channel data
162
//      byte9:  and byte10: channel data
54
//      byte11: and byte12: channel data
163
//      byte11: and byte12: channel data
55
//      byte13: and byte14: channel data
164
//      byte13: and byte14: channel data
56
//      byte15: and byte16: channel data
165
//      byte15: and byte16: channel data 
57
//  2nd Frame:
166
//  2nd Frame:
58
//              byte1:  unknown
167
//              byte1:  unknown
59
//      byte2:  unknown
168
//      byte2:  unknown
60
//              byte3:  and byte4:  channel data
169
//              byte3:  and byte4:  channel data
61
//      byte5:  and byte6:  channel data
170
//      byte5:  and byte6:  channel data
62
//      byte7:  and byte8:  0xffff
171
//      byte7:  and byte8:  0xffff
63
//      byte9:  and byte10: 0xffff
172
//      byte9:  and byte10: 0xffff
64
//      byte11: and byte12: 0xffff
173
//      byte11: and byte12: 0xffff
65
//      byte13: and byte14: 0xffff
174
//      byte13: and byte14: 0xffff
66
//      byte15: and byte16: 0xffff
175
//      byte15: and byte16: 0xffff
67
//
176
//
68
// Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as:
177
// Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as:
69
//
178
//
70
// Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
179
// Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
71
//
180
//
72
// 0 means a '0' bit
181
// 0 means a '0' bit
73
// F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only)
182
// F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only)
74
// C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter)
183
// C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter)
75
// D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel
184
// D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel
76
//
185
//
77
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
186
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78
 
-
 
-
 
187
 
-
 
188
//############################################################################
79
 
189
//Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever
80
void spectrum_parser(uint8_t c)
-
 
-
 
190
SIGNAL(USART1_RX_vect)
81
 
191
//############################################################################
82
{
192
{
83
        static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer;
193
static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer;
84
        unsigned int Channel, index;
194
        unsigned int Channel, index;
85
        signed int signal, tmp;
195
        signed int signal, tmp;
86
        int bCheckDelay;
196
        int bCheckDelay;
-
 
197
        uint8_t c;
87
 
198
       
-
 
199
        c = UDR1; // get data byte
-
 
200
       
88
        if (ReSync == 1)
201
        if (ReSync == 1)
89
            {
202
            {
90
                // wait for beginning of new frame
203
                // wait for beginning of new frame
91
                ReSync = 0;
204
                ReSync = 0;
92
 
205
               
93
                FrameTimer = SetDelay(7);  // minimum 7ms zwischen den frames
206
                FrameTimer = SetDelay(7);  // minimum 7ms zwischen den frames
94
                FrameCnt = 0;
207
                FrameCnt = 0;
95
                Sync = 0;
208
                Sync = 0;
96
                ByteHigh = 0;
209
                ByteHigh = 0;
97
                }
210
                }
98
  else
211
  else
99
  {
212
  {    
100
        bCheckDelay = CheckDelay(FrameTimer);
213
        bCheckDelay = CheckDelay(FrameTimer);
101
        if ( Sync == 0 )
214
        if ( Sync == 0 )
102
            {
215
            {
103
                if(bCheckDelay)
216
                if(bCheckDelay)
104
                    {
217
                    {
105
                        // nach einer Pause von mind. 7ms erstes Sync-Character gefunden
218
                        // nach einer Pause von mind. 7ms erstes Sync-Character gefunden
106
                        // Zeichen ignorieren, da Bedeutung unbekannt
219
                        // Zeichen ignorieren, da Bedeutung unbekannt
107
                        Sync = 1;
220
                        Sync = 1;
108
                        FrameCnt ++;
221
                        FrameCnt ++;
109
                        }
222
                        }
110
                else
223
                else
111
                        {
224
                        {
112
                        // Zeichen kam vor Ablauf der 7ms Sync-Pause
225
                        // Zeichen kam vor Ablauf der 7ms Sync-Pause
113
                        // warten auf erstes Sync-Zeichen
226
                        // warten auf erstes Sync-Zeichen
114
                        }
227
                        }
115
                }
228
                }
116
        else if((Sync == 1) && !bCheckDelay)
229
        else if((Sync == 1) && !bCheckDelay)
117
            {
230
            {
118
                // zweites Sync-Character ignorieren, Bedeutung unbekannt
231
                // zweites Sync-Character ignorieren, Bedeutung unbekannt
119
                Sync = 2;
232
                Sync = 2;
120
                FrameCnt ++;
233
                FrameCnt ++;
121
                }
234
                }
122
        else if((Sync == 2) && !bCheckDelay)
235
        else if((Sync == 2) && !bCheckDelay)
123
            {
236
            {
124
                // Datenbyte high
237
                // Datenbyte high
125
                ByteHigh = c;
238
                ByteHigh = c;
126
 
239
               
127
                if (FrameCnt == 2)
240
                if (FrameCnt == 2)
128
                    {
241
                    {
129
                    // is 1st Byte of Channel-data
242
                    // is 1st Byte of Channel-data
130
                        // Frame 1 with Channel 1-7 comming next
243
                        // Frame 1 with Channel 1-7 comming next
131
                        Frame2 = 0;
244
                        Frame2 = 0;
132
                        if(ByteHigh & 0x80)
245
                        if(ByteHigh & 0x80)
133
                            {
246
                            {
134
                                // DS9: Frame 2 with Channel 8-9 comming next
247
                                // DS9: Frame 2 with Channel 8-9 comming next
135
                                Frame2 = 1;
248
                                Frame2 = 1;
136
                                }
249
                                }
137
                        }
250
                        }      
138
                Sync = 3;
251
                Sync = 3;
139
                FrameCnt ++;
252
                FrameCnt ++;
140
                }
253
                }
141
        else if((Sync == 3) && !bCheckDelay)
254
        else if((Sync == 3) && !bCheckDelay)
142
            {
255
            {
143
                // Datenbyte low
256
                // Datenbyte low
144
 
257
               
145
                // High-Byte for next channel comes next
258
                // High-Byte for next channel comes next
146
                Sync = 2;
259
                Sync = 2;
147
                FrameCnt ++;
260
                FrameCnt ++;
148
 
261
               
149
                index = (ByteHigh >> 2) & 0x0f;
262
                index = (ByteHigh >> 2) & 0x0f;
150
                index ++;
263
                index ++;
151
                Channel = (ByteHigh << 8) | c;
264
                Channel = (ByteHigh << 8) | c;
152
                signal = Channel & 0x3ff;
265
                signal = Channel & 0x3ff;
153
                signal -= 0x200;                // Offset, range 0x000..0x3ff?
266
                signal -= 0x200;                // Offset, range 0x000..0x3ff?
154
                signal = signal/3;              // scaling to fit PPM resolution
267
                signal = signal/3;              // scaling to fit PPM resolution
155
 
268
               
156
                if(index >= 0  &&  index <= 10)
269
                if(index >= 0  &&  index <= 10)
157
                    {
270
                    {
158
                // Stabiles Signal
271
                // Stabiles Signal
159
                if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;}
272
                if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;}
160
                tmp = (3 * (PPM_in[index]) + signal) / 4;
273
                tmp = (3 * (PPM_in[index]) + signal) / 4;  
161
                if(tmp > signal+1) tmp--; else
274
                if(tmp > signal+1) tmp--; else
162
                if(tmp < signal-1) tmp++;
275
                if(tmp < signal-1) tmp++;
163
                if(SenderOkay >= 180)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
276
                if(SenderOkay >= 180)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
164
                else PPM_diff[index] = 0;
277
                else PPM_diff[index] = 0;
165
                PPM_in[index] = tmp;
278
                PPM_in[index] = tmp;
166
                        }
279
                        }
167
                }
280
                }
168
        else
281
        else
169
                {
282
                {
170
                // hier stimmt was nicht: neu synchronisieren
283
                // hier stimmt was nicht: neu synchronisieren
171
                ReSync = 1;
284
                ReSync = 1;
172
                FrameCnt = 0;
285
                FrameCnt = 0;
173
                Frame2 = 0;
286
                Frame2 = 0;
174
                }
287
                }
175
 
288
               
176
        // 16 Bytes per frame
289
        // 16 Bytes per frame
177
        if(FrameCnt >= 16)
290
        if(FrameCnt >= 16)
178
            {
291
            {
179
                // Frame complete
292
                // Frame complete
180
                if(Frame2 == 0)
293
                if(Frame2 == 0)
181
                        {
294
                        {
182
                        // Null bedeutet: Neue Daten
295
                        // Null bedeutet: Neue Daten
183
                        // nur beim ersten Frame (CH 0-7) setzen
296
                        // nur beim ersten Frame (CH 0-7) setzen
184
                        NewPpmData = 0;
297
                        NewPpmData = 0;  
185
                        }
298
                        }
186
 
299
               
187
                // new frame next, nach fruehestens 7ms erwartet
300
                // new frame next, nach fruehestens 7ms erwartet
188
                FrameCnt = 0;
301
                FrameCnt = 0;
189
                Frame2 = 0;
302
                Frame2 = 0;
190
                Sync = 0;
303
                Sync = 0;
191
                }
304
                }
192
        // Zeit bis zum nächsten Zeichen messen
305
        // Zeit bis zum nächsten Zeichen messen
193
        FrameTimer = SetDelay(7);
306
        FrameTimer = SetDelay(7);
194
   }
307
   }
195
}
308
}  
196
 
309
 
197
 
310
 
198
 
311