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