Subversion Repositories NaviCtrl

Rev

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

Rev 461 Rev 463
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <stdio.h>
56
#include <stdio.h>
57
#include <stdarg.h>
57
#include <stdarg.h>
58
#include <string.h>
58
#include <string.h>
59
 
59
 
60
#include "91x_lib.h"
60
#include "91x_lib.h"
61
#include "main.h"
61
#include "main.h"
-
 
62
#include "math.h"
62
#include "config.h"
63
#include "config.h"
63
#include "menu.h"
64
#include "menu.h"
64
#include "GPS.h"
65
#include "GPS.h"
65
#include "i2c.h"
66
#include "i2c.h"
66
#include "uart0.h"
67
#include "uart0.h"
67
#include "uart1.h"
68
#include "uart1.h"
68
#include "uart2.h"
69
#include "uart2.h"
69
#include "timer1.h"
70
#include "timer1.h"
70
#include "timer2.h"
71
#include "timer2.h"
71
#include "analog.h"
72
#include "analog.h"
72
#include "compass.h"
73
#include "compass.h"
73
#include "waypoints.h"
74
#include "waypoints.h"
74
#include "mkprotocol.h"
75
#include "mkprotocol.h"
75
#include "params.h"
76
#include "params.h"
76
#include "fifo.h"
77
#include "fifo.h"
77
#include "debug.h"
78
#include "debug.h"
78
#include "spi_slave.h"
79
#include "spi_slave.h"
79
#include "ftphelper.h"
80
#include "ftphelper.h"
80
#include "led.h"
81
#include "led.h"
81
#include "fat16.h"
82
#include "fat16.h"
82
 
83
 
83
 
84
 
84
#define FALSE   0
85
#define FALSE   0
85
#define TRUE    1
86
#define TRUE    1
86
 
87
 
87
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
88
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
88
u32 UART1_AboTimeOut = 0;
89
u32 UART1_AboTimeOut = 0;
89
 
90
 
90
u8 UART1_Request_VersionInfo    = FALSE;
91
u8 UART1_Request_VersionInfo    = FALSE;
91
u8 UART1_Request_ExternalControl= FALSE;
92
u8 UART1_Request_ExternalControl= FALSE;
92
u8 UART1_Request_Display                = FALSE;
93
u8 UART1_Request_Display                = FALSE;
93
u8 UART1_Request_Display1               = FALSE;
94
u8 UART1_Request_Display1               = FALSE;
94
u8 UART1_Request_DebugData              = FALSE;
95
u8 UART1_Request_DebugData              = FALSE;
95
u8 UART1_Request_DebugLabel             = 255;
96
u8 UART1_Request_DebugLabel             = 255;
96
u8 UART1_Request_NaviData               = FALSE;
97
u8 UART1_Request_NaviData               = FALSE;
97
u8 UART1_Request_ErrorMessage   = FALSE;
98
u8 UART1_Request_ErrorMessage   = FALSE;
98
u8 UART1_Request_WritePoint             = 0xFF;
99
u8 UART1_Request_WritePoint             = 0xFF;
99
u8 UART1_Request_ReadPoint              = 0;
100
u8 UART1_Request_ReadPoint              = 0;
100
u8 UART1_Request_Data3D             = FALSE;
101
u8 UART1_Request_Data3D             = FALSE;
101
u8 UART1_Request_MotorData          = FALSE;
102
u8 UART1_Request_MotorData          = FALSE;
102
u8 UART1_Request_Echo               = FALSE;
103
u8 UART1_Request_Echo               = FALSE;
103
u8 UART1_Request_ParameterId    = 0;
104
u8 UART1_Request_ParameterId    = 0;
104
u8 UART1_Request_Parameter              = FALSE;
105
u8 UART1_Request_Parameter              = FALSE;
105
u8 UART1_Request_SystemTime             = FALSE;
106
u8 UART1_Request_SystemTime             = FALSE;
106
u8 UART1_DisplayKeys                    = 0;
107
u8 UART1_DisplayKeys                    = 0;
107
u8 UART1_DisplayLine                    = 0;
108
u8 UART1_DisplayLine                    = 0;
108
u8 UART1_ConfirmFrame                   = 0;
109
u8 UART1_ConfirmFrame                   = 0;
109
u8 UART1_Request_FTP                    = FALSE;
110
u8 UART1_Request_FTP                    = FALSE;
110
u8 LastTransmittedFCStatusFlags2 = 0;
111
u8 LastTransmittedFCStatusFlags2 = 0;
111
u8 UART1_ExternalControlConfirmFrame = FALSE;
112
u8 UART1_ExternalControlConfirmFrame = FALSE;
112
 
113
 
113
UART_TypeDef *DebugUART = UART1;
114
UART_TypeDef *DebugUART = UART1;
114
 
115
 
115
#ifdef FOLLOW_ME
116
#ifdef FOLLOW_ME
116
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
117
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
117
u32 UART1_FollowMe_Timer        = 0;
118
u32 UART1_FollowMe_Timer        = 0;
118
Point_t FollowMe;
119
Point_t FollowMe;
119
#endif
120
#endif
120
 
121
 
121
// the primary rx fifo
122
// the primary rx fifo
122
#define UART1_RX_FIFO_LEN 1024
123
#define UART1_RX_FIFO_LEN 1024
123
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
124
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
124
fifo_t UART1_rx_fifo;
125
fifo_t UART1_rx_fifo;
125
 
126
 
126
// the rx buffer
127
// the rx buffer
127
#define UART1_RX_BUFFER_LEN  1024
128
#define UART1_RX_BUFFER_LEN  1024
128
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
129
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
129
Buffer_t UART1_rx_buffer;
130
Buffer_t UART1_rx_buffer;
130
 
131
 
131
// the tx buffer
132
// the tx buffer
132
#define UART1_TX_BUFFER_LEN  1024
133
#define UART1_TX_BUFFER_LEN  1024
133
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
134
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
134
Buffer_t UART1_tx_buffer;
135
Buffer_t UART1_tx_buffer;
135
 
136
 
136
volatile u8 SerialLinkOkay = 0;
137
volatile u8 SerialLinkOkay = 0;
137
 
138
 
138
u8 text[200];
139
u8 text[200];
139
 
140
 
140
const u8 ANALOG_LABEL[32][16] =
141
const u8 ANALOG_LABEL[32][16] =
141
{
142
{
142
   //1234567890123456
143
   //1234567890123456
143
        "AngleNick       ", //0
144
        "AngleNick       ", //0
144
        "AngleRoll       ",
145
        "AngleRoll       ",
145
        "AccNick         ",
146
        "AccNick         ",
146
        "AccRoll         ",
147
        "AccRoll         ",
147
        "OperatingRadius ",
148
        "OperatingRadius ",
148
        "FC-Flags        ", //5
149
        "FC-Flags        ", //5
149
        "NC-Flags        ",
150
        "NC-Flags        ",
150
        "NickServo       ",
151
        "NickServo       ",
151
        "RollServo       ",
152
        "RollServo       ",
152
        "GPS Data        ",
153
        "GPS Data        ",
153
        "CompassHeading  ", //10
154
        "CompassHeading  ", //10
154
        "GyroHeading     ",
155
        "GyroHeading     ",
155
        "SPI Error       ", // achtung: muss auf 12 bleiben
156
        "SPI Error       ", // achtung: muss auf 12 bleiben
156
        "SPI Okay        ",
157
        "SPI Okay        ",
157
        "I2C Error       ",
158
        "I2C Error       ",
158
        "I2C Okay        ", //15
159
        "I2C Okay        ", //15
159
        "16              ",
160
        "16              ",
160
        "17              ",
161
        "17              ",
161
        "18              ",
162
        "18              ",
162
        "19              ", // SD-Card-time 
163
        "19              ", // SD-Card-time 
163
        "EarthMagnet [%] ", //20
164
        "EarthMagnet [%] ", //20
164
        "Ground Speed    ", //  "Z_Speed         ",
165
        "Ground Speed    ", //  "Z_Speed         ",
165
        "N_Speed         ",
166
        "N_Speed         ",
166
        "E_Speed         ",
167
        "E_Speed         ",
167
        "Magnet X        ",
168
        "Magnet X        ",
168
        "Magnet Y        ", //25
169
        "Magnet Y        ", //25
169
        "Magnet Z        ",
170
        "Magnet Z        ",
170
        "Distance N      ",
171
        "Distance N      ",
171
        "Distance E      ",
172
        "Distance E      ",
172
        "GPS_Nick        ",
173
        "GPS_Nick        ",
173
        "GPS_Roll        ", //30
174
        "GPS_Roll        ", //30
174
        "Used_Sats       "
175
        "Used_Sats       "
175
};
176
};
176
 
