Subversion Repositories FlightCtrl

Rev

Rev 2302 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + www.MikroKopter.com
3
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// + Software Nutzungsbedingungen (english version: see below)
5
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
6
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
7
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
8
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
9
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
12
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
13
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
14
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
15
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
16
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
17
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
18
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
19
// + des Mitverschuldens offen.
20
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
21
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
22
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
23
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
24
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
25
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
26
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
27
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Software LICENSING TERMS
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
32
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
33
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
34
// + The Software may only be used with the Licensor's products.
35
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
36
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
37
// + agreement shall be the property of the Licensor.
38
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
39
// + features that can be used to identify the program may not be altered or defaced by the customer.
40
// + The customer shall be responsible for taking reasonable precautions
41
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
42
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
43
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
44
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
45
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
46
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
47
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
48
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
49
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
50
// + #### END OF LICENSING TERMS ####
51
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
52
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
#include <stdarg.h>
54
#include <string.h>
55
#include <avr/pgmspace.h>
56
#include "main.h"
57
#include "uart.h"
58
#include "libfc.h"
59
#include "eeprom.h"
60
 
61
#define FC_ADDRESS 1
62
#define NC_ADDRESS 2
63
#define MK3MAG_ADDRESS 3
64
#define BL_CTRL_ADDRESS 5
65
 
66
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
67
#define MAX_SENDE_BUFF     175
68
#define MAX_EMPFANGS_BUFF  175
69
 
70
#define BLPARAM_REVISION 1
71
#define MASK_SET_PWM_SCALING            0x01
72
#define MASK_SET_CURRENT_LIMIT          0x02
73
#define MASK_SET_TEMP_LIMIT                     0x04
74
#define MASK_SET_CURRENT_SCALING        0x08
75
#define MASK_SET_BITCONFIG                      0x10
76
#define MASK_RESET_CAPCOUNTER           0x20
77
#define MASK_SET_DEFAULT_PARAMS         0x40
78
#define MASK_SET_SAVE_EEPROM            0x80
79
 
80
unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0;
81
unsigned char DisplayLine = 0;
82
unsigned volatile char SioTmp = 0;
83
unsigned volatile char NeuerDatensatzEmpfangen = 0;
84
unsigned volatile char NeueKoordinateEmpfangen = 0;
85
unsigned volatile char UebertragungAbgeschlossen = 1;
86
unsigned volatile char CntCrcError = 0;
87
unsigned volatile char AnzahlEmpfangsBytes = 0;
88
unsigned volatile char TxdBuffer[MAX_SENDE_BUFF];
89
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
90
 
91
unsigned char *pRxData = 0;
92
unsigned char RxDataLen = 0;
93
unsigned volatile char PC_DebugTimeout = 0;
94
unsigned volatile char PC_MotortestActive = 0;
95
unsigned char DebugTextAnforderung = 255;
96
 
97
unsigned char PcZugriff = 100;
98
unsigned char MotorTest[16];
99
unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl
100
unsigned char ConfirmFrame;
101
struct str_DebugOut    DebugOut;
102
struct str_ExternControl  ExternControl;
103
struct str_VersionInfo VersionInfo;
104
struct str_WinkelOut WinkelOut;
105
struct str_Data3D Data3D;
106
 
107
int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D;
108
unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0;
109
unsigned int AboTimeOut = 0;
110
unsigned volatile char ReceiverUpdateModeActive = 0; // 1 = Update      2 = JetiBox-Simulation
111
 
112
const unsigned char ANALOG_TEXT[32][16] PROGMEM =
113
{
114
   //1234567890123456
115
    "AngleNick       ", //0
116
    "AngleRoll       ", //1
117
    "AccNick         ", //2
118
    "AccRoll         ", //3
119
    "YawGyro         ", //4
120
    "Height Value    ", //5
121
    "AccZ            ", //6
122
    "Voltage [0.1V]  ", //7
123
    "Motor 1         ", //8
124
    "Motor 2         ", //9
125
    "Motor 3         ", //10
126
    "Motor 4         ", //11
127
    "Motor 5         ", //12
128
    "Motor 6         ", //13
129
    "Motor 7         ", //14
130
    "Motor 8         ", //15
131
    "Receiver Level  ", //16
132
    "Gyro Compass    ", //17
133
    "Current [0.1A]  ", //18
134
    "Capacity [mAh]  ", //19
135
    "Servo           ", //20
136
    "Compass Value   ", //21
137
    "I2C-Error       ", //22
138
    "23              ", //23
139
    "GPS_Nick        ", //24
140
    "GPS_Roll        ", //25
141
    "26              ", //26
142
    "27              ", //27
143
    "28              ", //28
144
    "29              ", //29
145
    "30              ", //30
146
    "31              "  //31
147
};
148
 
