Subversion Repositories NaviCtrl

Rev

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

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