Subversion Repositories NaviCtrl

Rev

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

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