Subversion Repositories NaviCtrl

Rev

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

Rev 151 Rev 153
Line 71... Line 71...
71
#include "timer2.h"
71
#include "timer2.h"
72
#include "analog.h"
72
#include "analog.h"
73
#include "main.h"
73
#include "main.h"
74
#include "waypoints.h"
74
#include "waypoints.h"
75
#include "mkprotocol.h"
75
#include "mkprotocol.h"
-
 
76
#include "fifo.h"
Line 76... Line 77...
76
 
77
 
77
#define FALSE   0
78
#define FALSE   0
Line 94... Line 95...
94
u8 UART1_DisplayLine                    = 0;
95
u8 UART1_DisplayLine                    = 0;
95
u8 UART1_ConfirmFrame                   = 0;
96
u8 UART1_ConfirmFrame                   = 0;
Line 96... Line 97...
96
 
97
 
Line 97... Line 98...
97
UART_TypeDef *DebugUART = UART1;
98
UART_TypeDef *DebugUART = UART1;
98
 
99
 
99
// the tx buffer
100
// the primary rx fifo
100
#define UART1_TX_BUFFER_LEN  150
101
#define UART1_RX_FIFO_LEN 512
Line 101... Line 102...
101
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
102
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
102
Buffer_t UART1_tx_buffer;
103
fifo_t UART1_rx_fifo;
103
 
104
 
104
// the rx buffer
105
// the rx buffer
Line -... Line 106...
-
 
106
#define UART1_RX_BUFFER_LEN  150
-
 
107
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
-
 
108
Buffer_t UART1_rx_buffer;
-
 
109
 
-
 
110
// the tx buffer
-
 
111
#define UART1_TX_BUFFER_LEN  150
-
 
112
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
105
#define UART1_RX_BUFFER_LEN  150
113
Buffer_t UART1_tx_buffer;
Line 106... Line 114...
106
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
114
 
Line 107... Line 115...
107
Buffer_t UART1_rx_buffer;
115
 
Line 168... Line 176...
168
void UART1_Init (void)
176
void UART1_Init (void)
169
{
177
{
170
        GPIO_InitTypeDef GPIO_InitStructure;
178
        GPIO_InitTypeDef GPIO_InitStructure;
171
        UART_InitTypeDef UART_InitStructure;
179
        UART_InitTypeDef UART_InitStructure;
Line -... Line 180...
-
 
180
 
-
 
181
        // initialize txd buffer
-
 
182
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
-
 
183
       
-
 
184
        // initialize rxd buffer
-
 
185
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
-
 
186
 
-
 
187
        // initialize the rx fifo
-
 
188
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN);
172
 
189
 
173
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
190
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
Line 174... Line 191...
174
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
191
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
175
 
192
 
Line 209... Line 226...
209
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
226
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
210
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
227
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
211
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
228
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
Line 212... Line 229...
212
 
229
 
213
        UART_DeInit(UART1); // reset uart 1     to default
230
        UART_DeInit(UART1); // reset uart 1     to default
214
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
231
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1     
215
        // enable uart 1 interrupts selective
232
        // enable uart 1 interrupts selective
216
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
233
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
217
        UART_Cmd(UART1, ENABLE); // enable uart 1
234
        UART_Cmd(UART1, ENABLE); // enable uart 1
218
        // configure the uart 1 interupt line
235
        // configure the uart 1 interupt line
219
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
236
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
220
        // enable the uart 1 IRQ
237
                // enable the uart 1 IRQ
-
 
238
        VIC_ITCmd(UART1_ITLine, ENABLE);
221
        VIC_ITCmd(UART1_ITLine, ENABLE);
239
 
222
        // initialize the debug timer
240
        // initialize the debug timer
223
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
241
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
224
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
-
 
225
       
-
 
226
        // initialize txd buffer
-
 
227
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
-
 
228
       
-
 
229
        // initialize rxd buffer
-
 
Line 230... Line 242...
230
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
242
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
231
 
243
 
232
        // Fill Version Info Structure
244
        // Fill Version Info Structure
233
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
245
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
Line 250... Line 262...
250
        static u8 abortState = 0;
262
        static u8 abortState = 0;
251
        u8 c;
263
        u8 c;
Line 252... Line 264...
252
 
264
 
253
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
265
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
254
        {
266
        {
255
                // clear the pending bits
267
                // clear the pending bits!
256
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
268
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
257
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
269
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
258
                // if debug UART is not UART1
270
                // if debug UART is not UART1
259
                if (DebugUART != UART1)
271
                if (DebugUART != UART1)
Line 286... Line 298...
286
                                                {
298
                                                {
287
                                                        if(DebugUART == UART0)
299
                                                        if(DebugUART == UART0)
288
                                                        {
300
                                                        {
289
                                                                UART0_Connect_to_MKGPS();
301
                                                                UART0_Connect_to_MKGPS();
290
                                                                TIMER2_Init();                          // enbable servo outputs
302
                                                                TIMER2_Init();                          // enbable servo outputs
-
 
303
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
291
                                                        }
304
                                                        }
292
                                                        DebugUART = UART1;
305
                                                        DebugUART = UART1;
293
                                                }                      
306
                                                }                      
294
                                                abortState = 0;
307
                                                abortState = 0;
295
                                                break;
308
                                                break;
Line 306... Line 319...
306
                }
319
                }
307
                else  // DebugUART == UART1 (normal operation)
320
                else  // DebugUART == UART1 (normal operation)
308
                {
321
                {
309
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
322
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
310
                        { // some byes in the fifo and rxd buffer not locked
323
                        { // some byes in the fifo and rxd buffer not locked
311
                            // get byte from fifo
324
                            // get byte from hardware fifo
312
                        c = UART_ReceiveData(UART1);
325
                        c = UART_ReceiveData(UART1);
-
 
326
                                // put into the software fifo
313
                                MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c);
327
                                if(!fifo_put(&UART1_rx_fifo, c))
-
 
328
                                {       // fifo overflow
-
 
329
                                        fifo_purge(&UART1_rx_fifo); // flush the whole buffer
-
 
330
                                }
314
                        } // some byes in the fifo and rxd buffer not locked
331
                        } // some byes in the fifo and rxd buffer not locked
315
                } // eof DebugUart = UART1
332
                } // eof DebugUart = UART1
