Subversion Repositories FlightCtrl

Rev

Rev 1665 | Rev 1672 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1665 Rev 1668
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) Holger Buss, Ingo Busker
2
// + Copyright (c) Holger Buss, Ingo Busker
3
// + only for non-profit use
3
// + only for non-profit use
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
#include <stdarg.h>
7
#include <stdarg.h>
8
#include <string.h>
8
#include <string.h>
9
#include <avr/pgmspace.h>
9
#include <avr/pgmspace.h>
10
#include "main.h"
10
#include "main.h"
11
#include "uart.h"
11
#include "uart.h"
12
#include "libfc.h"
12
#include "libfc.h"
13
#include "eeprom.h"
13
#include "eeprom.h"
14
 
14
 
15
 
15
 
16
 
16
 
17
#define FC_ADDRESS 1
17
#define FC_ADDRESS 1
18
#define NC_ADDRESS 2
18
#define NC_ADDRESS 2
19
#define MK3MAG_ADDRESS 3
19
#define MK3MAG_ADDRESS 3
20
#define BL_CTRL_ADDRESS 5
20
#define BL_CTRL_ADDRESS 5
21
 
21
 
22
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
22
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
23
#define MAX_SENDE_BUFF     160
23
#define MAX_SENDE_BUFF     160
24
#define MAX_EMPFANGS_BUFF  160
24
#define MAX_EMPFANGS_BUFF  160
25
 
25
 
26
 
26
 
27
#define BLPARAM_REVISION 1
27
#define BLPARAM_REVISION 1
28
#define MASK_SET_PWM_SCALING            0x01
28
#define MASK_SET_PWM_SCALING            0x01
29
#define MASK_SET_CURRENT_LIMIT          0x02
29
#define MASK_SET_CURRENT_LIMIT          0x02
30
#define MASK_SET_TEMP_LIMIT                     0x04
30
#define MASK_SET_TEMP_LIMIT                     0x04
31
#define MASK_SET_CURRENT_SCALING        0x08
31
#define MASK_SET_CURRENT_SCALING        0x08
32
#define MASK_SET_BITCONFIG                      0x10
32
#define MASK_SET_BITCONFIG                      0x10
33
#define MASK_RESET_CAPCOUNTER           0x20
33
#define MASK_RESET_CAPCOUNTER           0x20
34
#define MASK_SET_DEFAULT_PARAMS         0x40
34
#define MASK_SET_DEFAULT_PARAMS         0x40
35
#define MASK_SET_SAVE_EEPROM            0x80
35
#define MASK_SET_SAVE_EEPROM            0x80
36
 
36
 
37
typedef struct
37
typedef struct
38
{
38
{
39
        unsigned char Revision;                 // revision of parameter structure
39
        unsigned char Revision;                 // revision of parameter structure
40
        unsigned char Address;                  // target address
40
        unsigned char Address;                  // target address
41
        unsigned char PwmScaling;               // maximum value of pwm setpoint
41
        unsigned char PwmScaling;               // maximum value of pwm setpoint
42
        unsigned char CurrentLimit;             // current limit in 1A steps
42
        unsigned char CurrentLimit;             // current limit in 1A steps
43
        unsigned char TemperatureLimit; // in °C
43
        unsigned char TemperatureLimit; // in °C
44
        unsigned char CurrentScaling;   // scaling factor for current measurement
44
        unsigned char CurrentScaling;   // scaling factor for current measurement
45
        unsigned char BitConfig;                // see defines above
45
        unsigned char BitConfig;                // see defines above
46
        unsigned char SetMask;                  // filter for active paramters
46
        unsigned char SetMask;                  // filter for active paramters
47
        unsigned char Checksum;                 // checksum for parameter sturcture
47
        unsigned char Checksum;                 // checksum for parameter sturcture
48
}  __attribute__((packed)) BLParameter_t;
48
}  __attribute__((packed)) BLParameter_t;
49
 
49
 
50
 
50
 
51
unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0;
51
unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0;
52
unsigned char DisplayLine = 0;
52
unsigned char DisplayLine = 0;
53
unsigned volatile char SioTmp = 0;
53
unsigned volatile char SioTmp = 0;
54
unsigned volatile char NeuerDatensatzEmpfangen = 0;
54
unsigned volatile char NeuerDatensatzEmpfangen = 0;
55
unsigned volatile char NeueKoordinateEmpfangen = 0;
55
unsigned volatile char NeueKoordinateEmpfangen = 0;
56
unsigned volatile char UebertragungAbgeschlossen = 1;
56
unsigned volatile char UebertragungAbgeschlossen = 1;
57
unsigned volatile char CntCrcError = 0;
57
unsigned volatile char CntCrcError = 0;
58
unsigned volatile char AnzahlEmpfangsBytes = 0;
58
unsigned volatile char AnzahlEmpfangsBytes = 0;
59
unsigned volatile char TxdBuffer[MAX_SENDE_BUFF];
59
unsigned volatile char TxdBuffer[MAX_SENDE_BUFF];
60
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
60
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
61
 
61
 
62
unsigned char *pRxData = 0;
62
unsigned char *pRxData = 0;
63
unsigned char RxDataLen = 0;
63
unsigned char RxDataLen = 0;
64
unsigned volatile char PC_DebugTimeout = 0;
64
unsigned volatile char PC_DebugTimeout = 0;
65
unsigned volatile char PC_MotortestActive = 0;
65
unsigned volatile char PC_MotortestActive = 0;
66
unsigned char DebugTextAnforderung = 255;
66
unsigned char DebugTextAnforderung = 255;
67
 
-
 
68
 
-
 
69
 
67
 
