Subversion Repositories FlightCtrl

Rev

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

Rev 1646 Rev 1775
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist.
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
 
52
 
53
#include <avr/io.h>
53
#include <avr/io.h>
54
#include <avr/interrupt.h>
54
#include <avr/interrupt.h>
55
#include <avr/wdt.h>
55
#include <avr/wdt.h>
56
#include <avr/pgmspace.h>
56
#include <avr/pgmspace.h>
57
#include <stdarg.h>
57
#include <stdarg.h>
58
#include <string.h>
58
#include <string.h>
59
 
59
 
60
#include "eeprom.h"
60
#include "eeprom.h"
61
#include "menu.h"
61
#include "menu.h"
62
#include "timer0.h"
62
#include "timer0.h"
63
#include "uart0.h"
63
#include "uart0.h"
64
#include "attitude.h"
64
#include "attitude.h"
65
#include "rc.h"
65
#include "rc.h"
66
#include "externalControl.h"
66
#include "externalControl.h"
-
 
67
#include "output.h"
67
 
68
 
68
#if defined (USE_MK3MAG)
69
#if defined (USE_MK3MAG)
69
#include "ubx.h"
70
#include "ubx.h"
70
#endif
71
#endif
71
#ifdef USE_MK3MAG
72
#ifdef USE_MK3MAG
72
#include "mk3mag.h"
73
#include "mk3mag.h"
73
#endif
74
#endif
74
 
75
 
75
#define FC_ADDRESS 1
76
#define FC_ADDRESS 1
76
#define NC_ADDRESS 2
77
#define NC_ADDRESS 2
77
#define MK3MAG_ADDRESS 3
78
#define MK3MAG_ADDRESS 3
78
 
79
 
79
#define FALSE   0
80
#define FALSE   0
80
#define TRUE    1
81
#define TRUE    1
81
//int8_t test __attribute__ ((section (".noinit")));
82
//int8_t test __attribute__ ((section (".noinit")));
82
uint8_t Request_VerInfo                 = FALSE;
83
uint8_t request_VerInfo                 = FALSE;
83
uint8_t Request_ExternalControl         = FALSE;
84
uint8_t request_ExternalControl         = FALSE;
84
uint8_t Request_Display                 = FALSE;
85
uint8_t request_Display                 = FALSE;
85
uint8_t Request_Display1                = FALSE;
86
uint8_t request_Display1                = FALSE;
86
uint8_t Request_DebugData               = FALSE;
87
uint8_t request_DebugData               = FALSE;
87
uint8_t Request_Data3D                  = FALSE;
88
uint8_t request_Data3D                  = FALSE;
88
uint8_t Request_DebugLabel              = 255;
89
uint8_t request_DebugLabel              = 255;
89
uint8_t Request_PPMChannels     = FALSE;
90
uint8_t request_PPMChannels             = FALSE;
90
uint8_t Request_MotorTest               = FALSE;
91
uint8_t request_MotorTest               = FALSE;
-
 
92
uint8_t request_variables               = FALSE;
-
 
93
 
91
uint8_t DisplayLine = 0;
94
uint8_t DisplayLine = 0;
92
 
95
 
93
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
96
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
94
volatile uint8_t rxd_buffer_locked = FALSE;
97
volatile uint8_t rxd_buffer_locked = FALSE;
95
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
98
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
96
volatile uint8_t txd_complete = TRUE;
99
volatile uint8_t txd_complete = TRUE;
97
volatile uint8_t ReceivedBytes = 0;
100
volatile uint8_t ReceivedBytes = 0;
98
volatile uint8_t *pRxData = 0;
101
volatile uint8_t *pRxData = 0;
99
volatile uint8_t RxDataLen = 0;
102
volatile uint8_t RxDataLen = 0;
100
 
103
 
