Subversion Repositories MK3Mag

Rev

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

Rev 39 Rev 41
Line 55... Line 55...
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <avr/io.h>
57
#include <avr/io.h>
58
#include <avr/interrupt.h>
58
#include <avr/interrupt.h>
59
#include <avr/wdt.h>
59
#include <avr/wdt.h>
-
 
60
#include <stdarg.h>
-
 
61
#include <string.h>
60
#include "main.h"
62
#include "main.h"
61
#include "uart.h"
63
#include "uart.h"
62
#include "timer0.h"
64
#include "timer0.h"
63
#include "twislave.h"
65
#include "twislave.h"
Line 77... Line 79...
77
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
79
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
78
volatile uint8_t rxd_buffer_locked = FALSE;
80
volatile uint8_t rxd_buffer_locked = FALSE;
79
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
81
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
80
volatile uint8_t txd_complete = TRUE;
82
volatile uint8_t txd_complete = TRUE;
81
volatile uint8_t ReceivedBytes = 0;
83
volatile uint8_t ReceivedBytes = 0;
-
 
84
volatile uint8_t *pRxData = 0;
-
 
85
volatile uint8_t RxDataLen = 0;
Line 82... Line 86...
82
 
86
 
83
// send flags
87
// send flags
84
#define RQST_VERSION_INFO               0x01
88
#define RQST_VERSION_INFO               0x01
85
#define RQST_DEBUG_DATA                 0x02
89
#define RQST_DEBUG_DATA                 0x02
Line 88... Line 92...
88
#define RQST_EXTERN_CTRL                0x10
92
#define RQST_EXTERN_CTRL                0x10
Line 89... Line 93...
89
 
93
 
90
 
94
 
-
 
95
uint8_t RequestFlags = 0x00;
Line 91... Line 96...
91
uint8_t RequestFlags = 0x00;
96
uint8_t RequestDebugLabel = 0;
92
uint8_t RequestDebugLabel = 0;
97
uint8_t ConfirmFrame = 0;
Line 93... Line 98...
93
 
98
 
Line 94... Line 99...
94
uint8_t PC_Connected = 0;
99
uint8_t PC_Connected = 0;
95
uint8_t FC_Connected = 0;
100
uint8_t FC_Connected = 0;
96
 
101
 
97
uint8_t MySlaveAddr = MK3MAG_ADDRESS;
102
uint8_t MySlaveAddr = MK3MAG_ADDRESS;
Line 98... Line 103...
98
 
103
 
-
 
104
 
Line 99... Line 105...
99
 
105
DebugOut_t              DebugOut;
100
DebugOut_t              DebugOut;
106
ExternData_t    ExternData;
101
ExternData_t    ExternData;
107
ExternControl_t ExternControl;
102
ExternControl_t ExternControl;
108
UART_VersionInfo_t      UART_VersionInfo;
Line 145... Line 151...
145
/****************************************************************/
151
/****************************************************************/
146
/*              Initialization of the USART0                    */
152
/*              Initialization of the USART0                    */
147
/****************************************************************/
153
/****************************************************************/
148
void USART0_Init (void)
154
void USART0_Init (void)
149
{
155
{
-
 
156
        uint8_t sreg = SREG;
150
        uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1);
157
        uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1);
Line 151... Line 158...
151
 
158
 
152
        // disable all interrupts before configuration
159
        // disable all interrupts before configuration
Line 200... Line 207...
200
        // enable RX-Interrupt
207
        // enable RX-Interrupt
201
        UCSR0B |= (1 << RXCIE0);
208
        UCSR0B |= (1 << RXCIE0);
202
        // enable TX-Interrupt
209
        // enable TX-Interrupt
203
        UCSR0B |= (1 << TXCIE0);
210
        UCSR0B |= (1 << TXCIE0);
Line -... Line 211...
-
 
211
 
-
 
212
        // initialize the debug timer
-
 
213
        DebugData_Timer = SetDelay(DebugData_Interval);
-
 
214
 
204
 
215
        // unlock rxd_buffer
-
 
216
        rxd_buffer_locked = FALSE;
-
 
217
        pRxData = 0;
-
 
218
        RxDataLen = 0;
-
 
