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" |
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 ", |
1523 | holgerb | 60 | "YawGyro ", |
1521 | killagreg | 61 | "Height Value ", //5 |
499 | hbuss | 62 | "AccZ ", |
63 | "Gas ", |
||
1175 | hbuss | 64 | "Compass Value ", |
1528 | holgerb | 65 | "Voltage [0.1V] ", |
1521 | killagreg | 66 | "Receiver Level ", //10 |
67 | "Gyro Compass ", |
||
68 | "Motor 1 ", |
||
69 | "Motor 2 ", |
||
70 | "Motor 3 ", |
||
71 | "Motor 4 ", //15 |
||
929 | hbuss | 72 | " ", |
1175 | hbuss | 73 | " ", |
1253 | killagreg | 74 | "VarioMeter ", |
819 | hbuss | 75 | "MK3Mag CalState ", |
854 | hbuss | 76 | "Servo ", //20 |
1521 | killagreg | 77 | "Hovergas ", |
1523 | holgerb | 78 | "Current [0.1A] ", |
1501 | holgerb | 79 | "Capacity [mAh] ", |
720 | ingob | 80 | " ", |
1322 | hbuss | 81 | " ", //25 |
82 | " ", |
||
83 | " ", |
||
84 | "I2C-Error ", |
||
1521 | killagreg | 85 | " ", // "Navi Serial Data", |
720 | ingob | 86 | "GPS_Nick ", //30 |
87 | "GPS_Roll " |
||
499 | hbuss | 88 | }; |
89 | |||
90 | |||
1 | ingob | 91 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
92 | //++ Sende-Part der Datenübertragung |
||
93 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
94 | SIGNAL(INT_VEC_TX) |
||
95 | { |
||
96 | static unsigned int ptr = 0; |
||
97 | unsigned char tmp_tx; |
||
1051 | killagreg | 98 | if(!UebertragungAbgeschlossen) |
1 | ingob | 99 | { |
100 | ptr++; // die [0] wurde schon gesendet |
||
1438 | ingob | 101 | tmp_tx = TxdBuffer[ptr]; |
1 | ingob | 102 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
103 | { |
||
104 | ptr = 0; |
||
105 | UebertragungAbgeschlossen = 1; |
||
106 | } |
||
1051 | killagreg | 107 | UDR = tmp_tx; |
108 | } |
||
1 | ingob | 109 | else ptr = 0; |
110 | } |
||
111 | |||
112 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
113 | //++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung |
||
114 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
115 | SIGNAL(INT_VEC_RX) |
||
116 | { |
||
117 | static unsigned int crc; |
||
118 | static unsigned char crc1,crc2,buf_ptr; |
||
119 | static unsigned char UartState = 0; |
||
120 | unsigned char CrcOkay = 0; |
||
121 | |||
1051 | killagreg | 122 | SioTmp = UDR; |
1438 | ingob | 123 | if(buf_ptr >= MAX_SENDE_BUFF) UartState = 0; |
1051 | killagreg | 124 | if(SioTmp == '\r' && UartState == 2) |
1 | ingob | 125 | { |
126 | UartState = 0; |
||
127 | crc -= RxdBuffer[buf_ptr-2]; |
||
128 | crc -= RxdBuffer[buf_ptr-1]; |
||
129 | crc %= 4096; |
||
130 | crc1 = '=' + crc / 64; |
||
131 | crc2 = '=' + crc % 64; |
||
132 | CrcOkay = 0; |
||
133 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
||
134 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
||
135 | { |
||
1051 | killagreg | 136 | NeuerDatensatzEmpfangen = 1; |
137 | AnzahlEmpfangsBytes = buf_ptr + 1; |
||
1 | ingob | 138 | RxdBuffer[buf_ptr] = '\r'; |
1253 | killagreg | 139 | if(RxdBuffer[2] == 'R') |
1232 | hbuss | 140 | { |
1435 | killagreg | 141 | LcdClear(); |
1232 | hbuss | 142 | wdt_enable(WDTO_250MS); // Reset-Commando |
143 | ServoActive = 0; |
||
1435 | killagreg | 144 | |
1253 | killagreg | 145 | } |
1051 | killagreg | 146 | } |
1 | ingob | 147 | } |
148 | else |
||
149 | switch(UartState) |
||
150 | { |
||
151 | case 0: |
||
152 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
||
153 | buf_ptr = 0; |
||
154 | RxdBuffer[buf_ptr++] = SioTmp; |
||
155 | crc = SioTmp; |
||
156 | break; |
||
157 | case 1: // Adresse auswerten |
||
158 | UartState++; |
||
159 | RxdBuffer[buf_ptr++] = SioTmp; |
||
160 | crc += SioTmp; |
||
161 | break; |
||
162 | case 2: // Eingangsdaten sammeln |
||
163 | RxdBuffer[buf_ptr] = SioTmp; |
||
1490 | killagreg | 164 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
1 | ingob | 165 | else UartState = 0; |
166 | crc += SioTmp; |
||
167 | break; |
||
1051 | killagreg | 168 | default: |
169 | UartState = 0; |
||
1 | ingob | 170 | break; |
171 | } |
||
172 | } |
||
173 | |||
174 | |||
175 | // -------------------------------------------------------------------------- |
||
176 | void AddCRC(unsigned int wieviele) |
||
177 | { |
||
1051 | killagreg | 178 | unsigned int tmpCRC = 0,i; |
1 | ingob | 179 | for(i = 0; i < wieviele;i++) |
180 | { |
||
1438 | ingob | 181 | tmpCRC += TxdBuffer[i]; |
1 | ingob | 182 | } |
183 | tmpCRC %= 4096; |
||
1438 | ingob | 184 | TxdBuffer[i++] = '=' + tmpCRC / 64; |
185 | TxdBuffer[i++] = '=' + tmpCRC % 64; |
||
186 | TxdBuffer[i++] = '\r'; |
||
1 | ingob | 187 | UebertragungAbgeschlossen = 0; |
1438 | ingob | 188 | UDR = TxdBuffer[0]; |
1 | ingob | 189 | } |
190 | |||
191 | |||
192 | |||
193 | // -------------------------------------------------------------------------- |
||
1056 | killagreg | 194 | void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len) |
1 | ingob | 195 | { |
1051 | killagreg | 196 | va_list ap; |
1 | ingob | 197 | unsigned int pt = 0; |
198 | unsigned char a,b,c; |
||
199 | unsigned char ptr = 0; |
||
200 | |||
1051 | killagreg | 201 | unsigned char *snd = 0; |
202 | int len = 0; |
||
203 | |||
1438 | ingob | 204 | TxdBuffer[pt++] = '#'; // Startzeichen |
205 | TxdBuffer[pt++] = 'a' + address; // Adresse (a=0; b=1,...) |
||
206 | TxdBuffer[pt++] = cmd; // Commando |
||
1 | ingob | 207 | |
1051 | killagreg | 208 | va_start(ap, BufferAnzahl); |
209 | if(BufferAnzahl) |
||
210 | { |
||
211 | snd = va_arg(ap, unsigned char*); |
||
212 | len = va_arg(ap, int); |
||
213 | ptr = 0; |
||
214 | BufferAnzahl--; |
||
215 | } |
||
1 | ingob | 216 | while(len) |
217 | { |
||
1051 | killagreg | 218 | if(len) |
219 | { |
||
220 | a = snd[ptr++]; |
||
221 | len--; |
||
222 | if((!len) && BufferAnzahl) |
||
223 | { |
||
224 | snd = va_arg(ap, unsigned char*); |
||
225 | len = va_arg(ap, int); |
||
226 | ptr = 0; |
||
227 | BufferAnzahl--; |
||
228 | } |
||
229 | } |
||
230 | else a = 0; |
||
231 | if(len) |
||
232 | { |
||
233 | b = snd[ptr++]; |
||
234 | len--; |
||
235 | if((!len) && BufferAnzahl) |
||
236 | { |
||
237 | snd = va_arg(ap, unsigned char*); |
||
238 | len = va_arg(ap, int); |
||
239 | ptr = 0; |
||
240 | BufferAnzahl--; |
||
241 | } |
||
242 | } |
||
243 | else b = 0; |
||
244 | if(len) |
||
245 | { |
||
246 | c = snd[ptr++]; |
||
247 | len--; |
||
248 | if((!len) && BufferAnzahl) |
||
249 | { |
||
250 | snd = va_arg(ap, unsigned char*); |
||
251 | len = va_arg(ap, int); |
||
252 | ptr = 0; |
||
253 | BufferAnzahl--; |
||
254 | } |
||
255 | } |
||
256 | else c = 0; |
||
1438 | ingob | 257 | TxdBuffer[pt++] = '=' + (a >> 2); |
258 | TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
259 | TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
260 | TxdBuffer[pt++] = '=' + ( c & 0x3f); |
||
1 | ingob | 261 | } |
1051 | killagreg | 262 | va_end(ap); |
1 | ingob | 263 | AddCRC(pt); |
264 | } |
||
265 | |||
266 | // -------------------------------------------------------------------------- |
||
1051 | killagreg | 267 | void Decode64(void) // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden. |
1 | ingob | 268 | { |
269 | unsigned char a,b,c,d; |
||
270 | unsigned char x,y,z; |
||
1051 | killagreg | 271 | unsigned char ptrIn = 3; // start at begin of data block |
272 | unsigned char ptrOut = 3; |
||
273 | 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. |
||
274 | |||
1 | ingob | 275 | while(len) |
276 | { |
||
277 | a = RxdBuffer[ptrIn++] - '='; |
||
278 | b = RxdBuffer[ptrIn++] - '='; |
||
279 | c = RxdBuffer[ptrIn++] - '='; |
||
280 | d = RxdBuffer[ptrIn++] - '='; |
||
281 | |||
282 | x = (a << 2) | (b >> 4); |
||
283 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
284 | z = ((c & 0x03) << 6) | d; |
||
285 | |||
1051 | killagreg | 286 | if(len--) RxdBuffer[ptrOut++] = x; else break; |
287 | if(len--) RxdBuffer[ptrOut++] = y; else break; |
||
288 | if(len--) RxdBuffer[ptrOut++] = z; else break; |
||
1 | ingob | 289 | } |
1051 | killagreg | 290 | pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte |
291 | RxDataLen = ptrOut - 3; // wie viele Bytes wurden dekodiert? |
||
1 | ingob | 292 | |
293 | } |
||
294 | |||
295 | // -------------------------------------------------------------------------- |
||
296 | void BearbeiteRxDaten(void) |
||
297 | { |
||
298 | if(!NeuerDatensatzEmpfangen) return; |
||
299 | |||
1058 | killagreg | 300 | unsigned char tempchar1, tempchar2; |
1056 | killagreg | 301 | Decode64(); // dekodiere datenblock im Empfangsbuffer |
1055 | ingob | 302 | switch(RxdBuffer[1]-'a') // check for Slave Address |
1054 | killagreg | 303 | { |
304 | case FC_ADDRESS: // FC special commands |
||
305 | switch(RxdBuffer[2]) |
||
306 | { |
||
307 | case 'K':// Kompasswert |
||
308 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
309 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
310 | break; |
||
311 | case 't':// Motortest |
||
1212 | hbuss | 312 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
313 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
||
314 | PC_MotortestActive = 240; |
||
1065 | killagreg | 315 | //while(!UebertragungAbgeschlossen); |
316 | //SendOutData('T', MeineSlaveAdresse, 0); |
||
1054 | killagreg | 317 | PcZugriff = 255; |
318 | break; |
||
1053 | killagreg | 319 | |
1209 | hbuss | 320 | case 'n':// "Get Mixer |
321 | while(!UebertragungAbgeschlossen); |
||
322 | SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer,sizeof(Mixer)); |
||
323 | break; |
||
324 | |||
325 | case 'm':// "Write Mixer |
||
326 | while(!UebertragungAbgeschlossen); |
||
1211 | hbuss | 327 | if(pRxData[0] == MIXER_REVISION) |
1521 | killagreg | 328 | { |
1211 | hbuss | 329 | memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer)); |
330 | eeprom_write_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer)); |
||
331 | tempchar1 = 1; |
||
1521 | killagreg | 332 | } |
333 | else tempchar1 = 0; |
||
1211 | hbuss | 334 | SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
1209 | hbuss | 335 | break; |
336 | |||
1054 | killagreg | 337 | case 'p': // get PPM Channels |
338 | GetPPMChannelAnforderung = 1; |
||
1377 | hbuss | 339 | PcZugriff = 255; |
1054 | killagreg | 340 | break; |
1053 | killagreg | 341 | |
1054 | killagreg | 342 | case 'q':// "Get"-Anforderung für Settings |
343 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
344 | if(pRxData[0] == 0xFF) |
||
345 | { |
||
346 | pRxData[0] = GetActiveParamSetNumber(); |
||
347 | } |
||
1063 | killagreg | 348 | // limit settings range |
349 | if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5 |
||
350 | else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5 |
||
351 | // load requested parameter set |
||
352 | ReadParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
353 | |||
1054 | killagreg | 354 | while(!UebertragungAbgeschlossen); |
1058 | killagreg | 355 | tempchar1 = pRxData[0]; |
356 | tempchar2 = EE_DATENREVISION; |
||
357 | SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
1054 | killagreg | 358 | break; |
1051 | killagreg | 359 | |
1054 | killagreg | 360 | case 's': // Parametersatz speichern |
1058 | killagreg | 361 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EE_DATENREVISION)) // check for setting to be in range |
1054 | killagreg | 362 | { |
1058 | killagreg | 363 | memcpy((unsigned char *) &EE_Parameter.Kanalbelegung[0], (unsigned char *)&pRxData[2], STRUCT_PARAM_LAENGE); |
1054 | killagreg | 364 | WriteParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
365 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
366 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
1063 | killagreg | 367 | SetActiveParamSetNumber(pRxData[0]); |
368 | tempchar1 = GetActiveParamSetNumber(); |
||
1054 | killagreg | 369 | } |
1058 | killagreg | 370 | else |
371 | { |
||
372 | tempchar1 = 0; // mark in response an invlid setting |
||
373 | } |
||
374 | while(!UebertragungAbgeschlossen); |
||
375 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
1405 | hbuss | 376 | if(!MotorenEin) Piep(tempchar1,110); |
377 | LipoDetection(0); |
||
1435 | killagreg | 378 | LIBFC_ReceiverInit(); |
1054 | killagreg | 379 | break; |
1405 | hbuss | 380 | case 'f': // auf anderen Parametersatz umschalten |
381 | if((1 <= pRxData[0]) && (pRxData[0] <= 5)) SetActiveParamSetNumber(pRxData[0]); |
||
382 | tempchar1 = pRxData[0]; |
||
1523 | holgerb | 383 | ReadParameterSet(tempchar1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
1405 | hbuss | 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 |