Subversion Repositories FlightCtrl

Rev

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

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