Subversion Repositories NaviCtrl

Rev

Rev 598 | Rev 608 | Go to most recent revision | 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
489 killagreg 10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
360 holgerb 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
489 killagreg 21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
360 holgerb 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 -
489 killagreg 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
360 holgerb 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"
489 killagreg 66
#include "i2c.h"
41 ingob 67
#include "uart0.h"
68
#include "uart1.h"
69
#include "uart2.h"
119 killagreg 70
#include "timer1.h"
139 killagreg 71
#include "timer2.h"
72
#include "analog.h"
243 killagreg 73
#include "compass.h"
41 ingob 74
#include "waypoints.h"
110 killagreg 75
#include "mkprotocol.h"
156 killagreg 76
#include "params.h"
153 killagreg 77
#include "fifo.h"
315 killagreg 78
#include "debug.h"
358 holgerb 79
#include "spi_slave.h"
349 ingob 80
#include "ftphelper.h"
378 holgerb 81
#include "led.h"
433 ingob 82
#include "fat16.h"
598 holgerb 83
#include "crc16.h"
529 holgerb 84
#include "eeprom.h"
41 ingob 85
 
529 holgerb 86
#define LIC_CMD_READ_LICENSE     1
87
#define LIC_CMD_WRITE_LICENSE    2
88
#define LIC_CMD_ERASE_LICENSE    3
433 ingob 89
 
41 ingob 90
#define FALSE   0
91
#define TRUE    1
92
 
351 holgerb 93
#define ABO_TIMEOUT 8000 // disable abo after 8 seconds
198 killagreg 94
u32 UART1_AboTimeOut = 0;
92 killagreg 95
 
598 holgerb 96
NaviData_Volatile_t NaviData_Volatile;
97
NaviData_WP_t NaviData_WP;
98
NaviData_Deviation_t NaviData_Deviation;
99
NaviData_Home_t NaviData_Home;
100
NaviData_Target_t NaviData_Target;
101
NaviData_Flags_t NaviData_Flags;
102
NaviData_Tiny_t NaviData_Tiny;
599 holgerb 103
NaviData_Failsafe_t NaviData_Failsafe;
598 holgerb 104
NaviData_t NaviData;
105
 
110 killagreg 106
u8 UART1_Request_VersionInfo    = FALSE;
107
u8 UART1_Request_ExternalControl= FALSE;
108
u8 UART1_Request_Display                = FALSE;
109
u8 UART1_Request_Display1               = FALSE;
110
u8 UART1_Request_DebugData              = FALSE;
111
u8 UART1_Request_DebugLabel             = 255;
112
u8 UART1_Request_NaviData               = FALSE;
113
u8 UART1_Request_ErrorMessage   = FALSE;
295 killagreg 114
u8 UART1_Request_WritePoint             = 0xFF;
280 killagreg 115
u8 UART1_Request_ReadPoint              = 0;
110 killagreg 116
u8 UART1_Request_Data3D             = FALSE;
358 holgerb 117
u8 UART1_Request_MotorData          = FALSE;
110 killagreg 118
u8 UART1_Request_Echo               = FALSE;
156 killagreg 119
u8 UART1_Request_ParameterId    = 0;
494 killagreg 120
u8 UART1_Request_WPLStore               = FALSE;
158 killagreg 121
u8 UART1_Request_Parameter              = FALSE;
447 holgerb 122
u8 UART1_Request_SystemTime             = FALSE;
201 killagreg 123
u8 UART1_DisplayKeys                    = 0;
112 killagreg 124
u8 UART1_DisplayLine                    = 0;
125
u8 UART1_ConfirmFrame                   = 0;
349 ingob 126
u8 UART1_Request_FTP                    = FALSE;
529 holgerb 127
u8 UART1_Request_LicenseString  = FALSE;
360 holgerb 128
u8 LastTransmittedFCStatusFlags2 = 0;
398 holgerb 129
u8 UART1_ExternalControlConfirmFrame = FALSE;
466 holgerb 130
u8 Send_NMEA_RMC = FALSE;
41 ingob 131
 
132
UART_TypeDef *DebugUART = UART1;
133
 
314 killagreg 134
#ifdef FOLLOW_ME
135
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
315 killagreg 136
u32 UART1_FollowMe_Timer        = 0;
314 killagreg 137
Point_t FollowMe;
138
#endif
139
 
153 killagreg 140
// the primary rx fifo
378 holgerb 141
#define UART1_RX_FIFO_LEN 1024
153 killagreg 142
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
143
fifo_t UART1_rx_fifo;
110 killagreg 144
 
145
// the rx buffer
530 holgerb 146
#define UART1_RX_BUFFER_LEN 1024
110 killagreg 147
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
148
Buffer_t UART1_rx_buffer;
149
 
153 killagreg 150
// the tx buffer
530 holgerb 151
#define UART1_TX_BUFFER_LEN 1024
153 killagreg 152
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
153
Buffer_t UART1_tx_buffer;
154
 
92 killagreg 155
volatile u8 SerialLinkOkay = 0;
41 ingob 156
 
150 killagreg 157
u8 text[200];
529 holgerb 158
u8 *LicensePtr = UART1_tbuffer;
41 ingob 159
 
160
const u8 ANALOG_LABEL[32][16] =
161
{
90 killagreg 162
   //1234567890123456
41 ingob 163
        "AngleNick       ", //0
164
        "AngleRoll       ",
165
        "AccNick         ",
166
        "AccRoll         ",
516 holgerb 167
        "Altitude [0.1m] ",
200 killagreg 168
        "FC-Flags        ", //5
95 killagreg 169
        "NC-Flags        ",
511 holgerb 170
        "Voltage  [0.1V] ",
171
        "Current  [0.1A] ",
41 ingob 172
        "GPS Data        ",
61 holgerb 173
        "CompassHeading  ", //10
174
        "GyroHeading     ",
419 holgerb 175
        "SPI Error       ", // achtung: muss auf 12 bleiben
41 ingob 176
        "SPI Okay        ",
189 killagreg 177
        "I2C Error       ",
41 ingob 178
        "I2C Okay        ", //15
529 holgerb 179
        "16              ",
180
        "17              ",
181
        "18              ",
182
        "19              ", // SD-Card-time
330 holgerb 183
        "EarthMagnet [%] ", //20
456 holgerb 184
        "Ground Speed    ", //  "Z_Speed         ",
338 holgerb 185
        "N_Speed         ",
186
        "E_Speed         ",
310 holgerb 187
        "Magnet X        ",
338 holgerb 188
        "Magnet Y        ", //25
310 holgerb 189
        "Magnet Z        ",
41 ingob 190
        "Distance N      ",
191
        "Distance E      ",
511 holgerb 192
        "-GPS_Nick       ",
193
        "-GPS_Roll       ", //30
41 ingob 194
        "Used_Sats       "
195
};
196
 
495 killagreg 197
typedef struct
198
{
199
        u8 Index;
200
        u8 Status;
201
} __attribute__((packed)) WPL_Answer_t;
202
WPL_Answer_t WPL_Answer;
203
 
112 killagreg 204
DebugOut_t DebugOut;
205
ExternControl_t ExternControl;
206
UART_VersionInfo_t UART_VersionInfo;
207
NaviData_t NaviData;
208
Data3D_t Data3D;
496 killagreg 209
 
112 killagreg 210
u16 Echo; // 2 bytes recieved will be sent back as echo
41 ingob 211
 
193 killagreg 212
u32 UART1_DebugData_Timer = 0;
198 killagreg 213
u32 UART1_DebugData_Interval = 0;       // in ms
193 killagreg 214
u32 UART1_NaviData_Timer = 0;
198 killagreg 215
u32 UART1_NaviData_Interval = 0;        // in ms
598 holgerb 216
u16 UART1_NaviData_MaxBytes = 0;                // newer protocol?
198 killagreg 217
u32 UART1_Data3D_Timer = 0;
218
u32 UART1_Data3D_Interval = 0;          // in ms
358 holgerb 219
u32 UART1_MotorData_Timer = 0;
220
u32 UART1_MotorData_Interval = 0;               // in ms
193 killagreg 221
u32 UART1_Display_Timer = 0;
201 killagreg 222
u32 UART1_Display_Interval = 0;         // in ms
460 holgerb 223
u32 NMEA_Timer = 0;
532 holgerb 224
u32 NMEA_Interval = 0;// in ms
112 killagreg 225
 
598 holgerb 226
u8 CalculateDebugLableCrc(void)
227
{
228
        u16 i;
229
        u8 crc = 0;
230
        for(i=0;i<sizeof(ANALOG_LABEL);i++) crc += ANALOG_LABEL[0][i];
231
        return(crc);
232
}
233
 
