Subversion Repositories NaviCtrl

Rev

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

Rev 358 Rev 360
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
-
 
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
-
 
7
// + FOR NON COMMERCIAL USE ONLY
-
 
8
// + www.MikroKopter.com
5
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
7
// + Software Nutzungsbedingungen (english version: see below)
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
15
// + Verkauf von Luftbildaufnahmen, usw.
-
 
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
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.
-
 
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.
-
 
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
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
-
 
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.
-
 
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.
-
 
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
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
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.
-
 
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.
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
-
 
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
-
 
22
// + eindeutig als Ursprung verlinkt werden
32
// + Software LICENSING TERMS
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
25
// + Benutzung auf eigene Gefahr
-
 
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
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 
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
29
// + mit unserer Zustimmung zulässig
37
// + The Software may only be used with the Licensor's products.
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
34
// + this list of conditions and the following disclaimer.
40
// + agreement shall be the property of the Licensor.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
36
// +     from this software without specific prior written permission.
-
 
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
38
// +     for non-commercial use (directly or indirectly)
43
// + The customer shall be responsible for taking reasonable precautions
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
40
// +     with our written permission
-
 
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
42
// +     clearly linked as origin
-
 
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
44
//
-
 
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-
 
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-
 
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// + #### END OF LICENSING TERMS ####
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// +  POSSIBILITY OF SUCH DAMAGE.
-
 
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdio.h>
56
#include <stdio.h>
58
#include <stdarg.h>
57
#include <stdarg.h>
59
#include <string.h>
58
#include <string.h>
60
 
59
 
61
#include "91x_lib.h"
60
#include "91x_lib.h"
62
#include "main.h"
61
#include "main.h"
63
#include "config.h"
62
#include "config.h"
64
#include "menu.h"
63
#include "menu.h"
65
#include "GPS.h"
64
#include "GPS.h"
66
#include "i2c.h"
65
#include "i2c.h"
67
#include "uart0.h"
66
#include "uart0.h"
68
#include "uart1.h"
67
#include "uart1.h"
69
#include "uart2.h"
68
#include "uart2.h"
70
#include "timer1.h"
69
#include "timer1.h"
71
#include "timer2.h"
70
#include "timer2.h"
72
#include "analog.h"
71
#include "analog.h"
73
#include "compass.h"
72
#include "compass.h"
74
#include "waypoints.h"
73
#include "waypoints.h"
75
#include "mkprotocol.h"
74
#include "mkprotocol.h"
76
#include "params.h"
75
#include "params.h"
77
#include "fifo.h"
76
#include "fifo.h"
78
#include "debug.h"
77
#include "debug.h"
79
#include "spi_slave.h"
78
#include "spi_slave.h"
80
#include "ftphelper.h"
79
#include "ftphelper.h"
81
 
80
 
82
#define FALSE   0
81
#define FALSE   0
83
#define TRUE    1
82
#define TRUE    1
84
 
83
 
85
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
84
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
86
u32 UART1_AboTimeOut = 0;
85
u32 UART1_AboTimeOut = 0;
87
 
86
 
88
u8 UART1_Request_VersionInfo    = FALSE;
87
u8 UART1_Request_VersionInfo    = FALSE;
89
u8 UART1_Request_ExternalControl= FALSE;
88
u8 UART1_Request_ExternalControl= FALSE;
90
u8 UART1_Request_Display                = FALSE;
89
u8 UART1_Request_Display                = FALSE;
91
u8 UART1_Request_Display1               = FALSE;
90
u8 UART1_Request_Display1               = FALSE;
92
u8 UART1_Request_DebugData              = FALSE;
91
u8 UART1_Request_DebugData              = FALSE;
93
u8 UART1_Request_DebugLabel             = 255;
92
u8 UART1_Request_DebugLabel             = 255;
94
u8 UART1_Request_NaviData               = FALSE;
93
u8 UART1_Request_NaviData               = FALSE;
95
u8 UART1_Request_ErrorMessage   = FALSE;
94
u8 UART1_Request_ErrorMessage   = FALSE;
96
u8 UART1_Request_WritePoint             = 0xFF;
95
u8 UART1_Request_WritePoint             = 0xFF;
97
u8 UART1_Request_ReadPoint              = 0;
96
u8 UART1_Request_ReadPoint              = 0;
98
u8 UART1_Request_Data3D             = FALSE;
97
u8 UART1_Request_Data3D             = FALSE;
99
u8 UART1_Request_MotorData          = FALSE;
98
u8 UART1_Request_MotorData          = FALSE;
100
u8 UART1_Request_Echo               = FALSE;
99
u8 UART1_Request_Echo               = FALSE;
101
u8 UART1_Request_ParameterId    = 0;
100
u8 UART1_Request_ParameterId    = 0;
102
u8 UART1_Request_Parameter              = FALSE;
101
u8 UART1_Request_Parameter              = FALSE;
103
u8 UART1_DisplayKeys                    = 0;
102
u8 UART1_DisplayKeys                    = 0;
104
u8 UART1_DisplayLine                    = 0;
103
u8 UART1_DisplayLine                    = 0;
105
u8 UART1_ConfirmFrame                   = 0;
104
u8 UART1_ConfirmFrame                   = 0;
106
u8 UART1_Request_FTP                    = FALSE;
105
u8 UART1_Request_FTP                    = FALSE;
107
 
-
 
-
 
106
u8 LastTransmittedFCStatusFlags2 = 0;
108
 
107
 
109
UART_TypeDef *DebugUART = UART1;
108
UART_TypeDef *DebugUART = UART1;
110
 
109
 
111
#ifdef FOLLOW_ME
110
#ifdef FOLLOW_ME
112
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
111
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
113
u32 UART1_FollowMe_Timer        = 0;
112
u32 UART1_FollowMe_Timer        = 0;
114
Point_t FollowMe;
113
Point_t FollowMe;
115
#endif
114
#endif
116
 
115
 