101
uint8_t motorTestActive  = 0;
104
uint8_t motorTestActive  = 0;
102
uint8_t motorTest[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
105
uint8_t motorTest[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
103
uint8_t ConfirmFrame;
106
uint8_t ConfirmFrame;
104
 
107
 
105
typedef struct {
108
typedef struct {
106
  int16_t Heading;
109
  int16_t Heading;
107
} __attribute__((packed)) Heading_t;
110
} __attribute__((packed)) Heading_t;
108
 
111
 
109
DebugOut_t              DebugOut;
112
DebugOut_t              DebugOut;
110
Data3D_t                Data3D;
113
Data3D_t                Data3D;
111
UART_VersionInfo_t      UART_VersionInfo;
114
UART_VersionInfo_t      UART_VersionInfo;
112
 
115
 
113
uint16_t DebugData_Timer;
116
uint16_t DebugData_Timer;
114
uint16_t Data3D_Timer;
117
uint16_t Data3D_Timer;
115
uint16_t DebugData_Interval = 500; // in 1ms
118
uint16_t DebugData_Interval = 500; // in 1ms
116
uint16_t Data3D_Interval = 0; // in 1ms
119
uint16_t Data3D_Interval = 0; // in 1ms
117
 
120
 
118
#ifdef USE_MK3MAG
121
#ifdef USE_MK3MAG
119
int16_t Compass_Timer;
122
int16_t Compass_Timer;
120
#endif
123
#endif
121
 
124
 
122
// keep lables in flash to save 512 bytes of sram space
125
// keep lables in flash to save 512 bytes of sram space
123
const prog_uint8_t ANALOG_LABEL[32][16] = {
126
const prog_uint8_t ANALOG_LABEL[32][16] = {
124
    //1234567890123456
127
    //1234567890123456
125
    "AnglePitch      ", //0
128
    "AnglePitch      ", //0
126
    "AngleRoll       ",
129
    "AngleRoll       ",
127
    "AngleYaw        ",
130
    "AngleYaw        ",
128
    "GyroPitch(PID)  ",
131
    "GyroPitch(PID)  ",
129
    "GyroRoll(PID)   ",
132
    "GyroRoll(PID)   ",
130
    "GyroYaw         ", //5
133
    "GyroYaw         ", //5
131
    "GyroPitch(AC)   ",
134
    "GyroPitch(AC)   ",
132
    "GyroRoll(AC)    ",
135
    "GyroRoll(AC)    ",
133
    "GyroYaw(AC)     ",
136
    "GyroYaw(AC)     ",
134
    "AccPitch        ",
137
    "AccPitch (angle)",
135
    "AccRoll         ", //10
138
    "AccRoll (angle) ", //10
136
    "CorrSumPitch    ",
139
    "UBat            ",
137
    "CorrSumRoll     ",
140
    "Pitch Term      ",
138
    "Pressure range  ",
141
    "Roll Term       ",
139
    "Pressure A/D    ",
142
    "Yaw Term        ",
140
    "Pressure Value  ", //15
143
    "Throttle Term   ", //15
141
    "Press. rangewidz",
144
    "0th O Corr pitch",
142
    "Press. init A/D ",
145
    "0th O Corr roll ",
143
    "FACTOR          ",
146
    "DriftCompDelta P",
144
    "throttleTerm    ",
147
    "DriftCompDelta R",
145
    "pitchTerm       ", //20
148
    "ADPitchGyroOffs ", //20
146
    "rollTerm        ",
149
    "ADRollGyroOffs  ",
147
    "yawTerm         ",
150
    "M1              ",
148
    "P Pitch         ",
151
    "M2              ",
149
    "I Pitch         ",
152
    "M3              ",
150
    "P+D Pitch       ", //25
153
    "M4              ", //25
151
    "Pitch acc noise ",
154
    "ControlYaw      ",
152
    "Roll acc noise  ",
155
    "Acc Z           ",
153
    "DynamicPitch    ",
156
    "DriftCompPitch  ",
154
    "DynamicRoll     ",
157
    "DriftCompRoll   ",
155
    "Pitch Noise     ", //30
158
    "Pitch Noise     ", //30
156
    "Roll Noise      "
159
    "Roll Noise      "
157
  };
160
  };
158
 
161
 
159
/****************************************************************/
162
/****************************************************************/
160
/*              Initialization of the USART0                    */
163
/*              Initialization of the USART0                    */
161
/****************************************************************/
164
/****************************************************************/
162
void usart0_Init (void) {
165
void usart0_Init (void) {
163
  uint8_t sreg = SREG;
166
  uint8_t sreg = SREG;
164
  uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
167
  uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
165
 
168
 
166
  // disable all interrupts before configuration
169
  // disable all interrupts before configuration
167
  cli();
170
  cli();
168
 
171
 
169
  // disable RX-Interrupt
172
  // disable RX-Interrupt
170
  UCSR0B &= ~(1 << RXCIE0);
173
  UCSR0B &= ~(1 << RXCIE0);
171
  // disable TX-Interrupt
174
  // disable TX-Interrupt
172
  UCSR0B &= ~(1 << TXCIE0);
175
  UCSR0B &= ~(1 << TXCIE0);
173
 
176
 
174
  // set direction of RXD0 and TXD0 pins
177
  // set direction of RXD0 and TXD0 pins
175
  // set RXD0 (PD0) as an input pin
178
  // set RXD0 (PD0) as an input pin
176
  PORTD |= (1 << PORTD0);
179
  PORTD |= (1 << PORTD0);
177
  DDRD &= ~(1 << DDD0);
180
  DDRD &= ~(1 << DDD0);
178
  // set TXD0 (PD1) as an output pin
181
  // set TXD0 (PD1) as an output pin
179
  PORTD |= (1 << PORTD1);
182
  PORTD |= (1 << PORTD1);
180
  DDRD |=  (1 << DDD1);
183
  DDRD |=  (1 << DDD1);
181
 
184
 
182
  // USART0 Baud Rate Register
185
  // USART0 Baud Rate Register
183
  // set clock divider
186
  // set clock divider
184
  UBRR0H = (uint8_t)(ubrr >> 8);
187
  UBRR0H = (uint8_t)(ubrr >> 8);
185
  UBRR0L = (uint8_t)ubrr;
188
  UBRR0L = (uint8_t)ubrr;
186
 
189
 
187
  // USART0 Control and Status Register A, B, C
190
  // USART0 Control and Status Register A, B, C
188
 
191
 
189
  // enable double speed operation in
192
  // enable double speed operation in
190
  UCSR0A |= (1 << U2X0);
193
  UCSR0A |= (1 << U2X0);
191
  // enable receiver and transmitter in
194
  // enable receiver and transmitter in
192
  UCSR0B = (1 << TXEN0) | (1 << RXEN0);
195
  UCSR0B = (1 << TXEN0) | (1 << RXEN0);
193
  // set asynchronous mode
196
  // set asynchronous mode
194
  UCSR0C &= ~(1 << UMSEL01);
197
  UCSR0C &= ~(1 << UMSEL01);
195
  UCSR0C &= ~(1 << UMSEL00);
198
  UCSR0C &= ~(1 << UMSEL00);
196
  // no parity
199
  // no parity
197
  UCSR0C &= ~(1 << UPM01);
200
  UCSR0C &= ~(1 << UPM01);
198
  UCSR0C &= ~(1 << UPM00);
201
  UCSR0C &= ~(1 << UPM00);
199
  // 1 stop bit
202
  // 1 stop bit
200
  UCSR0C &= ~(1 << USBS0);
203
  UCSR0C &= ~(1 << USBS0);
201
  // 8-bit
204
  // 8-bit
202
  UCSR0B &= ~(1 << UCSZ02);
205
  UCSR0B &= ~(1 << UCSZ02);
203
  UCSR0C |=  (1 << UCSZ01);
206
  UCSR0C |=  (1 << UCSZ01);
204
  UCSR0C |=  (1 << UCSZ00);
207
  UCSR0C |=  (1 << UCSZ00);
205
 
208
 
206
  // flush receive buffer
209
  // flush receive buffer
207
  while ( UCSR0A & (1<<RXC0) ) UDR0;
210
  while ( UCSR0A & (1<<RXC0) ) UDR0;
208
 
211
 
209
  // enable interrupts at the end
212
  // enable interrupts at the end
210
  // enable RX-Interrupt
213
  // enable RX-Interrupt
211
  UCSR0B |= (1 << RXCIE0);
214
  UCSR0B |= (1 << RXCIE0);
212
  // enable TX-Interrupt
215
  // enable TX-Interrupt
213
  UCSR0B |= (1 << TXCIE0);
216
  UCSR0B |= (1 << TXCIE0);
214
 
217
 
215
  // initialize the debug timer
218
  // initialize the debug timer
216
  DebugData_Timer = SetDelay(DebugData_Interval);
219
  DebugData_Timer = SetDelay(DebugData_Interval);
217
 
220
 
218
  // unlock rxd_buffer
221
  // unlock rxd_buffer
219
  rxd_buffer_locked = FALSE;
222
  rxd_buffer_locked = FALSE;
220
  pRxData = 0;
223
  pRxData = 0;
221
  RxDataLen = 0;
224
  RxDataLen = 0;
222
 
225
 
223
  // no bytes to send
226
  // no bytes to send
224
  txd_complete = TRUE;
227
  txd_complete = TRUE;
225
 
228
 
226
#ifdef USE_MK3MAG
229
#ifdef USE_MK3MAG
227
  Compass_Timer = SetDelay(220);
230
  Compass_Timer = SetDelay(220);
228
#endif
231
#endif
229
 
232
 
230
  UART_VersionInfo.SWMajor = VERSION_MAJOR;
233
  UART_VersionInfo.SWMajor = VERSION_MAJOR;
231
  UART_VersionInfo.SWMinor = VERSION_MINOR;
234
  UART_VersionInfo.SWMinor = VERSION_MINOR;
232
  UART_VersionInfo.SWPatch = VERSION_PATCH;
235
  UART_VersionInfo.SWPatch = VERSION_PATCH;
233
  UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
236
  UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
234
  UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
237
  UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
235
 
238
 
236
  // restore global interrupt flags
239
  // restore global interrupt flags
237
  SREG = sreg;
240
  SREG = sreg;
238
}
241
}
239
 
242
 
240
/****************************************************************/
243
/****************************************************************/
241
/* USART0 transmitter ISR                                       */
244
/* USART0 transmitter ISR                                       */
242
/****************************************************************/
245
/****************************************************************/
243
ISR(USART0_TX_vect) {
246
ISR(USART0_TX_vect) {
244
  static uint16_t ptr_txd_buffer = 0;
247
  static uint16_t ptr_txd_buffer = 0;
245
  uint8_t tmp_tx;
248
  uint8_t tmp_tx;
246
  if(!txd_complete) { // transmission not completed
249
  if(!txd_complete) { // transmission not completed
247
    ptr_txd_buffer++;                    // die [0] wurde schon gesendet
250
    ptr_txd_buffer++;                    // die [0] wurde schon gesendet
248
    tmp_tx = txd_buffer[ptr_txd_buffer];
251
    tmp_tx = txd_buffer[ptr_txd_buffer];
249
    // if terminating character or end of txd buffer was reached
252
    // if terminating character or end of txd buffer was reached
250
    if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) {
253
    if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) {
251
      ptr_txd_buffer = 0; // reset txd pointer
254
      ptr_txd_buffer = 0; // reset txd pointer
252
      txd_complete = 1; // stop transmission
255
      txd_complete = 1; // stop transmission
253
    }
256
    }
254
    UDR0 = tmp_tx; // send current byte will trigger this ISR again
257
    UDR0 = tmp_tx; // send current byte will trigger this ISR again
255
  }
258
  }
256
  // transmission completed
259
  // transmission completed
257
  else ptr_txd_buffer = 0;
260
  else ptr_txd_buffer = 0;
258
}
261
}
259
 