41 ingob 234
/********************************************************/
235
/*            Initialization the UART1                  */
236
/********************************************************/
237
void UART1_Init (void)
238
{
239
        GPIO_InitTypeDef GPIO_InitStructure;
240
        UART_InitTypeDef UART_InitStructure;
241
 
153 killagreg 242
        // initialize txd buffer
243
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
165 killagreg 244
 
153 killagreg 245
        // initialize rxd buffer
246
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
247
 
244 killagreg 248
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
249
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
153 killagreg 250
 
41 ingob 251
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
252
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
253
 
254
        /*Configure UART1_Rx pin GPIO3.2*/
255
        GPIO_StructInit(&GPIO_InitStructure);
256
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
257
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
258
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
196 killagreg 259
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
41 ingob 260
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
261
        GPIO_Init(GPIO3, &GPIO_InitStructure);
262
 
263
        /*Configure UART1_Tx pin GPIO3.3*/
264
        GPIO_StructInit(&GPIO_InitStructure);
265
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
266
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
267
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
268
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
269
        GPIO_Init(GPIO3, &GPIO_InitStructure);
270
 
271
        /* UART1 configured as follow:
272
        - Word Length = 8 Bits
273
        - One Stop Bit
274
        - No parity
275
        - BaudRate = 57600 baud
276
        - Hardware flow control Disabled
277
        - Receive and transmit enabled
278
        - Receive and transmit FIFOs are Disabled
279
        */
280
        UART_StructInit(&UART_InitStructure);
281
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
282
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
283
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
110 killagreg 284
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
489 killagreg 285
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
41 ingob 286
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
487 holgerb 287
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
288
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
489 killagreg 289
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;      // FIFO size 16 bytes, FIFO level 8 bytes
41 ingob 290
 
291
        UART_DeInit(UART1); // reset uart 1     to default
165 killagreg 292
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
41 ingob 293
        // enable uart 1 interrupts selective
294
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
295
        UART_Cmd(UART1, ENABLE); // enable uart 1
136 killagreg 296
        // configure the uart 1 interupt line
297
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
244 killagreg 298
        // enable the uart 1 IRQ
41 ingob 299
        VIC_ITCmd(UART1_ITLine, ENABLE);
153 killagreg 300
 
41 ingob 301
        // initialize the debug timer
110 killagreg 302
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
303
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
461 holgerb 304
        NMEA_Timer = SetDelay(14000);
110 killagreg 305
 
41 ingob 306
        // Fill Version Info Structure
307
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
308
        UART_VersionInfo.SWMinor = VERSION_MINOR;
309
        UART_VersionInfo.SWPatch = VERSION_PATCH;
310
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
489 killagreg 311
    UART_VersionInfo.HWMajor = Version_HW & 0x7F;
548 holgerb 312
    UART_VersionInfo.BL_Firmware = 255;
454 holgerb 313
        UART_VersionInfo.Flags = 0;
598 holgerb 314
        UART_VersionInfo.LabelTextCRC = CalculateDebugLableCrc();
89 killagreg 315
        NaviData.Version = NAVIDATA_VERSION;
212 killagreg 316
        UART1_PutString("\r\n UART1 init...ok");
41 ingob 317
}
318
 
319
 
320
/****************************************************************/
321
/*               USART1 receiver ISR                            */
322
/****************************************************************/
323
void UART1_IRQHandler(void)
324
{
325
        static u8 abortState = 0;
326
        u8 c;
327
 
328
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
329
        {
153 killagreg 330
                // clear the pending bits!
41 ingob 331
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
332
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
333
                // if debug UART is not UART1
334
                if (DebugUART != UART1)
335
                {       // forward received data to the debug UART tx buffer
336
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
337
                        {
338
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
339
                                c = UART_ReceiveData(UART1);
340
 
341
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
342
                                switch (abortState)
343
                                {
165 killagreg 344
                                        case 0:
139 killagreg 345
                                                if (c == 27) abortState++;
41 ingob 346
                                                break;
165 killagreg 347
                                        case 1:
348
                                                if (c == 27) abortState++;
139 killagreg 349
                                                else abortState = 0;
41 ingob 350
                                        break;
139 killagreg 351
                                        case 2:
165 killagreg 352
                                                if (c == 0x55) abortState++;
139 killagreg 353
                                                else abortState = 0;
41 ingob 354
                                                break;
139 killagreg 355
                                        case 3:
356
                                                if (c == 0xAA) abortState++;
357
                                                else abortState = 0;
41 ingob 358
                                                break;
165 killagreg 359
                                        case 4:
139 killagreg 360
                                                if (c == 0x00)
361
                                                {
362
                                                        if(DebugUART == UART0)
363
                                                        {
245 killagreg 364
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
179 killagreg 365
                                                                TIMER2_Init(); // enbable servo outputs
153 killagreg 366
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
139 killagreg 367
                                                        }
368
                                                        DebugUART = UART1;
165 killagreg 369
                                                }
139 killagreg 370
                                                abortState = 0;
371
                                                break;
372
                                } // end switch abort state
136 killagreg 373
                                // if the Debug uart is not UART1, redirect input to the Debug UART
110 killagreg 374
                                if (DebugUART != UART1)
375
                                {
376
                                        // wait for space in the tx buffer of the DebugUART
377
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
141 killagreg 378
                                        // move byte to the tx fifo of the debug uart
110 killagreg 379
                                        UART_SendData(DebugUART, c);
380
                                }
41 ingob 381
                        }
382
                }
383
                else  // DebugUART == UART1 (normal operation)
384
                {
110 killagreg 385
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
154 killagreg 386
                        { // some byes in the hardware fifo
153 killagreg 387
                            // get byte from hardware fifo
41 ingob 388
                        c = UART_ReceiveData(UART1);
153 killagreg 389
                                // put into the software fifo
390
                                if(!fifo_put(&UART1_rx_fifo, c))
391
                                {       // fifo overflow
155 killagreg 392
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
153 killagreg 393
                                }
154 killagreg 394
                        } // EOF while some byes in the hardware fifo
41 ingob 395
                } // eof DebugUart = UART1
396
        }
195 killagreg 397
 
380 holgerb 398
 
399
 
378 holgerb 400
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
41 ingob 401
}
402
 
