Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
438 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 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 NurKanalAnforderung = 0;
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;
30
 
31
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
//++ Sende-Part der Datenübertragung
33
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
SIGNAL(INT_VEC_TX)
35
{
36
 static unsigned int ptr = 0;
37
 unsigned char tmp_tx;
38
 if(!UebertragungAbgeschlossen)  
39
  {
40
   ptr++;                    // die [0] wurde schon gesendet
41
   tmp_tx = SendeBuffer[ptr];  
42
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
43
    {
44
     ptr = 0;
45
     UebertragungAbgeschlossen = 1;
46
    }
47
   UDR = tmp_tx;
48
  }
49
  else ptr = 0;
50
}
51
 
52
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
54
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
SIGNAL(INT_VEC_RX)
56
{
57
 static unsigned int crc;
58
 static unsigned char crc1,crc2,buf_ptr;
59
 static unsigned char UartState = 0;
60
 unsigned char CrcOkay = 0;
61
 
62
 SioTmp = UDR;
63
 if(buf_ptr >= MAX_EMPFANGS_BUFF)    UartState = 0;
64
 if(SioTmp == '\r' && UartState == 2)
65
  {
66
   UartState = 0;
67
   crc -= RxdBuffer[buf_ptr-2];
68
   crc -= RxdBuffer[buf_ptr-1];
69
   crc %= 4096;
70
   crc1 = '=' + crc / 64;
71
   crc2 = '=' + crc % 64;
72
   CrcOkay = 0;
73
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
74
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
75
    {
76
     NeuerDatensatzEmpfangen = 1;
77
         AnzahlEmpfangsBytes = buf_ptr;
78
     RxdBuffer[buf_ptr] = '\r';
79
         if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
80
        }                                
81
  }
82
  else
83
  switch(UartState)
84
  {
85
   case 0:
86
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
87
                  buf_ptr = 0;
88
                  RxdBuffer[buf_ptr++] = SioTmp;
89
                  crc = SioTmp;
90
          break;
91
   case 1: // Adresse auswerten
92
                  UartState++;
93
                  RxdBuffer[buf_ptr++] = SioTmp;
94
                  crc += SioTmp;
95
                  break;
96
   case 2: //  Eingangsdaten sammeln
97
                  RxdBuffer[buf_ptr] = SioTmp;
98
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
99
                  else UartState = 0;
100
                  crc += SioTmp;
101
                  break;
102
   default:
103
          UartState = 0;
104
          break;
105
  }
106
}
107
 
108
 
109
// --------------------------------------------------------------------------
110
void AddCRC(unsigned int wieviele)
111
{
112
 unsigned int tmpCRC = 0,i;
113
 for(i = 0; i < wieviele;i++)
114
  {
115
   tmpCRC += SendeBuffer[i];
116
  }
117
   tmpCRC %= 4096;
118
   SendeBuffer[i++] = '=' + tmpCRC / 64;
119
   SendeBuffer[i++] = '=' + tmpCRC % 64;
120
   SendeBuffer[i++] = '\r';
121
  UebertragungAbgeschlossen = 0;
122
  UDR = SendeBuffer[0];
123
}
124
 
125
 
126
 
127
// --------------------------------------------------------------------------
128
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
129
{
130
 unsigned int pt = 0;
131
 unsigned char a,b,c;
132
 unsigned char ptr = 0;
133
 
134
 SendeBuffer[pt++] = '#';               // Startzeichen
135
 SendeBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
136
 SendeBuffer[pt++] = cmd;                       // Commando
137
 
138
 while(len)
139
  {
140
   if(len) { a = snd[ptr++]; len--;} else a = 0;
141
   if(len) { b = snd[ptr++]; len--;} else b = 0;
142
   if(len) { c = snd[ptr++]; len--;} else c = 0;
143
   SendeBuffer[pt++] = '=' + (a >> 2);
144
   SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
145
   SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
146
   SendeBuffer[pt++] = '=' + ( c & 0x3f);
147
  }
148
 AddCRC(pt);
149
}
150
 
