Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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