Subversion Repositories MK3Mag

Rev

Rev 51 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 51 Rev 62
1
/*#######################################################################################
1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
2
MK3Mag 3D-Magnet sensor
3
!!! THIS IS NOT FREE SOFTWARE !!!
3
!!! THIS IS NOT FREE SOFTWARE !!!
4
#######################################################################################*/
4
#######################################################################################*/
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Copyright (c) 05.2008 Holger Buss
6
// + Copyright (c) 05.2008 Holger Buss
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
8
// + Nur für den privaten Gebrauch
8
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
9
// + www.MikroKopter.com
9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
11
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
12
// + mit unserer Zustimmung zulässig
12
// + mit unserer Zustimmung zulässig
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
24
// + eindeutig als Ursprung verlinkt werden
24
// + eindeutig als Ursprung verlinkt werden
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
27
// + Benutzung auf eigene Gefahr
27
// + Benutzung auf eigene Gefahr
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
30
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
-
 
31
// + mit unserer Zustimmung zulässig
-
 
32
/// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
33
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
35
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + this list of conditions and the following disclaimer.
36
// + this list of conditions and the following disclaimer.
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
37
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
38
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
39
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
40
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
38
// +     for non-commercial use (directly or indirectly)
41
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
42
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
43
// +     with our written permission
41
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
44
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
42
// +     In case of doubt please contact: info@MikroKopter.de
45
// +     In case of doubt please contact: info@MikroKopter.de
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
46
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
44
// +     clearly linked as origin
47
// +     clearly linked as origin
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
58
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <avr/io.h>
60
#include <avr/io.h>
58
#include <avr/interrupt.h>
61
#include <avr/interrupt.h>
59
#include <avr/wdt.h>
62
#include <avr/wdt.h>
60
#include <stdarg.h>
63
#include <stdarg.h>
61
#include <string.h>
64
#include <string.h>
62
#include "main.h"
65
#include "main.h"
63
#include "uart.h"
66
#include "uart.h"
64
#include "timer0.h"
67
#include "timer0.h"
65
#include "twislave.h"
68
#include "twislave.h"
66
 
69
 
67
// slave addresses
70
// slave addresses
68
#define FC_ADDRESS              1
71
#define FC_ADDRESS              1
69
#define NC_ADDRESS              2
72
#define NC_ADDRESS              2
70
#define MK3MAG_ADDRESS  3
73
#define MK3MAG_ADDRESS  3
71
 
74
 
72
#define FALSE   0
75
#define FALSE   0
73
#define TRUE    1
76
#define TRUE    1
74
 
77
 
75
// keep buffers as small as possible
78
// keep buffers as small as possible
76
#define TXD_BUFFER_LEN  100
79
#define TXD_BUFFER_LEN  100
77
#define RXD_BUFFER_LEN  30
80
#define RXD_BUFFER_LEN  30
78
 
81
 
79
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
82
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
80
volatile uint8_t rxd_buffer_locked = FALSE;
83
volatile uint8_t rxd_buffer_locked = FALSE;
81
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
84
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
82
volatile uint8_t txd_complete = TRUE;
85
volatile uint8_t txd_complete = TRUE;
83
volatile uint8_t ReceivedBytes = 0;
86
volatile uint8_t ReceivedBytes = 0;
84
volatile uint8_t *pRxData = 0;
87
volatile uint8_t *pRxData = 0;
85
volatile uint8_t RxDataLen = 0;
88
volatile uint8_t RxDataLen = 0;
86
 
89
 
87
// send flags
90
// send flags
88
#define RQST_VERSION_INFO               0x01
91
#define RQST_VERSION_INFO               0x01
89
#define RQST_DEBUG_DATA                 0x02
92
#define RQST_DEBUG_DATA                 0x02
90
#define RQST_DEBUG_LABEL                0x04
93
#define RQST_DEBUG_LABEL                0x04
91
#define RQST_COMPASS_HEADING    0x08
94
#define RQST_COMPASS_HEADING    0x08
92
#define RQST_EXTERN_CTRL                0x10
95
#define RQST_EXTERN_CTRL                0x10
93
 
96
 
94
 
97
 
95
uint8_t RequestFlags = 0x00;
98
uint8_t RequestFlags = 0x00;
96
uint8_t RequestDebugLabel = 0;
99
uint8_t RequestDebugLabel = 0;
97
uint8_t ConfirmFrame = 0;
100
uint8_t ConfirmFrame = 0;
98
 
101
 
99
uint8_t PC_Connected = 0;
102
uint8_t PC_Connected = 0;
100
uint8_t FC_Connected = 0;
103
uint8_t FC_Connected = 0;
101
 
104
 
102
 
105
 
103
DebugOut_t              DebugOut;
106
DebugOut_t              DebugOut;
104
ExternData_t    ExternData;
107
ExternData_t    ExternData;
105
ExternControl_t ExternControl;
108
ExternControl_t ExternControl;
106
UART_VersionInfo_t      UART_VersionInfo;
109
UART_VersionInfo_t      UART_VersionInfo;
107
 
110
 
108
uint16_t        DebugData_Timer;
111
uint16_t        DebugData_Timer;
109
uint16_t        DebugData_Interval = 500;
112
uint16_t        DebugData_Interval = 500;
110
 
113
 
