Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
231 killagreg 1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + only for non-profit use
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
 
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
#include <avr/wdt.h>
11
#include <stdarg.h>
12
#include <string.h>
13
 
14
#include "main.h"
15
#include "menu.h"
16
#include "timer0.h"
17
#include "uart0.h"
18
#include "ubx.h"
19
#include "led.h"
20
 
21
 
22
#define FC_ADDRESS 1
23
#define NC_ADDRESS 2
24
#define MK3MAG_ADDRESS 3
25
 
26
#define FM_ADDRESS 10 // FOLLOW ME
27
 
28
#define FALSE   0
29
#define TRUE    1
30
 
31
//int8_t test __attribute__ ((section (".noinit")));
32
uint8_t Request_VerInfo                 = FALSE;
33
uint8_t Request_Display                 = FALSE;
34
uint8_t Request_Display1                = FALSE;
35
uint8_t Request_ExternalControl = FALSE;
36
uint8_t Request_DebugData               = FALSE;
37
uint8_t Request_DebugLabel              = 255;
38
uint8_t DisplayLine = 0;
39
 
40
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
41
volatile uint8_t rxd_buffer_locked = FALSE;
42
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
43
volatile uint8_t txd_complete = TRUE;
44
volatile uint8_t ReceivedBytes = 0;
45
volatile uint8_t *pRxData = 0;
46
volatile uint8_t RxDataLen = 0;
47
 
48
uint8_t PcAccess = 100;
49
 
50
ExternControl_t ExternControl;
51
DebugOut_t              DebugOut;
52
UART_VersionInfo_t      UART_VersionInfo;
53
 
54
uint16_t DebugData_Timer;
55
uint16_t DebugData_Interval = 500; // in 1ms
56
 
57
 
58
const uint8_t ANALOG_LABEL[32][16] =
59
{
60
   //1234567890123456
273 killagreg 61
    "Analog Ch0      ", //0
62
    "Analog Ch1      ",
63
    "Analog Ch2      ",
64
    "Analog Ch3      ",
65
    "Analog Ch4      ",
66
    "Analog Ch5      ", //5
67
    "Analog Ch6      ",
68
    "Analog Ch7      ",
69
    "UBat            ",
303 joko 70
    "Speed_North     ",
71
    "Speed_East      ", //10
72
    "Speed_Top       ",
73
    "NumOfSats       ",
74
    "Pos.Longitude   ",
75
    "Pos.Latitude    ",
76
    "Pos.Altitude    ", //15
77
        "Zellenzahl      ",
78
    "PowerOn         ",
231 killagreg 79
    "Debug18         ",
80
    "Debug19         ",
81
    "Debug20         ", //20
82
    "Debug21         ",
83
    "Debug22         ",
84
    "Debug23         ",
85
    "Debug24         ",
86
    "Debug25         ", //25
87
    "Debug26         ",
88
    "Debug27         ",
89
    "Debug28         ",
90
    "Debug29         ",
91
    "Debug30         ", //30
92
    "Debug31         "
93
};
94
 
95
 
96
 
