Subversion Repositories NaviCtrl

Rev

Rev 101 | Rev 112 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 101 Rev 110
Line 57... Line 57...
57
#include <stdio.h>
57
#include <stdio.h>
58
#include <stdarg.h>
58
#include <stdarg.h>
59
#include <string.h>
59
#include <string.h>
Line 60... Line 60...
60
 
60
 
61
#include "91x_lib.h"
-
 
62
#include "ramfunc.h"
61
#include "91x_lib.h"
63
#include "menu.h"
62
#include "menu.h"
64
#include "printf_P.h"
63
#include "printf_P.h"
65
#include "GPS.h"
64
#include "GPS.h"
66
#include "i2c.h"
65
#include "i2c.h"
67
#include "uart0.h"
66
#include "uart0.h"
68
#include "uart1.h"
67
#include "uart1.h"
69
#include "uart2.h"
68
#include "uart2.h"
70
#include "timer.h"
-
 
71
#include "usb.h"
69
#include "timer.h"
72
#include "main.h"
70
#include "main.h"
73
#include "waypoints.h"
71
#include "waypoints.h"
74
#include "GPS.h"
-
 
75
 
-
 
76
// slave addresses
-
 
77
#define FC_ADDRESS 1
-
 
78
#define NC_ADDRESS 2
-
 
Line 79... Line 72...
79
#define MK3MAG_ADDRESS 3
72
#include "mkprotocol.h"
80
 
73
 
Line 81... Line 74...
81
#define FALSE   0
74
#define FALSE   0
Line 90... Line 83...
90
        u8 SWPatch;
83
        u8 SWPatch;
91
        u8 Reserved[5];
84
        u8 Reserved[5];
92
}  __attribute__((packed)) UART_VersionInfo_t;
85
}  __attribute__((packed)) UART_VersionInfo_t;
Line 93... Line 86...
93
 
86
 
94
 
87
 
95
u8 Request_SendFollowMe         = FALSE;
88
u8 UART1_Request_VersionInfo    = FALSE;
96
u8 Request_VerInfo                      = FALSE;
89
u8 UART1_Request_SendFollowMe   = FALSE;
97
u8 Request_ExternalControl      = FALSE;
90
u8 UART1_Request_ExternalControl= FALSE;
98
u8 Request_Display                      = FALSE;
91
u8 UART1_Request_Display                = FALSE;
99
u8 Request_Display1             = FALSE;
92
u8 UART1_Request_Display1               = FALSE;
100
u8 Request_DebugData            = FALSE;
-
 
101
u8 Request_DebugLabel           = 255;
93
u8 UART1_Request_DebugData              = FALSE;
102
u8 Request_ChannelOnly          = FALSE;
94
u8 UART1_Request_DebugLabel             = 255;
103
u8 Request_NaviData                     = FALSE;
95
u8 UART1_Request_NaviData               = FALSE;
104
u8 Request_ErrorMessage     = FALSE;
96
u8 UART1_Request_ErrorMessage   = FALSE;
105
u8 Request_NewWaypoint          = FALSE;
97
u8 UART1_Request_NewWaypoint    = FALSE;
106
u8 Request_ReadWaypoint         = 255;
98
u8 UART1_Request_ReadWaypoint   = 255;
Line 107... Line 99...
107
u8 Request_Data3D                   = FALSE;
99
u8 UART1_Request_Data3D             = FALSE;
Line 108... Line 100...
108
u8 Request_Echo                 = FALSE;
100
u8 UART1_Request_Echo               = FALSE;
Line -... Line 101...
-
 
101
 
-
 
102
u8 DisplayLine = 0;
109
 
103
 
110
u8 DisplayLine = 0;
104
UART_TypeDef *DebugUART = UART1;
111
 
105
 
112
UART_TypeDef *DebugUART = UART1;
106
#define UART1_BAUD_RATE 57600           //Baud Rate for the serial interfaces
-
 
107
 
113
 
108
// the tx buffer
114
volatile u8 txd_buffer[TXD_BUFFER_LEN];
109
#define UART1_TX_BUFFER_LEN  150
115
volatile u8 rxd_buffer_locked = FALSE;
110
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
116
volatile u8 rxd_buffer[RXD_BUFFER_LEN];
111
Buffer_t UART1_tx_buffer;
-
 
112
 
117
volatile u8 txd_complete = TRUE;
113
// the rx buffer
Line 118... Line 114...
118
volatile u8 ReceivedBytes = 0;
114
#define UART1_RX_BUFFER_LEN  150
Line 119... Line 115...
119
volatile u8 CntCrcError = 0;
115
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
120
volatile u8 *pRxData = NULL;
-
 
121
volatile u8 RxDataLen = 0;
116
Buffer_t UART1_rx_buffer;
Line 122... Line 117...
122
volatile u8 SerialLinkOkay = 0;
117
 
123
 
118
volatile u8 SerialLinkOkay = 0;
124
u8 text[100];
119
 
125
 
120
u8 text[100];
126
u8 PcAccess = 100;
121
 
