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