Subversion Repositories NaviCtrl

Rev

Rev 480 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 7
// + Software Nutzungsbedingungen (english version: see below)
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
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.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
41 ingob 13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
41 ingob 31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 32
// + Software LICENSING TERMS
41 ingob 33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 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 
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.
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
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
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
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
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.
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.
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.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
41 ingob 55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <stdio.h>
470 killagreg 57
#include <stdlib.h>
41 ingob 58
#include <stdarg.h>
59
#include <string.h>
60
 
61
#include "91x_lib.h"
251 killagreg 62
#include "main.h"
136 killagreg 63
#include "config.h"
41 ingob 64
#include "menu.h"
65
#include "GPS.h"
472 holgerb 66
#include "i2c0.h"
470 killagreg 67
#include "i2c1.h"
41 ingob 68
#include "uart0.h"
69
#include "uart1.h"
70
#include "uart2.h"
119 killagreg 71
#include "timer1.h"
139 killagreg 72
#include "timer2.h"
73
#include "analog.h"
243 killagreg 74
#include "compass.h"
41 ingob 75
#include "waypoints.h"
110 killagreg 76
#include "mkprotocol.h"
156 killagreg 77
#include "params.h"
153 killagreg 78
#include "fifo.h"
315 killagreg 79
#include "debug.h"
358 holgerb 80
#include "spi_slave.h"
349 ingob 81
#include "ftphelper.h"
378 holgerb 82
#include "led.h"
433 ingob 83
#include "fat16.h"
41 ingob 84
 
433 ingob 85
 
41 ingob 86
#define FALSE   0
87
#define TRUE    1
88
 
351 holgerb 89
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
198 killagreg 90
u32 UART1_AboTimeOut = 0;
92 killagreg 91
 
110 killagreg 92
u8 UART1_Request_VersionInfo    = FALSE;
93
u8 UART1_Request_ExternalControl= FALSE;
94
u8 UART1_Request_Display                = FALSE;
95
u8 UART1_Request_Display1               = FALSE;
96
u8 UART1_Request_DebugData              = FALSE;
97
u8 UART1_Request_DebugLabel             = 255;
98
u8 UART1_Request_NaviData               = FALSE;
99
u8 UART1_Request_ErrorMessage   = FALSE;
295 killagreg 100
u8 UART1_Request_WritePoint             = 0xFF;
280 killagreg 101
u8 UART1_Request_ReadPoint              = 0;
110 killagreg 102
u8 UART1_Request_Data3D             = FALSE;
358 holgerb 103
u8 UART1_Request_MotorData          = FALSE;
110 killagreg 104
u8 UART1_Request_Echo               = FALSE;
156 killagreg 105
u8 UART1_Request_ParameterId    = 0;
158 killagreg 106
u8 UART1_Request_Parameter              = FALSE;
447 holgerb 107
u8 UART1_Request_SystemTime             = FALSE;
201 killagreg 108
u8 UART1_DisplayKeys                    = 0;
112 killagreg 109
u8 UART1_DisplayLine                    = 0;
110
u8 UART1_ConfirmFrame                   = 0;
349 ingob 111
u8 UART1_Request_FTP                    = FALSE;
360 holgerb 112
u8 LastTransmittedFCStatusFlags2 = 0;
398 holgerb 113
u8 UART1_ExternalControlConfirmFrame = FALSE;
466 holgerb 114
u8 Send_NMEA_RMC = FALSE;
41 ingob 115
 
116
UART_TypeDef *DebugUART = UART1;
117
 
314 killagreg 118
#ifdef FOLLOW_ME
119
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
315 killagreg 120
u32 UART1_FollowMe_Timer        = 0;
314 killagreg 121
Point_t FollowMe;
122
#endif
123
 
153 killagreg 124
// the primary rx fifo
378 holgerb 125
#define UART1_RX_FIFO_LEN 1024
153 killagreg 126
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
127
fifo_t UART1_rx_fifo;
110 killagreg 128
 
129
// the rx buffer
378 holgerb 130
#define UART1_RX_BUFFER_LEN  1024
110 killagreg 131
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
132
Buffer_t UART1_rx_buffer;
133
 
153 killagreg 134
// the tx buffer
349 ingob 135
#define UART1_TX_BUFFER_LEN  1024
153 killagreg 136
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
137
Buffer_t UART1_tx_buffer;
138
 
92 killagreg 139
volatile u8 SerialLinkOkay = 0;
41 ingob 140
 
150 killagreg 141
u8 text[200];
41 ingob 142
 
143
const u8 ANALOG_LABEL[32][16] =
144
{
90 killagreg 145
   //1234567890123456
41 ingob 146
        "AngleNick       ", //0
147
        "AngleRoll       ",
148
        "AccNick         ",
149
        "AccRoll         ",
214 killagreg 150
        "OperatingRadius ",
200 killagreg 151
        "FC-Flags        ", //5
95 killagreg 152
        "NC-Flags        ",
480 holgerb 153
        "Voltage  [0,1V] ",
154
        "Current  [0,1A] ",
41 ingob 155
        "GPS Data        ",
61 holgerb 156
        "CompassHeading  ", //10
157
        "GyroHeading     ",
419 holgerb 158
        "SPI Error       ", // achtung: muss auf 12 bleiben
41 ingob 159
        "SPI Okay        ",
189 killagreg 160
        "I2C Error       ",
41 ingob 161
        "I2C Okay        ", //15
339 holgerb 162
        "16              ",
437 holgerb 163
        "17              ",
164
        "18              ",
165
        "19              ", // SD-Card-time 
330 holgerb 166
        "EarthMagnet [%] ", //20
456 holgerb 167
        "Ground Speed    ", //  "Z_Speed         ",
338 holgerb 168
        "N_Speed         ",
169
        "E_Speed         ",
310 holgerb 170
        "Magnet X        ",
338 holgerb 171
        "Magnet Y        ", //25
310 holgerb 172
        "Magnet Z        ",
41 ingob 173
        "Distance N      ",
174
        "Distance E      ",
175
        "GPS_Nick        ",
176
        "GPS_Roll        ", //30
177
        "Used_Sats       "
178
};
179
 