127
u8 MotorTest[4] = {0,0,0,0};
122
u8 PcAccess = 100;
128
u8 ConfirmFrame = 0;
123
u8 ConfirmFrame = 0;
Line 129... Line 124...
129
 
124
 
130
DebugOut_t DebugOut;
125
DebugOut_t DebugOut;
131
ExternControl_t ExternControl;
126
ExternControl_t ExternControl;
132
UART_VersionInfo_t UART_VersionInfo;
127
UART_VersionInfo_t UART_VersionInfo;
133
NaviData_t NaviData;
128
NaviData_t NaviData;
134
Waypoint_t FollowMe;
129
Waypoint_t FollowMe;
135
Data3D_t Data3D;
-
 
136
u16 Echo; // 2 bytes recieved will be sent back as echo
-
 
Line 137... Line 130...
137
 
130
Data3D_t Data3D;
138
u32 DebugData_Timer;
131
u16 Echo; // 2 bytes recieved will be sent back as echo
139
u32 DebugData_Interval = 5000;  // in ms
132
 
140
u32 NaviData_Timer;
133
u32 UART1_DebugData_Timer;
Line 221... Line 214...
221
        */
214
        */
222
        UART_StructInit(&UART_InitStructure);
215
        UART_StructInit(&UART_InitStructure);
223
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
216
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
224
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
217
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
225
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
218
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
226
        UART_InitStructure.UART_BaudRate =                              BAUD_RATE;
219
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
227
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
220
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
228
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
221
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
229
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
222
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
230
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
223
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
231
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
224
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
Line 238... Line 231...
238
        // configure the uart 1 interupt line as an IRQ with priority 4 (0 is highest)
231
        // configure the uart 1 interupt line as an IRQ with priority 4 (0 is highest)
239
        VIC_Config(UART1_ITLine, VIC_IRQ, 4);
232
        VIC_Config(UART1_ITLine, VIC_IRQ, 4);
240
        // enable the uart 1 IRQ
233
        // enable the uart 1 IRQ
241
        VIC_ITCmd(UART1_ITLine, ENABLE);
234
        VIC_ITCmd(UART1_ITLine, ENABLE);
242
        // initialize the debug timer
235
        // initialize the debug timer
243
        DebugData_Timer = SetDelay(DebugData_Interval);
236
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
244
        NaviData_Timer = SetDelay(NaviData_Interval)+500;
237
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
-
 
238
       
245
        // unlock rxd_buffer
239
        // initialize txd buffer
-
 
240
        UART1_tx_buffer.pData = UART1_tbuffer;
-
 
241
        UART1_tx_buffer.Size = UART1_TX_BUFFER_LEN;
-
 
242
        UART1_tx_buffer.Position = 0;
246
        rxd_buffer_locked = FALSE;
243
        UART1_tx_buffer.Locked = FALSE;
-
 
244
       
247
        pRxData = NULL;
245
        // initialize rxd buffer
248
        RxDataLen = 0;
246
        UART1_rx_buffer.pData = UART1_rbuffer;
-
 
247
        UART1_rx_buffer.Size = UART1_RX_BUFFER_LEN;
249
        // no bytes to send
248
        UART1_rx_buffer.Position = 0;
250
        txd_complete = TRUE;
249
        UART1_rx_buffer.Locked = FALSE;
-
 
250
 
251
        // Fill Version Info Structure
251
        // Fill Version Info Structure
252
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
252
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
253
        UART_VersionInfo.SWMinor = VERSION_MINOR;
253
        UART_VersionInfo.SWMinor = VERSION_MINOR;
254
        UART_VersionInfo.SWPatch = VERSION_PATCH;
254
        UART_VersionInfo.SWPatch = VERSION_PATCH;
255
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
255
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
256
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
256
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
Line 257... Line 257...
257
 
257
 
Line 258... Line 258...
258
        NaviData.Version = NAVIDATA_VERSION;
258
        NaviData.Version = NAVIDATA_VERSION;
259
 
259
 
Line 260... Line 260...
260
        SerialPutString("\r\nUART1 init...ok");
260
        UART1_PutString("\r\nUART1 init...ok");
261
}
261
}
262
 
262
 
263
 
263
 
264
/****************************************************************/
264
/****************************************************************/
265
/*               USART1 receiver ISR                            */
-
 
266
/****************************************************************/
-
 
267
void UART1_IRQHandler(void)
-
 
