Subversion Repositories FlightCtrl

Rev

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