Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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