149
 
150
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
151
//++ Sende-Part der Datenübertragung
152
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
153
ISR(USART0_TX_vect)
154
{
155
 static unsigned int ptr = 0;
156
 unsigned char tmp_tx;
157
 
158
 if(!UebertragungAbgeschlossen)
159
  {
160
   ptr++;                    // die [0] wurde schon gesendet
161
   tmp_tx = TxdBuffer[ptr];
162
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
163
    {
164
     ptr = 0;
165
     UebertragungAbgeschlossen = 1;
166
    }
167
   UDR0 = tmp_tx;
168
  }
169
  else ptr = 0;
170
}
171
 
172
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
173
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
174
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
175
ISR(USART0_RX_vect)
176
{
177
 static unsigned int crc;
178
 static unsigned char crc1,crc2,buf_ptr;
179
 static unsigned char UartState = 0;
180
 unsigned char CrcOkay = 0;
181
 
182
 if (ReceiverUpdateModeActive == 1) {   UDR1 = UDR0; return; }          // 1 = Update      
183
 if (ReceiverUpdateModeActive == 2) {   RxdBuffer[0] = UDR0; return; }  // 2 = JetiBox-Simulation 
184
 
185
 SioTmp = UDR0;
186
 
187
 if(buf_ptr >= MAX_SENDE_BUFF)    UartState = 0;
188
 if(SioTmp == '\r' && UartState == 2)
189
  {
190
   UartState = 0;
191
   crc -= RxdBuffer[buf_ptr-2];
192
   crc -= RxdBuffer[buf_ptr-1];
193
   crc %= 4096;
194
   crc1 = '=' + crc / 64;
195
   crc2 = '=' + crc % 64;
196
   CrcOkay = 0;
197
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
198
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
199
    {
200
     NeuerDatensatzEmpfangen = 1;
201
         AnzahlEmpfangsBytes = buf_ptr + 1;
202
     RxdBuffer[buf_ptr] = '\r';
203
         if(RxdBuffer[2] == 'R')
204
          {
205
           LcdClear();
206
           wdt_enable(WDTO_250MS); // Reset-Commando
207
           ServoActive = 0;
208
          }
209
 
210
        }
211
  }
212
  else
213
  switch(UartState)
214
  {
215
   case 0:
216
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
217
                  buf_ptr = 0;
218
                  RxdBuffer[buf_ptr++] = SioTmp;
219
                  crc = SioTmp;
220
          break;
221
   case 1: // Adresse auswerten
222
                  UartState++;
223
                  RxdBuffer[buf_ptr++] = SioTmp;
224
                  crc += SioTmp;
225
                  break;
226
   case 2: //  Eingangsdaten sammeln
227
                  RxdBuffer[buf_ptr] = SioTmp;
228
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
229
                  else UartState = 0;
230
                  crc += SioTmp;
231
                  break;
232
   default:
233
          UartState = 0;
234
          break;
235
  }
236
}
237
 
238
 
239
// --------------------------------------------------------------------------
240
void AddCRC(unsigned int wieviele)
241
{
242
 unsigned int tmpCRC = 0,i;
243
 for(i = 0; i < wieviele;i++)
244
  {
245
   tmpCRC += TxdBuffer[i];
246
  }
247
   tmpCRC %= 4096;
248
   TxdBuffer[i++] = '=' + tmpCRC / 64;
249
   TxdBuffer[i++] = '=' + tmpCRC % 64;
250
   TxdBuffer[i++] = '\r';
251
  UebertragungAbgeschlossen = 0;
252
  UDR0 = TxdBuffer[0];
253
}
254
 
