Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
528 salvo 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 "main.h"
9
#include "uart.h"
10
 
11
unsigned char DebugGetAnforderung = 0,DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0;
12
unsigned volatile char SioTmp = 0;
13
unsigned volatile char SendeBuffer[MAX_SENDE_BUFF];
14
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
15
unsigned volatile char NMEABuffer[MAX_EMPFANGS_BUFF];
16
unsigned volatile char NeuerDatensatzEmpfangen = 0;
17
unsigned volatile char NeueKoordinateEmpfangen = 0;
18
unsigned volatile char UebertragungAbgeschlossen = 1;
19
unsigned volatile char CntCrcError = 0;
20
unsigned volatile char AnzahlEmpfangsBytes = 0;
21
unsigned volatile char PC_DebugTimeout = 0;
529 salvo 22
unsigned char RemotePollDisplayLine = 0;
23
unsigned char NurKanalAnforderung = 0;
24
unsigned char DebugTextAnforderung = 255;
528 salvo 25
unsigned char PcZugriff = 100;
26
unsigned char MotorTest[4] = {0,0,0,0};
529 salvo 27
unsigned char DubWiseKeys[3] = {0,0,0};
528 salvo 28
unsigned char MeineSlaveAdresse;
29
struct str_DebugOut    DebugOut;
30
struct str_Debug       DebugIn;
31
struct str_VersionInfo VersionInfo;
32
int Debug_Timer;
33
//Salvo 26.10.2007
542 salvo 34
int dataset_cnt=0; //Zaehlt die uebertragenen Debugdaten
528 salvo 35
//Salvo End
529 salvo 36
 
37
const unsigned char ANALOG_TEXT[32][16] =
38
{
39
   //1234567890123456 
40
    "IntegralNick    ", //0
41
    "IntegralRoll    ",
42
    "AccNick         ",
43
    "AccRoll         ",
44
    "GyroGier        ",
45
    "HoehenWert      ", //5
46
    "AccZ            ",
47
    "Gas             ",
48
    "KompassValue    ",
49
    "Ersatzkompass   ",
542 salvo 50
    "Usr_Par1        ", //10
51
    "Usr_par3        ",
529 salvo 52
    "Motor_Vorne     ",
53
    "Motor_Hinten    ",
54
    "Motor_Links     ",
55
    "Motor_Rechts    ", //15
56
    "Index           ",
57
    "UBat            ",
58
    "Messwert_Nick   ",
59
    "Messwert_Roll   ",
60
    "Messwert_Gier   ", //20
61
    "Stick_Nick      ",
579 salvo 62
    "Stick_Roll      ",
529 salvo 63
    "Stick_Gier      ",
542 salvo 64
    "GPS_Nick        ",
65
    "GPS_Roll        ", //25
529 salvo 66
    "utm_east        ",
67
    "utm_north       ",
68
    "utm_alt         ",
69
    "gps_state       ",
602 salvo 70
//    "dist_flown      ", //30
71
//    "dist_2home      "
613 salvo 72
    "gp_reg_x        ", //30
73
    "gp_reg_y        "
529 salvo 74
};
75
 
76
 
77
 
528 salvo 78
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
79
//++ Sende-Part der Datenübertragung
80
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
81
SIGNAL(INT_VEC_TX)
82
{
83
 static unsigned int ptr = 0;
84
 unsigned char tmp_tx;
85
 if(!UebertragungAbgeschlossen)  
86
  {
87
   ptr++;                    // die [0] wurde schon gesendet
88
   tmp_tx = SendeBuffer[ptr];  
89
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
90
    {
91
     ptr = 0;
92
     UebertragungAbgeschlossen = 1;
93
    }
94
   UDR = tmp_tx;
95
  }
96
  else ptr = 0;
97
}
98
 
99
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
100
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
101
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102
SIGNAL(INT_VEC_RX)
103
{
104
 static unsigned int crc;
105
 static unsigned char crc1,crc2,buf_ptr;
106
 static unsigned char UartState = 0;
107
 unsigned char CrcOkay = 0;
108
 
109
 SioTmp = UDR;
110
 //Salvo 11.9.2007 GPS Daten holen
111
 Get_Ublox_Msg(SioTmp); // Daten vom GPS Modul holen
112
 // Salvo End
113
 if(buf_ptr >= MAX_EMPFANGS_BUFF)    UartState = 0;
114
 if(SioTmp == '\r' && UartState == 2)
115
  {
116
   UartState = 0;
117
   crc -= RxdBuffer[buf_ptr-2];
118
   crc -= RxdBuffer[buf_ptr-1];
119
   crc %= 4096;
120
   crc1 = '=' + crc / 64;
121
   crc2 = '=' + crc % 64;
122
   CrcOkay = 0;
123
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
124
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
125
    {
126
     NeuerDatensatzEmpfangen = 1;
127
         AnzahlEmpfangsBytes = buf_ptr;
128
     RxdBuffer[buf_ptr] = '\r';
129
         if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
130
        }                                
131
  }
132
  else
133
  switch(UartState)
134
  {
135
   case 0:
136
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
137
                  buf_ptr = 0;
138
                  RxdBuffer[buf_ptr++] = SioTmp;
139
                  crc = SioTmp;
140
          break;
141
   case 1: // Adresse auswerten
142
                  UartState++;
143
                  RxdBuffer[buf_ptr++] = SioTmp;
144
                  crc += SioTmp;
145
                  break;
146
   case 2: //  Eingangsdaten sammeln
147
                  RxdBuffer[buf_ptr] = SioTmp;
148
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
149
                  else UartState = 0;
150
                  crc += SioTmp;
151
                  break;
152
   default:
153
          UartState = 0;
154
          break;
155
  }
156
}
157
 
