Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2248 | - | 1 | /************************************************************************************************************************************** |
2 | * File: uart.c |
||
3 | * |
||
4 | * Purpose: This program treats the RS232 USRAT interface |
||
5 | * |
||
6 | * Functions: void UART_Init(void) |
||
7 | * ISR(USART0_TX_vect) |
||
8 | * ISR(USART0_RX_vect) |
||
9 | * void AddCRC(unsigned int wieviele) |
||
10 | * void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) |
||
11 | * void Decode64(void) |
||
12 | * void BearbeiteRxDaten(void) |
||
13 | * int uart_putchar(char c) |
||
14 | * void DatenUebertragung(void) |
||
15 | * |
||
16 | ******************************************************************************************************************************************/ |
||
17 | #include "main.h" |
||
18 | #include "uart.h" |
||
19 | |||
20 | #include <stdarg.h> |
||
21 | #include <string.h> |
||
22 | |||
23 | //------------------------ Definitionen --------- |
||
24 | #define FC_ADDRESS 1 |
||
25 | #define NC_ADDRESS 2 |
||
26 | #define MK3MAG_ADDRESS 3 |
||
27 | |||
28 | //---------------------------------------------Variables------- |
||
29 | unsigned char GetExternalControl = 0; |
||
30 | unsigned char DebugDisplayAnforderung1 = 0; |
||
31 | unsigned char DebugDisplayAnforderung = 0; |
||
32 | unsigned char DebugDataAnforderung = 0; |
||
33 | unsigned char GetVersionAnforderung = 0; |
||
34 | unsigned char GetPPMChannelAnforderung = 0; |
||
35 | |||
36 | unsigned char DisplayLine = 0; |
||
37 | unsigned volatile char SioTmp = 0; |
||
38 | unsigned volatile char SendeBuffer[MAX_SENDE_BUFF]; |
||
39 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
||
40 | unsigned volatile char NMEABuffer[MAX_EMPFANGS_BUFF]; |
||
41 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
||
42 | unsigned volatile char NeueKoordinateEmpfangen = 0; |
||
43 | unsigned volatile char UebertragungAbgeschlossen = 1; |
||
44 | unsigned volatile char CntCrcError = 0; |
||
45 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
||
46 | unsigned char *pRxData = 0; |
||
47 | unsigned char RxDataLen = 0; |
||
48 | unsigned volatile char PC_DebugTimeout = 0; |
||
49 | unsigned volatile char PC_MotortestActive = 0; |
||
50 | |||
51 | unsigned char DebugTextAnforderung = 255; |
||
52 | unsigned char PcZugriff = 100; |
||
53 | unsigned char MotorTest[16]; |
||
54 | unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl |
||
55 | unsigned char ConfirmFrame; |
||
56 | |||
57 | int Debug_Timer,Kompass_Timer,Timer3D; |
||
58 | unsigned int DebugDataIntervall = 200, Intervall3D = 0; |
||
59 | |||
60 | //---------------------------------------structs---------- |
||
61 | struct str_DebugOut DebugOut; |
||
62 | struct str_ExternControl ExternControl; |
||
63 | struct str_VersionInfo VersionInfo; |
||
64 | struct str_WinkelOut WinkelOut; |
||
65 | struct str_Data3D Data3D; |
||
66 | |||
67 | |||
68 | const unsigned char ANALOG_TEXT[32][16] = |
||
69 | { |
||
70 | //1234567890123456 // -------------------------------------------------------------// siehe "Debugwerte zuordnen" in fc.c Zeile 1270 |
||
71 | "3D Nick-Winkel ", // 0 = IntegralNick / (EE_Parameter.GyroAccFaktor * 4); // = Winkel * 10 |
||
72 | "AccNick ", // 1 = Mittelwert_AccNick / 4; // = so um die Null |
||
73 | "maxcontrollerDD ", // 2 = // = so um die Null |
||
74 | "mincontrollerDD ", // 3 = // = so um die Null |
||
75 | "MesswertNick ", // 4 = MesswertGier; // = -450 |
||
76 | "HiResNick ", // 5 = auf Null bezogener Gyro Nickwert // = so um die Null |
||
77 | "AdWertAccNick ", // 6 = // = 713 |
||
78 | "Gas = thrust ", // 7 = Gas // = min 72 max 840 entspricht der Gashebelstellung |
||
79 | " ", // 8 = // = 0 bis 360 Grad jenachdem wie der MK steht |
||
80 | "Spannung V ", // 9 = UBat; // = 120 bei 12V |
||
81 | "Empfang ", // 10 = SenderOkay; // = 0 wenn Sender aus und ca 200 wenn Sender an |
||
82 | "controllerP ", // 11 = controllerP // = dreht sich im Kreis - Umlauf 40 sec |
||
83 | "Motor vorne ", // 12 = Motor[0] // = 0 // wird in SendMotorData(void) hier in fc.c zugewiesen |
||
84 | "Motor hinten ", // 13 = Motor[1] // = 0 |
||
85 | "controllerD ", // 14 = // = 0 |
||
86 | "controllerDD ", // 15 = // = 0 |
||
87 | "PPM_in[3] Nick ", // 16 = // = -120...+120 // ab hier beginjnt der zweite Teil der Anzeige ------------ |
||
88 | "ipk[0] ", // 17 = // = |
||
89 | "ipk[1] ", // 18 = // = |
||
90 | "ipk[2] ", // 19 = // = |
||
91 | "ucflg1 ", // 20 = // = |
||
92 | "Stick Kanal 5 ", // 21 = PPM_in[5] // = ca -120 oder +120 |
||
93 | "MesswertNick ", // 22 = MesswertNick; // = 0 |
||
94 | "maxcontrollerD ", // 23 = // = 0 |
||
95 | "mincontrollerD ", // 24 = // = -560 |
||
96 | "AdWertNick ", // 25 = AdWertNick; // = 1130 |
||
97 | "maxcontrollerP ", // 26 = // |
||
98 | "mincontrollerP ", // 27 = // |
||
99 | "PPM_in[4] Gier ", // 28 = PPM_in[4]; // = ca -120 bis +120 |
||
100 | "PPM_in[3] Nick ", // 29 = PPM_in[3]; // = ca -120 bis +120 |
||
101 | "PPM_in[2] Roll ", // 30 = PPM_in[2]; // = ca -120 bis +120 |
||
102 | "PPM_in[1] Gas " // 31 = PPM_in[1]; // = ca -120 bis +120 |
||
103 | }; |
||
104 | //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
||
105 | |||
106 | |||
107 | |||
108 | //------------------------------------------------------------------------------------------------------------------ |
||
109 | // Installation of the USART COM Port |
||
110 | //------------------------------------------------------------------------------------------------------------------ |
||
111 | void UART_Init(void) |
||
112 | { |
||
113 | //------------------------------------------------------------------------------------------------------------ |
||
114 | // UCSR0B – USART Control and Status Register 0 B containing RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80 |
||
115 | // |
||
116 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); // Writing this bit to one enables the USART Transmitter |
||
117 | |||
118 | //------------------------------------------------------------------------------------------------------------ |
||
119 | // UCSR0A – USART Control and Status Register A containing RXC0 TXC0 UDRE0 FE0 DORn UPEn U2X0 MPCM0 |
||
120 | // |
||
121 | UCSR0A |= (1<<U2X0); // Double the USART Transmission Speed |
||
122 | |||
123 | //------------------------------------------------------------------------------------------------------------ |
||
124 | // UCSR0B – USART Control and Status Register 0 B containing RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80 |
||
125 | // |
||
126 | UCSR0B |= (1<<RXCIE); // RX Complete Interrupt Enable |
||
127 | UCSR0B |= (1<<TXCIE); // TX Complete Interrupt Enable |
||
128 | |||
129 | //------------------------------------------------------------------------------------------------------------ |
||
130 | // UBRRnL and UBRRnH – USART Baud Rate Registers |
||
131 | // |
||
132 | UBRR0L = (SYSCLK / (BAUD_RATE * 8L) - 1); // set Baudrate to 57600 |
||
133 | |||
134 | |||
135 | Debug_Timer = SetDelay(DebugDataIntervall); |
||
136 | Kompass_Timer = SetDelay(220); |
||
137 | |||
138 | VersionInfo.SWMajor = VERSION_MAJOR; |
||
139 | VersionInfo.SWMinor = VERSION_MINOR; |
||
140 | VersionInfo.SWPatch = VERSION_PATCH; |
||
141 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
142 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
143 | |||
144 | pRxData = 0; |
||
145 | RxDataLen = 0; |
||
146 | } |
||
147 | //------------------------------------------------------------------------------------------------------------------ |
||
148 | |||
149 | |||
150 | |||
151 | //----------------------------------------------------------------------------------------------------- |
||
152 | // Interrupt due to USART0 TX Complete |
||
153 | //----------------------------------------------------------------------------------------------------- |
||
154 | ISR(USART0_TX_vect) |
||
155 | { |
||
156 | static unsigned int ptr = 0; |
||
157 | unsigned char tmp_tx; |
||
158 | if(!UebertragungAbgeschlossen) |
||
159 | { |
||
160 | ptr++; // die [0] wurde schon gesendet |
||
161 | tmp_tx = SendeBuffer[ptr]; |
||
162 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
||
163 | { |
||
164 | ptr = 0; |
||
165 | UebertragungAbgeschlossen = 1; |
||
166 | } |
||
167 | UDR = tmp_tx; |
||
168 | } |
||
169 | else ptr = 0; |
||
170 | } |
||
171 | //----------------------------------------------------------------------------------------------------- |
||
172 | |||
173 | |||
174 | |||
175 | //----------------------------------------------------------------------------------------------------- |
||
176 | // Interrupt due to USART0 RX Complete |
||
177 | //----------------------------------------------------------------------------------------------------- |
||
178 | ISR(USART0_RX_vect) |
||
179 | { |
||
180 | static unsigned int crc; |
||
181 | static unsigned char crc1,crc2,buf_ptr; |
||
182 | static unsigned char UartState = 0; |
||
183 | unsigned char CrcOkay = 0; |
||
184 | |||
185 | SioTmp = UDR; |
||
186 | if(buf_ptr >= MAX_EMPFANGS_BUFF) UartState = 0; |
||
187 | if(SioTmp == '\r' && UartState == 2) |
||
188 | { |
||
189 | UartState = 0; |
||
190 | crc -= RxdBuffer[buf_ptr-2]; |
||
191 | crc -= RxdBuffer[buf_ptr-1]; |
||
192 | crc %= 4096; |
||
193 | crc1 = '=' + crc / 64; |
||
194 | crc2 = '=' + crc % 64; |
||
195 | CrcOkay = 0; |
||
196 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
||
197 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
||
198 | { |
||
199 | NeuerDatensatzEmpfangen = 1; |
||
200 | AnzahlEmpfangsBytes = buf_ptr + 1; |
||
201 | RxdBuffer[buf_ptr] = '\r'; |
||
202 | if(RxdBuffer[2] == 'R') |
||
203 | { |
||
204 | wdt_enable(WDTO_250MS); // Reset-Commando |
||
205 | ServoActive = 0; |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | else |
||
210 | switch(UartState) |
||
211 | { |
||
212 | case 0: |
||
213 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
||
214 | buf_ptr = 0; |
||
215 | RxdBuffer[buf_ptr++] = SioTmp; |
||
216 | crc = SioTmp; |
||
217 | break; |
||
218 | |||
219 | case 1: // Adresse auswerten |
||
220 | UartState++; |
||
221 | RxdBuffer[buf_ptr++] = SioTmp; |
||
222 | crc += SioTmp; |
||
223 | break; |
||
224 | |||
225 | case 2: // Eingangsdaten sammeln |
||
226 | RxdBuffer[buf_ptr] = SioTmp; |
||
227 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
||
228 | else UartState = 0; |
||
229 | crc += SioTmp; |
||
230 | break; |
||
231 | |||
232 | default: |
||
233 | UartState = 0; |
||
234 | break; |
||
235 | } |
||
236 | } |
||
237 | //----------------------------------------------------------------------------------------------------- |
||
238 | |||
239 | |||
240 | |||
241 | // -------------------------------------------------------------------------- |
||
242 | // checksum |
||
243 | // -------------------------------------------------------------------------- |
||
244 | void AddCRC(unsigned int wieviele) |
||
245 | { |
||
246 | unsigned int tmpCRC = 0,i; |
||
247 | for(i = 0; i < wieviele;i++) |
||
248 | { |
||
249 | tmpCRC += SendeBuffer[i]; |
||
250 | } |
||
251 | tmpCRC %= 4096; |
||
252 | SendeBuffer[i++] = '=' + tmpCRC / 64; |
||
253 | SendeBuffer[i++] = '=' + tmpCRC % 64; |
||
254 | SendeBuffer[i++] = '\r'; |
||
255 | UebertragungAbgeschlossen = 0; |
||
256 | UDR = SendeBuffer[0]; |
||
257 | } |
||
258 | //----------------------------------------------------------------------------------------------------- |
||
259 | |||
260 | |||
261 | |||
262 | // --------------------------------------------------------------------------------------------------------------------------- |
||
263 | //unsigned char *snd, unsigned char len) |
||
264 | // --------------------------------------------------------------------------------------------------------------------------- |
||
265 | void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) |
||
266 | { |
||
267 | va_list ap; |
||
268 | unsigned int pt = 0; |
||
269 | unsigned char a,b,c; |
||
270 | unsigned char ptr = 0; |
||
271 | |||
272 | unsigned char *snd = 0; |
||
273 | int len = 0; |
||
274 | |||
275 | SendeBuffer[pt++] = '#'; // Startzeichen |
||
276 | SendeBuffer[pt++] = 'a' + address; // Adresse (a=0; b=1,...) |
||
277 | SendeBuffer[pt++] = cmd; // Commando |
||
278 | |||
279 | va_start(ap, BufferAnzahl); |
||
280 | if(BufferAnzahl) |
||
281 | { |
||
282 | snd = va_arg(ap, unsigned char*); |
||
283 | len = va_arg(ap, int); |
||
284 | ptr = 0; |
||
285 | BufferAnzahl--; |
||
286 | } |
||
287 | while(len) |
||
288 | { |
||
289 | if(len) |
||
290 | { |
||
291 | a = snd[ptr++]; |
||
292 | len--; |
||
293 | if((!len) && BufferAnzahl) |
||
294 | { |
||
295 | snd = va_arg(ap, unsigned char*); |
||
296 | len = va_arg(ap, int); |
||
297 | ptr = 0; |
||
298 | BufferAnzahl--; |
||
299 | } |
||
300 | } |
||
301 | else a = 0; |
||
302 | if(len) |
||
303 | { |
||
304 | b = snd[ptr++]; |
||
305 | len--; |
||
306 | if((!len) && BufferAnzahl) |
||
307 | { |
||
308 | snd = va_arg(ap, unsigned char*); |
||
309 | len = va_arg(ap, int); |
||
310 | ptr = 0; |
||
311 | BufferAnzahl--; |
||
312 | } |
||
313 | } |
||
314 | else b = 0; |
||
315 | if(len) |
||
316 | { |
||
317 | c = snd[ptr++]; |
||
318 | len--; |
||
319 | if((!len) && BufferAnzahl) |
||
320 | { |
||
321 | snd = va_arg(ap, unsigned char*); |
||
322 | len = va_arg(ap, int); |
||
323 | ptr = 0; |
||
324 | BufferAnzahl--; |
||
325 | } |
||
326 | } |
||
327 | else c = 0; |
||
328 | SendeBuffer[pt++] = '=' + (a >> 2); |
||
329 | SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
330 | SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
331 | SendeBuffer[pt++] = '=' + ( c & 0x3f); |
||
332 | } |
||
333 | va_end(ap); |
||
334 | AddCRC(pt); |
||
335 | } |
||
336 | // EOF : SendOutData() ---------------------------------------------------------------------------------------------------- |
||
337 | |||
338 | |||
339 | |||
340 | // ---------------------------------------------------------------------------------------------------------------------- |
||
341 | // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden. |
||
342 | // ---------------------------------------------------------------------------------------------------------------------- |
||
343 | void Decode64(void) |
||
344 | { |
||
345 | unsigned char a,b,c,d; |
||
346 | unsigned char x,y,z; |
||
347 | unsigned char ptrIn = 3; // start at begin of data block |
||
348 | unsigned char ptrOut = 3; |
||
349 | 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. |
||
350 | |||
351 | while(len) |
||
352 | { |
||
353 | a = RxdBuffer[ptrIn++] - '='; |
||
354 | b = RxdBuffer[ptrIn++] - '='; |
||
355 | c = RxdBuffer[ptrIn++] - '='; |
||
356 | d = RxdBuffer[ptrIn++] - '='; |
||
357 | |||
358 | x = (a << 2) | (b >> 4); |
||
359 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
360 | z = ((c & 0x03) << 6) | d; |
||
361 | |||
362 | if(len--) RxdBuffer[ptrOut++] = x; else break; |
||
363 | if(len--) RxdBuffer[ptrOut++] = y; else break; |
||
364 | if(len--) RxdBuffer[ptrOut++] = z; else break; |
||
365 | } |
||
366 | pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte |
||
367 | RxDataLen = ptrOut - 3; // wie viele Bytes wurden dekodiert? |
||
368 | |||
369 | } |
||
370 | //----------------------------------------------------------------------------------------------------- |
||
371 | |||
372 | |||
373 | |||
374 | // ---------------------------------------------------------------------------------------------------------------------- |
||
375 | void BearbeiteRxDaten(void) |
||
376 | { |
||
377 | if(!NeuerDatensatzEmpfangen) return; |
||
378 | |||
379 | unsigned char tempchar1, tempchar2; |
||
380 | Decode64(); // dekodiere datenblock im Empfangsbuffer |
||
381 | switch(RxdBuffer[1]-'a') // check for Slave Address |
||
382 | { |
||
383 | case FC_ADDRESS: // FC special commands |
||
384 | |||
385 | switch(RxdBuffer[2]) |
||
386 | { |
||
387 | case 'K': // Kompasswert |
||
388 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
389 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
390 | break; |
||
391 | case 't': // Motortest |
||
392 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
||
393 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
||
394 | PC_MotortestActive = 240; |
||
395 | //while(!UebertragungAbgeschlossen); |
||
396 | //SendOutData('T', MeineSlaveAdresse, 0); |
||
397 | PcZugriff = 255; |
||
398 | break; |
||
399 | |||
400 | case 'n': // "Get Mixer |
||
401 | while(!UebertragungAbgeschlossen); |
||
402 | SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer,sizeof(Mixer)); |
||
403 | break; |
||
404 | |||
405 | case 'm': // "Write Mixer |
||
406 | while(!UebertragungAbgeschlossen); |
||
407 | if(pRxData[0] == MIXER_REVISION) |
||
408 | { |
||
409 | memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer)); |
||
410 | eeprom_write_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer)); |
||
411 | tempchar1 = 1; |
||
412 | } |
||
413 | else tempchar1 = 0; |
||
414 | SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
415 | break; |
||
416 | |||
417 | case 'p': // get PPM Channels |
||
418 | GetPPMChannelAnforderung = 1; |
||
419 | break; |
||
420 | |||
421 | case 'q': // "Get"-Anforderung für Settings |
||
422 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
423 | if(pRxData[0] == 0xFF) |
||
424 | { |
||
425 | pRxData[0] = GetActiveParamSetNumber(); |
||
426 | } |
||
427 | // limit settings range |
||
428 | if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5 |
||
429 | else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5 |
||
430 | // load requested parameter set |
||
431 | ReadParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); // #define STRUCT_PARAM_LAENGE sizeof(EE_Parameter) |
||
432 | |||
433 | while(!UebertragungAbgeschlossen); |
||
434 | tempchar1 = pRxData[0]; |
||
435 | tempchar2 = EE_DATENREVISION; // #define EE_DATENREVISION 80 // Parameter fürs Koptertool; entspricht den EEPROM-Daten von FlightCtrl Version V0.76g |
||
436 | SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); // #define STRUCT_PARAM_LAENGE sizeof(EE_Parameter) |
||
437 | break; |
||
438 | |||
439 | case 's': // Parametersatz speichern |
||
440 | |||
441 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EE_DATENREVISION)) // check for setting to be in range |
||
442 | { |
||
443 | memcpy((unsigned char *) &EE_Parameter.Kanalbelegung[0], (unsigned char *)&pRxData[2], STRUCT_PARAM_LAENGE); // #define STRUCT_PARAM_LAENGE sizeof(EE_Parameter) |
||
444 | WriteParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
445 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
446 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
447 | SetActiveParamSetNumber(pRxData[0]); |
||
448 | tempchar1 = GetActiveParamSetNumber(); |
||
449 | LipoDetection(0); // jump tp main.c line 59 |
||
450 | Piep(tempchar1,110); |
||
451 | } |
||
452 | else |
||
453 | { |
||
454 | tempchar1 = 0; // mark in response an invlid setting |
||
455 | } |
||
456 | while(!UebertragungAbgeschlossen); |
||
457 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
||
458 | break; |
||
459 | |||
460 | } // EOF : case FC_ADDRESS |
||
461 | |||
462 | default: // any Slave Address |
||
463 | |||
464 | switch(RxdBuffer[2]) |
||
465 | { |
||
466 | // 't' comand placed here only for compatibility to BL |
||
467 | case 't': // Motortest |
||
468 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
||
469 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
||
470 | while(!UebertragungAbgeschlossen); |
||
471 | SendOutData('T', MeineSlaveAdresse, 0); |
||
472 | PC_MotortestActive = 250; |
||
473 | PcZugriff = 255; |
||
474 | break; |
||
475 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
||
476 | case 'K': // Kompasswert |
||
477 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
||
478 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
479 | break; |
||
480 | case 'a': // Texte der Analogwerte |
||
481 | DebugTextAnforderung = pRxData[0]; |
||
482 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
||
483 | PcZugriff = 255; |
||
484 | break; |
||
485 | case 'b': |
||
486 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
||
487 | ConfirmFrame = ExternControl.Frame; |
||
488 | PcZugriff = 255; |
||
489 | break; |
||
490 | case 'c': // Poll the 3D-Data |
||
491 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
||
492 | Intervall3D = pRxData[0] * 10; |
||
493 | break; |
||
494 | case 'd': // Poll the debug data |
||
495 | DebugDataIntervall = pRxData[0] * 10; |
||
496 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
||
497 | break; |
||
498 | |||
499 | case 'h': // x-1 Displayzeilen |
||
500 | PcZugriff = 255; |
||
501 | RemoteKeys |= pRxData[0]; |
||
502 | if(RemoteKeys) DisplayLine = 0; |
||
503 | DebugDisplayAnforderung = 1; |
||
504 | break; |
||
505 | |||
506 | case 'l': // x-1 Displayzeilen |
||
507 | PcZugriff = 255; |
||
508 | MenuePunkt = pRxData[0]; |
||
509 | DebugDisplayAnforderung1 = 1; |
||
510 | break; |
||
511 | case 'v': // Version-Anforderung und Ausbaustufe |
||
512 | GetVersionAnforderung = 1; |
||
513 | break; |
||
514 | |||
515 | case 'g': // |
||
516 | GetExternalControl = 1; |
||
517 | break; |
||
518 | } |
||
519 | break; // default: |
||
520 | } |
||
521 | NeuerDatensatzEmpfangen = 0; |
||
522 | pRxData = 0; |
||
523 | RxDataLen = 0; |
||
524 | } |
||
525 | //----------------------------------------------------------------------------------------------------- |
||
526 | |||
527 | |||
528 | |||
529 | // -------------------------------------------------------------------------- |
||
530 | // function serial output |
||
531 | // -------------------------------------------------------------------------- |
||
532 | int uart_putchar(char c) |
||
533 | { |
||
534 | if (c == '\n') uart_putchar('\r'); |
||
535 | loop_until_bit_is_set(USR, UDRE); // Warten solange bis Zeichen gesendet wurde |
||
536 | UDR = c; // Ausgabe des Zeichens |
||
537 | |||
538 | return (0); |
||
539 | } |
||
540 | //----------------------------------------------------------------------------------------------------- |
||
541 | |||
542 | |||
543 | |||
544 | //--------------------------------------------------------------------------------------------- |
||
545 | // Datenübertragung |
||
546 | //--------------------------------------------------------------------------------------------- |
||
547 | void DatenUebertragung(void) |
||
548 | { |
||
549 | if(!UebertragungAbgeschlossen) return; |
||
550 | |||
551 | if(DebugDisplayAnforderung && UebertragungAbgeschlossen) |
||
552 | { |
||
553 | Menu(); |
||
554 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20); |
||
555 | DisplayLine++; |
||
556 | if(DisplayLine >= 4) DisplayLine = 0; |
||
557 | DebugDisplayAnforderung = 0; |
||
558 | } |
||
559 | |||
560 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
||
561 | { |
||
562 | Menu(); |
||
563 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
||
564 | DebugDisplayAnforderung1 = 0; |
||
565 | } |
||
566 | |||
567 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
568 | { |
||
569 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
||
570 | GetVersionAnforderung = 0; |
||
571 | } |
||
572 | |||
573 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
574 | { |
||
575 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
||
576 | GetExternalControl = 0; |
||
577 | } |
||
578 | |||
579 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
||
580 | { |
||
581 | WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
582 | WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
583 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
||
584 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
||
585 | SendOutData('w', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
||
586 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
||
587 | Kompass_Timer = SetDelay(99); |
||
588 | } |
||
589 | |||
590 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
||
591 | { |
||
592 | //if(Poti3 > 64) |
||
593 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
||
594 | DebugDataAnforderung = 0; |
||
595 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
||
596 | } |
||
597 | |||
598 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
||
599 | { |
||
600 | Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
601 | Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
||
602 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
||
603 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
||
604 | Timer3D = SetDelay(Intervall3D); |
||
605 | } |
||
606 | |||
607 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
||
608 | { |
||
609 | SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),(unsigned char *) ANALOG_TEXT[DebugTextAnforderung], 16); |
||
610 | DebugTextAnforderung = 255; |
||
611 | } |
||
612 | |||
613 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
||
614 | { |
||
615 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
||
616 | ConfirmFrame = 0; |
||
617 | } |
||
618 | |||
619 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
||
620 | { |
||
621 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
||
622 | GetPPMChannelAnforderung = 0; |
||
623 | } |
||
624 | } |
||
625 | //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |