Subversion Repositories FlightCtrl

Rev

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

Rev 2034 Rev 2050
1
/*#######################################################################################
1
/*#######################################################################################
2
Decodes the sbus protocol
2
Decodes the sbus protocol
3
#######################################################################################*/
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
 
56
 
5
#include "sbus.h"
57
#include "sbus.h"
6
#include "main.h"
58
#include "main.h"
7
 
59
 
8
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
60
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
9
unsigned char NewSBusData = 0, sBusBuffer[25];
61
unsigned char NewSBusData = 0, sBusBuffer[25];
10
 
62
 
11
//############################################################################
63
//############################################################################
12
// USART1 initialisation from killagreg
64
// USART1 initialisation from killagreg
13
void SbusUartInit(void)
65
void SbusUartInit(void)
14
//############################################################################
66
//############################################################################
15
    {
67
    {
16
        // -- Start of USART1 initialisation for Spekturm seriell-mode
68
        // -- Start of USART1 initialisation for Spekturm seriell-mode
17
        // USART1 Control and Status Register A, B, C and baud rate register
69
        // USART1 Control and Status Register A, B, C and baud rate register
18
        uint8_t sreg = SREG;
70
        uint8_t sreg = SREG;
19
       
71
       
20
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 100000) - 1);
72
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 100000) - 1);
21
       
73
       
22
        // disable all interrupts before reconfiguration
74
        // disable all interrupts before reconfiguration
23
        cli();
75
        cli();
24
        // disable RX-Interrupt
76
        // disable RX-Interrupt
25
        UCSR1B &= ~(1 << RXCIE1);
77
        UCSR1B &= ~(1 << RXCIE1);
26
        // disable TX-Interrupt
78
        // disable TX-Interrupt
27
        UCSR1B &= ~(1 << TXCIE1);
79
        UCSR1B &= ~(1 << TXCIE1);
28
        // disable DRE-Interrupt
80
        // disable DRE-Interrupt
29
        UCSR1B &= ~(1 << UDRIE1);
81
        UCSR1B &= ~(1 << UDRIE1);
30
        // set direction of RXD1 and TXD1 pins
82
        // set direction of RXD1 and TXD1 pins
31
        // set RXD1 (PD2) as an input pin
83
        // set RXD1 (PD2) as an input pin
32
        PORTD |= (1 << PORTD2);
84
        PORTD |= (1 << PORTD2);
33
        DDRD &= ~(1 << DDD2);
85
        DDRD &= ~(1 << DDD2);
34
 
86
 
35
        // set TXD1 (PD3) as an output pin
87
        // set TXD1 (PD3) as an output pin
36
        PORTD |= (1 << PORTD3);
88
        PORTD |= (1 << PORTD3);
37
        DDRD  |= (1 << DDD3);
89
        DDRD  |= (1 << DDD3);
38
       
90
       
39
        // USART0 Baud Rate Register
91
        // USART0 Baud Rate Register
40
        // set clock divider
92
        // set clock divider
41
        UBRR1H = (uint8_t)(ubrr>>8);
93
        UBRR1H = (uint8_t)(ubrr>>8);
42
        UBRR1L = (uint8_t)ubrr;
94
        UBRR1L = (uint8_t)ubrr;
43
        // enable double speed operation
95
        // enable double speed operation
44
        UCSR1A |= (1 << U2X1);
96
        UCSR1A |= (1 << U2X1);
45
        // enable receiver and transmitter
97
        // enable receiver and transmitter
46
        //UCSR1B = (1<<RXEN1)|(1<<TXEN1);
98
        //UCSR1B = (1<<RXEN1)|(1<<TXEN1);
47
 
99
 
48
        UCSR1B = (1<<RXEN1);
100
        UCSR1B = (1<<RXEN1);
49
        // set asynchronous mode
101
        // set asynchronous mode
50
        UCSR1C &= ~(1 << UMSEL11);
102
        UCSR1C &= ~(1 << UMSEL11);
51
        UCSR1C &= ~(1 << UMSEL10);
103
        UCSR1C &= ~(1 << UMSEL10);
52
        // parity
104
        // parity
53
        UCSR1C <=  (1 << UPM11);   // even
105
        UCSR1C <=  (1 << UPM11);   // even
54
        UCSR1C &= ~(1 << UPM10);
106
        UCSR1C &= ~(1 << UPM10);
55
        //  stop bit
107
        //  stop bit
56
        UCSR1C |= (1 << USBS1);    // two
108
        UCSR1C |= (1 << USBS1);    // two
57
        // 8-bit
109
        // 8-bit
58
        UCSR1B &= ~(1 << UCSZ12);
110
        UCSR1B &= ~(1 << UCSZ12);
59
        UCSR1C |=  (1 << UCSZ11);
111
        UCSR1C |=  (1 << UCSZ11);
60
        UCSR1C |=  (1 << UCSZ10);
112
        UCSR1C |=  (1 << UCSZ10);
61
        // flush receive buffer explicit
113
        // flush receive buffer explicit
62
        while(UCSR1A & (1<<RXC1)) UDR1;
114
        while(UCSR1A & (1<<RXC1)) UDR1;
63
        // enable RX-interrupts at the end
115
        // enable RX-interrupts at the end
64
        UCSR1B |= (1 << RXCIE1);
116
        UCSR1B |= (1 << RXCIE1);
65
        // -- End of USART1 initialisation
117
        // -- End of USART1 initialisation
66
        // restore global interrupt flags
118
        // restore global interrupt flags
67
        sBusBuffer[23] |= 4; // This Bit contains the 'Signal loss'
119
        sBusBuffer[23] |= 4; // This Bit contains the 'Signal loss'
68
        SREG = sreg;
120
        SREG = sreg;
69
  return;
121
  return;
70
 }