262
 
260
/****************************************************************/
263
/****************************************************************/
261
/* USART0 receiver               ISR                            */
264
/* USART0 receiver               ISR                            */
262
/****************************************************************/
265
/****************************************************************/
263
ISR(USART0_RX_vect) {
266
ISR(USART0_RX_vect) {
264
  static uint16_t crc;
267
  static uint16_t crc;
265
  static uint8_t ptr_rxd_buffer = 0;
268
  static uint8_t ptr_rxd_buffer = 0;
266
  uint8_t crc1, crc2;
269
  uint8_t crc1, crc2;
267
  uint8_t c;
270
  uint8_t c;
268
 
271
 
269
  c = UDR0;  // catch the received byte
272
  c = UDR0;  // catch the received byte
270
 
273
 
271
#if (defined (USE_MK3MAG))
274
#if (defined (USE_MK3MAG))
272
  // If the cpu is not an Atmega644P the ublox module should be conneced to rxd of the 1st uart.
275
  // If the cpu is not an Atmega644P the ublox module should be conneced to rxd of the 1st uart.
273
  if(CPUType != ATMEGA644P) ubx_parser(c);
276
  if(CPUType != ATMEGA644P) ubx_parser(c);
274
#endif
277
#endif
275
 
278
 
276
  if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
279
  if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
277
 
280
 
278
  // the rxd buffer is unlocked
281
  // the rxd buffer is unlocked
279
  if((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received
282
  if((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received
280
    rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
283
    rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
281
    crc = c; // init crc
284
    crc = c; // init crc
282
  }
285
  }
283
#if 0
286
#if 0
284
  else if (ptr_rxd_buffer == 1) { // handle address
287
  else if (ptr_rxd_buffer == 1) { // handle address
285
    rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
288
    rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
286
    crc += c; // update crc
289
    crc += c; // update crc
287
  }
290
  }
288
#endif
291
#endif
289
  else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes
292
  else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes
290
    if(c != '\r') { // no termination character
293
    if(c != '\r') { // no termination character
291
      rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
294
      rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
292
      crc += c; // update crc
295
      crc += c; // update crc
293
    } else { // termination character was received
296
    } else { // termination character was received
294
      // the last 2 bytes are no subject for checksum calculation
297
      // the last 2 bytes are no subject for checksum calculation
295
      // they are the checksum itself
298
      // they are the checksum itself
296
      crc -= rxd_buffer[ptr_rxd_buffer-2];
299
      crc -= rxd_buffer[ptr_rxd_buffer-2];
297
      crc -= rxd_buffer[ptr_rxd_buffer-1];
300
      crc -= rxd_buffer[ptr_rxd_buffer-1];
298
      // calculate checksum from transmitted data
301
      // calculate checksum from transmitted data
299
      crc %= 4096;
302
      crc %= 4096;
300
      crc1 = '=' + crc / 64;
303
      crc1 = '=' + crc / 64;
301
      crc2 = '=' + crc % 64;
304
      crc2 = '=' + crc % 64;
302
      // compare checksum to transmitted checksum bytes
305
      // compare checksum to transmitted checksum bytes
303
      if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) {
306
      if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) {
304
        // checksum valid
307
        // checksum valid
305
        rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
308
        rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
306
        ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
309
        ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
307
        rxd_buffer_locked = TRUE;          // lock the rxd buffer
310
        rxd_buffer_locked = TRUE;          // lock the rxd buffer
308
        // if 2nd byte is an 'R' enable watchdog that will result in an reset
311
        // if 2nd byte is an 'R' enable watchdog that will result in an reset
309
        if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
312
        if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
310
      } else {  // checksum invalid
313
      } else {  // checksum invalid
311
        rxd_buffer_locked = FALSE; // unlock rxd buffer
314
        rxd_buffer_locked = FALSE; // unlock rxd buffer
312
      }
315
      }
313
      ptr_rxd_buffer = 0; // reset rxd buffer pointer
316
      ptr_rxd_buffer = 0; // reset rxd buffer pointer
314
    }
317
    }
315
  } else { // rxd buffer overrun
318
  } else { // rxd buffer overrun
316
    ptr_rxd_buffer = 0; // reset rxd buffer
319
    ptr_rxd_buffer = 0; // reset rxd buffer
317
    rxd_buffer_locked = FALSE; // unlock rxd buffer
320
    rxd_buffer_locked = FALSE; // unlock rxd buffer
318
  }
321
  }
