Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
745 killagreg 1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 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
 
687 killagreg 8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
#include <avr/wdt.h>
11
 
12
#include "eeprom.h"
1 ingob 13
#include "main.h"
685 killagreg 14
#include "menu.h"
15
#include "timer0.h"
1 ingob 16
#include "uart.h"
685 killagreg 17
#include "fc.h"
18
#include "_Settings.h"
19
#include "rc.h"
1 ingob 20
 
754 killagreg 21
#if defined (__AVR_ATmega644__)
22
#include "ubx.h"
23
#endif
24
 
25
 
26
 
735 killagreg 27
#define FALSE   0
28
#define TRUE    1
685 killagreg 29
 
745 killagreg 30
//int8_t test __attribute__ ((section (".noinit")));
683 killagreg 31
 
745 killagreg 32
uint8_t DebugGetRequest = 0, DebugDisplayRequest = 0, DebugDataRequest = 0, GetVersionRequest = 0;
33
 
735 killagreg 34
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
737 killagreg 35
volatile uint8_t rxd_buffer_locked = FALSE;
735 killagreg 36
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
737 killagreg 37
volatile uint8_t txd_complete = TRUE;
735 killagreg 38
volatile uint8_t ReceivedBytes = 0;
39
 
40
uint8_t RemotePollDisplayLine = 0;
41
uint8_t NurKanalAnforderung = 0;
42
uint8_t DebugTextAnforderung = 255;
43
uint8_t PcAccess = 100;
44
uint8_t MotorTest[4] = {0,0,0,0};
45
uint8_t DubWiseKeys[4] = {0,0,0,0};
745 killagreg 46
uint8_t MySlaveAddr = 0;
735 killagreg 47
uint8_t ConfirmFrame;
48
 
49
DebugOut_t              DebugOut;
50
ExternControl_t ExternControl;
51
VersionInfo_t   VersionInfo;
52
 
53
const uint8_t ANALOG_TEXT[32][16] =
499 hbuss 54
{
683 killagreg 55
   //1234567890123456
701 killagreg 56
    "IntegralPitch   ", //0
499 hbuss 57
    "IntegralRoll    ",
701 killagreg 58
    "AccPitch        ",
499 hbuss 59
    "AccRoll         ",
701 killagreg 60
    "GyroYaw         ",
61
    "ReadingHight    ", //5
499 hbuss 62
    "AccZ            ",
707 killagreg 63
    "Thrust          ",
701 killagreg 64
    "CompassHeading  ",
65
    "Voltage         ",
66
    "Receiver Level  ", //10
513 hbuss 67
    "Analog11        ",
741 killagreg 68
    "GPSDevNorth     ",
69
    "GPSDevEast      ",
70
    "GPS_Pitch       ",
71
    "GPS_Roll        ", //15
513 hbuss 72
    "Acc_Z           ",
701 killagreg 73
    "MeanAccPitch    ",
74
    "MeanAccRoll     ",
75
    "IntegralErrPitch",
499 hbuss 76
    "IntegralErrRoll ", //20
701 killagreg 77
    "MeanIntPitch    ",
711 killagreg 78
    "MeanIntRoll         ",
701 killagreg 79
    "NeutralPitch    ",
513 hbuss 80
    "RollOffset      ",
701 killagreg 81
    "IntRoll*Factor  ", //25
712 killagreg 82
    "ReadingGyroPitch",
701 killagreg 83
    "DirectCorrRoll  ",
712 killagreg 84
    "ReadingGyroRoll ",
701 killagreg 85
    "CorrectionRoll  ",
86
    "I-AttRoll       ", //30
513 hbuss 87
    "StickRoll       "
499 hbuss 88
};
89
 
90
 
91
 
