Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2307 - 1
/*#######################################################################################
2
Decodes the sbus protocol
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
 
57
#include "sbus.h"
58
#include "main.h"
59
 
60
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
61
unsigned char NewSBusData = 0, sBusBuffer[25];
62
 
63
//############################################################################
64
// USART1 initialisation from killagreg
65
void SbusUartInit(void)
66
//############################################################################
67
    {
68
        // -- Start of USART1 initialisation for Spekturm seriell-mode
69
        // USART1 Control and Status Register A, B, C and baud rate register
70
        uint8_t sreg = SREG;
71
 
72
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 100000) - 1);
73
 
74
        // disable all interrupts before reconfiguration
75
        cli();
76
        // disable RX-Interrupt
77
        UCSR1B &= ~(1 << RXCIE1);
78
        // disable TX-Interrupt
79
        UCSR1B &= ~(1 << TXCIE1);
80
        // disable DRE-Interrupt
81
        UCSR1B &= ~(1 << UDRIE1);
82
        // set direction of RXD1 and TXD1 pins
83
        // set RXD1 (PD2) as an input pin
84
        PORTD |= (1 << PORTD2);
85
        DDRD &= ~(1 << DDD2);
86
 
87
        // set TXD1 (PD3) as an output pin
88
        PORTD |= (1 << PORTD3);
89
        DDRD  |= (1 << DDD3);
90
 
91
        // USART0 Baud Rate Register
92
        // set clock divider
93
        UBRR1H = (uint8_t)(ubrr>>8);
94
        UBRR1L = (uint8_t)ubrr;
95
        // enable double speed operation
96
        UCSR1A |= (1 << U2X1);
97
        // enable receiver and transmitter
98
        //UCSR1B = (1<<RXEN1)|(1<<TXEN1);
99
 
100
        UCSR1B = (1<<RXEN1);
101
        // set asynchronous mode
102
        UCSR1C &= ~(1 << UMSEL11);
103
        UCSR1C &= ~(1 << UMSEL10);
104
        // parity
105
        UCSR1C <=  (1 << UPM11);   // even
106
        UCSR1C &= ~(1 << UPM10);
107
        //  stop bit
108
        UCSR1C |= (1 << USBS1);    // two
109
        // 8-bit
110
        UCSR1B &= ~(1 << UCSZ12);
111
        UCSR1C |=  (1 << UCSZ11);
112
        UCSR1C |=  (1 << UCSZ10);
113
        // flush receive buffer explicit
114
        while(UCSR1A & (1<<RXC1)) UDR1;
115
        // enable RX-interrupts at the end
116
        UCSR1B |= (1 << RXCIE1);
117
        // -- End of USART1 initialisation
118
        // restore global interrupt flags
119
        sBusBuffer[23] |= 4; // This Bit contains the 'Signal loss'
120
        SREG = sreg;
121
  return;
122
 }
123
 
124
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
#define MIN_FRAMEGAP 68  // 7ms
126
#define MAX_BYTEGAP  3   // 310us
127
 
128
//############################################################################
129
// Is called by the uart RX interrupt
130
//############################################################################
131
void SbusParser(unsigned char udr)
132
{
133
 static unsigned char  ptr = 0;
134
  if(!SpektrumTimer && udr == 0x0f)  // wait for the start
135
   {
136
        ptr = 0;
137
    SpektrumTimer = 80; // 8ms gap
138
        }
139
        else
140
        {
141
     if(++ptr == 24)                    // last byte
142
          {
143
                NewSBusData = 1;
144
          }
145
        else
146
        if(ptr > 24) ptr = 25;
147
        else
148
        {
149
         sBusBuffer[ptr] = udr; // collect all bytes
150
        }
151
   }
152
}
153
 
154
void ProcessSBus(void)
155
{
156
 static unsigned char load = 0;
157
 unsigned char bitmask8 = 1, sbyte = 2, i, index = 1, process;
158
 unsigned int bitmask11 = 256;
159
 signed int signal = 0,tmp;
160
 
161
 if(!(sBusBuffer[23] & 4))      // This Bit contains the 'Signal loss'
162
           {
163
            TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input
164
                if(EE_Parameter.FailsafeChannel == 0 || PPM_in[EE_Parameter.FailsafeChannel] < 100)  // forces Failsafe if the receiver doesn't have 'signal loss' on Failsafe
165
                  {
166
                    if(SenderOkay < 200) SenderOkay += 20; else SenderOkay = 200;
167
                  }
168
                signal = sBusBuffer[1];
169
        if(!load--) { process = (12*11 - 8); load = 2;} else process = (4*11 - 8);  // lowers the processor load 
170
                for(i = 0; i < process; i++)  // collect the single bits
171
                {
172
                        if(sBusBuffer[sbyte] & bitmask8) signal |= bitmask11;
173
                        bitmask8 *= 2;
174
                        if(!bitmask8)
175
                        {
176
                         bitmask8 = 1;
177
                         sbyte++;
178
                        }
179
                        bitmask11 *= 2;
180
                    if(bitmask11 == 2048)
181
                    {
182
                         bitmask11 = 1;
183
                         signal = (signal-1024) / 5; // the resolution is higher than required
184
                tmp = (3 * (PPM_in[index]) + signal) / 4;
185
                if(tmp > signal+1) tmp--; else
186
                if(tmp < signal-1) tmp++;
187
                if(SenderOkay >= 195)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
188
                else PPM_diff[index] = 0;
189
                PPM_in[index] = tmp;
190
                         signal = 0;
191
                         index++; // next channel
192
                        }
193
                }
194
            NewPpmData = 0;  // Null bedeutet: Neue Daten
195
           }
196
 NewSBusData = 0;
197
}
198
 
199
#endif