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