117
// the primary rx fifo
116
// the primary rx fifo
118
#define UART1_RX_FIFO_LEN 512
117
#define UART1_RX_FIFO_LEN 512
119
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
118
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
120
fifo_t UART1_rx_fifo;
119
fifo_t UART1_rx_fifo;
121
 
120
 
122
// the rx buffer
121
// the rx buffer
123
#define UART1_RX_BUFFER_LEN  150
122
#define UART1_RX_BUFFER_LEN  150
124
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
123
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
125
Buffer_t UART1_rx_buffer;
124
Buffer_t UART1_rx_buffer;
126
 
125
 
127
// the tx buffer
126
// the tx buffer
128
#define UART1_TX_BUFFER_LEN  1024
127
#define UART1_TX_BUFFER_LEN  1024
129
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
128
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
130
Buffer_t UART1_tx_buffer;
129
Buffer_t UART1_tx_buffer;
131
 
130
 
132
volatile u8 SerialLinkOkay = 0;
131
volatile u8 SerialLinkOkay = 0;
133
 
132
 
134
u8 text[200];
133
u8 text[200];
135
 
134
 
136
const u8 ANALOG_LABEL[32][16] =
135
const u8 ANALOG_LABEL[32][16] =
137
{
136
{
138
   //1234567890123456
137
   //1234567890123456
139
        "AngleNick       ", //0
138
        "AngleNick       ", //0
140
        "AngleRoll       ",
139
        "AngleRoll       ",
141
        "AccNick         ",
140
        "AccNick         ",
142
        "AccRoll         ",
141
        "AccRoll         ",
143
        "OperatingRadius ",
142
        "OperatingRadius ",
144
        "FC-Flags        ", //5
143
        "FC-Flags        ", //5
145
        "NC-Flags        ",
144
        "NC-Flags        ",
146
        "NickServo       ",
145
        "NickServo       ",
147
        "RollServo       ",
146
        "RollServo       ",
148
        "GPS Data        ",
147
        "GPS Data        ",
149
        "CompassHeading  ", //10
148
        "CompassHeading  ", //10
150
        "GyroHeading     ",
149
        "GyroHeading     ",
151
        "SPI Error       ",
150
        "SPI Error       ",
152
        "SPI Okay        ",
151
        "SPI Okay        ",
153
        "I2C Error       ",
152
        "I2C Error       ",
154
        "I2C Okay        ", //15
153
        "I2C Okay        ", //15
155
        "16              ",
154
        "16              ",
156
        "17              ",
155
        "17              ",
157
        "18              ",
156
        "18              ",
158
        "19              ", // SD-Card-time 
157
        "19              ", // SD-Card-time 
159
        "EarthMagnet [%] ", //20
158
        "EarthMagnet [%] ", //20
160
        "Z_Speed         ",
159
        "Z_Speed         ",
161
        "N_Speed         ",
160
        "N_Speed         ",
162
        "E_Speed         ",
161
        "E_Speed         ",
163
        "Magnet X        ",
162
        "Magnet X        ",
164
        "Magnet Y        ", //25
163
        "Magnet Y        ", //25
165
        "Magnet Z        ",
164
        "Magnet Z        ",
166
        "Distance N      ",
165
        "Distance N      ",
167
        "Distance E      ",
166
        "Distance E      ",
168
        "GPS_Nick        ",
167
        "GPS_Nick        ",
169
        "GPS_Roll        ", //30
168
        "GPS_Roll        ", //30
170
        "Used_Sats       "
169
        "Used_Sats       "
171
};
170
};
172
 
171
 
173
DebugOut_t DebugOut;
172
DebugOut_t DebugOut;
174
ExternControl_t ExternControl;
173
ExternControl_t ExternControl;
175
UART_VersionInfo_t UART_VersionInfo;
174
UART_VersionInfo_t UART_VersionInfo;
176
NaviData_t NaviData;
175
NaviData_t NaviData;
177
Data3D_t Data3D;
176
Data3D_t Data3D;
178
u16 Echo; // 2 bytes recieved will be sent back as echo
177
u16 Echo; // 2 bytes recieved will be sent back as echo
179
 
178
 
180
u32 UART1_DebugData_Timer = 0;
179
u32 UART1_DebugData_Timer = 0;
181
u32 UART1_DebugData_Interval = 0;       // in ms
180
u32 UART1_DebugData_Interval = 0;       // in ms
182
u32 UART1_NaviData_Timer = 0;
181
u32 UART1_NaviData_Timer = 0;
183
u32 UART1_NaviData_Interval = 0;        // in ms
182
u32 UART1_NaviData_Interval = 0;        // in ms
184
u32 UART1_Data3D_Timer = 0;
183
u32 UART1_Data3D_Timer = 0;
185
u32 UART1_Data3D_Interval = 0;          // in ms
184
u32 UART1_Data3D_Interval = 0;          // in ms
186
u32 UART1_MotorData_Timer = 0;
185
u32 UART1_MotorData_Timer = 0;
187
u32 UART1_MotorData_Interval = 0;               // in ms
186
u32 UART1_MotorData_Interval = 0;               // in ms
188
u32 UART1_Display_Timer = 0;
187
u32 UART1_Display_Timer = 0;
189
u32 UART1_Display_Interval = 0;         // in ms
188
u32 UART1_Display_Interval = 0;         // in ms
190
 
189
 