177
 
177
DebugOut_t DebugOut;
178
DebugOut_t DebugOut;
178
ExternControl_t ExternControl;
179
ExternControl_t ExternControl;
179
UART_VersionInfo_t UART_VersionInfo;
180
UART_VersionInfo_t UART_VersionInfo;
180
NaviData_t NaviData;
181
NaviData_t NaviData;
181
Data3D_t Data3D;
182
Data3D_t Data3D;
182
u16 Echo; // 2 bytes recieved will be sent back as echo
183
u16 Echo; // 2 bytes recieved will be sent back as echo
183
 
184
 
184
u32 UART1_DebugData_Timer = 0;
185
u32 UART1_DebugData_Timer = 0;
185
u32 UART1_DebugData_Interval = 0;       // in ms
186
u32 UART1_DebugData_Interval = 0;       // in ms
186
u32 UART1_NaviData_Timer = 0;
187
u32 UART1_NaviData_Timer = 0;
187
u32 UART1_NaviData_Interval = 0;        // in ms
188
u32 UART1_NaviData_Interval = 0;        // in ms
188
u32 UART1_Data3D_Timer = 0;
189
u32 UART1_Data3D_Timer = 0;
189
u32 UART1_Data3D_Interval = 0;          // in ms
190
u32 UART1_Data3D_Interval = 0;          // in ms
190
u32 UART1_MotorData_Timer = 0;
191
u32 UART1_MotorData_Timer = 0;
191
u32 UART1_MotorData_Interval = 0;               // in ms
192
u32 UART1_MotorData_Interval = 0;               // in ms
192
u32 UART1_Display_Timer = 0;
193
u32 UART1_Display_Timer = 0;
193
u32 UART1_Display_Interval = 0;         // in ms
194
u32 UART1_Display_Interval = 0;         // in ms
194
u32 NMEA_Timer = 0;
195
u32 NMEA_Timer = 0;
195
u32 NMEA_Interval = 500;// in ms
196
u32 NMEA_Interval = 500;// in ms
196
 
197
 
197
/********************************************************/
198
/********************************************************/
198
/*            Initialization the UART1                  */
199
/*            Initialization the UART1                  */
199
/********************************************************/
200
/********************************************************/
200
void UART1_Init (void)
201
void UART1_Init (void)
201
{
202
{
202
        GPIO_InitTypeDef GPIO_InitStructure;
203
        GPIO_InitTypeDef GPIO_InitStructure;
203
        UART_InitTypeDef UART_InitStructure;
204
        UART_InitTypeDef UART_InitStructure;
204
 
205
 
205
        // initialize txd buffer
206
        // initialize txd buffer
206
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
207
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
207
 
208
 
208
        // initialize rxd buffer
209
        // initialize rxd buffer
209
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
210
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
210
 
211
 
211
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
212
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
212
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
213
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
213
 
214
 
214
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
215
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
215
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
216
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
216
 
217
 
217
        /*Configure UART1_Rx pin GPIO3.2*/
218
        /*Configure UART1_Rx pin GPIO3.2*/
218
        GPIO_StructInit(&GPIO_InitStructure);
219
        GPIO_StructInit(&GPIO_InitStructure);
219
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
220
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
220
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
221
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
221
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
222
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
222
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
223
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
223
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
224
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
224
        GPIO_Init(GPIO3, &GPIO_InitStructure);
225
        GPIO_Init(GPIO3, &GPIO_InitStructure);
225
 
226
 
226
        /*Configure UART1_Tx pin GPIO3.3*/
227
        /*Configure UART1_Tx pin GPIO3.3*/
227
        GPIO_StructInit(&GPIO_InitStructure);
228
        GPIO_StructInit(&GPIO_InitStructure);
228
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
229
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
229
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
230
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
230
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
231
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
231
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
232
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
232
        GPIO_Init(GPIO3, &GPIO_InitStructure);
233
        GPIO_Init(GPIO3, &GPIO_InitStructure);
233
 
234
 
234
        /* UART1 configured as follow:
235
        /* UART1 configured as follow:
235
        - Word Length = 8 Bits
236
        - Word Length = 8 Bits
236
        - One Stop Bit
237
        - One Stop Bit
237
        - No parity
238
        - No parity
238
        - BaudRate = 57600 baud
239
        - BaudRate = 57600 baud
239
        - Hardware flow control Disabled
240
        - Hardware flow control Disabled
240
        - Receive and transmit enabled
241
        - Receive and transmit enabled
241
        - Receive and transmit FIFOs are Disabled
242
        - Receive and transmit FIFOs are Disabled
242
        */
243
        */
243
        UART_StructInit(&UART_InitStructure);
244
        UART_StructInit(&UART_InitStructure);
244
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
245
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
245
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
246
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
246
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
247
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
247
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
248
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
248
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
249
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
249
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
250
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
250
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
251
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
251
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
252
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
252
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
253
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
253
 
254
 
254
        UART_DeInit(UART1); // reset uart 1     to default
255
        UART_DeInit(UART1); // reset uart 1     to default
255
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
256
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
256
        // enable uart 1 interrupts selective
257
        // enable uart 1 interrupts selective
257
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
258
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
258
        UART_Cmd(UART1, ENABLE); // enable uart 1
259
        UART_Cmd(UART1, ENABLE); // enable uart 1
259
        // configure the uart 1 interupt line
260
        // configure the uart 1 interupt line
260
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
261
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
261
        // enable the uart 1 IRQ
262
        // enable the uart 1 IRQ
262
        VIC_ITCmd(UART1_ITLine, ENABLE);
263
        VIC_ITCmd(UART1_ITLine, ENABLE);
263
 
264
 
264
        // initialize the debug timer
265
        // initialize the debug timer
265
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
266
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
266
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
267
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
267
        NMEA_Timer = SetDelay(14000);
268
        NMEA_Timer = SetDelay(14000);
268
 
269
 
269
        // Fill Version Info Structure
270
        // Fill Version Info Structure
270
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
271
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
271
        UART_VersionInfo.SWMinor = VERSION_MINOR;
272
        UART_VersionInfo.SWMinor = VERSION_MINOR;
272
        UART_VersionInfo.SWPatch = VERSION_PATCH;
273
        UART_VersionInfo.SWPatch = VERSION_PATCH;
273
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
274
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
274
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
275
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
275
    UART_VersionInfo.HWMajor = Version_HW;
276
    UART_VersionInfo.HWMajor = Version_HW;
276
    UART_VersionInfo.reserved2 = 0;  
277
    UART_VersionInfo.reserved2 = 0;  
277
        UART_VersionInfo.Flags = 0;
278
        UART_VersionInfo.Flags = 0;
278
        NaviData.Version = NAVIDATA_VERSION;
279
        NaviData.Version = NAVIDATA_VERSION;
279
 
280
 
280
        UART1_PutString("\r\n UART1 init...ok");
281
        UART1_PutString("\r\n UART1 init...ok");
281
}
282
}
282
 
283
 
283
 
284
 
