Subversion Repositories NaviCtrl

Rev

Rev 437 | Rev 451 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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