Rev 838 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
838 | MikeW | 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 | |||
20 | unsigned volatile char CntCrcError = 0; |
||
21 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
||
22 | unsigned volatile char PC_DebugTimeout = 0; |
||
23 | unsigned char NurKanalAnforderung = 0; |
||
24 | unsigned char PcZugriff = 100; |
||
25 | unsigned char MotorTest[4] = {0,0,0,0}; |
||
26 | unsigned char MeineSlaveAdresse; |
||
27 | struct str_DebugOut DebugOut; |
||
28 | struct str_Debug DebugIn; |
||
29 | struct str_VersionInfo VersionInfo; |
||
30 | int Debug_Timer; |
||
31 | |||
32 | |||
33 | |||
34 | |||
35 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
36 | //++ Sende-Part der Datenübertragung |
||
37 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
38 | SIGNAL(INT_VEC_TX) |
||
39 | { |
||
966 | MikeW | 40 | static unsigned int ptr = 0; |
41 | unsigned char tmp_tx; |
||
42 | if(!UebertragungAbgeschlossen) |
||
838 | MikeW | 43 | { |
966 | MikeW | 44 | ptr++; // die [0] wurde schon gesendet |
45 | tmp_tx = SendeBuffer[ptr]; |
||
46 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
||
838 | MikeW | 47 | { |
966 | MikeW | 48 | ptr = 0; |
49 | UebertragungAbgeschlossen = 1; |
||
838 | MikeW | 50 | } |
966 | MikeW | 51 | UDR = tmp_tx; |
838 | MikeW | 52 | } |
53 | else ptr = 0; |
||
54 | } |
||
55 | |||
56 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
57 | //++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung |
||
58 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
59 | SIGNAL(INT_VEC_RX) |
||
60 | { |
||
966 | MikeW | 61 | static unsigned int crc; |
62 | static unsigned char crc1,crc2,buf_ptr; |
||
63 | static unsigned char UartState = 0; |
||
64 | unsigned char CrcOkay = 0; |
||
65 | |||
66 | SioTmp = UDR; |
||
67 | if(buf_ptr >= MAX_EMPFANGS_BUFF) UartState = 0; |
||
68 | if(SioTmp == '\r' && UartState == 2) |
||
838 | MikeW | 69 | { |
966 | MikeW | 70 | UartState = 0; |
71 | crc -= RxdBuffer[buf_ptr-2]; |
||
72 | crc -= RxdBuffer[buf_ptr-1]; |
||
73 | crc %= 4096; |
||
74 | crc1 = '=' + crc / 64; |
||
75 | crc2 = '=' + crc % 64; |
||
76 | CrcOkay = 0; |
||
77 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
||
78 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
||
838 | MikeW | 79 | { |
966 | MikeW | 80 | NeuerDatensatzEmpfangen = 1; |
81 | AnzahlEmpfangsBytes = buf_ptr; |
||
82 | RxdBuffer[buf_ptr] = '\r'; |
||
83 | if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando |
||
84 | } |
||
838 | MikeW | 85 | } |
86 | else |
||
966 | MikeW | 87 | switch(UartState) |
838 | MikeW | 88 | { |
966 | MikeW | 89 | case 0: |
90 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
||
91 | buf_ptr = 0; |
||
92 | RxdBuffer[buf_ptr++] = SioTmp; |
||
93 | crc = SioTmp; |
||
94 | break; |
||
95 | case 1: // Adresse auswerten |
||
96 | UartState++; |
||
97 | RxdBuffer[buf_ptr++] = SioTmp; |
||
98 | crc += SioTmp; |
||
99 | break; |
||
100 | case 2: // Eingangsdaten sammeln |
||
101 | RxdBuffer[buf_ptr] = SioTmp; |
||
102 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
||
103 | else UartState = 0; |
||
104 | crc += SioTmp; |
||
105 | break; |
||
106 | default: |
||
107 | UartState = 0; |
||
108 | break; |
||
838 | MikeW | 109 | } |
110 | } |
||
111 | |||
112 | |||
113 | // -------------------------------------------------------------------------- |
||
114 | void AddCRC(unsigned int wieviele) |
||
115 | { |
||
116 | unsigned int tmpCRC = 0,i; |
||
117 | for(i = 0; i < wieviele;i++) |
||
118 | { |
||
119 | tmpCRC += SendeBuffer[i]; |
||
120 | } |
||
121 | tmpCRC %= 4096; |
||
122 | SendeBuffer[i++] = '=' + tmpCRC / 64; |
||
123 | SendeBuffer[i++] = '=' + tmpCRC % 64; |
||
124 | SendeBuffer[i++] = '\r'; |
||
125 | UebertragungAbgeschlossen = 0; |
||
126 | UDR = SendeBuffer[0]; |
||
127 | } |
||
128 | |||
129 | |||
130 | |||
131 | // -------------------------------------------------------------------------- |
||
132 | void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len) |
||
133 | { |
||
134 | unsigned int pt = 0; |
||
135 | unsigned char a,b,c; |
||
136 | unsigned char ptr = 0; |
||
137 | |||
138 | SendeBuffer[pt++] = '#'; // Startzeichen |
||
139 | SendeBuffer[pt++] = modul; // Adresse (a=0; b=1,...) |
||
966 | MikeW | 140 | SendeBuffer[pt++] = cmd; // Commando |
838 | MikeW | 141 | |
142 | while(len) |
||
143 | { |
||
144 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
||
145 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
||
146 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
||
147 | SendeBuffer[pt++] = '=' + (a >> 2); |
||
148 | SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
149 | SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
150 | SendeBuffer[pt++] = '=' + ( c & 0x3f); |
||
151 | } |
||
152 | AddCRC(pt); |
||
153 | } |
||
154 | |||
155 | |||
156 | // -------------------------------------------------------------------------- |
||
157 | void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max) // Wohin mit den Daten; Wie lang; Wo im RxdBuffer |
||
158 | { |
||
159 | unsigned char a,b,c,d; |
||
160 | unsigned char ptr = 0; |
||
161 | unsigned char x,y,z; |
||
162 | while(len) |
||
163 | { |
||
164 | a = RxdBuffer[ptrIn++] - '='; |
||
165 | b = RxdBuffer[ptrIn++] - '='; |
||
166 | c = RxdBuffer[ptrIn++] - '='; |
||
167 | d = RxdBuffer[ptrIn++] - '='; |
||
168 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
169 | |||
170 | x = (a << 2) | (b >> 4); |
||
171 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
172 | z = ((c & 0x03) << 6) | d; |
||
173 | |||
174 | if(len--) ptrOut[ptr++] = x; else break; |
||
175 | if(len--) ptrOut[ptr++] = y; else break; |
||
176 | if(len--) ptrOut[ptr++] = z; else break; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | // -------------------------------------------------------------------------- |
||
181 | void BearbeiteRxDaten(void) |
||
182 | { |
||
966 | MikeW | 183 | if(!NeuerDatensatzEmpfangen) return; |
184 | unsigned char tmp_char_arr2[2] ={0,0}; |
||
185 | PcZugriff = 255; |
||
838 | MikeW | 186 | switch(RxdBuffer[2]) |
187 | { |
||
966 | MikeW | 188 | case 'c':// Debugdaten incl. Externe IOs usw |
189 | Decode64((unsigned char *) &DebugIn,sizeof(DebugIn),3,AnzahlEmpfangsBytes); |
||
190 | DebugDataAnforderung = 1; |
||
191 | break; |
||
192 | case 'h':// x-1 Displayzeilen |
||
193 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
194 | if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten |
||
195 | DebugDisplayAnforderung = 1; |
||
196 | break; |
||
197 | case 't':// Motortest |
||
198 | Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes); |
||
199 | break; |
||
200 | case 'v': // Version-Anforderung und Ausbaustufe |
||
201 | GetVersionAnforderung = 1; |
||
202 | break; |
||
203 | case 'g':// "Get"-Anforderung für Debug-Daten |
||
204 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
205 | DebugGetAnforderung = 1; |
||
206 | break; |
||
207 | case 'q':// "Get"-Anforderung für Settings |
||
208 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
209 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
210 | if(tmp_char_arr2[0] != 0xff) |
||
211 | { |
||
212 | if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; |
||
213 | ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
214 | SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
||
215 | } |
||
216 | else |
||
217 | SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
||
218 | break; |
||
219 | case 'l': |
||
220 | case 'm': |
||
221 | case 'n': |
||
222 | case 'o': |
||
223 | case 'p': // Parametersatz speichern |
||
224 | Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes); |
||
225 | WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
226 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1); // aktiven Datensatz merken |
||
227 | beeptime = 10000; |
||
228 | BeepMuster = 0x0080; |
||
229 | break; |
||
838 | MikeW | 230 | } |
966 | MikeW | 231 | NeuerDatensatzEmpfangen = 0; |
838 | MikeW | 232 | } |
233 | |||
234 | |||
235 | //############################################################################ |
||
236 | //Routine für die Serielle Ausgabe |
||
237 | int uart_putchar (char c) |
||
238 | //############################################################################ |
||
239 | { |
||
966 | MikeW | 240 | if (c == '\n') |
241 | uart_putchar('\r'); |
||
242 | //Warten solange bis Zeichen gesendet wurde |
||
243 | loop_until_bit_is_set(USR, UDRE); |
||
244 | //Ausgabe des Zeichens |
||
245 | UDR = c; |
||
246 | return (0); |
||
838 | MikeW | 247 | } |
248 | |||
249 | // -------------------------------------------------------------------------- |
||
250 | void WriteProgramData(unsigned int pos, unsigned char wert) |
||
251 | { |
||
252 | //if (ProgramLocation == IN_RAM) Buffer[pos] = wert; |
||
253 | // else eeprom_write_byte(&EE_Buffer[pos], wert); |
||
254 | // Buffer[pos] = wert; |
||
255 | } |
||
256 | |||
257 | //############################################################################ |
||
258 | //INstallation der Seriellen Schnittstelle |
||
259 | void UART_Init (void) |
||
260 | //############################################################################ |
||
261 | { |
||
966 | MikeW | 262 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
263 | |||
264 | UCR=(1 << TXEN) | (1 << RXEN); |
||
265 | // UART Double Speed (U2X) |
||
266 | USR |= (1<<U2X); |
||
267 | // RX-Interrupt Freigabe |
||
268 | UCSRB |= (1<<RXCIE); |
||
269 | // TX-Interrupt Freigabe |
||
270 | UCSRB |= (1<<TXCIE); |
||
271 | |||
272 | //Teiler wird gesetzt |
||
273 | UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1); |
||
274 | //UBRR = 33; |
||
275 | //öffnet einen Kanal für printf (STDOUT) |
||
276 | //fdevopen (uart_putchar, 0); |
||
277 | //sbi(PORTD,4); |
||
838 | MikeW | 278 | Debug_Timer = SetDelay(400); |
279 | } |
||
280 | |||
281 | //--------------------------------------------------------------------------------------------- |
||
282 | void DatenUebertragung(void) |
||
283 | { |
||
966 | MikeW | 284 | if(!UebertragungAbgeschlossen) return; |
285 | |||
286 | if(DebugGetAnforderung && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
287 | { |
||
288 | SendOutData('G',MeineSlaveAdresse,(unsigned char *) &DebugIn,sizeof(DebugIn)); |
||
289 | DebugGetAnforderung = 0; |
||
290 | } |
||
291 | |||
292 | if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
||
293 | { |
||
294 | SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut)); |
||
295 | DebugDataAnforderung = 0; |
||
296 | Debug_Timer = SetDelay(2 * MIN_DEBUG_INTERVALL); |
||
297 | } |
||
298 | |||
299 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
300 | { |
||
301 | SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo)); |
||
302 | GetVersionAnforderung = 0; |
||
303 | } |
||
838 | MikeW | 304 | } |
305 |