158
 
159
// --------------------------------------------------------------------------
160
void AddCRC(unsigned int wieviele)
161
{
162
 unsigned int tmpCRC = 0,i;
163
 for(i = 0; i < wieviele;i++)
164
  {
165
   tmpCRC += SendeBuffer[i];
166
  }
167
   tmpCRC %= 4096;
168
   SendeBuffer[i++] = '=' + tmpCRC / 64;
169
   SendeBuffer[i++] = '=' + tmpCRC % 64;
170
   SendeBuffer[i++] = '\r';
171
  UebertragungAbgeschlossen = 0;
172
  UDR = SendeBuffer[0];
173
}
174
 
175
 
176
 
177
// --------------------------------------------------------------------------
178
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
179
{
180
 unsigned int pt = 0;
181
 unsigned char a,b,c;
182
 unsigned char ptr = 0;
183
 
184
 SendeBuffer[pt++] = '#';               // Startzeichen
185
 SendeBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
186
 SendeBuffer[pt++] = cmd;                       // Commando
187
 
188
 while(len)
189
  {
190
   if(len) { a = snd[ptr++]; len--;} else a = 0;
191
   if(len) { b = snd[ptr++]; len--;} else b = 0;
192
   if(len) { c = snd[ptr++]; len--;} else c = 0;
193
   SendeBuffer[pt++] = '=' + (a >> 2);
194
   SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
195
   SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
196
   SendeBuffer[pt++] = '=' + ( c & 0x3f);
197
  }
198
 AddCRC(pt);
199
}
200
 
201
 
202
// --------------------------------------------------------------------------
203
void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max)  // Wohin mit den Daten; Wie lang; Wo im RxdBuffer
204
{
205
 unsigned char a,b,c,d;
206
 unsigned char ptr = 0;
207
 unsigned char x,y,z;
208
 while(len)
209
  {
210
   a = RxdBuffer[ptrIn++] - '=';
211
   b = RxdBuffer[ptrIn++] - '=';
212
   c = RxdBuffer[ptrIn++] - '=';
213
   d = RxdBuffer[ptrIn++] - '=';
214
   if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
215
 
216
   x = (a << 2) | (b >> 4);
217
   y = ((b & 0x0f) << 4) | (c >> 2);
218
   z = ((c & 0x03) << 6) | d;
219
 
220
   if(len--) ptrOut[ptr++] = x; else break;
221
   if(len--) ptrOut[ptr++] = y; else break;
222
   if(len--) ptrOut[ptr++] = z; else break;
223
  }
224
 
225
}
226
 
227
// --------------------------------------------------------------------------
228
void BearbeiteRxDaten(void)
229
{
230
 if(!NeuerDatensatzEmpfangen) return;
231
 
529 salvo 232
// unsigned int tmp_int_arr1[1];
233
// unsigned int tmp_int_arr2[2];
234
// unsigned int tmp_int_arr3[3];
528 salvo 235
 unsigned char tmp_char_arr2[2];
529 salvo 236
// unsigned char tmp_char_arr3[3];
237
// unsigned char tmp_char_arr4[4];
528 salvo 238
 //if(!MotorenEin) 
239
 PcZugriff = 255;
240
  switch(RxdBuffer[2])
241
  {
529 salvo 242
   case 'a':// Texte der Analogwerte
243
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
244
            DebugTextAnforderung = tmp_char_arr2[0];
245
                        break;
528 salvo 246
   case 'c':// Debugdaten incl. Externe IOs usw 
247
                        Decode64((unsigned char *) &DebugIn,sizeof(DebugIn),3,AnzahlEmpfangsBytes);
248
/*              for(unsigned char i=0; i<4;i++)
249
                         {
250
              EE_CheckAndWrite(&EE_Buffer[EE_DEBUGWERTE + i*2],     DebugIn.Analog[i]);
251
                          EE_CheckAndWrite(&EE_Buffer[EE_DEBUGWERTE + i*2 + 1], DebugIn.Analog[i] >> 8);       
252
                         }*/
529 salvo 253
                        RemoteTasten |= DebugIn.RemoteTasten;
528 salvo 254
            DebugDataAnforderung = 1;
255
            break;
256
   case 'h':// x-1 Displayzeilen
257
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
258
            RemoteTasten |= tmp_char_arr2[0];
529 salvo 259
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten
528 salvo 260
                        DebugDisplayAnforderung = 1;
261
                        break;
262
   case 't':// Motortest
263
            Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
264
                        break;
529 salvo 265
   case 'k':// Keys von DubWise
266
            Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes);
267
                        break;
528 salvo 268
   case 'v': // Version-Anforderung     und Ausbaustufe
269
            GetVersionAnforderung = 1;
270
            break;                                                               
271
   case 'g':// "Get"-Anforderung für Debug-Daten 
272
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
273
            DebugGetAnforderung = 1;
274
            break;
275
   case 'q':// "Get"-Anforderung für Settings
276
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
277
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
278
            if(tmp_char_arr2[0] != 0xff)
279
             {
280
                          if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5;
281
                  ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);                   
282
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
283
             }