191
/********************************************************/
190
/********************************************************/
192
/*            Initialization the UART1                  */
191
/*            Initialization the UART1                  */
193
/********************************************************/
192
/********************************************************/
194
void UART1_Init (void)
193
void UART1_Init (void)
195
{
194
{
196
        GPIO_InitTypeDef GPIO_InitStructure;
195
        GPIO_InitTypeDef GPIO_InitStructure;
197
        UART_InitTypeDef UART_InitStructure;
196
        UART_InitTypeDef UART_InitStructure;
198
 
197
 
199
        // initialize txd buffer
198
        // initialize txd buffer
200
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
199
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
201
 
200
 
202
        // initialize rxd buffer
201
        // initialize rxd buffer
203
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
202
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
204
 
203
 
205
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
204
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
206
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
205
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
207
 
206
 
208
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
207
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
209
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
208
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
210
 
209
 
211
        /*Configure UART1_Rx pin GPIO3.2*/
210
        /*Configure UART1_Rx pin GPIO3.2*/
212
        GPIO_StructInit(&GPIO_InitStructure);
211
        GPIO_StructInit(&GPIO_InitStructure);
213
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
212
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
214
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
213
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
215
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
214
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
216
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
215
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
217
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
216
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
218
        GPIO_Init(GPIO3, &GPIO_InitStructure);
217
        GPIO_Init(GPIO3, &GPIO_InitStructure);
219
 
218
 
220
        /*Configure UART1_Tx pin GPIO3.3*/
219
        /*Configure UART1_Tx pin GPIO3.3*/
221
        GPIO_StructInit(&GPIO_InitStructure);
220
        GPIO_StructInit(&GPIO_InitStructure);
222
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
221
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
223
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
222
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
224
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
223
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
225
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
224
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
226
        GPIO_Init(GPIO3, &GPIO_InitStructure);
225
        GPIO_Init(GPIO3, &GPIO_InitStructure);
227
 
226
 
228
        /* UART1 configured as follow:
227
        /* UART1 configured as follow:
229
        - Word Length = 8 Bits
228
        - Word Length = 8 Bits
230
        - One Stop Bit
229
        - One Stop Bit
231
        - No parity
230
        - No parity
232
        - BaudRate = 57600 baud
231
        - BaudRate = 57600 baud
233
        - Hardware flow control Disabled
232
        - Hardware flow control Disabled
234
        - Receive and transmit enabled
233
        - Receive and transmit enabled
235
        - Receive and transmit FIFOs are Disabled
234
        - Receive and transmit FIFOs are Disabled
236
        */
235
        */
237
        UART_StructInit(&UART_InitStructure);
236
        UART_StructInit(&UART_InitStructure);
238
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
237
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
239
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
238
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
240
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
239
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
241
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
240
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
242
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
241
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
243
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
242
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
244
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
243
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
245
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
244
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
246
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
245
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
247
 
246
 
248
        UART_DeInit(UART1); // reset uart 1     to default
247
        UART_DeInit(UART1); // reset uart 1     to default
249
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
248
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
250
        // enable uart 1 interrupts selective
249
        // enable uart 1 interrupts selective
251
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
250
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
252
        UART_Cmd(UART1, ENABLE); // enable uart 1
251
        UART_Cmd(UART1, ENABLE); // enable uart 1
253
        // configure the uart 1 interupt line
252
        // configure the uart 1 interupt line
254
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
253
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
255
        // enable the uart 1 IRQ
254
        // enable the uart 1 IRQ
256
        VIC_ITCmd(UART1_ITLine, ENABLE);
255
        VIC_ITCmd(UART1_ITLine, ENABLE);
257
 
256
 
258
        // initialize the debug timer
257
        // initialize the debug timer
259
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
258
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
260
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
259
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
261
 
260
 
262
        // Fill Version Info Structure
261
        // Fill Version Info Structure
263
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
262
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
264
        UART_VersionInfo.SWMinor = VERSION_MINOR;
263
        UART_VersionInfo.SWMinor = VERSION_MINOR;
265
        UART_VersionInfo.SWPatch = VERSION_PATCH;
264
        UART_VersionInfo.SWPatch = VERSION_PATCH;
266
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
265
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
267
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
266
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
268
 
267
 
269
        NaviData.Version = NAVIDATA_VERSION;
268
        NaviData.Version = NAVIDATA_VERSION;
270
 
269
 
271
        UART1_PutString("\r\n UART1 init...ok");
270
        UART1_PutString("\r\n UART1 init...ok");
272
}
271
}
273
 
272
 
274
 
273
 
