Subversion Repositories NaviCtrl

Rev

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

Rev 380 Rev 398
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 "config.h"
62
#include "config.h"
63
#include "menu.h"
63
#include "menu.h"
64
#include "GPS.h"
64
#include "GPS.h"
65
#include "i2c.h"
65
#include "i2c.h"
66
#include "uart0.h"
66
#include "uart0.h"
67
#include "uart1.h"
67
#include "uart1.h"
68
#include "uart2.h"
68
#include "uart2.h"
69
#include "timer1.h"
69
#include "timer1.h"
70
#include "timer2.h"
70
#include "timer2.h"
71
#include "analog.h"
71
#include "analog.h"
72
#include "compass.h"
72
#include "compass.h"
73
#include "waypoints.h"
73
#include "waypoints.h"
74
#include "mkprotocol.h"
74
#include "mkprotocol.h"
75
#include "params.h"
75
#include "params.h"
76
#include "fifo.h"
76
#include "fifo.h"
77
#include "debug.h"
77
#include "debug.h"
78
#include "spi_slave.h"
78
#include "spi_slave.h"
79
#include "ftphelper.h"
79
#include "ftphelper.h"
80
#include "led.h"
80
#include "led.h"
81
 
81
 
82
#define FALSE   0
82
#define FALSE   0
83
#define TRUE    1
83
#define TRUE    1
84
 
84
 
85
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
85
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
86
u32 UART1_AboTimeOut = 0;
86
u32 UART1_AboTimeOut = 0;
87
 
87
 
88
u8 UART1_Request_VersionInfo    = FALSE;
88
u8 UART1_Request_VersionInfo    = FALSE;
89
u8 UART1_Request_ExternalControl= FALSE;
89
u8 UART1_Request_ExternalControl= FALSE;
90
u8 UART1_Request_Display                = FALSE;
90
u8 UART1_Request_Display                = FALSE;
91
u8 UART1_Request_Display1               = FALSE;
91
u8 UART1_Request_Display1               = FALSE;
92
u8 UART1_Request_DebugData              = FALSE;
92
u8 UART1_Request_DebugData              = FALSE;
93
u8 UART1_Request_DebugLabel             = 255;
93
u8 UART1_Request_DebugLabel             = 255;
94
u8 UART1_Request_NaviData               = FALSE;
94
u8 UART1_Request_NaviData               = FALSE;
95
u8 UART1_Request_ErrorMessage   = FALSE;
95
u8 UART1_Request_ErrorMessage   = FALSE;
96
u8 UART1_Request_WritePoint             = 0xFF;
96
u8 UART1_Request_WritePoint             = 0xFF;
97
u8 UART1_Request_ReadPoint              = 0;
97
u8 UART1_Request_ReadPoint              = 0;
98
u8 UART1_Request_Data3D             = FALSE;
98
u8 UART1_Request_Data3D             = FALSE;
99
u8 UART1_Request_MotorData          = FALSE;
99
u8 UART1_Request_MotorData          = FALSE;
100
u8 UART1_Request_Echo               = FALSE;
100
u8 UART1_Request_Echo               = FALSE;
101
u8 UART1_Request_ParameterId    = 0;
101
u8 UART1_Request_ParameterId    = 0;
102
u8 UART1_Request_Parameter              = FALSE;
102
u8 UART1_Request_Parameter              = FALSE;
103
u8 UART1_DisplayKeys                    = 0;
103
u8 UART1_DisplayKeys                    = 0;
104
u8 UART1_DisplayLine                    = 0;
104
u8 UART1_DisplayLine                    = 0;
105
u8 UART1_ConfirmFrame                   = 0;
105
u8 UART1_ConfirmFrame                   = 0;
106
u8 UART1_Request_FTP                    = FALSE;
106
u8 UART1_Request_FTP                    = FALSE;
107
u8 LastTransmittedFCStatusFlags2 = 0;
107
u8 LastTransmittedFCStatusFlags2 = 0;
-
 
108
u8 UART1_ExternalControlConfirmFrame = FALSE;
108
 
109
 
109
UART_TypeDef *DebugUART = UART1;
110
UART_TypeDef *DebugUART = UART1;
110
 
111
 
111
#ifdef FOLLOW_ME
112
#ifdef FOLLOW_ME
112
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
113
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
113
u32 UART1_FollowMe_Timer        = 0;
114
u32 UART1_FollowMe_Timer        = 0;
114
Point_t FollowMe;
115
Point_t FollowMe;
115
#endif
116
#endif
116
 
117
 
117
// the primary rx fifo
118
// the primary rx fifo
118
#define UART1_RX_FIFO_LEN 1024
119
#define UART1_RX_FIFO_LEN 1024
119
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
120
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
120
fifo_t UART1_rx_fifo;
121
fifo_t UART1_rx_fifo;
121
 
122
 
122
// the rx buffer
123
// the rx buffer
123
#define UART1_RX_BUFFER_LEN  1024
124
#define UART1_RX_BUFFER_LEN  1024
124
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
125
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
125
Buffer_t UART1_rx_buffer;
126
Buffer_t UART1_rx_buffer;
126
 
127
 
127
// the tx buffer
128
// the tx buffer
128
#define UART1_TX_BUFFER_LEN  1024
129
#define UART1_TX_BUFFER_LEN  1024
129
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
130
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
130
Buffer_t UART1_tx_buffer;
131
Buffer_t UART1_tx_buffer;
131
 
132
 
132
volatile u8 SerialLinkOkay = 0;
133
volatile u8 SerialLinkOkay = 0;
133
 
134
 
134
u8 text[200];
135
u8 text[200];
135
 
136
 
