Subversion Repositories FlightCtrl

Rev

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

Rev 745 Rev 754
1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + only for non-profit use
3
// + only for non-profit use
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
 
7
 
8
#include <avr/io.h>
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
9
#include <avr/interrupt.h>
10
#include <avr/wdt.h>
10
#include <avr/wdt.h>
11
 
11
 
12
#include "eeprom.h"
12
#include "eeprom.h"
13
#include "main.h"
13
#include "main.h"
14
#include "menu.h"
14
#include "menu.h"
15
#include "timer0.h"
15
#include "timer0.h"
16
#include "uart.h"
16
#include "uart.h"
17
#include "fc.h"
17
#include "fc.h"
18
#include "_Settings.h"
18
#include "_Settings.h"
19
#include "rc.h"
19
#include "rc.h"
-
 
20
 
-
 
21
#if defined (__AVR_ATmega644__)
-
 
22
#include "ubx.h"
-
 
23
#endif
-
 
24
 
-
 
25
 
20
 
26
 
21
#define FALSE   0
27
#define FALSE   0
22
#define TRUE    1
28
#define TRUE    1
23
 
29
 
24
//int8_t test __attribute__ ((section (".noinit")));
30
//int8_t test __attribute__ ((section (".noinit")));
25
 
31
 
26
uint8_t DebugGetRequest = 0, DebugDisplayRequest = 0, DebugDataRequest = 0, GetVersionRequest = 0;
32
uint8_t DebugGetRequest = 0, DebugDisplayRequest = 0, DebugDataRequest = 0, GetVersionRequest = 0;
27
 
33
 
28
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
34
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
29
volatile uint8_t rxd_buffer_locked = FALSE;
35
volatile uint8_t rxd_buffer_locked = FALSE;
30
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
36
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
31
volatile uint8_t txd_complete = TRUE;
37
volatile uint8_t txd_complete = TRUE;
32
volatile uint8_t ReceivedBytes = 0;
38
volatile uint8_t ReceivedBytes = 0;
33
 
39
 
34
uint8_t RemotePollDisplayLine = 0;
40
uint8_t RemotePollDisplayLine = 0;
35
uint8_t NurKanalAnforderung = 0;
41
uint8_t NurKanalAnforderung = 0;
36
uint8_t DebugTextAnforderung = 255;
42
uint8_t DebugTextAnforderung = 255;
37
uint8_t PcAccess = 100;
43
uint8_t PcAccess = 100;
38
uint8_t MotorTest[4] = {0,0,0,0};
44
uint8_t MotorTest[4] = {0,0,0,0};
39
uint8_t DubWiseKeys[4] = {0,0,0,0};
45
uint8_t DubWiseKeys[4] = {0,0,0,0};
40
uint8_t MySlaveAddr = 0;
46
uint8_t MySlaveAddr = 0;
41
uint8_t ConfirmFrame;
47
uint8_t ConfirmFrame;
42
 
48
 
43
DebugOut_t              DebugOut;
49
DebugOut_t              DebugOut;
44
ExternControl_t ExternControl;
50
ExternControl_t ExternControl;
45
VersionInfo_t   VersionInfo;
51
VersionInfo_t   VersionInfo;
46
 
52
 