319
}
322
}
320
 
323
 
321
// --------------------------------------------------------------------------
324
// --------------------------------------------------------------------------
322
void AddCRC(uint16_t datalen) {
325
void AddCRC(uint16_t datalen) {
323
  uint16_t tmpCRC = 0, i;
326
  uint16_t tmpCRC = 0, i;
324
  for(i = 0; i < datalen; i++) {
327
  for(i = 0; i < datalen; i++) {
325
    tmpCRC += txd_buffer[i];
328
    tmpCRC += txd_buffer[i];
326
  }
329
  }
327
  tmpCRC %= 4096;
330
  tmpCRC %= 4096;
328
  txd_buffer[i++] = '=' + tmpCRC / 64;
331
  txd_buffer[i++] = '=' + tmpCRC / 64;
329
  txd_buffer[i++] = '=' + tmpCRC % 64;
332
  txd_buffer[i++] = '=' + tmpCRC % 64;
330
  txd_buffer[i++] = '\r';
333
  txd_buffer[i++] = '\r';
331
  txd_complete = FALSE;
334
  txd_complete = FALSE;
332
  UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
335
  UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
333
}
336
}
334
 
337
 
335
// --------------------------------------------------------------------------
338
// --------------------------------------------------------------------------
-
 
339
// application example:
-
 
340
// SendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16);
-
 
341
/*
-
 
342
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
-
 
343
  va_list ap;
-
 
344
  uint16_t txd_bufferIndex = 0;
-
 
345
  uint8_t *currentBuffer;
-
 
346
  uint8_t currentBufferIndex;
-
 
347
  uint16_t lengthOfCurrentBuffer;
-
 
348
  uint8_t shift = 0;
-
 
349
 
-
 
350
  txd_buffer[txd_bufferIndex++] = '#';                  // Start character
-
 
351
  txd_buffer[txd_bufferIndex++] = 'a' + addr;           // Address (a=0; b=1,...)
-
 
352
  txd_buffer[txd_bufferIndex++] = cmd;                  // Command
-
 
353
 
-
 
354
  va_start(ap, numofbuffers);
-
 
355
 
-
 
356
  while(numofbuffers) {
-
 
357
    currentBuffer = va_arg(ap, uint8_t*);
-
 
358
    lengthOfCurrentBuffer = va_arg(ap, int);
-
 
359
    currentBufferIndex = 0;
-
 
360
    // Encode data: 3 bytes of data are encoded into 4 bytes,
-
 
361
    // where the 2 most significant bits are both 0.
-
 
362
    while(currentBufferIndex != lengthOfCurrentBuffer) {
-
 
363
      if (!shift) txd_buffer[txd_bufferIndex] = 0;
-
 
364
      txd_buffer[txd_bufferIndex]  |= currentBuffer[currentBufferIndex] >> (shift + 2);
-
 
365
      txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111;
-
 
366
      shift += 2;
-
 
367
      if (shift == 6) { shift=0; txd_bufferIndex++; }
-
 
368
      currentBufferIndex++;
-
 
369
    }
-
 
370
  }
-
 
371
  // If the number of data bytes was not divisible by 3, stuff
-
 
372
  //  with 0 pseudodata  until length is again divisible by 3.
-
 
373
  if (shift == 2) {
-
 
374
    // We need to stuff with zero bytes at the end.
-
 
375
    txd_buffer[txd_bufferIndex]  &= 0b00110000;
-
 
376
    txd_buffer[++txd_bufferIndex] = 0;
-
 
377
    shift = 4;
-
 
378
  }
-
 
379
  if (shift == 4) {
-
 
380
    // We need to stuff with zero bytes at the end.
-
 
381
    txd_buffer[txd_bufferIndex++] &= 0b00111100;
-
 
382
    txd_buffer[txd_bufferIndex]    = 0;
-
 
383
  }
-
 
384
  va_end(ap);
-
 
385
  AddCRC(pt); // add checksum after data block and initates the transmission
-
 
386
}
-
 
387
*/
-
 
388
 
336
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
389
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
337
  va_list ap;
390
  va_list ap;
338
  uint16_t pt = 0;
391
  uint16_t pt = 0;
339
  uint8_t a,b,c;
392
  uint8_t a,b,c;
340
  uint8_t ptr = 0;
393
  uint8_t ptr = 0;
341
 
394
 
342
  uint8_t *pdata = 0;
395
  uint8_t *pdata = 0;
343
  int len = 0;
396
  int len = 0;
344
 
397
 
345
  txd_buffer[pt++] = '#';                       // Start character
398
  txd_buffer[pt++] = '#';                       // Start character
346
  txd_buffer[pt++] = 'a' + addr;        // Address (a=0; b=1,...)
399
  txd_buffer[pt++] = 'a' + addr;        // Address (a=0; b=1,...)
347
  txd_buffer[pt++] = cmd;                       // Command
400
  txd_buffer[pt++] = cmd;                       // Command
348
 
401
 
349
  va_start(ap, numofbuffers);
402
  va_start(ap, numofbuffers);
350
 
403
 
351
  if(numofbuffers) {
404
  if(numofbuffers) {
352
    pdata = va_arg(ap, uint8_t*);
405
    pdata = va_arg(ap, uint8_t*);
353
    len = va_arg(ap, int);
406
    len = va_arg(ap, int);
354
    ptr = 0;
407
    ptr = 0;
355
    numofbuffers--;
408
    numofbuffers--;
356
  }
409
  }