136
const u8 ANALOG_LABEL[32][16] =
137
const u8 ANALOG_LABEL[32][16] =
137
{
138
{
138
   //1234567890123456
139
   //1234567890123456
139
        "AngleNick       ", //0
140
        "AngleNick       ", //0
140
        "AngleRoll       ",
141
        "AngleRoll       ",
141
        "AccNick         ",
142
        "AccNick         ",
142
        "AccRoll         ",
143
        "AccRoll         ",
143
        "OperatingRadius ",
144
        "OperatingRadius ",
144
        "FC-Flags        ", //5
145
        "FC-Flags        ", //5
145
        "NC-Flags        ",
146
        "NC-Flags        ",
146
        "NickServo       ",
147
        "NickServo       ",
147
        "RollServo       ",
148
        "RollServo       ",
148
        "GPS Data        ",
149
        "GPS Data        ",
149
        "CompassHeading  ", //10
150
        "CompassHeading  ", //10
150
        "GyroHeading     ",
151
        "GyroHeading     ",
151
        "SPI Error       ",
152
        "SPI Error       ",
152
        "SPI Okay        ",
153
        "SPI Okay        ",
153
        "I2C Error       ",
154
        "I2C Error       ",
154
        "I2C Okay        ", //15
155
        "I2C Okay        ", //15
155
        "16              ",
156
        "16              ",
156
        "17              ",
157
        "17              ",
157
        "18              ",
158
        "18              ",
158
        "19              ", // SD-Card-time 
159
        "19              ", // SD-Card-time 
159
        "EarthMagnet [%] ", //20
160
        "EarthMagnet [%] ", //20
160
        "Z_Speed         ",
161
        "Z_Speed         ",
161
        "N_Speed         ",
162
        "N_Speed         ",
162
        "E_Speed         ",
163
        "E_Speed         ",
163
        "Magnet X        ",
164
        "Magnet X        ",
164
        "Magnet Y        ", //25
165
        "Magnet Y        ", //25
165
        "Magnet Z        ",
166
        "Magnet Z        ",
166
        "Distance N      ",
167
        "Distance N      ",
167
        "Distance E      ",
168
        "Distance E      ",
168
        "GPS_Nick        ",
169
        "GPS_Nick        ",
169
        "GPS_Roll        ", //30
170
        "GPS_Roll        ", //30
170
        "Used_Sats       "
171
        "Used_Sats       "
171
};
172
};
172
 
173
 
173
DebugOut_t DebugOut;
174
DebugOut_t DebugOut;
174
ExternControl_t ExternControl;
175
ExternControl_t ExternControl;
175
UART_VersionInfo_t UART_VersionInfo;
176
UART_VersionInfo_t UART_VersionInfo;
176
NaviData_t NaviData;
177
NaviData_t NaviData;
177
Data3D_t Data3D;
178
Data3D_t Data3D;
178
u16 Echo; // 2 bytes recieved will be sent back as echo
179
u16 Echo; // 2 bytes recieved will be sent back as echo
179
 
180
 
180
u32 UART1_DebugData_Timer = 0;
181
u32 UART1_DebugData_Timer = 0;
181
u32 UART1_DebugData_Interval = 0;       // in ms
182
u32 UART1_DebugData_Interval = 0;       // in ms
182
u32 UART1_NaviData_Timer = 0;
183
u32 UART1_NaviData_Timer = 0;
183
u32 UART1_NaviData_Interval = 0;        // in ms
184
u32 UART1_NaviData_Interval = 0;        // in ms
184
u32 UART1_Data3D_Timer = 0;
185
u32 UART1_Data3D_Timer = 0;
185
u32 UART1_Data3D_Interval = 0;          // in ms
186
u32 UART1_Data3D_Interval = 0;          // in ms
186
u32 UART1_MotorData_Timer = 0;
187
u32 UART1_MotorData_Timer = 0;
187
u32 UART1_MotorData_Interval = 0;               // in ms
188
u32 UART1_MotorData_Interval = 0;               // in ms
188
u32 UART1_Display_Timer = 0;
189
u32 UART1_Display_Timer = 0;
189
u32 UART1_Display_Interval = 0;         // in ms
190
u32 UART1_Display_Interval = 0;         // in ms
190
 
191
 