111
const uint8_t ANALOG_LABEL[32][16] =
114
const uint8_t ANALOG_LABEL[32][16] =
112
{
115
{
113
        //1234567890123456
116
        //1234567890123456
114
        "Magnet X        ", //0
117
        "Magnet X        ", //0
115
        "Magnet Y        ",
118
        "Magnet Y        ",
116
        "Magnet Z        ",
119
        "Magnet Z        ",
117
        "RawMagnet X     ",
120
        "RawMagnet X     ",
118
        "RawMagnet Y     ",
121
        "RawMagnet Y     ",
119
        "RawMagnet Z     ", //5
122
        "RawMagnet Z     ", //5
120
        "Attitude Nick   ",
123
        "Attitude Nick   ",
121
        "Attitude Roll   ",
124
        "Attitude Roll   ",
122
        "Magnet X Offset ",
125
        "Magnet X Offset ",
123
        "Magnet X Range  ",
126
        "Magnet X Range  ",
124
        "Magnet Y Offset ", //10
127
        "Magnet Y Offset ", //10
125
        "Magnet Y Range  ",
128
        "Magnet Y Range  ",
126
        "Magnet Z Offset ",
129
        "Magnet Z Offset ",
127
        "Magnet Z Range  ",
130
        "Magnet Z Range  ",
128
        "Calstate        ",
131
        "Calstate        ",
129
        "Heading         ", //15
132
        "Heading         ", //15
130
        "User0           ",
133
        "User0           ",
131
        "User1           ",
134
        "User1           ",
132
        "Acc X           ",
135
        "Acc X           ",
133
        "Acc Y           ",
136
        "Acc Y           ",
134
        "Acc Z           ", //20
137
        "Acc Z           ", //20
135
        "RawAcc X        ",
138
        "RawAcc X        ",
136
        "RawAcc Y        ",
139
        "RawAcc Y        ",
137
        "RawAcc Z        ",
140
        "RawAcc Z        ",
138
        "Acc X Offset    ",
141
        "Acc X Offset    ",
139
        "Acc Y Offset    ", //25
142
        "Acc Y Offset    ", //25
140
        "Acc Z Offset    ",
143
        "Acc Z Offset    ",
141
        "Analog27        ",
144
        "Analog27        ",
142
        "Analog28        ",
145
        "Analog28        ",
143
        "Analog29        ",
146
        "Analog29        ",
144
        "I2C Error       ", //30
147
        "I2C Error       ", //30
145
        "I2C Okay        "
148
        "I2C Okay        "
146
};
149
};
147
 
150
 
148
 
151
 
149
/****************************************************************/
152
/****************************************************************/
150
/*              Initialization of the USART0                    */
153
/*              Initialization of the USART0                    */
151
/****************************************************************/
154
/****************************************************************/
152
void USART0_Init (void)
155
void USART0_Init (void)
153
{
156
{
154
        uint8_t sreg = SREG;
157
        uint8_t sreg = SREG;
155
        uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1);
158
        uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1);
156
 
159
 
157
        // disable all interrupts before configuration
160
        // disable all interrupts before configuration
158
        cli();
161
        cli();
159
 
162
 
160
        // disable RX-Interrupt
163
        // disable RX-Interrupt
161
        UCSR0B &= ~(1 << RXCIE0);
164
        UCSR0B &= ~(1 << RXCIE0);
162
        // disable TX-Interrupt
165
        // disable TX-Interrupt
163
        UCSR0B &= ~(1 << TXCIE0);
166
        UCSR0B &= ~(1 << TXCIE0);
164
 
167
 
165
        // set direction of RXD0 and TXD0 pins
168
        // set direction of RXD0 and TXD0 pins
166
 
169
 
167
        // set RXD0 (PD0) as an input pin tristate
170
        // set RXD0 (PD0) as an input pin tristate
168
        DDRD  &= ~(1 << DDD0);
171
        DDRD  &= ~(1 << DDD0);
169
        PORTD &= ~(1 << PORTD0);
172
        PORTD &= ~(1 << PORTD0);
170
        // set TXD0 (PD1) as an output pin
173
        // set TXD0 (PD1) as an output pin
171
        DDRD  |= (1 << DDD1);
174
        DDRD  |= (1 << DDD1);
172
        PORTD &= ~(1 << PORTD1);
175
        PORTD &= ~(1 << PORTD1);
173
 
176
 
174
 
177
 
175
        // USART0 Baud Rate Register
178
        // USART0 Baud Rate Register
176
        // set clock divider
179
        // set clock divider
177
        UBRR0H = (uint8_t)(ubrr >> 8);
180
        UBRR0H = (uint8_t)(ubrr >> 8);
178
        UBRR0L = (uint8_t)ubrr;
181
        UBRR0L = (uint8_t)ubrr;
179
 
182
 
180
        // USART0 Control and Status Register A, B, C
183
        // USART0 Control and Status Register A, B, C
181
 
184
 
182
        // enable double speed operation
185
        // enable double speed operation
183
        UCSR0A |= (1 << U2X0);
186
        UCSR0A |= (1 << U2X0);
184
 
187
 
185
        // set asynchronous mode
188
        // set asynchronous mode
186
        UCSR0C &= ~(1 << UMSEL01);
189
        UCSR0C &= ~(1 << UMSEL01);
187
        UCSR0C &= ~(1 << UMSEL00);