97
/****************************************************************/
98
/*              Initialization of the USART0                    */
99
/****************************************************************/
100
void USART0_Init (void)
101
{
102
        uint8_t sreg = SREG;
103
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
104
 
105
        // disable all interrupts before configuration
106
        cli();
107
 
108
        // disable RX-Interrupt
109
        UCSR0B &= ~(1 << RXCIE0);
110
        // disable TX-Interrupt
111
        UCSR0B &= ~(1 << TXCIE0);
112
 
113
        // set direction of RXD0 and TXD0 pins
114
        // set RXD0 (PD0) as an input pin
115
        PORTD |= (1 << PORTD0);
116
        DDRD &= ~(1 << DDD0);
117
        // set TXD0 (PD1) as an output pin
118
        PORTD |= (1 << PORTD1);
119
        DDRD |=  (1 << DDD1);
120
 
121
        // USART0 Baud Rate Register
122
        // set clock divider
123
        UBRR0H = (uint8_t)(ubrr >> 8);
124
        UBRR0L = (uint8_t)ubrr;
125
 
126
        // USART0 Control and Status Register A, B, C
127
 
128
        // enable double speed operation in
129
        UCSR0A |= (1 << U2X0);
130
        // enable receiver and transmitter in
131
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
132
        // set asynchronous mode
133
        UCSR0C &= ~(1 << UMSEL01);
134
        UCSR0C &= ~(1 << UMSEL00);
135
        // no parity
136
        UCSR0C &= ~(1 << UPM01);
137
        UCSR0C &= ~(1 << UPM00);
138
        // 1 stop bit
139
        UCSR0C &= ~(1 << USBS0);
140
        // 8-bit
141
        UCSR0B &= ~(1 << UCSZ02);
142
        UCSR0C |=  (1 << UCSZ01);
143
        UCSR0C |=  (1 << UCSZ00);
144
 
145
                // flush receive buffer
146
        while ( UCSR0A & (1<<RXC0) ) UDR0;
147
 
148
        // enable interrupts at the end
149
        // enable RX-Interrupt
150
        UCSR0B |= (1 << RXCIE0);
151
        // enable TX-Interrupt
152
        UCSR0B |= (1 << TXCIE0);
153
 
154
        // initialize the debug timer
155
        DebugData_Timer = SetDelay(DebugData_Interval);
156
 
157
        // unlock rxd_buffer
158
        rxd_buffer_locked = FALSE;
159
        pRxData = 0;
160
        RxDataLen = 0;
161
 
162
        // no bytes to send
163
        txd_complete = TRUE;
164
 
165
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
166
        UART_VersionInfo.SWMinor = VERSION_MINOR;
167
        UART_VersionInfo.SWPatch = VERSION_PATCH;
168
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
169
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
170
 
171
        // restore global interrupt flags
172
    SREG = sreg;
173
}
174
 
175
/****************************************************************/
176
/*               USART0 transmitter ISR                         */
177
/****************************************************************/
178
ISR(USART0_TX_vect)
179
{
180
        static uint16_t ptr_txd_buffer = 0;
181
        uint8_t tmp_tx;
182
        if(!txd_complete) // transmission not completed
183
        {
184
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
185
                tmp_tx = txd_buffer[ptr_txd_buffer];
186
                // if terminating character or end of txd buffer was reached
187
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
188
                {
189
                        ptr_txd_buffer = 0; // reset txd pointer
190
                        txd_complete = 1; // stop transmission
191
                }
192
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
193
        }
194
        // transmission completed
195
        else ptr_txd_buffer = 0;
196
}
197
 
198
/****************************************************************/
199
/*               USART0 receiver ISR                            */
200
/****************************************************************/
201
ISR(USART0_RX_vect)
202
{
203
        static uint16_t crc;
204
        static uint8_t ptr_rxd_buffer = 0;
205
        uint8_t crc1, crc2;
206
        uint8_t c;
207
 
208
        c = UDR0;  // catch the received byte
209
 
210
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
211
 
212
        // the rxd buffer is unlocked
213
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
214
        {
215
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
216
                crc = c; // init crc
217
        }
218
        #if 0
219
        else if (ptr_rxd_buffer == 1) // handle address
220
        {
221
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
222
                crc += c; // update crc
223
        }
224
        #endif
225
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
226
        {
227
                if(c != '\r') // no termination character
228
                {
229
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
230
                        crc += c; // update crc
231
                }
232
                else // termination character was received
233
                {
234
                        // the last 2 bytes are no subject for checksum calculation
235
                        // they are the checksum itself
236
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
237
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
238
                        // calculate checksum from transmitted data
239
                        crc %= 4096;
240
                        crc1 = '=' + crc / 64;
241
                        crc2 = '=' + crc % 64;
242
                        // compare checksum to transmitted checksum bytes
243
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
244
                        {   // checksum valid
245
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
246
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
247
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
248
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
249
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
250
                        }
251
                        else
252
                        {       // checksum invalid
253
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
254
                        }
255
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
256
                }
257
        }
258
        else // rxd buffer overrun
259
        {
260
                ptr_rxd_buffer = 0; // reset rxd buffer
261
                rxd_buffer_locked = FALSE; // unlock rxd buffer
262
        }
263
 
264
}
265
 