191
/********************************************************/
192
/********************************************************/
192
/*            Initialization the UART1                  */
193
/*            Initialization the UART1                  */
193
/********************************************************/
194
/********************************************************/
194
void UART1_Init (void)
195
void UART1_Init (void)
195
{
196
{
196
        GPIO_InitTypeDef GPIO_InitStructure;
197
        GPIO_InitTypeDef GPIO_InitStructure;
197
        UART_InitTypeDef UART_InitStructure;
198
        UART_InitTypeDef UART_InitStructure;
198
 
199
 
199
        // initialize txd buffer
200
        // initialize txd buffer
200
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
201
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
201
 
202
 
202
        // initialize rxd buffer
203
        // initialize rxd buffer
203
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
204
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
204
 
205
 
205
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
206
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
206
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
207
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
207
 
208
 
208
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
209
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
209
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
210
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
210
 
211
 
211
        /*Configure UART1_Rx pin GPIO3.2*/
212
        /*Configure UART1_Rx pin GPIO3.2*/
212
        GPIO_StructInit(&GPIO_InitStructure);
213
        GPIO_StructInit(&GPIO_InitStructure);
213
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
214
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
214
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
215
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
215
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
216
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
216
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
217
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
217
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
218
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
218
        GPIO_Init(GPIO3, &GPIO_InitStructure);
219
        GPIO_Init(GPIO3, &GPIO_InitStructure);
219
 
220
 
220
        /*Configure UART1_Tx pin GPIO3.3*/
221
        /*Configure UART1_Tx pin GPIO3.3*/
221
        GPIO_StructInit(&GPIO_InitStructure);
222
        GPIO_StructInit(&GPIO_InitStructure);
222
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
223
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
223
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
224
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
224
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
225
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
225
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
226
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
226
        GPIO_Init(GPIO3, &GPIO_InitStructure);
227
        GPIO_Init(GPIO3, &GPIO_InitStructure);
227
 
228
 
228
        /* UART1 configured as follow:
229
        /* UART1 configured as follow:
229
        - Word Length = 8 Bits
230
        - Word Length = 8 Bits
230
        - One Stop Bit
231
        - One Stop Bit
231
        - No parity
232
        - No parity
232
        - BaudRate = 57600 baud
233
        - BaudRate = 57600 baud
233
        - Hardware flow control Disabled
234
        - Hardware flow control Disabled
234
        - Receive and transmit enabled
235
        - Receive and transmit enabled
235
        - Receive and transmit FIFOs are Disabled
236
        - Receive and transmit FIFOs are Disabled
236
        */
237
        */
237
        UART_StructInit(&UART_InitStructure);
238
        UART_StructInit(&UART_InitStructure);
238
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
239
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
239
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
240
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
240
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
241
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
241
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
242
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
242
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
243
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
243
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
244
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
244
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
245
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
245
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
246
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
246
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
247
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
247
 
248
 
248
        UART_DeInit(UART1); // reset uart 1     to default
249
        UART_DeInit(UART1); // reset uart 1     to default
249
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
250
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
250
        // enable uart 1 interrupts selective
251
        // enable uart 1 interrupts selective
251
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
252
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
252
        UART_Cmd(UART1, ENABLE); // enable uart 1
253
        UART_Cmd(UART1, ENABLE); // enable uart 1
253
        // configure the uart 1 interupt line
254
        // configure the uart 1 interupt line
254
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
255
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
255
        // enable the uart 1 IRQ
256
        // enable the uart 1 IRQ
256
        VIC_ITCmd(UART1_ITLine, ENABLE);
257
        VIC_ITCmd(UART1_ITLine, ENABLE);
257
 
258
 
258
        // initialize the debug timer
259
        // initialize the debug timer
259
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
260
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
260
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
261
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
261
 
262
 
262
        // Fill Version Info Structure
263
        // Fill Version Info Structure
263
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
264
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
264
        UART_VersionInfo.SWMinor = VERSION_MINOR;
265
        UART_VersionInfo.SWMinor = VERSION_MINOR;
265
        UART_VersionInfo.SWPatch = VERSION_PATCH;
266
        UART_VersionInfo.SWPatch = VERSION_PATCH;
266
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
267
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
267
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
268
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
268
 
269
 
269
        NaviData.Version = NAVIDATA_VERSION;
270
        NaviData.Version = NAVIDATA_VERSION;
270
 
271
 
271
        UART1_PutString("\r\n UART1 init...ok");
272
        UART1_PutString("\r\n UART1 init...ok");
272
}
273
}
273
 
274
 
274
 
275
 
275
/****************************************************************/
276
/****************************************************************/
276
/*               USART1 receiver ISR                            */
277
/*               USART1 receiver ISR                            */
277
/****************************************************************/
278
/****************************************************************/
278
void UART1_IRQHandler(void)
279
void UART1_IRQHandler(void)
279
{
280
{
280
        static u8 abortState = 0;
281
        static u8 abortState = 0;
281
        u8 c;
282
        u8 c;
282
 
283
 
283
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
284
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
284
        {
285
        {
285
                // clear the pending bits!
286
                // clear the pending bits!
286
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
287
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
287
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
288
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
288
                // if debug UART is not UART1
289
                // if debug UART is not UART1
289
                if (DebugUART != UART1)
290
                if (DebugUART != UART1)
290
                {       // forward received data to the debug UART tx buffer
291
                {       // forward received data to the debug UART tx buffer
291
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
292
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
292
                        {
293
                        {
293
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
294
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
294
                                c = UART_ReceiveData(UART1);
295
                                c = UART_ReceiveData(UART1);
295
 
296
 
296
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
297
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
297
                                switch (abortState)
298
                                switch (abortState)
298
                                {
299
                                {
299
                                        case 0:
300
                                        case 0:
300
                                                if (c == 27) abortState++;
301
                                                if (c == 27) abortState++;
301
                                                break;
302
                                                break;
302
                                        case 1:
303
                                        case 1:
303
                                                if (c == 27) abortState++;
304
                                                if (c == 27) abortState++;
304
                                                else abortState = 0;
305
                                                else abortState = 0;
305
                                        break;
306
                                        break;
306
                                        case 2:
307
                                        case 2:
307
                                                if (c == 0x55) abortState++;
308
                                                if (c == 0x55) abortState++;
308
                                                else abortState = 0;
309
                                                else abortState = 0;
309
                                                break;
310
                                                break;
310
                                        case 3:
311
                                        case 3:
311
                                                if (c == 0xAA) abortState++;
312
                                                if (c == 0xAA) abortState++;
312
                                                else abortState = 0;
313
                                                else abortState = 0;
313
                                                break;
314
                                                break;
314
                                        case 4:
315
                                        case 4:
315
                                                if (c == 0x00)
316
                                                if (c == 0x00)
316
                                                {
317
                                                {
317
                                                        if(DebugUART == UART0)
318
                                                        if(DebugUART == UART0)
318
                                                        {
319
                                                        {
319
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
320
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
320
                                                                TIMER2_Init(); // enbable servo outputs
321
                                                                TIMER2_Init(); // enbable servo outputs
321
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
322
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
322
                                                        }
323
                                                        }
323
                                                        DebugUART = UART1;
324
                                                        DebugUART = UART1;
324
                                                }
325
                                                }
325
                                                abortState = 0;
326
                                                abortState = 0;
326
                                                break;
327
                                                break;
327
                                } // end switch abort state
328
                                } // end switch abort state
328
                                // if the Debug uart is not UART1, redirect input to the Debug UART
329
                                // if the Debug uart is not UART1, redirect input to the Debug UART
329
                                if (DebugUART != UART1)
330
                                if (DebugUART != UART1)
330
                                {
331
                                {
331
                                        // wait for space in the tx buffer of the DebugUART
332
                                        // wait for space in the tx buffer of the DebugUART
332
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
333
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
333
                                        // move byte to the tx fifo of the debug uart
334
                                        // move byte to the tx fifo of the debug uart
334
                                        UART_SendData(DebugUART, c);
335
                                        UART_SendData(DebugUART, c);
335
                                }
336
                                }
336
                        }
337
                        }
337
                }
338
                }
338
                else  // DebugUART == UART1 (normal operation)
339
                else  // DebugUART == UART1 (normal operation)
339
                {
340
                {
340
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
341
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
341
                        { // some byes in the hardware fifo
342
                        { // some byes in the hardware fifo
342
                            // get byte from hardware fifo
343
                            // get byte from hardware fifo
343
                        c = UART_ReceiveData(UART1);
344
                        c = UART_ReceiveData(UART1);
344
                                // put into the software fifo
345
                                // put into the software fifo
345
                                if(!fifo_put(&UART1_rx_fifo, c))
346
                                if(!fifo_put(&UART1_rx_fifo, c))
346
                                {       // fifo overflow
347
                                {       // fifo overflow
347
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
348
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
348
                                }
349
                                }
349
                        } // EOF while some byes in the hardware fifo
350
                        } // EOF while some byes in the hardware fifo
350
                } // eof DebugUart = UART1
351
                } // eof DebugUart = UART1
351
        }
352
        }
352
 
353
 
353
 
354
 
354
 
355
 
355
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
356
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
356
}
357
}
357
 