112 killagreg 180
DebugOut_t DebugOut;
181
ExternControl_t ExternControl;
182
UART_VersionInfo_t UART_VersionInfo;
183
NaviData_t NaviData;
184
Data3D_t Data3D;
185
u16 Echo; // 2 bytes recieved will be sent back as echo
41 ingob 186
 
193 killagreg 187
u32 UART1_DebugData_Timer = 0;
198 killagreg 188
u32 UART1_DebugData_Interval = 0;       // in ms
193 killagreg 189
u32 UART1_NaviData_Timer = 0;
198 killagreg 190
u32 UART1_NaviData_Interval = 0;        // in ms
191
u32 UART1_Data3D_Timer = 0;
192
u32 UART1_Data3D_Interval = 0;          // in ms
358 holgerb 193
u32 UART1_MotorData_Timer = 0;
194
u32 UART1_MotorData_Interval = 0;               // in ms
193 killagreg 195
u32 UART1_Display_Timer = 0;
201 killagreg 196
u32 UART1_Display_Interval = 0;         // in ms
460 holgerb 197
u32 NMEA_Timer = 0;
198
u32 NMEA_Interval = 500;// in ms
112 killagreg 199
 
41 ingob 200
/********************************************************/
201
/*            Initialization the UART1                  */
202
/********************************************************/
203
void UART1_Init (void)
204
{
205
        GPIO_InitTypeDef GPIO_InitStructure;
206
        UART_InitTypeDef UART_InitStructure;
207
 
153 killagreg 208
        // initialize txd buffer
209
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
165 killagreg 210
 
153 killagreg 211
        // initialize rxd buffer
212
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
213
 
244 killagreg 214
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
215
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
153 killagreg 216
 
41 ingob 217
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
218
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
219
 
220
        /*Configure UART1_Rx pin GPIO3.2*/
221
        GPIO_StructInit(&GPIO_InitStructure);
222
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
223
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
224
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
196 killagreg 225
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
41 ingob 226
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
227
        GPIO_Init(GPIO3, &GPIO_InitStructure);
228
 
229
        /*Configure UART1_Tx pin GPIO3.3*/
230
        GPIO_StructInit(&GPIO_InitStructure);
231
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
232
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
233
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
234
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
235
        GPIO_Init(GPIO3, &GPIO_InitStructure);
236
 
237
        /* UART1 configured as follow:
238
        - Word Length = 8 Bits
239
        - One Stop Bit
240
        - No parity
241
        - BaudRate = 57600 baud
242
        - Hardware flow control Disabled
243
        - Receive and transmit enabled
244
        - Receive and transmit FIFOs are Disabled
245
        */
246
        UART_StructInit(&UART_InitStructure);
247
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
248
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
249
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
110 killagreg 250
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
41 ingob 251
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
252
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
253
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
254
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
139 killagreg 255
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
41 ingob 256
 
257
        UART_DeInit(UART1); // reset uart 1     to default
165 killagreg 258
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
41 ingob 259
        // enable uart 1 interrupts selective
260
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
261
        UART_Cmd(UART1, ENABLE); // enable uart 1
136 killagreg 262
        // configure the uart 1 interupt line
263
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
244 killagreg 264
        // enable the uart 1 IRQ
41 ingob 265
        VIC_ITCmd(UART1_ITLine, ENABLE);
153 killagreg 266
 
41 ingob 267
        // initialize the debug timer
110 killagreg 268
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
269
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
461 holgerb 270
        NMEA_Timer = SetDelay(14000);
110 killagreg 271
 
41 ingob 272
        // Fill Version Info Structure
273
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
274
        UART_VersionInfo.SWMinor = VERSION_MINOR;
275
        UART_VersionInfo.SWPatch = VERSION_PATCH;
276
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
277
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
454 holgerb 278
    UART_VersionInfo.HWMajor = Version_HW;
279
    UART_VersionInfo.reserved2 = 0;  
280
        UART_VersionInfo.Flags = 0;
89 killagreg 281
        NaviData.Version = NAVIDATA_VERSION;
92 killagreg 282
 
212 killagreg 283
        UART1_PutString("\r\n UART1 init...ok");
41 ingob 284
}
285
 
286
 
