Subversion Repositories NaviCtrl

Rev

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