190
        UCSR0C &= ~(1 << UMSEL00);
188
        // no parity
191
        // no parity
189
        UCSR0C &= ~(1 << UPM01);
192
        UCSR0C &= ~(1 << UPM01);
190
        UCSR0C &= ~(1 << UPM00);
193
        UCSR0C &= ~(1 << UPM00);
191
        // 1 stop bit
194
        // 1 stop bit
192
        UCSR0C &= ~(1 << USBS0);
195
        UCSR0C &= ~(1 << USBS0);
193
        // 8-bit
196
        // 8-bit
194
        UCSR0B &= ~(1 << UCSZ02);
197
        UCSR0B &= ~(1 << UCSZ02);
195
        UCSR0C |=  (1 << UCSZ01);
198
        UCSR0C |=  (1 << UCSZ01);
196
        UCSR0C |=  (1 << UCSZ00);
199
        UCSR0C |=  (1 << UCSZ00);
197
 
200
 
198
        // enable receiver and transmitter
201
        // enable receiver and transmitter
199
        UCSR0B |= (1 << RXEN0);
202
        UCSR0B |= (1 << RXEN0);
200
        UCSR0B |= (1 << TXEN0);
203
        UCSR0B |= (1 << TXEN0);
201
 
204
 
202
        // flush receive buffer
205
        // flush receive buffer
203
        while ( UCSR0A & (1<<RXC0) ) UDR0;
206
        while ( UCSR0A & (1<<RXC0) ) UDR0;
204
 
207
 
205
        // enable RX-Interrupt
208
        // enable RX-Interrupt
206
        UCSR0B |= (1 << RXCIE0);
209
        UCSR0B |= (1 << RXCIE0);
207
        // enable TX-Interrupt
210
        // enable TX-Interrupt
208
        UCSR0B |= (1 << TXCIE0);
211
        UCSR0B |= (1 << TXCIE0);
209
 
212
 
210
        // initialize the debug timer
213
        // initialize the debug timer
211
        DebugData_Timer = SetDelay(DebugData_Interval);
214
        DebugData_Timer = SetDelay(DebugData_Interval);
212
 
215
 
213
        // unlock rxd_buffer
216
        // unlock rxd_buffer
214
        rxd_buffer_locked = FALSE;
217
        rxd_buffer_locked = FALSE;
215
        pRxData = 0;
218
        pRxData = 0;
216
        RxDataLen = 0;
219
        RxDataLen = 0;
217
 
220
 
218
        // no bytes to send
221
        // no bytes to send
219
        txd_complete = TRUE;
222
        txd_complete = TRUE;
220
 
223
 
221
 
224
 
222
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
225
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
223
        UART_VersionInfo.SWMinor = VERSION_MINOR;
226
        UART_VersionInfo.SWMinor = VERSION_MINOR;
224
        UART_VersionInfo.SWPatch = VERSION_PATCH;
227
        UART_VersionInfo.SWPatch = VERSION_PATCH;
225
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
228
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
226
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
229
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
227
 
230
 
228
        // Version beim Start ausgeben (nicht schön, aber geht... )
231
        // Version beim Start ausgeben (nicht schön, aber geht... )
229
        USART0_putchar ('\n');
232
        USART0_putchar ('\n');
230
        USART0_putchar ('C');
233
        USART0_putchar ('C');
231
        USART0_putchar ('P');
234
        USART0_putchar ('P');
232
        USART0_putchar (':');
235
        USART0_putchar (':');
233
        USART0_putchar ('V');
236
        USART0_putchar ('V');
234
        USART0_putchar (0x30 + VERSION_MAJOR);
237
        USART0_putchar (0x30 + VERSION_MAJOR);
235
        USART0_putchar ('.');
238
        USART0_putchar ('.');
236
        USART0_putchar (0x30 + VERSION_MINOR/10);
239
        USART0_putchar (0x30 + VERSION_MINOR/10);
237
        USART0_putchar (0x30 + VERSION_MINOR%10);
240
        USART0_putchar (0x30 + VERSION_MINOR%10);
238
        USART0_putchar ('a' + VERSION_PATCH);
241
        USART0_putchar ('a' + VERSION_PATCH);
239
    USART0_putchar ('\n');
242
    USART0_putchar ('\n');
240
 
243
 
241
    // restore global interrupt flags
244
    // restore global interrupt flags
242
    SREG = sreg;
245
    SREG = sreg;
243
}
246
}
244
 
247
 
245
// ---------------------------------------------------------------------------------
248
// ---------------------------------------------------------------------------------
246
void USART0_EnableTXD(void)
249
void USART0_EnableTXD(void)
247
{
250
{
248
        DDRD |= (1<<DDD1);                      // set TXD pin as output
251
        DDRD |= (1<<DDD1);                      // set TXD pin as output
249
        PORTD &= ~(1 << PORTD1);
252
        PORTD &= ~(1 << PORTD1);
250
        UCSR0B |= (1 << TXEN0);         // enable TX in USART
253
        UCSR0B |= (1 << TXEN0);         // enable TX in USART
251
        UCSR0B |= (1 << TXCIE0);        // disable TX-Interrupt
254
        UCSR0B |= (1 << TXCIE0);        // disable TX-Interrupt
252
}
255
}
253
 
256
 