47
const uint8_t ANALOG_TEXT[32][16] =
53
const uint8_t ANALOG_TEXT[32][16] =
48
{
54
{
49
   //1234567890123456
55
   //1234567890123456
50
    "IntegralPitch   ", //0
56
    "IntegralPitch   ", //0
51
    "IntegralRoll    ",
57
    "IntegralRoll    ",
52
    "AccPitch        ",
58
    "AccPitch        ",
53
    "AccRoll         ",
59
    "AccRoll         ",
54
    "GyroYaw         ",
60
    "GyroYaw         ",
55
    "ReadingHight    ", //5
61
    "ReadingHight    ", //5
56
    "AccZ            ",
62
    "AccZ            ",
57
    "Thrust          ",
63
    "Thrust          ",
58
    "CompassHeading  ",
64
    "CompassHeading  ",
59
    "Voltage         ",
65
    "Voltage         ",
60
    "Receiver Level  ", //10
66
    "Receiver Level  ", //10
61
    "Analog11        ",
67
    "Analog11        ",
62
    "GPSDevNorth     ",
68
    "GPSDevNorth     ",
63
    "GPSDevEast      ",
69
    "GPSDevEast      ",
64
    "GPS_Pitch       ",
70
    "GPS_Pitch       ",
65
    "GPS_Roll        ", //15
71
    "GPS_Roll        ", //15
66
    "Acc_Z           ",
72
    "Acc_Z           ",
67
    "MeanAccPitch    ",
73
    "MeanAccPitch    ",
68
    "MeanAccRoll     ",
74
    "MeanAccRoll     ",
69
    "IntegralErrPitch",
75
    "IntegralErrPitch",
70
    "IntegralErrRoll ", //20
76
    "IntegralErrRoll ", //20
71
    "MeanIntPitch    ",
77
    "MeanIntPitch    ",
72
    "MeanIntRoll         ",
78
    "MeanIntRoll         ",
73
    "NeutralPitch    ",
79
    "NeutralPitch    ",
74
    "RollOffset      ",
80
    "RollOffset      ",
75
    "IntRoll*Factor  ", //25
81
    "IntRoll*Factor  ", //25
76
    "ReadingGyroPitch",
82
    "ReadingGyroPitch",
77
    "DirectCorrRoll  ",
83
    "DirectCorrRoll  ",
78
    "ReadingGyroRoll ",
84
    "ReadingGyroRoll ",
79
    "CorrectionRoll  ",
85
    "CorrectionRoll  ",
80
    "I-AttRoll       ", //30
86
    "I-AttRoll       ", //30
81
    "StickRoll       "
87
    "StickRoll       "
82
};
88
};
83
 
89
 
84
 
90
 
85
 
91
 
