Subversion Repositories FlightCtrl

Rev

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

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