254
// ---------------------------------------------------------------------------------
257
// ---------------------------------------------------------------------------------
255
void USART0_DisableTXD(void)
258
void USART0_DisableTXD(void)
256
{
259
{
257
        while(!txd_complete){ };
260
        while(!txd_complete){ };
258
 
261
 
259
        UCSR0B &= ~(1 << TXCIE0);   // disable TX-Interrupt
262
        UCSR0B &= ~(1 << TXCIE0);   // disable TX-Interrupt
260
        UCSR0B &= ~(1 << TXEN0);        // disable TXD in USART
263
        UCSR0B &= ~(1 << TXEN0);        // disable TXD in USART
261
        DDRD &= ~(1<<DDD1);             // set TXD pin as input
264
        DDRD &= ~(1<<DDD1);             // set TXD pin as input
262
        PORTD &= ~(1 << PORTD1);
265
        PORTD &= ~(1 << PORTD1);
263
}
266
}
264
 
267
 
265
/****************************************************************/
268
/****************************************************************/
266
/*               USART0 transmitter ISR                         */
269
/*               USART0 transmitter ISR                         */
267
/****************************************************************/
270
/****************************************************************/
268
ISR(USART_TX_vect)
271
ISR(USART_TX_vect)
269
{
272
{
270
        static uint16_t ptr_txd_buffer = 0;
273
        static uint16_t ptr_txd_buffer = 0;
271
        uint8_t tmp_tx;
274
        uint8_t tmp_tx;
272
        if(!txd_complete) // transmission not completed
275
        if(!txd_complete) // transmission not completed
273
        {
276
        {
274
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
277
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
275
                tmp_tx = txd_buffer[ptr_txd_buffer];
278
                tmp_tx = txd_buffer[ptr_txd_buffer];
276
                // if terminating character or end of txd buffer was reached
279
                // if terminating character or end of txd buffer was reached
277
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
280
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
278
                {
281
                {
279
                        ptr_txd_buffer = 0; // reset txd pointer
282
                        ptr_txd_buffer = 0; // reset txd pointer
280
                        txd_complete = TRUE; // stop transmission
283
                        txd_complete = TRUE; // stop transmission
281
                }
284
                }
282
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
285
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
283
        }
286
        }
284
        // transmission completed
287
        // transmission completed
285
        else ptr_txd_buffer = 0;
288
        else ptr_txd_buffer = 0;
286
}
289
}
287
 
290
 
288
/****************************************************************/
291
/****************************************************************/
289
/*               USART0 receiver ISR                            */
292
/*               USART0 receiver ISR                            */
290
/****************************************************************/
293
/****************************************************************/
291
ISR(USART_RX_vect)
294
ISR(USART_RX_vect)
292
{
295
{
293
        static uint16_t crc;
296
        static uint16_t crc;
294
        uint8_t crc1, crc2;
297
        uint8_t crc1, crc2;
295
        uint8_t c;
298
        uint8_t c;
296
        static uint8_t ptr_rxd_buffer = 0;
299
        static uint8_t ptr_rxd_buffer = 0;
297
 
300
 
298
        c = UDR0;  // catch the received byte
301
        c = UDR0;  // catch the received byte
299
 
302
 
300
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
303
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
301
 
304
 
302
        // the rxd buffer is unlocked
305
        // the rxd buffer is unlocked
303
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
306
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
304
        {
307
        {
305
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
308
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
306
                crc = c; // init crc
309
                crc = c; // init crc
307
        }
310
        }
308
        #if 0
311
        #if 0
309
        else if (ptr_rxd_buffer == 1) // handle address
312
        else if (ptr_rxd_buffer == 1) // handle address
310
        {
313
        {
311
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
314
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
312
                crc += c; // update crc
315
                crc += c; // update crc
313
        }
316
        }
314
        #endif
317
        #endif
315
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
318
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
316
        {
319
        {
317
                if(c != '\r') // no termination character
320
                if(c != '\r') // no termination character
318
                {
321
                {
319
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
322
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
320
                        crc += c; // update crc
323
                        crc += c; // update crc
321
                }
324
                }
322
                else // termination character was received
325
                else // termination character was received
323
                {
326
                {
324
                        // the last 2 bytes are no subject for checksum calculation
327
                        // the last 2 bytes are no subject for checksum calculation
325
                        // they are the checksum itself
328
                        // they are the checksum itself
326
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
329
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
327
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
330
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
328
                        // calculate checksum from transmitted data
331
                        // calculate checksum from transmitted data
329
                        crc %= 4096;
332
                        crc %= 4096;
330
                        crc1 = '=' + crc / 64;
333
                        crc1 = '=' + crc / 64;
331
                        crc2 = '=' + crc % 64;
334
                        crc2 = '=' + crc % 64;
332
                        // compare checksum to transmitted checksum bytes
335
                        // compare checksum to transmitted checksum bytes
333
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
336
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
334
                        {   // checksum valid
337
                        {   // checksum valid
335
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
338
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
336
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
339
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
337
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
340
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
338
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
341
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
339
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
342
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
340
                        }
343
                        }
341
                        else
344
                        else
342
                        {       // checksum invalid
345
                        {       // checksum invalid
343
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
346
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
344
                        }
347
                        }
345
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
348
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
346
                }
349
                }
347
        }
350
        }
348
        else // rxd buffer overrun
351
        else // rxd buffer overrun
349
        {
352
        {
350
                ptr_rxd_buffer = 0; // reset rxd buffer
353
                ptr_rxd_buffer = 0; // reset rxd buffer
351
                rxd_buffer_locked = FALSE; // unlock rxd buffer
354
                rxd_buffer_locked = FALSE; // unlock rxd buffer
352
        }
355
        }
353
}
356
}
354
 