86
/****************************************************************/
92
/****************************************************************/
87
/*              Initialization of the USART0                    */
93
/*              Initialization of the USART0                    */
88
/****************************************************************/
94
/****************************************************************/
89
void USART0_Init (void)
95
void USART0_Init (void)
90
{
96
{
91
        uint8_t sreg = SREG;
97
        uint8_t sreg = SREG;
92
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
98
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
93
 
99
 
94
        // disable all interrupts before configuration
100
        // disable all interrupts before configuration
95
        cli();
101
        cli();
96
 
102
 
97
        // disable RX-Interrupt
103
        // disable RX-Interrupt
98
        UCSR0B &= ~(1 << RXCIE0);
104
        UCSR0B &= ~(1 << RXCIE0);
99
        // disable TX-Interrupt
105
        // disable TX-Interrupt
100
        UCSR0B &= ~(1 << TXCIE0);
106
        UCSR0B &= ~(1 << TXCIE0);
101
 
107
 
102
        // set direction of RXD0 and TXD0 pins
108
        // set direction of RXD0 and TXD0 pins
103
        // set RXD0 (PD0) as an input pin
109
        // set RXD0 (PD0) as an input pin
104
        PORTD |= (1 << PORTD0);
110
        PORTD |= (1 << PORTD0);
105
        DDRD &= ~(1 << DDD0);
111
        DDRD &= ~(1 << DDD0);
106
        // set TXD0 (PD1) as an output pin
112
        // set TXD0 (PD1) as an output pin
107
        PORTD |= (1 << PORTD1);
113
        PORTD |= (1 << PORTD1);
108
        DDRD |=  (1 << DDD1);
114
        DDRD |=  (1 << DDD1);
109
 
115
 
110
        // USART0 Baud Rate Register
116
        // USART0 Baud Rate Register
111
        // set clock divider
117
        // set clock divider
112
        UBRR0H = (uint8_t)(ubrr >> 8);
118
        UBRR0H = (uint8_t)(ubrr >> 8);
113
        UBRR0L = (uint8_t)ubrr;
119
        UBRR0L = (uint8_t)ubrr;
114
 
120
 
115
        // USART0 Control and Status Register A, B, C
121
        // USART0 Control and Status Register A, B, C
116
 
122
 
117
        // enable double speed operation in
123
        // enable double speed operation in
118
        UCSR0A |= (1 << U2X0);
124
        UCSR0A |= (1 << U2X0);
119
        // enable receiver and transmitter in
125
        // enable receiver and transmitter in
120
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
126
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
121
        // set asynchronous mode
127
        // set asynchronous mode
122
        UCSR0C &= ~(1 << UMSEL01);
128
        UCSR0C &= ~(1 << UMSEL01);
123
        UCSR0C &= ~(1 << UMSEL00);
129
        UCSR0C &= ~(1 << UMSEL00);
124
        // no parity
130
        // no parity
125
        UCSR0C &= ~(1 << UPM01);
131
        UCSR0C &= ~(1 << UPM01);
126
        UCSR0C &= ~(1 << UPM00);
132
        UCSR0C &= ~(1 << UPM00);
127
        // 1 stop bit
133
        // 1 stop bit
128
        UCSR0C &= ~(1 << USBS0);
134
        UCSR0C &= ~(1 << USBS0);
129
        // 8-bit
135
        // 8-bit
130
        UCSR0B &= ~(1 << UCSZ02);
136
        UCSR0B &= ~(1 << UCSZ02);
131
        UCSR0C |=  (1 << UCSZ01);
137
        UCSR0C |=  (1 << UCSZ01);
132
        UCSR0C |=  (1 << UCSZ00);
138
        UCSR0C |=  (1 << UCSZ00);
133
 
139
 
134
                // flush receive buffer
140
                // flush receive buffer
135
        while ( UCSR0A & (1<<RXC0) ) UDR0;
141
        while ( UCSR0A & (1<<RXC0) ) UDR0;
136
 
142
 
137
        // enable interrupts at the end
143
        // enable interrupts at the end
138
        // enable RX-Interrupt
144
        // enable RX-Interrupt
139
        UCSR0B |= (1 << RXCIE0);
145
        UCSR0B |= (1 << RXCIE0);
140
        // enable TX-Interrupt
146
        // enable TX-Interrupt
141
        UCSR0B |= (1 << TXCIE0);
147
        UCSR0B |= (1 << TXCIE0);
142
 
148
 
143
        rxd_buffer_locked = FALSE;
149
        rxd_buffer_locked = FALSE;
144
        // restore global interrupt flags
150
        // restore global interrupt flags
145
    SREG = sreg;
151
    SREG = sreg;
146
}
152
}
147
 
153
 
148
/****************************************************************/
154
/****************************************************************/
149
/*               USART0 transmitter ISR                         */
155
/*               USART0 transmitter ISR                         */
150
/****************************************************************/
156
/****************************************************************/
151
ISR(USART0_TX_vect)
157
ISR(USART0_TX_vect)
152
{
158
{
153
        static uint16_t ptr_txd_buffer = 0;
159
        static uint16_t ptr_txd_buffer = 0;
154
        uint8_t tmp_tx;
160
        uint8_t tmp_tx;
155
        if(!txd_complete) // transmission not completed
161
        if(!txd_complete) // transmission not completed
156
        {
162
        {
157
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
163
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
158
                tmp_tx = txd_buffer[ptr_txd_buffer];
164
                tmp_tx = txd_buffer[ptr_txd_buffer];
159
                // if terminating character or end of txd buffer was reached
165
                // if terminating character or end of txd buffer was reached
160
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
166
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
161
                {
167
                {
162
                        ptr_txd_buffer = 0; // reset txd pointer
168
                        ptr_txd_buffer = 0; // reset txd pointer
163
                        txd_complete = 1; // stop transmission
169
                        txd_complete = 1; // stop transmission
164
                }
170
                }
165
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
171
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
166
        }
172
        }
167
        // transmission completed
173
        // transmission completed
168
        else ptr_txd_buffer = 0;
174
        else ptr_txd_buffer = 0;
169
}
175
}
170
 
