Rev 1604 | 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 | |
1893 | - | 14 | #define VERSION_MAJOR 1 |
15 | #define VERSION_MINOR 1 |
||
16 | #define VERSION_PATCH 1 |
||
17 | #define VERSION_SERIAL_MAJOR 1 |
||
18 | #define VERSION_SERIAL_MINOR 1 |
||
1054 | killagreg | 19 | |
20 | #define FC_ADDRESS 1 |
||
21 | #define NC_ADDRESS 2 |
||
22 | #define MK3MAG_ADDRESS 3 |
||
23 | |||
1415 | killagreg | 24 | #define ABO_TIMEOUT 4000 // disable abo after 4 seconds |
1441 | ingob | 25 | #define MAX_SENDE_BUFF 160 |
26 | #define MAX_EMPFANGS_BUFF 160 |
||
1053 | killagreg | 27 | unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0; |
28 | unsigned char DisplayLine = 0; |
||
1 | ingob | 29 | unsigned volatile char SioTmp = 0; |
30 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
||
31 | unsigned volatile char NeueKoordinateEmpfangen = 0; |
||
32 | unsigned volatile char UebertragungAbgeschlossen = 1; |
||
33 | unsigned volatile char CntCrcError = 0; |
||
34 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
||
1441 | ingob | 35 | unsigned volatile char TxdBuffer[MAX_SENDE_BUFF]; |
36 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
||
37 | |||
1051 | killagreg | 38 | unsigned char *pRxData = 0; |
39 | unsigned char RxDataLen = 0; |
||
1 | ingob | 40 | unsigned volatile char PC_DebugTimeout = 0; |
1212 | hbuss | 41 | unsigned volatile char PC_MotortestActive = 0; |
1051 | killagreg | 42 | |
499 | hbuss | 43 | unsigned char DebugTextAnforderung = 255; |
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 | " ", |
1253 | killagreg | 79 | "VarioMeter ", |
819 | hbuss | 80 | "MK3Mag CalState ", |
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 | |||
1058 | killagreg | 305 | unsigned char tempchar1, tempchar2; |
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); |
||
327 | SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer,sizeof(Mixer)); |
||
328 | break; |
||
329 | |||
330 | case 'm':// "Write Mixer |
||
331 | while(!UebertragungAbgeschlossen); |
||
1211 | hbuss | 332 | if(pRxData[0] == MIXER_REVISION) |
1521 | killagreg | 333 | { |
1211 | hbuss | 334 | memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer)); |
335 | eeprom_write_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer)); |
||
336 | tempchar1 = 1; |
||
1521 | killagreg | 337 | } |
338 | else tempchar1 = 0; |
||
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 | { |
||
351 | pRxData[0] = GetActiveParamSetNumber(); |
||
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 |
||
357 | ReadParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
358 | |||
1054 | killagreg | 359 | while(!UebertragungAbgeschlossen); |
1058 | killagreg | 360 | tempchar1 = pRxData[0]; |
361 | tempchar2 = EE_DATENREVISION; |
||
362 | SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
1054 | killagreg | 363 | break; |
1051 | killagreg | 364 | |
1054 | killagreg | 365 | case 's': // Parametersatz speichern |
1058 | killagreg | 366 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EE_DATENREVISION)) // check for setting to be in range |
1054 | killagreg | 367 | { |
1058 | killagreg | 368 | memcpy((unsigned char *) &EE_Parameter.Kanalbelegung[0], (unsigned char *)&pRxData[2], STRUCT_PARAM_LAENGE); |
1054 | killagreg | 369 | WriteParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
370 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
371 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
1063 | killagreg | 372 | SetActiveParamSetNumber(pRxData[0]); |
373 | tempchar1 = GetActiveParamSetNumber(); |
||
1054 | killagreg | 374 | } |
1058 | killagreg | 375 | else |
376 | { |
||
377 | tempchar1 = 0; // mark in response an invlid setting |
||
378 | } |
||
379 | while(!UebertragungAbgeschlossen); |
||
380 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
1405 | hbuss | 381 | if(!MotorenEin) Piep(tempchar1,110); |
382 | LipoDetection(0); |
||
1435 | killagreg | 383 | LIBFC_ReceiverInit(); |
1054 | killagreg | 384 | break; |
1405 | hbuss | 385 | case 'f': // auf anderen Parametersatz umschalten |
386 | if((1 <= pRxData[0]) && (pRxData[0] <= 5)) SetActiveParamSetNumber(pRxData[0]); |
||
387 | tempchar1 = pRxData[0]; |
||
1523 | holgerb | 388 | ReadParameterSet(tempchar1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
1405 | hbuss | 389 | while(!UebertragungAbgeschlossen); |
390 | SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
391 | if(!MotorenEin) Piep(tempchar1,110); |
||
392 | LipoDetection(0); |
||
1435 | killagreg | 393 | LIBFC_ReceiverInit(); |
1405 | hbuss | 394 | break; |
1391 | killagreg | 395 | case 'y':// serial Potis |
396 | 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]; |
||
397 | 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]; |
||
398 | 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]; |
||
399 | break; |
||
400 | |||
1056 | killagreg | 401 | } // case FC_ADDRESS: |
1054 | killagreg | 402 | |
403 | default: // any Slave Address |
||
1056 | killagreg | 404 | |
1054 | killagreg | 405 | switch(RxdBuffer[2]) |
406 | { |
||
407 | // 't' comand placed here only for compatibility to BL |
||
408 | case 't':// Motortest |
||
1212 | hbuss | 409 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
410 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
||
1054 | killagreg | 411 | while(!UebertragungAbgeschlossen); |
412 | SendOutData('T', MeineSlaveAdresse, 0); |
||
1212 | hbuss | 413 | PC_MotortestActive = 250; |
1054 | killagreg | 414 | PcZugriff = 255; |
415 | break; |
||
416 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
||
417 | case 'K':// Kompasswert |
||
418 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
419 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
420 | break; |
||
421 | case 'a':// Texte der Analogwerte |
||
422 | DebugTextAnforderung = pRxData[0]; |
||
423 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
||
424 | PcZugriff = 255; |
||
425 | break; |
||
426 | case 'b': |
||
427 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
||
428 | ConfirmFrame = ExternControl.Frame; |
||
429 | PcZugriff = 255; |
||
430 | break; |
||
1171 | hbuss | 431 | case 'c': // Poll the 3D-Data |
432 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
||
433 | Intervall3D = pRxData[0] * 10; |
||
1417 | killagreg | 434 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1171 | hbuss | 435 | break; |
1054 | killagreg | 436 | case 'd': // Poll the debug data |
1438 | ingob | 437 | PcZugriff = 255; |
1399 | killagreg | 438 | DebugDataIntervall = (unsigned int)pRxData[0] * 10; |
1171 | hbuss | 439 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
1415 | killagreg | 440 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1054 | killagreg | 441 | break; |
442 | |||
443 | case 'h':// x-1 Displayzeilen |
||
1059 | hbuss | 444 | PcZugriff = 255; |
1399 | killagreg | 445 | if((pRxData[0] & 0x80) == 0x00) // old format |
446 | { |
||
447 | DisplayLine = 2; |
||
448 | Display_Interval = 0; |
||
449 | } |
||
450 | else // new format |
||
451 | { |
||
452 | RemoteKeys |= ~pRxData[0]; |
||
453 | Display_Interval = (unsigned int)pRxData[1] * 10; |
||
454 | DisplayLine = 4; |
||
1415 | killagreg | 455 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
1399 | killagreg | 456 | } |
1054 | killagreg | 457 | DebugDisplayAnforderung = 1; |
458 | break; |
||
459 | |||
460 | case 'l':// x-1 Displayzeilen |
||
1059 | hbuss | 461 | PcZugriff = 255; |
1054 | killagreg | 462 | MenuePunkt = pRxData[0]; |
463 | DebugDisplayAnforderung1 = 1; |
||
464 | break; |
||
465 | case 'v': // Version-Anforderung und Ausbaustufe |
||
466 | GetVersionAnforderung = 1; |
||
467 | break; |
||
468 | |||
469 | case 'g':// |
||
470 | GetExternalControl = 1; |
||
471 | break; |
||
472 | } |
||
473 | break; // default: |
||
474 | } |
||
1051 | killagreg | 475 | NeuerDatensatzEmpfangen = 0; |
476 | pRxData = 0; |
||
477 | RxDataLen = 0; |
||
1 | ingob | 478 | } |
479 | |||
480 | //############################################################################ |
||
481 | //Routine für die Serielle Ausgabe |
||
1444 | ingob | 482 | void uart_putchar (char c) |
1 | ingob | 483 | //############################################################################ |
484 | { |
||
485 | //Warten solange bis Zeichen gesendet wurde |
||
1561 | killagreg | 486 | loop_until_bit_is_set(UCSR0A, UDRE0); |
1 | ingob | 487 | //Ausgabe des Zeichens |
1561 | killagreg | 488 | UDR0 = c; |
1 | ingob | 489 | } |
490 | |||
491 | |||
492 | //############################################################################ |
||
493 | //INstallation der Seriellen Schnittstelle |
||
494 | void UART_Init (void) |
||
495 | //############################################################################ |
||
496 | { |
||
1561 | killagreg | 497 | unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1); |
498 | |||
1 | ingob | 499 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
1561 | killagreg | 500 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
1 | ingob | 501 | // UART Double Speed (U2X) |
1561 | killagreg | 502 | UCSR0A |= (1 << U2X0); |
1 | ingob | 503 | // RX-Interrupt Freigabe |
1561 | killagreg | 504 | UCSR0B |= (1 << RXCIE0); |
1 | ingob | 505 | // TX-Interrupt Freigabe |
1561 | killagreg | 506 | UCSR0B |= (1 << TXCIE0); |
507 | // USART0 Baud Rate Register |
||
508 | // set clock divider |
||
509 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
510 | UBRR0L = (uint8_t)ubrr; |
||
1 | ingob | 511 | |
1051 | killagreg | 512 | Debug_Timer = SetDelay(DebugDataIntervall); |
513 | Kompass_Timer = SetDelay(220); |
||
514 | |||
1058 | killagreg | 515 | VersionInfo.SWMajor = VERSION_MAJOR; |
516 | VersionInfo.SWMinor = VERSION_MINOR; |
||
517 | VersionInfo.SWPatch = VERSION_PATCH; |
||
518 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
519 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
1051 | killagreg | 520 | |
521 | pRxData = 0; |
||
522 | RxDataLen = 0; |
||
1 | ingob | 523 | } |
524 | |||
525 | //--------------------------------------------------------------------------------------------- |
||
1051 | killagreg | 526 | void DatenUebertragung(void) |
1 | ingob | 527 | { |
1415 | killagreg | 528 | if(!UebertragungAbgeschlossen) return; |
1 | ingob | 529 | |
1415 | killagreg | 530 | if(CheckDelay(AboTimeOut)) |
531 | { |
||
532 | Display_Interval = 0; |
||
533 | DebugDataIntervall = 0; |
||
1417 | killagreg | 534 | Intervall3D = 0; |
1415 | killagreg | 535 | } |
536 | |||
1399 | killagreg | 537 | if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen) |
1065 | killagreg | 538 | { |
1399 | killagreg | 539 | if(DisplayLine > 3)// new format |
540 | { |
||
1423 | hbuss | 541 | Menu(); |
1399 | killagreg | 542 | SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
543 | } |
||
544 | else // old format |
||
545 | { |
||
546 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
||
547 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
||
548 | if(DisplayLine++ > 3) DisplayLine = 0; |
||
549 | } |
||
550 | Display_Timer = SetDelay(Display_Interval); |
||
1065 | killagreg | 551 | DebugDisplayAnforderung = 0; |
552 | } |
||
553 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
||
554 | { |
||
555 | Menu(); |
||
556 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
||
557 | DebugDisplayAnforderung1 = 0; |
||
558 | } |
||
559 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
560 | { |
||
561 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
||
562 | GetVersionAnforderung = 0; |
||
563 | } |
||
1 | ingob | 564 | |
1065 | killagreg | 565 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
566 | { |
||
567 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
||
568 | GetExternalControl = 0; |
||
569 | } |
||
1051 | killagreg | 570 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
693 | hbuss | 571 | { |
1171 | hbuss | 572 | WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
573 | WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
717 | hbuss | 574 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
575 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
||
1054 | killagreg | 576 | SendOutData('w', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
855 | hbuss | 577 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
1051 | killagreg | 578 | Kompass_Timer = SetDelay(99); |
579 | } |
||
1171 | hbuss | 580 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
1 | ingob | 581 | { |
1391 | killagreg | 582 | //if(Poti3 > 64) |
1276 | hbuss | 583 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
1 | ingob | 584 | DebugDataAnforderung = 0; |
1055 | ingob | 585 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
1051 | killagreg | 586 | } |
1171 | hbuss | 587 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
588 | { |
||
589 | Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
590 | Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
591 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
||
592 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
||
593 | Timer3D = SetDelay(Intervall3D); |
||
594 | } |
||
499 | hbuss | 595 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
596 | { |
||
1490 | killagreg | 597 | unsigned char label[16]; // local sram buffer |
598 | memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra |
||
599 | SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16); |
||
499 | hbuss | 600 | DebugTextAnforderung = 255; |
601 | } |
||
1051 | killagreg | 602 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
595 | hbuss | 603 | { |
1054 | killagreg | 604 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
1051 | killagreg | 605 | ConfirmFrame = 0; |
595 | hbuss | 606 | } |
1053 | killagreg | 607 | |
1051 | killagreg | 608 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
609 | { |
||
1054 | killagreg | 610 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
1051 | killagreg | 611 | GetPPMChannelAnforderung = 0; |
612 | } |
||
1 | ingob | 613 | |
614 | } |
||
615 |