266
 
267
// --------------------------------------------------------------------------
268
void AddCRC(uint16_t datalen)
269
{
270
        uint16_t tmpCRC = 0, i;
271
        for(i = 0; i < datalen; i++)
272
        {
273
                tmpCRC += txd_buffer[i];
274
        }
275
        tmpCRC %= 4096;
276
        txd_buffer[i++] = '=' + tmpCRC / 64;
277
        txd_buffer[i++] = '=' + tmpCRC % 64;
278
        txd_buffer[i++] = '\r';
279
        txd_complete = FALSE;
280
        UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
281
}
282
 
283
 
284
 
285
// --------------------------------------------------------------------------
286
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
287
{
288
        va_list ap;
289
        uint16_t pt = 0;
290
        uint8_t a,b,c;
291
        uint8_t ptr = 0;
292
 
293
        uint8_t *pdata = 0;
294
        int len = 0;
295
 
296
        txd_buffer[pt++] = '#';                 // Start character
297
        txd_buffer[pt++] = 'a' + addr;  // Address (a=0; b=1,...)
298
        txd_buffer[pt++] = cmd;                 // Command
299
 
300
        va_start(ap, numofbuffers);
301
        if(numofbuffers)
302
        {
303
                pdata = va_arg(ap, uint8_t*);
304
                len = va_arg(ap, int);
305
                ptr = 0;
306
                numofbuffers--;
307
        }
308
 
309
        while(len)
310
        {
311
                if(len)
312
                {
313
                        a = pdata[ptr++];
314
                        len--;
315
                        if((!len) && numofbuffers)
316
                        {
317
                                pdata = va_arg(ap, uint8_t*);
318
                                len = va_arg(ap, int);
319
                                ptr = 0;
320
                                numofbuffers--;
321
                        }
322
                }
323
                else a = 0;
324
                if(len)
325
                {
326
                        b = pdata[ptr++];
327
                        len--;
328
                        if((!len) && numofbuffers)
329
                        {
330
                                pdata = va_arg(ap, uint8_t*);
331
                                len = va_arg(ap, int);
332
                                ptr = 0;
333
                                numofbuffers--;
334
                        }
335
                }
336
                else b = 0;
337
                if(len)
338
                {
339
                        c = pdata[ptr++];
340
                        len--;
341
                        if((!len) && numofbuffers)
342
                        {
343
                                pdata = va_arg(ap, uint8_t*);
344
                                len = va_arg(ap, int);
345
                                ptr = 0;
346
                                numofbuffers--;
347
                        }
348
                }
349
                else c = 0;
350
                txd_buffer[pt++] = '=' + (a >> 2);
351
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
352
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
353
                txd_buffer[pt++] = '=' + ( c & 0x3f);
354
        }
355
        va_end(ap);
356
        AddCRC(pt); // add checksum after data block and initates the transmission
357
}
358
 
359
 
360
// --------------------------------------------------------------------------
361
void Decode64(void)
362
{
363
        uint8_t a,b,c,d;
364
        uint8_t x,y,z;
365
        uint8_t ptrIn = 3;
366
        uint8_t ptrOut = 3;
367
        uint8_t len = ReceivedBytes - 6;
368
 
369
        while(len)
370
        {
371
                a = rxd_buffer[ptrIn++] - '=';
372
                b = rxd_buffer[ptrIn++] - '=';
373
                c = rxd_buffer[ptrIn++] - '=';
374
                d = rxd_buffer[ptrIn++] - '=';
375
                //if(ptrIn > ReceivedBytes - 3) break;
376
 
377
                x = (a << 2) | (b >> 4);
378
                y = ((b & 0x0f) << 4) | (c >> 2);
379
                z = ((c & 0x03) << 6) | d;
380
 
381
                if(len--) rxd_buffer[ptrOut++] = x; else break;
382
                if(len--) rxd_buffer[ptrOut++] = y; else break;
383
                if(len--) rxd_buffer[ptrOut++] = z; else break;
384
        }
385
        pRxData = &rxd_buffer[3];
386
        RxDataLen = ptrOut - 3;
387
}
388
 