70
unsigned char PcZugriff = 100;
68
unsigned char PcZugriff = 100;
71
unsigned char MotorTest[16];
69
unsigned char MotorTest[16];
72
unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl
70
unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl
73
unsigned char ConfirmFrame;
71
unsigned char ConfirmFrame;
74
struct str_DebugOut    DebugOut;
72
struct str_DebugOut    DebugOut;
75
struct str_ExternControl  ExternControl;
73
struct str_ExternControl  ExternControl;
76
struct str_VersionInfo VersionInfo;
74
struct str_VersionInfo VersionInfo;
77
struct str_WinkelOut WinkelOut;
75
struct str_WinkelOut WinkelOut;
78
struct str_Data3D Data3D;
76
struct str_Data3D Data3D;
79
 
77
 
80
int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D;
78
int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D;
81
unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0;
79
unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0;
82
unsigned int AboTimeOut = 0;
80
unsigned int AboTimeOut = 0;
83
 
81
 
84
const unsigned char ANALOG_TEXT[32][16] PROGMEM =
82
const unsigned char ANALOG_TEXT[32][16] PROGMEM =
85
{
83
{
86
   //1234567890123456
84
   //1234567890123456
87
    "AngleNick       ", //0
85
    "AngleNick       ", //0
88
    "AngleRoll       ",
86
    "AngleRoll       ",
89
    "AccNick         ",
87
    "AccNick         ",
90
    "AccRoll         ",
88
    "AccRoll         ",
91
    "YawGyro         ",
89
    "YawGyro         ",
92
    "Height Value    ", //5
90
    "Height Value    ", //5
93
    "AccZ            ",
91
    "AccZ            ",
94
    "Gas             ",
92
    "Gas             ",
95
    "Compass Value   ",
93
    "Compass Value   ",
96
    "Voltage [0.1V]  ",
94
    "Voltage [0.1V]  ",
97
    "Receiver Level  ", //10
95
    "Receiver Level  ", //10
98
    "Gyro Compass    ",
96
    "Gyro Compass    ",
99
    "Motor 1         ",
97
    "Motor 1         ",
100
    "Motor 2         ",
98
    "Motor 2         ",
101
    "Motor 3         ",
99
    "Motor 3         ",
102
    "Motor 4         ", //15
100
    "Motor 4         ", //15
103
    "                ",
101
    "                ",
104
    "                ",
102
    "                ",
105
    "                ",
103
    "                ",
106
    "                ",
104
    "                ",
107
    "Servo           ", //20
105
    "Servo           ", //20
108
    "Hovergas        ",
106
    "Hovergas        ",
109
    "Current [0.1A]  ",
107
    "Current [0.1A]  ",
110
    "Capacity [mAh]  ",
108
    "Capacity [mAh]  ",
111
    "                ",
109
    "                ",
112
    "                ", //25
110
    "                ", //25
113
    "                ",
111
    "                ",
114
    "                ",
112
    "                ",
115
    "I2C-Error       ",
113
    "I2C-Error       ",
116
    "                ", //    "Navi Serial Data",
114
    "                ", //    "Navi Serial Data",
117
    "GPS_Nick        ", //30
115
    "GPS_Nick        ", //30
118
    "GPS_Roll        "
116
    "GPS_Roll        "
119
};
117
};
120
 
118
 
121
 
119
 
122
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
120
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123
//++ Sende-Part der Datenübertragung
121
//++ Sende-Part der Datenübertragung
124
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
ISR(USART0_TX_vect)
123
ISR(USART0_TX_vect)
126
{
124
{
127
 static unsigned int ptr = 0;
125
 static unsigned int ptr = 0;
128
 unsigned char tmp_tx;
126
 unsigned char tmp_tx;
129
 if(!UebertragungAbgeschlossen)
127
 if(!UebertragungAbgeschlossen)
130
  {
128
  {
131
   ptr++;                    // die [0] wurde schon gesendet
129
   ptr++;                    // die [0] wurde schon gesendet
132
   tmp_tx = TxdBuffer[ptr];
130
   tmp_tx = TxdBuffer[ptr];
133
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
131
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
134
    {
132
    {
135
     ptr = 0;
133
     ptr = 0;
136
     UebertragungAbgeschlossen = 1;
134
     UebertragungAbgeschlossen = 1;
137
    }
135
    }
138
   UDR0 = tmp_tx;
136
   UDR0 = tmp_tx;
139
  }
137
  }
140
  else ptr = 0;
138
  else ptr = 0;
141
}
139
}
142
 
140
 