122
 }
71
 
123
 
72
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
73
#define MIN_FRAMEGAP 68  // 7ms
125
#define MIN_FRAMEGAP 68  // 7ms
74
#define MAX_BYTEGAP  3   // 310us
126
#define MAX_BYTEGAP  3   // 310us
75
 
127
 
76
//############################################################################
128
//############################################################################
77
// Is called by the uart interrupt
129
// Is called by the uart RX interrupt
78
//############################################################################
130
//############################################################################
79
void SbusParser(unsigned char udr)
131
void SbusParser(unsigned char udr)
80
{
132
{
81
 static unsigned char  ptr = 0;
133
 static unsigned char  ptr = 0;
82
  if(!SpektrumTimer && udr == 0x0f)  // wait for the start
134
  if(!SpektrumTimer && udr == 0x0f)  // wait for the start
83
   {
135
   {
84
        ptr = 0;
136
        ptr = 0;
85
    SpektrumTimer = 80; // 8ms gap
137
    SpektrumTimer = 80; // 8ms gap
86
        }
138
        }
87
        else
139
        else
88
        {
140
        {
89
     if(++ptr == 24)                    // last byte
141
     if(++ptr == 24)                    // last byte
90
          {
142
          {
91
                NewSBusData = 1;
143
                NewSBusData = 1;
92
          }
144
          }
93
        else
145
        else
94
        if(ptr > 24) ptr = 25;
146
        if(ptr > 24) ptr = 25;
95
        else
147
        else
96
        {
148
        {
97
         sBusBuffer[ptr] = udr; // collect all bytes
149
         sBusBuffer[ptr] = udr; // collect all bytes
98
        }
150
        }
99
   }
151
   }
100
}
152
}
101
 
153
 
102
void ProcessSBus(void)
154
void ProcessSBus(void)
103
{
155
{
104
 static unsigned char load = 0;
156
 static unsigned char load = 0;
105
 unsigned char bitmask8 = 1, sbyte = 2, i, index = 1, process;
157
 unsigned char bitmask8 = 1, sbyte = 2, i, index = 1, process;
106
 unsigned int bitmask11 = 256;
158
 unsigned int bitmask11 = 256;
107
 signed int signal = 0,tmp;
159
 signed int signal = 0,tmp;
108
 
160
 
109
 if(!(sBusBuffer[23] & 4))      // This Bit contains the 'Signal loss'
161
 if(!(sBusBuffer[23] & 4))      // This Bit contains the 'Signal loss'
110
           {
162
           {
111
            TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input
163
            TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input
112
                if(EE_Parameter.FailsafeChannel == 0 || PPM_in[EE_Parameter.FailsafeChannel] < 100)  // forces Failsafe if the receiver doesn't have 'signal loss' on Failsafe
164
                if(EE_Parameter.FailsafeChannel == 0 || PPM_in[EE_Parameter.FailsafeChannel] < 100)  // forces Failsafe if the receiver doesn't have 'signal loss' on Failsafe
113
                  {
165
                  {
114
                    if(SenderOkay < 200) SenderOkay += 20; else SenderOkay = 200;
166
                    if(SenderOkay < 200) SenderOkay += 20; else SenderOkay = 200;
115
                  }
167
                  }
116
                signal = sBusBuffer[1];
168
                signal = sBusBuffer[1];
117
        if(!load--) { process = (12*11 - 8); load = 2;} else process = (4*11 - 8);  // lowers the processor load 
169
        if(!load--) { process = (12*11 - 8); load = 2;} else process = (4*11 - 8);  // lowers the processor load 
118
                for(i = 0; i < process; i++)  // collect the single bits
170
                for(i = 0; i < process; i++)  // collect the single bits
119
                {
171
                {
120
                        if(sBusBuffer[sbyte] & bitmask8) signal |= bitmask11;
172
                        if(sBusBuffer[sbyte] & bitmask8) signal |= bitmask11;
121
                        bitmask8 *= 2;
173
                        bitmask8 *= 2;
122
                        if(!bitmask8)
174
                        if(!bitmask8)
123
                        {
175
                        {
124
                         bitmask8 = 1;
176
                         bitmask8 = 1;
125
                         sbyte++;
177
                         sbyte++;
126
                        }
178
                        }
127
                        bitmask11 *= 2;
179
                        bitmask11 *= 2;
128
                    if(bitmask11 == 2048)
180
                    if(bitmask11 == 2048)
129
                    {
181
                    {
130
                         bitmask11 = 1;
182
                         bitmask11 = 1;
131
                         signal = (signal-1024) / 5; // the resolution is higher than required
183
                         signal = (signal-1024) / 5; // the resolution is higher than required
132
                tmp = (3 * (PPM_in[index]) + signal) / 4;
184
                tmp = (3 * (PPM_in[index]) + signal) / 4;
133
                if(tmp > signal+1) tmp--; else
185
                if(tmp > signal+1) tmp--; else
134
                if(tmp < signal-1) tmp++;
186
                if(tmp < signal-1) tmp++;
135
                if(SenderOkay >= 195)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
187
                if(SenderOkay >= 195)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
136
                else PPM_diff[index] = 0;
188
                else PPM_diff[index] = 0;
137
                PPM_in[index] = tmp;
189
                PPM_in[index] = tmp;
138
                         signal = 0;
190
                         signal = 0;
139
                         index++; // next channel
191
                         index++; // next channel
140
                        }
192
                        }
141
                }
193
                }
142
            NewPpmData = 0;  // Null bedeutet: Neue Daten
194
            NewPpmData = 0;  // Null bedeutet: Neue Daten
143
           }
195
           }
144
 NewSBusData = 0;
196
 NewSBusData = 0;
145
}
197
}
146
 
198
 
147
#endif
199
#endif
148
 
200