268
{
265
/*               USART1 receiver ISR                            */
269
        static u16 crc;
266
/****************************************************************/
Line 270... Line 267...
270
        static u8 ptr_rxd_buffer = 0;
267
void UART1_IRQHandler(void)
271
        static u8 crc1, crc2;
268
{
Line 280... Line 277...
280
                // if debug UART is not UART1
277
                // if debug UART is not UART1
281
                if (DebugUART != UART1)
278
                if (DebugUART != UART1)
282
                {       // forward received data to the debug UART tx buffer
279
                {       // forward received data to the debug UART tx buffer
283
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
280
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
284
                        {
281
                        {
285
                                // wait for space in the tx buffer of the DebugUART
-
 
286
                                while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
-
 
287
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
282
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
288
                                c = UART_ReceiveData(UART1);
283
                                c = UART_ReceiveData(UART1);
Line 289... Line 284...
289
 
284
 
290
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
285
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
Line 304... Line 299...
304
                                                        UART0_Connect_to_MKGPS();
299
                                                        UART0_Connect_to_MKGPS();
305
                                                   }
300
                                                   }
306
                                          abortState = 0;
301
                                          abortState = 0;
307
                                                break;
302
                                                break;
308
                                }
303
                                }
-
 
304
                                // if the Debug uart is not UART1rederect input to the Debug UART
-
 
305
                                if (DebugUART != UART1)
309
 
306
                                {
-
 
307
                                        // wait for space in the tx buffer of the DebugUART
-
 
308
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
-
 
309
                                        // move byte to the tx fifi of the debug uart
310
                                if (DebugUART != UART1) UART_SendData(DebugUART, c);
310
                                        UART_SendData(DebugUART, c);
-
 
311
                                }
311
                        }
312
                        }
312
                }
313
                }
313
                else  // DebugUART == UART1 (normal operation)
314
                else  // DebugUART == UART1 (normal operation)
314
                {
315
                {
315
                        while ((UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET) && (!rxd_buffer_locked))
316
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
316
                        { // some byes in the fifo and rxd buffer not locked
317
                        { // some byes in the fifo and rxd buffer not locked
317
                            // get byte from fifo
318
                            // get byte from fifo
318
                        c = UART_ReceiveData(UART1);
319
                        c = UART_ReceiveData(UART1);
319
                                if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
-
 
320
                                {
-
 
321
                                        rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
-
 
322
                                        crc = c; // init crc
-
 
323
                                }
-
 
324
                                #if 0
-
 
325
                                else if (ptr_rxd_buffer == 1) // handle address
-
 
326
                                {
-
 
327
                                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
-
 
328
                                        crc += c; // update crc
-
 
329
                                }
-
 
330
                                #endif
-
 
331
                                else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // rxd buffer not full
-
 
332
                                {
-
 
333
                                        if (c != '\r') // no termination character received
-
 
334
                                        {
-
 
335
                                                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
-
 
336
                                                crc += c; // update crc
-
 
337
                                        }
-
 
338
                                        else // termination character received
-
 
339
                                        {
-
 
340
                                                // the last 2 bytes are no subject for checksum calculation
-
 
341
                                                // they are the checksum itself
-
 
342
                                                crc -= rxd_buffer[ptr_rxd_buffer-2];
-
 
343
                                                crc -= rxd_buffer[ptr_rxd_buffer-1];
320
                                MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c);
344
                                                // calculate checksum from transmitted data
-
 
345
                                                crc %= 4096;
-
 
346
                                                crc1 = '=' + crc / 64;
-
 
347
                                                crc2 = '=' + crc % 64;
-
 
348
                                                // compare checksum to transmitted checksum bytes
-
 
349
                                                if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
-
 
350
                                                {   // checksum valid
-
 
351
                                                        rxd_buffer_locked = TRUE;               // lock the rxd buffer
-
 
352
                                                        ReceivedBytes = ptr_rxd_buffer + 1;     // store number of received bytes
-
 
353
                                                        rxd_buffer[ptr_rxd_buffer] = '\r';      // set termination character
-
 
354
                                                        // if 2nd byte is an 'R' start bootloader
-
 
355
                                                        if(rxd_buffer[2] == 'R')
-
 
356
                                                        {
-
 
357
                                                                PowerOff();
-
 
358
                                                                VIC_DeInit();
-
 
359
                                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
-
 
360
                                                        }
-
 
361
                                                } // eof checksum valid
-
 
362
                                                else
-
 
363
                                                {       // checksum invalid
-
 
364
                                                        rxd_buffer_locked = FALSE; // unlock rxd buffer
-
 
365
                                                }  // eof checksum invalid
-
 
366
                                                ptr_rxd_buffer = 0; // reset rxd buffer pointer
-
 
367
                                        } // eof termination character received
-
 
368
                                } // rxd buffer not full
-
 
369
                                else // rxd buffer overrun
-
 
370
                                {
-
 
371
                                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
-
 
372
                                        rxd_buffer_locked = FALSE; // unlock rxd buffer
-
 
373
                                } // eof rxd buffer overrrun
-
 
374
                        } // some byes in the fifo and rxd buffer not locked
321
                        } // some byes in the fifo and rxd buffer not locked
375
                } // eof DebugUart = UART1
322
                } // eof DebugUart = UART1
376
        }
323
        }
377
}
324
}
Line 378... Line 325...
378
 
325
 
379
/**************************************************************/
-
 
380
/*         Transmit tx buffer via debug uart                  */
-
 
381
/**************************************************************/
-
 
382
void UART1_Transmit(void)
-
 