143
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
141
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
144
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
142
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
145
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
143
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
146
ISR(USART0_RX_vect)
144
ISR(USART0_RX_vect)
147
{
145
{
148
 static unsigned int crc;
146
 static unsigned int crc;
149
 static unsigned char crc1,crc2,buf_ptr;
147
 static unsigned char crc1,crc2,buf_ptr;
150
 static unsigned char UartState = 0;
148
 static unsigned char UartState = 0;
151
 unsigned char CrcOkay = 0;
149
 unsigned char CrcOkay = 0;
152
 
150
 
153
 SioTmp = UDR0;
151
 SioTmp = UDR0;
154
 if(buf_ptr >= MAX_SENDE_BUFF)    UartState = 0;
152
 if(buf_ptr >= MAX_SENDE_BUFF)    UartState = 0;
155
 if(SioTmp == '\r' && UartState == 2)
153
 if(SioTmp == '\r' && UartState == 2)
156
  {
154
  {
157
   UartState = 0;
155
   UartState = 0;
158
   crc -= RxdBuffer[buf_ptr-2];
156
   crc -= RxdBuffer[buf_ptr-2];
159
   crc -= RxdBuffer[buf_ptr-1];
157
   crc -= RxdBuffer[buf_ptr-1];
160
   crc %= 4096;
158
   crc %= 4096;
161
   crc1 = '=' + crc / 64;
159
   crc1 = '=' + crc / 64;
162
   crc2 = '=' + crc % 64;
160
   crc2 = '=' + crc % 64;
163
   CrcOkay = 0;
161
   CrcOkay = 0;
164
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
162
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
165
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
163
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
166
    {
164
    {
167
     NeuerDatensatzEmpfangen = 1;
165
     NeuerDatensatzEmpfangen = 1;
168
         AnzahlEmpfangsBytes = buf_ptr + 1;
166
         AnzahlEmpfangsBytes = buf_ptr + 1;
169
     RxdBuffer[buf_ptr] = '\r';
167
     RxdBuffer[buf_ptr] = '\r';
170
         if(RxdBuffer[2] == 'R')
168
         if(RxdBuffer[2] == 'R')
171
          {
169
          {
172
           LcdClear();
170
           LcdClear();
173
           wdt_enable(WDTO_250MS); // Reset-Commando
171
           wdt_enable(WDTO_250MS); // Reset-Commando
174
           ServoActive = 0;
172
           ServoActive = 0;
175
 
173
 
176
          }
174
          }
177
        }
175
        }
178
  }
176
  }
179
  else
177
  else
180
  switch(UartState)
178
  switch(UartState)
181
  {
179
  {
182
   case 0:
180
   case 0:
183
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
181
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
184
                  buf_ptr = 0;
182
                  buf_ptr = 0;
185
                  RxdBuffer[buf_ptr++] = SioTmp;
183
                  RxdBuffer[buf_ptr++] = SioTmp;
186
                  crc = SioTmp;
184
                  crc = SioTmp;
187
          break;
185
          break;
188
   case 1: // Adresse auswerten
186
   case 1: // Adresse auswerten
189
                  UartState++;
187
                  UartState++;
190
                  RxdBuffer[buf_ptr++] = SioTmp;
188
                  RxdBuffer[buf_ptr++] = SioTmp;
191
                  crc += SioTmp;
189
                  crc += SioTmp;
192
                  break;
190
                  break;
193
   case 2: //  Eingangsdaten sammeln
191
   case 2: //  Eingangsdaten sammeln
194
                  RxdBuffer[buf_ptr] = SioTmp;
192
                  RxdBuffer[buf_ptr] = SioTmp;
195
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
193
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
196
                  else UartState = 0;
194
                  else UartState = 0;
197
                  crc += SioTmp;
195
                  crc += SioTmp;
198
                  break;
196
                  break;
199
   default:
197
   default:
200
          UartState = 0;
198
          UartState = 0;
201
          break;
199
          break;
202
  }
200
  }
203
}
201
}
204
 
202
 
205
 
203
 
206
// --------------------------------------------------------------------------
204
// --------------------------------------------------------------------------
207
void AddCRC(unsigned int wieviele)
205
void AddCRC(unsigned int wieviele)
208
{
206
{
209
 unsigned int tmpCRC = 0,i;
207
 unsigned int tmpCRC = 0,i;
210
 for(i = 0; i < wieviele;i++)
208
 for(i = 0; i < wieviele;i++)
211
  {
209
  {
212
   tmpCRC += TxdBuffer[i];
210
   tmpCRC += TxdBuffer[i];
213
  }
211
  }
214
   tmpCRC %= 4096;
212
   tmpCRC %= 4096;
215
   TxdBuffer[i++] = '=' + tmpCRC / 64;
213
   TxdBuffer[i++] = '=' + tmpCRC / 64;
216
   TxdBuffer[i++] = '=' + tmpCRC % 64;
214
   TxdBuffer[i++] = '=' + tmpCRC % 64;
217
   TxdBuffer[i++] = '\r';
215
   TxdBuffer[i++] = '\r';
218
  UebertragungAbgeschlossen = 0;
216
  UebertragungAbgeschlossen = 0;
219
  UDR0 = TxdBuffer[0];
217
  UDR0 = TxdBuffer[0];
220
}
218
}
221
 
219
 
222
 
220
 
223
 
221
 
