Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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