255
 
256
 
257
// --------------------------------------------------------------------------
258
void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len)
259
{
260
 va_list ap;
261
 unsigned int pt = 0;
262
 unsigned char a,b,c;
263
 unsigned char ptr = 0;
264
 
265
 unsigned char *snd = 0;
266
 int len = 0;
267
 
268
 TxdBuffer[pt++] = '#';                         // Startzeichen
269
 TxdBuffer[pt++] = 'a' + address;               // Adresse (a=0; b=1,...)
270
 TxdBuffer[pt++] = cmd;                         // Commando
271
 
272
 va_start(ap, BufferAnzahl);
273
 if(BufferAnzahl)
274
 {
275
                snd = va_arg(ap, unsigned char*);
276
                len = va_arg(ap, int);
277
                ptr = 0;
278
                BufferAnzahl--;
279
 }
280
 while(len)
281
  {
282
        if(len)
283
        {
284
           a = snd[ptr++];
285
           len--;
286
           if((!len) && BufferAnzahl)
287
                {
288
                        snd = va_arg(ap, unsigned char*);
289
                        len = va_arg(ap, int);
290
                        ptr = 0;
291
                        BufferAnzahl--;
292
                }
293
        }
294
        else a = 0;
295
        if(len)
296
        {
297
                b = snd[ptr++];
298
                len--;
299
                if((!len) && BufferAnzahl)
300
                {
301
                        snd = va_arg(ap, unsigned char*);
302
                        len = va_arg(ap, int);
303
                        ptr = 0;
304
                        BufferAnzahl--;
305
                }
306
        }
307
        else b = 0;
308
        if(len)
309
        {
310
                c = snd[ptr++];
311
                len--;
312
                if((!len) && BufferAnzahl)
313
                {
314
                        snd = va_arg(ap, unsigned char*);
315
                        len = va_arg(ap, int);
316
                        ptr = 0;
317
                        BufferAnzahl--;
318
                }
319
        }
320
        else c = 0;
321
   TxdBuffer[pt++] = '=' + (a >> 2);
322
   TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
323
   TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
324
   TxdBuffer[pt++] = '=' + ( c & 0x3f);
325
  }
326
 va_end(ap);
327
 AddCRC(pt);
328
}
329
 
330
// --------------------------------------------------------------------------
331
void Decode64(void)  // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden.
332
{
333
 unsigned char a,b,c,d;
334
 unsigned char x,y,z;
335
 unsigned char ptrIn = 3; // start at begin of data block
336
 unsigned char ptrOut = 3;
337
 unsigned char len = AnzahlEmpfangsBytes - 6; // von der Gesamtbytezahl eines Frames gehen 3 Bytes des Headers  ('#',Addr, Cmd) und 3 Bytes des Footers (CRC1, CRC2, '\r') ab.
338
 
339
 while(len)
340
  {
341
   a = RxdBuffer[ptrIn++] - '=';
342
   b = RxdBuffer[ptrIn++] - '=';
343
   c = RxdBuffer[ptrIn++] - '=';
344
   d = RxdBuffer[ptrIn++] - '=';
345
 
346
   x = (a << 2) | (b >> 4);
347
   y = ((b & 0x0f) << 4) | (c >> 2);
348
   z = ((c & 0x03) << 6) | d;
349
 
350
   if(len--) RxdBuffer[ptrOut++] = x; else break;
351
   if(len--) RxdBuffer[ptrOut++] = y; else break;
352
   if(len--) RxdBuffer[ptrOut++] = z;   else break;
353
  }
354
        pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte
355
        RxDataLen = ptrOut - 3;  // wie viele Bytes wurden dekodiert?
356
 
357
}
358
 