224
// --------------------------------------------------------------------------
222
// --------------------------------------------------------------------------
225
void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len)
223
void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len)
226
{
224
{
227
 va_list ap;
225
 va_list ap;
228
 unsigned int pt = 0;
226
 unsigned int pt = 0;
229
 unsigned char a,b,c;
227
 unsigned char a,b,c;
230
 unsigned char ptr = 0;
228
 unsigned char ptr = 0;
231
 
229
 
232
 unsigned char *snd = 0;
230
 unsigned char *snd = 0;
233
 int len = 0;
231
 int len = 0;
234
 
232
 
235
 TxdBuffer[pt++] = '#';                         // Startzeichen
233
 TxdBuffer[pt++] = '#';                         // Startzeichen
236
 TxdBuffer[pt++] = 'a' + address;               // Adresse (a=0; b=1,...)
234
 TxdBuffer[pt++] = 'a' + address;               // Adresse (a=0; b=1,...)
237
 TxdBuffer[pt++] = cmd;                         // Commando
235
 TxdBuffer[pt++] = cmd;                         // Commando
238
 
236
 
239
 va_start(ap, BufferAnzahl);
237
 va_start(ap, BufferAnzahl);
240
 if(BufferAnzahl)
238
 if(BufferAnzahl)
241
 {
239
 {
242
                snd = va_arg(ap, unsigned char*);
240
                snd = va_arg(ap, unsigned char*);
243
                len = va_arg(ap, int);
241
                len = va_arg(ap, int);
244
                ptr = 0;
242
                ptr = 0;
245
                BufferAnzahl--;
243
                BufferAnzahl--;
246
 }
244
 }
247
 while(len)
245
 while(len)
248
  {
246
  {
249
        if(len)
247
        if(len)
250
        {
248
        {
251
           a = snd[ptr++];
249
           a = snd[ptr++];
252
           len--;
250
           len--;
253
           if((!len) && BufferAnzahl)
251
           if((!len) && BufferAnzahl)
254
                {
252
                {
255
                        snd = va_arg(ap, unsigned char*);
253
                        snd = va_arg(ap, unsigned char*);
256
                        len = va_arg(ap, int);
254
                        len = va_arg(ap, int);
257
                        ptr = 0;
255
                        ptr = 0;
258
                        BufferAnzahl--;
256
                        BufferAnzahl--;
259
                }
257
                }
260
        }
258
        }
261
        else a = 0;
259
        else a = 0;
262
        if(len)
260
        if(len)
263
        {
261
        {
264
                b = snd[ptr++];
262
                b = snd[ptr++];
265
                len--;
263
                len--;
266
                if((!len) && BufferAnzahl)
264
                if((!len) && BufferAnzahl)
267
                {
265
                {
268
                        snd = va_arg(ap, unsigned char*);
266
                        snd = va_arg(ap, unsigned char*);
269
                        len = va_arg(ap, int);
267
                        len = va_arg(ap, int);
270
                        ptr = 0;
268
                        ptr = 0;
271
                        BufferAnzahl--;
269
                        BufferAnzahl--;
272
                }
270
                }
273
        }
271
        }
274
        else b = 0;
272
        else b = 0;
275
        if(len)
273
        if(len)
276
        {
274
        {
277
                c = snd[ptr++];
275
                c = snd[ptr++];
278
                len--;
276
                len--;
279
                if((!len) && BufferAnzahl)
277
                if((!len) && BufferAnzahl)
280
                {
278
                {
281
                        snd = va_arg(ap, unsigned char*);
279
                        snd = va_arg(ap, unsigned char*);
282
                        len = va_arg(ap, int);
280
                        len = va_arg(ap, int);
283
                        ptr = 0;
281
                        ptr = 0;
284
                        BufferAnzahl--;
282
                        BufferAnzahl--;
285
                }
283
                }
286
        }
284
        }
287
        else c = 0;
285
        else c = 0;
288
   TxdBuffer[pt++] = '=' + (a >> 2);
286
   TxdBuffer[pt++] = '=' + (a >> 2);
289
   TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
287
   TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
290
   TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
288
   TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
291
   TxdBuffer[pt++] = '=' + ( c & 0x3f);
289
   TxdBuffer[pt++] = '=' + ( c & 0x3f);
292
  }
290
  }
293
 va_end(ap);
291
 va_end(ap);
294
 AddCRC(pt);
292
 AddCRC(pt);
295
}
293
}
296
 
294
 
297
// --------------------------------------------------------------------------
295
// --------------------------------------------------------------------------
298
void Decode64(void)  // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden.
296
void Decode64(void)  // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden.
299
{
297
{
300
 unsigned char a,b,c,d;
298
 unsigned char a,b,c,d;
301
 unsigned char x,y,z;
299
 unsigned char x,y,z;
302
 unsigned char ptrIn = 3; // start at begin of data block
300
 unsigned char ptrIn = 3; // start at begin of data block
303
 unsigned char ptrOut = 3;
301
 unsigned char ptrOut = 3;
304
 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.
302
 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.
305
 
303
 
306
 while(len)
304
 while(len)
307
  {
305
  {
308
   a = RxdBuffer[ptrIn++] - '=';
306
   a = RxdBuffer[ptrIn++] - '=';
309
   b = RxdBuffer[ptrIn++] - '=';
307
   b = RxdBuffer[ptrIn++] - '=';
310
   c = RxdBuffer[ptrIn++] - '=';
308
   c = RxdBuffer[ptrIn++] - '=';
311
   d = RxdBuffer[ptrIn++] - '=';
309
   d = RxdBuffer[ptrIn++] - '=';
312
 
310
 
313
   x = (a << 2) | (b >> 4);
311
   x = (a << 2) | (b >> 4);
314
   y = ((b & 0x0f) << 4) | (c >> 2);
312
   y = ((b & 0x0f) << 4) | (c >> 2);
315
   z = ((c & 0x03) << 6) | d;
313
   z = ((c & 0x03) << 6) | d;
316
 
314
 
317
   if(len--) RxdBuffer[ptrOut++] = x; else break;
315
   if(len--) RxdBuffer[ptrOut++] = x; else break;
318
   if(len--) RxdBuffer[ptrOut++] = y; else break;
316
   if(len--) RxdBuffer[ptrOut++] = y; else break;
319
   if(len--) RxdBuffer[ptrOut++] = z;   else break;
317
   if(len--) RxdBuffer[ptrOut++] = z;   else break;
320
  }
318
  }
321
        pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte
319
        pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte
322
        RxDataLen = ptrOut - 3;  // wie viele Bytes wurden dekodiert?
320
        RxDataLen = ptrOut - 3;  // wie viele Bytes wurden dekodiert?
323
 
321
 
324
}
322
}
325
 
323
 