357
 
355
 
358
 
356
// --------------------------------------------------------------------------
359
// --------------------------------------------------------------------------
357
void AddCRC(uint16_t datalen)
360
void AddCRC(uint16_t datalen)
358
{
361
{
359
        uint16_t tmpCRC = 0, i;
362
        uint16_t tmpCRC = 0, i;
360
        for(i = 0; i < datalen; i++)
363
        for(i = 0; i < datalen; i++)
361
        {
364
        {
362
                tmpCRC += txd_buffer[i];
365
                tmpCRC += txd_buffer[i];
363
        }
366
        }
364
        tmpCRC %= 4096;
367
        tmpCRC %= 4096;
365
        txd_buffer[i++] = '=' + tmpCRC / 64;
368
        txd_buffer[i++] = '=' + tmpCRC / 64;
366
        txd_buffer[i++] = '=' + tmpCRC % 64;
369
        txd_buffer[i++] = '=' + tmpCRC % 64;
367
        txd_buffer[i++] = '\r';
370
        txd_buffer[i++] = '\r';
368
        txd_complete = FALSE;
371
        txd_complete = FALSE;
369
        UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR)
372
        UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR)
370
}
373
}
371
 
374
 
372
 
375
 
373
 
376
 
374
// --------------------------------------------------------------------------
377
// --------------------------------------------------------------------------
375
void SendOutData(uint8_t cmd, uint8_t address, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
378
void SendOutData(uint8_t cmd, uint8_t address, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
376
{
379
{
377
        va_list ap;
380
        va_list ap;
378
        uint16_t pt = 0;
381
        uint16_t pt = 0;
379
        uint8_t a,b,c;
382
        uint8_t a,b,c;
380
        uint8_t ptr = 0;
383
        uint8_t ptr = 0;
381
 
384
 
382
        uint8_t *pdata = 0;
385
        uint8_t *pdata = 0;
383
        int len = 0;
386
        int len = 0;
384
 
387
 
385
        txd_buffer[pt++] = '#';                         // Start character
388
        txd_buffer[pt++] = '#';                         // Start character
386
        txd_buffer[pt++] = 'a' + address;       // Address (a=0; b=1,...)
389
        txd_buffer[pt++] = 'a' + address;       // Address (a=0; b=1,...)
387
        txd_buffer[pt++] = cmd;                         // Command
390
        txd_buffer[pt++] = cmd;                         // Command
388
 
391
 
389
        va_start(ap, numofbuffers);
392
        va_start(ap, numofbuffers);
390
        if(numofbuffers)
393
        if(numofbuffers)
391
        {
394
        {
392
                pdata = va_arg(ap, uint8_t*);
395
                pdata = va_arg(ap, uint8_t*);
393
                len = va_arg(ap, int);
396
                len = va_arg(ap, int);
394
                ptr = 0;
397
                ptr = 0;
395
                numofbuffers--;
398
                numofbuffers--;
396
        }
399
        }
397
 
400
 
398
        while(len)
401
        while(len)
399
        {
402
        {
400
                if(len)
403
                if(len)
401
                {
404
                {
402
                        a = pdata[ptr++];
405
                        a = pdata[ptr++];
403
                        len--;
406
                        len--;
404
                        if((!len) && numofbuffers)
407
                        if((!len) && numofbuffers)
405
                        {
408
                        {
406
                                pdata = va_arg(ap, uint8_t*);
409
                                pdata = va_arg(ap, uint8_t*);
407
                                len = va_arg(ap, int);
410
                                len = va_arg(ap, int);
408
                                ptr = 0;
411
                                ptr = 0;
409
                                numofbuffers--;
412
                                numofbuffers--;
410
                        }
413
                        }
411
                }
414
                }
412
                else a = 0;
415
                else a = 0;
413
                if(len)
416
                if(len)
414
                {
417
                {
415
                        b = pdata[ptr++];
418
                        b = pdata[ptr++];
416
                        len--;
419
                        len--;
417
                        if((!len) && numofbuffers)
420
                        if((!len) && numofbuffers)
418
                        {
421
                        {
419
                                pdata = va_arg(ap, uint8_t*);
422
                                pdata = va_arg(ap, uint8_t*);
420
                                len = va_arg(ap, int);
423
                                len = va_arg(ap, int);
421
                                ptr = 0;
424
                                ptr = 0;
422
                                numofbuffers--;
425
                                numofbuffers--;
423
                        }
426
                        }
424
                }
427
                }
425
                else b = 0;
428
                else b = 0;
426
                if(len)
429
                if(len)
427
                {
430
                {
428
                        c = pdata[ptr++];
431
                        c = pdata[ptr++];
429
                        len--;
432
                        len--;
430
                        if((!len) && numofbuffers)
433
                        if((!len) && numofbuffers)
431
                        {
434
                        {
432
                                pdata = va_arg(ap, uint8_t*);
435
                                pdata = va_arg(ap, uint8_t*);
433
                                len = va_arg(ap, int);
436
                                len = va_arg(ap, int);
434
                                ptr = 0;
437
                                ptr = 0;
435
                                numofbuffers--;
438
                                numofbuffers--;
436
                        }
439
                        }
437
                }
440
                }
438
                else c = 0;
441
                else c = 0;
439
                txd_buffer[pt++] = '=' + (a >> 2);
442
                txd_buffer[pt++] = '=' + (a >> 2);
440
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
443
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
441
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
444
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
442
                txd_buffer[pt++] = '=' + ( c & 0x3f);
445
                txd_buffer[pt++] = '=' + ( c & 0x3f);
443
        }
446
        }
444
        va_end(ap);
447
        va_end(ap);
445
        AddCRC(pt); // add checksum after data block and initates the transmission
448
        AddCRC(pt); // add checksum after data block and initates the transmission
446
}
449
}
447
 
450
 
448
 
451
 
449
// --------------------------------------------------------------------------
452
// --------------------------------------------------------------------------
450
void Decode64(void)
453
void Decode64(void)
451
{
454
{
452
        uint8_t a,b,c,d;
455
        uint8_t a,b,c,d;
453
        uint8_t x,y,z;
456
        uint8_t x,y,z;
454
        uint8_t ptrIn = 3;
457
        uint8_t ptrIn = 3;
455
        uint8_t ptrOut = 3;
458
        uint8_t ptrOut = 3;
456
        uint8_t len = ReceivedBytes - 6;
459
        uint8_t len = ReceivedBytes - 6;
457
 
460
 
458
        while(len)
461
        while(len)
459
        {
462
        {
460
                a = rxd_buffer[ptrIn++] - '=';
463
                a = rxd_buffer[ptrIn++] - '=';
461
                b = rxd_buffer[ptrIn++] - '=';
464
                b = rxd_buffer[ptrIn++] - '=';
462
                c = rxd_buffer[ptrIn++] - '=';
465
                c = rxd_buffer[ptrIn++] - '=';
463
                d = rxd_buffer[ptrIn++] - '=';
466
                d = rxd_buffer[ptrIn++] - '=';
464
                //if(ptrIn > ReceivedBytes - 3) break;
467
                //if(ptrIn > ReceivedBytes - 3) break;
465
 
468
 
466
                x = (a << 2) | (b >> 4);
469
                x = (a << 2) | (b >> 4);
467
                y = ((b & 0x0f) << 4) | (c >> 2);
470
                y = ((b & 0x0f) << 4) | (c >> 2);
468
                z = ((c & 0x03) << 6) | d;
471
                z = ((c & 0x03) << 6) | d;
469
 
472
 
470
                if(len--) rxd_buffer[ptrOut++] = x; else break;
473
                if(len--) rxd_buffer[ptrOut++] = x; else break;
471
                if(len--) rxd_buffer[ptrOut++] = y; else break;
474
                if(len--) rxd_buffer[ptrOut++] = y; else break;
472
                if(len--) rxd_buffer[ptrOut++] = z; else break;
475
                if(len--) rxd_buffer[ptrOut++] = z; else break;
473
        }
476
        }
474
        pRxData = &rxd_buffer[3];
477
        pRxData = &rxd_buffer[3];
475
        RxDataLen = ptrOut - 3;
478
        RxDataLen = ptrOut - 3;
476
}
479
}
477
 
480
 
478
 
481
 
479
 
482
 
480
// --------------------------------------------------------------------------
483
// --------------------------------------------------------------------------
481
int16_t USART0_putchar (int8_t c)
484
int16_t USART0_putchar (int8_t c)
482
{
485
{
483
        // if tx is not enabled return immediatly
486
        // if tx is not enabled return immediatly
484
        if(!(UCSR0B & (1 << TXEN0))) return (0);
487
        if(!(UCSR0B & (1 << TXEN0))) return (0);
485
        if (c == '\n') USART0_putchar('\r');
488
        if (c == '\n') USART0_putchar('\r');
486
        // wait until previous character was send
489
        // wait until previous character was send
487
        loop_until_bit_is_set(UCSR0A, UDRE0);
490
        loop_until_bit_is_set(UCSR0A, UDRE0);
488
        // send character
491
        // send character
489
        UDR0 = c;
492
        UDR0 = c;
490
        return (0);
493
        return (0);
491
}
494
}
492
 
495
 
493
 
496
 
494
// --------------------------------------------------------------------------
497
// --------------------------------------------------------------------------
495
void USART0_ProcessRxData(void)
498
void USART0_ProcessRxData(void)
496
{
499
{
497
        // if data in the rxd buffer are not locked immediately return
500
        // if data in the rxd buffer are not locked immediately return
498
        if(!rxd_buffer_locked) return;
501
        if(!rxd_buffer_locked) return;
499
 
502
 
500
        Decode64(); // decode data block in rxd_buffer
503
        Decode64(); // decode data block in rxd_buffer
501
 
504
 
502
        switch(rxd_buffer[1]-'a') // check Slave Address
505
        switch(rxd_buffer[1]-'a') // check Slave Address
503
        {
506
        {
504
                case MK3MAG_ADDRESS:
507
                case MK3MAG_ADDRESS:
505
 
508
 
506
                switch(rxd_buffer[2]) // check for CmdID
509
                switch(rxd_buffer[2]) // check for CmdID
507
                {
510
                {
508
                        case 'w': // old style
511
                        case 'w': // old style
509
                        case 'k': // Attitude info from FC
512
                        case 'k': // Attitude info from FC
510
                                memcpy(&ExternData, (uint8_t*)pRxData, sizeof(ExternData));
513
                                memcpy(&ExternData, (uint8_t*)pRxData, sizeof(ExternData));
511
                                RequestFlags |= RQST_COMPASS_HEADING;
514
                                RequestFlags |= RQST_COMPASS_HEADING;
512
                                AttitudeSource = ATTITUDE_SOURCE_UART;
515
                                AttitudeSource = ATTITUDE_SOURCE_UART;
513
                                Orientation = ExternData.Orientation;
516
                                Orientation = ExternData.Orientation;
514
                                FC_Connected = 255;
517
                                FC_Connected = 255;
515
                                break;
518
                                break;
516
 
519
 
517
                        default:
520
                        default:
518
                                // unsupported command
521
                                // unsupported command
519
                                break;
522
                                break;
520
                } // case MK3MAG_ADDRESS:
523
                } // case MK3MAG_ADDRESS:
521
 
524
 
522
                default: // any Slave Adress
525
                default: // any Slave Adress
523
 
526
 
524
                switch(rxd_buffer[2]) // check for CmdID
527
                switch(rxd_buffer[2]) // check for CmdID
525
                {
528
                {
526
                        case 'b': // extern control
529
                        case 'b': // extern control
527
                                memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl));
530
                                memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl));
528
                                #define KEY1    0x01
531
                                #define KEY1    0x01
529
                                #define KEY2    0x02
532
                                #define KEY2    0x02
530
                                #define KEY3    0x04
533
                                #define KEY3    0x04
531
                                #define KEY4    0x08
534
                                #define KEY4    0x08
532
                                #define KEY5    0x10
535
                                #define KEY5    0x10
533
                                // use right arrow at display for switching the calstate
536
                                // use right arrow at display for switching the calstate
534
                                if(ExternControl.RemoteButtons & KEY2)
537
                                if(ExternControl.RemoteButtons & KEY2)
535
                                {
538
                                {
536
                                        ExternData.CalState++;
539
                                        ExternData.CalState++;
537
                                        if(ExternData.CalState == 6) ExternData.CalState = 0;
540
                                        if(ExternData.CalState == 6) ExternData.CalState = 0;
538
                                }
541
                                }
539
                                ConfirmFrame = ExternControl.Frame;
542
                                ConfirmFrame = ExternControl.Frame;
540
                                PC_Connected = 255;
543
                                PC_Connected = 255;
541
                                break;
544
                                break;
542
 
545
 
543
                        case 'd': // request for the debug data
546
                        case 'd': // request for the debug data
544
                                DebugData_Interval = (uint16_t) pRxData[0] * 10;
547
                                DebugData_Interval = (uint16_t) pRxData[0] * 10;
545
                                if(DebugData_Interval>0) RequestFlags |= RQST_DEBUG_DATA;
548
                                if(DebugData_Interval>0) RequestFlags |= RQST_DEBUG_DATA;
546
                                PC_Connected = 255;
549
                                PC_Connected = 255;
547
                                break;
550
                                break;
548
 
551
 
549
                        case 'v': // request version and board release
552
                        case 'v': // request version and board release
550
                                RequestFlags |= RQST_VERSION_INFO;
553
                                RequestFlags |= RQST_VERSION_INFO;
551
                                PC_Connected = 255;
554
                                PC_Connected = 255;
552
                                break;
555
                                break;
553
 
556
 
554
                        case 'h': // request lcd data
557
                        case 'h': // request lcd data
555
                                PC_Connected = 255;
558
                                PC_Connected = 255;
556
                                break;
559
                                break;
557
 
560
 
558
                        case 'a':// Labels of the Analog Debug outputs
561
                        case 'a':// Labels of the Analog Debug outputs
559
                                RequestDebugLabel = pRxData[0];
562
                                RequestDebugLabel = pRxData[0];
560
                                RequestFlags |= RQST_DEBUG_LABEL;
563
                                RequestFlags |= RQST_DEBUG_LABEL;
561
                                PC_Connected = 255;
564
                                PC_Connected = 255;
562
                                break;
565
                                break;
563
 
566
 
564
                        case 'g':// get extern control data
567
                        case 'g':// get extern control data
565
                                RequestFlags |= RQST_EXTERN_CTRL;
568
                                RequestFlags |= RQST_EXTERN_CTRL;
566
                                PC_Connected = 255;
569
                                PC_Connected = 255;
567
                                break;
570
                                break;
568
 
571
 
569
                        default:
572
                        default:
570
                                // unsupported command
573
                                // unsupported command
571
                                break;
574
                                break;
572
                }
575
                }
573
                break; // default:
576
                break; // default:
574
        }
577
        }