383
{
-
 
384
        u8 tmp_tx;
-
 
385
        // if something has to be send and the txd fifo is not full
-
 
386
        if((!txd_complete) && (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == RESET))
-
 
387
        {
-
 
388
                tmp_tx = txd_buffer[ptr_txd_buffer]; // read byte from txd buffer
-
 
389
                // if terminating character or end of txd buffer reached
-
 
390
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
-
 
391
                {
-
 
392
                        ptr_txd_buffer = 0;     // reset txd buffer pointer
-
 
393
                        txd_complete = TRUE;// set complete flag
-
 
394
                }
-
 
395
                UART_SendData(UART1, tmp_tx); // put character to txd fifo
-
 
396
                // set pointer to next byte
-
 
397
                ptr_txd_buffer++;
-
 
398
        }
-
 
399
}
-
 
400
 
-
 
401
/**************************************************************/
-
 
402
/* Add CRC and initiate transmission via debug uart           */
-
 
403
/**************************************************************/
-
 
404
void AddCRC(u16 datalen)
-
 
405
{
-
 
406
        u16 tmpCRC = 0, i;
-
 
407
        for(i = 0; i < datalen; i++)
-
 
408
        {
-
 
409
                tmpCRC += txd_buffer[i];
-
 
410
        }
-
 
411
        tmpCRC %= 4096;
-
 
412
        txd_buffer[i++] = '=' + tmpCRC / 64;
-
 
413
        txd_buffer[i++] = '=' + tmpCRC % 64;
-
 
414
        txd_buffer[i++] = '\r';
-
 
415
 
-
 
416
        ptr_txd_buffer = 0;
-
 
417
        txd_complete = FALSE;
-
 
418
        UART_SendData(UART1, txd_buffer[ptr_txd_buffer++]);     // send first byte, to be continued in the txd irq
-
 
419
}
-
 
420
 
-
 
421
 
-
 
422
 
-
 
423
/**************************************************************/
-
 
424
/* Code output data                                           */
-
 
425
/**************************************************************/
-
 