358
 
358
/**************************************************************/
359
/**************************************************************/
359
/* Process incomming data from debug uart                     */
360
/* Process incomming data from debug uart                     */
360
/**************************************************************/
361
/**************************************************************/
361
void UART1_ProcessRxData(void)
362
void UART1_ProcessRxData(void)
362
{
363
{
363
        // return on forwarding uart  or unlocked rx buffer
364
        // return on forwarding uart  or unlocked rx buffer
364
        u8 c;
365
        u8 c;
365
        if(DebugUART != UART1) return;
366
        if(DebugUART != UART1) return;
366
        // if rx buffer is not locked
367
        // if rx buffer is not locked
367
        if(UART1_rx_buffer.Locked == FALSE)
368
        if(UART1_rx_buffer.Locked == FALSE)
368
        {
369
        {
369
                //collect data from primary rx fifo
370
                //collect data from primary rx fifo
370
                while(fifo_get(&UART1_rx_fifo, &c))
371
                while(fifo_get(&UART1_rx_fifo, &c))
371
                {
372
                {
372
                        // break if complete frame is collected
373
                        // break if complete frame is collected
373
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
374
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
374
                }
375
                }
375
        }
376
        }
376
        if(UART1_rx_buffer.Locked == FALSE) return;
377
        if(UART1_rx_buffer.Locked == FALSE) return;
377
 
378
 
378
        Point_t * pPoint = NULL;
379
        Point_t * pPoint = NULL;
379
        SerialMsg_t SerialMsg;
380
        SerialMsg_t SerialMsg;
380
 
381
 
381
        // analyze header first
382
        // analyze header first
382
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
383
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
383
        if( SerialMsg.Address == FC_ADDRESS )
384
        if( SerialMsg.Address == FC_ADDRESS )
384
        {
385
        {
385
                switch(SerialMsg.CmdID)
386
                switch(SerialMsg.CmdID)
386
                {
387
                {
387
//                      case 'v': // version
388
//                      case 'v': // version
388
                        case 'y': // serial poti values
-
 
389
                        case 'b': // extern control
389
                        case 'b': // extern control
-
 
390
                                          UART1_ExternalControlConfirmFrame = 1;
-
 
391
                        case 'y': // serial poti values
390
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
392
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
391
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
393
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
392
                                return; //end process rx data
394
                                return; //end process rx data
393
                        break;
395
                        break;
394
                }
396
                }
395
        }
397
        }
396
 
398
 
397
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
399
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
398
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
400
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
399
        switch(SerialMsg.Address) // check for Slave Address
401
        switch(SerialMsg.Address) // check for Slave Address
400
        {
402
        {
401
                case NC_ADDRESS:  // own Slave Address
403
                case NC_ADDRESS:  // own Slave Address
402
                switch(SerialMsg.CmdID)
404
                switch(SerialMsg.CmdID)
403
                {
405
                {
404
                       
406
                       
405
                        case 'f': // ftp command
407
                        case 'f': // ftp command
406
                                UART1_Request_FTP = SerialMsg.pData[0];
408
                                UART1_Request_FTP = SerialMsg.pData[0];
407
 
409
 
408
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
410
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
409
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
411
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
410
                        break;
412
                        break;
411
 
413
 
412
                        case 'z': // connection checker
414
                        case 'z': // connection checker
413
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
415
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
414
                                UART1_Request_Echo = TRUE;
416
                                UART1_Request_Echo = TRUE;
415
                                break;
417
                                break;
416
 
418
 
417
                        case 'e': // request for the text of the error status
419
                        case 'e': // request for the text of the error status
418
                                UART1_Request_ErrorMessage = TRUE;
420
                                UART1_Request_ErrorMessage = TRUE;
419
                                break;
421
                                break;
420
 
422
 
421
                        case 's'://  new target position
423
                        case 's'://  new target position
422
                                pPoint = (Point_t*)SerialMsg.pData;
424
                                pPoint = (Point_t*)SerialMsg.pData;
423
                                if(pPoint->Position.Status == NEWDATA)
425
                                if(pPoint->Position.Status == NEWDATA)
424
                                {
426
                                {
425
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
427
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
426
                                        //pPoint->Index = 1; // must be one after empty list
428
                                        //pPoint->Index = 1; // must be one after empty list
427
                                        PointList_SetAt(pPoint);
429
                                        PointList_SetAt(pPoint);
428
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
430
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
429
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
431
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
430
                                        if(GPS_pWaypoint != NULL) // if new WP exist
432
                                        if(GPS_pWaypoint != NULL) // if new WP exist
431
                                        {   // update WP hold time stamp immediately!
433
                                        {   // update WP hold time stamp immediately!
432
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
434
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
433
                                                {
435
                                                {
434
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
436
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
435
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
437
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
436
                                                }
438
                                                }
437
*/
439
*/
438
                                        }
440
                                        }
439
                                        BeepTime = 50;
441
                                        BeepTime = 50;
440
                                }
442
                                }
441
                                break;
443
                                break;
442
 
444
 
443
                        case 'u': // redirect debug uart
445
                        case 'u': // redirect debug uart
444
                                switch(SerialMsg.pData[0])
446
                                switch(SerialMsg.pData[0])
445
                                {
447
                                {
446
                                        case UART_FLIGHTCTRL:
448
                                        case UART_FLIGHTCTRL:
447
                                                UART2_Init();                           // initialize UART2 to FC pins
449
                                                UART2_Init();                           // initialize UART2 to FC pins
448
                                                fifo_purge(&UART1_rx_fifo);
450
                                                fifo_purge(&UART1_rx_fifo);
449
                                                TIMER2_Deinit();                        // reduce irq load
451
                                                TIMER2_Deinit();                        // reduce irq load
450
                                                DebugUART = UART2;
452
                                                DebugUART = UART2;
451
                                                break;
453
                                                break;
452
                                        case UART_MK3MAG:
454
                                        case UART_MK3MAG:
453
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
455
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
454
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
456
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
455
                                                GPSData.Status = INVALID;
457
                                                GPSData.Status = INVALID;
456
                                                fifo_purge(&UART1_rx_fifo);
458
                                                fifo_purge(&UART1_rx_fifo);
457
                                                DebugUART = UART0;
459
                                                DebugUART = UART0;
458
                                                break;
460
                                                break;
459
                                        case UART_MKGPS:
461
                                        case UART_MKGPS:
460
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
462
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
461
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
463
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
462
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
464
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
463
                                                GPSData.Status = INVALID;
465
                                                GPSData.Status = INVALID;
464
                                                fifo_purge(&UART1_rx_fifo);
466
                                                fifo_purge(&UART1_rx_fifo);
465
                                                DebugUART = UART0;
467
                                                DebugUART = UART0;
466
                                                break;
468
                                                break;
467
                                        default:
469
                                        default:
468
                                                break;
470
                                                break;
469
                                }
471
                                }
470
                                break;
472
                                break;
471
 
473
 
472
                        case 'w'://  Set point in list at index
474
                        case 'w'://  Set point in list at index
473
                                {
475
                                {
474
                                        pPoint = (Point_t*)SerialMsg.pData;
476
                                        pPoint = (Point_t*)SerialMsg.pData;
475
 
477
 
476
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
478
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
477
                                        {
479
                                        {
478
                                                PointList_Clear();
480
                                                PointList_Clear();
479
                                                GPS_pWaypoint = PointList_WPBegin();
481
                                                GPS_pWaypoint = PointList_WPBegin();
480
                                                UART1_Request_WritePoint = 0; // return new point count
482
                                                UART1_Request_WritePoint = 0; // return new point count
481
                                        }
483
                                        }
482
                                        else
484
                                        else
483
                                        {  // update WP in list at index
485
                                        {  // update WP in list at index
484
                                                if(pPoint->Index > MaxNumberOfWaypoints)
486
                                                if(pPoint->Index > MaxNumberOfWaypoints)
485
                                                 {
487
                                                 {
486
                                                  UART1_Request_WritePoint = 254;
488
                                                  UART1_Request_WritePoint = 254;
487
                                                  pPoint->Index = MaxNumberOfWaypoints;
489
                                                  pPoint->Index = MaxNumberOfWaypoints;
488
                                                 }
490
                                                 }
489
                                                 else
491
                                                 else
490
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
492
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
491
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
493
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
492
                                                if(UART1_Request_WritePoint == pPoint->Index)
494
                                                if(UART1_Request_WritePoint == pPoint->Index)
493
                                                {
495
                                                {
494
                                                        BeepTime = 500;
496
                                                        BeepTime = 500;
495
                                                }
497
                                                }
496
                                        }
498
                                        }
497
                                }
499
                                }
498
                                break;
500
                                break;
499
 
501
 
500
                        case 'x'://  Read Waypoint from List
502
                        case 'x'://  Read Waypoint from List
501
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
503
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
502
                                break;
504
                                break;
503
 
505
 
504
                        case 'j':// Set/Get NC-Parameter
506
                        case 'j':// Set/Get NC-Parameter
505
                                switch(SerialMsg.pData[0])
507
                                switch(SerialMsg.pData[0])
506
                                {
508
                                {
507
                                        case 0: // get
509
                                        case 0: // get
508
                                        break;
510
                                        break;
509
 
511
 
510
                                        case 1: // set
512
                                        case 1: // set
511
                                        {
513
                                        {
512
                                                s16 value;
514
                                                s16 value;
513
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
515
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
514
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
516
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
515
                                        }
517
                                        }
516
                                        break;
518
                                        break;
517
 
519
 
518
                                        default:
520
                                        default:
519
                                        break;
521
                                        break;
520
                                }
522
                                }
521
                                UART1_Request_ParameterId = SerialMsg.pData[1];
523
                                UART1_Request_ParameterId = SerialMsg.pData[1];
522
                                UART1_Request_Parameter = TRUE;
524
                                UART1_Request_Parameter = TRUE;
523
                                break;
525
                                break;
524
                        default:
526
                        default:
525
                                // unsupported command recieved
527
                                // unsupported command recieved
526
                                break;
528
                                break;
527
                } // case NC_ADDRESS
529
                } // case NC_ADDRESS
528
                // "break;" is missing here to fall thru to the common commands
530
                // "break;" is missing here to fall thru to the common commands
529
 
531
 
530
                default:  // and any other Slave Address
532
                default:  // and any other Slave Address
531
 
533
 
532
                switch(SerialMsg.CmdID) // check CmdID
534
                switch(SerialMsg.CmdID) // check CmdID
533
                {
535
                {
534
                        case 'a':// request for the labels of the analog debug outputs
536
                        case 'a':// request for the labels of the analog debug outputs
535
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
537
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
536
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
538
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
537
                                break;
539
                                break;
538
                        /*
540
                        /*
539
                        case 'b': // submit extern control
541
                        case 'b': // submit extern control
540
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
542
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
541
                                UART1_ConfirmFrame = ExternControl.Frame;
543
                                UART1_ConfirmFrame = ExternControl.Frame;
542
                                break;
544
                                break;
543
                        */
545
                        */
544
                        case 'd': // request for debug data;
546
                        case 'd': // request for debug data;
545
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
547
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
546
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
548
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
547
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
549
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
548
                                break;
550
                                break;
549
 
551
 
550
                        case 'c': // request for 3D data;
552
                        case 'c': // request for 3D data;
551
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
553
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
552
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
554
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
553
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
555
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
554
                                break;
556
                                break;
555
 
557
 
556
                        case 'k': // request for Motor data;
558
                        case 'k': // request for Motor data;
557
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
559
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
558
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
560
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
559
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
561
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
560
                                break;
562
                                break;
561
 
563
 
562
                        case 'h':// reqest for display line
564
                        case 'h':// reqest for display line
563
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
565
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
564
                                {
566
                                {
565
                                        UART1_DisplayLine = 2;
567
                                        UART1_DisplayLine = 2;
566
                                        UART1_Display_Interval = 0;
568
                                        UART1_Display_Interval = 0;
567
                                }
569
                                }
568
                                else
570
                                else
569
                                {
571
                                {
570
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
572
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
571
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
573
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
572
                                        UART1_DisplayLine = 4;
574
                                        UART1_DisplayLine = 4;
573
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
575
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
574
                                }
576
                                }
575
                                UART1_Request_Display = TRUE;
577
                                UART1_Request_Display = TRUE;
576
                                break;
578
                                break;
577
 
579
 
578
                        case 'l':// reqest for display columns
580
                        case 'l':// reqest for display columns
579
                                MenuItem = SerialMsg.pData[0];
581
                                MenuItem = SerialMsg.pData[0];
580
                                UART1_Request_Display1 = TRUE;
582
                                UART1_Request_Display1 = TRUE;
581
                                break;
583
                                break;
582
 
584
 
583
                        case 'o': // request for navigation information
585
                        case 'o': // request for navigation information
584
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
586
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
585
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
587
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
586
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
588
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
587
                                break;
589
                                break;
588
 
590
 
589
                        case 'v': // request for version info
591
                        case 'v': // request for version info
590
                                UART1_Request_VersionInfo = TRUE;
592
                                UART1_Request_VersionInfo = TRUE;
591
                                break;
593
                                break;
592
                        default:
594
                        default:
593
                                // unsupported command recieved
595
                                // unsupported command recieved
594
                                break;
596
                                break;
595
                }
597
                }
596
                break; // default:
598
                break; // default:
597
        }
599
        }
598
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
600
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
599
}
601
}
600
 