176
 
171
/****************************************************************/
177
/****************************************************************/
172
/*               USART0 receiver ISR                            */
178
/*               USART0 receiver ISR                            */
173
/****************************************************************/
179
/****************************************************************/
174
ISR(USART0_RX_vect)
180
ISR(USART0_RX_vect)
175
{
181
{
176
        static uint16_t crc;
182
        static uint16_t crc;
177
        static uint8_t ptr_rxd_buffer = 0;
183
        static uint8_t ptr_rxd_buffer = 0;
178
        uint8_t crc1, crc2;
184
        uint8_t crc1, crc2;
179
        uint8_t c;
185
        uint8_t c;
180
 
186
 
181
        c = UDR0;  // catch the received byte
187
        c = UDR0;  // catch the received byte
-
 
188
 
-
 
189
        // If the ATMEGA644 cpu is used the ublox module should be conneced to rxd of the 1st uart.
-
 
190
        // The ATMEGA644p cpu has a 2nd uart to which the ublox is connected.
-
 
191
        #if defined (__AVR_ATmega644__)
-
 
192
        ubx_parser(c);
182
 
193
        #endif
183
 
194
 
184
        if(rxd_buffer_locked) return; // if txd buffer is locked immediately return
195
        if(rxd_buffer_locked) return; // if txd buffer is locked immediately return
185
 
196
 
186
        // the rxd buffer is unlocked
197
        // the rxd buffer is unlocked
187
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
198
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
188
        {
199
        {
189
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
200
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
190
                crc = c; // init crc
201
                crc = c; // init crc
191
        }
202
        }
192
        #if 0
203
        #if 0
193
        else if (ptr_rxd_buffer == 1) // handle address
204
        else if (ptr_rxd_buffer == 1) // handle address
194
        {
205
        {
195
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
206
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
196
                crc += c; // update crc
207
                crc += c; // update crc
197
        }
208
        }
198
        #endif
209
        #endif
199
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
210
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
200
        {
211
        {
201
                if(c != '\r') // no termination character
212
                if(c != '\r') // no termination character
202
                {
213
                {
203
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
214
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
204
                        crc += c; // update crc
215
                        crc += c; // update crc
205
                }
216
                }
206
                else // termination character was received
217
                else // termination character was received
207
                {
218
                {
208
                        // the last 2 bytes are no subject for checksum calculation
219
                        // the last 2 bytes are no subject for checksum calculation
209
                        // they are the checksum itself
220
                        // they are the checksum itself
210
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
221
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
211
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
222
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
212
                        // calculate checksum from transmitted data
223
                        // calculate checksum from transmitted data
213
                        crc %= 4096;
224
                        crc %= 4096;
214
                        crc1 = '=' + crc / 64;
225
                        crc1 = '=' + crc / 64;
215
                        crc2 = '=' + crc % 64;
226
                        crc2 = '=' + crc % 64;
216
                        // compare checksum to transmitted checksum bytes
227
                        // compare checksum to transmitted checksum bytes
217
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
228
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
218
                        {   // checksum valid
229
                        {   // checksum valid
219
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
230
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
220
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
231
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
221
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
232
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
222
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
233
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
223
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
234
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
224
                        }
235
                        }
225
                        else
236
                        else
226
                        {       // checksum invalid
237
                        {       // checksum invalid
227
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
238
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
228
                        }
239
                        }
229
                        ptr_rxd_buffer = 0; // reset txd buffer
240
                        ptr_rxd_buffer = 0; // reset txd buffer
230
                }
241
                }
231
        } // buffer overrun
242
        } // buffer overrun
232
        else
243
        else
233
        {
244
        {
234
                ptr_rxd_buffer = 0; // reset rxd buffer
245
                ptr_rxd_buffer = 0; // reset rxd buffer
235
                rxd_buffer_locked = FALSE; // unlock rxd buffer
246
                rxd_buffer_locked = FALSE; // unlock rxd buffer
236
        }
247
        }
237
 
248
 
238
}
249
}
239
 