389
 
390
// --------------------------------------------------------------------------
391
void USART0_ProcessRxData(void)
392
{
393
        // if data in the rxd buffer are not locked immediately return
394
        if(!rxd_buffer_locked) return;
395
 
396
        Decode64(); // decode data block in rxd_buffer
397
 
398
 
399
        switch(rxd_buffer[1] - 'a')
400
        {
401
                case FM_ADDRESS:
402
 
403
                        switch(rxd_buffer[2])
404
                        {
405
                                default:
406
                                        //unsupported command received
407
                                        break;
408
                        } // case FC_ADDRESS:
409
 
410
                default: // any Slave Address
411
 
412
                switch(rxd_buffer[2])
413
                        {
414
                                case 'a':// request for labels of the analog debug outputs
415
                                        Request_DebugLabel = pRxData[0];
416
                                        if(Request_DebugLabel > 31) Request_DebugLabel = 31;
417
                                        PcAccess = 255;
418
                                        break;
419
 
420
                                case 'h':// request for display columns
421
                                        PcAccess = 255;
422
                                        RemoteKeys |= pRxData[0];
423
                                        if(RemoteKeys) DisplayLine = 0;
424
                                        Request_Display = TRUE;
425
                                        break;
426
 
427
                                case 'l':// request for display columns
428
                                        PcAccess = 255;
429
                                        MenuItem = pRxData[0];
430
                                        Request_Display1 = TRUE;
431
                                        break;
432
 
433
                                case 'v': // request for version and board release
434
                                        Request_VerInfo = TRUE;
435
                                        break;
436
 
437
                                case 'd': // request for the debug data
438
                                        DebugData_Interval = (uint16_t) pRxData[0] * 10;
439
                                        if(DebugData_Interval > 0) Request_DebugData = TRUE;
440
                                        break;
441
 
442
                                case 'g':// get external control data
443
                                        Request_ExternalControl = TRUE;
444
                                        break;
445
 
446
                                default:
447
                                        //unsupported command received
448
                                        break;
449
                }
450
                break; // default:
451
        }
452
        // unlock the rxd buffer after processing
453
        pRxData = 0;
454
        RxDataLen = 0;
455
        rxd_buffer_locked = FALSE;
456
}
457
 
458
//############################################################################
459
//Routine für die Serielle Ausgabe
460
int16_t uart_putchar (int8_t c)
461
//############################################################################
462
{
463
        if (c == '\n')
464
                uart_putchar('\r');
465
        // wait until previous character was send
466
        loop_until_bit_is_set(UCSR0A, UDRE0);
467
        // send character
468
        UDR0 = c;
469
        return (0);
470
}
471
 
472
 
473
//---------------------------------------------------------------------------------------------
474
void USART0_TransmitTxData(void)
475
{
476
        if(!txd_complete) return;
477
 
478
        if(Request_VerInfo && txd_complete)
479
        {
480
                SendOutData('V', FM_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
481
                Request_VerInfo = FALSE;
482
        }
483
        if(Request_Display && txd_complete)
484
        {
485
                LCD_PrintMenu();
486
                SendOutData('H', FM_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20);
487
                DisplayLine++;
488
                if(DisplayLine >= 4) DisplayLine = 0;
489
                Request_Display = FALSE;
490
        }
491
        if(Request_Display1 && txd_complete)
492
        {
493
                LCD_PrintMenu();
494
                SendOutData('L', FM_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff));
495
                Request_Display1 = FALSE;
496
        }
497
        if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten
498
        {
499
                SendOutData('A', FM_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), ANALOG_LABEL[Request_DebugLabel], 16);
500
                Request_DebugLabel = 0xFF;
501
        }
502
        if(Request_ExternalControl && txd_complete)
503
        {
504
                SendOutData('G', FM_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl));
505
                Request_ExternalControl = FALSE;
506
        }
507
        if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete)
508
        {
509
                SendOutData('D', FM_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut));
510
                DebugData_Timer = SetDelay(DebugData_Interval);
511
                Request_DebugData = FALSE;
512
    }
513
}
514