284
/****************************************************************/
285
/****************************************************************/
285
/*               USART1 receiver ISR                            */
286
/*               USART1 receiver ISR                            */
286
/****************************************************************/
287
/****************************************************************/
287
void UART1_IRQHandler(void)
288
void UART1_IRQHandler(void)
288
{
289
{
289
        static u8 abortState = 0;
290
        static u8 abortState = 0;
290
        u8 c;
291
        u8 c;
291
 
292
 
292
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
293
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
293
        {
294
        {
294
                // clear the pending bits!
295
                // clear the pending bits!
295
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
296
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
296
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
297
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
297
                // if debug UART is not UART1
298
                // if debug UART is not UART1
298
                if (DebugUART != UART1)
299
                if (DebugUART != UART1)
299
                {       // forward received data to the debug UART tx buffer
300
                {       // forward received data to the debug UART tx buffer
300
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
301
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
301
                        {
302
                        {
302
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
303
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
303
                                c = UART_ReceiveData(UART1);
304
                                c = UART_ReceiveData(UART1);
304
 
305
 
305
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
306
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
306
                                switch (abortState)
307
                                switch (abortState)
307
                                {
308
                                {
308
                                        case 0:
309
                                        case 0:
309
                                                if (c == 27) abortState++;
310
                                                if (c == 27) abortState++;
310
                                                break;
311
                                                break;
311
                                        case 1:
312
                                        case 1:
312
                                                if (c == 27) abortState++;
313
                                                if (c == 27) abortState++;
313
                                                else abortState = 0;
314
                                                else abortState = 0;
314
                                        break;
315
                                        break;
315
                                        case 2:
316
                                        case 2:
316
                                                if (c == 0x55) abortState++;
317
                                                if (c == 0x55) abortState++;
317
                                                else abortState = 0;
318
                                                else abortState = 0;
318
                                                break;
319
                                                break;
319
                                        case 3:
320
                                        case 3:
320
                                                if (c == 0xAA) abortState++;
321
                                                if (c == 0xAA) abortState++;
321
                                                else abortState = 0;
322
                                                else abortState = 0;
322
                                                break;
323
                                                break;
323
                                        case 4:
324
                                        case 4:
324
                                                if (c == 0x00)
325
                                                if (c == 0x00)
325
                                                {
326
                                                {
326
                                                        if(DebugUART == UART0)
327
                                                        if(DebugUART == UART0)
327
                                                        {
328
                                                        {
328
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
329
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
329
                                                                TIMER2_Init(); // enbable servo outputs
330
                                                                TIMER2_Init(); // enbable servo outputs
330
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
331
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
331
                                                        }
332
                                                        }
332
                                                        DebugUART = UART1;
333
                                                        DebugUART = UART1;
333
                                                }
334
                                                }
334
                                                abortState = 0;
335
                                                abortState = 0;
335
                                                break;
336
                                                break;
336
                                } // end switch abort state
337
                                } // end switch abort state
337
                                // if the Debug uart is not UART1, redirect input to the Debug UART
338
                                // if the Debug uart is not UART1, redirect input to the Debug UART
338
                                if (DebugUART != UART1)
339
                                if (DebugUART != UART1)
339
                                {
340
                                {
340
                                        // wait for space in the tx buffer of the DebugUART
341
                                        // wait for space in the tx buffer of the DebugUART
341
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
342
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
342
                                        // move byte to the tx fifo of the debug uart
343
                                        // move byte to the tx fifo of the debug uart
343
                                        UART_SendData(DebugUART, c);
344
                                        UART_SendData(DebugUART, c);
344
                                }
345
                                }
345
                        }
346
                        }
346
                }
347
                }
347
                else  // DebugUART == UART1 (normal operation)
348
                else  // DebugUART == UART1 (normal operation)
348
                {
349
                {
349
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
350
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
350
                        { // some byes in the hardware fifo
351
                        { // some byes in the hardware fifo
351
                            // get byte from hardware fifo
352
                            // get byte from hardware fifo
352
                        c = UART_ReceiveData(UART1);
353
                        c = UART_ReceiveData(UART1);
353
                                // put into the software fifo
354
                                // put into the software fifo
354
                                if(!fifo_put(&UART1_rx_fifo, c))
355
                                if(!fifo_put(&UART1_rx_fifo, c))
355
                                {       // fifo overflow
356
                                {       // fifo overflow
356
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
357
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
357
                                }
358
                                }
358
                        } // EOF while some byes in the hardware fifo
359
                        } // EOF while some byes in the hardware fifo
359
                } // eof DebugUart = UART1
360
                } // eof DebugUart = UART1
360
        }
361
        }
361
 
362
 
362
 
363
 
363
 
364
 
364
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
365
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
365
}
366
}
366
 
367
 