403
/**************************************************************/
404
/* Process incomming data from debug uart                     */
405
/**************************************************************/
406
void UART1_ProcessRxData(void)
407
{
154 killagreg 408
        // return on forwarding uart  or unlocked rx buffer
380 holgerb 409
        u8 c;
155 killagreg 410
        if(DebugUART != UART1) return;
411
        // if rx buffer is not locked
412
        if(UART1_rx_buffer.Locked == FALSE)
315 killagreg 413
        {
244 killagreg 414
                //collect data from primary rx fifo
155 killagreg 415
                while(fifo_get(&UART1_rx_fifo, &c))
315 killagreg 416
                {
244 killagreg 417
                        // break if complete frame is collected
155 killagreg 418
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
419
                }
420
        }
421
        if(UART1_rx_buffer.Locked == FALSE) return;
422
 
280 killagreg 423
        Point_t * pPoint = NULL;
153 killagreg 424
        SerialMsg_t SerialMsg;
41 ingob 425
 
190 killagreg 426
        // analyze header first
427
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
194 killagreg 428
        if( SerialMsg.Address == FC_ADDRESS )
41 ingob 429
        {
192 killagreg 430
                switch(SerialMsg.CmdID)
431
                {
231 holgerb 432
//                      case 'v': // version
398 holgerb 433
                        case 'b': // extern control
434
                                          UART1_ExternalControlConfirmFrame = 1;
192 killagreg 435
                        case 'y': // serial poti values
436
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
437
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
194 killagreg 438
                                return; //end process rx data
192 killagreg 439
                        break;
440
                }
195 killagreg 441
        }
190 killagreg 442
 
443
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
444
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
445
        switch(SerialMsg.Address) // check for Slave Address
446
        {
41 ingob 447
                case NC_ADDRESS:  // own Slave Address
190 killagreg 448
                switch(SerialMsg.CmdID)
41 ingob 449
                {
489 killagreg 450
                        case 't': // request for the GPS time
447 holgerb 451
                                UART1_Request_SystemTime  = TRUE;
452
                                break;
453
 
529 holgerb 454
                        case 'm': // request for the license string
455
                                UART1_Request_LicenseString = SerialMsg.pData[0];
531 holgerb 456
                                if((UART1_Request_LicenseString == LIC_CMD_WRITE_LICENSE) && (UART_VersionInfo.HWMajor >= 20))
529 holgerb 457
                                 {
458
                                  memcpy(LicensePtr, &SerialMsg.pData[1],LICENSE_SIZE_TEXT); // copy ftp parameter
459
                                 }
460
                                break;
349 ingob 461
                        case 'f': // ftp command
462
                                UART1_Request_FTP = SerialMsg.pData[0];
489 killagreg 463
                                //if (UART1_Request_FTP == FTP_CMD_SET_CWD || UART1_Request_FTP == FTP_CMD_GET_FILE)
349 ingob 464
                                memcpy(&FTP_data, &SerialMsg.pData[1], sizeof(FTP_data)); // copy ftp parameter
465
                        break;
466
 
101 holgerb 467
                        case 'z': // connection checker
110 killagreg 468
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
469
                                UART1_Request_Echo = TRUE;
92 killagreg 470
                                break;
471
 
41 ingob 472
                        case 'e': // request for the text of the error status
110 killagreg 473
                                UART1_Request_ErrorMessage = TRUE;
41 ingob 474
                                break;
475
 
476
                        case 's'://  new target position
280 killagreg 477
                                pPoint = (Point_t*)SerialMsg.pData;
478
                                if(pPoint->Position.Status == NEWDATA)
41 ingob 479
                                {
315 killagreg 480
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list
300 holgerb 481
                                        //pPoint->Index = 1; // must be one after empty list
296 killagreg 482
                                        PointList_SetAt(pPoint);
315 killagreg 483
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
295 killagreg 484
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
485
                                        BeepTime = 50;
41 ingob 486
                                }
568 holgerb 487
                                else
488
                                if((pPoint->Position.Status == SIMULATION) && !(FC.RealStatusFlags & FC_STATUS_MOTOR_RUN))
489
                                {
586 holgerb 490
                                if(pPoint->Event_Flag & SIMULATION_MOTOR_ON)
491
                                 {
492
                                  GPSPos_Copy(&(pPoint->Position), &SimulationPosition); // update hold position
493
                                  CompassDirectionAtMotorStart = SimulatedDirection*10;
494
                                 }
568 holgerb 495
                                if(!(SimulationFlags & SIMULATION_MOTOR_ON) && (pPoint->Event_Flag & SIMULATION_MOTOR_ON))
496
                                 {
497
                                  SimulationFlags = pPoint->Event_Flag | SIMULATION_MOTOR_START; // dann steht da noch nicht "SIMULATION_MOTOR_ON" drin
498
                                  SpeakHoTT = SPEAK_STARTING;
499
                                 }
500
                                else
501
                                if(!(pPoint->Event_Flag & SIMULATION_MOTOR_ON) && (SimulationFlags & SIMULATION_MOTOR_ON))
502
                                 {
503
                                  SimulationFlags = pPoint->Event_Flag;
504
                                  SpeakHoTT = SPEAK_MK_OFF;
505
                                  BeepTime = 50;
506
                                 }
507
                                 else
508
                                  SimulationFlags = pPoint->Event_Flag | (SimulationFlags & SIMULATION_MOTOR_START);
509
                                }
41 ingob 510
                                break;
511
                        case 'u': // redirect debug uart
110 killagreg 512
                                switch(SerialMsg.pData[0])
41 ingob 513
                                {
514
                                        case UART_FLIGHTCTRL:
515
                                                UART2_Init();                           // initialize UART2 to FC pins
153 killagreg 516
                                                fifo_purge(&UART1_rx_fifo);
180 killagreg 517
                                                TIMER2_Deinit();                        // reduce irq load
41 ingob 518
                                                DebugUART = UART2;
519
                                                break;
520
                                        case UART_MK3MAG:
255 killagreg 521
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
41 ingob 522
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
523
                                                GPSData.Status = INVALID;
153 killagreg 524
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 525
                                                DebugUART = UART0;
526
                                                break;
527
                                        case UART_MKGPS:
255 killagreg 528
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
179 killagreg 529
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
245 killagreg 530
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
41 ingob 531
                                                GPSData.Status = INVALID;
153 killagreg 532
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 533
                                                DebugUART = UART0;
534
                                                break;
153 killagreg 535
                                        default:
536
                                                break;
41 ingob 537
                                }
538
                                break;
539
 
295 killagreg 540
                        case 'w'://  Set point in list at index
165 killagreg 541
                                {
280 killagreg 542
                                        pPoint = (Point_t*)SerialMsg.pData;
224 killagreg 543
 
280 killagreg 544
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
224 killagreg 545
                                        {
280 killagreg 546
                                                PointList_Clear();
547
                                                GPS_pWaypoint = PointList_WPBegin();
315 killagreg 548
                                                UART1_Request_WritePoint = 0; // return new point count
568 holgerb 549
                                                NewWaypointsReceived = 1;
165 killagreg 550
                                        }
227 killagreg 551
                                        else
295 killagreg 552
                                        {  // update WP in list at index
489 killagreg 553
                                                if(pPoint->Index > MaxNumberOfWaypoints)
490 killagreg 554
                                                {
555
                                                        UART1_Request_WritePoint = 254;
556
                                                        pPoint->Index = MaxNumberOfWaypoints;
557
                                                }
558
                                                else UART1_Request_WritePoint = PointList_SetAt(pPoint);
315 killagreg 559
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
519 holgerb 560
                                                SpeakWaypointRached = 1;        // Speak once when the last Point is reached
295 killagreg 561
                                                if(UART1_Request_WritePoint == pPoint->Index)
225 killagreg 562
                                                {
563
                                                        BeepTime = 500;
519 holgerb 564
                                                    if(UART1_Request_WritePoint == 1) SpeakNextWaypoint = 1;            // Speak once as soon as the Points are active
165 killagreg 565
                                                }
566
                                        }
41 ingob 567
                                }
568
                                break;
569
 
92 killagreg 570
                        case 'x'://  Read Waypoint from List
280 killagreg 571
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
92 killagreg 572
                                break;
573
 
494 killagreg 574
                        case 'i':// Store WP List to file
496 killagreg 575
                                memcpy((u8*)&WPL_Store, SerialMsg.pData, sizeof(WPL_Store_t));
576
                                WPL_Store.Name[11] = 0; // make sure the name string is terminated
497 killagreg 577
                                WPL_Answer.Index = WPL_Store.Index; // echo Index in cmd answer
513 killagreg 578
                                WPL_Answer.Status = PointList_WriteToFile(&WPL_Store);
495 killagreg 579
                                UART1_Request_WPLStore = TRUE;
494 killagreg 580
                                break;
581
 
160 holgerb 582
                        case 'j':// Set/Get NC-Parameter
156 killagreg 583
                                switch(SerialMsg.pData[0])
584
                                {
585
                                        case 0: // get
586
                                        break;
165 killagreg 587
 
156 killagreg 588
                                        case 1: // set
159 killagreg 589
                                        {
590
                                                s16 value;
591
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
592
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
593
                                        }
156 killagreg 594
                                        break;
491 killagreg 595
 
156 killagreg 596
                                        default:
165 killagreg 597
                                        break;
156 killagreg 598
                                }
599
                                UART1_Request_ParameterId = SerialMsg.pData[1];
600
                                UART1_Request_Parameter = TRUE;
601
                                break;
41 ingob 602
                        default:
603
                                // unsupported command recieved
604
                                break;
605
                } // case NC_ADDRESS
65 killagreg 606
                // "break;" is missing here to fall thru to the common commands
41 ingob 607
 
608
                default:  // and any other Slave Address
609
 
190 killagreg 610
                switch(SerialMsg.CmdID) // check CmdID
41 ingob 611
                {
612
                        case 'a':// request for the labels of the analog debug outputs
110 killagreg 613
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
614
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
41 ingob 615
                                break;
193 killagreg 616
                        /*
41 ingob 617
                        case 'b': // submit extern control
121 killagreg 618
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
112 killagreg 619
                                UART1_ConfirmFrame = ExternControl.Frame;
41 ingob 620
                                break;
195 killagreg 621
                        */
41 ingob 622
                        case 'd': // request for debug data;
110 killagreg 623
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
624
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
198 killagreg 625
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
41 ingob 626
                                break;
627
 
63 killagreg 628
                        case 'c': // request for 3D data;
110 killagreg 629
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
630
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
198 killagreg 631
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
63 killagreg 632
                                break;
275 killagreg 633
 
358 holgerb 634
                        case 'k': // request for Motor data;
635
                                UART1_MotorData_Interval = (u32) SerialMsg.pData[0] * 10;
636
                                if(UART1_MotorData_Interval > 0) UART1_Request_MotorData = TRUE;
637
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
638
                                break;
639
 
41 ingob 640
                        case 'h':// reqest for display line
193 killagreg 641
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
642
                                {
643
                                        UART1_DisplayLine = 2;
195 killagreg 644
                                        UART1_Display_Interval = 0;
598 holgerb 645
                                        UART1_Request_Display = TRUE;
193 killagreg 646
                                }
647
                                else
648
                                {
201 killagreg 649
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
193 killagreg 650
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
651
                                        UART1_DisplayLine = 4;
198 killagreg 652
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
598 holgerb 653
                                        if(UART1_Display_Interval) UART1_Request_Display = TRUE;
193 killagreg 654
                                }
41 ingob 655
                                break;
656
 
657
                        case 'l':// reqest for display columns
110 killagreg 658
                                MenuItem = SerialMsg.pData[0];
659
                                UART1_Request_Display1 = TRUE;
41 ingob 660
                                break;
64 holgerb 661
 
662
                        case 'o': // request for navigation information
110 killagreg 663
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
598 holgerb 664
                                if(SerialMsg.DataLen > 2) UART1_NaviData_MaxBytes = SerialMsg.pData[1] * 256 + SerialMsg.pData[2];
665
                                else UART1_NaviData_MaxBytes = 0;
110 killagreg 666
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
198 killagreg 667
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
64 holgerb 668
                                break;
669
 
41 ingob 670
                        case 'v': // request for version info
548 holgerb 671
                                if(SerialMsg.DataLen > 0 && SerialMsg.pData[0] == 1) UART1_Request_VersionInfo = 1;
672
                                else UART1_Request_VersionInfo = 2;
41 ingob 673
                                break;
674
                        default:
675
                                // unsupported command recieved
676
                                break;
677
                }
678
                break; // default:
679
        }
153 killagreg 680
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
41 ingob 681
}
682
 