287
/****************************************************************/
288
/*               USART1 receiver ISR                            */
289
/****************************************************************/
290
void UART1_IRQHandler(void)
291
{
292
        static u8 abortState = 0;
293
        u8 c;
294
 
295
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
296
        {
153 killagreg 297
                // clear the pending bits!
41 ingob 298
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
299
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
300
                // if debug UART is not UART1
301
                if (DebugUART != UART1)
302
                {       // forward received data to the debug UART tx buffer
303
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
304
                        {
305
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
306
                                c = UART_ReceiveData(UART1);
307
 
308
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
309
                                switch (abortState)
310
                                {
165 killagreg 311
                                        case 0:
139 killagreg 312
                                                if (c == 27) abortState++;
41 ingob 313
                                                break;
165 killagreg 314
                                        case 1:
315
                                                if (c == 27) abortState++;
139 killagreg 316
                                                else abortState = 0;
41 ingob 317
                                        break;
139 killagreg 318
                                        case 2:
165 killagreg 319
                                                if (c == 0x55) abortState++;
139 killagreg 320
                                                else abortState = 0;
41 ingob 321
                                                break;
139 killagreg 322
                                        case 3:
323
                                                if (c == 0xAA) abortState++;
324
                                                else abortState = 0;
41 ingob 325
                                                break;
165 killagreg 326
                                        case 4:
139 killagreg 327
                                                if (c == 0x00)
328
                                                {
329
                                                        if(DebugUART == UART0)
330
                                                        {
245 killagreg 331
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
179 killagreg 332
                                                                TIMER2_Init(); // enbable servo outputs
153 killagreg 333
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
139 killagreg 334
                                                        }
335
                                                        DebugUART = UART1;
165 killagreg 336
                                                }
139 killagreg 337
                                                abortState = 0;
338
                                                break;
339
                                } // end switch abort state
136 killagreg 340
                                // if the Debug uart is not UART1, redirect input to the Debug UART
110 killagreg 341
                                if (DebugUART != UART1)
342
                                {
343
                                        // wait for space in the tx buffer of the DebugUART
344
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
141 killagreg 345
                                        // move byte to the tx fifo of the debug uart
110 killagreg 346
                                        UART_SendData(DebugUART, c);
347
                                }
41 ingob 348
                        }
349
                }
350
                else  // DebugUART == UART1 (normal operation)
351
                {
110 killagreg 352
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
154 killagreg 353
                        { // some byes in the hardware fifo
153 killagreg 354
                            // get byte from hardware fifo
41 ingob 355
                        c = UART_ReceiveData(UART1);
153 killagreg 356
                                // put into the software fifo
357
                                if(!fifo_put(&UART1_rx_fifo, c))
358
                                {       // fifo overflow
155 killagreg 359
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
153 killagreg 360
                                }
154 killagreg 361
                        } // EOF while some byes in the hardware fifo
41 ingob 362
                } // eof DebugUart = UART1
363
        }
195 killagreg 364
 
380 holgerb 365
 
366
 
378 holgerb 367
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
41 ingob 368
}
369
 