250
 
240
 
251
 
241
// --------------------------------------------------------------------------
252
// --------------------------------------------------------------------------
242
void AddCRC(uint16_t datalen)
253
void AddCRC(uint16_t datalen)
243
{
254
{
244
 uint16_t tmpCRC = 0,i;
255
 uint16_t tmpCRC = 0,i;
245
 for(i = 0; i < datalen;i++)
256
 for(i = 0; i < datalen;i++)
246
  {
257
  {
247
   tmpCRC += txd_buffer[i];
258
   tmpCRC += txd_buffer[i];
248
  }
259
  }
249
   tmpCRC %= 4096;
260
   tmpCRC %= 4096;
250
   txd_buffer[i++] = '=' + tmpCRC / 64;
261
   txd_buffer[i++] = '=' + tmpCRC / 64;
251
   txd_buffer[i++] = '=' + tmpCRC % 64;
262
   txd_buffer[i++] = '=' + tmpCRC % 64;
252
   txd_buffer[i++] = '\r';
263
   txd_buffer[i++] = '\r';
253
  txd_complete = 0;
264
  txd_complete = 0;
254
  UDR0 = txd_buffer[0]; // initiates the transmittion
265
  UDR0 = txd_buffer[0]; // initiates the transmittion
255
}
266
}
256
 
267
 
257
 
268
 
258
 
269
 
259
// --------------------------------------------------------------------------
270
// --------------------------------------------------------------------------
260
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
271
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
261
{
272
{
262
        uint16_t pt = 0;
273
        uint16_t pt = 0;
263
        uint8_t a,b,c;
274
        uint8_t a,b,c;
264
        uint8_t ptr = 0;
275
        uint8_t ptr = 0;
265
 
276
 
266
        txd_buffer[pt++] = '#';         // Start character
277
        txd_buffer[pt++] = '#';         // Start character
267
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
278
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
268
        txd_buffer[pt++] = cmd;         // Command
279
        txd_buffer[pt++] = cmd;         // Command
269
 
280
 
270
        while(len)
281
        while(len)
271
        {
282
        {
272
                if(len) { a = snd[ptr++]; len--;} else a = 0;
283
                if(len) { a = snd[ptr++]; len--;} else a = 0;
273
                if(len) { b = snd[ptr++]; len--;} else b = 0;
284
                if(len) { b = snd[ptr++]; len--;} else b = 0;
274
                if(len) { c = snd[ptr++]; len--;} else c = 0;
285
                if(len) { c = snd[ptr++]; len--;} else c = 0;
275
                txd_buffer[pt++] = '=' + (a >> 2);
286
                txd_buffer[pt++] = '=' + (a >> 2);
276
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
287
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
277
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
288
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
278
                txd_buffer[pt++] = '=' + ( c & 0x3f);
289
                txd_buffer[pt++] = '=' + ( c & 0x3f);
279
        }
290
        }
280
        AddCRC(pt); // add checksum after data block and initates the transmission
291
        AddCRC(pt); // add checksum after data block and initates the transmission
281
}
292
}
282
 
293
 
283
 
294
 