357
 
410
 
358
  while(len){
411
  while(len){
359
    if(len) {
412
    if(len) {
360
      a = pdata[ptr++];
413
      a = pdata[ptr++];
361
      len--;
414
      len--;
362
      if((!len) && numofbuffers) {
415
      if((!len) && numofbuffers) {
363
        pdata = va_arg(ap, uint8_t*);
416
        pdata = va_arg(ap, uint8_t*);
364
        len = va_arg(ap, int);
417
        len = va_arg(ap, int);
365
        ptr = 0;
418
        ptr = 0;
366
        numofbuffers--;
419
        numofbuffers--;
367
      }
420
      }
368
    }
421
    }
369
    else a = 0;
422
    else a = 0;
370
    if(len) {
423
    if(len) {
371
      b = pdata[ptr++];
424
      b = pdata[ptr++];
372
      len--;
425
      len--;
373
      if((!len) && numofbuffers) {
426
      if((!len) && numofbuffers) {
374
        pdata = va_arg(ap, uint8_t*);
427
        pdata = va_arg(ap, uint8_t*);
375
        len = va_arg(ap, int);
428
        len = va_arg(ap, int);
376
        ptr = 0;
429
        ptr = 0;
377
        numofbuffers--;
430
        numofbuffers--;
378
      }
431
      }
379
    } else b = 0;
432
    } else b = 0;
380
    if(len) {
433
    if(len) {
381
      c = pdata[ptr++];
434
      c = pdata[ptr++];
382
      len--;
435
      len--;
383
      if((!len) && numofbuffers) {
436
      if((!len) && numofbuffers) {
384
        pdata = va_arg(ap, uint8_t*);
437
        pdata = va_arg(ap, uint8_t*);
385
        len = va_arg(ap, int);
438
        len = va_arg(ap, int);
386
        ptr = 0;
439
        ptr = 0;
387
        numofbuffers--;
440
        numofbuffers--;
388
      }
441
      }
389
    }
442
    }
390
    else c = 0;
443
    else c = 0;
391
    txd_buffer[pt++] = '=' + (a >> 2);
444
    txd_buffer[pt++] = '=' + (a >> 2);
392
    txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
445
    txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
393
    txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
446
    txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
394
    txd_buffer[pt++] = '=' + ( c & 0x3f);
447
    txd_buffer[pt++] = '=' + ( c & 0x3f);
395
  }
448
  }
396
  va_end(ap);
449
  va_end(ap);
397
  AddCRC(pt); // add checksum after data block and initates the transmission
450
  AddCRC(pt); // add checksum after data block and initates the transmission
398
}
451
}
399
 
452
 
400
// --------------------------------------------------------------------------
453
// --------------------------------------------------------------------------
401
void Decode64(void) {
454
void Decode64(void) {
402
  uint8_t a,b,c,d;
455
  uint8_t a,b,c,d;
403
  uint8_t x,y,z;
456
  uint8_t x,y,z;
404
  uint8_t ptrIn = 3;
457
  uint8_t ptrIn = 3;
405
  uint8_t ptrOut = 3;
458
  uint8_t ptrOut = 3;
406
  uint8_t len = ReceivedBytes - 6;
459
  uint8_t len = ReceivedBytes - 6;
407
 
460
 
408
  while(len) {
461
  while(len) {
409
    a = rxd_buffer[ptrIn++] - '=';
462
    a = rxd_buffer[ptrIn++] - '=';
410
    b = rxd_buffer[ptrIn++] - '=';
463
    b = rxd_buffer[ptrIn++] - '=';
411
    c = rxd_buffer[ptrIn++] - '=';
464
    c = rxd_buffer[ptrIn++] - '=';
412
    d = rxd_buffer[ptrIn++] - '=';
465
    d = rxd_buffer[ptrIn++] - '=';
413
    //if(ptrIn > ReceivedBytes - 3) break;
466
    //if(ptrIn > ReceivedBytes - 3) break;
414
   
467
   
415
    x = (a << 2) | (b >> 4);
468
    x = (a << 2) | (b >> 4);
416
    y = ((b & 0x0f) << 4) | (c >> 2);
469
    y = ((b & 0x0f) << 4) | (c >> 2);
417
    z = ((c & 0x03) << 6) | d;
470
    z = ((c & 0x03) << 6) | d;
418
   
471
   
419
    if(len--) rxd_buffer[ptrOut++] = x; else break;
472
    if(len--) rxd_buffer[ptrOut++] = x; else break;
420
    if(len--) rxd_buffer[ptrOut++] = y; else break;
473
    if(len--) rxd_buffer[ptrOut++] = y; else break;
421
    if(len--) rxd_buffer[ptrOut++] = z; else break;
474
    if(len--) rxd_buffer[ptrOut++] = z; else break;
422
  }
475
  }
423
  pRxData = &rxd_buffer[3];
476
  pRxData = &rxd_buffer[3];
424
  RxDataLen = ptrOut - 3;
477
  RxDataLen = ptrOut - 3;
425
}
478
}
426
 
479
 