683
 
684
/*****************************************************/
685
/*                   Send a character                */
686
/*****************************************************/
110 killagreg 687
s16 UART1_Putchar(char c)
41 ingob 688
{
362 holgerb 689
        u32 timeout = 10000;
110 killagreg 690
        if (c == '\n') UART1_Putchar('\r');
41 ingob 691
        // wait until txd fifo is not full
362 holgerb 692
        while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET) if(--timeout == 0) return(0);
41 ingob 693
        // transmit byte
694
        UART_SendData(UART1, c);
314 killagreg 695
        #ifdef FOLLOW_ME
696
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
697
        #endif
41 ingob 698
        return (0);
699
}
700
 
701
/*****************************************************/
702
/*       Send a string to the debug uart              */
703
/*****************************************************/
110 killagreg 704
void UART1_PutString(u8 *s)
41 ingob 705
{
706
        if(s == NULL) return;
707
        while (*s != '\0' && DebugUART == UART1)
708
        {
110 killagreg 709
                UART1_Putchar(*s);
41 ingob 710
                s ++;
711
        }
712
}
713
 
113 killagreg 714
 
110 killagreg 715
/**************************************************************/
716
/*         Transmit tx buffer via debug uart                  */
717
/**************************************************************/
718
void UART1_Transmit(void)
719
{
720
        u8 tmp_tx;
721
        if(DebugUART != UART1) return;
722
        // if something has to be send and the txd fifo is not full
113 killagreg 723
        if(UART1_tx_buffer.Locked == TRUE)
110 killagreg 724
        {
113 killagreg 725
                // while there is some space in the tx fifo
726
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
110 killagreg 727
                {
113 killagreg 728
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
729
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
314 killagreg 730
                        #ifdef FOLLOW_ME
315 killagreg 731
                        if(TransmitAlsoToFC)
732
                        {
314 killagreg 733
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
734
                        }
735
                        #endif
113 killagreg 736
                        // if terminating character or end of txd buffer reached
461 holgerb 737
                        if((tmp_tx == '\0') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
113 killagreg 738
                        {
739
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
314 killagreg 740
                                #ifdef FOLLOW_ME
741
                                TransmitAlsoToFC = 0;
742
                                #endif
113 killagreg 743
                                break; // end while loop
744
                        }
110 killagreg 745
                }
746
        }
747
}
41 ingob 748
 
460 holgerb 749
//$GPGGA,HHMMSS.ss,BBBB.BBBB,b,LLLLL.LLLL,l,Q,NN,D.D,H.H,h,G.G,g,A.A,RRRR*PP
750
//$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5,M,48.0,M,,*45
751
//$GPGGA,092120.20,,,,,0,00,99.99,,,,,,*6C
466 holgerb 752
//http://www.kowoma.de/gps/zusatzerklaerungen/NMEA.htm
576 holgerb 753
 
460 holgerb 754
void CreateNmeaGGA(void)
755
{
489 killagreg 756
        u8 array[200], i = 0, crc = 0, x;
757
        s32 tmp1, tmp2;
378 holgerb 758
 
489 killagreg 759
        i += sprintf(array, "$GPGGA,");
760
        // +++++++++++++++++++++++++++++++++++++++++++
761
        if(SystemTime.Valid)
762
        {
763
                i += sprintf(&array[i], "%02d%02d%02d.%02d,", SystemTime.Hour, SystemTime.Min, SystemTime.Sec, SystemTime.mSec/10);
764
        }
765
        else
766
        {
767
                i += sprintf(&array[i], ",");
768
        }
769
        // +++++++++++++++++++++++++++++++++++++++++++
770
        if(GPSData.Flags & FLAG_GPSFIXOK)
771
        {
772
                tmp1 = abs(GPSData.Position.Latitude)/10000000L;
773
                i += sprintf(&array[i],"%02d",(s16)tmp1);
460 holgerb 774
 
489 killagreg 775
                tmp1 = abs(GPSData.Position.Latitude)%10000000L;
776
                tmp1 *= 6; // in Minuten
777
                tmp2 = tmp1 / 1000000L;
576 holgerb 778
                i += sprintf(&array[i],"%02d", (u16)tmp2);
489 killagreg 779
                tmp2 = tmp1 % 1000000L;
576 holgerb 780
                tmp2 /= 100; // zwei Stellen zu viel
781
                i += sprintf(&array[i],".%04d,", (u16)tmp2);
460 holgerb 782
 
489 killagreg 783
                if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
784
                else i += sprintf(&array[i],"S,");
785
                // +++++++++++++++++++++++++++++++++++++++++++
460 holgerb 786
 
489 killagreg 787
                tmp1 = abs(GPSData.Position.Longitude)/10000000L;
576 holgerb 788
                i += sprintf(&array[i],"%03d", (u16)tmp1);
460 holgerb 789
 
489 killagreg 790
                tmp1 = abs(GPSData.Position.Longitude)%10000000L;
791
                tmp1 *= 6; // in Minuten
792
                tmp2 = tmp1 / 1000000L;
576 holgerb 793
                i += sprintf(&array[i],"%02d", (u16)tmp2);
489 killagreg 794
                tmp2 = tmp1 % 1000000L;
576 holgerb 795
                tmp2 /= 100; // zwei Stellen zu viel
796
                i += sprintf(&array[i],".%04d,", (u16)tmp2);
460 holgerb 797
 
798
 
489 killagreg 799
                if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
800
                else i += sprintf(&array[i],"W,");
801
                i += sprintf(&array[i],"%d,",GPSData.SatFix);
802
                i += sprintf(&array[i],"%d,",GPSData.NumOfSats);
803
                i += sprintf(&array[i],"%d.%d,",(s16)(GPSData.Position_Accuracy/100),abs(GPSData.Position_Accuracy%100));
804
                //  i += sprintf(&array[i],"%d.%d,M,",GPSData.Position.Altitude/1000,abs(GPSData.Position.Altitude%1000)/100);
805
                tmp1 = NaviData.Altimeter / 2; // in dm
806
                i += sprintf(&array[i],"%d.%d,M,",(s16)tmp1 / 10,abs((s16)tmp1 % 10));
807
                i += sprintf(&array[i],",,,*");
808
        }
809
        else
810
        {
811
                i += sprintf(&array[i], ",,,,%d,00,99.99,,,,,,*",GPSData.NumOfSats);
812
        }
813
        for(x = 1; x < i-1; x++)
814
        {
815
                crc ^= array[x];
816
        }
817
        i += sprintf(&array[i], "%02x%c%c",crc,0x0d,0x0a);
818
        AddSerialData(&UART1_tx_buffer,array,i);
819
 
820
        // +++++++++++++++++++++++++++++++++++++++++++
466 holgerb 821
}
822
 
823
//$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006,1.2,E,A*13
824
//$GPRMC,HHMMSS.sss,A,BBBB.BBBB,b,LLLLL.LLLL,l,GG.G,RR.R,DDMMYY,M.M,m,F*PP
825
 
826
void CreateNmeaRMC(void)
827
{
489 killagreg 828
        u8 array[200], i = 0, crc = 0, x;
829
        s16 tmp_int;
830
        s32 tmp1, tmp2;
831
        // +++++++++++++++++++++++++++++++++++++++++++
832
        i += sprintf(array, "$GPRMC,");
833
        // +++++++++++++++++++++++++++++++++++++++++++
834
        if(SystemTime.Valid)
835
        {
836
                i += sprintf(&array[i], "%02d%02d%02d.%03d,", SystemTime.Hour, SystemTime.Min, SystemTime.Sec, SystemTime.mSec);
837
        }
838
        else
839
        {
840
                i += sprintf(&array[i], ",");
841
        }
842
        if(GPSData.Flags & FLAG_GPSFIXOK)
843
        {
844
                // +++++++++++++++++++++++++++++++++++++++++++
845
                tmp1 = abs(GPSData.Position.Latitude)/10000000L;
846
                i += sprintf(&array[i],"A,%02d", (s16)tmp1); // Status: A = Okay  V = Warnung
483 holgerb 847
 
489 killagreg 848
                tmp1 = abs(GPSData.Position.Latitude)%10000000L;
849
                tmp1 *= 6; // in Minuten
850
                tmp2 = tmp1 / 1000000L;
851
                i += sprintf(&array[i],"%02d", (s16)tmp2);
852
                tmp2 = tmp1 % 1000000L;
576 holgerb 853
                tmp2 /= 100; // zwei Stellen zu viel
854
                i += sprintf(&array[i],".%04d,", (s16)tmp2);
489 killagreg 855
                if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
856
                else i += sprintf(&array[i],"S,");
857
                // +++++++++++++++++++++++++++++++++++++++++++
858
                tmp1 = abs(GPSData.Position.Longitude)/10000000L;
859
                i += sprintf(&array[i],"%03d", (s16)tmp1);
483 holgerb 860
 
489 killagreg 861
                tmp1 = abs(GPSData.Position.Longitude)%10000000L;
862
                tmp1 *= 6; // in Minuten
863
                tmp2 = tmp1 / 1000000L;
864
                i += sprintf(&array[i],"%02d", (s16)tmp2);
865
                tmp2 = tmp1 % 1000000L;
576 holgerb 866
                tmp2 /= 100; // zwei Stellen zu viel
867
                i += sprintf(&array[i],".%04d,", (s16)tmp2);
489 killagreg 868
                if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
869
                else i += sprintf(&array[i],"W,");
870
                // +++++++++++++++++++++++++++++++++++++++++++
871
                tmp_int = GPSData.Speed_Ground; // in cm/sek
872
                tmp_int *= 90;
873
                tmp_int /= 463;
874
                i += sprintf(&array[i],"%02d.%d,",tmp_int/10,tmp_int%10); // in Knoten
875
                // +++++++++++++++++++++++++++++++++++++++++++
876
                i += sprintf(&array[i],"%03d.%d,",GyroCompassCorrected/10,GyroCompassCorrected%10);
877
                // +++++++++++++++++++++++++++++++++++++++++++
878
                if(SystemTime.Valid)
879
                {
880
                        i += sprintf(&array[i], "%02d%02d%02d,",SystemTime.Day,SystemTime.Month,SystemTime.Year);
881
                }
882
                else
883
                {
884
                        i += sprintf(&array[i], ",");
885
                }
886
                // +++++++++++++++++++++++++++++++++++++++++++
887
                i += sprintf(&array[i],"%d.%1d,", abs(GeoMagDec)/10,abs(GeoMagDec)%10);
888
                if(GeoMagDec < 0) i += sprintf(&array[i], "W,"); else i += sprintf(&array[i], "E,");
889
                // +++++++++++++++++++++++++++++++++++++++++++
576 holgerb 890
                if(GPSData.Flags & FLAG_DIFFSOLN) i += sprintf(&array[i], "D*");
891
                else i += sprintf(&array[i], "A*");
489 killagreg 892
        }
893
        else // kein Satfix
894
        {
895
                i += sprintf(&array[i], "V,,,,,,,,,,N*");
896
        }
897
        // +++++++++++++++++++++++++++++++++++++++++++
898
        //  CRC
899
        // +++++++++++++++++++++++++++++++++++++++++++
900
        for(x=1; x<i-1; x++)
901
        {
902
                crc ^= array[x];
903
        }
904
        i += sprintf(&array[i], "%02x%c%c",crc,0x0d,0x0a);
905
        // +++++++++++++++++++++++++++++++++++++++++++
906
        AddSerialData(&UART1_tx_buffer,array,i);
907
        // +++++++++++++++++++++++++++++++++++++++++++
908
/*
460 holgerb 909
 
910
 
911
 
489 killagreg 912
                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
913
                GPSData.NumOfSats =                     UbxSol.numSV;
914
                GPSData.SatFix =                                UbxSol.GPSfix;
915
                GPSData.Position_Accuracy =             UbxSol.PAcc;
916
                GPSData.Speed_Accuracy =                UbxSol.SAcc;
917
                SetGPSTime(&SystemTime); // update system time
918
                // NAV POSLLH
919
                GPSData.Position.Status =               INVALID;
920
                GPSData.Position.Longitude =    UbxPosLlh.LON;
921
                GPSData.Position.Latitude =     UbxPosLlh.LAT;
922
                GPSData.Position.Altitude =     UbxPosLlh.HMSL;
923
                GPSData.Position.Status =               NEWDATA;
924
                // NAV VELNED
925
                GPSData.Speed_East =                    UbxVelNed.VEL_E;
926
                GPSData.Speed_North =                   UbxVelNed.VEL_N;
927
                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;
928
                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;
929
                GPSData.Heading =                               UbxVelNed.Heading;
930
                SystemTime.Year = 0;
931
                SystemTime.Month = 0;
932
                SystemTime.Day = 0;
933
                SystemTime.Hour = 0;
934
                SystemTime.Min = 0;
935
                SystemTime.Sec = 0;
936
                SystemTime.mSec = 0;
937
                SystemTime.Valid = 0;
460 holgerb 938
 
489 killagreg 939
                FromFlightCtrl.GyroHeading / 10;//NaviData.HomePositionDeviation.Bearing / 2;
940
                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
941
                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
942
                        i1 = abs(GPSData.Position.Latitude)/10000000L;
943
                        i2 = abs(GPSData.Position.Latitude)%10000000L;
460 holgerb 944
 
489 killagreg 945
 
946
 
947
                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
948
                        i1 *= 100;
949
                        i1 += i2 / 100000;
950
                        i2  = i2 % 100000;
951
                        i2 /= 10;
952
                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
953
                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
954
                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
955
                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
956
                        break;
957
                case 1:
958
                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
959
                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
960
                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
961
                        //-----------------------------
962
                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
963
                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
964
                        i1 = abs(GPSData.Position.Longitude)/10000000L;
965
                        i2 = abs(GPSData.Position.Longitude)%10000000L;
966
 
460 holgerb 967
*/
968
}
969
 
970
 
599 holgerb 971
u16 TransmitNavigationData(u16 MaxBytesPerSecond, u8 clear) // returns the minumum pause time in ms
598 holgerb 972
{
599 holgerb 973
static u8 state = 0, count_flags = 2, count_target = 3, count_home = 4, count_wp = 5 , count_tiny = 6, count_fs = 7;
974
static u16 CRC_Home = 0, CRC_Target = 0, CRC_Flags = 0, CRC_Wp = 0, CRC_Fs = 0;
975
u16 pause, sent = 0, crc_home, crc_target, crc_flags, crc_wp, crc_fs;
598 holgerb 976
 
599 holgerb 977
 if(clear)
978
 {
979
        state = 0;
980
        CRC_Home++;
981
        CRC_Target++;
982
        CRC_Flags++;
983
        CRC_Wp++;
984
        CRC_Fs++;
985
        return(1);
986
 }
598 holgerb 987
while(!sent)
988
 {
989
                switch(state++)
990
                {
991
                 case 0:
599 holgerb 992
                 case 5:
598 holgerb 993
// belegt 35 ASCII-Zeichen
994
                                NaviData_Flags.Index = 11;
995
                                NaviData_Flags.ActualLongitude = NaviData.CurrentPosition.Longitude;
996
                                NaviData_Flags.ActualLatitude = NaviData.CurrentPosition.Latitude;
997
                                NaviData_Flags.Altimeter = NaviData.Altimeter;
998
                                NaviData_Flags.GroundSpeed = NaviData.GroundSpeed / 10;
999
                                NaviData_Flags.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1000
                                NaviData_Flags.OSDStatusFlags2 = (FC.StatusFlags & ~OSD_FLAG_MASK1) | (FC.StatusFlags2 & ~OSD_FLAG_MASK2);
1001
                                NaviData_Flags.NCFlags = NaviData.NCFlags;
1002
                                NaviData_Flags.Errorcode = ErrorCode;
1003
                                NaviData_Flags.ReserveFlags = 0;
1004
                                NaviData_Flags.SpeakHoTT = FC.FromFC_SpeakHoTT;
1005
                                NaviData_Flags.VarioCharacter = FromFC_VarioCharacter;
1006
                                NaviData_Flags.GPS_ModeCharacter = NC_GPS_ModeCharacter;
1007
                                NaviData_Flags.BL_MinOfMaxPWM = BL_MinOfMaxPWM;
1008
                                crc_flags = CRC16((unsigned char*)(&NaviData_Flags.OSDStatusFlags2), sizeof(NaviData_Flags) - START_PAYLOAD_DATA); // update crc for the license structure
1009
                                if((crc_flags != CRC_Flags) || (--count_flags == 0))
1010
                                {
1011
                                 sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Flags, sizeof(NaviData_Flags)) + 1;
1012
                                 CRC_Flags = crc_flags;
1013
                                 count_flags = 11*2;
1014
                                }
1015
                                break;
1016
                 case 1:
599 holgerb 1017
                 case 6:
598 holgerb 1018
// belegt 43 ASCII-Zeichen
1019
                                NaviData_Target.Index = 12;
1020
                                NaviData_Target.ActualLongitude = NaviData.CurrentPosition.Longitude;
1021
                                NaviData_Target.ActualLatitude = NaviData.CurrentPosition.Latitude;
1022
                                NaviData_Target.Altimeter = NaviData.Altimeter;
1023
                                NaviData_Target.GroundSpeed = NaviData.GroundSpeed / 10;
1024
                                NaviData_Target.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1025
                                NaviData_Target.TargetLongitude = NaviData.TargetPosition.Longitude;
1026
                                NaviData_Target.TargetLatitude = NaviData.TargetPosition.Latitude;
1027
                                NaviData_Target.TargetAltitude = NaviData.TargetPosition.Altitude;
1028
                                NaviData_Target.RC_Quality = NaviData.RC_Quality;
1029
                                crc_target = CRC16((unsigned char*)(&NaviData_Target.TargetLongitude), sizeof(NaviData_Target) - START_PAYLOAD_DATA); // update crc for the license structure
1030
                            if((crc_target != CRC_Target) || (--count_target == 0))
1031
                                {
1032
                                 sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Target, sizeof(NaviData_Target)) + 1;
1033
                                 CRC_Target = crc_target;
1034
                                 count_target = 10*2;
1035
                                }
1036
                                break;
1037
                 case 2:
599 holgerb 1038
                 case 7:
598 holgerb 1039
// belegt 31 ASCII-Zeichen
1040
                                NaviData_WP.Index = 15;
1041
                                NaviData_WP.ActualLongitude = NaviData.CurrentPosition.Longitude;
1042
                                NaviData_WP.ActualLatitude = NaviData.CurrentPosition.Latitude;
1043
                                NaviData_WP.Altimeter = NaviData.Altimeter;
1044
                                NaviData_WP.GroundSpeed = NaviData.GroundSpeed / 10;
1045
                                NaviData_WP.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1046
                                NaviData_WP.WaypointIndex = NaviData.WaypointIndex;
1047
                                NaviData_WP.WaypointNumber = NaviData.WaypointNumber;
1048
                                NaviData_WP.TargetHoldTime = NaviData.TargetHoldTime;
1049
                                // NaviData_WP.WP_Eventchannel = FC_WP_EventChannel; -> happends already in SPI.c
1050
                                crc_wp = CRC16((unsigned char*)(&NaviData_WP.WaypointIndex), sizeof(NaviData_WP) - START_PAYLOAD_DATA); // update crc for the license structure
1051
                                if((crc_wp != CRC_Wp) || (--count_wp == 0))
1052
                                {
1053
                         sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_WP, sizeof(NaviData_WP)) + 1;
1054
                                 CRC_Wp = crc_wp;
1055
                                 count_wp = 12*2;
1056
                                }
1057
                                break;
1058
                 case 3:
599 holgerb 1059
                 case 8:
1060
                                NaviData_Failsafe.Index = 17;
1061
                                NaviData_Failsafe.ActualLongitude = NaviData.CurrentPosition.Longitude;
1062
                                NaviData_Failsafe.ActualLatitude = NaviData.CurrentPosition.Latitude;
1063
                                NaviData_Failsafe.Altimeter = NaviData.Altimeter;
1064
                                NaviData_Failsafe.GroundSpeed = NaviData.GroundSpeed / 10;
1065
                                NaviData_Failsafe.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1066
                                NaviData_Failsafe.FailsafeLongitude = GPS_FailsafePosition.Longitude;
1067
                                NaviData_Failsafe.FailsafeLatitude = GPS_FailsafePosition.Latitude;
1068
                                NaviData_Failsafe.FailsafeAltitude = GPS_FailsafePosition.Altitude;
1069
                                crc_fs = CRC16((unsigned char*)(&NaviData_Failsafe.FailsafeLongitude), sizeof(NaviData_Failsafe_t) - START_PAYLOAD_DATA); // update crc for the license structure
1070
                                if((crc_fs != CRC_Fs) || (--count_fs == 0))
1071
                                {
1072
                                 sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Failsafe, sizeof(NaviData_Failsafe)) + 1;
1073
                                 CRC_Fs = crc_fs;
1074
                                 count_fs = 20*2;
1075
                                }