370
/**************************************************************/
371
/* Process incomming data from debug uart                     */
372
/**************************************************************/
373
void UART1_ProcessRxData(void)
374
{
154 killagreg 375
        // return on forwarding uart  or unlocked rx buffer
380 holgerb 376
        u8 c;
155 killagreg 377
        if(DebugUART != UART1) return;
378
        // if rx buffer is not locked
379
        if(UART1_rx_buffer.Locked == FALSE)
315 killagreg 380
        {
244 killagreg 381
                //collect data from primary rx fifo
155 killagreg 382
                while(fifo_get(&UART1_rx_fifo, &c))
315 killagreg 383
                {
244 killagreg 384
                        // break if complete frame is collected
155 killagreg 385
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
386
                }
387
        }
388
        if(UART1_rx_buffer.Locked == FALSE) return;
389
 
280 killagreg 390
        Point_t * pPoint = NULL;
153 killagreg 391
        SerialMsg_t SerialMsg;
41 ingob 392
 
190 killagreg 393
        // analyze header first
394
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
194 killagreg 395
        if( SerialMsg.Address == FC_ADDRESS )
41 ingob 396
        {
192 killagreg 397
                switch(SerialMsg.CmdID)
398
                {
231 holgerb 399
//                      case 'v': // version
398 holgerb 400
                        case 'b': // extern control
401
                                          UART1_ExternalControlConfirmFrame = 1;
192 killagreg 402
                        case 'y': // serial poti values
403
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
404
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
194 killagreg 405
                                return; //end process rx data
192 killagreg 406
                        break;
407
                }
195 killagreg 408
        }
190 killagreg 409
 
410
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
411
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
412
        switch(SerialMsg.Address) // check for Slave Address
413
        {
41 ingob 414
                case NC_ADDRESS:  // own Slave Address
190 killagreg 415
                switch(SerialMsg.CmdID)
41 ingob 416
                {
447 holgerb 417
                        case 't': // request for the GPS time 
418
                                UART1_Request_SystemTime  = TRUE;
419
                                break;
420
 
349 ingob 421
                        case 'f': // ftp command
422
                                UART1_Request_FTP = SerialMsg.pData[0];
423
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE) 
424
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
425
                        break;
426
 
101 holgerb 427
                        case 'z': // connection checker
110 killagreg 428
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
429
                                UART1_Request_Echo = TRUE;
92 killagreg 430
                                break;
431
 
41 ingob 432
                        case 'e': // request for the text of the error status
110 killagreg 433
                                UART1_Request_ErrorMessage = TRUE;
41 ingob 434
                                break;
435
 
436
                        case 's'://  new target position
280 killagreg 437
                                pPoint = (Point_t*)SerialMsg.pData;
438
                                if(pPoint->Position.Status == NEWDATA)
41 ingob 439
                                {
315 killagreg 440
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
300 holgerb 441
                                        //pPoint->Index = 1; // must be one after empty list
296 killagreg 442
                                        PointList_SetAt(pPoint);
315 killagreg 443
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
295 killagreg 444
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
302 holgerb 445
                                        if(GPS_pWaypoint != NULL) // if new WP exist
446
                                        {   // update WP hold time stamp immediately!
451 holgerb 447
                                                EnableNewWpHeading();
302 holgerb 448
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
449
                                                {
450
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
451
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
452
                                                }
453
*/
454
                                        }
295 killagreg 455
                                        BeepTime = 50;
41 ingob 456
                                }
457
                                break;
458
 
459
                        case 'u': // redirect debug uart
110 killagreg 460
                                switch(SerialMsg.pData[0])
41 ingob 461
                                {
462
                                        case UART_FLIGHTCTRL:
463
                                                UART2_Init();                           // initialize UART2 to FC pins
153 killagreg 464
                                                fifo_purge(&UART1_rx_fifo);
180 killagreg 465
                                                TIMER2_Deinit();                        // reduce irq load
41 ingob 466
                                                DebugUART = UART2;
467
                                                break;
468
                                        case UART_MK3MAG:
255 killagreg 469
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
41 ingob 470
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
471
                                                GPSData.Status = INVALID;
153 killagreg 472
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 473
                                                DebugUART = UART0;
474
                                                break;
475
                                        case UART_MKGPS:
255 killagreg 476
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
179 killagreg 477
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
245 killagreg 478
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
41 ingob 479
                                                GPSData.Status = INVALID;
153 killagreg 480
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 481
                                                DebugUART = UART0;
482
                                                break;
153 killagreg 483
                                        default:
484
                                                break;
41 ingob 485
                                }
486
                                break;
487
 
295 killagreg 488
                        case 'w'://  Set point in list at index
165 killagreg 489
                                {
280 killagreg 490
                                        pPoint = (Point_t*)SerialMsg.pData;
224 killagreg 491
 
280 killagreg 492
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
224 killagreg 493
                                        {
280 killagreg 494
                                                PointList_Clear();
495
                                                GPS_pWaypoint = PointList_WPBegin();
315 killagreg 496
                                                UART1_Request_WritePoint = 0; // return new point count
165 killagreg 497
                                        }
227 killagreg 498
                                        else
295 killagreg 499
                                        {  // update WP in list at index
351 holgerb 500
                                                if(pPoint->Index > MaxNumberOfWaypoints)
501
                                                 {
502
                                                  UART1_Request_WritePoint = 254;
503
                                                  pPoint->Index = MaxNumberOfWaypoints;
504
                                                 }
505
                                                 else
506
                                                 UART1_Request_WritePoint = PointList_SetAt(pPoint);
315 killagreg 507
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
295 killagreg 508
                                                if(UART1_Request_WritePoint == pPoint->Index)
225 killagreg 509
                                                {
510
                                                        BeepTime = 500;
165 killagreg 511
                                                }
512
                                        }
41 ingob 513
                                }
514
                                break;
515
 
92 killagreg 516
                        case 'x'://  Read Waypoint from List
280 killagreg 517
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
92 killagreg 518
                                break;
519
 
160 holgerb 520
                        case 'j':// Set/Get NC-Parameter
156 killagreg 521
                                switch(SerialMsg.pData[0])
522
                                {
523
                                        case 0: // get
524
                                        break;
165 killagreg 525
 
156 killagreg 526
                                        case 1: // set
159 killagreg 527
                                        {
528
                                                s16 value;
529
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
530
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
531
                                        }
156 killagreg 532
                                        break;
533
 
534
                                        default:
165 killagreg 535
                                        break;
156 killagreg 536
                                }
537
                                UART1_Request_ParameterId = SerialMsg.pData[1];
538
                                UART1_Request_Parameter = TRUE;
539
                                break;
41 ingob 540
                        default:
541
                                // unsupported command recieved
542
                                break;
543
                } // case NC_ADDRESS
65 killagreg 544
                // "break;" is missing here to fall thru to the common commands
41 ingob 545
 
546
                default:  // and any other Slave Address
547
 
190 killagreg 548
                switch(SerialMsg.CmdID) // check CmdID
41 ingob 549
                {
550
                        case 'a':// request for the labels of the analog debug outputs
110 killagreg 551
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
552
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
41 ingob 553
                                break;
193 killagreg 554
                        /*
41 ingob 555
                        case 'b': // submit extern control
121 killagreg 556
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
112 killagreg 557
                                UART1_ConfirmFrame = ExternControl.Frame;
41 ingob 558
                                break;
195 killagreg 559
                        */
41 ingob 560
                        case 'd': // request for debug data;
110 killagreg 561
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
562
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
198 killagreg 563
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
41 ingob 564
                                break;
565
 
63 killagreg 566
                        case 'c': // request for 3D data;
110 killagreg 567
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
568
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
198 killagreg 569
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
63 killagreg 570
                                break;
275 killagreg 571
 
358 holgerb 572
                        case 'k': // request for Motor data;
573
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
574
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
575
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
576
                                break;
577
 
41 ingob 578
                        case 'h':// reqest for display line
193 killagreg 579
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
580
                                {
581
                                        UART1_DisplayLine = 2;
195 killagreg 582
                                        UART1_Display_Interval = 0;
193 killagreg 583
                                }
584
                                else
585
                                {
201 killagreg 586
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
193 killagreg 587
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
588
                                        UART1_DisplayLine = 4;
198 killagreg 589
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
193 killagreg 590
                                }
110 killagreg 591
                                UART1_Request_Display = TRUE;
41 ingob 592
                                break;
593
 
594
                        case 'l':// reqest for display columns
110 killagreg 595
                                MenuItem = SerialMsg.pData[0];
596
                                UART1_Request_Display1 = TRUE;
41 ingob 597
                                break;
64 holgerb 598
 
599
                        case 'o': // request for navigation information
110 killagreg 600
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
601
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
198 killagreg 602
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
64 holgerb 603
                                break;
604
 
41 ingob 605
                        case 'v': // request for version info
110 killagreg 606
                                UART1_Request_VersionInfo = TRUE;
41 ingob 607
                                break;
608
                        default:
609
                                // unsupported command recieved
610
                                break;
611
                }
612
                break; // default:
613
        }
