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