275
/****************************************************************/
274
/****************************************************************/
276
/*               USART1 receiver ISR                            */
275
/*               USART1 receiver ISR                            */
277
/****************************************************************/
276
/****************************************************************/
278
void UART1_IRQHandler(void)
277
void UART1_IRQHandler(void)
279
{
278
{
280
        static u8 abortState = 0;
279
        static u8 abortState = 0;
281
        u8 c;
280
        u8 c;
282
 
281
 
283
        IENABLE;
282
        IENABLE;
284
 
283
 
285
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
284
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
286
        {
285
        {
287
                // clear the pending bits!
286
                // clear the pending bits!
288
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
287
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
289
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
288
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
290
                // if debug UART is not UART1
289
                // if debug UART is not UART1
291
                if (DebugUART != UART1)
290
                if (DebugUART != UART1)
292
                {       // forward received data to the debug UART tx buffer
291
                {       // forward received data to the debug UART tx buffer
293
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
292
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
294
                        {
293
                        {
295
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
294
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
296
                                c = UART_ReceiveData(UART1);
295
                                c = UART_ReceiveData(UART1);
297
 
296
 
298
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
297
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
299
                                switch (abortState)
298
                                switch (abortState)
300
                                {
299
                                {
301
                                        case 0:
300
                                        case 0:
302
                                                if (c == 27) abortState++;
301
                                                if (c == 27) abortState++;
303
                                                break;
302
                                                break;
304
                                        case 1:
303
                                        case 1:
305
                                                if (c == 27) abortState++;
304
                                                if (c == 27) abortState++;
306
                                                else abortState = 0;
305
                                                else abortState = 0;
307
                                        break;
306
                                        break;
308
                                        case 2:
307
                                        case 2:
309
                                                if (c == 0x55) abortState++;
308
                                                if (c == 0x55) abortState++;
310
                                                else abortState = 0;
309
                                                else abortState = 0;
311
                                                break;
310
                                                break;
312
                                        case 3:
311
                                        case 3:
313
                                                if (c == 0xAA) abortState++;
312
                                                if (c == 0xAA) abortState++;
314
                                                else abortState = 0;
313
                                                else abortState = 0;
315
                                                break;
314
                                                break;
316
                                        case 4:
315
                                        case 4:
317
                                                if (c == 0x00)
316
                                                if (c == 0x00)
318
                                                {
317
                                                {
319
                                                        if(DebugUART == UART0)
318
                                                        if(DebugUART == UART0)
320
                                                        {
319
                                                        {
321
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
320
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
322
                                                                TIMER2_Init(); // enbable servo outputs
321
                                                                TIMER2_Init(); // enbable servo outputs
323
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
322
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
324
                                                        }
323
                                                        }
325
                                                        DebugUART = UART1;
324
                                                        DebugUART = UART1;
326
                                                }
325
                                                }
327
                                                abortState = 0;
326
                                                abortState = 0;
328
                                                break;
327
                                                break;
329
                                } // end switch abort state
328
                                } // end switch abort state
330
                                // if the Debug uart is not UART1, redirect input to the Debug UART
329
                                // if the Debug uart is not UART1, redirect input to the Debug UART
331
                                if (DebugUART != UART1)
330
                                if (DebugUART != UART1)
332
                                {
331
                                {
333
                                        // wait for space in the tx buffer of the DebugUART
332
                                        // wait for space in the tx buffer of the DebugUART
334
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
333
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
335
                                        // move byte to the tx fifo of the debug uart
334
                                        // move byte to the tx fifo of the debug uart
336
                                        UART_SendData(DebugUART, c);
335
                                        UART_SendData(DebugUART, c);
337
                                }
336
                                }
338
                        }
337
                        }
339
                }
338
                }
340
                else  // DebugUART == UART1 (normal operation)
339
                else  // DebugUART == UART1 (normal operation)
341
                {
340
                {
342
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
341
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
343
                        { // some byes in the hardware fifo
342
                        { // some byes in the hardware fifo
344
                            // get byte from hardware fifo
343
                            // get byte from hardware fifo
345
                        c = UART_ReceiveData(UART1);
344
                        c = UART_ReceiveData(UART1);
346
                                // put into the software fifo
345
                                // put into the software fifo
347
                                if(!fifo_put(&UART1_rx_fifo, c))
346
                                if(!fifo_put(&UART1_rx_fifo, c))
348
                                {       // fifo overflow
347
                                {       // fifo overflow
349
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
348
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
350
                                }
349
                                }
351
                        } // EOF while some byes in the hardware fifo
350
                        } // EOF while some byes in the hardware fifo
352
                } // eof DebugUart = UART1
351
                } // eof DebugUart = UART1
353
        }
352
        }
354
 
353
 
355
        IDISABLE;
354
        IDISABLE;
356
}
355
}
357
 
356
 