1076
                                break;
1077
                 case 4:
1078
                 case 9:
598 holgerb 1079
// belegt 43 ASCII-Zeichen
1080
                                NaviData_Home.Index = 13;
1081
                                NaviData_Home.ActualLongitude = NaviData.CurrentPosition.Longitude;
1082
                                NaviData_Home.ActualLatitude = NaviData.CurrentPosition.Latitude;
1083
                                NaviData_Home.Altimeter = NaviData.Altimeter;
1084
                                NaviData_Home.GroundSpeed = NaviData.GroundSpeed / 10;
1085
                                NaviData_Home.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1086
                                NaviData_Home.HomeLongitude = NaviData.HomePosition.Longitude;
1087
                                NaviData_Home.HomeLatitude = NaviData.HomePosition.Latitude;
1088
                                NaviData_Home.HomeAltitude = NaviData.HomePosition.Altitude;
1089
                                crc_home = CRC16((unsigned char*)(&NaviData_Home.HomeLongitude), sizeof(NaviData_Home_t) - START_PAYLOAD_DATA); // update crc for the license structure
1090
                                if((crc_home != CRC_Home) || (--count_home == 0))
1091
                                {
1092
                                 sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Home, sizeof(NaviData_Home)) + 1;
1093
                                 CRC_Home = crc_home;