284
// --------------------------------------------------------------------------
295
// --------------------------------------------------------------------------
285
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
296
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
286
{
297
{
287
        uint8_t a,b,c,d;
298
        uint8_t a,b,c,d;
288
        uint8_t ptr = 0;
299
        uint8_t ptr = 0;
289
        uint8_t x,y,z;
300
        uint8_t x,y,z;
290
        while(len)
301
        while(len)
291
        {
302
        {
292
                a = rxd_buffer[ptrIn++] - '=';
303
                a = rxd_buffer[ptrIn++] - '=';
293
                b = rxd_buffer[ptrIn++] - '=';
304
                b = rxd_buffer[ptrIn++] - '=';
294
                c = rxd_buffer[ptrIn++] - '=';
305
                c = rxd_buffer[ptrIn++] - '=';
295
                d = rxd_buffer[ptrIn++] - '=';
306
                d = rxd_buffer[ptrIn++] - '=';
296
                if(ptrIn > max - 2) break;
307
                if(ptrIn > max - 2) break;
297
 
308
 
298
                x = (a << 2) | (b >> 4);
309
                x = (a << 2) | (b >> 4);
299
                y = ((b & 0x0f) << 4) | (c >> 2);
310
                y = ((b & 0x0f) << 4) | (c >> 2);
300
                z = ((c & 0x03) << 6) | d;
311
                z = ((c & 0x03) << 6) | d;
301
 
312
 
302
                if(len--) ptrOut[ptr++] = x; else break;
313
                if(len--) ptrOut[ptr++] = x; else break;
303
                if(len--) ptrOut[ptr++] = y; else break;
314
                if(len--) ptrOut[ptr++] = y; else break;
304
                if(len--) ptrOut[ptr++] = z; else break;
315
                if(len--) ptrOut[ptr++] = z; else break;
305
        }
316
        }
306
}
317
}
307
 
318
 
308
 
319
 
309
// --------------------------------------------------------------------------
320
// --------------------------------------------------------------------------
310
void ProcessRxData(void)
321
void ProcessRxData(void)
311
{
322
{
312
        // if data in the rxd buffer are not locked immediately return
323
        // if data in the rxd buffer are not locked immediately return
313
        if(!rxd_buffer_locked) return;
324
        if(!rxd_buffer_locked) return;
314
 
325
 
315
        uint8_t tmp_char_arr2[2]; // local buffer
326
        uint8_t tmp_char_arr2[2]; // local buffer
316
 
327
 
317
        PcAccess = 255;
328
        PcAccess = 255;
318
        switch(rxd_buffer[2])
329
        switch(rxd_buffer[2])
319
        {
330
        {
320
                case 'a':// Labels of the Analog Deboug outputs
331
                case 'a':// Labels of the Analog Deboug outputs
321
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
332
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
322
                        DebugTextAnforderung = tmp_char_arr2[0];
333
                        DebugTextAnforderung = tmp_char_arr2[0];
323
                        break;
334
                        break;
324
                case 'b': // extern control
335
                case 'b': // extern control
325
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3, ReceivedBytes);
336
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3, ReceivedBytes);
326
                        RemoteButtons |= ExternControl.RemoteButtons;
337
                        RemoteButtons |= ExternControl.RemoteButtons;
327
                        ConfirmFrame = ExternControl.Frame;
338
                        ConfirmFrame = ExternControl.Frame;
328
                        break;
339
                        break;
329
                case 'c': // extern control with debug request
340
                case 'c': // extern control with debug request
330
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,ReceivedBytes);
341
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,ReceivedBytes);
331
                        RemoteButtons |= ExternControl.RemoteButtons;
342
                        RemoteButtons |= ExternControl.RemoteButtons;
332
                        ConfirmFrame = ExternControl.Frame;
343
                        ConfirmFrame = ExternControl.Frame;
333
                        DebugDataRequest = 1;
344
                        DebugDataRequest = 1;
334
                        break;
345
                        break;
335
                case 'h':// x-1 display columns
346
                case 'h':// x-1 display columns
336
                        Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes);
347
                        Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes);
337
                        RemoteButtons |= tmp_char_arr2[0];
348
                        RemoteButtons |= tmp_char_arr2[0];
338
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1;
349
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1;
339
                        else NurKanalAnforderung = 0; // keine Displaydaten
350
                        else NurKanalAnforderung = 0; // keine Displaydaten