153 killagreg 614
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
41 ingob 615
}
616
 
617
 
618
/*****************************************************/
619
/*                   Send a character                */
620
/*****************************************************/
110 killagreg 621
s16 UART1_Putchar(char c)
41 ingob 622
{
362 holgerb 623
        u32 timeout = 10000;
110 killagreg 624
        if (c == '\n') UART1_Putchar('\r');
41 ingob 625
        // wait until txd fifo is not full
362 holgerb 626
        while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET) if(--timeout == 0) return(0);
41 ingob 627
        // transmit byte
628
        UART_SendData(UART1, c);
314 killagreg 629
        #ifdef FOLLOW_ME
630
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
631
        #endif
41 ingob 632
        return (0);
633
}
634
 
635
/*****************************************************/
636
/*       Send a string to the debug uart              */
637
/*****************************************************/
110 killagreg 638
void UART1_PutString(u8 *s)
41 ingob 639
{
640
        if(s == NULL) return;
641
        while (*s != '\0' && DebugUART == UART1)
642
        {
110 killagreg 643
                UART1_Putchar(*s);
41 ingob 644
                s ++;
645
        }
646
}
647
 
113 killagreg 648
 
110 killagreg 649
/**************************************************************/
650
/*         Transmit tx buffer via debug uart                  */
651
/**************************************************************/
652
void UART1_Transmit(void)
653
{
654
        u8 tmp_tx;
655
        if(DebugUART != UART1) return;
656
        // if something has to be send and the txd fifo is not full
113 killagreg 657
        if(UART1_tx_buffer.Locked == TRUE)
110 killagreg 658
        {
113 killagreg 659
                // while there is some space in the tx fifo
660
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
110 killagreg 661
                {
113 killagreg 662
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
663
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
314 killagreg 664
                        #ifdef FOLLOW_ME
315 killagreg 665
                        if(TransmitAlsoToFC)
666
                        {
314 killagreg 667
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
668
                        }
669
                        #endif
113 killagreg 670
                        // if terminating character or end of txd buffer reached
461 holgerb 671
                        if((tmp_tx == '\0') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
113 killagreg 672
                        {
673
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
314 killagreg 674
                                #ifdef FOLLOW_ME
675
                                TransmitAlsoToFC = 0;
676
                                #endif
113 killagreg 677
                                break; // end while loop
678
                        }
110 killagreg 679
                }
680
        }
681
}
41 ingob 682
 
460 holgerb 683
//$GPGGA,HHMMSS.ss,BBBB.BBBB,b,LLLLL.LLLL,l,Q,NN,D.D,H.H,h,G.G,g,A.A,RRRR*PP
684
//$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5,M,48.0,M,,*45
685
//$GPGGA,092120.20,,,,,0,00,99.99,,,,,,*6C
466 holgerb 686
//http://www.kowoma.de/gps/zusatzerklaerungen/NMEA.htm
460 holgerb 687
void CreateNmeaGGA(void)
688
{
466 holgerb 689
 unsigned char array[200], i = 0, crc = 0, x;
460 holgerb 690
 long tmp1, tmp2;
691
 i += sprintf(array, "$GPGGA,");
692
// +++++++++++++++++++++++++++++++++++++++++++
693
 if(SystemTime.Valid)
694
  {
695
   i += sprintf(&array[i], "%02d%02d%02d.%02d,",SystemTime.Hour,SystemTime.Min,SystemTime.Sec,SystemTime.mSec/10);
696
  }
697
 else
698
  {
699
  i += sprintf(&array[i], ",");
700
  }
701
// +++++++++++++++++++++++++++++++++++++++++++
702
 if(GPSData.Flags & FLAG_GPSFIXOK)
703
 {
704
  tmp1 = abs(GPSData.Position.Latitude)/10000000L;
472 holgerb 705
  i += sprintf(&array[i],"%02d",(int)tmp1);
378 holgerb 706
 
460 holgerb 707
  tmp1 = abs(GPSData.Position.Latitude)%10000000L;
708
  tmp1 *= 6; // in Minuten
709
  tmp2 = tmp1 / 1000000L;
472 holgerb 710
  i += sprintf(&array[i],"%02d",(int)tmp2);
460 holgerb 711
  tmp2 = tmp1 % 1000000L;
712
  tmp2 /= 10; // eine Stelle zu viel
472 holgerb 713
  i += sprintf(&array[i],".%05d,",(int)tmp2);
460 holgerb 714
 
715
  if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
716
  else i += sprintf(&array[i],"S,");
717
// +++++++++++++++++++++++++++++++++++++++++++
718
 
719
  tmp1 = abs(GPSData.Position.Longitude)/10000000L;
472 holgerb 720
  i += sprintf(&array[i],"%03d",(int)tmp1);
460 holgerb 721
 
722
  tmp1 = abs(GPSData.Position.Longitude)%10000000L;
723
  tmp1 *= 6; // in Minuten
724
  tmp2 = tmp1 / 1000000L;
472 holgerb 725
  i += sprintf(&array[i],"%02d",(int)tmp2);
460 holgerb 726
  tmp2 = tmp1 % 1000000L;
727
  tmp2 /= 10; // eine Stelle zu viel
472 holgerb 728
  i += sprintf(&array[i],".%05d,",(int)tmp2);
460 holgerb 729
 
730
 
731
  if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
732
  else i += sprintf(&array[i],"W,");
733
  i += sprintf(&array[i],"%d,",GPSData.SatFix);
734
  i += sprintf(&array[i],"%d,",GPSData.NumOfSats);
735
  i += sprintf(&array[i],"%d.%d,",GPSData.Position_Accuracy/100,abs(GPSData.Position_Accuracy%100));
736
//  i += sprintf(&array[i],"%d.%d,M,",GPSData.Position.Altitude/1000,abs(GPSData.Position.Altitude%1000)/100);
737
 tmp1 = NaviData.Altimeter / 2; // in dm
738
  i += sprintf(&array[i],"%d.%d,M,",tmp1 / 10,abs(tmp1 % 10));
739
  i += sprintf(&array[i],",,,*");
740
 }
741
 else
742
  {
743
   i += sprintf(&array[i], ",,,,%d,00,99.99,,,,,,*",GPSData.NumOfSats);
744
  }
745
 for(x=1; x<i-1; x++)
746
  {
747
   crc ^= array[x];
748
  }
461 holgerb 749
  i += sprintf(&array[i], "%02x%c%c",crc,0x0d,0x0a);
460 holgerb 750
  AddSerialData(&UART1_tx_buffer,array,i);
751
 
752
// +++++++++++++++++++++++++++++++++++++++++++
466 holgerb 753
}
754
 
755
//$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006,1.2,E,A*13
756
//$GPRMC,HHMMSS.sss,A,BBBB.BBBB,b,LLLLL.LLLL,l,GG.G,RR.R,DDMMYY,M.M,m,F*PP
757
 
758
void CreateNmeaRMC(void)
759
{
760
 unsigned char array[200], i = 0, crc = 0, x;
761
 int tmp_int;
762
 long tmp1, tmp2;
763
// +++++++++++++++++++++++++++++++++++++++++++
764
 i += sprintf(array, "$GPRMC,");
765
// +++++++++++++++++++++++++++++++++++++++++++
766
 if(SystemTime.Valid)
767
  {
768
   i += sprintf(&array[i], "%02d%02d%02d.%03d,",SystemTime.Hour,SystemTime.Min,SystemTime.Sec,SystemTime.mSec);
769
  }
770
 else
771
  {
772
  i += sprintf(&array[i], ",");
773
  }
774
 if(GPSData.Flags & FLAG_GPSFIXOK)
775
 {
776
// +++++++++++++++++++++++++++++++++++++++++++
777
  tmp1 = abs(GPSData.Position.Latitude)/10000000L;
473 holgerb 778
  i += sprintf(&array[i],"A,%02d",(int)tmp1); // Status: A = Okay  V = Warnung
466 holgerb 779
 
780
  tmp1 = abs(GPSData.Position.Latitude)%10000000L;
781
  tmp1 *= 6; // in Minuten
782
  tmp2 = tmp1 / 1000000L;
473 holgerb 783
  i += sprintf(&array[i],"%02d",(int)tmp2);
466 holgerb 784
  tmp2 = tmp1 % 1000000L;
785
  tmp2 /= 10; // eine Stelle zu viel
473 holgerb 786
  i += sprintf(&array[i],".%05d,",(int)tmp2);
466 holgerb 787
  if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
788
  else i += sprintf(&array[i],"S,");
789
// +++++++++++++++++++++++++++++++++++++++++++
790
  tmp1 = abs(GPSData.Position.Longitude)/10000000L;
473 holgerb 791
  i += sprintf(&array[i],"%03d",(int)tmp1);
466 holgerb 792
 
793
  tmp1 = abs(GPSData.Position.Longitude)%10000000L;
794
  tmp1 *= 6; // in Minuten
795
  tmp2 = tmp1 / 1000000L;
473 holgerb 796
  i += sprintf(&array[i],"%02d",(int)tmp2);
466 holgerb 797
  tmp2 = tmp1 % 1000000L;
798
  tmp2 /= 10; // eine Stelle zu viel
473 holgerb 799
  i += sprintf(&array[i],".%05d,",(int)tmp2);
466 holgerb 800
  if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
801
  else i += sprintf(&array[i],"W,");
802
// +++++++++++++++++++++++++++++++++++++++++++
803
  tmp_int = GPSData.Speed_Ground; // in cm/sek
804
  tmp_int *= 90;
805
  tmp_int /= 463;
806
  i += sprintf(&array[i],"%02d.%d,",tmp_int/10,tmp_int%10); // in Knoten
807
// +++++++++++++++++++++++++++++++++++++++++++
808
  i += sprintf(&array[i],"%03d.%d,",GyroCompassCorrected/10,GyroCompassCorrected%10);
809
// +++++++++++++++++++++++++++++++++++++++++++
810
 if(SystemTime.Valid)
811
  {
812
   i += sprintf(&array[i], "%02d%02d%02d,",SystemTime.Day,SystemTime.Month,SystemTime.Year);
813
  }
814
 else
815
  {
816
  i += sprintf(&array[i], ",");
817
  }
818
// +++++++++++++++++++++++++++++++++++++++++++
819
  i += sprintf(&array[i],"%d.%1d,", abs(GeoMagDec)/10,abs(GeoMagDec)%10);
820
  if(GeoMagDec < 0) i += sprintf(&array[i], "W,"); else i += sprintf(&array[i], "E,");
821
// +++++++++++++++++++++++++++++++++++++++++++
822
  if(GPSData.Flags & FLAG_DIFFSOLN) i += sprintf(&array[i], "D,");
823
  else i += sprintf(&array[i], "A,");
824
 }
825
 else // kein Satfix
826
  {
827
   i += sprintf(&array[i], "V,,,,,,,,,,N*");
828
  }
829
// +++++++++++++++++++++++++++++++++++++++++++
830
//  CRC
831
// +++++++++++++++++++++++++++++++++++++++++++
832
 for(x=1; x<i-1; x++)
833
  {
834
   crc ^= array[x];
835
  }
836
  i += sprintf(&array[i], "%02x%c%c",crc,0x0d,0x0a);
837
// +++++++++++++++++++++++++++++++++++++++++++
838
  AddSerialData(&UART1_tx_buffer,array,i);
839
// +++++++++++++++++++++++++++++++++++++++++++
460 holgerb 840
/*
841
 
842
 
843
 
844
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
845
                                GPSData.NumOfSats =                     UbxSol.numSV;
846
                                GPSData.SatFix =                                UbxSol.GPSfix;
847
                                GPSData.Position_Accuracy =             UbxSol.PAcc;
848
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;
849
                                SetGPSTime(&SystemTime); // update system time
850
                                // NAV POSLLH
851
                                GPSData.Position.Status =               INVALID;
852
                                GPSData.Position.Longitude =    UbxPosLlh.LON;
853
                                GPSData.Position.Latitude =     UbxPosLlh.LAT;
854
                                GPSData.Position.Altitude =     UbxPosLlh.HMSL;
855
                                GPSData.Position.Status =               NEWDATA;
856
                                // NAV VELNED
857
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;
858
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;
859
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;
860
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;
861
                                GPSData.Heading =                               UbxVelNed.Heading;
862
        SystemTime.Year = 0;
863
        SystemTime.Month = 0;
864
        SystemTime.Day = 0;
865
        SystemTime.Hour = 0;
866
        SystemTime.Min = 0;
867
        SystemTime.Sec = 0;
868
        SystemTime.mSec = 0;
869
        SystemTime.Valid = 0;
870
 
871
                                FromFlightCtrl.GyroHeading / 10;//NaviData.HomePositionDeviation.Bearing / 2;
872
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
873
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
874
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
875
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
876
 
877
 
878
 
879
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
880
                                        i1 *= 100;
881
                                        i1 += i2 / 100000;
882
                                        i2  = i2 % 100000;
883
                                        i2 /= 10;
884
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
885
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
886
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
887
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
888
                                        break;
889
                                case 1:
890
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
891
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
892
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
893
                                        //-----------------------------
894
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
895
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
896
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
897
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
898
 
899
*/
900
}
901
 
902
 
41 ingob 903
/**************************************************************/
904
/* Send the answers to incomming commands at the debug uart   */
905
/**************************************************************/
906
void UART1_TransmitTxData(void)
907
{
358 holgerb 908
static u8 motorindex1 = 255, motorindex2 = 0;
110 killagreg 909
        if(DebugUART != UART1) return;
198 killagreg 910
 
911
        if(CheckDelay(UART1_AboTimeOut))
912
        {
913
                UART1_DebugData_Interval = 0;
914
                UART1_NaviData_Interval = 0;
915
                UART1_Data3D_Interval = 0;
916
                UART1_Display_Interval = 0;
378 holgerb 917
                UART1_MotorData_Interval = 0;
198 killagreg 918
        }
919
 
110 killagreg 920
        UART1_Transmit(); // output pending bytes in tx buffer
921
        if((UART1_tx_buffer.Locked == TRUE)) return;
41 ingob 922
 
156 killagreg 923
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
92 killagreg 924
        {
315 killagreg 925
                s16 ParamValue;
299 killagreg 926
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
160 holgerb 927
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'J', NC_ADDRESS, 2, &UART1_Request_ParameterId, sizeof(UART1_Request_ParameterId), &ParamValue, sizeof(ParamValue)); // answer the param request
156 killagreg 928
                UART1_Request_Parameter = FALSE;
929
        }
930
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
931
        {
110 killagreg 932
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
92 killagreg 933
                Echo = 0; // reset echo value
110 killagreg 934
                UART1_Request_Echo = FALSE;
92 killagreg 935
        }
349 ingob 936
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
937
        {
433 ingob 938
                u8 errorcode = FTP_ERROR_NONE;
939
        if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) errorcode = FTP_ERROR_MOTOR_RUN;
940
                else if (!Partition.IsValid) errorcode = FTP_ERROR_NO_SDCARD;
941
 
942
                if (!errorcode) CheckFTPCommand(UART1_Request_FTP);
943
                else
944
                {
945
                        u8 cmd = FTP_CMD_ERROR;
946
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &cmd, 1, &errorcode, 1);
947
                }