359
// --------------------------------------------------------------------------
360
void BearbeiteRxDaten(void)
361
{
362
 if(!NeuerDatensatzEmpfangen) return;
363
 
364
        unsigned char tempchar1, tempchar2;
365
        Decode64(); // dekodiere datenblock im Empfangsbuffer
366
        switch(RxdBuffer[1]-'a') // check for Slave Address
367
        {
368
                case FC_ADDRESS: // FC special commands
369
                switch(RxdBuffer[2])
370
                {
371
                        case 'K':// Kompasswert
372
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
373
//                                      KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180;
374
                                        break;
375
                        case 't':// Motortest
376
                                if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
377
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
378
                                        PC_MotortestActive = 240;
379
                                        //while(!UebertragungAbgeschlossen);
380
                                        //SendOutData('T', MeineSlaveAdresse, 0);
381
                                        PcZugriff = 255;
382
                                        break;
383
 
384
                        case 'n':// "Get Mixer
385
                                        while(!UebertragungAbgeschlossen);
386
                    SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer, sizeof(Mixer) - 1);
387
                                        Debug("Mixer lesen");
388
                                        break;
389
 
390
                        case 'm':// "Write Mixer
391
                    if(pRxData[0] == EEMIXER_REVISION)
392
                                        {
393
                       memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer) - 1);
394
                       MixerTable_WriteToEEProm();
395
                                           tempchar1 = 1;
396
                                           VersionInfo.HardwareError[1] &= ~FC_ERROR1_MIXER;
397
                                        }
398
                    else
399
                    {
400
                                                tempchar1 = 0;
401
                                        }
402
                                        while(!UebertragungAbgeschlossen);
403
                                        SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
404
                                        break;
405
 
406
                        case 'p': // get PPM Channels
407
                                        GetPPMChannelAnforderung = 1;
408
                                        PcZugriff = 255;
409
                                        break;
410
 
411
                        case 'q':// "Get"-Anforderung für Settings
412
                                        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
413
                                        if((10 <= pRxData[0]) && (pRxData[0] < 20))
414
                                        {
415
                                                tempchar1 = pRxData[0] - 10;
416
                                                if(tempchar1< 1) tempchar1 = 1; // limit to 1
417
                                                else if(tempchar1 > 5) tempchar1 = 5; // limit to 5
418
                                                SetDefaultParameter(tempchar1, 1);
419
                                        }
420
                                        else if((20 <= pRxData[0]) && (pRxData[0] < 30))
421
                                        {
422
                                                tempchar1 = pRxData[0] - 20;
423
                                                if(tempchar1< 1) tempchar1 = 1; // limit to 1
424
                                                else if(tempchar1 > 5) tempchar1 = 5; // limit to 5
425
                                                SetDefaultParameter(tempchar1, 0);
426
                                        }
427
                                        else
428
                                        {
429
                                                tempchar1 = pRxData[0];
430
                                                if(tempchar1 == 0xFF)
431
                                                {
432
                                                        tempchar1 = GetActiveParamSet();
433
                                                }
434
                                                if(tempchar1< 1) tempchar1 = 1; // limit to 1
435
                                                else if(tempchar1 > 5) tempchar1 = 5; // limit to 5
436
                                                // load requested parameter set
437
                                                ParamSet_ReadFromEEProm(tempchar1);
438
                                        }
439
                                        while(!UebertragungAbgeschlossen);
440
                                        SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1);
441
                                        Debug("Lese Setting %d", tempchar1);
442
 
443
                                        break;
444
 
445
                        case 's': // Parametersatz speichern
446
                                        if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION) && MotorenEin == 0) // check for setting to be in range
447
                                        {
448
                                                memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1);
449
                                                ParamSet_WriteToEEProm(pRxData[0]);
450
                                                Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
451
                                                Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
452
                                                tempchar1 = GetActiveParamSet();
453
                                        }
454
                                        else
455
                                        {
456
                                                tempchar1 = 0; // mark in response an invlid setting
457
                                        }
458
                                        while(!UebertragungAbgeschlossen);
459
                                        SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
460
                                        if(!MotorenEin) Piep(tempchar1,110);
461
                                        LipoDetection(0);
462
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
463
                                        break;
464
                        case 'f': // auf anderen Parametersatz umschalten
465
                                if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]);
466
                                        tempchar1 = GetActiveParamSet();
467
                                        while(!UebertragungAbgeschlossen);
468
                                        SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
469
                                        if(!MotorenEin) Piep(tempchar1,110);
470
                                        LipoDetection(0);
471
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
472
                                        break;
473
                        case 'y':// serial Potis