367
/**************************************************************/
368
/**************************************************************/
368
/* Process incomming data from debug uart                     */
369
/* Process incomming data from debug uart                     */
369
/**************************************************************/
370
/**************************************************************/
370
void UART1_ProcessRxData(void)
371
void UART1_ProcessRxData(void)
371
{
372
{
372
        // return on forwarding uart  or unlocked rx buffer
373
        // return on forwarding uart  or unlocked rx buffer
373
        u8 c;
374
        u8 c;
374
        if(DebugUART != UART1) return;
375
        if(DebugUART != UART1) return;
375
        // if rx buffer is not locked
376
        // if rx buffer is not locked
376
        if(UART1_rx_buffer.Locked == FALSE)
377
        if(UART1_rx_buffer.Locked == FALSE)
377
        {
378
        {
378
                //collect data from primary rx fifo
379
                //collect data from primary rx fifo
379
                while(fifo_get(&UART1_rx_fifo, &c))
380
                while(fifo_get(&UART1_rx_fifo, &c))
380
                {
381
                {
381
                        // break if complete frame is collected
382
                        // break if complete frame is collected
382
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
383
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
383
                }
384
                }
384
        }
385
        }
385
        if(UART1_rx_buffer.Locked == FALSE) return;
386
        if(UART1_rx_buffer.Locked == FALSE) return;
386
 
387
 
387
        Point_t * pPoint = NULL;
388
        Point_t * pPoint = NULL;
388
        SerialMsg_t SerialMsg;
389
        SerialMsg_t SerialMsg;
389
 
390
 
390
        // analyze header first
391
        // analyze header first
391
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
392
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
392
        if( SerialMsg.Address == FC_ADDRESS )
393
        if( SerialMsg.Address == FC_ADDRESS )
393
        {
394
        {
394
                switch(SerialMsg.CmdID)
395
                switch(SerialMsg.CmdID)
395
                {
396
                {
396
//                      case 'v': // version
397
//                      case 'v': // version
397
                        case 'b': // extern control
398
                        case 'b': // extern control
398
                                          UART1_ExternalControlConfirmFrame = 1;
399
                                          UART1_ExternalControlConfirmFrame = 1;
399
                        case 'y': // serial poti values
400
                        case 'y': // serial poti values
400
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
401
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
401
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
402
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
402
                                return; //end process rx data
403
                                return; //end process rx data
403
                        break;
404
                        break;
404
                }
405
                }
405
        }
406
        }
406
 
407
 
407
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
408
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
408
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
409
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
409
        switch(SerialMsg.Address) // check for Slave Address
410
        switch(SerialMsg.Address) // check for Slave Address
410
        {
411
        {
411
                case NC_ADDRESS:  // own Slave Address
412
                case NC_ADDRESS:  // own Slave Address
412
                switch(SerialMsg.CmdID)
413
                switch(SerialMsg.CmdID)
413
                {
414
                {
414
                        case 't': // request for the GPS time 
415
                        case 't': // request for the GPS time 
415
                                UART1_Request_SystemTime  = TRUE;
416
                                UART1_Request_SystemTime  = TRUE;
416
                                break;
417
                                break;
417
 
418
 
418
                        case 'f': // ftp command
419
                        case 'f': // ftp command
419
                                UART1_Request_FTP = SerialMsg.pData[0];
420
                                UART1_Request_FTP = SerialMsg.pData[0];
420
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
421
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
421
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
422
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
422
                        break;
423
                        break;
423
 
424
 
424
                        case 'z': // connection checker
425
                        case 'z': // connection checker
425
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
426
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
426
                                UART1_Request_Echo = TRUE;
427
                                UART1_Request_Echo = TRUE;
427
                                break;
428
                                break;
428
 
429
 
429
                        case 'e': // request for the text of the error status
430
                        case 'e': // request for the text of the error status
430
                                UART1_Request_ErrorMessage = TRUE;
431
                                UART1_Request_ErrorMessage = TRUE;
431
                                break;
432
                                break;
432
 
433
 
433
                        case 's'://  new target position
434
                        case 's'://  new target position
434
                                pPoint = (Point_t*)SerialMsg.pData;
435
                                pPoint = (Point_t*)SerialMsg.pData;
435
                                if(pPoint->Position.Status == NEWDATA)
436
                                if(pPoint->Position.Status == NEWDATA)
436
                                {
437
                                {
437
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
438
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
438
                                        //pPoint->Index = 1; // must be one after empty list
439
                                        //pPoint->Index = 1; // must be one after empty list
439
                                        PointList_SetAt(pPoint);
440
                                        PointList_SetAt(pPoint);
440
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
441
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
441
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
442
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
442
                                        if(GPS_pWaypoint != NULL) // if new WP exist
443
                                        if(GPS_pWaypoint != NULL) // if new WP exist
443
                                        {   // update WP hold time stamp immediately!
444
                                        {   // update WP hold time stamp immediately!
444
                                                EnableNewWpHeading();
445
                                                EnableNewWpHeading();
445
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
446
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
446
                                                {
447
                                                {
447
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
448
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
448
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
449
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
449
                                                }
450
                                                }
450
*/
451
*/
451
                                        }
452
                                        }
452
                                        BeepTime = 50;
453
                                        BeepTime = 50;
453
                                }
454
                                }
454
                                break;
455
                                break;
455
 
456
 
456
                        case 'u': // redirect debug uart
457
                        case 'u': // redirect debug uart
457
                                switch(SerialMsg.pData[0])
458
                                switch(SerialMsg.pData[0])
458
                                {
459
                                {
459
                                        case UART_FLIGHTCTRL:
460
                                        case UART_FLIGHTCTRL:
460
                                                UART2_Init();                           // initialize UART2 to FC pins
461
                                                UART2_Init();                           // initialize UART2 to FC pins
461
                                                fifo_purge(&UART1_rx_fifo);
462
                                                fifo_purge(&UART1_rx_fifo);
462
                                                TIMER2_Deinit();                        // reduce irq load
463
                                                TIMER2_Deinit();                        // reduce irq load
463
                                                DebugUART = UART2;
464
                                                DebugUART = UART2;
464
                                                break;
465
                                                break;
465
                                        case UART_MK3MAG:
466
                                        case UART_MK3MAG:
466
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
467
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
467
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
468
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
468
                                                GPSData.Status = INVALID;
469
                                                GPSData.Status = INVALID;
469
                                                fifo_purge(&UART1_rx_fifo);
470
                                                fifo_purge(&UART1_rx_fifo);
470
                                                DebugUART = UART0;
471
                                                DebugUART = UART0;
471
                                                break;
472
                                                break;
472
                                        case UART_MKGPS:
473
                                        case UART_MKGPS:
473
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
474
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
474
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
475
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
475
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
476
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
476
                                                GPSData.Status = INVALID;
477
                                                GPSData.Status = INVALID;
477
                                                fifo_purge(&UART1_rx_fifo);
478
                                                fifo_purge(&UART1_rx_fifo);
478
                                                DebugUART = UART0;
479
                                                DebugUART = UART0;
479
                                                break;
480
                                                break;
480
                                        default:
481
                                        default:
481
                                                break;
482
                                                break;
482
                                }
483
                                }
483
                                break;
484
                                break;
484
 
485
 
485
                        case 'w'://  Set point in list at index
486
                        case 'w'://  Set point in list at index
486
                                {
487
                                {
487
                                        pPoint = (Point_t*)SerialMsg.pData;
488
                                        pPoint = (Point_t*)SerialMsg.pData;
488
 
489
 
489
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
490
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
490
                                        {
491
                                        {
491
                                                PointList_Clear();
492
                                                PointList_Clear();
492
                                                GPS_pWaypoint = PointList_WPBegin();
493
                                                GPS_pWaypoint = PointList_WPBegin();
493
                                                UART1_Request_WritePoint = 0; // return new point count
494
                                                UART1_Request_WritePoint = 0; // return new point count
494
                                        }
495
                                        }
495
                                        else
496
                                        else
496
                                        {  // update WP in list at index
497
                                        {  // update WP in list at index
497
                                                if(pPoint->Index > MaxNumberOfWaypoints)
498
                                                if(pPoint->Index > MaxNumberOfWaypoints)
498
                                                 {
499
                                                 {
499
                                                  UART1_Request_WritePoint = 254;
500
                                                  UART1_Request_WritePoint = 254;
500
                                                  pPoint->Index = MaxNumberOfWaypoints;
501
                                                  pPoint->Index = MaxNumberOfWaypoints;
501
                                                 }
502
                                                 }
502
                                                 else
503
                                                 else
503
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
504
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
504
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
505
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
505
                                                if(UART1_Request_WritePoint == pPoint->Index)
506
                                                if(UART1_Request_WritePoint == pPoint->Index)
506
                                                {
507
                                                {
507
                                                        BeepTime = 500;
508
                                                        BeepTime = 500;
508
                                                }
509
                                                }
509
                                        }
510
                                        }
510
                                }
511
                                }
511
                                break;
512
                                break;
512
 
513
 
513
                        case 'x'://  Read Waypoint from List
514
                        case 'x'://  Read Waypoint from List
514
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
515
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
515
                                break;
516
                                break;
516
 
517
 
517
                        case 'j':// Set/Get NC-Parameter
518
                        case 'j':// Set/Get NC-Parameter
518
                                switch(SerialMsg.pData[0])
519
                                switch(SerialMsg.pData[0])
519
                                {
520
                                {
520
                                        case 0: // get
521
                                        case 0: // get
521
                                        break;
522
                                        break;
522
 
523
 
523
                                        case 1: // set
524
                                        case 1: // set
524
                                        {
525
                                        {
525
                                                s16 value;
526
                                                s16 value;
526
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
527
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
527
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
528
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
528
                                        }
529
                                        }
529
                                        break;
530
                                        break;
530
 
531
 
531
                                        default:
532
                                        default:
532
                                        break;
533
                                        break;
533
                                }
534
                                }
534
                                UART1_Request_ParameterId = SerialMsg.pData[1];
535
                                UART1_Request_ParameterId = SerialMsg.pData[1];
535
                                UART1_Request_Parameter = TRUE;
536
                                UART1_Request_Parameter = TRUE;
536
                                break;
537
                                break;
537
                        default:
538
                        default:
538
                                // unsupported command recieved
539
                                // unsupported command recieved
539
                                break;
540
                                break;
540
                } // case NC_ADDRESS
541
                } // case NC_ADDRESS
541
                // "break;" is missing here to fall thru to the common commands
542
                // "break;" is missing here to fall thru to the common commands
542
 
543
 
543
                default:  // and any other Slave Address
544
                default:  // and any other Slave Address
544
 
545
 
545
                switch(SerialMsg.CmdID) // check CmdID
546
                switch(SerialMsg.CmdID) // check CmdID
546
                {
547
                {
547
                        case 'a':// request for the labels of the analog debug outputs
548
                        case 'a':// request for the labels of the analog debug outputs
548
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
549
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
549
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
550
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
550
                                break;
551
                                break;
551
                        /*
552
                        /*
552
                        case 'b': // submit extern control
553
                        case 'b': // submit extern control
553
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
554
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
554
                                UART1_ConfirmFrame = ExternControl.Frame;
555
                                UART1_ConfirmFrame = ExternControl.Frame;
555
                                break;
556
                                break;
556
                        */
557
                        */
557
                        case 'd': // request for debug data;
558
                        case 'd': // request for debug data;
558
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
559
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
559
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
560
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
560
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
561
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
561
                                break;
562
                                break;
562
 
563
 
563
                        case 'c': // request for 3D data;
564
                        case 'c': // request for 3D data;
564
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
565
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
565
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
566
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
566
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
567
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
567
                                break;
568
                                break;
568
 
569
 
569
                        case 'k': // request for Motor data;
570
                        case 'k': // request for Motor data;
570
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
571
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
571
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
572
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
572
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
573
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
573
                                break;
574
                                break;
574
 
575
 
575
                        case 'h':// reqest for display line
576
                        case 'h':// reqest for display line
576
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
577
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
577
                                {
578
                                {
578
                                        UART1_DisplayLine = 2;
579
                                        UART1_DisplayLine = 2;
579
                                        UART1_Display_Interval = 0;
580
                                        UART1_Display_Interval = 0;
580
                                }
581
                                }
581
                                else
582
                                else
582
                                {
583
                                {
583
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
584
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
584
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
585
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
585
                                        UART1_DisplayLine = 4;
586
                                        UART1_DisplayLine = 4;
586
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
587
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
587
                                }
588
                                }
588
                                UART1_Request_Display = TRUE;
589
                                UART1_Request_Display = TRUE;
589
                                break;
590
                                break;
590
 
591
 
591
                        case 'l':// reqest for display columns
592
                        case 'l':// reqest for display columns
592
                                MenuItem = SerialMsg.pData[0];
593
                                MenuItem = SerialMsg.pData[0];
593
                                UART1_Request_Display1 = TRUE;
594
                                UART1_Request_Display1 = TRUE;
594
                                break;
595
                                break;
595
 
596
 
596
                        case 'o': // request for navigation information
597
                        case 'o': // request for navigation information
597
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
598
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
598
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
599
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
599
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
600
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
600
                                break;
601
                                break;
601
 
602
 
602
                        case 'v': // request for version info
603
                        case 'v': // request for version info
603
                                UART1_Request_VersionInfo = TRUE;
604
                                UART1_Request_VersionInfo = TRUE;
604
                                break;
605
                                break;
605
                        default:
606
                        default:
606
                                // unsupported command recieved
607
                                // unsupported command recieved
607
                                break;
608
                                break;
608
                }
609
                }
609
                break; // default:
610
                break; // default:
610
        }
611
        }
611
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
612
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
612
}
613
}
613
 