340
                        DebugDisplayRequest = 1;
351
                        DebugDisplayRequest = 1;
341
                        break;
352
                        break;
342
                case 't':// motor test
353
                case 't':// motor test
343
                        Decode64((uint8_t *) &MotorTest[0],sizeof(MotorTest),3,ReceivedBytes);
354
                        Decode64((uint8_t *) &MotorTest[0],sizeof(MotorTest),3,ReceivedBytes);
344
                        break;
355
                        break;
345
                case 'k':// keys from DubWise
356
                case 'k':// keys from DubWise
346
                        Decode64((uint8_t *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,ReceivedBytes);
357
                        Decode64((uint8_t *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,ReceivedBytes);
347
                        ConfirmFrame = DubWiseKeys[3];
358
                        ConfirmFrame = DubWiseKeys[3];
348
                        break;
359
                        break;
349
                case 'v': // get version and board release
360
                case 'v': // get version and board release
350
                        GetVersionRequest = 1;
361
                        GetVersionRequest = 1;
351
                        break;
362
                        break;
352
                case 'g':// get debug data
363
                case 'g':// get debug data
353
                        DebugGetRequest = 1;
364
                        DebugGetRequest = 1;
354
                        break;
365
                        break;
355
                case 'q':// get settings
366
                case 'q':// get settings
356
                        Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes);
367
                        Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes);
357
                        if(tmp_char_arr2[0] != 0xff)
368
                        if(tmp_char_arr2[0] != 0xff)
358
                        {
369
                        {
359
                                if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; // limit to 5
370
                                if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; // limit to 5
360
                                // load requested parameter set
371
                                // load requested parameter set
361
                                ParamSet_ReadFromEEProm(tmp_char_arr2[0]);
372
                                ParamSet_ReadFromEEProm(tmp_char_arr2[0]);
362
                                SendOutData('L' + tmp_char_arr2[0] -1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN);
373
                                SendOutData('L' + tmp_char_arr2[0] -1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN);
363
                        }
374
                        }
364
                        else // send active parameter set
375
                        else // send active parameter set
365
                        SendOutData('L' + GetParamByte(PID_ACTIVE_SET)-1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN);
376
                        SendOutData('L' + GetParamByte(PID_ACTIVE_SET)-1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN);
366
 
377
 
367
                        break;
378
                        break;
368
 
379
 
369
                case 'l':
380
                case 'l':
370
                case 'm':
381
                case 'm':
371
                case 'n':
382
                case 'n':
372
                case 'o':
383
                case 'o':
373
                case 'p': // save parameterset
384
                case 'p': // save parameterset
374
                        Decode64((uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN,3,ReceivedBytes);
385
                        Decode64((uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN,3,ReceivedBytes);
375
                        ParamSet_WriteToEEProm(rxd_buffer[2] - 'l' + 1);
386
                        ParamSet_WriteToEEProm(rxd_buffer[2] - 'l' + 1);
376
                        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
387
                        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
377
                        TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L;
388
                        TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L;
378
                        Beep(GetActiveParamSet());
389
                        Beep(GetActiveParamSet());
379
                        break;
390
                        break;
380
 
391
 
381
 
392
 
382
        }
393
        }
383
        // unlock the rxd buffer after processing
394
        // unlock the rxd buffer after processing
384
        rxd_buffer_locked = FALSE;
395
        rxd_buffer_locked = FALSE;
385
}
396
}
386
 
397
 
387
//############################################################################
398
//############################################################################
388
//Routine für die Serielle Ausgabe
399
//Routine für die Serielle Ausgabe
389
int16_t uart_putchar (int8_t c)
400
int16_t uart_putchar (int8_t c)
390
//############################################################################
401
//############################################################################
391
{
402
{
392
        if (c == '\n')
403
        if (c == '\n')
393
                uart_putchar('\r');
404
                uart_putchar('\r');
394
        // wait until previous character was send
405
        // wait until previous character was send
395
        loop_until_bit_is_set(UCSR0A, UDRE0);
406
        loop_until_bit_is_set(UCSR0A, UDRE0);
396
        //Ausgabe des Zeichens
407
        //Ausgabe des Zeichens
397
        UDR0 = c;
408
        UDR0 = c;
398
 
409
 
399
        return (0);
410
        return (0);
400
}
411
}
401
 