326
// --------------------------------------------------------------------------
324
// --------------------------------------------------------------------------
327
void BearbeiteRxDaten(void)
325
void BearbeiteRxDaten(void)
328
{
326
{
329
 if(!NeuerDatensatzEmpfangen) return;
327
 if(!NeuerDatensatzEmpfangen) return;
330
 
328
 
331
        unsigned char tempchar1, tempchar2;
329
        unsigned char tempchar1, tempchar2;
332
        Decode64(); // dekodiere datenblock im Empfangsbuffer
330
        Decode64(); // dekodiere datenblock im Empfangsbuffer
333
        switch(RxdBuffer[1]-'a') // check for Slave Address
331
        switch(RxdBuffer[1]-'a') // check for Slave Address
334
        {
332
        {
335
                case FC_ADDRESS: // FC special commands
333
                case FC_ADDRESS: // FC special commands
336
                switch(RxdBuffer[2])
334
                switch(RxdBuffer[2])
337
                {
335
                {
338
                        case 'K':// Kompasswert
336
                        case 'K':// Kompasswert
339
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
337
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
340
                                        KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
338
                                        KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
341
                                        break;
339
                                        break;
342
                        case 't':// Motortest
340
                        case 't':// Motortest
343
                                if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
341
                                if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
344
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
342
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
345
                                        PC_MotortestActive = 240;
343
                                        PC_MotortestActive = 240;
346
                                        //while(!UebertragungAbgeschlossen);
344
                                        //while(!UebertragungAbgeschlossen);
347
                                        //SendOutData('T', MeineSlaveAdresse, 0);
345
                                        //SendOutData('T', MeineSlaveAdresse, 0);
348
                                        PcZugriff = 255;
346
                                        PcZugriff = 255;
349
                                        break;
347
                                        break;
350
 
348
 
351
                        case 'n':// "Get Mixer
349
                        case 'n':// "Get Mixer
352
                                        while(!UebertragungAbgeschlossen);
350
                                        while(!UebertragungAbgeschlossen);
353
                    SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer, sizeof(Mixer) - 1);
351
                    SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer, sizeof(Mixer) - 1);
354
                                        Debug("Mixer lesen");
352
                                        Debug("Mixer lesen");
355
                                        break;
353
                                        break;
356
 
354
 
357
                        case 'm':// "Write Mixer
355
                        case 'm':// "Write Mixer
358
                    if(pRxData[0] == EEMIXER_REVISION)
356
                    if(pRxData[0] == EEMIXER_REVISION)
359
                                        {
357
                                        {
360
                       memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer) - 1);
358
                       memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer) - 1);
361
                       MixerTable_WriteToEEProm();
359
                       MixerTable_WriteToEEProm();
362
                                           tempchar1 = 1;
360
                                           tempchar1 = 1;
363
                                        }
361
                                        }
364
                    else
362
                    else
365
                    {
363
                    {
366
                                                tempchar1 = 0;
364
                                                tempchar1 = 0;
367
                                        }
365
                                        }
368
                                        while(!UebertragungAbgeschlossen);
366
                                        while(!UebertragungAbgeschlossen);
369
                                        SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
367
                                        SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
370
 
368
 
371
                                        break;
369
                                        break;
372
 
370
 
373
                        case 'p': // get PPM Channels
371
                        case 'p': // get PPM Channels
374
                                        GetPPMChannelAnforderung = 1;
372
                                        GetPPMChannelAnforderung = 1;
375
                                        PcZugriff = 255;
373
                                        PcZugriff = 255;
376
                                        break;
374
                                        break;
377
 
375
 
378
                        case 'q':// "Get"-Anforderung für Settings
376
                        case 'q':// "Get"-Anforderung für Settings
379
                                        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
377
                                        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
380
                                        if(pRxData[0] == 0xFF)
378
                                        if(pRxData[0] == 0xFF)
381
                                        {
379
                                        {
382
                                                pRxData[0] = GetActiveParamSet();
380
                                                pRxData[0] = GetActiveParamSet();
383
                                        }
381
                                        }
384
                                        // limit settings range
382
                                        // limit settings range
385
                                        if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5
383
                                        if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5
386
                                        else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5
384
                                        else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5
387
                                        // load requested parameter set
385
                                        // load requested parameter set
388
                                        ParamSet_ReadFromEEProm(pRxData[0]);
386
                                        ParamSet_ReadFromEEProm(pRxData[0]);
389
                                        tempchar1 = pRxData[0];
387
                                        tempchar1 = pRxData[0];
390
                                        while(!UebertragungAbgeschlossen);
388
                                        while(!UebertragungAbgeschlossen);
391
                                        SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1);
389
                                        SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1);
392
                                        Debug("Lese Setting %d", tempchar1);
390
                                        Debug("Lese Setting %d", tempchar1);
393
 
391
 
394
                                        break;
392
                                        break;
395
 
393
 
396
                        case 's': // Parametersatz speichern
394
                        case 's': // Parametersatz speichern
397
                                        if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range
395
                                        if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range
398
                                        {
396
                                        {
399
                                                memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1);
397
                                                memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1);
400
                                                ParamSet_WriteToEEProm(pRxData[0]);
398
                                                ParamSet_WriteToEEProm(pRxData[0]);
401
                                                Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
399
                                                Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
402
                                                Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
400
                                                Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
403
                                                tempchar1 = GetActiveParamSet();
401
                                                tempchar1 = GetActiveParamSet();
404
                                        }
402
                                        }
405
                                        else
403
                                        else
406
                                        {
404
                                        {
407
                                                tempchar1 = 0; // mark in response an invlid setting
405
                                                tempchar1 = 0; // mark in response an invlid setting
408
                                        }
406
                                        }
409
                                        while(!UebertragungAbgeschlossen);
407
                                        while(!UebertragungAbgeschlossen);
410
                                        SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
408
                                        SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
411
                                        if(!MotorenEin) Piep(tempchar1,110);
409
                                        if(!MotorenEin) Piep(tempchar1,110);
412
                                        LipoDetection(0);
410
                                        LipoDetection(0);
413
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
411
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
414
                                        break;
412
                                        break;
415
                        case 'f': // auf anderen Parametersatz umschalten
413
                        case 'f': // auf anderen Parametersatz umschalten
416
                                if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]);
414
                                if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]);
417
                                        tempchar1 = GetActiveParamSet();
415
                                        tempchar1 = GetActiveParamSet();
418
                                        while(!UebertragungAbgeschlossen);
416
                                        while(!UebertragungAbgeschlossen);
419
                                        SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
417
                                        SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
420
                                        if(!MotorenEin) Piep(tempchar1,110);