426
void SendOutData(u8 cmd, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
-
 
427
{
-
 
428
        va_list ap;
-
 
429
 
-
 
430
        u16 pt = 0;
-
 
431
        u8 a,b,c;
-
 
432
        u8 ptr = 0;
-
 
433
 
-
 
434
        u8* pdata = NULL;
-
 
435
        int len = 0;
-
 
436
 
-
 
437
        txd_buffer[pt++] = '#';                         // Start character
-
 
438
        txd_buffer[pt++] = 'a' + Address;       // Address (a=0; b=1,...)
-
 
439
        txd_buffer[pt++] = cmd;                         // Command
-
 
440
 
-
 
441
        va_start(ap, numofbuffers);
-
 
442
        if(numofbuffers)
-
 
443
        {
-
 
444
                pdata = va_arg(ap, u8*);
-
 
445
                len = va_arg(ap, int);
-
 
446
                ptr = 0;
-
 
447
                numofbuffers--;
-
 
448
        }
-
 
449
        while(len)
-
 
450
        {
-
 
451
                if(len)
-
 
452
                {
-
 
453
                        a = pdata[ptr++];
-
 
454
                        len--;
-
 
455
                        if((!len) && numofbuffers) // try to jump to next buffer
-
 
456
                        {
-
 
457
                                pdata = va_arg(ap, u8*);
-
 
458
                                len = va_arg(ap, int);
-
 
459
                                ptr = 0;
-
 
460
                                numofbuffers--;
-
 
461
                        }
-
 
462
                }
-
 
463
                else a = 0;
-
 
464
                if(len)
-
 
465
                {
-
 
466
                        b = pdata[ptr++];
-
 
467
                        len--;
-
 
468
                        if((!len) && numofbuffers) // try to jump to next buffer
-
 
469
                        {
-
 
470
                                pdata = va_arg(ap, u8*);
-
 
471
                                len = va_arg(ap, int);
-
 
472
                                ptr = 0;
-
 
473
                                numofbuffers--;
-
 
474
                        }
-
 
475
                }
-
 
476
                else b = 0;
-
 
477
                if(len)
-
 
478
                {
-
 
479
                        c = pdata[ptr++];
-
 
480
                        len--;
-
 
481
                        if((!len) && numofbuffers) // try to jump to next buffer
-
 
482
                        {
-
 
483
                                pdata = va_arg(ap, u8*);
-
 
484
                                len = va_arg(ap, int);
-
 
485
                                ptr = 0;
-
 
486
                                numofbuffers--;
-
 
487
                        }
-
 
488
                }
-
 
489
                else c = 0;
-
 
490
                txd_buffer[pt++] = '=' + (a >> 2);
-
 
491
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
-
 
492
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
-
 
493
                txd_buffer[pt++] = '=' + ( c & 0x3f);
-
 
494
        }
-
 
495
        va_end(ap);
-
 
496
        AddCRC(pt);     // add checksum after data block and initates the transmission
-
 
497
}
-
 
498
 
-
 
499
 
-
 
500
/**************************************************************/
-
 
501
/* Decode data                                                */
-
 
502
/**************************************************************/
-
 
503
void Decode64(void)
-
 
504
{
-
 
505
        u8 a,b,c,d;
-
 
506
        u8 x,y,z;
-
 
507
        u8 ptrIn = 3; // start with first data byte in rx buffer
-
 
508
        u8 ptrOut = 3;
-
 
509
        u8 len = ReceivedBytes - 6;      // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
-
 
510
        while(len)
-
 
511
        {
-
 
512
                a = rxd_buffer[ptrIn++] - '=';
-
 
513
                b = rxd_buffer[ptrIn++] - '=';
-
 
514
                c = rxd_buffer[ptrIn++] - '=';
-
 
515
                d = rxd_buffer[ptrIn++] - '=';
-
 
516
                //if(ptrIn > ReceivedBytes - 3) break;
-
 
517
 
-
 
518
                x = (a << 2) | (b >> 4);
-
 
519
                y = ((b & 0x0f) << 4) | (c >> 2);
-
 
520
                z = ((c & 0x03) << 6) | d;
-
 
521
 
-
 
522
                if(len--) rxd_buffer[ptrOut++] = x; else break;
-
 
523
                if(len--) rxd_buffer[ptrOut++] = y; else break;
-
 
524
                if(len--) rxd_buffer[ptrOut++] = z; else break;
-
 
525
        }
-
 
526
        pRxData = &rxd_buffer[3];
-
 
527
        RxDataLen = ptrOut - 3;
-
 
528
}
-
 
529
 
-
 
530
/**************************************************************/
326
/**************************************************************/
531
/* Process incomming data from debug uart                     */
327
/* Process incomming data from debug uart                     */
532
/**************************************************************/
328
/**************************************************************/
533
void UART1_ProcessRxData(void)
329
void UART1_ProcessRxData(void)
-
 
330
{
534
{
331
        SerialMsg_t SerialMsg;
535
        // if data in the rxd buffer are not locked immediately return
332
        // if data in the rxd buffer are not locked immediately return
536
        if((!rxd_buffer_locked) || (DebugUART != UART1) ) return;
333
        if((UART1_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return;
Line 537... Line 334...
537
        Waypoint_t * pWaypoint = NULL;
334
        Waypoint_t * pWaypoint = NULL;
538
 
335
 
539
        PcAccess = 255;
336
        PcAccess = 255;
540
        Decode64(); // decode data block in rxd buffer
337
        MKProtocol_DecodeSerialFrame(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
541
    if(rxd_buffer[2] != 'z') SerialLinkOkay = 250;               // reset SerialTimeout, but not in case of the "ping"
338
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
542
        switch(rxd_buffer[1] - 'a') // check for Slave Address
339
        switch(SerialMsg.Address) // check for Slave Address
543
        {
340
        {
544
                case NC_ADDRESS:  // own Slave Address
341
                case NC_ADDRESS:  // own Slave Address
545
                switch(rxd_buffer[2])
342
                switch(SerialMsg.CmdID)
546
                {
343
                {
547
                        case 'z': // connection checker
344
                        case 'z': // connection checker
548
                                memcpy(&Echo, (u8*)pRxData, sizeof(Echo)); // copy echo pattern
345
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
Line 549... Line 346...
549
                                Request_Echo = TRUE;
346
                                UART1_Request_Echo = TRUE;
550
                                break;
347
                                break;
551
 
348
 
Line 552... Line 349...
552
                        case 'e': // request for the text of the error status
349
                        case 'e': // request for the text of the error status
553
                                Request_ErrorMessage = TRUE;
350
                                UART1_Request_ErrorMessage = TRUE;
554
                                break;
351
                                break;
555
 
352
 
556
                        case 's'://  new target position
353
                        case 's'://  new target position
557
                                pWaypoint = (Waypoint_t*)&pRxData[0];
354
                                pWaypoint = (Waypoint_t*)SerialMsg.pData;
558
                                BeepTime = 300;
355
                                BeepTime = 300;
559
                                if(pWaypoint->Position.Status == NEWDATA)
356
                                if(pWaypoint->Position.Status == NEWDATA)
560
                                {
357
                                {
561
                                        WPList_Clear(); // empty WPList
358
                                        WPList_Clear(); // empty WPList
Line 562... Line 359...
562
                                        WPList_Append(pWaypoint);
359
                                        WPList_Append(pWaypoint);
563
                                        GPS_pWaypoint = WPList_Begin();
360
                                        GPS_pWaypoint = WPList_Begin();
564
                                }
361
                                }
565
                                break;
362
                                break;
566
 
363
 
567
                        case 'u': // redirect debug uart
364
                        case 'u': // redirect debug uart
568
                                switch(pRxData[0])
365
                                switch(SerialMsg.pData[0])
Line 585... Line 382...
585
                                                break;
382
                                                break;
586
                                }
383
                                }
587
                                break;
384
                                break;
Line 588... Line 385...
588
 
385
 
589
                        case 'w'://  Append Waypoint to List
386
                        case 'w'://  Append Waypoint to List
590
                                pWaypoint = (Waypoint_t*)&pRxData[0];
387
                                pWaypoint = (Waypoint_t*)SerialMsg.pData;
591
                                if(pWaypoint->Position.Status == INVALID)
388
                                if(pWaypoint->Position.Status == INVALID)
592
                                {  // clear WP List
389
                                {  // clear WP List
593
                                        WPList_Clear();
390
                                        WPList_Clear();
594
                                        GPS_pWaypoint = WPList_Begin();
391
                                        GPS_pWaypoint = WPList_Begin();
595
                                        //SerialPutString("\r\nClear WP List\r\n");
392
                                        //UART1_PutString("\r\nClear WP List\r\n");
596
                                }
393
                                }
597
                                else if (pWaypoint->Position.Status == NEWDATA)
394
                                else if (pWaypoint->Position.Status == NEWDATA)
598
                                {  // app current WP to the list
395
                                {  // app current WP to the list
599
                                        WPList_Append(pWaypoint);
396
                                        WPList_Append(pWaypoint);
600
                                        BeepTime = 500;
397
                                        BeepTime = 500;
601
                                        //SerialPutString("\r\nAdd WP to List\r\n");
398
                                        //UART1_PutString("\r\nAdd WP to List\r\n");
602
                                }
399
                                }
603
                                Request_NewWaypoint = TRUE;
400
                                UART1_Request_NewWaypoint = TRUE;
Line 604... Line 401...
604
                                break;
401
                                break;
605
 
402
 
606
                        case 'x'://  Read Waypoint from List
403
                        case 'x'://  Read Waypoint from List
Line 607... Line 404...
607
                                Request_ReadWaypoint = pRxData[0];
404
                                UART1_Request_ReadWaypoint = SerialMsg.pData[0];
608
                                break;
405
                                break;
609
 
406
 
610
                        default:
407
                        default:
611
                                // unsupported command recieved
408
                                // unsupported command recieved
Line 612... Line 409...
612
                                break;
409
                                break;
Line 613... Line 410...
613
                } // case NC_ADDRESS
410
                } // case NC_ADDRESS
614
                // "break;" is missing here to fall thru to the common commands
411
                // "break;" is missing here to fall thru to the common commands
615
 
412
 
616
                default:  // and any other Slave Address
413
                default:  // and any other Slave Address
617
 
414
 
618
                switch(rxd_buffer[2]) // check CmdID
415
                switch(SerialMsg.CmdID) // check CmdID
Line 619... Line 416...
619
                {
416
                {
620
                        case 'a':// request for the labels of the analog debug outputs
417
                        case 'a':// request for the labels of the analog debug outputs
621
                                Request_DebugLabel = pRxData[0];
418
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
622
                                if(Request_DebugLabel > 31) Request_DebugLabel = 31;
419
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
Line 623... Line 420...
623
                                break;
420
                                break;
624
 
421
 
625
                        case 'b': // submit extern control
422
                        case 'b': // submit extern control
626
                                memcpy(&ExternControl, (u8*)&pRxData[0], sizeof(ExternControl));
423
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
Line 627... Line 424...
627
                                ConfirmFrame = ExternControl.Frame;
424
                                ConfirmFrame = ExternControl.Frame;
628
                                break;
425
                                break;
629
 
426
 
630
                        case 'd': // request for debug data;
427
                        case 'd': // request for debug data;
Line 631... Line 428...
631
                                DebugData_Interval = (u32) pRxData[0] * 10;
428
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
632
                                if(DebugData_Interval > 0) Request_DebugData = TRUE;
429
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
633
                                break;
430
                                break;
Line 634... Line 431...
634
 
431
 
635
                        case 'c': // request for 3D data;
432
                        case 'c': // request for 3D data;
636
                                Data3D_Interval = (u32) pRxData[0] * 10;
433
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
637
                                if(Data3D_Interval > 0) Request_Data3D = TRUE;
434
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
638
                                break;
435
                                break;
Line 639... Line 436...
639
 
436
 
640
                        case 'g':// request for external control data
437
                        case 'g':// request for external control data
641
                                Request_ExternalControl = TRUE;
438
                                UART1_Request_ExternalControl = TRUE;
642
                                break;
439
                                break;
Line 643... Line 440...
643
 
440
 
644
                        case 'h':// reqest for display line
441
                        case 'h':// reqest for display line
645
                                RemoteKeys |= pRxData[0];
442
                                RemoteKeys |= SerialMsg.pData[0];
646
                                if(RemoteKeys != 0) DisplayLine = 0;
443
                                if(RemoteKeys != 0) DisplayLine = 0;
Line 647... Line 444...
647
                                Request_Display = TRUE;
444
                                UART1_Request_Display = TRUE;
648
                                break;
445
                                break;
649
 
446
 
650
                        case 'l':// reqest for display columns
447
                        case 'l':// reqest for display columns
651
                                MenuItem = pRxData[0];
448
                                MenuItem = SerialMsg.pData[0];
652
                                Request_Display1 = TRUE;
449
                                UART1_Request_Display1 = TRUE;
653
                                break;
450
                                break;
654
 
451
 
655
                        case 'o': // request for navigation information
452
                        case 'o': // request for navigation information
656
                                NaviData_Interval = (u32) pRxData[0] * 10;
453
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
657
                                if(NaviData_Interval > 0) Request_NaviData = TRUE;
-
 
658
                                break;
454
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
659
 
455
                                break;
660
                        case 'v': // request for version info
456
 
Line 661... Line 457...
661
                                Request_VerInfo = TRUE;
457
                        case 'v': // request for version info
662
                                break;
458
                                UART1_Request_VersionInfo = TRUE;
663
                        default:
459
                                break;
664
                                // unsupported command recieved
460
                        default:
665
                                break;
461
                                // unsupported command recieved
666
                }
462
                                break;
667
                break; // default:
463
                }
668
        }
464
                break; // default:
669
        // unlock the rxd buffer after processing
465
        }
670
        pRxData = NULL;
466
        // unlock the rxd buffer after processing
671
        RxDataLen = 0;
467
        UART1_rx_buffer.Position = 0;
672
        rxd_buffer_locked = FALSE;
468
        UART1_rx_buffer.Locked = FALSE;
Line 673... Line 469...
673
}
469
}
674
 
470
 
675
 
471
 
676
/*****************************************************/
472
/*****************************************************/
677
/*                   Send a character                */
473
/*                   Send a character                */
678
/*****************************************************/
474
/*****************************************************/
679
s16 uart_putchar (char c)
475
s16 UART1_Putchar(char c)
680
{
476
{
681
        if (c == '\n') uart_putchar('\r');
477
        if (c == '\n') UART1_Putchar('\r');
682
        // wait until txd fifo is not full
478
        // wait until txd fifo is not full
683
        while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);
479
        while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);
684
        // transmit byte
480
        // transmit byte
Line -... Line 481...
-
 
481
        UART_SendData(UART1, c);
-
 
482
        return (0);
-
 
483
}
-
 
484
 
-
 
485
/*****************************************************/
-
 
486
/*       Send a string to the debug uart              */
-
 
487
/*****************************************************/
-
 
488
void UART1_PutString(u8 *s)
-
 
489
{
-
 
490
        if(s == NULL) return;
-
 
491
        while (*s != '\0' && DebugUART == UART1)
-
 
492
        {
-
 
493
                UART1_Putchar(*s);
-
 
494
                s ++;
-
 
495
        }
-
 
496
}
-
 
497
 
-
 
498
/**************************************************************/
-
 
499
/*         Transmit tx buffer via debug uart                  */
-
 
500
/**************************************************************/
Line 685... Line 501...
685
        UART_SendData(UART1, c);
501
void UART1_Transmit(void)
686
        return (0);
502
{
687
}
503
        u8 tmp_tx;
688
 
504
        if(DebugUART != UART1) return;
689
/*****************************************************/
505
        // if something has to be send and the txd fifo is not full
690
/*       Send a string to the debug uart              */
506
        if((UART1_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == RESET))
-
 
507
        {
-
 
508
                tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
Line 691... Line 509...
691
/*****************************************************/
509
                UART_SendData(UART1, tmp_tx); // put character to txd fifo
692
void SerialPutString(u8 *s)
510
                // if terminating character or end of txd buffer reached
693
{
511
                if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.Size))
694
        if(s == NULL) return;
512
                {
695
        while (*s != '\0' && DebugUART == UART1)
513
                        UART1_tx_buffer.Position = 0; // reset txd buffer positon
696
        {
514
                        UART1_tx_buffer.Locked = FALSE;// unlock tx buffer
697
                uart_putchar(*s);
515
                }
698
                s ++;
516
        }
699
        }