1094
                                 count_home = 25;
1095
                                }
1096
                                break;
599 holgerb 1097
                 case 10:
598 holgerb 1098
// belegt 39 ASCII-Zeichen
1099
                                NaviData_Deviation.Index = 14;
1100
                                NaviData_Deviation.ActualLongitude = NaviData.CurrentPosition.Longitude;
1101
                                NaviData_Deviation.ActualLatitude = NaviData.CurrentPosition.Latitude;
1102
                                NaviData_Deviation.Altimeter = NaviData.Altimeter;
1103
                                NaviData_Deviation.GroundSpeed = NaviData.GroundSpeed / 10;
1104
                                NaviData_Deviation.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1105
                                NaviData_Deviation.FlyingTime = NaviData.FlyingTime;
1106
                                NaviData_Deviation.DistanceToHome = NaviData.HomePositionDeviation.Distance;
1107
                                NaviData_Deviation.HeadingToHome = NaviData.HomePositionDeviation.Bearing/2;
1108
                                NaviData_Deviation.DistanceToTarget = NaviData.TargetPositionDeviation.Distance;
1109
                                NaviData_Deviation.HeadingToTarget = NaviData.TargetPositionDeviation.Bearing/2;
1110
                                NaviData_Deviation.AngleNick = NaviData.AngleNick;
1111
                                NaviData_Deviation.AngleRoll = NaviData.AngleRoll;
1112
                                NaviData_Deviation.SatsInUse = NaviData.SatsInUse;
1113
                            sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Deviation, sizeof(NaviData_Deviation)) + 1;
1114
                                break;
599 holgerb 1115
                 case 11:
598 holgerb 1116
// belegt 43 ASCII-Zeichen
1117
                                NaviData_Volatile.Index = 16;
1118
                                NaviData_Volatile.ActualLongitude = NaviData.CurrentPosition.Longitude;
1119
                                NaviData_Volatile.ActualLatitude = NaviData.CurrentPosition.Latitude;
1120
                                NaviData_Volatile.Altimeter = NaviData.Altimeter;
1121
                                NaviData_Volatile.GroundSpeed = NaviData.GroundSpeed / 10;
1122
                                NaviData_Volatile.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1123
                                NaviData_Volatile.UBat = FC.BAT_Voltage;
1124
                                NaviData_Volatile.Current = NaviData.Current;
1125
                                NaviData_Volatile.UsedCapacity = NaviData.UsedCapacity;
1126
                                NaviData_Volatile.Variometer = NaviData.Variometer;
1127
                                NaviData_Volatile.Heading = NaviData.Heading / 2;
1128
                                NaviData_Volatile.CompassHeading = NaviData.CompassHeading / 2;
1129
                                NaviData_Volatile.Gas = NaviData.Gas;
599 holgerb 1130
                                NaviData_Volatile.SetpointAltitude = NaviData.SetpointAltitude;
598 holgerb 1131
                                sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Volatile, sizeof(NaviData_Volatile)) + 1;
1132
                        break;
599 holgerb 1133
                 case 12:
598 holgerb 1134
// belegt 27 ASCII-Zeichen
1135
                                NaviData_Tiny.Index = 10;