418
                                        if(!MotorenEin) Piep(tempchar1,110);
421
                                        LipoDetection(0);
419
                                        LipoDetection(0);
422
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
420
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
423
                                        break;
421
                                        break;
424
                        case 'y':// serial Potis
422
                        case 'y':// serial Potis
425
                                        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];
423
                                        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];
426
                                        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];
424
                                        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];
427
                                        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];
425
                                        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];
428
                                        break;
426
                                        break;
429
 
427
 
430
                        case 'u': // request BL parameter
428
                        case 'u': // request BL parameter
431
                                Debug("Reading BL %d", pRxData[0]);
429
                                Debug("Reading BL %d", pRxData[0]);
432
                                // try to read BL configuration
430
                                // try to read BL configuration
433
                                tempchar2 = I2C_ReadBLConfig(pRxData[0]);
431
                                tempchar2 = I2C_ReadBLConfig(pRxData[0]);
434
                                if(tempchar2 == BLCONFIG_SUCCESS)
432
                                if(tempchar2 == BLCONFIG_SUCCESS)
435
                                {
433
                                {
436
                                        while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
434
                                        while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
437
                                        SendOutData('U', FC_ADDRESS, 2, pRxData[0], 1, &BLConfig, sizeof(BLConfig_t));
435
                                        SendOutData('U', FC_ADDRESS, 2, pRxData[0], 1, &BLConfig, sizeof(BLConfig_t));
438
                                }
436
                                }
439
                                break;
437
                                break;
440
 
438
 
441
                        case 'w': // write BL parameter
439
                        case 'w': // write BL parameter
442
                                Debug("Writing BL %d", pRxData[0]);
440
                                Debug("Writing BL %d", pRxData[0]);
443
                                if(RxDataLen >= 1+sizeof(BLConfig_t))
441
                                if(RxDataLen >= 1+sizeof(BLConfig_t))
444
                                {
442
                                {
445
                                        memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t));
443
                                        memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t));
446
                                        tempchar2 = I2C_WriteBLConfig(pRxData[0]);
444
                                        tempchar2 = I2C_WriteBLConfig(pRxData[0]);
447
                                        if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1;
445
                                        if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1;
448
                                        else tempchar1 = 0; // indicate error
446
                                        else tempchar1 = 0; // indicate error
449
                                        while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
447
                                        while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
450
                                        SendOutData('W', FC_ADDRESS,2, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2));
448
                                        SendOutData('W', FC_ADDRESS,2, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2));
451
                                }
449
                                }
452
                                break;
450
                                break;
453
 
451
 
454
                } // case FC_ADDRESS:
452
                } // case FC_ADDRESS:
455
 
453
 
456
                default: // any Slave Address
454
                default: // any Slave Address
457
 
455
 
458
                switch(RxdBuffer[2])
456
                switch(RxdBuffer[2])
459
                {
457
                {
460
                        // 't' comand placed here only for compatibility to BL
458
                        // 't' comand placed here only for compatibility to BL
461
                        case 't':// Motortest
459
                        case 't':// Motortest
462
                                if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
460
                                if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
463
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
461
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
464
                                        while(!UebertragungAbgeschlossen);
462
                                        while(!UebertragungAbgeschlossen);
465
                                        SendOutData('T', MeineSlaveAdresse, 0);
463
                                        SendOutData('T', MeineSlaveAdresse, 0);
466
                                        PC_MotortestActive = 250;
464
                                        PC_MotortestActive = 250;
467
                                        PcZugriff = 255;
465
                                        PcZugriff = 255;
468
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
466
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
469
                                        break;
467
                                        break;
470
                        // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address
468
                        // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address
471
                        case 'K':// Kompasswert
469
                        case 'K':// Kompasswert
472
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
470
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
473
                                        KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
471
                                        KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
474
                                        break;
472
                                        break;
475
                        case 'a':// Texte der Analogwerte
473
                        case 'a':// Texte der Analogwerte
476
                                        DebugTextAnforderung = pRxData[0];
474
                                        DebugTextAnforderung = pRxData[0];
477
                                        if (DebugTextAnforderung > 31) DebugTextAnforderung = 31;
475
                                        if (DebugTextAnforderung > 31) DebugTextAnforderung = 31;
478
                                        PcZugriff = 255;
476
                                        PcZugriff = 255;
479
                                        break;
477
                                        break;
480
                        case 'b':
478
                        case 'b':
481
                                        memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl));
479
                                        memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl));
482
                                        ConfirmFrame = ExternControl.Frame;
480
                                        ConfirmFrame = ExternControl.Frame;
483
                                        PcZugriff = 255;
481
                                        PcZugriff = 255;
484
                                        break;
482
                                        break;
485
                        case 'c': // Poll the 3D-Data
483
                        case 'c': // Poll the 3D-Data
486
                    if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);}
484
                    if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);}
487
                                        Intervall3D = pRxData[0] * 10;
485
                                        Intervall3D = pRxData[0] * 10;
488
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
486
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
489
                                        break;
487
                                        break;
490
                        case 'd': // Poll the debug data
488
                        case 'd': // Poll the debug data
491
                                        PcZugriff = 255;
489
                                        PcZugriff = 255;
492
                                        DebugDataIntervall = (unsigned int)pRxData[0] * 10;
490
                                        DebugDataIntervall = (unsigned int)pRxData[0] * 10;
493
                                        if(DebugDataIntervall > 0) DebugDataAnforderung = 1;
491
                                        if(DebugDataIntervall > 0) DebugDataAnforderung = 1;
494
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
492
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
495
                                        break;
493
                                        break;
496
 
494
 
497
                        case 'h':// x-1 Displayzeilen
495
                        case 'h':// x-1 Displayzeilen
498
                                PcZugriff = 255;
496
                                PcZugriff = 255;