151
 
152
// --------------------------------------------------------------------------
153
void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max)  // Wohin mit den Daten; Wie lang; Wo im RxdBuffer
154
{
155
 unsigned char a,b,c,d;
156
 unsigned char ptr = 0;
157
 unsigned char x,y,z;
158
 while(len)
159
  {
160
   a = RxdBuffer[ptrIn++] - '=';
161
   b = RxdBuffer[ptrIn++] - '=';
162
   c = RxdBuffer[ptrIn++] - '=';
163
   d = RxdBuffer[ptrIn++] - '=';
164
   if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
165
 
166
   x = (a << 2) | (b >> 4);
167
   y = ((b & 0x0f) << 4) | (c >> 2);
168
   z = ((c & 0x03) << 6) | d;
169
 
170
   if(len--) ptrOut[ptr++] = x; else break;
171
   if(len--) ptrOut[ptr++] = y; else break;
172
   if(len--) ptrOut[ptr++] = z; else break;
173
  }
174
 
175
}
176
 
177
// --------------------------------------------------------------------------
178
void BearbeiteRxDaten(void)
179
{
180
 if(!NeuerDatensatzEmpfangen) return;
181
 
182
// unsigned int tmp_int_arr1[1];
183
// unsigned int tmp_int_arr2[2];
184
// unsigned int tmp_int_arr3[3];
185
 unsigned char tmp_char_arr2[2];
186
// unsigned char tmp_char_arr3[3];
187
// unsigned char tmp_char_arr4[4];
188
 //if(!MotorenEin) 
189
 PcZugriff = 255;
190
  switch(RxdBuffer[2])
191
  {
192
   case 'c':// Debugdaten incl. Externe IOs usw 
193
                        Decode64((unsigned char *) &DebugIn,sizeof(DebugIn),3,AnzahlEmpfangsBytes);
194
/*              for(unsigned char i=0; i<4;i++)
195
                         {
196
              EE_CheckAndWrite(&EE_Buffer[EE_DEBUGWERTE + i*2],     DebugIn.Analog[i]);
197
                          EE_CheckAndWrite(&EE_Buffer[EE_DEBUGWERTE + i*2 + 1], DebugIn.Analog[i] >> 8);       
198
                         }*/
199
                        RemoteTasten |= DebugIn.RemoteTasten;
200
            DebugDataAnforderung = 1;
201
            break;
202
   case 'h':// x-1 Displayzeilen
203
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
204
            RemoteTasten |= tmp_char_arr2[0];
205
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten
206
                        DebugDisplayAnforderung = 1;
207
                        break;
208
   case 't':// Motortest
209
            Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
210
                        break;
211
   case 'v': // Version-Anforderung     und Ausbaustufe
212
            GetVersionAnforderung = 1;
213
            break;                                                               
214
   case 'g':// "Get"-Anforderung für Debug-Daten 
215
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
216
            DebugGetAnforderung = 1;
217
            break;
218
   case 'q':// "Get"-Anforderung für Settings
219
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
220
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
221
            if(tmp_char_arr2[0] != 0xff)
222
             {
223
                          if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5;
224
                  ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);                   
225
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
226
             }
227
             else
228
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
229
 
230
            break;
231
 
232
   case 'l':
233
   case 'm':
234
   case 'n':
235
   case 'o':
236
   case 'p': // Parametersatz speichern
237
            Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes);
238
                        WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
239
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
240
            Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
241
            Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
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 || NurKanalAnforderung)
322
      {
323
       SendOutData('4',0,(unsigned char *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
324
       dis_zeile = -1;
325
      }
326
      else  SendOutData('0' + dis_zeile,0,(unsigned char *)&DisplayBuff[20 * dis_zeile],20);   // DisplayZeile übertragen
327
         }
328
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
329
     {
330
      SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo));
331
          GetVersionAnforderung = 0;
332
     }
333
 
334
}
335