427
// --------------------------------------------------------------------------
480
// --------------------------------------------------------------------------
428
void usart0_ProcessRxData(void) {
481
void usart0_ProcessRxData(void) {
-
 
482
  // We control the motorTestActive var from here: Count it down.
-
 
483
  if (motorTestActive) motorTestActive--;
429
  // if data in the rxd buffer are not locked immediately return
484
  // if data in the rxd buffer are not locked immediately return
430
  if(!rxd_buffer_locked) return;
485
  if(!rxd_buffer_locked) return;
431
 
-
 
432
  uint8_t tempchar1, tempchar2;
486
  uint8_t tempchar1, tempchar2;
433
 
-
 
434
  Decode64(); // decode data block in rxd_buffer
487
  Decode64(); // decode data block in rxd_buffer
435
 
488
 
436
  switch(rxd_buffer[1] - 'a') {
489
  switch(rxd_buffer[1] - 'a') {
-
 
490
 
437
  case FC_ADDRESS:
491
  case FC_ADDRESS:
438
    switch(rxd_buffer[2]) {
492
    switch(rxd_buffer[2]) {
439
#ifdef USE_MK3MAG
493
#ifdef USE_MK3MAG
440
    case 'K':// compass value
494
    case 'K':// compass value
441
      CompassHeading = ((Heading_t *)pRxData)->Heading;
495
      compassHeading = ((Heading_t *)pRxData)->Heading;
442
      CompassOffCourse = ((540 + CompassHeading - CompassCourse) % 360) - 180;
496
      compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
443
      break;
497
      break;
444
#endif
498
#endif
445
     
-
 
446
    case 't':// motor test
499
    case 't': // motor test
447
      if(RxDataLen > 20) //
500
      if(RxDataLen > 20) {
448
        {
-
 
449
          memcpy(&motorTest[0], (uint8_t*)pRxData, sizeof(motorTest));
501
        memcpy(&motorTest[0], (uint8_t*)pRxData, sizeof(motorTest));
450
        }
-
 
451
      else
502
      } else {
452
        {
-
 
453
          memcpy(&motorTest[0], (uint8_t*)pRxData, 4);
503
        memcpy(&motorTest[0], (uint8_t*)pRxData, 4);
454
        }
504
      }
455
      //Request_MotorTest = TRUE;
-
 
456
      motorTestActive = 255;
505
      motorTestActive = 255;
457
      externalControlActive = 255;
506
      externalControlActive = 255;
458
      break;
507
      break;
459
     
508
     
460
    case 'n':// "Get Mixer Table
509
    case 'n':// "Get Mixer Table
461
      while(!txd_complete); // wait for previous frame to be sent
510
      while(!txd_complete); // wait for previous frame to be sent
462
      SendOutData('N', FC_ADDRESS, 1, (uint8_t *) &Mixer, sizeof(Mixer));
511
      SendOutData('N', FC_ADDRESS, 1, (uint8_t *) &Mixer, sizeof(Mixer));
463
      break;
512
      break;
464
 
513
 
465
    case 'm':// "Set Mixer Table
514
    case 'm':// "Set Mixer Table
466
      if(pRxData[0] == EEMIXER_REVISION)
515
      if(pRxData[0] == EEMIXER_REVISION) {
467
        {
-
 
468
          memcpy(&Mixer, (uint8_t*)pRxData, sizeof(Mixer));
516
        memcpy(&Mixer, (uint8_t*)pRxData, sizeof(Mixer));
469
          MixerTable_WriteToEEProm();
517
        MixerTable_WriteToEEProm();
470
          while(!txd_complete); // wait for previous frame to be sent
518
        while(!txd_complete); // wait for previous frame to be sent
471
          tempchar1 = 1;
519
        tempchar1 = 1;
472
        }
-
 
473
      else
520
      } else {
474
        {
-
 
475
          tempchar1 = 0;
521
        tempchar1 = 0;
476
        }
522
      }
477
      SendOutData('M', FC_ADDRESS,  1, &tempchar1, 1);
523
      SendOutData('M', FC_ADDRESS,  1, &tempchar1, 1);
478
      break;
524
      break;
479
 
525
 
480
    case 'p': // get PPM channels
526
    case 'p': // get PPM channels
481
      Request_PPMChannels = TRUE;
527
      request_PPMChannels = TRUE;
482
      break;
528
      break;
483
 
529
 
484
    case 'q':// request settings
530
    case 'q':// request settings
485
      if(pRxData[0] == 0xFF)
531
      if(pRxData[0] == 0xFF) {
486
        {
-
 
487
          pRxData[0] = GetParamByte(PID_ACTIVE_SET);
532
        pRxData[0] = GetParamByte(PID_ACTIVE_SET);
488
        }
533
      }
489
      // limit settings range
534
      // limit settings range
490
      if(pRxData[0] < 1) pRxData[0] = 1; // limit to 1
535
      if(pRxData[0] < 1) pRxData[0] = 1; // limit to 1
491
      else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5
536
      else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5
492
      // load requested parameter set
537
      // load requested parameter set
493
      ParamSet_ReadFromEEProm(pRxData[0]);
538
      ParamSet_ReadFromEEProm(pRxData[0]);
494
      tempchar1 = pRxData[0];
539
      tempchar1 = pRxData[0];
495
      tempchar2 = EEPARAM_REVISION;
540
      tempchar2 = EEPARAM_REVISION;
496
      while(!txd_complete); // wait for previous frame to be sent
541
      while(!txd_complete); // wait for previous frame to be sent
497
      SendOutData('Q', FC_ADDRESS,3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (uint8_t *) &staticParams, sizeof(staticParams));
542
      SendOutData('Q', FC_ADDRESS,3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (uint8_t *) &staticParams, sizeof(staticParams));
498
      break;
543
      break;
499
 
544
 
500
    case 's': // save settings
545
    case 's': // save settings
501
      if(!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors ar off
546
      if(!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors ar off
502
        {
547
        {
503
          if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range and version of settings
548
          if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range and version of settings
504
            {
549
            {
505
              memcpy(&staticParams, (uint8_t*)&pRxData[2], sizeof(staticParams));
550
              memcpy(&staticParams, (uint8_t*)&pRxData[2], sizeof(staticParams));
506
              ParamSet_WriteToEEProm(pRxData[0]);
551
              ParamSet_WriteToEEProm(pRxData[0]);
507
              /*
552
              /*
508
                TODO: Remove this encapsulation breach
553
                TODO: Remove this encapsulation breach
509
                turnOver180Pitch = (int32_t) staticParams.AngleTurnOverPitch * 2500L;
554
                turnOver180Pitch = (int32_t) staticParams.AngleTurnOverPitch * 2500L;
510
                turnOver180Roll = (int32_t) staticParams.AngleTurnOverRoll * 2500L;
555
                turnOver180Roll = (int32_t) staticParams.AngleTurnOverRoll * 2500L;
511
              */
556
              */
512
              tempchar1 = getActiveParamSet();
557
              tempchar1 = getActiveParamSet();
513
              beepNumber(tempchar1);
558
              beepNumber(tempchar1);
514
            }
559
            }
515
          else
560
          else
516
            {
561
            {
517
              tempchar1 = 0;    //indicate bad data
562
              tempchar1 = 0;    //indicate bad data
518
            }
563
            }
519
          while(!txd_complete); // wait for previous frame to be sent
564
          while(!txd_complete); // wait for previous frame to be sent
520
          SendOutData('S', FC_ADDRESS,1, &tempchar1, sizeof(tempchar1));
565
          SendOutData('S', FC_ADDRESS,1, &tempchar1, sizeof(tempchar1));
521
        }
566
        }
522
      break;
567
      break;
523
 
568
 
524
    default:
569
    default:
525
      //unsupported command received
570
      //unsupported command received
526
      break;
571
      break;
527
    } // case FC_ADDRESS:
572
    } // case FC_ADDRESS:
528
 
573
 
529
  default: // any Slave Address
574
  default: // any Slave Address
530
 
-
 
531
    switch(rxd_buffer[2])
575
    switch(rxd_buffer[2]) {
532
      {
-
 
533
      case 'a':// request for labels of the analog debug outputs
576
      case 'a':// request for labels of the analog debug outputs
534
        Request_DebugLabel = pRxData[0];
577
        request_DebugLabel = pRxData[0];
535
        if(Request_DebugLabel > 31) Request_DebugLabel = 31;
578
        if(request_DebugLabel > 31) request_DebugLabel = 31;
536
        externalControlActive = 255;
579
        externalControlActive = 255;
537
        break;
580
        break;
538
 
581
 
539
      case 'b': // submit extern control
582
      case 'b': // submit extern control
540
        memcpy(&externalControl, (uint8_t*)pRxData, sizeof(externalControl));
583
        memcpy(&externalControl, (uint8_t*)pRxData, sizeof(externalControl));
541
        ConfirmFrame = externalControl.frame;
584
        ConfirmFrame = externalControl.frame;
542
        externalControlActive = 255;
585
        externalControlActive = 255;
543
        break;
586
        break;
544
 
587
 
545
      case 'h':// request for display columns
588
      case 'h':// request for display columns
546
        externalControlActive = 255;
589
        externalControlActive = 255;
547
        RemoteKeys |= pRxData[0];
590
        RemoteKeys |= pRxData[0];
548
        if(RemoteKeys) DisplayLine = 0;
591
        if(RemoteKeys) DisplayLine = 0;
549
        Request_Display = TRUE;
592
        request_Display = TRUE;
550
        break;
593
        break;
551
 
594
 
552
      case 'l':// request for display columns
595
      case 'l':// request for display columns
553
        externalControlActive = 255;
596
        externalControlActive = 255;
554
        MenuItem = pRxData[0];
597
        MenuItem = pRxData[0];
555
        Request_Display1 = TRUE;
598
        request_Display1 = TRUE;
556
        break;
599
        break;
557
 
600
 
558
      case 'v': // request for version and board release
601
      case 'v': // request for version and board release
559
        Request_VerInfo = TRUE;
602
        request_VerInfo = TRUE;
560
        break;
603
        break;
-
 
604
 
-
 
605
    case 'x':
-
 
606
      request_variables = TRUE;
-
 
607
      break;
561
 
608
 
562
      case 'g':// get external control data
609
      case 'g':// get external control data
563
        Request_ExternalControl = TRUE;
610
        request_ExternalControl = TRUE;
564
        break;
611
        break;
565
 
612
 
566
      case 'd': // request for the debug data
613
      case 'd': // request for the debug data
567
        DebugData_Interval = (uint16_t) pRxData[0] * 10;
614
        DebugData_Interval = (uint16_t) pRxData[0] * 10;
568
        if(DebugData_Interval > 0) Request_DebugData = TRUE;
615
        if(DebugData_Interval > 0) request_DebugData = TRUE;
569
        break;
616
        break;
570
 
617
 
571
      case 'c': // request for the 3D data
618
      case 'c': // request for the 3D data
572
        Data3D_Interval = (uint16_t) pRxData[0] * 10;
619
        Data3D_Interval = (uint16_t) pRxData[0] * 10;
573
        if(Data3D_Interval > 0) Request_Data3D = TRUE;
620
        if(Data3D_Interval > 0) request_Data3D = TRUE;
574
        break;
621
        break;
575
 
622
 
576
      default:
623
      default:
577
        //unsupported command received
624
        //unsupported command received
578
        break;
625
        break;
579
      }
626
      }
580
    break; // default:
627
    break; // default:
581
  }
628
  }
582
  // unlock the rxd buffer after processing
629
  // unlock the rxd buffer after processing
583
  pRxData = 0;
630
  pRxData = 0;
584
  RxDataLen = 0;
631
  RxDataLen = 0;
585
  rxd_buffer_locked = FALSE;
632
  rxd_buffer_locked = FALSE;
586
}
633
}
587
 
634
 
588
/************************************************************************/
635
/************************************************************************/
589
/* Routine für die Serielle Ausgabe                                     */
636
/* Routine für die Serielle Ausgabe                                     */
590
/************************************************************************/
637
/************************************************************************/
591
int16_t uart_putchar (int8_t c) {
638
int16_t uart_putchar (int8_t c) {
592
  if (c == '\n')
639
  if (c == '\n')
593
    uart_putchar('\r');
640
    uart_putchar('\r');
594
  // wait until previous character was send
641
  // wait until previous character was send
595
  loop_until_bit_is_set(UCSR0A, UDRE0);
642
  loop_until_bit_is_set(UCSR0A, UDRE0);
596
  // send character
643
  // send character
597
  UDR0 = c;
644
  UDR0 = c;
598
  return (0);
645
  return (0);
599
}
646
}
600
 
647
 
601
//---------------------------------------------------------------------------------------------
648
//---------------------------------------------------------------------------------------------
602
void usart0_TransmitTxData(void) {
649
void usart0_TransmitTxData(void) {
603
  if(!txd_complete) return;
650
  if(!txd_complete) return;
604
 
651
 
605
  if(Request_VerInfo && txd_complete) {
652
  if(request_VerInfo && txd_complete) {
606
    SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
653
    SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
607
    Request_VerInfo = FALSE;
654
    request_VerInfo = FALSE;
608
  }
655
  }
609
 
656
 
610
  if(Request_Display && txd_complete) {
657
  if(request_Display && txd_complete) {
611
    LCD_PrintMenu();
658
    LCD_PrintMenu();
612
    SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20);
659
    SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20);
613
    DisplayLine++;
660
    DisplayLine++;
614
    if(DisplayLine >= 4) DisplayLine = 0;
661
    if(DisplayLine >= 4) DisplayLine = 0;
615
    Request_Display = FALSE;
662
    request_Display = FALSE;
616
  }
663
  }
617
 
664
 
618
  if(Request_Display1 && txd_complete) {
665
  if(request_Display1 && txd_complete) {
619
    LCD_PrintMenu();
666
    LCD_PrintMenu();
620
    SendOutData('L', FC_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff));
667
    SendOutData('L', FC_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff));
621
    Request_Display1 = FALSE;
668
    request_Display1 = FALSE;
622
  }
669
  }