683 killagreg 92
/****************************************************************/
93
/*              Initialization of the USART0                    */
94
/****************************************************************/
95
void USART0_Init (void)
1 ingob 96
{
683 killagreg 97
        uint8_t sreg = SREG;
98
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
99
 
100
        // disable all interrupts before configuration
101
        cli();
102
 
103
        // disable RX-Interrupt
104
        UCSR0B &= ~(1 << RXCIE0);
105
        // disable TX-Interrupt
106
        UCSR0B &= ~(1 << TXCIE0);
107
 
108
        // set direction of RXD0 and TXD0 pins
109
        // set RXD0 (PD0) as an input pin
110
        PORTD |= (1 << PORTD0);
111
        DDRD &= ~(1 << DDD0);
112
        // set TXD0 (PD1) as an output pin
113
        PORTD |= (1 << PORTD1);
114
        DDRD |=  (1 << DDD1);
115
 
116
        // USART0 Baud Rate Register
117
        // set clock divider
118
        UBRR0H = (uint8_t)(ubrr >> 8);
119
        UBRR0L = (uint8_t)ubrr;
120
 
121
        // USART0 Control and Status Register A, B, C
122
 
123
        // enable double speed operation in
124
        UCSR0A |= (1 << U2X0);
125
        // enable receiver and transmitter in
126
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
127
        // set asynchronous mode
128
        UCSR0C &= ~(1 << UMSEL01);
129
        UCSR0C &= ~(1 << UMSEL00);
130
        // no parity
131
        UCSR0C &= ~(1 << UPM01);
132
        UCSR0C &= ~(1 << UPM00);
133
        // 1 stop bit
134
        UCSR0C &= ~(1 << USBS0);
135
        // 8-bit
136
        UCSR0B &= ~(1 << UCSZ02);
137
        UCSR0C |=  (1 << UCSZ01);
138
        UCSR0C |=  (1 << UCSZ00);
139
 
140
                // flush receive buffer
141
        while ( UCSR0A & (1<<RXC0) ) UDR0;
142
 
143
        // enable interrupts at the end
144
        // enable RX-Interrupt
145
        UCSR0B |= (1 << RXCIE0);
146
        // enable TX-Interrupt
147
        UCSR0B |= (1 << TXCIE0);
148
 
737 killagreg 149
        rxd_buffer_locked = FALSE;
683 killagreg 150
        // restore global interrupt flags
151
    SREG = sreg;
152
}
153
 
154
/****************************************************************/
155
/*               USART0 transmitter ISR                         */
156
/****************************************************************/
157
ISR(USART0_TX_vect)
158
{
735 killagreg 159
        static uint16_t ptr_txd_buffer = 0;
160
        uint8_t tmp_tx;
161
        if(!txd_complete) // transmission not completed
162
        {
163
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
164
                tmp_tx = txd_buffer[ptr_txd_buffer];
165
                // if terminating character or end of txd buffer was reached
166
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
167
                {
168
                        ptr_txd_buffer = 0; // reset txd pointer
169
                        txd_complete = 1; // stop transmission
170
                }
171
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
172
        }
173
        // transmission completed
174
        else ptr_txd_buffer = 0;
1 ingob 175
}
176
 
683 killagreg 177
/****************************************************************/
178
/*               USART0 receiver ISR                            */
179
/****************************************************************/
180
ISR(USART0_RX_vect)
1 ingob 181
{
735 killagreg 182
        static uint16_t crc;
737 killagreg 183
        static uint8_t ptr_rxd_buffer = 0;
184
        uint8_t crc1, crc2;
735 killagreg 185
        uint8_t c;
1 ingob 186
 
737 killagreg 187
        c = UDR0;  // catch the received byte
188
 
754 killagreg 189
        // If the ATMEGA644 cpu is used the ublox module should be conneced to rxd of the 1st uart.
190
        // The ATMEGA644p cpu has a 2nd uart to which the ublox is connected.
191
        #if defined (__AVR_ATmega644__)
192
        ubx_parser(c);
193
        #endif
737 killagreg 194
 
195
        if(rxd_buffer_locked) return; // if txd buffer is locked immediately return
196
 
197
        // the rxd buffer is unlocked
198
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
735 killagreg 199
        {
737 killagreg 200
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
201
                crc = c; // init crc
202
        }
203
        #if 0
204
        else if (ptr_rxd_buffer == 1) // handle address
205
        {
206
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
207
                crc += c; // update crc
208
        }
209
        #endif
210
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
211
        {
212
                if(c != '\r') // no termination character
735 killagreg 213
                {
737 killagreg 214
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
215
                        crc += c; // update crc
735 killagreg 216
                }
737 killagreg 217
                else // termination character was received
218
                {
219
                        // the last 2 bytes are no subject for checksum calculation
220
                        // they are the checksum itself
221
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
222
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
223
                        // calculate checksum from transmitted data
224
                        crc %= 4096;
225
                        crc1 = '=' + crc / 64;
226
                        crc2 = '=' + crc % 64;
227
                        // compare checksum to transmitted checksum bytes
228
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
229
                        {   // checksum valid
230
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
231
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
232
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
233
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
745 killagreg 234
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
737 killagreg 235
                        }
236
                        else
237
                        {       // checksum invalid
238
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
239
                        }
240
                        ptr_rxd_buffer = 0; // reset txd buffer
241
                }
242
        } // buffer overrun
735 killagreg 243
        else
244
        {
737 killagreg 245
                ptr_rxd_buffer = 0; // reset rxd buffer
246
                rxd_buffer_locked = FALSE; // unlock rxd buffer
735 killagreg 247
        }
737 killagreg 248
 
1 ingob 249
}
250
 
251
 
