Subversion Repositories FlightCtrl

Rev

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

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