1136
                                NaviData_Tiny.ActualLongitude = NaviData.CurrentPosition.Longitude;
1137
                                NaviData_Tiny.ActualLatitude = NaviData.CurrentPosition.Latitude;
1138
                                NaviData_Tiny.Altimeter = NaviData.Altimeter;
1139
                                NaviData_Tiny.GroundSpeed = NaviData.GroundSpeed / 10;
1140
                                NaviData_Tiny.OSDStatusFlags = (FC.StatusFlags & OSD_FLAG_MASK1) | (FC.StatusFlags2 & OSD_FLAG_MASK2);
1141
                                if(--count_tiny == 0)
1142
                                 {
1143
                                        sent += MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData_Tiny, sizeof(NaviData_Tiny)) + 1;
1144
                                        count_tiny = 60;
1145
                                 }
1146
                        break;
1147
                 default: state = 0;
1148
                                break;
1149
                }
1150
 }
1151
        pause = (sent * 1000) / MaxBytesPerSecond;
1152
 
1153
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
1154
        UART1_Request_NaviData = FALSE;
1155
        LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
1156
return(pause);
1157
// Clear at timeout: 
1158
// NaviData_WP.WP_Eventchannel
1159
}
1160
 
41 ingob 1161
/**************************************************************/
1162
/* Send the answers to incomming commands at the debug uart   */
1163
/**************************************************************/
1164
void UART1_TransmitTxData(void)
1165
{
358 holgerb 1166
static u8 motorindex1 = 255, motorindex2 = 0;
110 killagreg 1167
        if(DebugUART != UART1) return;
198 killagreg 1168
 
1169
        if(CheckDelay(UART1_AboTimeOut))
1170
        {
1171
                UART1_DebugData_Interval = 0;
1172
                UART1_NaviData_Interval = 0;
598 holgerb 1173
                UART1_NaviData_MaxBytes = 0;
198 killagreg 1174
                UART1_Data3D_Interval = 0;
1175
                UART1_Display_Interval = 0;
378 holgerb 1176
                UART1_MotorData_Interval = 0;
598 holgerb 1177
                UART1_NaviData_Timer = SetDelay(500);
1178
                UART1_AboTimeOut = SetDelay(100);
599 holgerb 1179
                TransmitNavigationData(0,1); // clear the CRC values
198 killagreg 1180
        }
598 holgerb 1181
/*
1182
#define CHK_MIN_INTERVAL(a,b) {if(a && a < b) a = b;}
1183
UART1_NaviData_Interval = 500;
1184
CHK_MIN_INTERVAL(UART1_DebugData_Interval,500);
1185
CHK_MIN_INTERVAL(UART1_NaviData_Interval,1000);
1186
CHK_MIN_INTERVAL(UART1_Data3D_Interval,255);
1187
CHK_MIN_INTERVAL(UART1_Display_Interval,1500);
1188
CHK_MIN_INTERVAL(UART1_MotorData_Interval,750);
1189
*/
110 killagreg 1190
        UART1_Transmit(); // output pending bytes in tx buffer
1191
        if((UART1_tx_buffer.Locked == TRUE)) return;
41 ingob 1192
 
156 killagreg 1193
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
92 killagreg 1194
        {
315 killagreg 1195
                s16 ParamValue;
299 killagreg 1196
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
160 holgerb 1197
                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 1198
                UART1_Request_Parameter = FALSE;
1199
        }
1200
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
1201
        {
110 killagreg 1202
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
92 killagreg 1203
                Echo = 0; // reset echo value
110 killagreg 1204
                UART1_Request_Echo = FALSE;
92 killagreg 1205
        }
349 ingob 1206
        else if(UART1_Request_FTP && (UART1_tx_buffer.Locked == FALSE))
1207
        {
433 ingob 1208
                u8 errorcode = FTP_ERROR_NONE;
1209
        if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) errorcode = FTP_ERROR_MOTOR_RUN;
1210
                else if (!Partition.IsValid) errorcode = FTP_ERROR_NO_SDCARD;
1211
 
1212
                if (!errorcode) CheckFTPCommand(UART1_Request_FTP);
489 killagreg 1213
                else
433 ingob 1214
                {
1215
                        u8 cmd = FTP_CMD_ERROR;
1216
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &cmd, 1, &errorcode, 1);
1217
                }
1218
 
349 ingob 1219
                UART1_Request_FTP = FALSE;
1220
        }
295 killagreg 1221
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
154 killagreg 1222
        {
295 killagreg 1223
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
1224
                UART1_Request_WritePoint = 0xFF;
154 killagreg 1225
        }
280 killagreg 1226
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
154 killagreg 1227
        {
280 killagreg 1228
                u8 PointCount = PointList_GetCount();
1229
                if (UART1_Request_ReadPoint <= PointCount)
154 killagreg 1230
                {
280 killagreg 1231
                        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 1232
                }
1233
                else
1234
                {
280 killagreg 1235
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
154 killagreg 1236
                }
280 killagreg 1237
                UART1_Request_ReadPoint = 0;
154 killagreg 1238
        }
1239
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
1240
        {
1241
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
1242
                UART1_Request_DebugLabel = 0xFF;
1243
        }
398 holgerb 1244
        else if(UART1_ExternalControlConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
1245
        {
1246
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1,(u8 *)&UART1_ExternalControlConfirmFrame, sizeof(UART1_ExternalControlConfirmFrame));
1247
                UART1_ExternalControlConfirmFrame = 0;
1248
        }
151 killagreg 1249
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1250
        {
598 holgerb 1251
          u16 time = 0;
599 holgerb 1252
//UART1_NaviData_MaxBytes = 250;
598 holgerb 1253
      if(UART1_NaviData_MaxBytes == 0) // Transmit the big NC Data frame
1254
           {
151 killagreg 1255
                NaviData.Errorcode = ErrorCode;
378 holgerb 1256
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS, 1 , (u8 *)&NaviData, sizeof(NaviData));
598 holgerb 1257
                LastTransmittedFCStatusFlags2 = NaviData.FCStatusFlags2;
151 killagreg 1258
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
598 holgerb 1259
           }
1260
           else
1261
           if(CheckDelay(UART1_NaviData_Timer))
1262
           {
1263
// Wert =  50 -> Sekunden laufen in 2 s-Abstand (im Wegpunkteflug mit aktivem Countdown in 3-4s)
1264
// Wert = 100 -> Sekunden laufen in 1-2 s-Abstand (im Wegpunkteflug mit aktivem Countdown in 2s)
1265
// Wert = 150 -> Sekunden laufen flüssig (im Wegpunkteflug mit aktivem Countdown und gleichzeitig Target verschieben manchmal in 2s)
1266
// Wert = 200 -> Sekunden laufen flüssig
1267
// Wert >= 250 -> optimal 
1268
//UART1_NaviData_MaxBytes = 250;
599 holgerb 1269
            time = TransmitNavigationData(UART1_NaviData_MaxBytes,0);
598 holgerb 1270
                if(UART1_NaviData_Interval > time) time = UART1_NaviData_Interval;
1271
                UART1_NaviData_Timer = SetDelay(time);
1272
           }
1273
        UART1_Request_NaviData = FALSE;
151 killagreg 1274
        }
1275
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
1276
        {
110 killagreg 1277
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
1278
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
1279
                UART1_Request_DebugData = FALSE;
41 ingob 1280
        }
151 killagreg 1281
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
61 holgerb 1282
        {
358 holgerb 1283
                Data3D.StickNick = FC.StickNick;
1284
                Data3D.StickRoll = FC.StickRoll;
1285
        Data3D.StickYaw = FC.StickYaw;
1286
        Data3D.StickGas = FC.StickGas;
588 holgerb 1287
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
1288
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
1289
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
1290
                Data3D.Altimeter = FC.Altimeter;                                        // in 5cm -> 20 = 1m
110 killagreg 1291
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
1292
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
1293
                UART1_Request_Data3D = FALSE;
61 holgerb 1294
        }
358 holgerb 1295
        else if((((UART1_MotorData_Interval > 0) && CheckDelay(UART1_MotorData_Timer) ) || UART1_Request_MotorData) && (UART1_tx_buffer.Locked == FALSE))
1296
        {
1297
                do
1298
                {
1299
                 motorindex1++;
1300
                 motorindex1%=12;
1301
         if(!motorindex1) {motorindex2++;  motorindex2 %= 12;};
1302
                 if(motorindex1 == motorindex2) break;
1303
                }
1304
                while((Motor[motorindex1].State & 0x80) != 0x80); // skip unused Motors
1305
 
1306
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'K', NC_ADDRESS, 2, &motorindex1, sizeof(motorindex1),(u8 *)&Motor[motorindex1], sizeof(Motor_t));
1307
            UART1_MotorData_Timer = SetDelay(UART1_MotorData_Interval);
1308
                UART1_Request_MotorData = FALSE;
1309
        }
