Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
787 Nick666 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 char SendeBuffer[MAX_SENDE_BUFF];
13
unsigned char RxdBuffer[MAX_EMPFANGS_BUFF];
14
unsigned volatile char NMEABuffer[MAX_EMPFANGS_BUFF];
15
unsigned volatile char NeuerDatensatzEmpfangen = 0;
16
unsigned volatile char NeueKoordinateEmpfangen = 0;
17
unsigned volatile char UebertragungAbgeschlossen = 1;
18
unsigned volatile char CntCrcError = 0;
19
unsigned volatile char AnzahlEmpfangsBytes = 0;
20
unsigned volatile char PC_DebugTimeout = 0;
21
unsigned char RemotePollDisplayLine = 0;
22
unsigned char NurKanalAnforderung = 0;
23
unsigned char DebugTextAnforderung = 255;
24
unsigned char PcZugriff = 100;
25
unsigned char MotorTest[4] = {0,0,0,0};
26
unsigned char DubWiseKeys[4] = {0,0,0,0};
27
unsigned char MeineSlaveAdresse;
28
unsigned char ConfirmFrame;
29
struct str_DebugOut    DebugOut;
30
struct str_ExternControl  ExternControl;
31
struct str_VersionInfo VersionInfo;
32
int Debug_Timer;
33
 
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
    "Spannung        ",
47
    "Empfang         ", //10
48
    "Analog11        ",
49
    "Motor_Vorne     ",
50
    "Motor_Hinten    ",
51
    "Motor_Links     ",
52
    "Motor_Rechts    ", //15
53
    "Acc_Z           ",
54
    "MittelAccNick   ",
55
    "MittelAccRoll   ",
56
    "IntegralErrNick ",
57
    "IntegralErrRoll ", //20
58
    "MittelIntNick   ",
59
    "MittelIntRoll       ",
60
    "NeutralNick     ",
61
    "RollOffset      ",
62
    "IntRoll*Faktor  ", //25
63
    "Analog26        ",
64
    "DirektAusglRoll ",
65
    "MesswertRoll    ",
66
    "AusgleichRoll   ",
67
    "I-LageRoll      ", //30
68
    "StickRoll       "
69
};
70
 
71
 
72
 
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 SioTmp;
103
 
104
 SioTmp = UDR;
105
 if(buf_ptr >= MAX_EMPFANGS_BUFF)    UartState = 0;
106
if(buf_ptr == 0)
107
 {
108
 if(SioTmp == '#' && !NeuerDatensatzEmpfangen) buf_ptr = 1; // Startzeichen und Daten schon verarbeitet
109
 RxdBuffer[0] = SioTmp;
110
 crc = SioTmp;
111
 }
112
#if 0
113
 else if ((buf_ptr == 1) // Adresse auswerten
114
 {
115
 RxdBuffer[buf_ptr++] = SioTmp;
116
 crc += SioTmp;
117
 }
118
#endif
119
 else if (/*(buf_ptr > 0) && */(buf_ptr < MAX_EMPFANGS_BUFF))
120
 { // Eingangsdaten sammeln
121
 if(SioTmp != '\r')
122
 {
123
 RxdBuffer[buf_ptr++] = SioTmp;
124
 crc += SioTmp;
125
 }
126
 else
127
 {
128
 crc -= RxdBuffer[buf_ptr-2];
129
 crc -= RxdBuffer[buf_ptr-1];
130
 crc %= 4096;
131
 crc1 = '=' + crc / 64;
132
 crc2 = '=' + crc % 64;
133
 // CrcOkay = 0;
134
 if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1]))
135
 {
136
 if(!NeuerDatensatzEmpfangen) // Datensatz schon verarbeitet
137
 {
138
 NeuerDatensatzEmpfangen = 1;
139
 AnzahlEmpfangsBytes = buf_ptr;
140
 RxdBuffer[buf_ptr] = '\r';
141
 if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
142
 }
143
 }
144
 else { CntCrcError++;}
145
 buf_ptr = 0;
146
 }
147
 }
148
 else
149
 {
150
 buf_ptr = 0;
151
 }
152
}
153
 
154
 
155
// --------------------------------------------------------------------------
156
void AddCRC(unsigned int wieviele)
157
{
158
 unsigned int tmpCRC = 0,i;
159
 for(i = 0; i < wieviele;i++)
160
  {
161
   tmpCRC += SendeBuffer[i];
162
  }
163
   tmpCRC %= 4096;
164
   SendeBuffer[i++] = '=' + tmpCRC / 64;
165
   SendeBuffer[i++] = '=' + tmpCRC % 64;
166
   SendeBuffer[i++] = '\r';
167
  UebertragungAbgeschlossen = 0;
168
  UDR = SendeBuffer[0];
169
}
170
 
171
 
172
 
