Subversion Repositories FlightCtrl

Rev

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