358
/**************************************************************/
357
/**************************************************************/
359
/* Process incomming data from debug uart                     */
358
/* Process incomming data from debug uart                     */
360
/**************************************************************/
359
/**************************************************************/
361
void UART1_ProcessRxData(void)
360
void UART1_ProcessRxData(void)
362
{
361
{
363
        // return on forwarding uart  or unlocked rx buffer
362
        // return on forwarding uart  or unlocked rx buffer
364
        if(DebugUART != UART1) return;
363
        if(DebugUART != UART1) return;
365
 
364
 
366
        u8 c;
365
        u8 c;
367
        // if rx buffer is not locked
366
        // if rx buffer is not locked
368
        if(UART1_rx_buffer.Locked == FALSE)
367
        if(UART1_rx_buffer.Locked == FALSE)
369
        {
368
        {
370
                //collect data from primary rx fifo
369
                //collect data from primary rx fifo
371
                while(fifo_get(&UART1_rx_fifo, &c))
370
                while(fifo_get(&UART1_rx_fifo, &c))
372
                {
371
                {
373
                        // break if complete frame is collected
372
                        // break if complete frame is collected
374
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
373
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
375
                }
374
                }
376
        }
375
        }
377
        if(UART1_rx_buffer.Locked == FALSE) return;
376
        if(UART1_rx_buffer.Locked == FALSE) return;
378
 
377
 
379
        Point_t * pPoint = NULL;
378
        Point_t * pPoint = NULL;
380
        SerialMsg_t SerialMsg;
379
        SerialMsg_t SerialMsg;
381
 
380
 
382
        // analyze header first
381
        // analyze header first
383
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
382
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
384
        if( SerialMsg.Address == FC_ADDRESS )
383
        if( SerialMsg.Address == FC_ADDRESS )
385
        {
384
        {
386
                switch(SerialMsg.CmdID)
385
                switch(SerialMsg.CmdID)
387
                {
386
                {
388
//                      case 'v': // version
387
//                      case 'v': // version
389
                        case 'y': // serial poti values
388
                        case 'y': // serial poti values
390
                        case 'b': // extern control
389
                        case 'b': // extern control
391
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
390
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
392
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
391
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
393
                                return; //end process rx data
392
                                return; //end process rx data
394
                        break;
393
                        break;
395
                }
394
                }
396
        }
395
        }
397
 
396
 
398
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
397
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
399
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
398
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
400
        switch(SerialMsg.Address) // check for Slave Address
399
        switch(SerialMsg.Address) // check for Slave Address
401
        {
400
        {
402
                case NC_ADDRESS:  // own Slave Address
401
                case NC_ADDRESS:  // own Slave Address
403
                switch(SerialMsg.CmdID)
402
                switch(SerialMsg.CmdID)
404
                {
403
                {
405
                       
404
                       
406
                        case 'f': // ftp command
405
                        case 'f': // ftp command
407
                                UART1_Request_FTP = SerialMsg.pData[0];
406
                                UART1_Request_FTP = SerialMsg.pData[0];
408
 
407
 
409
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
408
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
410
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
409
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
411
                        break;
410
                        break;
412
 
411
 
413
                        case 'z': // connection checker
412
                        case 'z': // connection checker
414
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
413
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
415
                                UART1_Request_Echo = TRUE;
414
                                UART1_Request_Echo = TRUE;
416
                                break;
415
                                break;
417
 
416
 
418
                        case 'e': // request for the text of the error status
417
                        case 'e': // request for the text of the error status
419
                                UART1_Request_ErrorMessage = TRUE;
418
                                UART1_Request_ErrorMessage = TRUE;
420
                                break;
419
                                break;
421
 
420
 
422
                        case 's'://  new target position
421
                        case 's'://  new target position
423
                                pPoint = (Point_t*)SerialMsg.pData;
422
                                pPoint = (Point_t*)SerialMsg.pData;
424
                                if(pPoint->Position.Status == NEWDATA)
423
                                if(pPoint->Position.Status == NEWDATA)
425
                                {
424
                                {
426
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
425
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
427
                                        //pPoint->Index = 1; // must be one after empty list
426
                                        //pPoint->Index = 1; // must be one after empty list
428
                                        PointList_SetAt(pPoint);
427
                                        PointList_SetAt(pPoint);
429
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
428
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
430
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
429
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
431
                                        if(GPS_pWaypoint != NULL) // if new WP exist
430
                                        if(GPS_pWaypoint != NULL) // if new WP exist
432
                                        {   // update WP hold time stamp immediately!
431
                                        {   // update WP hold time stamp immediately!
433
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
432
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
434
                                                {
433
                                                {
435
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
434
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
436
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
435
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
437
                                                }
436
                                                }
438
*/
437
*/
439
                                        }
438
                                        }
440
                                        BeepTime = 50;
439
                                        BeepTime = 50;
441
                                }
440
                                }
442
                                break;
441
                                break;
443
 
442
 
444
                        case 'u': // redirect debug uart
443
                        case 'u': // redirect debug uart
445
                                switch(SerialMsg.pData[0])
444
                                switch(SerialMsg.pData[0])
446
                                {
445
                                {
447
                                        case UART_FLIGHTCTRL:
446
                                        case UART_FLIGHTCTRL:
448
                                                UART2_Init();                           // initialize UART2 to FC pins
447
                                                UART2_Init();                           // initialize UART2 to FC pins
449
                                                fifo_purge(&UART1_rx_fifo);
448
                                                fifo_purge(&UART1_rx_fifo);
450
                                                TIMER2_Deinit();                        // reduce irq load
449
                                                TIMER2_Deinit();                        // reduce irq load
451
                                                DebugUART = UART2;
450
                                                DebugUART = UART2;
452
                                                break;
451
                                                break;
453
                                        case UART_MK3MAG:
452
                                        case UART_MK3MAG:
454
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
453
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
455
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
454
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
456
                                                GPSData.Status = INVALID;
455
                                                GPSData.Status = INVALID;
457
                                                fifo_purge(&UART1_rx_fifo);
456
                                                fifo_purge(&UART1_rx_fifo);
458
                                                DebugUART = UART0;
457
                                                DebugUART = UART0;
459
                                                break;
458
                                                break;
460
                                        case UART_MKGPS:
459
                                        case UART_MKGPS:
461
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
460
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
462
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
461
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
463
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
462
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
464
                                                GPSData.Status = INVALID;
463
                                                GPSData.Status = INVALID;
465
                                                fifo_purge(&UART1_rx_fifo);
464
                                                fifo_purge(&UART1_rx_fifo);
466
                                                DebugUART = UART0;
465
                                                DebugUART = UART0;
467
                                                break;
466
                                                break;
468
                                        default:
467
                                        default:
469
                                                break;
468
                                                break;
470
                                }
469
                                }
471
                                break;
470
                                break;
472
 
471
 
473
                        case 'w'://  Set point in list at index
472
                        case 'w'://  Set point in list at index
474
                                {
473
                                {
475
                                        pPoint = (Point_t*)SerialMsg.pData;
474
                                        pPoint = (Point_t*)SerialMsg.pData;
476
 
475
 
477
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
476
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
478
                                        {
477
                                        {
479
                                                PointList_Clear();
478
                                                PointList_Clear();
480
                                                GPS_pWaypoint = PointList_WPBegin();
479
                                                GPS_pWaypoint = PointList_WPBegin();
481
                                                UART1_Request_WritePoint = 0; // return new point count
480
                                                UART1_Request_WritePoint = 0; // return new point count
482
                                        }
481
                                        }
483
                                        else
482
                                        else
484
                                        {  // update WP in list at index
483
                                        {  // update WP in list at index
485
                                                if(pPoint->Index > MaxNumberOfWaypoints)
484
                                                if(pPoint->Index > MaxNumberOfWaypoints)
486
                                                 {
485
                                                 {
487
                                                  UART1_Request_WritePoint = 254;
486
                                                  UART1_Request_WritePoint = 254;
488
                                                  pPoint->Index = MaxNumberOfWaypoints;
487
                                                  pPoint->Index = MaxNumberOfWaypoints;
489
                                                 }
488
                                                 }
490
                                                 else
489
                                                 else
491
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
490
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
492
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
491
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
493
                                                if(UART1_Request_WritePoint == pPoint->Index)
492
                                                if(UART1_Request_WritePoint == pPoint->Index)
494
                                                {
493
                                                {
495
                                                        BeepTime = 500;
494
                                                        BeepTime = 500;
496
                                                }
495
                                                }
497
                                        }
496
                                        }
498
                                }
497
                                }
499
                                break;
498
                                break;
500
 
499
 
501
                        case 'x'://  Read Waypoint from List
500
                        case 'x'://  Read Waypoint from List
502
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
501
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
503
                                break;
502
                                break;
504
 
503
 
505
                        case 'j':// Set/Get NC-Parameter
504
                        case 'j':// Set/Get NC-Parameter
506
                                switch(SerialMsg.pData[0])
505
                                switch(SerialMsg.pData[0])
507
                                {
506
                                {
508
                                        case 0: // get
507
                                        case 0: // get
509
                                        break;
508
                                        break;
510
 
509
 
511
                                        case 1: // set
510
                                        case 1: // set
512
                                        {
511
                                        {
513
                                                s16 value;
512
                                                s16 value;
514
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
513
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
515
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
514
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
516
                                        }
515
                                        }
517
                                        break;
516
                                        break;
518
 
517
 
519
                                        default:
518
                                        default:
520
                                        break;
519
                                        break;
521
                                }
520
                                }
522
                                UART1_Request_ParameterId = SerialMsg.pData[1];
521
                                UART1_Request_ParameterId = SerialMsg.pData[1];
523
                                UART1_Request_Parameter = TRUE;
522
                                UART1_Request_Parameter = TRUE;
524
                                break;
523
                                break;
525
                        default:
524
                        default:
526
                                // unsupported command recieved
525
                                // unsupported command recieved
527
                                break;
526
                                break;
528
                } // case NC_ADDRESS
527
                } // case NC_ADDRESS
529
                // "break;" is missing here to fall thru to the common commands
528
                // "break;" is missing here to fall thru to the common commands
530
 
529
 
531
                default:  // and any other Slave Address
530
                default:  // and any other Slave Address
532
 
531
 
533
                switch(SerialMsg.CmdID) // check CmdID
532
                switch(SerialMsg.CmdID) // check CmdID
534
                {
533
                {
535
                        case 'a':// request for the labels of the analog debug outputs
534
                        case 'a':// request for the labels of the analog debug outputs
536
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
535
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
537
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
536
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
538
                                break;
537
                                break;
539
                        /*
538
                        /*
540
                        case 'b': // submit extern control
539
                        case 'b': // submit extern control
541
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
540
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
542
                                UART1_ConfirmFrame = ExternControl.Frame;
541
                                UART1_ConfirmFrame = ExternControl.Frame;
543
                                break;
542
                                break;
544
                        */
543
                        */
545
                        case 'd': // request for debug data;
544
                        case 'd': // request for debug data;
546
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
545
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
547
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
546
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
548
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
547
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
549
                                break;
548
                                break;
550
 
549
 
551
                        case 'c': // request for 3D data;
550
                        case 'c': // request for 3D data;
552
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
551
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
553
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
552
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
554
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
553
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
555
                                break;
554
                                break;
556
 
555
 
557
                        case 'k': // request for Motor data;
556
                        case 'k': // request for Motor data;
558
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
557
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
559
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
558
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
560
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
559
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
561
                                break;
560
                                break;
562
 
561
 
563
                        case 'h':// reqest for display line
562
                        case 'h':// reqest for display line
564
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
563
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
565
                                {
564
                                {
566
                                        UART1_DisplayLine = 2;
565
                                        UART1_DisplayLine = 2;
567
                                        UART1_Display_Interval = 0;
566
                                        UART1_Display_Interval = 0;
568
                                }
567
                                }
569
                                else
568
                                else
570
                                {
569
                                {
571
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
570
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
572
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
571
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
573
                                        UART1_DisplayLine = 4;
572
                                        UART1_DisplayLine = 4;
574
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
573
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
575
                                }
574
                                }
576
                                UART1_Request_Display = TRUE;
575
                                UART1_Request_Display = TRUE;
577
                                break;
576
                                break;
578
 
577
 
579
                        case 'l':// reqest for display columns
578
                        case 'l':// reqest for display columns
580
                                MenuItem = SerialMsg.pData[0];
579
                                MenuItem = SerialMsg.pData[0];
581
                                UART1_Request_Display1 = TRUE;
580
                                UART1_Request_Display1 = TRUE;
582
                                break;
581
                                break;
583
 
582
 
584
                        case 'o': // request for navigation information
583
                        case 'o': // request for navigation information
585
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
584
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
586
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
585
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
587
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
586
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
588
                                break;
587
                                break;
589
 
588
 
590
                        case 'v': // request for version info
589
                        case 'v': // request for version info
591
                                UART1_Request_VersionInfo = TRUE;
590
                                UART1_Request_VersionInfo = TRUE;
592
                                break;
591
                                break;
593
                        default:
592
                        default:
594
                                // unsupported command recieved
593
                                // unsupported command recieved
595
                                break;
594
                                break;
596
                }
595
                }
597
                break; // default:
596
                break; // default:
598
        }
597
        }
599
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
598
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
600
}
599
}
601
 