602
 
601
 
603
 
602
/*****************************************************/
604
/*****************************************************/
603
/*                   Send a character                */
605
/*                   Send a character                */
604
/*****************************************************/
606
/*****************************************************/
605
s16 UART1_Putchar(char c)
607
s16 UART1_Putchar(char c)
606
{
608
{
607
        u32 timeout = 10000;
609
        u32 timeout = 10000;
608
        if (c == '\n') UART1_Putchar('\r');
610
        if (c == '\n') UART1_Putchar('\r');
609
        // wait until txd fifo is not full
611
        // wait until txd fifo is not full
610
        while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET) if(--timeout == 0) return(0);
612
        while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET) if(--timeout == 0) return(0);
611
        // transmit byte
613
        // transmit byte
612
        UART_SendData(UART1, c);
614
        UART_SendData(UART1, c);
613
        #ifdef FOLLOW_ME
615
        #ifdef FOLLOW_ME
614
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
616
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
615
        #endif
617
        #endif
616
        return (0);
618
        return (0);
617
}
619
}
618
 
620
 
619
/*****************************************************/
621
/*****************************************************/
620
/*       Send a string to the debug uart              */
622
/*       Send a string to the debug uart              */
621
/*****************************************************/
623
/*****************************************************/
622
void UART1_PutString(u8 *s)
624
void UART1_PutString(u8 *s)
623
{
625
{
624
        if(s == NULL) return;
626
        if(s == NULL) return;
625
        while (*s != '\0' && DebugUART == UART1)
627
        while (*s != '\0' && DebugUART == UART1)
626
        {
628
        {
627
                UART1_Putchar(*s);
629
                UART1_Putchar(*s);
628
                s ++;
630
                s ++;
629
        }
631
        }
630
}
632
}
631
 
