Rev 624 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + only for non-profit use |
||
4 | // + www.MikroKopter.com |
||
5 | // + see the File "License.txt" for further Informations |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | |||
8 | #include "main.h" |
||
9 | #include "uart.h" |
||
10 | |||
11 | unsigned char DebugGetAnforderung = 0,DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0; |
||
12 | unsigned volatile char SioTmp = 0; |
||
13 | unsigned volatile char SendeBuffer[MAX_SENDE_BUFF]; |
||
14 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
||
15 | unsigned volatile char NMEABuffer[MAX_EMPFANGS_BUFF]; |
||
16 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
||
17 | unsigned volatile char NeueKoordinateEmpfangen = 0; |
||
18 | unsigned volatile char UebertragungAbgeschlossen = 1; |
||
19 | unsigned volatile char CntCrcError = 0; |
||
20 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
||
21 | unsigned volatile char PC_DebugTimeout = 0; |
||
499 | hbuss | 22 | unsigned char RemotePollDisplayLine = 0; |
395 | hbuss | 23 | unsigned char NurKanalAnforderung = 0; |
499 | hbuss | 24 | unsigned char DebugTextAnforderung = 255; |
1 | ingob | 25 | unsigned char PcZugriff = 100; |
26 | unsigned char MotorTest[4] = {0,0,0,0}; |
||
595 | hbuss | 27 | unsigned char DubWiseKeys[4] = {0,0,0,0}; |
1 | ingob | 28 | unsigned char MeineSlaveAdresse; |
595 | hbuss | 29 | unsigned char ConfirmFrame; |
1 | ingob | 30 | struct str_DebugOut DebugOut; |
595 | hbuss | 31 | struct str_ExternControl ExternControl; |
1 | ingob | 32 | struct str_VersionInfo VersionInfo; |
33 | int Debug_Timer; |
||
34 | |||
499 | hbuss | 35 | const unsigned char ANALOG_TEXT[32][16] = |
36 | { |
||
37 | //1234567890123456 |
||
38 | "IntegralNick ", //0 |
||
39 | "IntegralRoll ", |
||
40 | "AccNick ", |
||
41 | "AccRoll ", |
||
42 | "GyroGier ", |
||
43 | "HoehenWert ", //5 |
||
44 | "AccZ ", |
||
45 | "Gas ", |
||
46 | "KompassValue ", |
||
513 | hbuss | 47 | "Spannung ", |
48 | "Empfang ", //10 |
||
49 | "Analog11 ", |
||
499 | hbuss | 50 | "Motor_Vorne ", |
51 | "Motor_Hinten ", |
||
52 | "Motor_Links ", |
||
53 | "Motor_Rechts ", //15 |
||
513 | hbuss | 54 | "Acc_Z ", |
55 | "MittelAccNick ", |
||
56 | "MittelAccRoll ", |
||
57 | "IntegralErrNick ", |
||
499 | hbuss | 58 | "IntegralErrRoll ", //20 |
513 | hbuss | 59 | "MittelIntNick ", |
60 | "MittelIntRoll ", |
||
61 | "NeutralNick ", |
||
62 | "RollOffset ", |
||
63 | "IntRoll*Faktor ", //25 |
||
623 | hbuss | 64 | "Analog26 ", |
499 | hbuss | 65 | "DirektAusglRoll ", |
513 | hbuss | 66 | "MesswertRoll ", |
67 | "AusgleichRoll ", |
||
68 | "I-LageRoll ", //30 |
||
69 | "StickRoll " |
||
499 | hbuss | 70 | }; |
71 | |||
72 | |||
73 | |||
1 | ingob | 74 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
75 | //++ Sende-Part der Datenübertragung |
||
76 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
77 | SIGNAL(INT_VEC_TX) |
||
78 | { |
||
79 | static unsigned int ptr = 0; |
||
80 | unsigned char tmp_tx; |
||
81 | if(!UebertragungAbgeschlossen) |
||
82 | { |
||
83 | ptr++; // die [0] wurde schon gesendet |
||
84 | tmp_tx = SendeBuffer[ptr]; |
||
85 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
||
86 | { |
||
87 | ptr = 0; |
||
88 | UebertragungAbgeschlossen = 1; |
||
89 | } |
||
90 | UDR = tmp_tx; |
||
91 | } |
||
92 | else ptr = 0; |
||
93 | } |
||
94 | |||
95 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
96 | //++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung |
||
97 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
98 | SIGNAL(INT_VEC_RX) |
||
99 | { |
||
100 | static unsigned int crc; |
||
101 | static unsigned char crc1,crc2,buf_ptr; |
||
102 | static unsigned char UartState = 0; |
||
103 | unsigned char CrcOkay = 0; |
||
104 | |||
105 | SioTmp = UDR; |
||
106 | if(buf_ptr >= MAX_EMPFANGS_BUFF) UartState = 0; |
||
107 | if(SioTmp == '\r' && UartState == 2) |
||
108 | { |
||
109 | UartState = 0; |
||
110 | crc -= RxdBuffer[buf_ptr-2]; |
||
111 | crc -= RxdBuffer[buf_ptr-1]; |
||
112 | crc %= 4096; |
||
113 | crc1 = '=' + crc / 64; |
||
114 | crc2 = '=' + crc % 64; |
||
115 | CrcOkay = 0; |
||
116 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
||
117 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
||
118 | { |
||
119 | NeuerDatensatzEmpfangen = 1; |
||
120 | AnzahlEmpfangsBytes = buf_ptr; |
||
121 | RxdBuffer[buf_ptr] = '\r'; |
||
173 | holgerb | 122 | if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando |
1 | ingob | 123 | } |
124 | } |
||
125 | else |
||
126 | switch(UartState) |
||
127 | { |
||
128 | case 0: |
||
129 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
||
130 | buf_ptr = 0; |
||
131 | RxdBuffer[buf_ptr++] = SioTmp; |
||
132 | crc = SioTmp; |
||
133 | break; |
||
134 | case 1: // Adresse auswerten |
||
135 | UartState++; |
||
136 | RxdBuffer[buf_ptr++] = SioTmp; |
||
137 | crc += SioTmp; |
||
138 | break; |
||
139 | case 2: // Eingangsdaten sammeln |
||
140 | RxdBuffer[buf_ptr] = SioTmp; |
||
141 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
||
142 | else UartState = 0; |
||
143 | crc += SioTmp; |
||
144 | break; |
||
145 | default: |
||
146 | UartState = 0; |
||
147 | break; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | |||
152 | // -------------------------------------------------------------------------- |
||
153 | void AddCRC(unsigned int wieviele) |
||
154 | { |
||
155 | unsigned int tmpCRC = 0,i; |
||
156 | for(i = 0; i < wieviele;i++) |
||
157 | { |
||
158 | tmpCRC += SendeBuffer[i]; |
||
159 | } |
||
160 | tmpCRC %= 4096; |
||
161 | SendeBuffer[i++] = '=' + tmpCRC / 64; |
||
162 | SendeBuffer[i++] = '=' + tmpCRC % 64; |
||
163 | SendeBuffer[i++] = '\r'; |
||
164 | UebertragungAbgeschlossen = 0; |
||
165 | UDR = SendeBuffer[0]; |
||
166 | } |
||
167 | |||
168 | |||
169 | |||
170 | // -------------------------------------------------------------------------- |
||
171 | void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len) |
||
172 | { |
||
173 | unsigned int pt = 0; |
||
174 | unsigned char a,b,c; |
||
175 | unsigned char ptr = 0; |
||
176 | |||
177 | SendeBuffer[pt++] = '#'; // Startzeichen |
||
178 | SendeBuffer[pt++] = modul; // Adresse (a=0; b=1,...) |
||
179 | SendeBuffer[pt++] = cmd; // Commando |
||
180 | |||
181 | while(len) |
||
182 | { |
||
183 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
||
184 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
||
185 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
||
186 | SendeBuffer[pt++] = '=' + (a >> 2); |
||
187 | SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
188 | SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
189 | SendeBuffer[pt++] = '=' + ( c & 0x3f); |
||
190 | } |
||
191 | AddCRC(pt); |
||
192 | } |
||
193 | |||
194 | |||
195 | // -------------------------------------------------------------------------- |
||
196 | void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max) // Wohin mit den Daten; Wie lang; Wo im RxdBuffer |
||
197 | { |
||
198 | unsigned char a,b,c,d; |
||
199 | unsigned char ptr = 0; |
||
200 | unsigned char x,y,z; |
||
201 | while(len) |
||
202 | { |
||
203 | a = RxdBuffer[ptrIn++] - '='; |
||
204 | b = RxdBuffer[ptrIn++] - '='; |
||
205 | c = RxdBuffer[ptrIn++] - '='; |
||
206 | d = RxdBuffer[ptrIn++] - '='; |
||
207 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
208 | |||
209 | x = (a << 2) | (b >> 4); |
||
210 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
211 | z = ((c & 0x03) << 6) | d; |
||
212 | |||
213 | if(len--) ptrOut[ptr++] = x; else break; |
||
214 | if(len--) ptrOut[ptr++] = y; else break; |
||
215 | if(len--) ptrOut[ptr++] = z; else break; |
||
216 | } |
||
217 | |||
218 | } |
||
219 | |||
220 | // -------------------------------------------------------------------------- |
||
221 | void BearbeiteRxDaten(void) |
||
222 | { |
||
223 | if(!NeuerDatensatzEmpfangen) return; |
||
224 | |||
304 | ingob | 225 | // unsigned int tmp_int_arr1[1]; |
226 | // unsigned int tmp_int_arr2[2]; |
||
227 | // unsigned int tmp_int_arr3[3]; |
||
1 | ingob | 228 | unsigned char tmp_char_arr2[2]; |
304 | ingob | 229 | // unsigned char tmp_char_arr3[3]; |
230 | // unsigned char tmp_char_arr4[4]; |
||
1 | ingob | 231 | //if(!MotorenEin) |
232 | PcZugriff = 255; |
||
233 | switch(RxdBuffer[2]) |
||
234 | { |
||
499 | hbuss | 235 | case 'a':// Texte der Analogwerte |
236 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
237 | DebugTextAnforderung = tmp_char_arr2[0]; |
||
238 | break; |
||
595 | hbuss | 239 | case 'b': |
240 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
||
241 | RemoteTasten |= ExternControl.RemoteTasten; |
||
242 | ConfirmFrame = ExternControl.Frame; |
||
243 | break; |
||
244 | case 'c': |
||
245 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
||
246 | RemoteTasten |= ExternControl.RemoteTasten; |
||
247 | ConfirmFrame = ExternControl.Frame; |
||
1 | ingob | 248 | DebugDataAnforderung = 1; |
249 | break; |
||
250 | case 'h':// x-1 Displayzeilen |
||
251 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
252 | RemoteTasten |= tmp_char_arr2[0]; |
||
395 | hbuss | 253 | if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten |
1 | ingob | 254 | DebugDisplayAnforderung = 1; |
255 | break; |
||
256 | case 't':// Motortest |
||
257 | Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes); |
||
258 | break; |
||
492 | hbuss | 259 | case 'k':// Keys von DubWise |
260 | Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes); |
||
595 | hbuss | 261 | ConfirmFrame = DubWiseKeys[3]; |
492 | hbuss | 262 | break; |
1 | ingob | 263 | case 'v': // Version-Anforderung und Ausbaustufe |
264 | GetVersionAnforderung = 1; |
||
265 | break; |
||
266 | case 'g':// "Get"-Anforderung für Debug-Daten |
||
267 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
268 | DebugGetAnforderung = 1; |
||
269 | break; |
||
270 | case 'q':// "Get"-Anforderung für Settings |
||
271 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
272 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
273 | if(tmp_char_arr2[0] != 0xff) |
||
274 | { |
||
275 | if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; |
||
276 | ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
277 | SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
||
278 | } |
||
279 | else |
||
280 | SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
||
281 | |||
282 | break; |
||
283 | |||
284 | case 'l': |
||
285 | case 'm': |
||
286 | case 'n': |
||
287 | case 'o': |
||
288 | case 'p': // Parametersatz speichern |
||
289 | Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes); |
||
290 | WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
291 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1); // aktiven Datensatz merken |
||
395 | hbuss | 292 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
293 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
1 | ingob | 294 | Piep(GetActiveParamSetNumber()); |
295 | break; |
||
296 | |||
297 | |||
298 | } |
||
299 | // DebugOut.AnzahlZyklen = Debug_Timer_Intervall; |
||
300 | NeuerDatensatzEmpfangen = 0; |
||
301 | } |
||
302 | |||
303 | //############################################################################ |
||
304 | //Routine für die Serielle Ausgabe |
||
305 | int uart_putchar (char c) |
||
306 | //############################################################################ |
||
307 | { |
||
308 | if (c == '\n') |
||
309 | uart_putchar('\r'); |
||
310 | //Warten solange bis Zeichen gesendet wurde |
||
311 | loop_until_bit_is_set(USR, UDRE); |
||
312 | //Ausgabe des Zeichens |
||
313 | UDR = c; |
||
314 | |||
315 | return (0); |
||
316 | } |
||
317 | |||
318 | // -------------------------------------------------------------------------- |
||
319 | void WriteProgramData(unsigned int pos, unsigned char wert) |
||
320 | { |
||
321 | //if (ProgramLocation == IN_RAM) Buffer[pos] = wert; |
||
322 | // else eeprom_write_byte(&EE_Buffer[pos], wert); |
||
323 | // Buffer[pos] = wert; |
||
324 | } |
||
325 | |||
326 | //############################################################################ |
||
327 | //INstallation der Seriellen Schnittstelle |
||
328 | void UART_Init (void) |
||
329 | //############################################################################ |
||
330 | { |
||
331 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
||
332 | |||
333 | UCR=(1 << TXEN) | (1 << RXEN); |
||
334 | // UART Double Speed (U2X) |
||
335 | USR |= (1<<U2X); |
||
336 | // RX-Interrupt Freigabe |
||
337 | UCSRB |= (1<<RXCIE); |
||
338 | // TX-Interrupt Freigabe |
||
339 | UCSRB |= (1<<TXCIE); |
||
340 | |||
341 | //Teiler wird gesetzt |
||
342 | UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1); |
||
343 | //UBRR = 33; |
||
344 | //öffnet einen Kanal für printf (STDOUT) |
||
345 | //fdevopen (uart_putchar, 0); |
||
346 | //sbi(PORTD,4); |
||
347 | Debug_Timer = SetDelay(200); |
||
348 | } |
||
349 | |||
350 | //--------------------------------------------------------------------------------------------- |
||
351 | void DatenUebertragung(void) |
||
352 | { |
||
353 | if(!UebertragungAbgeschlossen) return; |
||
354 | |||
355 | if(DebugGetAnforderung && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
356 | { |
||
595 | hbuss | 357 | SendOutData('G',MeineSlaveAdresse,(unsigned char *) &ExternControl,sizeof(ExternControl)); |
1 | ingob | 358 | DebugGetAnforderung = 0; |
359 | } |
||
360 | |||
361 | if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
||
362 | { |
||
363 | SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut)); |
||
364 | DebugDataAnforderung = 0; |
||
365 | Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL); |
||
366 | } |
||
499 | hbuss | 367 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
368 | { |
||
369 | SendOutData('A',DebugTextAnforderung + '0',(unsigned char *) ANALOG_TEXT[DebugTextAnforderung],16); |
||
370 | DebugTextAnforderung = 255; |
||
371 | } |
||
595 | hbuss | 372 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz ohne CRC bestätigen |
373 | { |
||
374 | SendeBuffer[0] = '#'; |
||
375 | SendeBuffer[1] = ConfirmFrame; |
||
376 | SendeBuffer[2] = '\r'; |
||
377 | UebertragungAbgeschlossen = 0; |
||
378 | ConfirmFrame = 0; |
||
379 | UDR = SendeBuffer[0]; |
||
380 | } |
||
1 | ingob | 381 | if(DebugDisplayAnforderung && UebertragungAbgeschlossen) |
382 | { |
||
383 | Menu(); |
||
384 | DebugDisplayAnforderung = 0; |
||
499 | hbuss | 385 | if(++RemotePollDisplayLine == 4 || NurKanalAnforderung) |
173 | holgerb | 386 | { |
304 | ingob | 387 | SendOutData('4',0,(unsigned char *)&PPM_in,sizeof(PPM_in)); // DisplayZeile übertragen |
499 | hbuss | 388 | RemotePollDisplayLine = -1; |
173 | holgerb | 389 | } |
499 | hbuss | 390 | else SendOutData('0' + RemotePollDisplayLine,0,(unsigned char *)&DisplayBuff[20 * RemotePollDisplayLine],20); // DisplayZeile übertragen |
1 | ingob | 391 | } |
392 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
393 | { |
||
394 | SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo)); |
||
395 | GetVersionAnforderung = 0; |
||
396 | } |
||
397 | |||
398 | } |
||
399 |