474
                                        PPM_in[13] = (signed char) pRxData[0]; PPM_in[14] = (signed char) pRxData[1]; PPM_in[15] = (signed char) pRxData[2]; PPM_in[16] = (signed char) pRxData[3];
475
                                        PPM_in[17] = (signed char) pRxData[4]; PPM_in[18] = (signed char) pRxData[5]; PPM_in[19] = (signed char) pRxData[6]; PPM_in[20] = (signed char) pRxData[7];
476
                                        PPM_in[21] = (signed char) pRxData[8]; PPM_in[22] = (signed char) pRxData[9]; PPM_in[23] = (signed char) pRxData[10]; PPM_in[24] = (signed char) pRxData[11];
477
                                        break;
478
 
479
                        case 'u': // request BL parameter
480
                                Debug("Reading BL %d", pRxData[0]);
481
                                // try to read BL configuration
482
                                tempchar2 = I2C_ReadBLConfig(pRxData[0]);
483
                                if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1;
484
                                else tempchar1 = 0;
485
                                while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
486
                                SendOutData('U', FC_ADDRESS, 4, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), &pRxData[0], 1, &BLConfig, sizeof(BLConfig_t));
487
                                break;
488
 
489
                        case 'w': // write BL parameter
490
                                Debug("Writing BL %d", pRxData[0]);
491
                                if(RxDataLen >= 1+sizeof(BLConfig_t))
492
                                {
493
                                        memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t));
494
                                        tempchar2 = I2C_WriteBLConfig(pRxData[0]);
495
                                        if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1;
496
                                        else tempchar1 = 0; // indicate error
497
                                        while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
498
                                        SendOutData('W', FC_ADDRESS,2, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2));
499
                                }
500
                                break;
501
                        case 'j':
502
                                if(MotorenEin) break;
503
                                tempchar1 = LIBFC_GetCPUType();
504
                                if((tempchar1 == CPU_ATMEGA644P) || (tempchar1 == CPU_ATMEGA1284P))
505
                                {
506
                                        uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/ (8 * 38400L) - 1);
507
 
508
                                        cli();
509
 
510
                                        // UART0 & UART1 disable RX and TX-Interrupt
511
                                        UCSR0B &= ~((1 << RXCIE0)|(1 << TXCIE0));
512
                                        UCSR1B &= ~((1 << RXCIE1)|(1 << TXCIE1));
513
 
514
                                        // UART0 & UART1 disable receiver and transmitter
515
                                        UCSR0B &= ~((1 << TXEN0) | (1 << RXEN0));
516
                                        UCSR1B &= ~((1 << TXEN1) | (1 << RXEN1));
517
 
518
                                        // UART0 & UART1 flush receive buffer explicit
519
                                        while ( UCSR1A & (1<<RXC1) ) UDR1;
520
                                        while ( UCSR0A & (1<<RXC0) ) UDR0;
521
 
522
 
523
                                        if(pRxData[0] == 1) ReceiverUpdateModeActive = 2;
524
                                        else
525
                                        {           // Jeti or HoTT update
526
//#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
527
                                                if(pRxData[0] == 100)   ubrr = (uint16_t) ((uint32_t) F_CPU/ (8 * 19200L) - 1);  // HoTT
528
//#endif
529
                                                ReceiverUpdateModeActive = 1;
530
                                                // UART0 & UART1 set baudrate
531
                                                UBRR1H = (uint8_t)(ubrr>>8);
532
                                                UBRR1L = (uint8_t)ubrr;
533
                                                UBRR0H = UBRR1H;
534
                                                UBRR0L = UBRR1L;
535
                                                // UART1 no parity
536
                                                UCSR1C &= ~(1 << UPM11);
537
                                                UCSR1C &= ~(1 << UPM10);
538
                                                // UART1 8-bit
539
                                                UCSR1B &= ~(1 << UCSZ12);
540
                                                UCSR1C |= (1 << UCSZ11);
541
                                                UCSR1C |= (1 << UCSZ10);
542
                                        }
543
                                        // UART0 & UART1 1 stop bit
544
                                        UCSR1C &= ~(1 << USBS1);
545
                                        UCSR0C &= ~(1 << USBS0);
546
                                        // UART1 clear 9th bit
