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 ", |
1761 | killagreg | 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; |
1765 | killagreg | 358 | VersionInfo.HardwareError[1] &= ~FC_ERROR1_MIXER; |
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 |
||
1761 | killagreg | 375 | if((10 <= pRxData[0]) && (pRxData[0] < 20)) |
1054 | killagreg | 376 | { |
1761 | killagreg | 377 | tempchar1 = pRxData[0] - 10; |
378 | if(tempchar1< 1) tempchar1 = 1; // limit to 1 |
||
379 | else if(tempchar1 > 5) tempchar1 = 5; // limit to 5 |
||
380 | SetDefaultParameter(tempchar1, 1); |
||
1054 | killagreg | 381 | } |
1761 | killagreg | 382 | else if((20 <= pRxData[0]) && (pRxData[0] < 30)) |
383 | { |
||
384 | tempchar1 = pRxData[0] - 20; |
||
385 | if(tempchar1< 1) tempchar1 = 1; // limit to 1 |
||
386 | else if(tempchar1 > 5) tempchar1 = 5; // limit to 5 |
||
387 | SetDefaultParameter(tempchar1, 0); |
||
388 | } |
||
389 | else |
||
390 | { |
||
391 | tempchar1 = pRxData[0]; |
||
392 | if(tempchar1 == 0xFF) |
||
393 | { |
||
394 | tempchar1 = GetActiveParamSet(); |
||
395 | } |
||
396 | if(tempchar1< 1) tempchar1 = 1; // limit to 1 |
||
397 | else if(tempchar1 > 5) tempchar1 = 5; // limit to 5 |
||
398 | // load requested parameter set |
||
399 | ParamSet_ReadFromEEProm(tempchar1); |
||
400 | } |
||
1054 | killagreg | 401 | while(!UebertragungAbgeschlossen); |
1622 | killagreg | 402 | SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1); |
1636 | ingob | 403 | Debug("Lese Setting %d", tempchar1); |
1654 | killagreg | 404 | |
1054 | killagreg | 405 | break; |
1051 | killagreg | 406 | |
1054 | killagreg | 407 | case 's': // Parametersatz speichern |
1622 | killagreg | 408 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range |
1054 | killagreg | 409 | { |
1622 | killagreg | 410 | memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1); |
411 | ParamSet_WriteToEEProm(pRxData[0]); |
||
1054 | killagreg | 412 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
413 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
1622 | killagreg | 414 | tempchar1 = GetActiveParamSet(); |
1054 | killagreg | 415 | } |
1058 | killagreg | 416 | else |
417 | { |
||
418 | tempchar1 = 0; // mark in response an invlid setting |
||
419 | } |
||
420 | while(!UebertragungAbgeschlossen); |
||
421 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
1405 | hbuss | 422 | if(!MotorenEin) Piep(tempchar1,110); |
423 | LipoDetection(0); |
||
1626 | killagreg | 424 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
1054 | killagreg | 425 | break; |
1405 | hbuss | 426 | case 'f': // auf anderen Parametersatz umschalten |
1622 | killagreg | 427 | if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]); |
428 | tempchar1 = GetActiveParamSet(); |
||
1405 | hbuss | 429 | while(!UebertragungAbgeschlossen); |
430 | SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
431 | if(!MotorenEin) Piep(tempchar1,110); |
||
432 | LipoDetection(0); |
||
1626 | killagreg | 433 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
1405 | hbuss | 434 | break; |
1391 | killagreg | 435 | case 'y':// serial Potis |
436 | 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]; |
||
437 | 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]; |
||
438 | 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]; |
||
439 | break; |
||
440 | |||
1662 | killagreg | 441 | case 'u': // request BL parameter |
442 | Debug("Reading BL %d", pRxData[0]); |
||
443 | // try to read BL configuration |
||
1665 | killagreg | 444 | tempchar2 = I2C_ReadBLConfig(pRxData[0]); |
1673 | killagreg | 445 | if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1; |
446 | else tempchar1 = 0; |
||
447 | while(!UebertragungAbgeschlossen); // wait for previous frame to be sent |
||
1674 | ingob | 448 | SendOutData('U', FC_ADDRESS, 4, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), &pRxData[0], 1, &BLConfig, sizeof(BLConfig_t)); |
1662 | killagreg | 449 | break; |
450 | |||
451 | case 'w': // write BL parameter |
||
452 | Debug("Writing BL %d", pRxData[0]); |
||
453 | if(RxDataLen >= 1+sizeof(BLConfig_t)) |
||
454 | { |
||
455 | memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t)); |
||
1665 | killagreg | 456 | tempchar2 = I2C_WriteBLConfig(pRxData[0]); |
457 | if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1; |
||
458 | else tempchar1 = 0; // indicate error |
||
1662 | killagreg | 459 | while(!UebertragungAbgeschlossen); // wait for previous frame to be sent |
1665 | killagreg | 460 | SendOutData('W', FC_ADDRESS,2, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2)); |
1662 | killagreg | 461 | } |
462 | break; |
||
463 | |||
1056 | killagreg | 464 | } // case FC_ADDRESS: |
1054 | killagreg | 465 | |
466 | default: // any Slave Address |
||
1056 | killagreg | 467 | |
1054 | killagreg | 468 | switch(RxdBuffer[2]) |
469 | { |
||
470 | // 't' comand placed here only for compatibility to BL |
||
471 | case 't':// Motortest |
||
1662 | killagreg | 472 | if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
1212 | hbuss | 473 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
1054 | killagreg | 474 | while(!UebertragungAbgeschlossen); |
475 | SendOutData('T', MeineSlaveAdresse, 0); |
||
1212 | hbuss | 476 | PC_MotortestActive = 250; |
1054 | killagreg | 477 | PcZugriff = 255; |
1638 | holgerb | 478 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1054 | killagreg | 479 | break; |
480 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
||
481 | case 'K':// Kompasswert |
||
482 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
483 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
484 | break; |
||
485 | case 'a':// Texte der Analogwerte |
||
486 | DebugTextAnforderung = pRxData[0]; |
||
487 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
||
488 | PcZugriff = 255; |
||
489 | break; |
||
490 | case 'b': |
||
491 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
||
492 | ConfirmFrame = ExternControl.Frame; |
||
493 | PcZugriff = 255; |
||
494 | break; |
||
1171 | hbuss | 495 | case 'c': // Poll the 3D-Data |
496 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
||
497 | Intervall3D = pRxData[0] * 10; |
||
1417 | killagreg | 498 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1171 | hbuss | 499 | break; |
1054 | killagreg | 500 | case 'd': // Poll the debug data |
1438 | ingob | 501 | PcZugriff = 255; |
1399 | killagreg | 502 | DebugDataIntervall = (unsigned int)pRxData[0] * 10; |
1171 | hbuss | 503 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
1415 | killagreg | 504 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1054 | killagreg | 505 | break; |
506 | |||
507 | case 'h':// x-1 Displayzeilen |
||
1059 | hbuss | 508 | PcZugriff = 255; |
1399 | killagreg | 509 | if((pRxData[0] & 0x80) == 0x00) // old format |
510 | { |
||
511 | DisplayLine = 2; |
||
512 | Display_Interval = 0; |
||
513 | } |
||
514 | else // new format |
||
515 | { |
||
516 | RemoteKeys |= ~pRxData[0]; |
||
517 | Display_Interval = (unsigned int)pRxData[1] * 10; |
||
518 | DisplayLine = 4; |
||
1415 | killagreg | 519 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1399 | killagreg | 520 | } |
1054 | killagreg | 521 | DebugDisplayAnforderung = 1; |
522 | break; |
||
523 | |||
524 | case 'l':// x-1 Displayzeilen |
||
1059 | hbuss | 525 | PcZugriff = 255; |
1054 | killagreg | 526 | MenuePunkt = pRxData[0]; |
527 | DebugDisplayAnforderung1 = 1; |
||
528 | break; |
||
529 | case 'v': // Version-Anforderung und Ausbaustufe |
||
530 | GetVersionAnforderung = 1; |
||
531 | break; |
||
532 | |||
533 | case 'g':// |
||
534 | GetExternalControl = 1; |
||
535 | break; |
||
1657 | killagreg | 536 | |
537 | default: |
||
538 | //unsupported command received |
||
539 | break; |
||
1054 | killagreg | 540 | } |
541 | break; // default: |
||
542 | } |
||
1051 | killagreg | 543 | NeuerDatensatzEmpfangen = 0; |
544 | pRxData = 0; |
||
545 | RxDataLen = 0; |
||
1 | ingob | 546 | } |
547 | |||
548 | //############################################################################ |
||
549 | //Routine für die Serielle Ausgabe |
||
1444 | ingob | 550 | void uart_putchar (char c) |
1 | ingob | 551 | //############################################################################ |
552 | { |
||
553 | //Warten solange bis Zeichen gesendet wurde |
||
1561 | killagreg | 554 | loop_until_bit_is_set(UCSR0A, UDRE0); |
1 | ingob | 555 | //Ausgabe des Zeichens |
1561 | killagreg | 556 | UDR0 = c; |
1 | ingob | 557 | } |
558 | |||
559 | |||
560 | //############################################################################ |
||
561 | //INstallation der Seriellen Schnittstelle |
||
562 | void UART_Init (void) |
||
563 | //############################################################################ |
||
564 | { |
||
1561 | killagreg | 565 | unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1); |
566 | |||
1 | ingob | 567 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
1561 | killagreg | 568 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
1 | ingob | 569 | // UART Double Speed (U2X) |
1561 | killagreg | 570 | UCSR0A |= (1 << U2X0); |
1 | ingob | 571 | // RX-Interrupt Freigabe |
1561 | killagreg | 572 | UCSR0B |= (1 << RXCIE0); |
1 | ingob | 573 | // TX-Interrupt Freigabe |
1561 | killagreg | 574 | UCSR0B |= (1 << TXCIE0); |
575 | // USART0 Baud Rate Register |
||
576 | // set clock divider |
||
577 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
578 | UBRR0L = (uint8_t)ubrr; |
||
1 | ingob | 579 | |
1051 | killagreg | 580 | Debug_Timer = SetDelay(DebugDataIntervall); |
581 | Kompass_Timer = SetDelay(220); |
||
582 | |||
1058 | killagreg | 583 | VersionInfo.SWMajor = VERSION_MAJOR; |
584 | VersionInfo.SWMinor = VERSION_MINOR; |
||
585 | VersionInfo.SWPatch = VERSION_PATCH; |
||
586 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
587 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
1051 | killagreg | 588 | |
589 | pRxData = 0; |
||
590 | RxDataLen = 0; |
||
1 | ingob | 591 | } |
592 | |||
593 | //--------------------------------------------------------------------------------------------- |
||
1051 | killagreg | 594 | void DatenUebertragung(void) |
1 | ingob | 595 | { |
1415 | killagreg | 596 | if(!UebertragungAbgeschlossen) return; |
1 | ingob | 597 | |
1415 | killagreg | 598 | if(CheckDelay(AboTimeOut)) |
599 | { |
||
600 | Display_Interval = 0; |
||
601 | DebugDataIntervall = 0; |
||
1417 | killagreg | 602 | Intervall3D = 0; |
1415 | killagreg | 603 | } |
604 | |||
1399 | killagreg | 605 | if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen) |
1065 | killagreg | 606 | { |
1399 | killagreg | 607 | if(DisplayLine > 3)// new format |
608 | { |
||
1423 | hbuss | 609 | Menu(); |
1399 | killagreg | 610 | SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
611 | } |
||
612 | else // old format |
||
613 | { |
||
614 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
||
615 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
||
616 | if(DisplayLine++ > 3) DisplayLine = 0; |
||
617 | } |
||
618 | Display_Timer = SetDelay(Display_Interval); |
||
1065 | killagreg | 619 | DebugDisplayAnforderung = 0; |
620 | } |
||
621 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
||
622 | { |
||
623 | Menu(); |
||
624 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
||
625 | DebugDisplayAnforderung1 = 0; |
||
626 | } |
||
627 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
628 | { |
||
629 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
||
630 | GetVersionAnforderung = 0; |
||
1636 | ingob | 631 | Debug_OK("Version gesendet"); |
1065 | killagreg | 632 | } |
1 | ingob | 633 | |
1065 | killagreg | 634 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
635 | { |
||
636 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
||
637 | GetExternalControl = 0; |
||
638 | } |
||
1051 | killagreg | 639 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
693 | hbuss | 640 | { |
1171 | hbuss | 641 | WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
642 | WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
717 | hbuss | 643 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
644 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
||
1619 | killagreg | 645 | SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
855 | hbuss | 646 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
1051 | killagreg | 647 | Kompass_Timer = SetDelay(99); |
648 | } |
||
1171 | hbuss | 649 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
1 | ingob | 650 | { |
1639 | holgerb | 651 | CopyDebugValues(); |
1276 | hbuss | 652 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
1 | ingob | 653 | DebugDataAnforderung = 0; |
1055 | ingob | 654 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
1051 | killagreg | 655 | } |
1171 | hbuss | 656 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
657 | { |
||
658 | Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
659 | Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
660 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
||
1685 | holgerb | 661 | Data3D.Centroid[0] = SummeNick >> 9; |
662 | Data3D.Centroid[1] = SummeRoll >> 9; |
||
663 | Data3D.Centroid[2] = Mess_Integral_Gier >> 9; |
||
1171 | hbuss | 664 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
665 | Timer3D = SetDelay(Intervall3D); |
||
666 | } |
||
499 | hbuss | 667 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
668 | { |
||
1490 | killagreg | 669 | unsigned char label[16]; // local sram buffer |
670 | memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra |
||
671 | SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16); |
||
499 | hbuss | 672 | DebugTextAnforderung = 255; |
673 | } |
||
1051 | killagreg | 674 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
595 | hbuss | 675 | { |
1054 | killagreg | 676 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
1051 | killagreg | 677 | ConfirmFrame = 0; |
595 | hbuss | 678 | } |
1053 | killagreg | 679 | |
1051 | killagreg | 680 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
681 | { |
||
1054 | killagreg | 682 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
1051 | killagreg | 683 | GetPPMChannelAnforderung = 0; |
684 | } |
||
1654 | killagreg | 685 | |
1636 | ingob | 686 | #ifdef DEBUG // only include functions if DEBUG is defined |
687 | if(SendDebugOutput && UebertragungAbgeschlossen) |
||
688 | { |
||
689 | SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug)); |
||
690 | SendDebugOutput = 0; |
||
691 | } |
||
1654 | killagreg | 692 | #endif |
1 | ingob | 693 | |
694 | } |
||
695 | |||
1636 | ingob | 696 |