Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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