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