948
 
349 ingob 949
                UART1_Request_FTP = FALSE;
950
        }
295 killagreg 951
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
154 killagreg 952
        {
295 killagreg 953
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
954
                UART1_Request_WritePoint = 0xFF;
154 killagreg 955
        }
280 killagreg 956
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
154 killagreg 957
        {
280 killagreg 958
                u8 PointCount = PointList_GetCount();
959
                if (UART1_Request_ReadPoint <= PointCount)
154 killagreg 960
                {
280 killagreg 961
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
154 killagreg 962
                }
963
                else
964
                {
280 killagreg 965
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
154 killagreg 966
                }
280 killagreg 967
                UART1_Request_ReadPoint = 0;
154 killagreg 968
        }
969
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
970
        {
971
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
972
                UART1_Request_DebugLabel = 0xFF;
973
        }
398 holgerb 974
        else if(UART1_ExternalControlConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
975
        {
976
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1,(u8 *)&UART1_ExternalControlConfirmFrame, sizeof(UART1_ExternalControlConfirmFrame));
977
                UART1_ExternalControlConfirmFrame = 0;
978
        }
151 killagreg 979
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
41 ingob 980
        {
151 killagreg 981
                NaviData.Errorcode = ErrorCode;
378 holgerb 982
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData, sizeof(NaviData));
151 killagreg 983
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
984
                UART1_Request_NaviData = FALSE;