284
             else
285
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
286
 
287
            break;
288
 
289
   case 'l':
290
   case 'm':
291
   case 'n':
292
   case 'o':
293
   case 'p': // Parametersatz speichern
294
            Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes);
295
                        WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
296
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
529 salvo 297
            Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
298
            Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
528 salvo 299
            Piep(GetActiveParamSetNumber());
300
         break;
301
 
302
 
303
  }
304
// DebugOut.AnzahlZyklen =  Debug_Timer_Intervall;
305
 NeuerDatensatzEmpfangen = 0;
306
}
307
 
308
//############################################################################
309
//Routine für die Serielle Ausgabe
310
int uart_putchar (char c)
311
//############################################################################
312
{
313
        if (c == '\n')
314
                uart_putchar('\r');
315
        //Warten solange bis Zeichen gesendet wurde
316
        loop_until_bit_is_set(USR, UDRE);
317
        //Ausgabe des Zeichens
318
        UDR = c;
319
 
320
        return (0);
321
}
322
 
323
// --------------------------------------------------------------------------
324
void WriteProgramData(unsigned int pos, unsigned char wert)
325
{
326
  //if (ProgramLocation == IN_RAM) Buffer[pos] = wert;
327
  // else eeprom_write_byte(&EE_Buffer[pos], wert);
328
  // Buffer[pos] = wert;
329
}
330
 
331
//############################################################################
332
//INstallation der Seriellen Schnittstelle
333
void UART_Init (void)
334
//############################################################################
335
{
336
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
337
 
338
        UCR=(1 << TXEN) | (1 << RXEN);
339
    // UART Double Speed (U2X)
340
        USR   |= (1<<U2X);          
341
        // RX-Interrupt Freigabe
342
        UCSRB |= (1<<RXCIE);          
343
        // TX-Interrupt Freigabe
344
        UCSRB |= (1<<TXCIE);          
345
 
346
        //Teiler wird gesetzt 
347
        UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1);
348
        //UBRR = 33;
349
        //öffnet einen Kanal für printf (STDOUT)
350
        //fdevopen (uart_putchar, 0);
351
        //sbi(PORTD,4);
352
  Debug_Timer = SetDelay(200);  
353
}
354
 
355
//---------------------------------------------------------------------------------------------
356
void DatenUebertragung(void)  
357
{
358
 static char dis_zeile = 0;
359
 if(!UebertragungAbgeschlossen) return;
360
 
361
   if(DebugGetAnforderung && UebertragungAbgeschlossen)               // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
362
   {
363
      SendOutData('G',MeineSlaveAdresse,(unsigned char *) &DebugIn,sizeof(DebugIn));
364
          DebugGetAnforderung = 0;
365
   }
366
 
367
    if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen)  
368
         {
369
          SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut));
370
          DebugDataAnforderung = 0;
371
                  //Salvo 26.10.2007  Counter fuer Erkennung von fehlenden Daten per Bluetoothuebertragung
372
                  dataset_cnt++;
373
                  // Salvo End
374
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);  
375
         }
529 salvo 376
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
377
     {
378
      SendOutData('A',DebugTextAnforderung + '0',(unsigned char *) ANALOG_TEXT[DebugTextAnforderung],16);
379
      DebugTextAnforderung = 255;
380
         }
528 salvo 381
 
382
     if(DebugDisplayAnforderung && UebertragungAbgeschlossen)
383
         {
384
      Menu();
385
          DebugDisplayAnforderung = 0;
529 salvo 386
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
528 salvo 387
      {
529 salvo 388
       SendOutData('4',0,(unsigned char *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
389
       RemotePollDisplayLine = -1;
528 salvo 390
      }
529 salvo 391
      else  SendOutData('0' + RemotePollDisplayLine,0,(unsigned char *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
528 salvo 392
         }
393
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
394
     {
395
      SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo));
396
          GetVersionAnforderung = 0;
397
     }
398
 
399
}
400