614
 
614
 
615
 
615
/*****************************************************/
616
/*****************************************************/
616
/*                   Send a character                */
617
/*                   Send a character                */
617
/*****************************************************/
618
/*****************************************************/
618
s16 UART1_Putchar(char c)
619
s16 UART1_Putchar(char c)
619
{
620
{
620
        u32 timeout = 10000;
621
        u32 timeout = 10000;
621
        if (c == '\n') UART1_Putchar('\r');
622
        if (c == '\n') UART1_Putchar('\r');
622
        // wait until txd fifo is not full
623
        // wait until txd fifo is not full
623
        while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET) if(--timeout == 0) return(0);
624
        while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET) if(--timeout == 0) return(0);
624
        // transmit byte
625
        // transmit byte
625
        UART_SendData(UART1, c);
626
        UART_SendData(UART1, c);
626
        #ifdef FOLLOW_ME
627
        #ifdef FOLLOW_ME
627
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
628
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
628
        #endif
629
        #endif
629
        return (0);
630
        return (0);
630
}
631
}
631
 
632
 
632
/*****************************************************/
633
/*****************************************************/
633
/*       Send a string to the debug uart              */
634
/*       Send a string to the debug uart              */
634
/*****************************************************/
635
/*****************************************************/
635
void UART1_PutString(u8 *s)
636
void UART1_PutString(u8 *s)
636
{
637
{
637
        if(s == NULL) return;
638
        if(s == NULL) return;
638
        while (*s != '\0' && DebugUART == UART1)
639
        while (*s != '\0' && DebugUART == UART1)
639
        {
640
        {
640
                UART1_Putchar(*s);
641
                UART1_Putchar(*s);
641
                s ++;
642
                s ++;
642
        }
643
        }
643
}
644
}
644
 
645
 
645
 
646
 