494 killagreg 1310
        else if(UART1_Request_WPLStore)
1311
        {
495 killagreg 1312
                /*
1313
                s8 txt[50];
1314
                sprintf(txt, "\r\nWPL Overwride = %d, Type = %d, Index = %d, Status = %d\r\n",  WPL_Store.OverwriteFile,  WPL_Store.Type, WPL_Answer.Index, WPL_Answer.Status);
1315
                UART1_PutString(txt);
1316
                */
1317
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'I', NC_ADDRESS, 1,(u8 *)&(WPL_Answer), sizeof(WPL_Answer_t));
494 killagreg 1318
                UART1_Request_WPLStore = FALSE;                
1319
        }
460 holgerb 1320
        else if((((NMEA_Interval > 0) && CheckDelay(NMEA_Timer))) && (UART1_tx_buffer.Locked == FALSE))
1321
        {
1322
                CreateNmeaGGA();
466 holgerb 1323
                Send_NMEA_RMC = TRUE;   // das muss noch da hinter
460 holgerb 1324
                NMEA_Timer = SetDelay(NMEA_Interval);
1325
        }
466 holgerb 1326
        else if(Send_NMEA_RMC == TRUE && (UART1_tx_buffer.Locked == FALSE))
1327
        {
1328
          CreateNmeaRMC();
1329
          Send_NMEA_RMC = FALSE;
1330
        }
460 holgerb 1331
 
193 killagreg 1332
        /*
153 killagreg 1333
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
1334
        {
1335
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
1336
                UART1_ConfirmFrame = 0;
1337
        }
193 killagreg 1338
        */
1339
        /*
151 killagreg 1340
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1341
        {
110 killagreg 1342
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
1343
                UART1_Request_ExternalControl = FALSE;
41 ingob 1344
        }
193 killagreg 1345
        */
1346
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
195 killagreg 1347
        {
193 killagreg 1348
                if(UART1_DisplayLine > 3)
1349
                {
201 killagreg 1350
                        Menu_Update(UART1_DisplayKeys);
1351
                        UART1_DisplayKeys = 0;
193 killagreg 1352
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
1353
                }
1354
                else
1355
                {
1356
                        UART1_DisplayLine = 2;
1357
                        sprintf(text,"!!! incompatible !!!");
1358
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
1359
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
1360
                }
1361
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
110 killagreg 1362
                UART1_Request_Display = FALSE;
41 ingob 1363
        }
151 killagreg 1364
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1365
        {
201 killagreg 1366
                Menu_Update(0);
110 killagreg 1367
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
1368
                UART1_Request_Display1 = FALSE;
41 ingob 1369
        }
548 holgerb 1370
        else if(UART1_Request_VersionInfo == 1 && (UART1_tx_buffer.Locked == FALSE)) // get FC-Versions
41 ingob 1371
        {
548 holgerb 1372
                UART_VersionInfo_t version_tmp;
1373
                version_tmp.SWMajor = FC_Version.Major;
1374
                version_tmp.SWMinor = FC_Version.Minor;
1375
                version_tmp.SWPatch = FC_Version.Patch;
1376
                version_tmp.HWMajor = FC_Version.Hardware;
1377
                version_tmp.HardwareError[0] = 0xff; // tells the KopterTool that it is the FC-version 
1378
                version_tmp.HardwareError[1] = 0xff; // tells the KopterTool that it is the FC-version 
1379
                version_tmp.ProtoMajor = UART_VersionInfo.ProtoMajor;
1380
                version_tmp.BL_Firmware = UART_VersionInfo.BL_Firmware;
1381
                version_tmp.Flags = 0;
598 holgerb 1382
                version_tmp.LabelTextCRC = 0;//UART_VersionInfo.DebugTextCRC;
548 holgerb 1383
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&version_tmp, sizeof(version_tmp));
1384
                UART1_Request_VersionInfo = FALSE;
1385
        }
1386
        else if(UART1_Request_VersionInfo == 2 && (UART1_tx_buffer.Locked == FALSE)) // get NC-Versions
1387
        {
110 killagreg 1388
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
1389
                UART1_Request_VersionInfo = FALSE;
41 ingob 1390
        }
447 holgerb 1391
        else if(UART1_Request_SystemTime && (UART1_tx_buffer.Locked == FALSE))
1392
        {
1393
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'T', NC_ADDRESS,1, (u8 *)&SystemTime, sizeof(SystemTime));
1394
                UART1_Request_SystemTime = FALSE;
1395
        }
151 killagreg 1396
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
41 ingob 1397
        {
110 killagreg 1398
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
1399
                UART1_Request_ErrorMessage = FALSE;
41 ingob 1400
        }
529 holgerb 1401
        else if(UART1_Request_LicenseString && (UART1_tx_buffer.Locked == FALSE))
1402
        {
531 holgerb 1403
                u8 result = 1, cmd = 0;
529 holgerb 1404
                if(UART1_Request_LicenseString == LIC_CMD_READ_LICENSE)  
1405
                {
1406
                  result = LIC_CMD_READ_LICENSE;
1407
                  MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'M', NC_ADDRESS, 2, &result, 1, LicensePtr, LICENSE_SIZE);
1408
                }
1409
                else
531 holgerb 1410
                if((UART1_Request_LicenseString == LIC_CMD_ERASE_LICENSE) && !(FC.StatusFlags & FC_STATUS_MOTOR_RUN) && (UART_VersionInfo.HWMajor >= 20))
529 holgerb 1411
                 {
1412
                  result = LIC_CMD_ERASE_LICENSE;
1413
                  DeleteLicenseInEEPROM();
1414
                  CheckLicense(GET_LICENSE);
1415
                  MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'M', NC_ADDRESS, 1, &result, 1);
1416
                 }
1417
                else
531 holgerb 1418
                if((UART1_Request_LicenseString == LIC_CMD_WRITE_LICENSE) && !(FC.StatusFlags & FC_STATUS_MOTOR_RUN) && (UART_VersionInfo.HWMajor >= 20))
529 holgerb 1419
                {
531 holgerb 1420
                  cmd = LIC_CMD_WRITE_LICENSE;
529 holgerb 1421
                  if(CheckLicense(CHECK_ONLY))  //new license is okay
1422
                  {
1423
                   WriteLicenseToEEPROM(EEPROM_LICENSE_DATA_KOMPATIBEL);
530 holgerb 1424
                   result = 1;
529 holgerb 1425
                  }
1426
                  else //new license is NOT okay
1427
                  {
1428
                   ClearLicenseText();
531 holgerb 1429
                   result = 0;
1430
                   CheckLicense(GET_LICENSE);  // fetch a license if available
529 holgerb 1431
                  }
531 holgerb 1432
                  MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'M', NC_ADDRESS, 2, &cmd, 1,&result, 1 );
529 holgerb 1433
                }
1434
                UART1_Request_LicenseString = 0;
1435
        }
314 killagreg 1436
#ifdef FOLLOW_ME
1437
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
1438
        {
1439
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
315 killagreg 1440
                {
314 killagreg 1441
                        TransmitAlsoToFC = 1;
1442
                        // update FollowMe content
1443
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
1444
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
1445
                        FollowMe.Position.Status = NEWDATA;
1446
                        FollowMe.Position.Altitude = 1;
1447
                        // 0  -> no Orientation
1448
                        // 1-360 -> CompassCourse Setpoint
1449
                        // -1 -> points to  WP1 -> itself
1450
                FollowMe.Heading = -1;
1451
                        FollowMe.ToleranceRadius = 1;
1452
                        FollowMe.HoldTime = 60;
1453
                        FollowMe.Event_Flag = 1;
1454
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
1455
                        FollowMe.Type = POINT_TYPE_WP;
1456
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
1457
                        FollowMe.AltitudeRate = 0;                        // do not change height
403 holgerb 1458
                        FollowMe.Speed = 0;                             // rate to change the Position (0 = max)
1459
                        FollowMe.CamAngle = 255;                // Camera servo angle in degree (255 -> POI-Automatic)
1460
                        FollowMe.Name[0] = 'F';             // Name of that point (ASCII)
1461
                        FollowMe.Name[1] = 'O';             // Name of that point (ASCII)
1462
                        FollowMe.Name[2] = 'L';             // Name of that point (ASCII)
1463
                        FollowMe.Name[3] = 'L';             // Name of that point (ASCII)
314 killagreg 1464
                        FollowMe.reserve[0] = 0;                // reserve
1465
                        FollowMe.reserve[1] = 0;                // reserve
1466
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
1467
                }
1468
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
1469
        }
1470
#endif
250 ingob 1471
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
314 killagreg 1472
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
254 killagreg 1473
        {
1474
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
1475
                SendDebugOutput = 0;
1476
        }
315 killagreg 1477
#endif
110 killagreg 1478
        UART1_Transmit(); // output pending bytes in tx buffer
41 ingob 1479
}
1480