Rev 1444 | 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" |
1 | ingob | 13 | |
1054 | killagreg | 14 | |
15 | #define FC_ADDRESS 1 |
||
16 | #define NC_ADDRESS 2 |
||
17 | #define MK3MAG_ADDRESS 3 |
||
18 | |||
1415 | killagreg | 19 | #define ABO_TIMEOUT 4000 // disable abo after 4 seconds |
1441 | ingob | 20 | #define MAX_SENDE_BUFF 160 |
21 | #define MAX_EMPFANGS_BUFF 160 |
||
1053 | killagreg | 22 | unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0; |
23 | unsigned char DisplayLine = 0; |
||
1 | ingob | 24 | unsigned volatile char SioTmp = 0; |
25 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
||
26 | unsigned volatile char NeueKoordinateEmpfangen = 0; |
||
27 | unsigned volatile char UebertragungAbgeschlossen = 1; |
||
28 | unsigned volatile char CntCrcError = 0; |
||
29 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
||
1441 | ingob | 30 | unsigned volatile char TxdBuffer[MAX_SENDE_BUFF]; |
31 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
||
32 | |||
1051 | killagreg | 33 | unsigned char *pRxData = 0; |
34 | unsigned char RxDataLen = 0; |
||
1 | ingob | 35 | unsigned volatile char PC_DebugTimeout = 0; |
1212 | hbuss | 36 | unsigned volatile char PC_MotortestActive = 0; |
1051 | killagreg | 37 | |
499 | hbuss | 38 | unsigned char DebugTextAnforderung = 255; |
1 | ingob | 39 | unsigned char PcZugriff = 100; |
1212 | hbuss | 40 | unsigned char MotorTest[16]; |
1036 | hbuss | 41 | unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl |
595 | hbuss | 42 | unsigned char ConfirmFrame; |
1 | ingob | 43 | struct str_DebugOut DebugOut; |
595 | hbuss | 44 | struct str_ExternControl ExternControl; |
1 | ingob | 45 | struct str_VersionInfo VersionInfo; |
693 | hbuss | 46 | struct str_WinkelOut WinkelOut; |
1171 | hbuss | 47 | struct str_Data3D Data3D; |
1 | ingob | 48 | |
1399 | killagreg | 49 | int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D; |
1415 | killagreg | 50 | unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0; |
51 | unsigned int AboTimeOut = 0; |
||
693 | hbuss | 52 | |
1490 | killagreg | 53 | const unsigned char ANALOG_TEXT[32][16] PROGMEM = |
499 | hbuss | 54 | { |
1051 | killagreg | 55 | //1234567890123456 |
1175 | hbuss | 56 | "AngleNick ", //0 |
57 | "AngleRoll ", |
||
1051 | killagreg | 58 | "AccNick ", |
499 | hbuss | 59 | "AccRoll ", |
60 | "GyroGier ", |
||
1175 | hbuss | 61 | "Hight Value ", //5 |
499 | hbuss | 62 | "AccZ ", |
63 | "Gas ", |
||
1175 | hbuss | 64 | "Compass Value ", |
65 | "Voltage ", |
||
513 | hbuss | 66 | "Empfang ", //10 |
1175 | hbuss | 67 | "Gyro Kompass ", |
68 | "Motor Front ", |
||
69 | "Motor Rear ", |
||
70 | "Motor Left ", |
||
71 | "Motor Right ", //15 |
||
929 | hbuss | 72 | " ", |
1175 | hbuss | 73 | " ", |
1253 | killagreg | 74 | "VarioMeter ", |
819 | hbuss | 75 | "MK3Mag CalState ", |
854 | hbuss | 76 | "Servo ", //20 |
1322 | hbuss | 77 | "Hoovergas ", |
720 | ingob | 78 | " ", |
1322 | hbuss | 79 | " ", |
80 | " ", |
||
81 | " ", //25 |
||
82 | " ", |
||
83 | " ", |
||
84 | "I2C-Error ", |
||
1352 | hbuss | 85 | " ",// "Navi Serial Data", |
720 | ingob | 86 | "GPS_Nick ", //30 |
87 | "GPS_Roll " |
||
499 | hbuss | 88 | }; |
89 | |||
90 | |||
91 | |||
1 | ingob | 92 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
93 | //++ Sende-Part der Datenübertragung |
||
94 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
95 | SIGNAL(INT_VEC_TX) |
||
96 | { |
||
97 | static unsigned int ptr = 0; |
||
98 | unsigned char tmp_tx; |
||
1051 | killagreg | 99 | if(!UebertragungAbgeschlossen) |
1 | ingob | 100 | { |
101 | ptr++; // die [0] wurde schon gesendet |
||
1438 | ingob | 102 | tmp_tx = TxdBuffer[ptr]; |
1 | ingob | 103 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
104 | { |
||
105 | ptr = 0; |
||
106 | UebertragungAbgeschlossen = 1; |
||
107 | } |
||
1051 | killagreg | 108 | UDR = tmp_tx; |
109 | } |
||
1 | ingob | 110 | else ptr = 0; |
111 | } |
||
112 | |||
113 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
114 | //++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung |
||
115 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
116 | SIGNAL(INT_VEC_RX) |
||
117 | { |
||
118 | static unsigned int crc; |
||
119 | static unsigned char crc1,crc2,buf_ptr; |
||
120 | static unsigned char UartState = 0; |
||
121 | unsigned char CrcOkay = 0; |
||
122 | |||
1051 | killagreg | 123 | SioTmp = UDR; |
1438 | ingob | 124 | if(buf_ptr >= MAX_SENDE_BUFF) UartState = 0; |
1051 | killagreg | 125 | if(SioTmp == '\r' && UartState == 2) |
1 | ingob | 126 | { |
127 | UartState = 0; |
||
128 | crc -= RxdBuffer[buf_ptr-2]; |
||
129 | crc -= RxdBuffer[buf_ptr-1]; |
||
130 | crc %= 4096; |
||
131 | crc1 = '=' + crc / 64; |
||
132 | crc2 = '=' + crc % 64; |
||
133 | CrcOkay = 0; |
||
134 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
||
135 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
||
136 | { |
||
1051 | killagreg | 137 | NeuerDatensatzEmpfangen = 1; |
138 | AnzahlEmpfangsBytes = buf_ptr + 1; |
||
1 | ingob | 139 | RxdBuffer[buf_ptr] = '\r'; |
1253 | killagreg | 140 | if(RxdBuffer[2] == 'R') |
1232 | hbuss | 141 | { |
1435 | killagreg | 142 | LcdClear(); |
1232 | hbuss | 143 | wdt_enable(WDTO_250MS); // Reset-Commando |
144 | ServoActive = 0; |
||
1435 | killagreg | 145 | |
1253 | killagreg | 146 | } |
1051 | killagreg | 147 | } |
1 | ingob | 148 | } |
149 | else |
||
150 | switch(UartState) |
||
151 | { |
||
152 | case 0: |
||
153 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
||
154 | buf_ptr = 0; |
||
155 | RxdBuffer[buf_ptr++] = SioTmp; |
||
156 | crc = SioTmp; |
||
157 | break; |
||
158 | case 1: // Adresse auswerten |
||
159 | UartState++; |
||
160 | RxdBuffer[buf_ptr++] = SioTmp; |
||
161 | crc += SioTmp; |
||
162 | break; |
||
163 | case 2: // Eingangsdaten sammeln |
||
164 | RxdBuffer[buf_ptr] = SioTmp; |
||
1490 | killagreg | 165 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
1 | ingob | 166 | else UartState = 0; |
167 | crc += SioTmp; |
||
168 | break; |
||
1051 | killagreg | 169 | default: |
170 | UartState = 0; |
||
1 | ingob | 171 | break; |
172 | } |
||
173 | } |
||
174 | |||
175 | |||
176 | // -------------------------------------------------------------------------- |
||
177 | void AddCRC(unsigned int wieviele) |
||
178 | { |
||
1051 | killagreg | 179 | unsigned int tmpCRC = 0,i; |
1 | ingob | 180 | for(i = 0; i < wieviele;i++) |
181 | { |
||
1438 | ingob | 182 | tmpCRC += TxdBuffer[i]; |
1 | ingob | 183 | } |
184 | tmpCRC %= 4096; |
||
1438 | ingob | 185 | TxdBuffer[i++] = '=' + tmpCRC / 64; |
186 | TxdBuffer[i++] = '=' + tmpCRC % 64; |
||
187 | TxdBuffer[i++] = '\r'; |
||
1 | ingob | 188 | UebertragungAbgeschlossen = 0; |
1438 | ingob | 189 | UDR = TxdBuffer[0]; |
1 | ingob | 190 | } |
191 | |||
192 | |||
193 | |||
194 | // -------------------------------------------------------------------------- |
||
1056 | killagreg | 195 | void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len) |
1 | ingob | 196 | { |
1051 | killagreg | 197 | va_list ap; |
1 | ingob | 198 | unsigned int pt = 0; |
199 | unsigned char a,b,c; |
||
200 | unsigned char ptr = 0; |
||
201 | |||
1051 | killagreg | 202 | unsigned char *snd = 0; |
203 | int len = 0; |
||
204 | |||
1438 | ingob | 205 | TxdBuffer[pt++] = '#'; // Startzeichen |
206 | TxdBuffer[pt++] = 'a' + address; // Adresse (a=0; b=1,...) |
||
207 | TxdBuffer[pt++] = cmd; // Commando |
||
1 | ingob | 208 | |
1051 | killagreg | 209 | va_start(ap, BufferAnzahl); |
210 | if(BufferAnzahl) |
||
211 | { |
||
212 | snd = va_arg(ap, unsigned char*); |
||
213 | len = va_arg(ap, int); |
||
214 | ptr = 0; |
||
215 | BufferAnzahl--; |
||
216 | } |
||
1 | ingob | 217 | while(len) |
218 | { |
||
1051 | killagreg | 219 | if(len) |
220 | { |
||
221 | a = snd[ptr++]; |
||
222 | len--; |
||
223 | if((!len) && BufferAnzahl) |
||
224 | { |
||
225 | snd = va_arg(ap, unsigned char*); |
||
226 | len = va_arg(ap, int); |
||
227 | ptr = 0; |
||
228 | BufferAnzahl--; |
||
229 | } |
||
230 | } |
||
231 | else a = 0; |
||
232 | if(len) |
||
233 | { |
||
234 | b = snd[ptr++]; |
||
235 | len--; |
||
236 | if((!len) && BufferAnzahl) |
||
237 | { |
||
238 | snd = va_arg(ap, unsigned char*); |
||
239 | len = va_arg(ap, int); |
||
240 | ptr = 0; |
||
241 | BufferAnzahl--; |
||
242 | } |
||
243 | } |
||
244 | else b = 0; |
||
245 | if(len) |
||
246 | { |
||
247 | c = snd[ptr++]; |
||
248 | len--; |
||
249 | if((!len) && BufferAnzahl) |
||
250 | { |
||
251 | snd = va_arg(ap, unsigned char*); |
||
252 | len = va_arg(ap, int); |
||
253 | ptr = 0; |
||
254 | BufferAnzahl--; |
||
255 | } |
||
256 | } |
||
257 | else c = 0; |
||
1438 | ingob | 258 | TxdBuffer[pt++] = '=' + (a >> 2); |
259 | TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
260 | TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
261 | TxdBuffer[pt++] = '=' + ( c & 0x3f); |
||
1 | ingob | 262 | } |
1051 | killagreg | 263 | va_end(ap); |
1 | ingob | 264 | AddCRC(pt); |
265 | } |
||
266 | |||
267 | // -------------------------------------------------------------------------- |
||
1051 | killagreg | 268 | void Decode64(void) // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden. |
1 | ingob | 269 | { |
270 | unsigned char a,b,c,d; |
||
271 | unsigned char x,y,z; |
||
1051 | killagreg | 272 | unsigned char ptrIn = 3; // start at begin of data block |
273 | unsigned char ptrOut = 3; |
||
274 | 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. |
||
275 | |||
1 | ingob | 276 | while(len) |
277 | { |
||
278 | a = RxdBuffer[ptrIn++] - '='; |
||
279 | b = RxdBuffer[ptrIn++] - '='; |
||
280 | c = RxdBuffer[ptrIn++] - '='; |
||
281 | d = RxdBuffer[ptrIn++] - '='; |
||
282 | |||
283 | x = (a << 2) | (b >> 4); |
||
284 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
285 | z = ((c & 0x03) << 6) | d; |
||
286 | |||
1051 | killagreg | 287 | if(len--) RxdBuffer[ptrOut++] = x; else break; |
288 | if(len--) RxdBuffer[ptrOut++] = y; else break; |
||
289 | if(len--) RxdBuffer[ptrOut++] = z; else break; |
||
1 | ingob | 290 | } |
1051 | killagreg | 291 | pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte |
292 | RxDataLen = ptrOut - 3; // wie viele Bytes wurden dekodiert? |
||
1 | ingob | 293 | |
294 | } |
||
295 | |||
296 | // -------------------------------------------------------------------------- |
||
297 | void BearbeiteRxDaten(void) |
||
298 | { |
||
299 | if(!NeuerDatensatzEmpfangen) return; |
||
300 | |||
1058 | killagreg | 301 | unsigned char tempchar1, tempchar2; |
1056 | killagreg | 302 | Decode64(); // dekodiere datenblock im Empfangsbuffer |
1055 | ingob | 303 | switch(RxdBuffer[1]-'a') // check for Slave Address |
1054 | killagreg | 304 | { |
305 | case FC_ADDRESS: // FC special commands |
||
306 | switch(RxdBuffer[2]) |
||
307 | { |
||
308 | case 'K':// Kompasswert |
||
309 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
310 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
311 | break; |
||
312 | case 't':// Motortest |
||
1212 | hbuss | 313 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
314 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
||
315 | PC_MotortestActive = 240; |
||
1065 | killagreg | 316 | //while(!UebertragungAbgeschlossen); |
317 | //SendOutData('T', MeineSlaveAdresse, 0); |
||
1054 | killagreg | 318 | PcZugriff = 255; |
319 | break; |
||
1053 | killagreg | 320 | |
1209 | hbuss | 321 | case 'n':// "Get Mixer |
322 | while(!UebertragungAbgeschlossen); |
||
323 | SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer,sizeof(Mixer)); |
||
324 | break; |
||
325 | |||
326 | case 'm':// "Write Mixer |
||
327 | while(!UebertragungAbgeschlossen); |
||
1211 | hbuss | 328 | if(pRxData[0] == MIXER_REVISION) |
329 | { |
||
330 | memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer)); |
||
331 | eeprom_write_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer)); |
||
332 | tempchar1 = 1; |
||
333 | } |
||
334 | else tempchar1 = 0; |
||
335 | SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
1209 | hbuss | 336 | break; |
337 | |||
1054 | killagreg | 338 | case 'p': // get PPM Channels |
339 | GetPPMChannelAnforderung = 1; |
||
1377 | hbuss | 340 | PcZugriff = 255; |
1054 | killagreg | 341 | break; |
1053 | killagreg | 342 | |
1054 | killagreg | 343 | case 'q':// "Get"-Anforderung für Settings |
344 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
345 | if(pRxData[0] == 0xFF) |
||
346 | { |
||
347 | pRxData[0] = GetActiveParamSetNumber(); |
||
348 | } |
||
1063 | killagreg | 349 | // limit settings range |
350 | if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5 |
||
351 | else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5 |
||
352 | // load requested parameter set |
||
353 | ReadParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
354 | |||
1054 | killagreg | 355 | while(!UebertragungAbgeschlossen); |
1058 | killagreg | 356 | tempchar1 = pRxData[0]; |
357 | tempchar2 = EE_DATENREVISION; |
||
358 | SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
1054 | killagreg | 359 | break; |
1051 | killagreg | 360 | |
1054 | killagreg | 361 | case 's': // Parametersatz speichern |
1058 | killagreg | 362 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EE_DATENREVISION)) // check for setting to be in range |
1054 | killagreg | 363 | { |
1058 | killagreg | 364 | memcpy((unsigned char *) &EE_Parameter.Kanalbelegung[0], (unsigned char *)&pRxData[2], STRUCT_PARAM_LAENGE); |
1054 | killagreg | 365 | WriteParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
366 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
367 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
1063 | killagreg | 368 | SetActiveParamSetNumber(pRxData[0]); |
369 | tempchar1 = GetActiveParamSetNumber(); |
||
1054 | killagreg | 370 | } |
1058 | killagreg | 371 | else |
372 | { |
||
373 | tempchar1 = 0; // mark in response an invlid setting |
||
374 | } |
||
375 | while(!UebertragungAbgeschlossen); |
||
376 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
1405 | hbuss | 377 | if(!MotorenEin) Piep(tempchar1,110); |
378 | LipoDetection(0); |
||
1435 | killagreg | 379 | LIBFC_ReceiverInit(); |
1054 | killagreg | 380 | break; |
1405 | hbuss | 381 | case 'f': // auf anderen Parametersatz umschalten |
382 | if((1 <= pRxData[0]) && (pRxData[0] <= 5)) SetActiveParamSetNumber(pRxData[0]); |
||
383 | tempchar1 = pRxData[0]; |
||
384 | while(!UebertragungAbgeschlossen); |
||
385 | SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
386 | if(!MotorenEin) Piep(tempchar1,110); |
||
387 | LipoDetection(0); |
||
1435 | killagreg | 388 | LIBFC_ReceiverInit(); |
1405 | hbuss | 389 | break; |
1391 | killagreg | 390 | case 'y':// serial Potis |
391 | 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]; |
||
392 | 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]; |
||
393 | 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]; |
||
394 | break; |
||
395 | |||
1056 | killagreg | 396 | } // case FC_ADDRESS: |
1054 | killagreg | 397 | |
398 | default: // any Slave Address |
||
1056 | killagreg | 399 | |
1054 | killagreg | 400 | switch(RxdBuffer[2]) |
401 | { |
||
402 | // 't' comand placed here only for compatibility to BL |
||
403 | case 't':// Motortest |
||
1212 | hbuss | 404 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
405 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
||
1054 | killagreg | 406 | while(!UebertragungAbgeschlossen); |
407 | SendOutData('T', MeineSlaveAdresse, 0); |
||
1212 | hbuss | 408 | PC_MotortestActive = 250; |
1054 | killagreg | 409 | PcZugriff = 255; |
410 | break; |
||
411 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
||
412 | case 'K':// Kompasswert |
||
413 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
414 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
415 | break; |
||
416 | case 'a':// Texte der Analogwerte |
||
417 | DebugTextAnforderung = pRxData[0]; |
||
418 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
||
419 | PcZugriff = 255; |
||
420 | break; |
||
421 | case 'b': |
||
422 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
||
423 | ConfirmFrame = ExternControl.Frame; |
||
424 | PcZugriff = 255; |
||
425 | break; |
||
1171 | hbuss | 426 | case 'c': // Poll the 3D-Data |
427 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
||
428 | Intervall3D = pRxData[0] * 10; |
||
1417 | killagreg | 429 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1171 | hbuss | 430 | break; |
1054 | killagreg | 431 | case 'd': // Poll the debug data |
1438 | ingob | 432 | PcZugriff = 255; |
1399 | killagreg | 433 | DebugDataIntervall = (unsigned int)pRxData[0] * 10; |
1171 | hbuss | 434 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
1415 | killagreg | 435 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1054 | killagreg | 436 | break; |
437 | |||
438 | case 'h':// x-1 Displayzeilen |
||
1059 | hbuss | 439 | PcZugriff = 255; |
1399 | killagreg | 440 | if((pRxData[0] & 0x80) == 0x00) // old format |
441 | { |
||
442 | DisplayLine = 2; |
||
443 | Display_Interval = 0; |
||
444 | } |
||
445 | else // new format |
||
446 | { |
||
447 | RemoteKeys |= ~pRxData[0]; |
||
448 | Display_Interval = (unsigned int)pRxData[1] * 10; |
||
449 | DisplayLine = 4; |
||
1415 | killagreg | 450 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1399 | killagreg | 451 | } |
1054 | killagreg | 452 | DebugDisplayAnforderung = 1; |
453 | break; |
||
454 | |||
455 | case 'l':// x-1 Displayzeilen |
||
1059 | hbuss | 456 | PcZugriff = 255; |
1054 | killagreg | 457 | MenuePunkt = pRxData[0]; |
458 | DebugDisplayAnforderung1 = 1; |
||
459 | break; |
||
460 | case 'v': // Version-Anforderung und Ausbaustufe |
||
461 | GetVersionAnforderung = 1; |
||
462 | break; |
||
463 | |||
464 | case 'g':// |
||
465 | GetExternalControl = 1; |
||
466 | break; |
||
467 | } |
||
468 | break; // default: |
||
469 | } |
||
1051 | killagreg | 470 | NeuerDatensatzEmpfangen = 0; |
471 | pRxData = 0; |
||
472 | RxDataLen = 0; |
||
1 | ingob | 473 | } |
474 | |||
475 | //############################################################################ |
||
476 | //Routine für die Serielle Ausgabe |
||
1444 | ingob | 477 | void uart_putchar (char c) |
1 | ingob | 478 | //############################################################################ |
479 | { |
||
1444 | ingob | 480 | //if (c == '\n') uart_putchar('\r'); |
1 | ingob | 481 | //Warten solange bis Zeichen gesendet wurde |
482 | loop_until_bit_is_set(USR, UDRE); |
||
483 | //Ausgabe des Zeichens |
||
484 | UDR = c; |
||
485 | } |
||
486 | |||
487 | |||
488 | //############################################################################ |
||
489 | //INstallation der Seriellen Schnittstelle |
||
490 | void UART_Init (void) |
||
491 | //############################################################################ |
||
492 | { |
||
493 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
||
494 | |||
495 | UCR=(1 << TXEN) | (1 << RXEN); |
||
496 | // UART Double Speed (U2X) |
||
1051 | killagreg | 497 | USR |= (1<<U2X); |
1 | ingob | 498 | // RX-Interrupt Freigabe |
1051 | killagreg | 499 | UCSRB |= (1<<RXCIE); |
1 | ingob | 500 | // TX-Interrupt Freigabe |
1051 | killagreg | 501 | UCSRB |= (1<<TXCIE); |
1 | ingob | 502 | |
1051 | killagreg | 503 | //Teiler wird gesetzt |
1 | ingob | 504 | UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1); |
505 | //UBRR = 33; |
||
506 | //öffnet einen Kanal für printf (STDOUT) |
||
507 | //fdevopen (uart_putchar, 0); |
||
508 | //sbi(PORTD,4); |
||
1051 | killagreg | 509 | Debug_Timer = SetDelay(DebugDataIntervall); |
510 | Kompass_Timer = SetDelay(220); |
||
511 | |||
1058 | killagreg | 512 | VersionInfo.SWMajor = VERSION_MAJOR; |
513 | VersionInfo.SWMinor = VERSION_MINOR; |
||
514 | VersionInfo.SWPatch = VERSION_PATCH; |
||
515 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
516 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
1051 | killagreg | 517 | |
518 | pRxData = 0; |
||
519 | RxDataLen = 0; |
||
1 | ingob | 520 | } |
521 | |||
522 | //--------------------------------------------------------------------------------------------- |
||
1051 | killagreg | 523 | void DatenUebertragung(void) |
1 | ingob | 524 | { |
1415 | killagreg | 525 | if(!UebertragungAbgeschlossen) return; |
1 | ingob | 526 | |
1415 | killagreg | 527 | if(CheckDelay(AboTimeOut)) |
528 | { |
||
529 | Display_Interval = 0; |
||
530 | DebugDataIntervall = 0; |
||
1417 | killagreg | 531 | Intervall3D = 0; |
1415 | killagreg | 532 | } |
533 | |||
1399 | killagreg | 534 | if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen) |
1065 | killagreg | 535 | { |
1399 | killagreg | 536 | if(DisplayLine > 3)// new format |
537 | { |
||
1423 | hbuss | 538 | Menu(); |
1399 | killagreg | 539 | SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
540 | } |
||
541 | else // old format |
||
542 | { |
||
543 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
||
544 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
||
545 | if(DisplayLine++ > 3) DisplayLine = 0; |
||
546 | } |
||
547 | Display_Timer = SetDelay(Display_Interval); |
||
1065 | killagreg | 548 | DebugDisplayAnforderung = 0; |
549 | } |
||
550 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
||
551 | { |
||
552 | Menu(); |
||
553 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
||
554 | DebugDisplayAnforderung1 = 0; |
||
555 | } |
||
556 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
557 | { |
||
558 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
||
559 | GetVersionAnforderung = 0; |
||
560 | } |
||
1 | ingob | 561 | |
1065 | killagreg | 562 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
563 | { |
||
564 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
||
565 | GetExternalControl = 0; |
||
566 | } |
||
1051 | killagreg | 567 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
693 | hbuss | 568 | { |
1171 | hbuss | 569 | WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
570 | WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
717 | hbuss | 571 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
572 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
||
1054 | killagreg | 573 | SendOutData('w', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
855 | hbuss | 574 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
1051 | killagreg | 575 | Kompass_Timer = SetDelay(99); |
576 | } |
||
1171 | hbuss | 577 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
1 | ingob | 578 | { |
1391 | killagreg | 579 | //if(Poti3 > 64) |
1276 | hbuss | 580 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
1 | ingob | 581 | DebugDataAnforderung = 0; |
1055 | ingob | 582 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
1051 | killagreg | 583 | } |
1171 | hbuss | 584 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
585 | { |
||
586 | Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
587 | Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
588 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
||
589 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
||
590 | Timer3D = SetDelay(Intervall3D); |
||
591 | } |
||
499 | hbuss | 592 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
593 | { |
||
1490 | killagreg | 594 | unsigned char label[16]; // local sram buffer |
595 | memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra |
||
596 | SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16); |
||
499 | hbuss | 597 | DebugTextAnforderung = 255; |
598 | } |
||
1051 | killagreg | 599 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
595 | hbuss | 600 | { |
1054 | killagreg | 601 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
1051 | killagreg | 602 | ConfirmFrame = 0; |
595 | hbuss | 603 | } |
1053 | killagreg | 604 | |
1051 | killagreg | 605 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
606 | { |
||
1054 | killagreg | 607 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
1051 | killagreg | 608 | GetPPMChannelAnforderung = 0; |
609 | } |
||
1 | ingob | 610 | |
611 | } |
||
612 |