499
                                if((pRxData[0] & 0x80) == 0x00) // old format
497
                                if((pRxData[0] & 0x80) == 0x00) // old format
500
                                        {
498
                                        {
501
                                                DisplayLine = 2;
499
                                                DisplayLine = 2;
502
                                                Display_Interval = 0;
500
                                                Display_Interval = 0;
503
                                        }
501
                                        }
504
                                        else // new format
502
                                        else // new format
505
                                        {
503
                                        {
506
                                                RemoteKeys |= ~pRxData[0];
504
                                                RemoteKeys |= ~pRxData[0];
507
                                                Display_Interval = (unsigned int)pRxData[1] * 10;
505
                                                Display_Interval = (unsigned int)pRxData[1] * 10;
508
                                                DisplayLine = 4;
506
                                                DisplayLine = 4;
509
                                                AboTimeOut = SetDelay(ABO_TIMEOUT);
507
                                                AboTimeOut = SetDelay(ABO_TIMEOUT);
510
                                        }
508
                                        }
511
                                        DebugDisplayAnforderung = 1;
509
                                        DebugDisplayAnforderung = 1;
512
                                        break;
510
                                        break;
513
 
511
 
514
                        case 'l':// x-1 Displayzeilen
512
                        case 'l':// x-1 Displayzeilen
515
                                PcZugriff = 255;
513
                                PcZugriff = 255;
516
                                        MenuePunkt = pRxData[0];
514
                                        MenuePunkt = pRxData[0];
517
                                        DebugDisplayAnforderung1 = 1;
515
                                        DebugDisplayAnforderung1 = 1;
518
                                        break;
516
                                        break;
519
                        case 'v': // Version-Anforderung und Ausbaustufe
517
                        case 'v': // Version-Anforderung und Ausbaustufe
520
                                        GetVersionAnforderung = 1;
518
                                        GetVersionAnforderung = 1;
521
                                        break;
519
                                        break;
522
 
520
 
523
                        case 'g'://
521
                        case 'g'://
524
                                        GetExternalControl = 1;
522
                                        GetExternalControl = 1;
525
                                        break;
523
                                        break;
526
 
524
 
527
                        default:
525
                        default:
528
                                //unsupported command received
526
                                //unsupported command received
529
                                break;
527
                                break;
530
                }
528
                }
531
                break; // default:
529
                break; // default:
532
        }
530
        }
533
        NeuerDatensatzEmpfangen = 0;
531
        NeuerDatensatzEmpfangen = 0;
534
        pRxData = 0;
532
        pRxData = 0;
535
        RxDataLen = 0;
533
        RxDataLen = 0;
536
}
534
}
537
 
535
 
538
//############################################################################
536
//############################################################################
539
//Routine für die Serielle Ausgabe
537
//Routine für die Serielle Ausgabe
540
void uart_putchar (char c)
538
void uart_putchar (char c)
541
//############################################################################
539
//############################################################################
542
{
540
{
543
        //Warten solange bis Zeichen gesendet wurde
541
        //Warten solange bis Zeichen gesendet wurde
544
        loop_until_bit_is_set(UCSR0A, UDRE0);
542
        loop_until_bit_is_set(UCSR0A, UDRE0);
545
        //Ausgabe des Zeichens
543
        //Ausgabe des Zeichens
546
        UDR0 = c;
544
        UDR0 = c;
547
}
545
}
548
 
546
 
549
 
547
 
550
//############################################################################
548
//############################################################################
551
//INstallation der Seriellen Schnittstelle
549
//INstallation der Seriellen Schnittstelle
552
void UART_Init (void)
550
void UART_Init (void)
553
//############################################################################
551
//############################################################################
554
{
552
{
555
        unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1);
553
        unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1);
556
 
554
 
557
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
555
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
558
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
556
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
559
    // UART Double Speed (U2X)
557
    // UART Double Speed (U2X)
560
        UCSR0A |= (1 << U2X0);
558
        UCSR0A |= (1 << U2X0);
561
        // RX-Interrupt Freigabe
559
        // RX-Interrupt Freigabe
562
        UCSR0B |= (1 << RXCIE0);
560
        UCSR0B |= (1 << RXCIE0);
563
        // TX-Interrupt Freigabe
561
        // TX-Interrupt Freigabe
564
        UCSR0B |= (1 << TXCIE0);
562
        UCSR0B |= (1 << TXCIE0);
565
        // USART0 Baud Rate Register
563
        // USART0 Baud Rate Register
566
        // set clock divider
564
        // set clock divider
567
        UBRR0H = (uint8_t)(ubrr >> 8);
565
        UBRR0H = (uint8_t)(ubrr >> 8);
568
        UBRR0L = (uint8_t)ubrr;
566
        UBRR0L = (uint8_t)ubrr;
569
 
567
 
570
        Debug_Timer = SetDelay(DebugDataIntervall);
568
        Debug_Timer = SetDelay(DebugDataIntervall);
571
        Kompass_Timer = SetDelay(220);
569
        Kompass_Timer = SetDelay(220);
572
 
570
 
573
        VersionInfo.SWMajor = VERSION_MAJOR;
571
        VersionInfo.SWMajor = VERSION_MAJOR;
574
        VersionInfo.SWMinor = VERSION_MINOR;
572
        VersionInfo.SWMinor = VERSION_MINOR;
575
        VersionInfo.SWPatch = VERSION_PATCH;
573
        VersionInfo.SWPatch = VERSION_PATCH;
576
        VersionInfo.ProtoMajor  = VERSION_SERIAL_MAJOR;
574
        VersionInfo.ProtoMajor  = VERSION_SERIAL_MAJOR;
577
        VersionInfo.ProtoMinor  = VERSION_SERIAL_MINOR;