252
// --------------------------------------------------------------------------
735 killagreg 253
void AddCRC(uint16_t datalen)
1 ingob 254
{
683 killagreg 255
 uint16_t tmpCRC = 0,i;
735 killagreg 256
 for(i = 0; i < datalen;i++)
1 ingob 257
  {
683 killagreg 258
   tmpCRC += txd_buffer[i];
1 ingob 259
  }
260
   tmpCRC %= 4096;
683 killagreg 261
   txd_buffer[i++] = '=' + tmpCRC / 64;
262
   txd_buffer[i++] = '=' + tmpCRC % 64;
263
   txd_buffer[i++] = '\r';
735 killagreg 264
  txd_complete = 0;
265
  UDR0 = txd_buffer[0]; // initiates the transmittion
1 ingob 266
}
267
 
268
 
269
 
270
// --------------------------------------------------------------------------
745 killagreg 271
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
1 ingob 272
{
735 killagreg 273
        uint16_t pt = 0;
274
        uint8_t a,b,c;
275
        uint8_t ptr = 0;
1 ingob 276
 
735 killagreg 277
        txd_buffer[pt++] = '#';         // Start character
745 killagreg 278
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
735 killagreg 279
        txd_buffer[pt++] = cmd;         // Command
1 ingob 280
 
735 killagreg 281
        while(len)
282
        {
283
                if(len) { a = snd[ptr++]; len--;} else a = 0;
284
                if(len) { b = snd[ptr++]; len--;} else b = 0;
285
                if(len) { c = snd[ptr++]; len--;} else c = 0;
286
                txd_buffer[pt++] = '=' + (a >> 2);
287
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
288
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
289
                txd_buffer[pt++] = '=' + ( c & 0x3f);
290
        }
291
        AddCRC(pt); // add checksum after data block and initates the transmission
1 ingob 292
}
293
 
294
 
295
// --------------------------------------------------------------------------
745 killagreg 296
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
1 ingob 297
{
735 killagreg 298
        uint8_t a,b,c,d;
299
        uint8_t ptr = 0;
300
        uint8_t x,y,z;
301
        while(len)
302
        {
303
                a = rxd_buffer[ptrIn++] - '=';
304
                b = rxd_buffer[ptrIn++] - '=';
305
                c = rxd_buffer[ptrIn++] - '=';
306
                d = rxd_buffer[ptrIn++] - '=';
745 killagreg 307
                if(ptrIn > max - 2) break;
1 ingob 308
 
735 killagreg 309
                x = (a << 2) | (b >> 4);
310
                y = ((b & 0x0f) << 4) | (c >> 2);
311
                z = ((c & 0x03) << 6) | d;
1 ingob 312
 
735 killagreg 313
                if(len--) ptrOut[ptr++] = x; else break;
314
                if(len--) ptrOut[ptr++] = y; else break;
315
                if(len--) ptrOut[ptr++] = z; else break;
316
        }
1 ingob 317
}
318
 
735 killagreg 319
 
1 ingob 320
// --------------------------------------------------------------------------
735 killagreg 321
void ProcessRxData(void)
1 ingob 322
{
737 killagreg 323
        // if data in the rxd buffer are not locked immediately return
324
        if(!rxd_buffer_locked) return;
1 ingob 325
 
735 killagreg 326
        uint8_t tmp_char_arr2[2]; // local buffer
683 killagreg 327
 
735 killagreg 328
        PcAccess = 255;
329
        switch(rxd_buffer[2])
330
        {
331
                case 'a':// Labels of the Analog Deboug outputs
332
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
333
                        DebugTextAnforderung = tmp_char_arr2[0];
499 hbuss 334
                        break;
735 killagreg 335
                case 'b': // extern control
336
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3, ReceivedBytes);
685 killagreg 337
                        RemoteButtons |= ExternControl.RemoteButtons;
735 killagreg 338
                        ConfirmFrame = ExternControl.Frame;
339
                        break;
340
                case 'c': // extern control with debug request
341
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,ReceivedBytes);
685 killagreg 342
                        RemoteButtons |= ExternControl.RemoteButtons;
735 killagreg 343
                        ConfirmFrame = ExternControl.Frame;
745 killagreg 344
                        DebugDataRequest = 1;
735 killagreg 345
                        break;
346
                case 'h':// x-1 display columns
347
                        Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes);
348
                        RemoteButtons |= tmp_char_arr2[0];
349
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1;
350
                        else NurKanalAnforderung = 0; // keine Displaydaten
745 killagreg 351
                        DebugDisplayRequest = 1;
1 ingob 352
                        break;
735 killagreg 353
                case 't':// motor test
354
                        Decode64((uint8_t *) &MotorTest[0],sizeof(MotorTest),3,ReceivedBytes);
1 ingob 355
                        break;