646
/**************************************************************/
647
/**************************************************************/
647
/*         Transmit tx buffer via debug uart                  */
648
/*         Transmit tx buffer via debug uart                  */
648
/**************************************************************/
649
/**************************************************************/
649
void UART1_Transmit(void)
650
void UART1_Transmit(void)
650
{
651
{
651
        u8 tmp_tx;
652
        u8 tmp_tx;
652
        if(DebugUART != UART1) return;
653
        if(DebugUART != UART1) return;
653
        // if something has to be send and the txd fifo is not full
654
        // if something has to be send and the txd fifo is not full
654
        if(UART1_tx_buffer.Locked == TRUE)
655
        if(UART1_tx_buffer.Locked == TRUE)
655
        {
656
        {
656
                // while there is some space in the tx fifo
657
                // while there is some space in the tx fifo
657
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
658
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
658
                {
659
                {
659
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
660
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
660
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
661
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
661
                        #ifdef FOLLOW_ME
662
                        #ifdef FOLLOW_ME
662
                        if(TransmitAlsoToFC)
663
                        if(TransmitAlsoToFC)
663
                        {
664
                        {
664
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
665
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
665
                        }
666
                        }
666
                        #endif
667
                        #endif
667
                        // if terminating character or end of txd buffer reached
668
                        // if terminating character or end of txd buffer reached
668
                        if((tmp_tx == '\0') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
669
                        if((tmp_tx == '\0') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
669
                        {
670
                        {
670
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
671
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
671
                                #ifdef FOLLOW_ME
672
                                #ifdef FOLLOW_ME
672
                                TransmitAlsoToFC = 0;
673
                                TransmitAlsoToFC = 0;
673
                                #endif
674
                                #endif
674
                                break; // end while loop
675
                                break; // end while loop
675
                        }
676
                        }
676
                }
677
                }
677
        }
678
        }
678
}
679
}
679
 
680
 
680
//$GPGGA,HHMMSS.ss,BBBB.BBBB,b,LLLLL.LLLL,l,Q,NN,D.D,H.H,h,G.G,g,A.A,RRRR*PP
681
//$GPGGA,HHMMSS.ss,BBBB.BBBB,b,LLLLL.LLLL,l,Q,NN,D.D,H.H,h,G.G,g,A.A,RRRR*PP
681
//$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5,M,48.0,M,,*45
682
//$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5,M,48.0,M,,*45
682
//$GPGGA,092120.20,,,,,0,00,99.99,,,,,,*6C
683
//$GPGGA,092120.20,,,,,0,00,99.99,,,,,,*6C
683
void CreateNmeaGGA(void)
684
void CreateNmeaGGA(void)
684
{
685
{
685
 unsigned char array[100], i = 0, crc = 0, x;
686
 unsigned char array[100], i = 0, crc = 0, x;
686
 long tmp1, tmp2;
687
 long tmp1, tmp2;
687
 i += sprintf(array, "$GPGGA,");
688
 i += sprintf(array, "$GPGGA,");
688
// +++++++++++++++++++++++++++++++++++++++++++
689
// +++++++++++++++++++++++++++++++++++++++++++
689
 if(SystemTime.Valid)
690
 if(SystemTime.Valid)
690
  {
691
  {
691
   i += sprintf(&array[i], "%02d%02d%02d.%02d,",SystemTime.Hour,SystemTime.Min,SystemTime.Sec,SystemTime.mSec/10);
692
   i += sprintf(&array[i], "%02d%02d%02d.%02d,",SystemTime.Hour,SystemTime.Min,SystemTime.Sec,SystemTime.mSec/10);
692
  }
693
  }
693
 else
694
 else
694
  {
695
  {
695
  i += sprintf(&array[i], ",");
696
  i += sprintf(&array[i], ",");
696
  }
697
  }
697
// +++++++++++++++++++++++++++++++++++++++++++
698
// +++++++++++++++++++++++++++++++++++++++++++
698
 if(GPSData.Flags & FLAG_GPSFIXOK)
699
 if(GPSData.Flags & FLAG_GPSFIXOK)
699
 {
700
 {
700
  tmp1 = abs(GPSData.Position.Latitude)/10000000L;
701
  tmp1 = abs(GPSData.Position.Latitude)/10000000L;
701
  i += sprintf(&array[i],"%02d",tmp1);
702
  i += sprintf(&array[i],"%02d",tmp1);
702
 
703
 
703
  tmp1 = abs(GPSData.Position.Latitude)%10000000L;
704
  tmp1 = abs(GPSData.Position.Latitude)%10000000L;
704
  tmp1 *= 6; // in Minuten
705
  tmp1 *= 6; // in Minuten
705
  tmp2 = tmp1 / 1000000L;
706
  tmp2 = tmp1 / 1000000L;
706
  i += sprintf(&array[i],"%02d",tmp2);
707
  i += sprintf(&array[i],"%02d",tmp2);
707
  tmp2 = tmp1 % 1000000L;
708
  tmp2 = tmp1 % 1000000L;
708
  tmp2 /= 10; // eine Stelle zu viel
709
  tmp2 /= 10; // eine Stelle zu viel
709
  i += sprintf(&array[i],".%05d,",tmp2);
710
  i += sprintf(&array[i],".%05d,",tmp2);
710
 
711
 
711
  if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
712
  if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
712
  else i += sprintf(&array[i],"S,");
713
  else i += sprintf(&array[i],"S,");
713
// +++++++++++++++++++++++++++++++++++++++++++
714
// +++++++++++++++++++++++++++++++++++++++++++
714
 
715
 
715
  tmp1 = abs(GPSData.Position.Longitude)/10000000L;
716
  tmp1 = abs(GPSData.Position.Longitude)/10000000L;
716
  i += sprintf(&array[i],"%03d",tmp1);
717
  i += sprintf(&array[i],"%03d",tmp1);
717
 
718
 
718
  tmp1 = abs(GPSData.Position.Longitude)%10000000L;
719
  tmp1 = abs(GPSData.Position.Longitude)%10000000L;
719
  tmp1 *= 6; // in Minuten
720
  tmp1 *= 6; // in Minuten
720
  tmp2 = tmp1 / 1000000L;
721
  tmp2 = tmp1 / 1000000L;
721
  i += sprintf(&array[i],"%02d",tmp2);
722
  i += sprintf(&array[i],"%02d",tmp2);
722
  tmp2 = tmp1 % 1000000L;
723
  tmp2 = tmp1 % 1000000L;
723
  tmp2 /= 10; // eine Stelle zu viel
724
  tmp2 /= 10; // eine Stelle zu viel
724
  i += sprintf(&array[i],".%05d,",tmp2);
725
  i += sprintf(&array[i],".%05d,",tmp2);
725
 
726
 
726
 
727
 
727
  if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
728
  if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
728
  else i += sprintf(&array[i],"W,");
729
  else i += sprintf(&array[i],"W,");
729
  i += sprintf(&array[i],"%d,",GPSData.SatFix);
730
  i += sprintf(&array[i],"%d,",GPSData.SatFix);
730
  i += sprintf(&array[i],"%d,",GPSData.NumOfSats);
731
  i += sprintf(&array[i],"%d,",GPSData.NumOfSats);
731
  i += sprintf(&array[i],"%d.%d,",GPSData.Position_Accuracy/100,abs(GPSData.Position_Accuracy%100));
732
  i += sprintf(&array[i],"%d.%d,",GPSData.Position_Accuracy/100,abs(GPSData.Position_Accuracy%100));
732
//  i += sprintf(&array[i],"%d.%d,M,",GPSData.Position.Altitude/1000,abs(GPSData.Position.Altitude%1000)/100);
733
//  i += sprintf(&array[i],"%d.%d,M,",GPSData.Position.Altitude/1000,abs(GPSData.Position.Altitude%1000)/100);
733
 tmp1 = NaviData.Altimeter / 2; // in dm
734
 tmp1 = NaviData.Altimeter / 2; // in dm
734
  i += sprintf(&array[i],"%d.%d,M,",tmp1 / 10,abs(tmp1 % 10));
735
  i += sprintf(&array[i],"%d.%d,M,",tmp1 / 10,abs(tmp1 % 10));
735
  i += sprintf(&array[i],",,,*");
736
  i += sprintf(&array[i],",,,*");
736
 }
737
 }
737
 else
738
 else
738
  {
739
  {
739
   i += sprintf(&array[i], ",,,,%d,00,99.99,,,,,,*",GPSData.NumOfSats);
740
   i += sprintf(&array[i], ",,,,%d,00,99.99,,,,,,*",GPSData.NumOfSats);
740
  }
741
  }
741
 for(x=1; x<i-1; x++)
742
 for(x=1; x<i-1; x++)
742
  {
743
  {
743
   crc ^= array[x];
744
   crc ^= array[x];
744
  }
745
  }
745
  i += sprintf(&array[i], "%02x%c%c",crc,0x0d,0x0a);
746
  i += sprintf(&array[i], "%02x%c%c",crc,0x0d,0x0a);
746
  AddSerialData(&UART1_tx_buffer,array,i);
747
  AddSerialData(&UART1_tx_buffer,array,i);
747
 
748
 
748
// +++++++++++++++++++++++++++++++++++++++++++
749
// +++++++++++++++++++++++++++++++++++++++++++
749
/*
750
/*
750
 
751
 
751
 
752
 
752
 
753
 
753
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
754
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
754
                                GPSData.NumOfSats =                     UbxSol.numSV;
755
                                GPSData.NumOfSats =                     UbxSol.numSV;
755
                                GPSData.SatFix =                                UbxSol.GPSfix;
756
                                GPSData.SatFix =                                UbxSol.GPSfix;
756
                                GPSData.Position_Accuracy =             UbxSol.PAcc;
757
                                GPSData.Position_Accuracy =             UbxSol.PAcc;
757
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;
758
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;
758
                                SetGPSTime(&SystemTime); // update system time
759
                                SetGPSTime(&SystemTime); // update system time
759
                                // NAV POSLLH
760
                                // NAV POSLLH
760
                                GPSData.Position.Status =               INVALID;
761
                                GPSData.Position.Status =               INVALID;
761
                                GPSData.Position.Longitude =    UbxPosLlh.LON;
762
                                GPSData.Position.Longitude =    UbxPosLlh.LON;
762
                                GPSData.Position.Latitude =     UbxPosLlh.LAT;
763
                                GPSData.Position.Latitude =     UbxPosLlh.LAT;
763
                                GPSData.Position.Altitude =     UbxPosLlh.HMSL;
764
                                GPSData.Position.Altitude =     UbxPosLlh.HMSL;
764
                                GPSData.Position.Status =               NEWDATA;
765
                                GPSData.Position.Status =               NEWDATA;
765
                                // NAV VELNED
766
                                // NAV VELNED
766
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;
767
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;
767
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;
768
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;
768
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;
769
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;
769
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;
770
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;
770
                                GPSData.Heading =                               UbxVelNed.Heading;
771
                                GPSData.Heading =                               UbxVelNed.Heading;
771
        SystemTime.Year = 0;
772
        SystemTime.Year = 0;
772
        SystemTime.Month = 0;
773
        SystemTime.Month = 0;
773
        SystemTime.Day = 0;
774
        SystemTime.Day = 0;
774
        SystemTime.Hour = 0;
775
        SystemTime.Hour = 0;
775
        SystemTime.Min = 0;
776
        SystemTime.Min = 0;
776
        SystemTime.Sec = 0;
777
        SystemTime.Sec = 0;
777
        SystemTime.mSec = 0;
778
        SystemTime.mSec = 0;
778
        SystemTime.Valid = 0;
779
        SystemTime.Valid = 0;
779
       
780
       
780
                                FromFlightCtrl.GyroHeading / 10;//NaviData.HomePositionDeviation.Bearing / 2;
781
                                FromFlightCtrl.GyroHeading / 10;//NaviData.HomePositionDeviation.Bearing / 2;
781
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
782
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
782
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
783
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
783
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
784
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
784
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
785
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
785
 
786
 
786
 
787
 
787
 
788
 
788
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
789
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
789
                                        i1 *= 100;
790
                                        i1 *= 100;
790
                                        i1 += i2 / 100000;
791
                                        i1 += i2 / 100000;
791
                                        i2  = i2 % 100000;
792
                                        i2  = i2 % 100000;
792
                                        i2 /= 10;
793
                                        i2 /= 10;
793
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
794
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
794
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
795
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
795
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
796
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
796
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
797
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
797
                                        break;
798
                                        break;
798
                                case 1:
799
                                case 1:
799
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
800
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
800
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
801
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
801
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
802
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
802
                                        //-----------------------------
803
                                        //-----------------------------
803
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
804
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
804
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
805
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
805
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
806
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
806
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
807
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
807
 
808
 
808
*/
809
*/
809
}
810
}
810
 
811
 
811
 
812
 
812
/**************************************************************/
813
/**************************************************************/
813
/* Send the answers to incomming commands at the debug uart   */
814
/* Send the answers to incomming commands at the debug uart   */
814
/**************************************************************/
815
/**************************************************************/
815
void UART1_TransmitTxData(void)
816
void UART1_TransmitTxData(void)
816
{
817
{
817
static u8 motorindex1 = 255, motorindex2 = 0;
818
static u8 motorindex1 = 255, motorindex2 = 0;
818
        if(DebugUART != UART1) return;
819
        if(DebugUART != UART1) return;
819
 
820
 
820
        if(CheckDelay(UART1_AboTimeOut))
821
        if(CheckDelay(UART1_AboTimeOut))
821
        {
822
        {
822
                UART1_DebugData_Interval = 0;
823
                UART1_DebugData_Interval = 0;
823
                UART1_NaviData_Interval = 0;
824
                UART1_NaviData_Interval = 0;
824
                UART1_Data3D_Interval = 0;
825
                UART1_Data3D_Interval = 0;
825
                UART1_Display_Interval = 0;
826
                UART1_Display_Interval = 0;
826
                UART1_MotorData_Interval = 0;
827
                UART1_MotorData_Interval = 0;
827
        }
828
        }
828
 
829
 
829
        UART1_Transmit(); // output pending bytes in tx buffer
830
        UART1_Transmit(); // output pending bytes in tx buffer
830
        if((UART1_tx_buffer.Locked == TRUE)) return;
831
        if((UART1_tx_buffer.Locked == TRUE)) return;
831
 
832
 
832
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
833
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
833
        {
834
        {
834
                s16 ParamValue;
835
                s16 ParamValue;
835
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
836
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
836
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'J', NC_ADDRESS, 2, &UART1_Request_ParameterId, sizeof(UART1_Request_ParameterId), &ParamValue, sizeof(ParamValue)); // answer the param request
837
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'J', NC_ADDRESS, 2, &UART1_Request_ParameterId, sizeof(UART1_Request_ParameterId), &ParamValue, sizeof(ParamValue)); // answer the param request
837
                UART1_Request_Parameter = FALSE;
838
                UART1_Request_Parameter = FALSE;
838
        }
839
        }
839
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
840
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
840
        {
841
        {
841
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
842
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
842
                Echo = 0; // reset echo value
843
                Echo = 0; // reset echo value
843
                UART1_Request_Echo = FALSE;
844
                UART1_Request_Echo = FALSE;
844
        }
845
        }
845
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
846
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
846
        {
847
        {
847
                u8 errorcode = FTP_ERROR_NONE;
848
                u8 errorcode = FTP_ERROR_NONE;
848
        if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) errorcode = FTP_ERROR_MOTOR_RUN;
849
        if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) errorcode = FTP_ERROR_MOTOR_RUN;
849
                else if (!Partition.IsValid) errorcode = FTP_ERROR_NO_SDCARD;
850
                else if (!Partition.IsValid) errorcode = FTP_ERROR_NO_SDCARD;
850
 
851
 
851
                if (!errorcode) CheckFTPCommand(UART1_Request_FTP);
852
                if (!errorcode) CheckFTPCommand(UART1_Request_FTP);
852
                else
853
                else
853
                {
854
                {
854
                        u8 cmd = FTP_CMD_ERROR;
855
                        u8 cmd = FTP_CMD_ERROR;
855
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &cmd, 1, &errorcode, 1);
856
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &cmd, 1, &errorcode, 1);
856
                }
857
                }