219
 
205
        rxd_buffer_locked = FALSE;
220
        // no bytes to send
Line 206... Line 221...
206
        txd_complete = TRUE;
221
        txd_complete = TRUE;
207
 
222
 
-
 
223
 
208
 
224
        UART_VersionInfo.Major = VERSION_MAJOR;
Line 209... Line 225...
209
        VersionInfo.Major = VERSION_MAJOR;
225
        UART_VersionInfo.Minor = VERSION_MINOR;
Line 210... Line 226...
210
        VersionInfo.Minor = VERSION_MINOR;
226
        UART_VersionInfo.Patch = VERSION_PATCH;
211
        VersionInfo.PCCompatible = VERSION_PCCOMPATIBLE;
227
        UART_VersionInfo.Compatible = VERSION_SERIAL_COMPATIBLE;
Line 220... Line 236...
220
        USART0_putchar ('V');
236
        USART0_putchar ('V');
221
        USART0_putchar (0x30 + VERSION_MAJOR);
237
        USART0_putchar (0x30 + VERSION_MAJOR);
222
        USART0_putchar ('.');
238
        USART0_putchar ('.');
223
        USART0_putchar (0x30 + VERSION_MINOR/10);
239
        USART0_putchar (0x30 + VERSION_MINOR/10);
224
        USART0_putchar (0x30 + VERSION_MINOR%10);
240
        USART0_putchar (0x30 + VERSION_MINOR%10);
-
 
241
        USART0_putchar ('a' + VERSION_PATCH);
225
    USART0_putchar ('\n');
242
    USART0_putchar ('\n');
-
 
243
 
-
 
244
    // restore global interrupt flags
-
 
245
    SREG = sreg;
226
}
246
}
Line 227... Line 247...
227
 
247
 
228
// ---------------------------------------------------------------------------------
248
// ---------------------------------------------------------------------------------
229
void USART0_EnableTXD(void)
249
void USART0_EnableTXD(void)
Line 313... Line 333...
313
                        crc1 = '=' + crc / 64;
333
                        crc1 = '=' + crc / 64;
314
                        crc2 = '=' + crc % 64;
334
                        crc2 = '=' + crc % 64;
315
                        // compare checksum to transmitted checksum bytes
335
                        // compare checksum to transmitted checksum bytes
316
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
336
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
317
                        {   // checksum valid
337
                        {   // checksum valid
318
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
-
 
319
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
-
 
320
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
338
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
-
 
339
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
-
 
340
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
321
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
341
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
322
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
342
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
323
                        }
343
                        }
324
                        else
344
                        else
325
                        {       // checksum invalid
345
                        {       // checksum invalid
Line 353... Line 373...
353
}
373
}
Line 354... Line 374...
354
 
374
 
355
 
375
 
356
 
376
 
-
 