360 holgerb 985
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
151 killagreg 986
        }
987
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
988
        {
110 killagreg 989
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
990
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
991
                UART1_Request_DebugData = FALSE;
41 ingob 992
        }
151 killagreg 993
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
61 holgerb 994
        {
358 holgerb 995
                Data3D.StickNick = FC.StickNick;
996
                Data3D.StickRoll = FC.StickRoll;
997
        Data3D.StickYaw = FC.StickYaw;
998
        Data3D.StickGas = FC.StickGas;
110 killagreg 999
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
1000
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
1001
                UART1_Request_Data3D = FALSE;
61 holgerb 1002
        }
358 holgerb 1003
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
1004
        {
1005
                do
1006
                {
1007
                 motorindex1++;
1008
                 motorindex1%=12;
1009
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
1010
                 if(motorindex1 == motorindex2) break;
1011
                }
1012
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
1013
 
1014
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
1015
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
1016
                UART1_Request_MotorData = FALSE;
1017
        }
460 holgerb 1018
        else if((((NMEA_Interval > 0) && CheckDelay(NMEA_Timer))) && (UART1_tx_buffer.Locked == FALSE))
1019
        {
1020
                CreateNmeaGGA();
466 holgerb 1021
                Send_NMEA_RMC = TRUE;   // das muss noch da hinter
460 holgerb 1022
                NMEA_Timer = SetDelay(NMEA_Interval);
1023
        }
