Subversion Repositories NaviCtrl

Rev

Rev 848 | Rev 856 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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