633
 
632
 
634
 
633
/**************************************************************/
635
/**************************************************************/
634
/*         Transmit tx buffer via debug uart                  */
636
/*         Transmit tx buffer via debug uart                  */
635
/**************************************************************/
637
/**************************************************************/
636
void UART1_Transmit(void)
638
void UART1_Transmit(void)
637
{
639
{
638
        u8 tmp_tx;
640
        u8 tmp_tx;
639
        if(DebugUART != UART1) return;
641
        if(DebugUART != UART1) return;
640
        // if something has to be send and the txd fifo is not full
642
        // if something has to be send and the txd fifo is not full
641
        if(UART1_tx_buffer.Locked == TRUE)
643
        if(UART1_tx_buffer.Locked == TRUE)
642
        {
644
        {
643
                // while there is some space in the tx fifo
645
                // while there is some space in the tx fifo
644
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
646
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
645
                {
647
                {
646
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
648
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
647
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
649
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
648
                        #ifdef FOLLOW_ME
650
                        #ifdef FOLLOW_ME
649
                        if(TransmitAlsoToFC)
651
                        if(TransmitAlsoToFC)
650
                        {
652
                        {
651
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
653
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
652
                        }
654
                        }
653
                        #endif
655
                        #endif
654
                        // if terminating character or end of txd buffer reached
656
                        // if terminating character or end of txd buffer reached
655
                        if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
657
                        if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
656
                        {
658
                        {
657
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
659
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
658
                                #ifdef FOLLOW_ME
660
                                #ifdef FOLLOW_ME
659
                                TransmitAlsoToFC = 0;
661
                                TransmitAlsoToFC = 0;
660
                                #endif
662
                                #endif
661
                                break; // end while loop
663
                                break; // end while loop
662
                        }
664
                        }
663
                }
665
                }
664
        }
666
        }
665
}
667
}
666
 