575
        // unlock the rxd buffer after processing
578
        // unlock the rxd buffer after processing
576
        pRxData = 0;
579
        pRxData = 0;
577
        RxDataLen = 0;
580
        RxDataLen = 0;
578
        rxd_buffer_locked = FALSE;
581
        rxd_buffer_locked = FALSE;
579
}
582
}
580
 
583
 
581
 
584
 
582
 
585
 
583
//---------------------------------------------------------------------------------------------
586
//---------------------------------------------------------------------------------------------
584
void USART0_TransmitTxData(void)
587
void USART0_TransmitTxData(void)
585
{
588
{
586
        if(!(UCSR0B & (1 << TXEN0))) return;
589
        if(!(UCSR0B & (1 << TXEN0))) return;
587
 
590
 
588
        if(!txd_complete) return;
591
        if(!txd_complete) return;
589
 
592
 
590
        if((RequestFlags & RQST_DEBUG_LABEL) && txd_complete)
593
        if((RequestFlags & RQST_DEBUG_LABEL) && txd_complete)
591
        {
594
        {
592
                SendOutData('A', MK3MAG_ADDRESS, 2, (uint8_t *)&RequestDebugLabel, sizeof(RequestDebugLabel), (uint8_t *) ANALOG_LABEL[RequestDebugLabel], 16);
595
                SendOutData('A', MK3MAG_ADDRESS, 2, (uint8_t *)&RequestDebugLabel, sizeof(RequestDebugLabel), (uint8_t *) ANALOG_LABEL[RequestDebugLabel], 16);
593
                RequestDebugLabel = 0xFF;
596
                RequestDebugLabel = 0xFF;
594
                RequestFlags &= ~RQST_DEBUG_LABEL;
597
                RequestFlags &= ~RQST_DEBUG_LABEL;
595
        }
598
        }
596
 
599
 
597
        if(ConfirmFrame && txd_complete)
600
        if(ConfirmFrame && txd_complete)
598
        {
601
        {
599
                SendOutData('B', MK3MAG_ADDRESS, 1, (uint8_t *) &ConfirmFrame, sizeof(ConfirmFrame));
602
                SendOutData('B', MK3MAG_ADDRESS, 1, (uint8_t *) &ConfirmFrame, sizeof(ConfirmFrame));
600
                ConfirmFrame = 0;
603
                ConfirmFrame = 0;
601
        }
604
        }
602
 
605
 
603
        if(( (DebugData_Interval>0 && CheckDelay(DebugData_Timer)) || (RequestFlags & RQST_DEBUG_DATA)) && txd_complete)
606
        if(( (DebugData_Interval>0 && CheckDelay(DebugData_Timer)) || (RequestFlags & RQST_DEBUG_DATA)) && txd_complete)
604
        {
607
        {
605
                SetDebugValues();
608
                SetDebugValues();
606
                SendOutData('D', MK3MAG_ADDRESS, 1, (uint8_t *) &DebugOut, sizeof(DebugOut));
609
                SendOutData('D', MK3MAG_ADDRESS, 1, (uint8_t *) &DebugOut, sizeof(DebugOut));
607
                DebugData_Timer = SetDelay(DebugData_Interval);
610
                DebugData_Timer = SetDelay(DebugData_Interval);
608
                RequestFlags &= ~RQST_DEBUG_DATA;
611
                RequestFlags &= ~RQST_DEBUG_DATA;
609
        }
612
        }
610
 
613
 
611
 
614
 
612
        if((RequestFlags & RQST_EXTERN_CTRL) && txd_complete)
615
        if((RequestFlags & RQST_EXTERN_CTRL) && txd_complete)
613
        {
616
        {
614
                SendOutData('G', MK3MAG_ADDRESS, 1, (uint8_t *) &ExternControl,sizeof(ExternControl));
617
                SendOutData('G', MK3MAG_ADDRESS, 1, (uint8_t *) &ExternControl,sizeof(ExternControl));
615
                RequestFlags &= ~RQST_EXTERN_CTRL;
618
                RequestFlags &= ~RQST_EXTERN_CTRL;
616
        }
619
        }
617
 
620
 
618
        if((RequestFlags & RQST_COMPASS_HEADING) && txd_complete)
621
        if((RequestFlags & RQST_COMPASS_HEADING) && txd_complete)
619
        {
622
        {
620
                SendOutData('K', FC_ADDRESS, 1, (uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); // send compass heading to FC
623
                SendOutData('K', FC_ADDRESS, 1, (uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); // send compass heading to FC
621
                RequestFlags &= ~RQST_COMPASS_HEADING;
624
                RequestFlags &= ~RQST_COMPASS_HEADING;
622
        }
625
        }
623
 
626
 
624
        if((RequestFlags & RQST_VERSION_INFO) && txd_complete)
627
        if((RequestFlags & RQST_VERSION_INFO) && txd_complete)
625
        {
628
        {
626
                SendOutData('V', MK3MAG_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
629
                SendOutData('V', MK3MAG_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
627
                RequestFlags &= ~RQST_VERSION_INFO;
630
                RequestFlags &= ~RQST_VERSION_INFO;
628
        }
631
        }
629
}
632
}
630
 
633
 
631
 
634
 
632
void USART0_Print(int8_t *msg)
635
void USART0_Print(int8_t *msg)
633
{
636
{
634
        uint8_t i = 0;
637
        uint8_t i = 0;
635
        while(msg[i] != 0)
638
        while(msg[i] != 0)
636
        {
639
        {
637
                USART0_putchar(msg[i++]);
640
                USART0_putchar(msg[i++]);
638
        }
641
        }
639
}
642
}
640
 
643