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