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