Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1207 killagreg 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// This code has been derived from the implementation of Stefan Engelke.
3
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
/*
5
    Copyright (c) 2008 Stefan Engelke <stefan@tinkerer.eu>
1199 killagreg 6
 
1207 killagreg 7
    Permission is hereby granted, free of charge, to any person
8
    obtaining a copy of this software and associated documentation
9
    files (the "Software"), to deal in the Software without
10
    restriction, including without limitation the rights to use, copy,
11
    modify, merge, publish, distribute, sublicense, and/or sell copies
12
    of the Software, and to permit persons to whom the Software is
13
    furnished to do so, subject to the following conditions:
14
 
15
    The above copyright notice and this permission notice shall be
16
    included in all copies or substantial portions of the Software.
17
 
18
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
    DEALINGS IN THE SOFTWARE.
26
 
27
    $Id: rcdsl.c 60 2008-08-21 07:50:48Z taser $
28
 
29
    RCDSL.H and RCDSL.C is an INOFFICIAL implementation of the
30
    communication protocol used by DSL receivers of Act Europe.
31
    The DSL receivers have a serial communication port to connect
32
    two receivers in diversity mode. Each receiver is sending the
33
    received servo signals periodically over this port. This fact
34
    can be used to connect the receiver to the control unit of the
35
    model via UART instead of evaluating the PPM signal.
36
 
37
    If you have any questions, fell free to send me an e-mail.
38
 
39
*/
40
 
41
 
1199 killagreg 42
/*
1206 killagreg 43
Connection of DSL to SV1 of FC:
44
( DSL Pin1 is on side of channel 4 )
1199 killagreg 45
 
1206 killagreg 46
1. GND <--> pin 7 (GND)
47
2. TXD <--> pin 3 (RXD1 Atmega644p)
48
3. RXD <--> pin 4 (TXD1 Atmega644p) optional
49
4. 5V  <--> pin 2 (5V)
1199 killagreg 50
 
1206 killagreg 51
Do not connect the receiver via PPM-Sumsignal output the same time.
1199 killagreg 52
 
1207 killagreg 53
Data are send at every 20 ms @ 38400 Baud 8-N-1
1199 killagreg 54
 
1206 killagreg 55
Data Frame: |0xFF|0xFF|0x1F|FREQALLOC|??|RSSI|VBAT|??|CRC|10|CH0D1|CH0D0|CH1D1|CH1D0|CRC| ...etc
56
 
57
FREQALLOC = 35, 40, 72
58
RSSI = 0.. 255 // Received signal strength indicator
1207 killagreg 59
VBAT = 0...255 // supply voltage (0.0V.. 7.8V)
1206 killagreg 60
 
61
Servo Pair:   |0x1X|CHXD1|CHXD0|CHX+1D1|CHX+1D0|CRC|
62
X is channel index of 1 servo value
63
D1D0 is servo value as u16 in range of 7373 (1ms) to 14745 (2ms)
64
there are 8 channels submitted, i.e 4 servo pairs
65
 
66
 
67
Frame examples with signel received
68
 
69
FFFF 1F23F079A304AD 1036012B1E6F 122AFB2AECB2 142B4D2B4404 1636872B33CE
70
FFFF 1F23F079A304AD 1036002B1F6F 122AFE2AEBB0 142B4B2B4406 1636872B33CE
71
FFFF 1F23F079A304AD 1035FF2B226E 122AFC2AEAB3 142B4E2B4304 1636882B33CD
72
FFFF 1F23F079A304AD 1036022B1E6E 122AFB2AEEB0 142B4A2B4506 1636872B33CE
73
FFFF 1F23F079A304AD 1036022B1E6E 122AFE2AEBB0 142B4B2B4406 1636882B33CD
74
FFFF 1F23F079A304AD 1036012B1E6F 122AFD2AEAB2 142B4E2B4403 1636862B33CF
75
FFFF 1F23F079A304AD 1036032B1D6E 122AFD2AEBB1 142B4C2B4504 1636862B33CF
76
 
77
Frame examples with no signal received
78
 
79
FFFF 1F23F000A30426
80
FFFF 1F23F000A30426
81
FFFF 1F23F000A30426
82
FFFF 1F23F000A30426
83
FFFF 1F23F000A30426
84
FFFF 1F23F000A30426
85
FFFF 1F23F000A30426
86
*/
87
 
1207 killagreg 88
 
89
#include "dsl.h"
90
#include "rc.h"
1200 killagreg 91
uint8_t dsl_RSSI = 0;
92
uint8_t dsl_Battery = 0;
93
uint8_t dsl_Allocation = 0;
94
uint8_t PacketBuffer[6];
1199 killagreg 95
 
1200 killagreg 96
typedef union
97
{
98
        int16_t Servo[2];
99
        uint8_t  byte[4];
1206 killagreg 100
} ChannelPair_t;
1199 killagreg 101
 
1206 killagreg 102
ChannelPair_t ChannelPair;
1199 killagreg 103
 
1200 killagreg 104
 