668
 
667
 
669
 
668
/**************************************************************/
670
/**************************************************************/
669
/* Send the answers to incomming commands at the debug uart   */
671
/* Send the answers to incomming commands at the debug uart   */
670
/**************************************************************/
672
/**************************************************************/
671
void UART1_TransmitTxData(void)
673
void UART1_TransmitTxData(void)
672
{
674
{
673
static u8 motorindex1 = 255, motorindex2 = 0;
675
static u8 motorindex1 = 255, motorindex2 = 0;
674
        if(DebugUART != UART1) return;
676
        if(DebugUART != UART1) return;
675
 
677
 
676
        if(CheckDelay(UART1_AboTimeOut))
678
        if(CheckDelay(UART1_AboTimeOut))
677
        {
679
        {
678
                UART1_DebugData_Interval = 0;
680
                UART1_DebugData_Interval = 0;
679
                UART1_NaviData_Interval = 0;
681
                UART1_NaviData_Interval = 0;
680
                UART1_Data3D_Interval = 0;
682
                UART1_Data3D_Interval = 0;
681
                UART1_Display_Interval = 0;
683
                UART1_Display_Interval = 0;
682
                UART1_MotorData_Interval = 0;
684
                UART1_MotorData_Interval = 0;
683
        }
685
        }
684
 
686
 
685
        UART1_Transmit(); // output pending bytes in tx buffer
687
        UART1_Transmit(); // output pending bytes in tx buffer
686
        if((UART1_tx_buffer.Locked == TRUE)) return;
688
        if((UART1_tx_buffer.Locked == TRUE)) return;
687
 
689
 
688
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
690
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
689
        {
691
        {
690
                s16 ParamValue;
692
                s16 ParamValue;
691
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
693
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
692
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'J', NC_ADDRESS, 2, &UART1_Request_ParameterId, sizeof(UART1_Request_ParameterId), &ParamValue, sizeof(ParamValue)); // answer the param request
694
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'J', NC_ADDRESS, 2, &UART1_Request_ParameterId, sizeof(UART1_Request_ParameterId), &ParamValue, sizeof(ParamValue)); // answer the param request
693
                UART1_Request_Parameter = FALSE;
695
                UART1_Request_Parameter = FALSE;
694
        }
696
        }
695
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
697
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
696
        {
698
        {
697
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
699
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
698
                Echo = 0; // reset echo value
700
                Echo = 0; // reset echo value
699
                UART1_Request_Echo = FALSE;
701
                UART1_Request_Echo = FALSE;
700
        }
702
        }
701
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
703
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
702
        {
704
        {
703
                CheckFTPCommand(UART1_Request_FTP);
705
                CheckFTPCommand(UART1_Request_FTP);
704
                UART1_Request_FTP = FALSE;
706
                UART1_Request_FTP = FALSE;
705
        }
707
        }
706
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
708
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
707
        {
709
        {
708
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
710
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
709
                UART1_Request_WritePoint = 0xFF;
711
                UART1_Request_WritePoint = 0xFF;
710
        }
712
        }
711
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
713
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
712
        {
714
        {
713
                u8 PointCount = PointList_GetCount();
715
                u8 PointCount = PointList_GetCount();
714
                if (UART1_Request_ReadPoint <= PointCount)
716
                if (UART1_Request_ReadPoint <= PointCount)
715
                {
717
                {
716
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
718
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
717
                }
719
                }
718
                else
720
                else
719
                {
721
                {
720
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
722
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
721
                }
723
                }
722
                UART1_Request_ReadPoint = 0;
724
                UART1_Request_ReadPoint = 0;
723
        }
725
        }
724
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
726
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
725
        {
727
        {
726
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
728
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
727
                UART1_Request_DebugLabel = 0xFF;
729
                UART1_Request_DebugLabel = 0xFF;
728
        }
730
        }
-
 
731
        else if(UART1_ExternalControlConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
-
 
732
        {
-
 
733
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1,(u8 *)&UART1_ExternalControlConfirmFrame, sizeof(UART1_ExternalControlConfirmFrame));
-
 
734
                UART1_ExternalControlConfirmFrame = 0;
-
 
735
        }
729
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
736
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
730
        {
737
        {
731
                NaviData.Errorcode = ErrorCode;
738
                NaviData.Errorcode = ErrorCode;
732
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData, sizeof(NaviData));
739
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData, sizeof(NaviData));
733
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
740
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
734
                UART1_Request_NaviData = FALSE;
741
                UART1_Request_NaviData = FALSE;
735
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
742
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
736
        }
743
        }
737
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
744
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
738
        {
745
        {
739
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
746
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
740
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
747
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
741
                UART1_Request_DebugData = FALSE;
748
                UART1_Request_DebugData = FALSE;
742
        }
749
        }