517
}
700
}
518
 
701
 
519
 
702
 
520
/**************************************************************/
703
 
521
/* Send the answers to incomming commands at the debug uart   */
704
/**************************************************************/
522
/**************************************************************/
705
/* Send the answers to incomming commands at the debug uart   */
523
void UART1_TransmitTxData(void)
706
/**************************************************************/
524
{
707
void UART1_TransmitTxData(void)
525
        if(DebugUART != UART1) return;
708
{
526
        UART1_Transmit(); // output pending bytes in tx buffer
709
        if(!txd_complete || (DebugUART != UART1) ) return;
527
        if((UART1_tx_buffer.Locked == TRUE)) return;
710
 
528
 
711
        if(Request_Echo && txd_complete)
529
        if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
712
        {
530
        {
Line 713... Line 531...
713
                SendOutData('Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
531
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
714
                Echo = 0; // reset echo value
532
                Echo = 0; // reset echo value
715
                Request_Echo = FALSE;
533
                UART1_Request_Echo = FALSE;
716
        }
534
        }
717
        if(Request_DebugLabel != 0xFF)
535
        if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
718
        {
536
        {
Line 719... Line 537...
719
                SendOutData('A', NC_ADDRESS, 2, &Request_DebugLabel, sizeof(Request_DebugLabel), (u8 *) ANALOG_LABEL[Request_DebugLabel], 16);
537
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
720
                Request_DebugLabel = 0xFF;
538
                UART1_Request_DebugLabel = 0xFF;
721
        }
539
        }
722
        if(ConfirmFrame && txd_complete)
540
        if(ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
723
        {
541
        {
724
                SendOutData('B', NC_ADDRESS, 1, &ConfirmFrame, sizeof(ConfirmFrame));
542
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &ConfirmFrame, sizeof(ConfirmFrame));
725
                ConfirmFrame = 0;
543
                ConfirmFrame = 0;
726
        }
544
        }
727
        if( (( (DebugData_Interval > 0) && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete)
545
        if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
728
        {
546
        {
729
                SendOutData('D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
547
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
730
                DebugData_Timer = SetDelay(DebugData_Interval);
548
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
731
                Request_DebugData = FALSE;
549
                UART1_Request_DebugData = FALSE;
732
        }
550
        }
733
 
551
 
734
        if((( (Data3D_Interval > 0) && CheckDelay(Data3D_Timer) ) || Request_Data3D) && txd_complete)
552
        if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
735
        {
553
        {
736
                SendOutData('C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
554
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
737
                Data3D_Timer = SetDelay(Data3D_Interval);
555
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
738
                Request_Data3D = FALSE;
556
                UART1_Request_Data3D = FALSE;
739
        }
557
        }
740
 
558
 
741
        if(Request_ExternalControl && txd_complete)
559
        if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
742
        {
560
        {
743
                SendOutData('G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
561
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
744
                Request_ExternalControl = FALSE;
562
                UART1_Request_ExternalControl = FALSE;
745
        }
563
        }
746
        if(Request_Display && txd_complete)
-
 
747
        {
564
        if(UART1_Request_Display && (UART1_tx_buffer.Locked == FALSE))
748
                LCD_PrintMenu();
565
        {
749
                SendOutData('H', NC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (u8*)&DisplayBuff[DisplayLine * 20], 20);
566
                LCD_PrintMenu();
750
                DisplayLine++;
567
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (u8*)&DisplayBuff[DisplayLine * 20], 20);
751
                if(DisplayLine >= 4) DisplayLine = 0;
568
                DisplayLine++;
752
                Request_Display = FALSE;
569
                if(DisplayLine >= 4) DisplayLine = 0;
753
        }
570
                UART1_Request_Display = FALSE;
754
        if(Request_Display1 && txd_complete)
571
        }
755
        {
572
        if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
756
                LCD_PrintMenu();
573
        {
757
                SendOutData('L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
574
                LCD_PrintMenu();
758
                Request_Display1 = FALSE;
575
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
759
        }
576
                UART1_Request_Display1 = FALSE;
760
        if(Request_VerInfo && txd_complete)
577
        }
761
        {
578
        if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
Line 785... Line 602...
785
                FollowMe.Event_Flag = 0;
602
                FollowMe.Event_Flag = 0;
786
                FollowMe.reserve[0] = 0;                // reserve
603
                FollowMe.reserve[0] = 0;                // reserve
787
                FollowMe.reserve[1] = 0;                // reserve
604
                FollowMe.reserve[1] = 0;                // reserve
788
                FollowMe.reserve[2] = 0;                // reserve
605
                FollowMe.reserve[2] = 0;                // reserve
789
                FollowMe.reserve[3] = 0;                // reserve
606
                FollowMe.reserve[3] = 0;                // reserve
790
                SendOutData('s', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
607
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
791
                Request_SendFollowMe = FALSE;
608
                UART1_Request_SendFollowMe = FALSE;
792
        }
609
        }
793
 
-
 
794
        if(Request_NewWaypoint && txd_complete)
610
        if(UART1_Request_NewWaypoint && (UART1_tx_buffer.Locked == FALSE))
795
        {
611
        {
796
                u8 WPNumber = WPList_GetCount();
612
                u8 WPNumber = WPList_GetCount();
797
                SendOutData('W', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber));
613
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber));
798
                Request_NewWaypoint = FALSE;
614
                UART1_Request_NewWaypoint = FALSE;
799
        }
615
        }
800
 
-
 
801
        if((Request_ReadWaypoint != 0xFF) && txd_complete)
616
        if((UART1_Request_ReadWaypoint != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
802
        {
617
        {
803
                u8 WPNumber = WPList_GetCount();
618
                u8 WPNumber = WPList_GetCount();
804
                if (Request_ReadWaypoint < WPNumber)
619
                if (UART1_Request_ReadWaypoint < WPNumber)
805
                {
620
                {
806
                        SendOutData('X', NC_ADDRESS, 3, &WPNumber, 1, &Request_ReadWaypoint, 1, WPList_GetAt(Request_ReadWaypoint), sizeof(Waypoint_t));
621
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &WPNumber, 1, &UART1_Request_ReadWaypoint, 1, WPList_GetAt(UART1_Request_ReadWaypoint), sizeof(Waypoint_t));
807
                }
622
                }
808
                else
623
                else
809
                {
624
                {
810
                        SendOutData('X', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber));
625
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber));
811
                }
626
                }
812
                Request_ReadWaypoint = 0xFF;
627
                UART1_Request_ReadWaypoint = 0xFF;
813
        }
628
        }
814
 
-
 
-
 
629
        UART1_Transmit(); // output pending bytes in tx buffer
815
}
630
}