547
                                        UCSR1B &= ~(1<<TXB81);
548
                                        // enable receiver and transmitter for UART0 and UART1
549
                                        UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
550
                                        UCSR1B |= (1 << TXEN1) | (1 << RXEN1);
551
                                        // enable RX-Interrupt for UART0 and UART1
552
                                        UCSR0B |= (1 << RXCIE0);
553
                                        UCSR1B |= (1 << RXCIE1);
554
                                        // disable other Interrupts
555
                                        TIMSK0 = 0;
556
                                        TIMSK1 = 0;
557
                                        TIMSK2 = 0;
558
 
559
                                        sei();
560
                                }
561
                                break;
562
 
563
                } // case FC_ADDRESS:
564
 
565
                default: // any Slave Address
566
 
567
                switch(RxdBuffer[2])
568
                {
569
                        // 't' comand placed here only for compatibility to BL
570
                        case 't':// Motortest
571
                                if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
572
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
573
                                        while(!UebertragungAbgeschlossen);
574
                                        SendOutData('T', MeineSlaveAdresse, 0);
575
                                        PC_MotortestActive = 250;
576
                                        PcZugriff = 255;
577
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
578
                                        break;
579
                        // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address
580
                        case 'K':// Kompasswert
581
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
582
//                                      KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180;
583
                                        break;
584
                        case 'a':// Texte der Analogwerte
585
                                        DebugTextAnforderung = pRxData[0];
586
                                        if (DebugTextAnforderung > 31) DebugTextAnforderung = 31;
587
                                        PcZugriff = 255;
588
                                        break;
589
                        case 'b':
590
                                        memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl));
591
                                        ConfirmFrame = ExternControl.Frame;
592
                                        PcZugriff = 255;
593
                                        break;
594
                        case 'c': // Poll the 3D-Data
595
                    if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);}
596
                                        Intervall3D = pRxData[0] * 10;
597
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
598
                                        break;
599
                        case 'd': // Poll the debug data
600
                                        PcZugriff = 255;
601
                                        DebugDataIntervall = (unsigned int)pRxData[0] * 10;
602
                                        if(DebugDataIntervall > 0) DebugDataAnforderung = 1;
603
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
604
                                        break;
605
 
606
                        case 'h':// x-1 Displayzeilen
607
                                PcZugriff = 255;
608
                                if((pRxData[0] & 0x80) == 0x00) // old format
609
                                        {
610
                                                DisplayLine = 2;
611
                                                Display_Interval = 0;
612
                                        }
613
                                        else // new format
614
                                        {
615
                                                RemoteKeys |= ~pRxData[0];
616
                                                Display_Interval = (unsigned int)pRxData[1] * 10;
617
                                                DisplayLine = 4;
618
                                                AboTimeOut = SetDelay(ABO_TIMEOUT);
619
                                        }
620
                                        DebugDisplayAnforderung = 1;
621
                                        break;
622
 
623
                        case 'l':// x-1 Displayzeilen
624
                                PcZugriff = 255;
625
                                        MenuePunkt = pRxData[0];
626
                                        DebugDisplayAnforderung1 = 1;
627
                                        break;
628
                        case 'v': // Version-Anforderung und Ausbaustufe
629
                                        GetVersionAnforderung = 1;
630
                                        break;
631
 
632
                        case 'g'://
633
                                        GetExternalControl = 1;
634
                                        break;
635
 
636
                        default:
637
                                //unsupported command received
638
                                break;
639
                }
640
                break; // default:
641
        }
642
        NeuerDatensatzEmpfangen = 0;
643
        pRxData = 0;
644
        RxDataLen = 0;
645
}
646
 
647
//############################################################################
648
//Routine für die Serielle Ausgabe
649
void uart_putchar (char c)
650
//############################################################################
651
{
652
        //Warten solange bis Zeichen gesendet wurde
653
        loop_until_bit_is_set(UCSR0A, UDRE0);
654
        //Ausgabe des Zeichens
655
        UDR0 = c;
656
}
657
 
658
 
659
//############################################################################
660
//INstallation der Seriellen Schnittstelle
661
void UART_Init (void)
662
//############################################################################
663
{
664
        unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1);