600
 
602
 
601
 
603
/*****************************************************/
602
/*****************************************************/
604
/*                   Send a character                */
603
/*                   Send a character                */
605
/*****************************************************/
604
/*****************************************************/
606
s16 UART1_Putchar(char c)
605
s16 UART1_Putchar(char c)
607
{
606
{
608
        if (c == '\n') UART1_Putchar('\r');
607
        if (c == '\n') UART1_Putchar('\r');
609
        // wait until txd fifo is not full
608
        // wait until txd fifo is not full
610
        while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);
609
        while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);
611
        // transmit byte
610
        // transmit byte
612
        UART_SendData(UART1, c);
611
        UART_SendData(UART1, c);
613
        #ifdef FOLLOW_ME
612
        #ifdef FOLLOW_ME
614
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
613
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
615
        #endif
614
        #endif
616
        return (0);
615
        return (0);
617
}
616
}
618
 
617
 
619
/*****************************************************/
618
/*****************************************************/
620
/*       Send a string to the debug uart              */
619
/*       Send a string to the debug uart              */
621
/*****************************************************/
620
/*****************************************************/
622
void UART1_PutString(u8 *s)
621
void UART1_PutString(u8 *s)
623
{
622
{
624
        if(s == NULL) return;
623
        if(s == NULL) return;
625
        while (*s != '\0' && DebugUART == UART1)
624
        while (*s != '\0' && DebugUART == UART1)
626
        {
625
        {
627
                UART1_Putchar(*s);
626
                UART1_Putchar(*s);
628
                s ++;
627
                s ++;
629
        }
628
        }
630
}
629
}
631
 