412
 
402
 
413
 
403
//---------------------------------------------------------------------------------------------
414
//---------------------------------------------------------------------------------------------
404
void TransmitTxData(void)
415
void TransmitTxData(void)
405
{
416
{
406
 static int16_t Debug_Timer = 0;
417
 static int16_t Debug_Timer = 0;
407
 if(!txd_complete) return;
418
 if(!txd_complete) return;
408
 
419
 
409
   if(DebugGetRequest && txd_complete)        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
420
   if(DebugGetRequest && txd_complete)        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
410
   {
421
   {
411
      SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
422
      SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
412
          DebugGetRequest = 0;
423
          DebugGetRequest = 0;
413
   }
424
   }
414
 
425
 
415
    if((CheckDelay(Debug_Timer) || DebugDataRequest) && txd_complete)
426
    if((CheckDelay(Debug_Timer) || DebugDataRequest) && txd_complete)
416
         {
427
         {
417
          SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
428
          SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
418
          DebugDataRequest = 0;
429
          DebugDataRequest = 0;
419
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);
430
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);
420
         }
431
         }
421
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
432
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
422
     {
433
     {
423
      SendOutData('A',DebugTextAnforderung + '0',(uint8_t *) ANALOG_TEXT[DebugTextAnforderung],16);
434
      SendOutData('A',DebugTextAnforderung + '0',(uint8_t *) ANALOG_TEXT[DebugTextAnforderung],16);
424
      DebugTextAnforderung = 255;
435
      DebugTextAnforderung = 255;
425
         }
436
         }
426
     if(ConfirmFrame && txd_complete)   // Datensatz ohne CRC bestätigen
437
     if(ConfirmFrame && txd_complete)   // Datensatz ohne CRC bestätigen
427
         {
438
         {
428
      txd_buffer[0] = '#';
439
      txd_buffer[0] = '#';
429
      txd_buffer[1] = ConfirmFrame;
440
      txd_buffer[1] = ConfirmFrame;
430
      txd_buffer[2] = '\r';
441
      txd_buffer[2] = '\r';
431
      txd_complete = 0;
442
      txd_complete = 0;
432
      ConfirmFrame = 0;
443
      ConfirmFrame = 0;
433
      UDR0 = txd_buffer[0];
444
      UDR0 = txd_buffer[0];
434
     }
445
     }
435
     if(DebugDisplayRequest && txd_complete)
446
     if(DebugDisplayRequest && txd_complete)
436
         {
447
         {
437
      LCD_PrintMenu();
448
      LCD_PrintMenu();
438
          DebugDisplayRequest = 0;
449
          DebugDisplayRequest = 0;
439
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
450
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
440
      {
451
      {
441
       SendOutData('4',0,(uint8_t *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
452
       SendOutData('4',0,(uint8_t *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
442
       RemotePollDisplayLine = -1;
453
       RemotePollDisplayLine = -1;
443
      }
454
      }
444
      else  SendOutData('0' + RemotePollDisplayLine,0,(uint8_t *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
455
      else  SendOutData('0' + RemotePollDisplayLine,0,(uint8_t *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
445
         }
456
         }
446
    if(GetVersionRequest && txd_complete)
457
    if(GetVersionRequest && txd_complete)
447
     {
458
     {
448
      SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo,sizeof(VersionInfo));
459
      SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo,sizeof(VersionInfo));
449
          GetVersionRequest = 0;
460
          GetVersionRequest = 0;
450
     }
461
     }
451
 
462
 
452
}
463
}
453
 
464
 
454
 
465