Subversion Repositories NaviCtrl

Rev

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

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