Subversion Repositories NaviCtrl

Rev

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