466 holgerb 1024
        else if(Send_NMEA_RMC == TRUE && (UART1_tx_buffer.Locked == FALSE))
1025
        {
1026
          CreateNmeaRMC();
1027
          Send_NMEA_RMC = FALSE;
1028
        }
460 holgerb 1029
 
193 killagreg 1030
        /*
153 killagreg 1031
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
1032
        {
1033
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
1034
                UART1_ConfirmFrame = 0;
1035
        }
193 killagreg 1036
        */
1037
        /*
151 killagreg 1038
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1039
        {
110 killagreg 1040
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
1041
                UART1_Request_ExternalControl = FALSE;
41 ingob 1042
        }
193 killagreg 1043
        */
1044
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
195 killagreg 1045
        {
193 killagreg 1046
                if(UART1_DisplayLine > 3)
1047
                {
201 killagreg 1048
                        Menu_Update(UART1_DisplayKeys);
1049
                        UART1_DisplayKeys = 0;
193 killagreg 1050
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
1051
                }
1052
                else
1053
                {
1054
                        UART1_DisplayLine = 2;
1055
                        sprintf(text,"!!! incompatible !!!");
1056
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
1057
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
1058
                }
1059
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
110 killagreg 1060
                UART1_Request_Display = FALSE;
41 ingob 1061
        }
151 killagreg 1062
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1063
        {
201 killagreg 1064
                Menu_Update(0);
110 killagreg 1065
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
1066
                UART1_Request_Display1 = FALSE;
41 ingob 1067
        }
151 killagreg 1068
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1069
        {
110 killagreg 1070
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
1071
                UART1_Request_VersionInfo = FALSE;
41 ingob 1072
        }
447 holgerb 1073
        else if(UART1_Request_SystemTime && (UART1_tx_buffer.Locked == FALSE))
1074
        {
1075
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'T', NC_ADDRESS,1, (u8 *)&SystemTime, sizeof(SystemTime));
1076
                UART1_Request_SystemTime = FALSE;
1077
        }
151 killagreg 1078
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1079
        {
110 killagreg 1080
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
1081
                UART1_Request_ErrorMessage = FALSE;
41 ingob 1082
        }
314 killagreg 1083
#ifdef FOLLOW_ME
1084
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
1085
        {
1086
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
315 killagreg 1087
                {
314 killagreg 1088
                        TransmitAlsoToFC = 1;
1089
                        // update FollowMe content
1090
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
1091
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
1092
                        FollowMe.Position.Status = NEWDATA;
1093
                        FollowMe.Position.Altitude = 1;
1094
                        // 0  -> no Orientation
1095
                        // 1-360 -> CompassCourse Setpoint
1096
                        // -1 -> points to  WP1 -> itself
1097
                FollowMe.Heading = -1;
1098
                        FollowMe.ToleranceRadius = 1;
1099
                        FollowMe.HoldTime = 60;
1100
                        FollowMe.Event_Flag = 1;
1101
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
1102
                        FollowMe.Type = POINT_TYPE_WP;
1103
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
1104
                        FollowMe.AltitudeRate = 0;                        // do not change height
403 holgerb 1105
                        FollowMe.Speed = 0;                             // rate to change the Position (0 = max)
1106
                        FollowMe.CamAngle = 255;                // Camera servo angle in degree (255 -> POI-Automatic)
1107
                        FollowMe.Name[0] = 'F';             // Name of that point (ASCII)
1108
                        FollowMe.Name[1] = 'O';             // Name of that point (ASCII)
1109
                        FollowMe.Name[2] = 'L';             // Name of that point (ASCII)
1110
                        FollowMe.Name[3] = 'L';             // Name of that point (ASCII)
314 killagreg 1111
                        FollowMe.reserve[0] = 0;                // reserve
1112
                        FollowMe.reserve[1] = 0;                // reserve
1113
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
1114
                }
1115
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
1116
        }
1117
#endif
250 ingob 1118
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
314 killagreg 1119
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
254 killagreg 1120
        {
1121
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
1122
                SendDebugOutput = 0;
1123
        }
315 killagreg 1124
#endif
110 killagreg 1125
        UART1_Transmit(); // output pending bytes in tx buffer
41 ingob 1126
}
1127