316
        }
333
        }
317
}
334
}
Line 318... Line 335...
318
 
335
 
319
/**************************************************************/
336
/**************************************************************/
320
/* Process incomming data from debug uart                     */
337
/* Process incomming data from debug uart                     */
321
/**************************************************************/
338
/**************************************************************/
322
void UART1_ProcessRxData(void)
339
void UART1_ProcessRxData(void)
-
 
340
{
-
 
341
        u8 c;
323
{
342
 
-
 
343
        // return on forwarding uart
-
 
344
        if(DebugUART != UART1) return;
-
 
345
       
-
 
346
        // collect data from primary rx fifo
-
 
347
        while(fifo_get(&UART1_rx_fifo, &c))
-
 
348
        {       // break if complete frame is collected
-
 
349
                if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
324
        SerialMsg_t SerialMsg;
350
        }
325
        // if data in the rxd buffer are not locked immediately return
351
        // if data in the rxd buffer are not locked immediately return
-
 
352
        if(UART1_rx_buffer.Locked == FALSE) return;
326
        if((UART1_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return;
353
       
-
 
354
        Waypoint_t * pWaypoint = NULL;
Line 327... Line 355...
327
        Waypoint_t * pWaypoint = NULL;
355
        SerialMsg_t SerialMsg;
328
 
356
 
329
        MKProtocol_DecodeSerialFrame(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
357
        MKProtocol_DecodeSerialFrame(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
330
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
358
    if(*(SerialMsg.pCmdID) != 'z') SerialLinkOkay = 250;          // reset SerialTimeout, but not in case of the "ping"
331
        switch(SerialMsg.Address) // check for Slave Address
359
        switch(*(SerialMsg.pAddress)) // check for Slave Address
332
        {
360
        {
333
                case NC_ADDRESS:  // own Slave Address
361
                case NC_ADDRESS:  // own Slave Address
334
                switch(SerialMsg.CmdID)
362
                switch(*(SerialMsg.pCmdID))
335
                {
363
                {
336
                        case 'z': // connection checker
364
                        case 'z': // connection checker
337
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
365
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
Line 356... Line 384...
356
                        case 'u': // redirect debug uart
384
                        case 'u': // redirect debug uart
357
                                switch(SerialMsg.pData[0])
385
                                switch(SerialMsg.pData[0])
358
                                {
386
                                {
359
                                        case UART_FLIGHTCTRL:
387
                                        case UART_FLIGHTCTRL:
360
                                                UART2_Init();                           // initialize UART2 to FC pins
388
                                                UART2_Init();                           // initialize UART2 to FC pins
361
                                                //TIMER2_Deinit();                      // stop servo output
389
                                                fifo_purge(&UART1_rx_fifo);
362
                                                DebugUART = UART2;
390
                                                DebugUART = UART2;
363
                                                break;
391
                                                break;
364
                                        case UART_MK3MAG:
392
                                        case UART_MK3MAG:
365
                                                if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
393
                                                if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
366
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
394
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
367
                                                //TIMER2_Deinit();                      // stop servo output
-
 
368
                                                GPSData.Status = INVALID;
395
                                                GPSData.Status = INVALID;
-
 
396
                                                fifo_purge(&UART1_rx_fifo);
369
                                                DebugUART = UART0;
397
                                                DebugUART = UART0;
370
                                                break;
398
                                                break;
371
                                        case UART_MKGPS:
399
                                        case UART_MKGPS:
372
                                                if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
400
                                                if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
373
                                                TIMER2_Deinit();
401
                                                TIMER2_Deinit();
374
                                                UART0_Connect_to_MKGPS();       // connect UART0 to MKGPS pins
402
                                                UART0_Connect_to_MKGPS();       // connect UART0 to MKGPS pins
375
                                                GPSData.Status = INVALID;
403
                                                GPSData.Status = INVALID;
-
 
404
                                                fifo_purge(&UART1_rx_fifo);
376
                                                DebugUART = UART0;
405
                                                DebugUART = UART0;
377
                                                break;
406
                                                break;
-
 
407
                                        default:
-
 
408
                                                break;
378
                                }
409
                                }
379
                                break;
410
                                break;
Line 380... Line 411...
380
 
411
 
381
                        case 'w'://  Append Waypoint to List
412
                        case 'w'://  Append Waypoint to List
Line 405... Line 436...
405
                } // case NC_ADDRESS
436
                } // case NC_ADDRESS
406
                // "break;" is missing here to fall thru to the common commands
437
                // "break;" is missing here to fall thru to the common commands
Line 407... Line 438...
407
 
438
 
Line 408... Line 439...
408
                default:  // and any other Slave Address
439
                default:  // and any other Slave Address
409
 
440
 
410
                switch(SerialMsg.CmdID) // check CmdID
441
                switch(*(SerialMsg.pCmdID)) // check CmdID
411
                {
442
                {
412
                        case 'a':// request for the labels of the analog debug outputs
443
                        case 'a':// request for the labels of the analog debug outputs
413
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
444
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
Line 456... Line 487...
456
                                // unsupported command recieved
487
                                // unsupported command recieved
457
                                break;
488
                                break;
458
                }
489
                }
459
                break; // default:
490
                break; // default:
460
        }
491
        }
461
        Buffer_Clear(&UART1_rx_buffer);
492
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
462
}
493
}
Line 463... Line 494...
463
 
494
 
464
 
495
 
Line 527... Line 558...
527
        {
558
        {
528
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
559
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
529
                Echo = 0; // reset echo value
560
                Echo = 0; // reset echo value
530
                UART1_Request_Echo = FALSE;
561
                UART1_Request_Echo = FALSE;
531
        }
562
        }
532
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
-
 
533
        {
-
 
534
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
-
 
535
                UART1_Request_DebugLabel = 0xFF;
-
 
536
        }
-
 
537
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
-
 
538
        {
-
 
539
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
-
 
540
                UART1_ConfirmFrame = 0;
-
 
541
        }
-
 
542
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
563
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
543
        {
564
        {
544
                NaviData.Errorcode = ErrorCode;
565
                NaviData.Errorcode = ErrorCode;
545
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
566
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
546
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
567
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
Line 556... Line 577...
556
        {
577
        {
557
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
578
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
558
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
579
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
559
                UART1_Request_Data3D = FALSE;
580
                UART1_Request_Data3D = FALSE;
560
        }
581
        }
-
 
582
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
-
 
583
        {
-
 
584
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
-
 
585
                UART1_Request_DebugLabel = 0xFF;
561
 
586
        }
-
 
587
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
-
 
588
        {
-
 
589
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
-
 
590
                UART1_ConfirmFrame = 0;
-
 
591
        }
562
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
592
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
563
        {
593
        {
564
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
594
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
565
                UART1_Request_ExternalControl = FALSE;
595
                UART1_Request_ExternalControl = FALSE;
566
        }
596
        }