857
 
858
 
858
                UART1_Request_FTP = FALSE;
859
                UART1_Request_FTP = FALSE;
859
        }
860
        }
860
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
861
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
861
        {
862
        {
862
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
863
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
863
                UART1_Request_WritePoint = 0xFF;
864
                UART1_Request_WritePoint = 0xFF;
864
        }
865
        }
865
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
866
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
866
        {
867
        {
867
                u8 PointCount = PointList_GetCount();
868
                u8 PointCount = PointList_GetCount();
868
                if (UART1_Request_ReadPoint <= PointCount)
869
                if (UART1_Request_ReadPoint <= PointCount)
869
                {
870
                {
870
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
871
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
871
                }
872
                }
872
                else
873
                else
873
                {
874
                {
874
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
875
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
875
                }
876
                }
876
                UART1_Request_ReadPoint = 0;
877
                UART1_Request_ReadPoint = 0;
877
        }
878
        }
878
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
879
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
879
        {
880
        {
880
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
881
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
881
                UART1_Request_DebugLabel = 0xFF;
882
                UART1_Request_DebugLabel = 0xFF;
882
        }
883
        }
883
        else if(UART1_ExternalControlConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
884
        else if(UART1_ExternalControlConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
884
        {
885
        {
885
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1,(u8 *)&UART1_ExternalControlConfirmFrame, sizeof(UART1_ExternalControlConfirmFrame));
886
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1,(u8 *)&UART1_ExternalControlConfirmFrame, sizeof(UART1_ExternalControlConfirmFrame));
886
                UART1_ExternalControlConfirmFrame = 0;
887
                UART1_ExternalControlConfirmFrame = 0;
887
        }
888
        }
888
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
889
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
889
        {
890
        {
890
                NaviData.Errorcode = ErrorCode;
891
                NaviData.Errorcode = ErrorCode;
891
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData, sizeof(NaviData));
892
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData, sizeof(NaviData));
892
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
893
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
893
                UART1_Request_NaviData = FALSE;
894
                UART1_Request_NaviData = FALSE;
894
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
895
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
895
        }
896
        }
896
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
897
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
897
        {
898
        {
898
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
899
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
899
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
900
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
900
                UART1_Request_DebugData = FALSE;
901
                UART1_Request_DebugData = FALSE;
901
        }
902
        }