173
// --------------------------------------------------------------------------
174
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
175
{
176
 unsigned int pt = 0;
177
 unsigned char a,b,c;
178
 unsigned char ptr = 0;
179
 
180
 SendeBuffer[pt++] = '#';               // Startzeichen
181
 SendeBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
182
 SendeBuffer[pt++] = cmd;                       // Commando
183
 
184
 while(len)
185
  {
186
   if(len) { a = snd[ptr++]; len--;} else a = 0;
187
   if(len) { b = snd[ptr++]; len--;} else b = 0;
188
   if(len) { c = snd[ptr++]; len--;} else c = 0;
189
   SendeBuffer[pt++] = '=' + (a >> 2);
190
   SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
191
   SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
192
   SendeBuffer[pt++] = '=' + ( c & 0x3f);
193
  }
194
 AddCRC(pt);
195
}
196
 
197
 
198
// --------------------------------------------------------------------------
199
void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max)  // Wohin mit den Daten; Wie lang; Wo im RxdBuffer
200
{
201
 unsigned char a,b,c,d;
202
 unsigned char ptr = 0;
203
 unsigned char x,y,z;
204
 while(len)
205
  {
206
   a = RxdBuffer[ptrIn++] - '=';
207
   b = RxdBuffer[ptrIn++] - '=';
208
   c = RxdBuffer[ptrIn++] - '=';
209
   d = RxdBuffer[ptrIn++] - '=';
210
   if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
211
 
212
   x = (a << 2) | (b >> 4);
213
   y = ((b & 0x0f) << 4) | (c >> 2);
214
   z = ((c & 0x03) << 6) | d;
215
 
216
   if(len--) ptrOut[ptr++] = x; else break;
217
   if(len--) ptrOut[ptr++] = y; else break;
218
   if(len--) ptrOut[ptr++] = z; else break;
219
  }
220
 
221
}
222
 
223
// --------------------------------------------------------------------------
224
void BearbeiteRxDaten(void)
225
{
226
 if(!NeuerDatensatzEmpfangen) return;
227
 
228
// unsigned int tmp_int_arr1[1];
229
// unsigned int tmp_int_arr2[2];
230
// unsigned int tmp_int_arr3[3];
231
 unsigned char tmp_char_arr2[2];
232
// unsigned char tmp_char_arr3[3];
233
// unsigned char tmp_char_arr4[4];
234
 //if(!MotorenEin) 
235
 PcZugriff = 255;
236
  switch(RxdBuffer[2])
237
  {
238
   case 'a':// Texte der Analogwerte
239
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
240
            DebugTextAnforderung = tmp_char_arr2[0];
241
                        break;
242
   case 'b':
243
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
244
                        RemoteTasten |= ExternControl.RemoteTasten;
245
            ConfirmFrame = ExternControl.Frame;
246
            break;
247
   case 'c':
248
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
249
                        RemoteTasten |= ExternControl.RemoteTasten;
250
            ConfirmFrame = ExternControl.Frame;
251
            DebugDataAnforderung = 1;
252
            break;
253
   case 'h':// x-1 Displayzeilen
254
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
255
            RemoteTasten |= tmp_char_arr2[0];
256
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten
257
                        DebugDisplayAnforderung = 1;
258
                        break;
259
   case 't':// Motortest
260
            Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
261
                        break;
262
   case 'k':// Keys von DubWise
263
            Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes);
264
                        ConfirmFrame = DubWiseKeys[3];
265
                        break;
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], sizeof(struct mk_param_struct));                
280
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],sizeof(struct mk_param_struct));
281
             }
282
             else
283
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],sizeof(struct mk_param_struct));
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],sizeof(struct mk_param_struct),3,AnzahlEmpfangsBytes);
293
                        WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], sizeof(struct mk_param_struct));
294
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
295
            Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
296
            Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
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
 if(!UebertragungAbgeschlossen) return;
357
 
358
   if(DebugGetAnforderung && UebertragungAbgeschlossen)               // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
359
   {
360
      SendOutData('G',MeineSlaveAdresse,(unsigned char *) &ExternControl,sizeof(ExternControl));
361
          DebugGetAnforderung = 0;
362
   }
363
 
364
    if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen)  
365
         {
366
          SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut));
367
          DebugDataAnforderung = 0;
368
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);  
369
         }
370
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
371
     {
372
      SendOutData('A',DebugTextAnforderung + '0',(unsigned char *) ANALOG_TEXT[DebugTextAnforderung],16);
373
      DebugTextAnforderung = 255;
374
         }
375
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz ohne CRC bestätigen 
376
         {
377
      SendeBuffer[0] = '#';
378
      SendeBuffer[1] = ConfirmFrame;
379
      SendeBuffer[2] = '\r';
380
      UebertragungAbgeschlossen = 0;
381
      ConfirmFrame = 0;
382
      UDR = SendeBuffer[0];
383
     }
384
     if(DebugDisplayAnforderung && UebertragungAbgeschlossen)
385
         {
386
      Menu();
387
          DebugDisplayAnforderung = 0;
388
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
389
      {
390
       SendOutData('4',0,(unsigned char *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
391
       RemotePollDisplayLine = -1;
392
      }
393
      else  SendOutData('0' + RemotePollDisplayLine,0,(unsigned char *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
394
         }
395
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
396
     {
397
      SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo));
398
          GetVersionAnforderung = 0;
399
     }
400
 
401
}
402