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