Subversion Repositories NaviCtrl

Rev

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