630
 
632
 
631
 
633
/**************************************************************/
632
/**************************************************************/
634
/*         Transmit tx buffer via debug uart                  */
633
/*         Transmit tx buffer via debug uart                  */
635
/**************************************************************/
634
/**************************************************************/
636
void UART1_Transmit(void)
635
void UART1_Transmit(void)
637
{
636
{
638
        u8 tmp_tx;
637
        u8 tmp_tx;
639
        if(DebugUART != UART1) return;
638
        if(DebugUART != UART1) return;
640
        // if something has to be send and the txd fifo is not full
639
        // if something has to be send and the txd fifo is not full
641
        if(UART1_tx_buffer.Locked == TRUE)
640
        if(UART1_tx_buffer.Locked == TRUE)
642
        {
641
        {
643
                // while there is some space in the tx fifo
642
                // while there is some space in the tx fifo
644
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
643
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
645
                {
644
                {
646
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
645
                        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
646
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
648
                        #ifdef FOLLOW_ME
647
                        #ifdef FOLLOW_ME
649
                        if(TransmitAlsoToFC)
648
                        if(TransmitAlsoToFC)
650
                        {
649
                        {
651
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
650
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
652
                        }
651
                        }
653
                        #endif
652
                        #endif
654
                        // if terminating character or end of txd buffer reached
653
                        // if terminating character or end of txd buffer reached
655
                        if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
654
                        if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
656
                        {
655
                        {
657
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
656
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
658
                                #ifdef FOLLOW_ME
657
                                #ifdef FOLLOW_ME
659
                                TransmitAlsoToFC = 0;
658
                                TransmitAlsoToFC = 0;
660
                                #endif
659
                                #endif
661
                                break; // end while loop
660
                                break; // end while loop
662
                        }
661
                        }
663
                }
662
                }
664
        }
663
        }
665
}
664
}
666
 
665
 