735 killagreg 356
                case 'k':// keys from DubWise
357
                        Decode64((uint8_t *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,ReceivedBytes);
595 hbuss 358
                        ConfirmFrame = DubWiseKeys[3];
492 hbuss 359
                        break;
735 killagreg 360
                case 'v': // get version and board release
745 killagreg 361
                        GetVersionRequest = 1;
735 killagreg 362
                        break;
363
                case 'g':// get debug data
745 killagreg 364
                        DebugGetRequest = 1;
735 killagreg 365
                        break;
366
                case 'q':// get settings
367
                        Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes);
368
                        if(tmp_char_arr2[0] != 0xff)
369
                        {
370
                                if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; // limit to 5
371
                                // load requested parameter set
372
                                ParamSet_ReadFromEEProm(tmp_char_arr2[0]);
373
                                SendOutData('L' + tmp_char_arr2[0] -1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN);
374
                        }
375
                        else // send active parameter set
376
                        SendOutData('L' + GetParamByte(PID_ACTIVE_SET)-1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN);
683 killagreg 377
 
735 killagreg 378
                        break;
683 killagreg 379
 
735 killagreg 380
                case 'l':
381
                case 'm':
382
                case 'n':
383
                case 'o':
384
                case 'p': // save parameterset
385
                        Decode64((uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN,3,ReceivedBytes);
687 killagreg 386
                        ParamSet_WriteToEEProm(rxd_buffer[2] - 'l' + 1);
735 killagreg 387
                        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
388
                        TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L;
389
                        Beep(GetActiveParamSet());
390
                        break;
683 killagreg 391
 
392
 
735 killagreg 393
        }
737 killagreg 394
        // unlock the rxd buffer after processing
395
        rxd_buffer_locked = FALSE;
1 ingob 396
}
397
 
398
//############################################################################
399
//Routine für die Serielle Ausgabe
683 killagreg 400
int16_t uart_putchar (int8_t c)
1 ingob 401
//############################################################################
402
{
403
        if (c == '\n')
404
                uart_putchar('\r');
735 killagreg 405
        // wait until previous character was send
683 killagreg 406
        loop_until_bit_is_set(UCSR0A, UDRE0);
1 ingob 407
        //Ausgabe des Zeichens
683 killagreg 408
        UDR0 = c;
409
 
1 ingob 410
        return (0);
411
}
412
 
413
 
414
//---------------------------------------------------------------------------------------------
735 killagreg 415
void TransmitTxData(void)
1 ingob 416
{
735 killagreg 417
 static int16_t Debug_Timer = 0;
418
 if(!txd_complete) return;
1 ingob 419
 
745 killagreg 420
   if(DebugGetRequest && txd_complete)        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
683 killagreg 421
   {
735 killagreg 422
      SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
745 killagreg 423
          DebugGetRequest = 0;
1 ingob 424
   }
425
 
745 killagreg 426
    if((CheckDelay(Debug_Timer) || DebugDataRequest) && txd_complete)
1 ingob 427
         {
735 killagreg 428
          SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
745 killagreg 429
          DebugDataRequest = 0;
683 killagreg 430
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);
431
         }
499 hbuss 432
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
433
     {
683 killagreg 434
      SendOutData('A',DebugTextAnforderung + '0',(uint8_t *) ANALOG_TEXT[DebugTextAnforderung],16);
499 hbuss 435
      DebugTextAnforderung = 255;
436
         }
735 killagreg 437
     if(ConfirmFrame && txd_complete)   // Datensatz ohne CRC bestätigen
595 hbuss 438
         {
683 killagreg 439
      txd_buffer[0] = '#';
440
      txd_buffer[1] = ConfirmFrame;
441
      txd_buffer[2] = '\r';
735 killagreg 442
      txd_complete = 0;
595 hbuss 443
      ConfirmFrame = 0;
683 killagreg 444
      UDR0 = txd_buffer[0];
595 hbuss 445
     }
745 killagreg 446
     if(DebugDisplayRequest && txd_complete)
1 ingob 447
         {
685 killagreg 448
      LCD_PrintMenu();
745 killagreg 449
          DebugDisplayRequest = 0;
683 killagreg 450
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
173 holgerb 451
      {
683 killagreg 452
       SendOutData('4',0,(uint8_t *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
499 hbuss 453
       RemotePollDisplayLine = -1;
683 killagreg 454
      }
455
      else  SendOutData('0' + RemotePollDisplayLine,0,(uint8_t *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
456
         }
745 killagreg 457
    if(GetVersionRequest && txd_complete)
683 killagreg 458
     {
735 killagreg 459
      SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo,sizeof(VersionInfo));
745 killagreg 460
          GetVersionRequest = 0;
1 ingob 461
     }
462
 
463
}
464