743
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
750
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
744
        {
751
        {
745
                Data3D.StickNick = FC.StickNick;
752
                Data3D.StickNick = FC.StickNick;
746
                Data3D.StickRoll = FC.StickRoll;
753
                Data3D.StickRoll = FC.StickRoll;
747
        Data3D.StickYaw = FC.StickYaw;
754
        Data3D.StickYaw = FC.StickYaw;
748
        Data3D.StickGas = FC.StickGas;
755
        Data3D.StickGas = FC.StickGas;
749
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
756
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
750
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
757
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
751
                UART1_Request_Data3D = FALSE;
758
                UART1_Request_Data3D = FALSE;
752
        }
759
        }
753
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
760
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
754
        {
761
        {
755
                do
762
                do
756
                {
763
                {
757
                 motorindex1++;
764
                 motorindex1++;
758
                 motorindex1%=12;
765
                 motorindex1%=12;
759
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
766
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
760
                 if(motorindex1 == motorindex2) break;
767
                 if(motorindex1 == motorindex2) break;
761
                }
768
                }
762
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
769
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
763
 
770
 
764
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
771
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
765
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
772
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
766
                UART1_Request_MotorData = FALSE;
773
                UART1_Request_MotorData = FALSE;
767
        }
774
        }
768
        /*
775
        /*
769
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
776
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
770
        {
777
        {
771
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
778
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
772
                UART1_ConfirmFrame = 0;
779
                UART1_ConfirmFrame = 0;
773
        }
780
        }
774
        */
781
        */
775
        /*
782
        /*
776
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
783
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
777
        {
784
        {
778
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
785
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
779
                UART1_Request_ExternalControl = FALSE;
786
                UART1_Request_ExternalControl = FALSE;
780
        }
787
        }
781
        */
788
        */
782
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
789
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
783
        {
790
        {
784
                if(UART1_DisplayLine > 3)
791
                if(UART1_DisplayLine > 3)
785
                {
792
                {
786
                        Menu_Update(UART1_DisplayKeys);
793
                        Menu_Update(UART1_DisplayKeys);
787
                        UART1_DisplayKeys = 0;
794
                        UART1_DisplayKeys = 0;
788
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
795
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
789
                }
796
                }
790
                else
797
                else
791
                {
798
                {
792
                        UART1_DisplayLine = 2;
799
                        UART1_DisplayLine = 2;
793
                        sprintf(text,"!!! incompatible !!!");
800
                        sprintf(text,"!!! incompatible !!!");
794
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
801
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
795
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
802
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
796
                }
803
                }
797
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
804
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
798
                UART1_Request_Display = FALSE;
805
                UART1_Request_Display = FALSE;
799
        }
806
        }
800
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
807
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
801
        {
808
        {
802
                Menu_Update(0);
809
                Menu_Update(0);
803
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
810
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
804
                UART1_Request_Display1 = FALSE;
811
                UART1_Request_Display1 = FALSE;
805
        }
812
        }
806
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
813
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
807
        {
814
        {
808
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
815
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
809
                UART1_Request_VersionInfo = FALSE;
816
                UART1_Request_VersionInfo = FALSE;
810
        }
817
        }
811
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
818
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
812
        {
819
        {
813
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
820
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
814
                UART1_Request_ErrorMessage = FALSE;
821
                UART1_Request_ErrorMessage = FALSE;
815
        }
822
        }
816
#ifdef FOLLOW_ME
823
#ifdef FOLLOW_ME
817
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
824
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
818
        {
825
        {
819
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
826
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
820
                {
827
                {
821
                        TransmitAlsoToFC = 1;
828
                        TransmitAlsoToFC = 1;
822
                        // update FollowMe content
829
                        // update FollowMe content
823
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
830
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
824
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
831
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
825
                        FollowMe.Position.Status = NEWDATA;
832
                        FollowMe.Position.Status = NEWDATA;
826
                        FollowMe.Position.Altitude = 1;
833
                        FollowMe.Position.Altitude = 1;
827
                        // 0  -> no Orientation
834
                        // 0  -> no Orientation
828
                        // 1-360 -> CompassCourse Setpoint
835
                        // 1-360 -> CompassCourse Setpoint
829
                        // -1 -> points to  WP1 -> itself
836
                        // -1 -> points to  WP1 -> itself
830
                FollowMe.Heading = -1;
837
                FollowMe.Heading = -1;
831
                        FollowMe.ToleranceRadius = 1;
838
                        FollowMe.ToleranceRadius = 1;
832
                        FollowMe.HoldTime = 60;
839
                        FollowMe.HoldTime = 60;
833
                        FollowMe.Event_Flag = 1;
840
                        FollowMe.Event_Flag = 1;
834
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
841
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
835
                        FollowMe.Type = POINT_TYPE_WP;
842
                        FollowMe.Type = POINT_TYPE_WP;
836
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
843
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
837
                        FollowMe.AltitudeRate = 0;                        // do not change height
844
                        FollowMe.AltitudeRate = 0;                        // do not change height
838
                        FollowMe.reserve[0] = 0;                // reserve
845
                        FollowMe.reserve[0] = 0;                // reserve
839
                        FollowMe.reserve[1] = 0;                // reserve
846
                        FollowMe.reserve[1] = 0;                // reserve
840
                        FollowMe.reserve[2] = 0;                // reserve
847
                        FollowMe.reserve[2] = 0;                // reserve
841
                        FollowMe.reserve[3] = 0;                // reserve
848
                        FollowMe.reserve[3] = 0;                // reserve
842
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
849
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
843
                }
850
                }
844
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
851
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
845
        }
852
        }
846
#endif
853
#endif
847
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
854
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
848
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
855
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
849
        {
856
        {
850
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
857
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
851
                SendDebugOutput = 0;
858
                SendDebugOutput = 0;
852
        }
859
        }
853
#endif
860
#endif
854
        UART1_Transmit(); // output pending bytes in tx buffer
861
        UART1_Transmit(); // output pending bytes in tx buffer
855
}
862
}
856
 
863
 
857
 
864