667
/**************************************************************/
666
/**************************************************************/
668
/* Send the answers to incomming commands at the debug uart   */
667
/* Send the answers to incomming commands at the debug uart   */
669
/**************************************************************/
668
/**************************************************************/
670
void UART1_TransmitTxData(void)
669
void UART1_TransmitTxData(void)
671
{
670
{
672
static u8 motorindex1 = 255, motorindex2 = 0;
671
static u8 motorindex1 = 255, motorindex2 = 0;
673
        if(DebugUART != UART1) return;
672
        if(DebugUART != UART1) return;
674
 
673
 
675
        if(CheckDelay(UART1_AboTimeOut))
674
        if(CheckDelay(UART1_AboTimeOut))
676
        {
675
        {
677
                UART1_DebugData_Interval = 0;
676
                UART1_DebugData_Interval = 0;
678
                UART1_NaviData_Interval = 0;
677
                UART1_NaviData_Interval = 0;
679
                UART1_Data3D_Interval = 0;
678
                UART1_Data3D_Interval = 0;
680
                UART1_Display_Interval = 0;
679
                UART1_Display_Interval = 0;
681
        }
680
        }
682
 
681
 
683
        UART1_Transmit(); // output pending bytes in tx buffer
682
        UART1_Transmit(); // output pending bytes in tx buffer
684
        if((UART1_tx_buffer.Locked == TRUE)) return;
683
        if((UART1_tx_buffer.Locked == TRUE)) return;
685
 
684
 
686
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
685
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
687
        {
686
        {
688
                s16 ParamValue;
687
                s16 ParamValue;
689
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
688
                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
689
                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;
690
                UART1_Request_Parameter = FALSE;
692
        }
691
        }
693
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
692
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
694
        {
693
        {
695
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
694
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
696
                Echo = 0; // reset echo value
695
                Echo = 0; // reset echo value
697
                UART1_Request_Echo = FALSE;
696
                UART1_Request_Echo = FALSE;
698
        }
697
        }
699
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
698
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
700
        {
699
        {
701
                CheckFTPCommand(UART1_Request_FTP);
700
                CheckFTPCommand(UART1_Request_FTP);
702
                UART1_Request_FTP = FALSE;
701
                UART1_Request_FTP = FALSE;
703
        }
702
        }
704
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
703
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
705
        {
704
        {
706
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
705
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
707
                UART1_Request_WritePoint = 0xFF;
706
                UART1_Request_WritePoint = 0xFF;
708
        }
707
        }
709
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
708
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
710
        {
709
        {
711
                u8 PointCount = PointList_GetCount();
710
                u8 PointCount = PointList_GetCount();
712
                if (UART1_Request_ReadPoint <= PointCount)
711
                if (UART1_Request_ReadPoint <= PointCount)
713
                {
712
                {
714
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
713
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
715
                }
714
                }
716
                else
715
                else
717
                {
716
                {
718
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
717
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
719
                }
718
                }
720
                UART1_Request_ReadPoint = 0;
719
                UART1_Request_ReadPoint = 0;
721
        }
720
        }
722
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
721
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
723
        {
722
        {
724
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
723
                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;
724
                UART1_Request_DebugLabel = 0xFF;
726
        }
725
        }
727
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
726
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
728
        {
727
        {
729
                NaviData.Errorcode = ErrorCode;
728
                NaviData.Errorcode = ErrorCode;
730
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
729
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
731
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
730
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
732
                UART1_Request_NaviData = FALSE;
731
                UART1_Request_NaviData = FALSE;
-
 
732
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
733
        }
733
        }
734
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
734
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
735
        {
735
        {
736
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
736
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
737
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
737
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
738
                UART1_Request_DebugData = FALSE;
738
                UART1_Request_DebugData = FALSE;
739
        }
739
        }
740
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
740
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
741
        {
741
        {
742
                Data3D.StickNick = FC.StickNick;
742
                Data3D.StickNick = FC.StickNick;
743
                Data3D.StickRoll = FC.StickRoll;
743
                Data3D.StickRoll = FC.StickRoll;
744
        Data3D.StickYaw = FC.StickYaw;
744
        Data3D.StickYaw = FC.StickYaw;
745
        Data3D.StickGas = FC.StickGas;
745
        Data3D.StickGas = FC.StickGas;
746
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
746
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
747
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
747
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
748
                UART1_Request_Data3D = FALSE;
748
                UART1_Request_Data3D = FALSE;
749
        }
749
        }
750
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
750
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
751
        {
751
        {
752
                do
752
                do
753
                {
753
                {
754
                 motorindex1++;
754
                 motorindex1++;
755
                 motorindex1%=12;
755
                 motorindex1%=12;
756
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
756
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
757
                 if(motorindex1 == motorindex2) break;
757
                 if(motorindex1 == motorindex2) break;
758
                }
758
                }
759
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
759
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
760
 
760
 
761
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
761
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
762
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
762
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
763
                UART1_Request_MotorData = FALSE;
763
                UART1_Request_MotorData = FALSE;
764
        }
764
        }
765
        /*
765
        /*
766
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
766
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
767
        {
767
        {
768
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
768
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
769
                UART1_ConfirmFrame = 0;
769
                UART1_ConfirmFrame = 0;
770
        }
770
        }
771
        */
771
        */
772
        /*
772
        /*
773
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
773
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
774
        {
774
        {
775
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
775
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
776
                UART1_Request_ExternalControl = FALSE;
776
                UART1_Request_ExternalControl = FALSE;
777
        }
777
        }
778
        */
778
        */
779
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
779
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
780
        {
780
        {
781
                if(UART1_DisplayLine > 3)
781
                if(UART1_DisplayLine > 3)
782
                {
782
                {
783
                        Menu_Update(UART1_DisplayKeys);
783
                        Menu_Update(UART1_DisplayKeys);
784
                        UART1_DisplayKeys = 0;
784
                        UART1_DisplayKeys = 0;
785
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
785
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
786
                }
786
                }
787
                else
787
                else
788
                {
788
                {
789
                        UART1_DisplayLine = 2;
789
                        UART1_DisplayLine = 2;
790
                        sprintf(text,"!!! incompatible !!!");
790
                        sprintf(text,"!!! incompatible !!!");
791
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
791
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
792
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
792
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
793
                }
793
                }
794
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
794
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
795
                UART1_Request_Display = FALSE;
795
                UART1_Request_Display = FALSE;
796
        }
796
        }
797
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
797
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
798
        {
798
        {
799
                Menu_Update(0);
799
                Menu_Update(0);
800
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
800
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
801
                UART1_Request_Display1 = FALSE;
801
                UART1_Request_Display1 = FALSE;
802
        }
802
        }
803
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
803
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
804
        {
804
        {
805
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
805
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
806
                UART1_Request_VersionInfo = FALSE;
806
                UART1_Request_VersionInfo = FALSE;
807
        }
807
        }
808
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
808
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
809
        {
809
        {
810
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
810
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
811
                UART1_Request_ErrorMessage = FALSE;
811
                UART1_Request_ErrorMessage = FALSE;
812
        }
812
        }
813
#ifdef FOLLOW_ME
813
#ifdef FOLLOW_ME
814
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
814
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
815
        {
815
        {
816
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
816
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
817
                {
817
                {
818
                        TransmitAlsoToFC = 1;
818
                        TransmitAlsoToFC = 1;
819
                        // update FollowMe content
819
                        // update FollowMe content
820
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
820
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
821
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
821
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
822
                        FollowMe.Position.Status = NEWDATA;
822
                        FollowMe.Position.Status = NEWDATA;
823
                        FollowMe.Position.Altitude = 1;
823
                        FollowMe.Position.Altitude = 1;
824
                        // 0  -> no Orientation
824
                        // 0  -> no Orientation
825
                        // 1-360 -> CompassCourse Setpoint
825
                        // 1-360 -> CompassCourse Setpoint
826
                        // -1 -> points to  WP1 -> itself
826
                        // -1 -> points to  WP1 -> itself
827
                FollowMe.Heading = -1;
827
                FollowMe.Heading = -1;
828
                        FollowMe.ToleranceRadius = 1;
828
                        FollowMe.ToleranceRadius = 1;
829
                        FollowMe.HoldTime = 60;
829
                        FollowMe.HoldTime = 60;
830
                        FollowMe.Event_Flag = 1;
830
                        FollowMe.Event_Flag = 1;
831
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
831
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
832
                        FollowMe.Type = POINT_TYPE_WP;
832
                        FollowMe.Type = POINT_TYPE_WP;
833
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
833
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
834
                        FollowMe.AltitudeRate = 0;                        // do not change height
834
                        FollowMe.AltitudeRate = 0;                        // do not change height
835
                        FollowMe.reserve[0] = 0;                // reserve
835
                        FollowMe.reserve[0] = 0;                // reserve
836
                        FollowMe.reserve[1] = 0;                // reserve
836
                        FollowMe.reserve[1] = 0;                // reserve
837
                        FollowMe.reserve[2] = 0;                // reserve
837
                        FollowMe.reserve[2] = 0;                // reserve
838
                        FollowMe.reserve[3] = 0;                // reserve
838
                        FollowMe.reserve[3] = 0;                // reserve
839
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
839
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
840
                }
840
                }
841
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
841
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
842
        }
842
        }
843
#endif
843
#endif
844
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
844
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
845
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
845
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
846
        {
846
        {
847
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
847
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
848
                SendDebugOutput = 0;
848
                SendDebugOutput = 0;
849
        }
849
        }
850
#endif
850
#endif
851
        UART1_Transmit(); // output pending bytes in tx buffer
851
        UART1_Transmit(); // output pending bytes in tx buffer
852
}
852
}
853
 
853
 
854
 
854