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;
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
 SioTmp = UDR;
143 salvo 61
 //Salvo 11.9.2007 GPS Daten holen
62
 Get_Ublox_Msg(SioTmp); // Daten vom GPS Modul holen
63
 // Salvo End
1 ingob 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';
80
         if(/*(RxdBuffer[1] == MeineSlaveAdresse || (RxdBuffer[1] == 'a')) && */(RxdBuffer[2] == 'R')) wdt_enable(WDTO_250MS); // Reset-Commando
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
 
183
 unsigned int tmp_int_arr1[1];
184
 unsigned int tmp_int_arr2[2];
185
 unsigned int tmp_int_arr3[3];
186
 unsigned char tmp_char_arr2[2];
187
 unsigned char tmp_char_arr3[3];
188
 unsigned char tmp_char_arr4[4];
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
                         }*/
200
                        RemoteTasten |= DebugIn.RemoteTasten;
201
            DebugDataAnforderung = 1;
202
            break;
203
 
204
   case 'h':// x-1 Displayzeilen
205
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
206
            RemoteTasten |= tmp_char_arr2[0];
207
                        DebugDisplayAnforderung = 1;
208
                        break;
209
   case 't':// Motortest
210
            Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
211
                        break;
212
   case 'v': // Version-Anforderung     und Ausbaustufe
213
            GetVersionAnforderung = 1;
214
            break;                                                               
215
   case 'g':// "Get"-Anforderung für Debug-Daten 
216
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
217
            DebugGetAnforderung = 1;
218
            break;
219
   case 'q':// "Get"-Anforderung für Settings
220
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
221
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
222
            if(tmp_char_arr2[0] != 0xff)
223
             {
224
                          if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5;
225
                  ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);                   
226
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
227
             }
228
             else
229
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
230
 
231
            break;
232
 
233
   case 'l':
234
   case 'm':
235
   case 'n':
236
   case 'o':
237
   case 'p': // Parametersatz speichern
238
            Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes);
239
                        WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
240
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
241
            Piep(GetActiveParamSetNumber());
242
         break;
243
 
244
 
245
  }
246
// DebugOut.AnzahlZyklen =  Debug_Timer_Intervall;
247
 NeuerDatensatzEmpfangen = 0;
248
}
249
 
250
//############################################################################
251
//Routine für die Serielle Ausgabe
252
int uart_putchar (char c)
253
//############################################################################
254
{
255
        if (c == '\n')
256
                uart_putchar('\r');
257
        //Warten solange bis Zeichen gesendet wurde
258
        loop_until_bit_is_set(USR, UDRE);
259
        //Ausgabe des Zeichens
260
        UDR = c;
261
 
262
        return (0);
263
}
264
 
265
// --------------------------------------------------------------------------
266
void WriteProgramData(unsigned int pos, unsigned char wert)
267
{
268
  //if (ProgramLocation == IN_RAM) Buffer[pos] = wert;
269
  // else eeprom_write_byte(&EE_Buffer[pos], wert);
270
  // Buffer[pos] = wert;
271
}
272
 
273
//############################################################################
274
//INstallation der Seriellen Schnittstelle
275
void UART_Init (void)
276
//############################################################################
277
{
278
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
279
 
280
        UCR=(1 << TXEN) | (1 << RXEN);
281
    // UART Double Speed (U2X)
282
        USR   |= (1<<U2X);          
283
        // RX-Interrupt Freigabe
284
        UCSRB |= (1<<RXCIE);          
285
        // TX-Interrupt Freigabe
286
        UCSRB |= (1<<TXCIE);          
287
 
288
        //Teiler wird gesetzt 
289
        UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1);
290
        //UBRR = 33;
291
        //öffnet einen Kanal für printf (STDOUT)
292
        //fdevopen (uart_putchar, 0);
293
        //sbi(PORTD,4);
294
  Debug_Timer = SetDelay(200);  
295
}
296
 
297
//---------------------------------------------------------------------------------------------
298
void DatenUebertragung(void)  
299
{
300
 static char dis_zeile = 0;
301
 if(!UebertragungAbgeschlossen) return;
302
 
303
   if(DebugGetAnforderung && UebertragungAbgeschlossen)               // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
304
   {
305
      SendOutData('G',MeineSlaveAdresse,(unsigned char *) &DebugIn,sizeof(DebugIn));
306
          DebugGetAnforderung = 0;
307
   }
308
 
309
    if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen)  
310
         {
311
          SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut));
312
          DebugDataAnforderung = 0;
313
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);  
314
         }
315
 
316
     if(DebugDisplayAnforderung && UebertragungAbgeschlossen)
317
         {
318
      Menu();
319
          DebugDisplayAnforderung = 0;
320
      if(++dis_zeile == 4) dis_zeile = 0;
321
      SendOutData('0' + dis_zeile,0,&DisplayBuff[20 * dis_zeile],20);   // DisplayZeile übertragen
322
         }
323
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
324
     {
325
      SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo));
326
          GetVersionAnforderung = 0;
327
     }
328
 
329
}
330