623
 
670
 
624
  if(Request_DebugLabel != 0xFF) { // Texte für die Analogdaten
671
  if(request_DebugLabel != 0xFF) { // Texte für die Analogdaten
625
    uint8_t label[16]; // local sram buffer
672
    uint8_t label[16]; // local sram buffer
626
    memcpy_P(label, ANALOG_LABEL[Request_DebugLabel], 16); // read lable from flash to sram buffer
673
    memcpy_P(label, ANALOG_LABEL[request_DebugLabel], 16); // read lable from flash to sram buffer
627
    SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), label, 16);
674
    SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &request_DebugLabel, sizeof(request_DebugLabel), label, 16);
628
    Request_DebugLabel = 0xFF;
675
    request_DebugLabel = 0xFF;
629
  }
676
  }
630
 
677
 
631
  if(ConfirmFrame && txd_complete) {   // Datensatz ohne CRC bestätigen
678
  if(ConfirmFrame && txd_complete) {   // Datensatz ohne CRC bestätigen
632
    SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
679
    SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
633
    ConfirmFrame = 0;
680
    ConfirmFrame = 0;
634
  }
681
  }
635
 
682
 
636
  if(((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete) {
683
  if(((DebugData_Interval && CheckDelay(DebugData_Timer)) || request_DebugData) && txd_complete) {
637
    SendOutData('D', FC_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut));
684
    SendOutData('D', FC_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut));
638
    DebugData_Timer = SetDelay(DebugData_Interval);
685
    DebugData_Timer = SetDelay(DebugData_Interval);
639
    Request_DebugData = FALSE;
686
    request_DebugData = FALSE;
640
  }
687
  }