902
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
903
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
903
        {
904
        {
904
                Data3D.StickNick = FC.StickNick;
905
                Data3D.StickNick = FC.StickNick;
905
                Data3D.StickRoll = FC.StickRoll;
906
                Data3D.StickRoll = FC.StickRoll;
906
        Data3D.StickYaw = FC.StickYaw;
907
        Data3D.StickYaw = FC.StickYaw;
907
        Data3D.StickGas = FC.StickGas;
908
        Data3D.StickGas = FC.StickGas;
908
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
909
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
909
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
910
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
910
                UART1_Request_Data3D = FALSE;
911
                UART1_Request_Data3D = FALSE;
911
        }
912
        }
912
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
913
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
913
        {
914
        {
914
                do
915
                do
915
                {
916
                {
916
                 motorindex1++;
917
                 motorindex1++;
917
                 motorindex1%=12;
918
                 motorindex1%=12;
918
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
919
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
919
                 if(motorindex1 == motorindex2) break;
920
                 if(motorindex1 == motorindex2) break;
920
                }
921
                }
921
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
922
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
922
 
923
 
923
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
924
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
924
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
925
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
925
                UART1_Request_MotorData = FALSE;
926
                UART1_Request_MotorData = FALSE;
926
        }
927
        }
927
        else if((((NMEA_Interval > 0) && CheckDelay(NMEA_Timer))) && (UART1_tx_buffer.Locked == FALSE))
928
        else if((((NMEA_Interval > 0) && CheckDelay(NMEA_Timer))) && (UART1_tx_buffer.Locked == FALSE))
928
        {
929
        {
929
                CreateNmeaGGA();
930
                CreateNmeaGGA();
930
                NMEA_Timer = SetDelay(NMEA_Interval);
931
                NMEA_Timer = SetDelay(NMEA_Interval);
931
        }
932
        }
932
 
933
 
933
        /*
934
        /*
934
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
935
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
935
        {
936
        {
936
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
937
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
937
                UART1_ConfirmFrame = 0;
938
                UART1_ConfirmFrame = 0;
938
        }
939
        }
939
        */
940
        */
940
        /*
941
        /*
941
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
942
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
942
        {
943
        {
943
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
944
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
944
                UART1_Request_ExternalControl = FALSE;
945
                UART1_Request_ExternalControl = FALSE;
945
        }
946
        }
946
        */
947
        */
947
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
948
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
948
        {
949
        {
949
                if(UART1_DisplayLine > 3)
950
                if(UART1_DisplayLine > 3)
950
                {
951
                {
951
                        Menu_Update(UART1_DisplayKeys);
952
                        Menu_Update(UART1_DisplayKeys);
952
                        UART1_DisplayKeys = 0;
953
                        UART1_DisplayKeys = 0;
953
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
954
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
954
                }
955
                }
955
                else
956
                else
956
                {
957
                {
957
                        UART1_DisplayLine = 2;
958
                        UART1_DisplayLine = 2;
958
                        sprintf(text,"!!! incompatible !!!");
959
                        sprintf(text,"!!! incompatible !!!");
959
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
960
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
960
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
961
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
961
                }
962
                }
962
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
963
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
963
                UART1_Request_Display = FALSE;
964
                UART1_Request_Display = FALSE;
964
        }
965
        }
965
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
966
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
966
        {
967
        {
967
                Menu_Update(0);
968
                Menu_Update(0);
968
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
969
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
969
                UART1_Request_Display1 = FALSE;
970
                UART1_Request_Display1 = FALSE;
970
        }
971
        }
971
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
972
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
972
        {
973
        {
973
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
974
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
974
                UART1_Request_VersionInfo = FALSE;
975
                UART1_Request_VersionInfo = FALSE;
975
        }
976
        }
976
        else if(UART1_Request_SystemTime && (UART1_tx_buffer.Locked == FALSE))
977
        else if(UART1_Request_SystemTime && (UART1_tx_buffer.Locked == FALSE))
977
        {
978
        {
978
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'T', NC_ADDRESS,1, (u8 *)&SystemTime, sizeof(SystemTime));
979
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'T', NC_ADDRESS,1, (u8 *)&SystemTime, sizeof(SystemTime));
979
                UART1_Request_SystemTime = FALSE;
980
                UART1_Request_SystemTime = FALSE;
980
        }
981
        }
981
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
982
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
982
        {
983
        {
983
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
984
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
984
                UART1_Request_ErrorMessage = FALSE;
985
                UART1_Request_ErrorMessage = FALSE;
985
        }
986
        }
986
#ifdef FOLLOW_ME
987
#ifdef FOLLOW_ME
987
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
988
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
988
        {
989
        {
989
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
990
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
990
                {
991
                {
991
                        TransmitAlsoToFC = 1;
992
                        TransmitAlsoToFC = 1;
992
                        // update FollowMe content
993
                        // update FollowMe content
993
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
994
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
994
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
995
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
995
                        FollowMe.Position.Status = NEWDATA;
996
                        FollowMe.Position.Status = NEWDATA;
996
                        FollowMe.Position.Altitude = 1;
997
                        FollowMe.Position.Altitude = 1;
997
                        // 0  -> no Orientation
998
                        // 0  -> no Orientation
998
                        // 1-360 -> CompassCourse Setpoint
999
                        // 1-360 -> CompassCourse Setpoint
999
                        // -1 -> points to  WP1 -> itself
1000
                        // -1 -> points to  WP1 -> itself
1000
                FollowMe.Heading = -1;
1001
                FollowMe.Heading = -1;
1001
                        FollowMe.ToleranceRadius = 1;
1002
                        FollowMe.ToleranceRadius = 1;
1002
                        FollowMe.HoldTime = 60;
1003
                        FollowMe.HoldTime = 60;
1003
                        FollowMe.Event_Flag = 1;
1004
                        FollowMe.Event_Flag = 1;
1004
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
1005
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
1005
                        FollowMe.Type = POINT_TYPE_WP;
1006
                        FollowMe.Type = POINT_TYPE_WP;
1006
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
1007
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
1007
                        FollowMe.AltitudeRate = 0;                        // do not change height
1008
                        FollowMe.AltitudeRate = 0;                        // do not change height
1008
                        FollowMe.Speed = 0;                             // rate to change the Position (0 = max)
1009
                        FollowMe.Speed = 0;                             // rate to change the Position (0 = max)
1009
                        FollowMe.CamAngle = 255;                // Camera servo angle in degree (255 -> POI-Automatic)
1010
                        FollowMe.CamAngle = 255;                // Camera servo angle in degree (255 -> POI-Automatic)
1010
                        FollowMe.Name[0] = 'F';             // Name of that point (ASCII)
1011
                        FollowMe.Name[0] = 'F';             // Name of that point (ASCII)
1011
                        FollowMe.Name[1] = 'O';             // Name of that point (ASCII)
1012
                        FollowMe.Name[1] = 'O';             // Name of that point (ASCII)
1012
                        FollowMe.Name[2] = 'L';             // Name of that point (ASCII)
1013
                        FollowMe.Name[2] = 'L';             // Name of that point (ASCII)
1013
                        FollowMe.Name[3] = 'L';             // Name of that point (ASCII)
1014
                        FollowMe.Name[3] = 'L';             // Name of that point (ASCII)
1014
                        FollowMe.reserve[0] = 0;                // reserve
1015
                        FollowMe.reserve[0] = 0;                // reserve
1015
                        FollowMe.reserve[1] = 0;                // reserve
1016
                        FollowMe.reserve[1] = 0;                // reserve
1016
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
1017
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
1017
                }
1018
                }
1018
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
1019
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
1019
        }
1020
        }
1020
#endif
1021
#endif
1021
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
1022
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
1022
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
1023
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
1023
        {
1024
        {
1024
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
1025
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
1025
                SendDebugOutput = 0;
1026
                SendDebugOutput = 0;
1026
        }
1027
        }
1027
#endif
1028
#endif
1028
        UART1_Transmit(); // output pending bytes in tx buffer
1029
        UART1_Transmit(); // output pending bytes in tx buffer
1029
}
1030
}
1030
 
1031
 
1031
 
1032