575
        VersionInfo.ProtoMinor  = VERSION_SERIAL_MINOR;
578
 
576
 
579
        pRxData = 0;
577
        pRxData = 0;
580
        RxDataLen = 0;
578
        RxDataLen = 0;
581
}
579
}
582
 
580
 
583
//---------------------------------------------------------------------------------------------
581
//---------------------------------------------------------------------------------------------
584
void DatenUebertragung(void)
582
void DatenUebertragung(void)
585
{
583
{
586
        if(!UebertragungAbgeschlossen) return;
584
        if(!UebertragungAbgeschlossen) return;
587
 
585
 
588
        if(CheckDelay(AboTimeOut))
586
        if(CheckDelay(AboTimeOut))
589
        {
587
        {
590
                Display_Interval = 0;
588
                Display_Interval = 0;
591
                DebugDataIntervall = 0;
589
                DebugDataIntervall = 0;
592
                Intervall3D = 0;
590
                Intervall3D = 0;
593
        }
591
        }
594
 
592
 
595
        if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen)
593
        if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen)
596
        {
594
        {
597
                if(DisplayLine > 3)// new format
595
                if(DisplayLine > 3)// new format
598
                {
596
                {
599
                        Menu();
597
                        Menu();
600
                        SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80);
598
                        SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80);
601
                }
599
                }
602
                else // old format
600
                else // old format
603
                {
601
                {
604
                        LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!");
602
                        LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!");
605
                        SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20);
603
                        SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20);
606
                        if(DisplayLine++ > 3) DisplayLine = 0;
604
                        if(DisplayLine++ > 3) DisplayLine = 0;
607
                }
605
                }
608
                Display_Timer = SetDelay(Display_Interval);
606
                Display_Timer = SetDelay(Display_Interval);
609
                DebugDisplayAnforderung = 0;
607
                DebugDisplayAnforderung = 0;
610
        }
608
        }
611
        if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen)
609
        if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen)
612
        {
610
        {
613
                Menu();
611
                Menu();
614
                SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff));
612
                SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff));
615
                DebugDisplayAnforderung1 = 0;
613
                DebugDisplayAnforderung1 = 0;
616
        }
614
        }
617
        if(GetVersionAnforderung && UebertragungAbgeschlossen)
615
        if(GetVersionAnforderung && UebertragungAbgeschlossen)
618
        {
616
        {
619
                SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo));
617
                SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo));
620
                GetVersionAnforderung = 0;
618
                GetVersionAnforderung = 0;
621
                Debug_OK("Version gesendet");
619
                Debug_OK("Version gesendet");
622
        }
620
        }
623
 
621
 
624
        if(GetExternalControl && UebertragungAbgeschlossen)           // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
622
        if(GetExternalControl && UebertragungAbgeschlossen)           // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
625
        {
623
        {
626
                SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl));
624
                SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl));
627
                GetExternalControl = 0;
625
                GetExternalControl = 0;
628
        }
626
        }
629
    if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen)
627
    if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen)
630
         {
628
         {
631
                  WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
629
                  WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
632
                  WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
630
                  WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
633
                  WinkelOut.UserParameter[0] = Parameter_UserParam1;
631
                  WinkelOut.UserParameter[0] = Parameter_UserParam1;
634
                  WinkelOut.UserParameter[1] = Parameter_UserParam2;
632
                  WinkelOut.UserParameter[1] = Parameter_UserParam2;
635
          SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut));
633
          SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut));
636
          if(WinkelOut.CalcState > 4)  WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt
634
          if(WinkelOut.CalcState > 4)  WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt
637
          Kompass_Timer = SetDelay(99);
635
          Kompass_Timer = SetDelay(99);
638
         }
636
         }
639
    if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen)
637
    if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen)
640
         {
638
         {
641
                  CopyDebugValues();
639
                  CopyDebugValues();
642
          SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut));
640
          SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut));
643
          DebugDataAnforderung = 0;
641
          DebugDataAnforderung = 0;
644
          if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall);
642
          if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall);
645
         }
643
         }
646
    if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen)
644
    if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen)
647
         {
645
         {
648
                  Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
646
                  Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
649
                  Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
647
                  Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
650
          Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR);
648
          Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR);
651
          SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D));
649
          SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D));
652
          Timer3D = SetDelay(Intervall3D);
650
          Timer3D = SetDelay(Intervall3D);
653
         }
651
         }
654
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
652
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
655
     {
653
     {
656
                unsigned char label[16]; // local sram buffer
654
                unsigned char label[16]; // local sram buffer
657
                memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra
655
                memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra
658
      SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16);
656
      SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16);
659
      DebugTextAnforderung = 255;
657
      DebugTextAnforderung = 255;
660
         }
658
         }
661
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz bestätigen
659
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz bestätigen
662
         {
660
         {
663
                SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
661
                SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
664
        ConfirmFrame = 0;
662
        ConfirmFrame = 0;
665
     }
663
     }
666
 
664
 
667
     if(GetPPMChannelAnforderung && UebertragungAbgeschlossen)
665
     if(GetPPMChannelAnforderung && UebertragungAbgeschlossen)
668
     {
666
     {
669
                 SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in));
667
                 SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in));
670
                 GetPPMChannelAnforderung = 0;
668
                 GetPPMChannelAnforderung = 0;
671
         }
669
         }
672
 
670
 
673
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
671
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
674
     if(SendDebugOutput && UebertragungAbgeschlossen)
672
     if(SendDebugOutput && UebertragungAbgeschlossen)
675
     {
673
     {
676
                 SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug));
674
                 SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug));
677
                 SendDebugOutput = 0;
675
                 SendDebugOutput = 0;
678
         }
676
         }
679
#endif
677
#endif
680
 
678
 
681
}
679
}
682
 
680
 
683
 
681
 
684
 
682