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