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