377
// --------------------------------------------------------------------------
357
// --------------------------------------------------------------------------
378
void SendOutData(uint8_t cmd,uint8_t module, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
358
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
379
{
359
{
380
        va_list ap;
Line -... Line 381...
-
 
381
        uint16_t pt = 0;
-
 
382
        uint8_t a,b,c;
-
 
383
        uint8_t ptr = 0;
360
        uint16_t pt = 0;
384
 
361
        uint8_t a,b,c;
385
        uint8_t *pdata = 0;
362
        uint8_t ptr = 0;
386
        int len = 0;
Line -... Line 387...
-
 
387
 
-
 
388
        txd_buffer[pt++] = '#';         // Start character
-
 
389
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
-
 
390
        txd_buffer[pt++] = cmd;         // Command
-
 
391
 
-
 
392
        va_start(ap, numofbuffers);
-
 
393
        if(numofbuffers--)
-
 
394
        {
363
 
395
                pdata = va_arg(ap, uint8_t*);
364
        txd_buffer[pt++] = '#';         // Start character
396
                len = va_arg(ap, int);
-
 
397
                ptr = 0;
-
 
398
        }
-
 
399
 
-
 
400
        while(len)
365
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
401
        {
-
 
402
                if(len)
-
 
403
                {
-
 
404
                        a = pdata[ptr++];
-
 
405
                        len--;
-
 
406
                        if((!len) && numofbuffers)
-
 
407
                        {
-
 
408
                                pdata = va_arg(ap, uint8_t*);
-
 
409
                                len = va_arg(ap, int);
-
 
410
                                ptr = 0;
-
 
411
                                numofbuffers--;
-
 
412
                        }
-
 
413
                }
366
        txd_buffer[pt++] = cmd;         // Command
414
                else a = 0;
-
 
415
                if(len)
-
 
416
                {
-
 
417
                        b = pdata[ptr++];
-
 
418
                        len--;
-
 
419
                        if((!len) && numofbuffers)
-
 
420
                        {
-
 
421
                                pdata = va_arg(ap, uint8_t*);
-
 
422
                                len = va_arg(ap, int);
-
 
423
                                ptr = 0;
-
 
424
                                numofbuffers--;
-
 
425
                        }
-
 
426
                }
367
 
427
                else b = 0;
-
 
428
                if(len)
-
 
429
                {
-
 
430
                        c = pdata[ptr++];
-
 
431
                        len--;
-
 
432
                        if((!len) && numofbuffers)
-
 
433
                        {
-
 
434
                                pdata = va_arg(ap, uint8_t*);
-
 
435
                                len = va_arg(ap, int);
368
        while(len)
436
                                ptr = 0;
369
        {
437
                                numofbuffers--;
370
                if(len) { a = snd[ptr++]; len--;} else a = 0;
438
                        }
371
                if(len) { b = snd[ptr++]; len--;} else b = 0;
439
                }
372
                if(len) { c = snd[ptr++]; len--;} else c = 0;
440
                else c = 0;
-
 
441
                txd_buffer[pt++] = '=' + (a >> 2);
373
                txd_buffer[pt++] = '=' + (a >> 2);
442
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
374
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
443
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
Line 375... Line 444...
375
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
444
                txd_buffer[pt++] = '=' + ( c & 0x3f);
376
                txd_buffer[pt++] = '=' + ( c & 0x3f);
445
        }
377
        }
446
        va_end(ap);
378
        AddCRC(pt); // add checksum after data block and initates the transmission
447
        AddCRC(pt); // add checksum after data block and initates the transmission
379
}
-
 
380
 
448
}
-
 
449
 
-
 
450
 
-
 
451
// --------------------------------------------------------------------------
-
 
452
void Decode64(void)
381
 
453
{
382
// --------------------------------------------------------------------------
454
        uint8_t a,b,c,d;
383
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
455
        uint8_t x,y,z;
384
{
456
        uint8_t ptrIn = 3;
385
        uint8_t a,b,c,d;
457
        uint8_t ptrOut = 3;
386
        uint8_t ptr = 0;
458
        uint8_t len = ReceivedBytes - 6;
387
        uint8_t x,y,z;
459
 
Line 388... Line 460...
388
        while(len)
460
        while(len)
389
        {
461
        {
390
                a = rxd_buffer[ptrIn++] - '=';
462
                a = rxd_buffer[ptrIn++] - '=';
Line 391... Line 463...
391
                b = rxd_buffer[ptrIn++] - '=';
463
                b = rxd_buffer[ptrIn++] - '=';
392
                c = rxd_buffer[ptrIn++] - '=';
464
                c = rxd_buffer[ptrIn++] - '=';
393
                d = rxd_buffer[ptrIn++] - '=';
465
                d = rxd_buffer[ptrIn++] - '=';
394
                if(ptrIn > max - 2) break;
466
                //if(ptrIn > ReceivedBytes - 3) break;
-
 
467
 
-
 
468
                x = (a << 2) | (b >> 4);
395
 
469
                y = ((b & 0x0f) << 4) | (c >> 2);
Line -... Line 470...
-
 
470
                z = ((c & 0x03) << 6) | d;
396
                x = (a << 2) | (b >> 4);
471
 
397
                y = ((b & 0x0f) << 4) | (c >> 2);
472
                if(len--) rxd_buffer[ptrOut++] = x; else break;
398
                z = ((c & 0x03) << 6) | d;
473
                if(len--) rxd_buffer[ptrOut++] = y; else break;
399
 
474
                if(len--) rxd_buffer[ptrOut++] = z; else break;
400
                if(len--) ptrOut[ptr++] = x; else break;
475
        }
Line 422... Line 497...
422
void USART0_ProcessRxData(void)
497
void USART0_ProcessRxData(void)
423
{
498
{
424
        // if data in the rxd buffer are not locked immediately return
499
        // if data in the rxd buffer are not locked immediately return
425
        if(!rxd_buffer_locked) return;
500
        if(!rxd_buffer_locked) return;
Line 426... Line 501...
426
 
501
 
Line 427... Line 502...
427
        uint8_t tmp_char_arr2[2]; // local char buffer
502
        Decode64(); // decode data block in rxd_buffer
428
 
503
 
429
        switch(rxd_buffer[2])
504
        switch(rxd_buffer[2])
430
        {
505
        {
431
           case 'w':// Attitude info from FC
506
           case 'k':// Attitude info from FC
432
                        Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes);
507
                        memcpy(&ExternData, (uint8_t*)pRxData, sizeof(ExternData));
433
                        RequestFlags |= RQST_COMPASS_HEADING;
508
                        RequestFlags |= RQST_COMPASS_HEADING;
434
                        AttitudeSource = ATTITUDE_SOURCE_UART;
509
                        AttitudeSource = ATTITUDE_SOURCE_UART;
435
                        Orientation = ExternData.Orientation;
510
                        Orientation = ExternData.Orientation;
Line 436... Line 511...
436
                        FC_Connected = 255;
511
                        FC_Connected = 255;
437
                        break;
512
                        break;
438
 
-
 
439
        // used only for debug proposes at serial port
513
 
440
                case 'b': // extern control
514
        // used only for debug proposes at serial port
441
                case 'c': // extern control with debug request
515
                case 'b': // extern control
442
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes);
516
                        memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl));
443
                        #define KEY1    0x01
517
                        #define KEY1    0x01
444
                        #define KEY2    0x02
518
                        #define KEY2    0x02
Line 449... Line 523...
449
                        if(ExternControl.RemoteButtons & KEY2)
523
                        if(ExternControl.RemoteButtons & KEY2)
450
                        {
524
                        {
451
                                ExternData.CalState++;
525
                                ExternData.CalState++;
452
                                if(ExternData.CalState == 6) ExternData.CalState = 0;
526
                                if(ExternData.CalState == 6) ExternData.CalState = 0;
453
                        }
527
                        }
-
 
528
                        ConfirmFrame = ExternControl.Frame;
454
                        PC_Connected = 255;
529
                        PC_Connected = 255;
455
                        break;
530
                        break;
Line 456... Line 531...
456
 
531
 
457
                // get debug values
-
 
458
                case 'f':
532
                case 'd': // request for the debug data
459
                        RequestFlags |= RQST_DEBUG_DATA;
-
 
460
                        PC_Connected = 255;
-
 
461
                        break;
-
 
462
 
533
                        DebugData_Interval = (uint16_t) pRxData[0] * 10;
463
                // no reaction for display line request
-
 
464
                case 'h':// x-1 display columns
-
 
465
                        PC_Connected = 255;
534
                        if(DebugData_Interval) RequestFlags |= RQST_DEBUG_DATA;
Line 466... Line 535...
466
                        break;
535
                        break;
467
 
536
 
468
                case 'v': // get version and board release
537
                case 'v': // request version and board release
469
                        RequestFlags |= RQST_VERSION_INFO;
538
                        RequestFlags |= RQST_VERSION_INFO;
Line 470... Line 539...
470
                        PC_Connected = 255;
539
                        PC_Connected = 255;
471
                        break;
-
 
472
 
540
                        break;
473
                case 'a':// Labels of the Analog Debug outputs
541
 
474
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
542
                case 'a':// Labels of the Analog Debug outputs
475
                        RequestDebugLabel = tmp_char_arr2[0];
543
                        RequestDebugLabel = pRxData[0];
Line 476... Line 544...
476
                        RequestFlags |= RQST_DEBUG_LABEL;
544
                        RequestFlags |= RQST_DEBUG_LABEL;
477
                        PC_Connected = 255;
545
                        PC_Connected = 255;
478
                        break;
546
                        break;
479
 
547
 
480
                case 'g':// get extern control data
548
                case 'g':// get extern control data
481
                        RequestFlags |= RQST_EXTERN_CTRL;
549
                        RequestFlags |= RQST_EXTERN_CTRL;
-
 
550
                        PC_Connected = 255;
-
 
551
                        break;
482
                        PC_Connected = 255;
552
        }
483
                        break;
553
        // unlock the rxd buffer after processing
Line 493... Line 563...
493
{
563
{
494
        if(!(UCSR0B & (1 << TXEN0))) return;
564
        if(!(UCSR0B & (1 << TXEN0))) return;
Line 495... Line 565...
495
 
565
 
Line 496... Line 566...
496
        if(!txd_complete) return;
566
        if(!txd_complete) return;
497
 
567
 
498
        if(CheckDelay(Debug_Timer) || (RequestFlags & RQST_DEBUG_DATA))
-
 
499
        {
568
        if((RequestFlags & RQST_DEBUG_LABEL) && txd_complete)
500
                SetDebugValues();
569
        {
501
                SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
570
                SendOutData('A', MySlaveAddr, 2, (uint8_t *)&RequestDebugLabel, sizeof(RequestDebugLabel), (uint8_t *) ANALOG_LABEL[RequestDebugLabel], 16);
502
                Debug_Timer = SetDelay(250);
571
                RequestDebugLabel = 0xFF;
Line 503... Line 572...
503
                RequestFlags &= ~RQST_DEBUG_DATA;
572
                RequestFlags &= ~RQST_DEBUG_LABEL;
504
        }
573
        }
505
 
574
 
506
        if(RequestFlags & RQST_DEBUG_LABEL)
575
        if(ConfirmFrame && txd_complete)
507
        {
-
 
508
                SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16);
-
 
509
                RequestDebugLabel = 255;
576
        {
Line 510... Line 577...
510
                RequestFlags &= ~RQST_DEBUG_LABEL;
577
                SendOutData('B',MySlaveAddr, 1, (uint8_t *) &ConfirmFrame, sizeof(ConfirmFrame));
511
                Debug_Timer = SetDelay(500);
578
                ConfirmFrame = 0;
-
 
579
        }
512
        }
580
 
-
 
581
        if(( (DebugData_Interval && CheckDelay(DebugData_Timer)) || (RequestFlags & RQST_DEBUG_DATA)) && txd_complete)
513
 
582
        {
514
        if(RequestFlags & RQST_VERSION_INFO)
583
                SetDebugValues();
Line -... Line 584...
-
 
584
                SendOutData('D', MySlaveAddr, 1, (uint8_t *) &DebugOut, sizeof(DebugOut));
515
        {
585
                DebugData_Timer = SetDelay(DebugData_Interval);
516
                SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo));
586
                RequestFlags &= ~RQST_DEBUG_DATA;
517
                RequestFlags &= ~RQST_VERSION_INFO;
-
 
518
        }
587
        }
519
 
588
 
520
        if(RequestFlags & RQST_EXTERN_CTRL)
589
 
Line 521... Line 590...
521
        {
590
        if((RequestFlags & RQST_EXTERN_CTRL) && txd_complete)
522
                SetDebugValues();
591
        {
523
                SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
592
                SendOutData('G',MySlaveAddr, 1, (uint8_t *) &ExternControl,sizeof(ExternControl));
524
                RequestFlags &= ~RQST_EXTERN_CTRL;
593
                RequestFlags &= ~RQST_EXTERN_CTRL;
525
        }
594
        }
-
 
595
 
-
 
596
        if((RequestFlags & RQST_COMPASS_HEADING) && txd_complete)
-
 
597
        {
-
 
598
                SendOutData('K',MySlaveAddr, 1, (uint8_t *) &I2C_Heading, sizeof(I2C_Heading));
-
 
599
                RequestFlags &= ~RQST_COMPASS_HEADING;
-
 
600
        }
526
 
601
 
Line 527... Line 602...
527
        if(RequestFlags & RQST_COMPASS_HEADING)
602
        if((RequestFlags & RQST_VERSION_INFO) && txd_complete)
528
        {
603
        {