665
 
666
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
667
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
668
    // UART Double Speed (U2X)
669
        UCSR0A |= (1 << U2X0);
670
        // RX-Interrupt Freigabe
671
        UCSR0B |= (1 << RXCIE0);
672
        // TX-Interrupt Freigabe
673
        UCSR0B |= (1 << TXCIE0);
674
        // USART0 Baud Rate Register
675
        // set clock divider
676
        UBRR0H = (uint8_t)(ubrr >> 8);
677
        UBRR0L = (uint8_t)ubrr;
678
 
679
        Debug_Timer = SetDelay(DebugDataIntervall);
680
        Kompass_Timer = SetDelay(220);
681
 
682
        VersionInfo.SWMajor = VERSION_MAJOR;
683
        VersionInfo.SWMinor = VERSION_MINOR;
684
        VersionInfo.SWPatch = VERSION_PATCH;
685
        VersionInfo.ProtoMajor  = VERSION_SERIAL_MAJOR;
686
        VersionInfo.ProtoMinor  = VERSION_SERIAL_MINOR;
687
 
688
        pRxData = 0;
689
        RxDataLen = 0;
690
}
691
 
692
//---------------------------------------------------------------------------------------------
693
void DatenUebertragung(void)
694
{
695
        if(!UebertragungAbgeschlossen) return;
696
 
697
        if(CheckDelay(AboTimeOut))
698
        {
699
                Display_Interval = 0;
700
                DebugDataIntervall = 0;
701
                Intervall3D = 0;
702
        }
703
 
704
        if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen)
705
        {
706
                if(DisplayLine > 3)// new format
707
                {
708
                        Menu();
709
                        SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80);
710
                }
711
                else // old format
712
                {
713
                        LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!");
714
                        SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20);
715
                        if(DisplayLine++ > 3) DisplayLine = 0;
716
                }
717
                Display_Timer = SetDelay(Display_Interval);
718
                DebugDisplayAnforderung = 0;
719
        }
720
        if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen)
721
        {
722
                Menu();
723
                SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff));
724
                DebugDisplayAnforderung1 = 0;
725
        }
726
        if(GetVersionAnforderung && UebertragungAbgeschlossen)
727
        {
728
                SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo));
729
                GetVersionAnforderung = 0;
730
                Debug_OK("Version gesendet");
731
        }
732
 
733
        if(GetExternalControl && UebertragungAbgeschlossen)           // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
734
        {
735
                SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl));
736
                GetExternalControl = 0;
737
        }
738
    if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen)
739
         {
740
                  WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
741
                  WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
742
                  WinkelOut.UserParameter[0] = Parameter_UserParam1;
743
                  WinkelOut.UserParameter[1] = Parameter_UserParam2;
744
          SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut));
745
          if(WinkelOut.CalcState > 4)  WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt
746
          Kompass_Timer = SetDelay(99);
747
         }
748
    if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen)
749
         {
750
                  CopyDebugValues();
751
          SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut));
752
          DebugDataAnforderung = 0;
753
          if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall);
754
         }
755
    if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen)
756
         {
757
                  Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
758
                  Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
759
          Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR);
760
                  Data3D.Centroid[0] = SummeNick >> 9;
761
                  Data3D.Centroid[1] = SummeRoll >> 9;
762
                  Data3D.Centroid[2] = Mess_Integral_Gier >> 9;
763
          SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D));
764
          Timer3D = SetDelay(Intervall3D);
765
         }
766
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
767
     {
768
                unsigned char label[16]; // local sram buffer
769
                memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra
770
      SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16);
771
      DebugTextAnforderung = 255;
772
         }
773
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz bestätigen
774
         {
775
                SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
776
        ConfirmFrame = 0;
777
     }
778
 
779
     if(GetPPMChannelAnforderung && UebertragungAbgeschlossen)
780
     {
781
                 SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in));
782
                 GetPPMChannelAnforderung = 0;
783
         }
784
 
785
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
786
     if(SendDebugOutput && UebertragungAbgeschlossen)
787
     {
788
                 SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug));
789
                 SendDebugOutput = 0;
790
         }
791
#endif
792
 
793
}
794
 
795