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