641
 
688
 
642
  if( ((Data3D_Interval && CheckDelay(Data3D_Timer)) || Request_Data3D) && txd_complete) {
689
  if( ((Data3D_Interval && CheckDelay(Data3D_Timer)) || request_Data3D) && txd_complete) {
643
    SendOutData('C', FC_ADDRESS, 1,(uint8_t *) &Data3D, sizeof(Data3D));
690
    SendOutData('C', FC_ADDRESS, 1,(uint8_t *) &Data3D, sizeof(Data3D));
644
    Data3D.AngleNick = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
691
    Data3D.AngleNick = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
645
    Data3D.AngleRoll = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
692
    Data3D.AngleRoll = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
646
    Data3D.Heading   = (int16_t)((10 * yawGyroHeading)   / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1°
693
    Data3D.Heading   = (int16_t)((10 * yawGyroHeading)   / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1°
647
    Data3D_Timer = SetDelay(Data3D_Interval);
694
    Data3D_Timer = SetDelay(Data3D_Interval);
648
    Request_Data3D = FALSE;
695
    request_Data3D = FALSE;
649
  }
696
  }
650
 
697
 
651
  if(Request_ExternalControl && txd_complete) {
698
  if(request_ExternalControl && txd_complete) {
652
    SendOutData('G', FC_ADDRESS, 1,(uint8_t *) &externalControl, sizeof(externalControl));
699
    SendOutData('G', FC_ADDRESS, 1,(uint8_t *) &externalControl, sizeof(externalControl));
653
    Request_ExternalControl = FALSE;
700
    request_ExternalControl = FALSE;
654
  }
701
  }
655
 
702
 
656
#ifdef USE_MK3MAG
703
#ifdef USE_MK3MAG
657
  if((CheckDelay(Compass_Timer)) && txd_complete) {
704
  if((CheckDelay(Compass_Timer)) && txd_complete) {
658
    ToMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCH_ROLL);  // approx. 0.1 deg
705
    ToMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL);  // approx. 0.1 deg
659
    ToMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCH_ROLL);  // approx. 0.1 deg
706
    ToMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL);  // approx. 0.1 deg
660
    ToMk3Mag.UserParam[0] = dynamicParams.UserParams[0];
707
    ToMk3Mag.UserParam[0] = dynamicParams.UserParams[0];
661
    ToMk3Mag.UserParam[1] = dynamicParams.UserParams[1];
708
    ToMk3Mag.UserParam[1] = dynamicParams.UserParams[1];
662
    ToMk3Mag.CalState = CompassCalState;
709
    ToMk3Mag.CalState = compassCalState;
663
    SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &ToMk3Mag,sizeof(ToMk3Mag));
710
    SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &ToMk3Mag,sizeof(ToMk3Mag));
664
    // the last state is 5 and should be send only once to avoid multiple flash writing
711
    // the last state is 5 and should be send only once to avoid multiple flash writing
665
    if(CompassCalState > 4)  CompassCalState = 0;
712
    if(compassCalState > 4)  compassCalState = 0;
666
    Compass_Timer = SetDelay(99);
713
    Compass_Timer = SetDelay(99);
667
  }
714
  }
668
#endif
715
#endif
669
 
716
 
670
  if(Request_MotorTest && txd_complete) {
717
  if(request_MotorTest && txd_complete) {
671
    SendOutData('T', FC_ADDRESS, 0);
718
    SendOutData('T', FC_ADDRESS, 0);
672
    Request_MotorTest = FALSE;
719
    request_MotorTest = FALSE;
673
  }
720
  }
674
 
721
 
675
  if(Request_PPMChannels && txd_complete) {
722
  if(request_PPMChannels && txd_complete) {
-
 
723
    SendOutData('P', FC_ADDRESS, 1, (uint8_t *)&PPM_in, sizeof(PPM_in));
-
 
724
    request_PPMChannels = FALSE;
-
 
725
  }
-
 
726
 
-
 
727
  if (request_variables && txd_complete) {
676
    SendOutData('P', FC_ADDRESS, 1, (uint8_t *)&PPM_in, sizeof(PPM_in));
728
    SendOutData('X', FC_ADDRESS, 1, (uint8_t *)&variables, sizeof(variables));
677
    Request_PPMChannels = FALSE;
729
    request_variables = FALSE;
678
  }
730
  }
679
}
731
}
680
 
732