1199 killagreg 105
// This function is called, when a new servo signal is properly received.
106
// Parameters: servo  - servo number (0-9)
107
//             signal - servo signal between 7373 (1ms) and 14745 (2ms)
108
void dsl_new_signal(uint8_t channel, int16_t signal)
109
{
1200 killagreg 110
    int16_t tmp;
111
    uint8_t index = channel + 1; // mk channels start with 1
1199 killagreg 112
 
113
 
1206 killagreg 114
    // signal from  DSL-receiver is between 7373 (1ms) und 14745 (2ms).
1200 killagreg 115
    signal-= 11059;     // shift to neutral
116
    signal/= 24;        // scale to mk rc resolution
1199 killagreg 117
 
1200 killagreg 118
        // calculate exponential history for signal
119
        tmp = (3 * (PPM_in[index]) + signal) / 4;
120
        if(tmp > signal+1) tmp--; else
121
        if(tmp < signal-1) tmp++;
122
        // calculate signal difference on good signal level
123
        if(RC_Quality >= 195)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for noise reduction
124
        else PPM_diff[index] = 0;
125
        PPM_in[index] = tmp; // update channel value
1199 killagreg 126
 
1206 killagreg 127
    if(index == 4)
128
    {
129
                NewPpmData = 0;
130
                NewRCFrames++;
131
        }
1199 killagreg 132
}
133
 
134
// This function is called within dsl_parser(), when a complete
135
// data packet with valid checksum has been received.
1200 killagreg 136
void dsl_decode_packet(void)
1199 killagreg 137
{
138
    uint8_t  i;
139
 
1206 killagreg 140
        // check for header condition
141
        if((PacketBuffer[0] & 0xF0) == 0x10)
142
        {
143
                if(PacketBuffer[0] == 0x1F) // separate status frame
1200 killagreg 144
                {
1206 killagreg 145
                        dsl_Allocation  = PacketBuffer[1]; // Get frequency allocation
146
                        // ??                   = PacketBuffer[2];
147
                        dsl_RSSI                = PacketBuffer[3]; // Get signal quality
148
                        RC_Quality              = (212 * (uint16_t)dsl_RSSI) / 128; // have to be scaled approx. by a factor of 1.66 to get 200 at full level
149
                        if(RC_Quality > 255) RC_Quality = 255;
150
                        dsl_Battery     = PacketBuffer[4]; // Get voltage of battery supply
151
                        // ??                   = PacketBuffer[5];
152
                        if(dsl_RSSI == 0)
153
                        {
154
                                for (i = 0; i<5; i++)
155
                                {
156
                                        PPM_diff[i] = 0;
157
                                        PPM_in[i] = 0;
158
                                }
159
                        }
1200 killagreg 160
                }
1206 killagreg 161
                else // probably a channel pair
162
                {
163
                        i = PacketBuffer[0] & 0x0F;   // last 4 bits of the header indicates the channel pair
164
                        if(i < 10)// maximum 12 channels
165
                        {
166
                                // big to little endian
167
                                ChannelPair.byte[1] = PacketBuffer[1];
168
                                ChannelPair.byte[0] = PacketBuffer[2];
169
                                ChannelPair.byte[3] = PacketBuffer[3];
170
                                ChannelPair.byte[2] = PacketBuffer[4];
171
                                dsl_new_signal(i,  ChannelPair.Servo[0]);
172
                                dsl_new_signal(i+1,ChannelPair.Servo[1]);
173
                        }
174
                }
175
        } // EOF header condition
1199 killagreg 176
}
177
 
178
 
179
// this function should be called within the UART RX ISR
180
void dsl_parser(uint8_t c)
181
{
1206 killagreg 182
        static uint8_t last_c = 0;
1199 killagreg 183
        static uint8_t crc      = 0;
1200 killagreg 184
        static uint8_t cnt = 0;
185
        static uint8_t packet_len = 0;
1199 killagreg 186
 
1206 killagreg 187
        // check for sync condition
188
        if ((c==0xFF) && (last_c==0xFF))
189
        {
190
                cnt = 0; // reset byte counter
191
                crc = 0; // reset checksum
192
                return;
193
    }
1199 killagreg 194
 
1206 killagreg 195
        if(cnt == 0) // begin of a packet
1199 killagreg 196
        {
1206 killagreg 197
                if(c == 0x1F)   packet_len = 5; // a status packet has 5 bytes + crc
198
                else                    packet_len = 4; // a channel pair packet has 4 bytes + crc
1199 killagreg 199
        }
1206 killagreg 200
        if(cnt > packet_len) // packet complete, crc byte received
201
        {
202
                // calculate checksum
203
                crc = ~crc;
204
                if (crc == 0xFF) crc = 0xFE;
205
                // if crc matches decode the packet
206
        if (c == crc) dsl_decode_packet();
207
        // handle next packet
208
        cnt = 0;
209
        crc = 0;
210
        }
211
        else // collect channel data bytes
212
        {
213
                PacketBuffer[cnt++] = c;
214
                crc += c;
215
        }
216
        // store last byte for sync check
217
        last_c = c;
1200 killagreg 218
}