Rev 1654 | Go to most recent revision | 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 | |
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 | |||
1622 | killagreg | 331 | unsigned char tempchar1; |
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 | |||
1056 | killagreg | 430 | } // case FC_ADDRESS: |
1054 | killagreg | 431 | |
432 | default: // any Slave Address |
||
1056 | killagreg | 433 | |
1054 | killagreg | 434 | switch(RxdBuffer[2]) |
435 | { |
||
436 | // 't' comand placed here only for compatibility to BL |
||
437 | case 't':// Motortest |
||
1654 | killagreg | 438 | if(AnzahlEmpfangsBytes > 10) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
1212 | hbuss | 439 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
1054 | killagreg | 440 | while(!UebertragungAbgeschlossen); |
441 | SendOutData('T', MeineSlaveAdresse, 0); |
||
1212 | hbuss | 442 | PC_MotortestActive = 250; |
1054 | killagreg | 443 | PcZugriff = 255; |
1638 | holgerb | 444 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1054 | killagreg | 445 | break; |
446 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
||
447 | case 'K':// Kompasswert |
||
448 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
449 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
450 | break; |
||
451 | case 'a':// Texte der Analogwerte |
||
452 | DebugTextAnforderung = pRxData[0]; |
||
453 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
||
454 | PcZugriff = 255; |
||
455 | break; |
||
456 | case 'b': |
||
457 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
||
458 | ConfirmFrame = ExternControl.Frame; |
||
459 | PcZugriff = 255; |
||
460 | break; |
||
1171 | hbuss | 461 | case 'c': // Poll the 3D-Data |
462 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
||
463 | Intervall3D = pRxData[0] * 10; |
||
1417 | killagreg | 464 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1171 | hbuss | 465 | break; |
1054 | killagreg | 466 | case 'd': // Poll the debug data |
1438 | ingob | 467 | PcZugriff = 255; |
1399 | killagreg | 468 | DebugDataIntervall = (unsigned int)pRxData[0] * 10; |
1171 | hbuss | 469 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
1415 | killagreg | 470 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1054 | killagreg | 471 | break; |
472 | |||
473 | case 'h':// x-1 Displayzeilen |
||
1059 | hbuss | 474 | PcZugriff = 255; |
1399 | killagreg | 475 | if((pRxData[0] & 0x80) == 0x00) // old format |
476 | { |
||
477 | DisplayLine = 2; |
||
478 | Display_Interval = 0; |
||
479 | } |
||
480 | else // new format |
||
481 | { |
||
482 | RemoteKeys |= ~pRxData[0]; |
||
483 | Display_Interval = (unsigned int)pRxData[1] * 10; |
||
484 | DisplayLine = 4; |
||
1415 | killagreg | 485 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1399 | killagreg | 486 | } |
1054 | killagreg | 487 | DebugDisplayAnforderung = 1; |
488 | break; |
||
489 | |||
490 | case 'l':// x-1 Displayzeilen |
||
1059 | hbuss | 491 | PcZugriff = 255; |
1054 | killagreg | 492 | MenuePunkt = pRxData[0]; |
493 | DebugDisplayAnforderung1 = 1; |
||
494 | break; |
||
495 | case 'v': // Version-Anforderung und Ausbaustufe |
||
496 | GetVersionAnforderung = 1; |
||
497 | break; |
||
498 | |||
499 | case 'g':// |
||
500 | GetExternalControl = 1; |
||
501 | break; |
||
1657 | killagreg | 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 | |||
566 | default: |
||
567 | //unsupported command received |
||
568 | break; |
||
1054 | killagreg | 569 | } |
570 | break; // default: |
||
571 | } |
||
1051 | killagreg | 572 | NeuerDatensatzEmpfangen = 0; |
573 | pRxData = 0; |
||
574 | RxDataLen = 0; |
||
1 | ingob | 575 | } |
576 | |||
577 | //############################################################################ |
||
578 | //Routine für die Serielle Ausgabe |
||
1444 | ingob | 579 | void uart_putchar (char c) |
1 | ingob | 580 | //############################################################################ |
581 | { |
||
582 | //Warten solange bis Zeichen gesendet wurde |
||
1561 | killagreg | 583 | loop_until_bit_is_set(UCSR0A, UDRE0); |
1 | ingob | 584 | //Ausgabe des Zeichens |
1561 | killagreg | 585 | UDR0 = c; |
1 | ingob | 586 | } |
587 | |||
588 | |||
589 | //############################################################################ |
||
590 | //INstallation der Seriellen Schnittstelle |
||
591 | void UART_Init (void) |
||
592 | //############################################################################ |
||
593 | { |
||
1561 | killagreg | 594 | unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1); |
595 | |||
1 | ingob | 596 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
1561 | killagreg | 597 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
1 | ingob | 598 | // UART Double Speed (U2X) |
1561 | killagreg | 599 | UCSR0A |= (1 << U2X0); |
1 | ingob | 600 | // RX-Interrupt Freigabe |
1561 | killagreg | 601 | UCSR0B |= (1 << RXCIE0); |
1 | ingob | 602 | // TX-Interrupt Freigabe |
1561 | killagreg | 603 | UCSR0B |= (1 << TXCIE0); |
604 | // USART0 Baud Rate Register |
||
605 | // set clock divider |
||
606 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
607 | UBRR0L = (uint8_t)ubrr; |
||
1 | ingob | 608 | |
1051 | killagreg | 609 | Debug_Timer = SetDelay(DebugDataIntervall); |
610 | Kompass_Timer = SetDelay(220); |
||
611 | |||
1058 | killagreg | 612 | VersionInfo.SWMajor = VERSION_MAJOR; |
613 | VersionInfo.SWMinor = VERSION_MINOR; |
||
614 | VersionInfo.SWPatch = VERSION_PATCH; |
||
615 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
616 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
1051 | killagreg | 617 | |
618 | pRxData = 0; |
||
619 | RxDataLen = 0; |
||
1 | ingob | 620 | } |
621 | |||
622 | //--------------------------------------------------------------------------------------------- |
||
1051 | killagreg | 623 | void DatenUebertragung(void) |
1 | ingob | 624 | { |
1415 | killagreg | 625 | if(!UebertragungAbgeschlossen) return; |
1 | ingob | 626 | |
1415 | killagreg | 627 | if(CheckDelay(AboTimeOut)) |
628 | { |
||
629 | Display_Interval = 0; |
||
630 | DebugDataIntervall = 0; |
||
1417 | killagreg | 631 | Intervall3D = 0; |
1415 | killagreg | 632 | } |
633 | |||
1399 | killagreg | 634 | if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen) |
1065 | killagreg | 635 | { |
1399 | killagreg | 636 | if(DisplayLine > 3)// new format |
637 | { |
||
1423 | hbuss | 638 | Menu(); |
1399 | killagreg | 639 | SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
640 | } |
||
641 | else // old format |
||
642 | { |
||
643 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
||
644 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
||
645 | if(DisplayLine++ > 3) DisplayLine = 0; |
||
646 | } |
||
647 | Display_Timer = SetDelay(Display_Interval); |
||
1065 | killagreg | 648 | DebugDisplayAnforderung = 0; |
649 | } |
||
650 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
||
651 | { |
||
652 | Menu(); |
||
653 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
||
654 | DebugDisplayAnforderung1 = 0; |
||
655 | } |
||
656 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
657 | { |
||
658 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
||
659 | GetVersionAnforderung = 0; |
||
1636 | ingob | 660 | Debug_OK("Version gesendet"); |
1065 | killagreg | 661 | } |
1 | ingob | 662 | |
1065 | killagreg | 663 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
664 | { |
||
665 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
||
666 | GetExternalControl = 0; |
||
667 | } |
||
1051 | killagreg | 668 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
693 | hbuss | 669 | { |
1171 | hbuss | 670 | 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 |
||
717 | hbuss | 672 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
673 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
||
1619 | killagreg | 674 | SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
855 | hbuss | 675 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
1051 | killagreg | 676 | Kompass_Timer = SetDelay(99); |
677 | } |
||
1171 | hbuss | 678 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
1 | ingob | 679 | { |
1639 | holgerb | 680 | CopyDebugValues(); |
1276 | hbuss | 681 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
1 | ingob | 682 | DebugDataAnforderung = 0; |
1055 | ingob | 683 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
1051 | killagreg | 684 | } |
1171 | hbuss | 685 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
686 | { |
||
687 | 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 |
||
689 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
||
690 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
||
691 | Timer3D = SetDelay(Intervall3D); |
||
692 | } |
||
499 | hbuss | 693 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
694 | { |
||
1490 | killagreg | 695 | unsigned char label[16]; // local sram buffer |
696 | 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); |
||
499 | hbuss | 698 | DebugTextAnforderung = 255; |
699 | } |
||
1051 | killagreg | 700 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
595 | hbuss | 701 | { |
1054 | killagreg | 702 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
1051 | killagreg | 703 | ConfirmFrame = 0; |
595 | hbuss | 704 | } |
1053 | killagreg | 705 | |
1051 | killagreg | 706 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
707 | { |
||
1054 | killagreg | 708 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
1051 | killagreg | 709 | GetPPMChannelAnforderung = 0; |
710 | } |
||
1654 | killagreg | 711 | |
1636 | ingob | 712 | #ifdef DEBUG // only include functions if DEBUG is defined |
713 | if(SendDebugOutput && UebertragungAbgeschlossen) |
||
714 | { |
||
715 | SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug)); |
||
716 | SendDebugOutput = 0; |
||
717 | } |
||
1654 | killagreg | 718 | #endif |
1 | ingob | 719 | |
720 | } |
||
721 | |||
1636 | ingob | 722 |