Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1734 | - | 1 | /***************************************************************************** |
2 | * Copyright (C) 2008 Thomas Kaiser, thomas@ft-fanpage.de * |
||
3 | * Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net * |
||
4 | * Copyright (C) 2011 Christian "Cebra" Brandtner, brandtner@brandtner.net * |
||
5 | * Copyright (C) 2011 Harald Bongartz * |
||
6 | * * |
||
7 | * This program is free software; you can redistribute it and/or modify * |
||
8 | * it under the terms of the GNU General Public License as published by * |
||
9 | * the Free Software Foundation; either version 2 of the License. * |
||
10 | * * |
||
11 | * This program is distributed in the hope that it will be useful, * |
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
14 | * GNU General Public License for more details. * |
||
15 | * * |
||
16 | * You should have received a copy of the GNU General Public License * |
||
17 | * along with this program; if not, write to the * |
||
18 | * Free Software Foundation, Inc., * |
||
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
20 | * * |
||
21 | * * |
||
22 | * Credits to: * |
||
23 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN * |
||
24 | * http://www.mikrokopter.de * |
||
25 | * Gregor "killagreg" Stobrawa for his version of the MK code * |
||
26 | * Thomas Kaiser "thkais" for the original project. See * |
||
27 | * http://www.ft-fanpage.de/mikrokopter/ * |
||
28 | * http://forum.mikrokopter.de/topic-4061-1.html * |
||
29 | * Claas Anders "CaScAdE" Rathje for providing the font and his C-OSD code * |
||
30 | * http://www.mylifesucks.de/oss/c-osd/ * |
||
31 | * Harald Bongartz "HaraldB" for providing his Ideas and Code for usibility* |
||
32 | *****************************************************************************/ |
||
33 | |||
34 | |||
35 | #include "cpu.h" |
||
36 | #include <avr/io.h> |
||
37 | #include <avr/pgmspace.h> |
||
38 | #include <util/delay.h> |
||
39 | #include <stdlib.h> |
||
40 | #include <string.h> |
||
41 | #include "lcd.h" |
||
42 | #include "usart.h" |
||
43 | #include "uart1.h" |
||
44 | #include "main.h" |
||
45 | #include "Wi232.h" |
||
46 | #include "timer.h" |
||
47 | #include "eeprom.h" |
||
48 | |||
49 | |||
50 | |||
51 | |||
52 | |||
53 | #if defined HWVERSION1_3W || defined HWVERSION3_9 || defined HWVERSION1_2W |
||
54 | |||
55 | uint8_t Wi232_hardware = 0; |
||
56 | uint8_t InitErr=0; |
||
57 | uint8_t Wi232_Baudrate = 0; //Merkzelle für aktuelle Baudrate |
||
58 | uint8_t New_Baudrate = 0; //Merkzelle für zu setzende Baudrate |
||
59 | uint8_t Old_Baudrate = 0; //Merkzelle für alte Baudrate |
||
60 | |||
61 | |||
62 | void SetUart0_Wi232(uint8_t Baudrate) |
||
63 | { |
||
64 | switch (Baudrate) |
||
65 | { |
||
66 | case Wi232_2400: USART_Init( UART_BAUD_SELECT(2400,F_CPU) ); break; /* erstmal mit 2400 versuchen*/ |
||
67 | case Wi232_9600: USART_Init( UART_BAUD_SELECT(9600,F_CPU) ); break; /* erstmal mit 9600 versuchen*/ |
||
68 | case Wi232_19200: USART_Init( UART_BAUD_SELECT(19200,F_CPU) ); break; /* erstmal mit 19200 versuchen*/ |
||
69 | case Wi232_38400: USART_Init( UART_BAUD_SELECT(38400,F_CPU) ); break; /* erstmal mit 38400 versuchen*/ |
||
70 | case Wi232_57600: USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); break; /* erstmal mit 57600 versuchen*/ |
||
71 | // case Wi232_115200: USART_Init( UART_BAUD_SELECT(115200,F_CPU) ); break; /* erstmal mit 115200 versuchen*/ |
||
72 | // Macro erechnet falschen Wert (9,85 = 9) für 115200 Baud mit 20Mhz Quarz, zu grosse Abweichung |
||
73 | |||
74 | #warning "Baurate prüfen wenn kein 20 Mhz Quarz verwendet wird" |
||
75 | |||
76 | case Wi232_115200: USART_Init( 10 ); break; /* erstmal mit 115200 versuchen*/ |
||
77 | } |
||
78 | } |
||
79 | void SetUart1_Wi232(uint8_t Baudrate) |
||
80 | { |
||
81 | switch (Baudrate) |
||
82 | { |
||
83 | case Wi232_2400: uart1_init( UART_BAUD_SELECT(2400,F_CPU) ); break; /* erstmal mit 2400 versuchen*/ |
||
84 | case Wi232_9600: uart1_init( UART_BAUD_SELECT(9600,F_CPU) ); break; /* erstmal mit 9600 versuchen*/ |
||
85 | case Wi232_19200: uart1_init( UART_BAUD_SELECT(19200,F_CPU) ); break; /* erstmal mit 19200 versuchen*/ |
||
86 | case Wi232_38400: uart1_init( UART_BAUD_SELECT(38400,F_CPU) ); break; /* erstmal mit 38400 versuchen*/ |
||
87 | case Wi232_57600: uart1_init( UART_BAUD_SELECT(57600,F_CPU) ); break; /* erstmal mit 57600 versuchen*/ |
||
88 | // case Wi232_115200: uart1_init( UART_BAUD_SELECT(115200,F_CPU) ); break; /* erstmal mit 115200 versuchen*/ |
||
89 | case Wi232_115200: uart1_init( 10 ); break; /* erstmal mit 115200 versuchen*/ |
||
90 | } |
||
91 | } |
||
92 | |||
93 | |||
94 | |||
95 | /************************************************************************* |
||
96 | Function: discoverWI232() |
||
97 | Purpose: check if Wi232 available |
||
98 | Returns: Version or 0 = timeout |
||
99 | |||
100 | **************************************************************************/ |
||
101 | void discoverWi232(uint8_t Baudrate) |
||
102 | |||
103 | { |
||
104 | int16_t RegisterWi232; |
||
105 | |||
106 | SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
||
107 | set_WI232CMD(); |
||
108 | _delay_ms(200); |
||
109 | lcd_cls(); |
||
110 | SetUart0_Wi232( Baudrate); |
||
111 | |||
112 | // switch (PKT_Baudrate) |
||
113 | // { |
||
114 | // case Wi232_2400: USART_Init( UART_BAUD_SELECT(2400,F_CPU) ); break; /* erstmal mit 2400 versuchen*/ |
||
115 | // case Wi232_9600: USART_Init( UART_BAUD_SELECT(9600,F_CPU) ); break; /* erstmal mit 9600 versuchen*/ |
||
116 | // case Wi232_19200: USART_Init( UART_BAUD_SELECT(19200,F_CPU) ); break; /* erstmal mit 19200 versuchen*/ |
||
117 | // case Wi232_38400: USART_Init( UART_BAUD_SELECT(38400,F_CPU) ); break; /* erstmal mit 38400 versuchen*/ |
||
118 | // case Wi232_57600: USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); break; /* erstmal mit 57600 versuchen*/ |
||
119 | // case Wi232_115200: USART_Init( UART_BAUD_SELECT(115200,F_CPU) ); break; /* erstmal mit 115200 versuchen*/ |
||
120 | // } |
||
121 | |||
122 | lcd_printpns_at (0, 0, PSTR("search Wi.232 Modul"),0); |
||
123 | RegisterWi232 = ReadWi232(regDiscover); |
||
124 | lcd_print_hex_at(18,0,RegisterWi232,0); |
||
125 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
126 | { Wi232_hardware = 1; // Wi232 mit aktueller Baudrate gefunden |
||
127 | Wi232_Baudrate = PKT_Baudrate;} |
||
128 | |||
129 | if (RegisterWi232 == 0) // nicht gefunden, dann alle Baudraten durchsuchen |
||
130 | { |
||
131 | USART_Init( UART_BAUD_SELECT(2400,F_CPU) ); /* neues Modul mit 2400 suchen*/ |
||
132 | lcd_printpns_at (0, 1, PSTR(" with 2400 Baud "),0); |
||
133 | RegisterWi232 = ReadWi232(regDiscover); |
||
134 | lcd_print_hex_at(18,1,RegisterWi232,0); |
||
135 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
136 | { Wi232_hardware = 2; |
||
137 | Wi232_Baudrate = Wi232_2400; } |
||
138 | |||
139 | if (RegisterWi232 == 0) |
||
140 | { |
||
141 | USART_Init( UART_BAUD_SELECT(9600,F_CPU) ); /* 9600 suchen*/ |
||
142 | lcd_printpns_at (0, 2, PSTR(" with 9600 Baud "),0); |
||
143 | RegisterWi232 = ReadWi232(regDiscover); |
||
144 | lcd_print_hex_at(18,2,RegisterWi232,0); |
||
145 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
146 | { Wi232_hardware = 2; |
||
147 | Wi232_Baudrate = Wi232_9600; } |
||
148 | } |
||
149 | |||
150 | if (RegisterWi232 == 0) |
||
151 | { |
||
152 | USART_Init( UART_BAUD_SELECT(19200,F_CPU) ); /* 19200 suchen*/ |
||
153 | lcd_printpns_at (0, 3, PSTR(" with 19200 Baud "),0); |
||
154 | RegisterWi232 = ReadWi232(regDiscover); |
||
155 | lcd_print_hex_at(18,3,RegisterWi232,0); |
||
156 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
157 | { Wi232_hardware = 2; |
||
158 | Wi232_Baudrate = Wi232_19200; } |
||
159 | } |
||
160 | if (RegisterWi232 == 0) |
||
161 | { |
||
162 | USART_Init( UART_BAUD_SELECT(38400,F_CPU) ); /* 38400 suchen*/ |
||
163 | lcd_printpns_at (0, 4, PSTR(" with 38400 Baud"),0); |
||
164 | RegisterWi232 = ReadWi232(regDiscover); |
||
165 | lcd_print_hex_at(18,4,RegisterWi232,0); |
||
166 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
167 | { Wi232_hardware = 2; |
||
168 | Wi232_Baudrate = Wi232_38400; } |
||
169 | } |
||
170 | if (RegisterWi232 == 0 ) |
||
171 | { |
||
172 | USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); /* mit 57600 suchen*/ |
||
173 | lcd_printpns_at (0, 5, PSTR(" with 57600 Baud"),0); |
||
174 | RegisterWi232 = ReadWi232(regDiscover); |
||
175 | lcd_print_hex_at(18,5,RegisterWi232,0); |
||
176 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
177 | { Wi232_hardware = 2; |
||
178 | Wi232_Baudrate = Wi232_57600; } |
||
179 | } |
||
180 | if (RegisterWi232 == 0) |
||
181 | { |
||
182 | USART_Init( UART_BAUD_SELECT(115200,F_CPU) ); /* 115200 suchen*/ |
||
183 | lcd_printpns_at (0, 6, PSTR(" with 115200 Baud"),0); |
||
184 | RegisterWi232 = ReadWi232(regDiscover); |
||
185 | lcd_print_hex_at(18,6,RegisterWi232,0); |
||
186 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
187 | { Wi232_hardware = 2; |
||
188 | Wi232_Baudrate = Wi232_115200; } |
||
189 | } |
||
190 | |||
191 | _delay_ms(2000); |
||
192 | |||
193 | } |
||
194 | |||
195 | if (RegisterWi232 == 0) |
||
196 | { |
||
197 | lcd_cls(); |
||
198 | lcd_printpns_at (0, 0, PSTR("no Wi.232 found"),0); |
||
199 | Wi232_hardware = 0; |
||
200 | _delay_ms(2000); |
||
201 | } |
||
202 | |||
203 | if (RegisterWi232 == 0xFF) |
||
204 | { |
||
205 | lcd_cls(); |
||
206 | lcd_printpns_at (0, 0, PSTR("Wi.232 Sytaxerror "),0); |
||
207 | set_beep ( 1000, 0x0040, BeepNormal); |
||
208 | _delay_ms(2000); |
||
209 | RegisterWi232 = 0; |
||
210 | } |
||
211 | |||
212 | if (RegisterWi232 > 0 && RegisterWi232 < 0xFF) |
||
213 | { |
||
214 | lcd_cls(); |
||
215 | if (Wi232_hardware ==1) // alles ok , Baudrate Wi232 passt |
||
216 | lcd_printpns_at (0, 0, PSTR("Wi.232 found "),0); |
||
217 | |||
218 | if (Wi232_hardware ==2) // Wi232 gefunden, aber falsche Baudrate |
||
219 | { |
||
220 | WiIsSet= false; //wenn hier 2400 gefunden wurde, ist Wi232 nicht initialisiert |
||
221 | lcd_printpns_at (0, 0, PSTR("Wi.232 wrong Baudrate"),0); |
||
222 | if (WriteWi232(regNVDATARATE,PKT_Baudrate)!=0) /* NV-Ram auf PKT-Baudrate setzen*/ |
||
223 | { |
||
224 | lcd_printpns_at (0, 1, PSTR("Error set NV-RAM"),0); |
||
225 | set_beep ( 1000, 0x0040, BeepNormal); |
||
226 | _delay_ms(2000); |
||
227 | } |
||
228 | else |
||
229 | { |
||
230 | _delay_ms(1000); |
||
231 | lcd_printpns_at (0, 1, PSTR("NV-RAM is set to "),0); |
||
232 | lcd_printpns_at (0, 2, PSTR("given Baudrate"),0); |
||
233 | _delay_ms(2000); |
||
234 | } |
||
235 | |||
236 | if (WriteWi232(regDATARATE,PKT_Baudrate)!=0) /* Ram auf PKT_Baudrate setzen*/ |
||
237 | { |
||
238 | lcd_printpns_at (0, 3, PSTR("Error set RAM "),0); |
||
239 | set_beep ( 1000, 0x0040, BeepNormal); |
||
240 | _delay_ms(2000); |
||
241 | } |
||
242 | else |
||
243 | { |
||
244 | _delay_ms(1000); |
||
245 | lcd_printpns_at (0, 1, PSTR("RAM is set to "),0); |
||
246 | lcd_printpns_at (0, 2, PSTR("given Baudrate"),0); |
||
247 | _delay_ms(2000); |
||
248 | } |
||
249 | |||
250 | SetUart0_Wi232(PKT_Baudrate); |
||
251 | Old_Baudrate = PKT_Baudrate; |
||
252 | |||
253 | } |
||
254 | lcd_cls_line (0,1,21); |
||
255 | lcd_printpns_at (0, 1, PSTR("Version:"),0); |
||
256 | lcd_print_hex_at(9,1,RegisterWi232,0); |
||
257 | } |
||
258 | clr_WI232CMD(); |
||
259 | } |
||
260 | |||
261 | |||
262 | /************************************************************************* |
||
263 | Function: InitWI232() |
||
264 | Purpose: set Wi232Register for Mikrokopter |
||
265 | Returns: 0 = ACK, FF = NAK |
||
266 | |||
267 | **************************************************************************/ |
||
268 | void InitWi232(uint8_t Baudrate) |
||
269 | { |
||
270 | |||
271 | uint8_t i = 0; |
||
272 | #ifdef HWVERSION3_9 |
||
273 | Change_Output(Uart02Wi); // Verbindung zu Wi232 herstellen |
||
274 | #endif |
||
275 | |||
276 | discoverWi232(Old_Baudrate); // Check if Wi232 available |
||
277 | |||
278 | if (Wi232_hardware != 0) |
||
279 | { |
||
280 | lcd_printpns_at (0, 2, PSTR("Init Wi232 wait...."),0); |
||
281 | set_WI232CMD(); |
||
282 | _delay_ms(200); |
||
283 | SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
||
284 | // USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); /* erstmal mit 57600 versuchen*/ |
||
285 | |||
286 | if (WriteWi232(regNETGRP,126)!=0) /*damit Wi232 nix mehr vom Kopter schickt erstmal Networkgroup ins Nirwana setzen */ |
||
287 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
288 | // InitErr =12; |
||
289 | // Grund: |
||
290 | //If RF packets are received while the CMD line is active, |
||
291 | //they are still processed and presented to the module’s UART for transmission. |
||
292 | |||
293 | // wenn sich ein EEPROM-Wert ändert wird auch das Ram beschrieben damit die Änderung sofort wirksam wird |
||
294 | if (WriteWi232(regNVTXCHANNEL,WiTXRXChannel)!=0) |
||
295 | InitErrorWi232(1); /*TX Channel*/ |
||
296 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
297 | |||
298 | if (WriteWi232(regTXCHANNEL,WiTXRXChannel)!=0) |
||
299 | InitErrorWi232(2);/*TX Channel*/ |
||
300 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
301 | |||
302 | if (WriteWi232(regNVRXCHANNEL,WiTXRXChannel)!=0) |
||
303 | InitErrorWi232(3);/* RX Channel*/ |
||
304 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
305 | |||
306 | if (WriteWi232(regRXCHANNEL,WiTXRXChannel)!=0) |
||
307 | InitErrorWi232(4);/* RX Channel*/ |
||
308 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
309 | |||
310 | if (WriteWi232(regNVSLPMODE ,Sleep_Awake)!=0) |
||
311 | InitErrorWi232(5);/* Sleepmode*/ |
||
312 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
313 | |||
314 | if (WriteWi232(regNVPWRMODE,WbModeP15)!=0) |
||
315 | InitErrorWi232(6);/* Transceiver Mode/Powermode */ |
||
316 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
317 | |||
318 | if (WriteWi232(regNVTXTO,WiTXTO)!=0) |
||
319 | InitErrorWi232(7);/* UART Timeout */ |
||
320 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
321 | |||
322 | if (WriteWi232(regTXTO,WiTXTO)!=0) |
||
323 | InitErrorWi232(8);/* UART Timeout */ |
||
324 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
325 | |||
326 | if (WriteWi232(regNVUARTMTU,WiUartMTU)!=0) |
||
327 | InitErrorWi232(9);/* UART Buffer*/ |
||
328 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
329 | |||
330 | if (WriteWi232(regUARTMTU,WiUartMTU)!=0) |
||
331 | InitErrorWi232(10);/* UART Buffer*/ |
||
332 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
333 | |||
334 | if (WriteWi232(regNVNETMODE,WiNetworkMode)!=0) |
||
335 | InitErrorWi232(11);/* Networkmode*/ |
||
336 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
337 | |||
338 | if (WriteWi232(regNETMODE,WiNetworkMode)!=0) |
||
339 | InitErrorWi232(12);/* Networkmode*/ |
||
340 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
341 | |||
342 | if (WriteWi232(regNVUSECRC ,CRC_Enable)!=0) |
||
343 | InitErrorWi232(13);/* CRC*/ |
||
344 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
345 | |||
346 | if (WriteWi232(regNVCSMAMODE,CSMA_En)!=0) |
||
347 | InitErrorWi232(14);/* CSMA*/ |
||
348 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
349 | |||
350 | if (WriteWi232(regNVNETGRP,WiNetworkGroup)!=0) |
||
351 | InitErrorWi232(15);/* Networkgroup */ |
||
352 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
353 | |||
354 | if (WriteWi232(regNETGRP,WiNetworkGroup)!=0) |
||
355 | InitErrorWi232(15);/* Networkgroup */ |
||
356 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
357 | |||
358 | if (WriteWi232(regNVDATARATE,New_Baudrate)!=0) |
||
359 | InitErrorWi232(16);/* Baudrate*/ |
||
360 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
361 | |||
362 | if (WriteWi232(regDATARATE,New_Baudrate)!=0) |
||
363 | InitErrorWi232(17);/* Baudrate*/ |
||
364 | lcd_printpns_at (i++,4,PSTR("."),0); |
||
365 | |||
366 | |||
367 | clr_WI232CMD(); |
||
368 | |||
369 | if (InitErr !=0) |
||
370 | { |
||
371 | lcd_printpns_at (0, 2, PSTR("Wi232 InitError "),0); |
||
372 | lcd_print_hex(InitErr,0); |
||
373 | set_beep ( 1000, 0x0040, BeepNormal); |
||
374 | _delay_ms(2000); |
||
375 | } |
||
376 | else |
||
377 | { |
||
378 | lcd_printpns_at (0, 2, PSTR("Wi232 Init ok...."),0); |
||
379 | WriteWiInitFlag(); // Init merken |
||
380 | } |
||
381 | // USART_Init (UART_BAUD_SELECT(PKT_Baudrate,F_CPU)); |
||
382 | SetUart0_Wi232(PKT_Baudrate); |
||
383 | Old_Baudrate = PKT_Baudrate; |
||
384 | _delay_ms(2000); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /************************************************************************* |
||
389 | Function: InitErrorWI232() |
||
390 | Purpose: Show Wi232 Error, Value |
||
391 | Returns: |
||
392 | **************************************************************************/ |
||
393 | void InitErrorWi232(uint8_t Error) |
||
394 | { |
||
395 | |||
396 | lcd_printpns_at (0, 3, PSTR("Wi232 InitError "),0); |
||
397 | lcd_print_hex(Error,0); |
||
398 | InitErr=Error; |
||
399 | set_beep ( 500, 0x0040, BeepNormal); |
||
400 | _delay_ms(500); |
||
401 | |||
402 | } |
||
403 | |||
404 | |||
405 | |||
406 | |||
407 | /************************************************************************* |
||
408 | Function: WriteWI232() |
||
409 | Purpose: set Register to Wi232, Register, Value |
||
410 | Returns: 0 = ACK, FF = NAK |
||
411 | ACHTUNG nur für Value <0x80 |
||
412 | **************************************************************************/ |
||
413 | int16_t WriteWi232(uint8_t Wi232Register, uint8_t RegisterValue) |
||
414 | |||
415 | { |
||
416 | uint8_t timeout=10; |
||
417 | uint8_t tc=0; |
||
418 | unsigned int v; |
||
419 | |||
420 | USART_putc(0xff); |
||
421 | USART_putc(0x02); |
||
422 | USART_putc(Wi232Register); |
||
423 | USART_putc(RegisterValue); |
||
424 | // lcd_print_hex(Wi232Register,0); |
||
425 | // lcd_print_hex(RegisterValue,0); |
||
426 | |||
427 | do |
||
428 | { |
||
429 | v = USART_getc(); /*ACK erwartet*/ |
||
430 | _delay_ms(100); |
||
431 | tc ++; |
||
432 | } |
||
433 | while (v==0 && tc!=timeout); |
||
434 | |||
435 | // lcd_print_hex(v,0); |
||
436 | if (v != 0x06) |
||
437 | { |
||
438 | lcd_printpns_at (0, 2, PSTR("Wi.232 NAK"),0); |
||
439 | set_beep ( 1000, 0x0040, BeepNormal); |
||
440 | _delay_ms(2000); |
||
441 | return 0xFF; |
||
442 | } |
||
443 | |||
444 | if (v==0x06) |
||
445 | return 0; |
||
446 | |||
447 | return 0xFF; |
||
448 | } |
||
449 | |||
450 | |||
451 | /************************************************************************* |
||
452 | Function: ReadWI232() |
||
453 | Purpose: send Readcommand to Wi232, |
||
454 | Returns: Registervalue, 0 = timeout 0xFF = Syntaxerror |
||
455 | |||
456 | **************************************************************************/ |
||
457 | int16_t ReadWi232(uint16_t Wi232Register) |
||
458 | |||
459 | { |
||
460 | uint8_t timeout=10; |
||
461 | uint8_t tc=0; |
||
462 | |||
463 | |||
464 | unsigned int v; |
||
465 | |||
466 | v = USART_getc(); /*Zeichen löschen*/ |
||
467 | USART_putc(0xff); |
||
468 | USART_putc(0x02); |
||
469 | USART_putc(0xfe); |
||
470 | USART_putc(Wi232Register); |
||
471 | _delay_ms(50); |
||
472 | // lcd_printpns_at (0, 2, PSTR("read Wi232"),0); |
||
473 | |||
474 | |||
475 | do |
||
476 | { |
||
477 | v = USART_getc(); /*ACK erwartet*/ |
||
478 | _delay_ms(100); |
||
479 | tc ++; |
||
480 | } |
||
481 | while (v==0 && tc!=timeout); |
||
482 | |||
483 | if (tc == timeout) |
||
484 | return 0; /* Timeout*/ |
||
485 | |||
486 | if (v != 0x06) |
||
487 | return 0xFF; /* Syntaxerror*/ |
||
488 | |||
489 | lcd_print_hex(v,0); |
||
490 | v = USART_getc(); /*Register*/ |
||
491 | // lcd_print_hex(v,0); |
||
492 | v = USART_getc(); /*Value*/ |
||
493 | // lcd_print_hex(v,0); |
||
494 | |||
495 | return v; |
||
496 | |||
497 | } |
||
498 | |||
499 | |||
500 | |||
501 | /************************************************************************* |
||
502 | Function: EscapeString() |
||
503 | Purpose: |
||
504 | Returns: |
||
505 | Quelle: Radiotronix Wi.232 Manual |
||
506 | **************************************************************************/ |
||
507 | |||
508 | |||
509 | int EscapeString(char *src, char src_len, char *dest) |
||
510 | { |
||
511 | // The following function copies and encodes the first |
||
512 | // src_len characters from *src into *dest. This |
||
513 | // encoding is necessary for Wi.232 command formats. |
||
514 | // The resulting string is null terminated. The size |
||
515 | // of this string is the function return value. |
||
516 | // --------------------------------------------------- |
||
517 | uint8_t src_idx, dest_idx; |
||
518 | // Save space for the command header and size bytes |
||
519 | // ------------------------------------------------ |
||
520 | dest_idx = 2; |
||
521 | // Loop through source string and copy/encode |
||
522 | // ------------------------------------------ |
||
523 | for (src_idx = 0; src_idx < src_len; src_idx++) |
||
524 | { |
||
525 | if (src[src_idx] > 127) |
||
526 | { |
||
527 | dest[dest_idx++] = 0xFE; |
||
528 | } |
||
529 | dest[dest_idx++] = (src[src_idx] & 0x7F); |
||
530 | } |
||
531 | // Add null terminator |
||
532 | // ------------------- |
||
533 | dest[dest_idx] = 0; |
||
534 | // Add command header |
||
535 | // ------------------ |
||
536 | dest[0] = 0xFF; |
||
537 | dest[1] = dest_idx-2; |
||
538 | |||
539 | // Return escape string size |
||
540 | // ------------------------- |
||
541 | return dest_idx; |
||
542 | } |
||
543 | |||
544 | |||
545 | //#if defined HWVERSION1_3W || defined HWVERSION3_9 |
||
546 | /************************************************************************* |
||
547 | Function: Wi232USB() |
||
548 | Purpose: Connect Wi232 Programmmode to PKT USB, |
||
549 | Returns: |
||
550 | |||
551 | **************************************************************************/ |
||
552 | void Wi232_USB(void) |
||
553 | |||
554 | |||
555 | { |
||
556 | unsigned int c0,c1; |
||
557 | |||
558 | if (Wi232_hardware==1) |
||
559 | { |
||
560 | // USART_Init (UART_BAUD_SELECT(57600,F_CPU)); |
||
561 | // uart1_init( UART_BAUD_SELECT(57600,F_CPU) ); |
||
562 | // USART_Init (UART_BAUD_SELECT(2400,F_CPU)); |
||
563 | // uart1_init( UART_BAUD_SELECT(2400,F_CPU) ); |
||
564 | } |
||
565 | |||
566 | if (Wi232_hardware==2) |
||
567 | { |
||
568 | USART_Init (UART_BAUD_SELECT(2400,F_CPU)); |
||
569 | uart1_init( UART_BAUD_SELECT(2400,F_CPU) ); |
||
570 | } |
||
571 | |||
572 | lcd_cls (); |
||
573 | // SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
||
574 | |||
575 | set_WI232CMD(); |
||
576 | |||
577 | lcd_printpns_at (0, 0, PSTR("Wi.232 Konfiguration "),0); |
||
578 | lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0); |
||
579 | lcd_printpns_at (0, 2, PSTR("Wi.232"),0); |
||
580 | lcd_printpns_at (0, 3, PSTR("Programm starten"),0); |
||
581 | lcd_printpns_at (17, 7, PSTR("Exit"),0); |
||
582 | |||
583 | c1 = 0; |
||
584 | |||
585 | for(;;) |
||
586 | { |
||
587 | c0 = uart1_getc(); /* from USB*/ |
||
588 | if ( c0 & UART_NO_DATA ) |
||
589 | { |
||
590 | c1 = USART_getc(); |
||
591 | |||
592 | if (c1 == 0) |
||
593 | {} |
||
594 | else |
||
595 | { |
||
596 | // lcd_print_hex(c1,0); |
||
597 | uart1_putc (c1); /*to USB*/; |
||
598 | } |
||
599 | } |
||
600 | else |
||
601 | { |
||
602 | USART_putc(c0 ); /* to Wi232*/ |
||
603 | // lcd_print_hex(c0,0); |
||
604 | // _delay_ms(1); |
||
605 | } |
||
606 | |||
607 | if ((get_key_press (1 << KEY_ENTER))) |
||
608 | { |
||
609 | clr_WI232CMD(); |
||
610 | // uart1_init( UART_BAUD_SELECT(57600,F_CPU) ); |
||
611 | // USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); |
||
612 | // SwitchToFC(); |
||
613 | return; |
||
614 | } |
||
615 | |||
616 | } |
||
617 | } |
||
618 | /************************************************************************* |
||
619 | Function: Wi232_FC() |
||
620 | Purpose: Connect Wi232 to PKT USB, Transparent |
||
621 | Returns: |
||
622 | |||
623 | **************************************************************************/ |
||
624 | void Wi232_FC(void) |
||
625 | |||
626 | |||
627 | { |
||
628 | unsigned int c0,c1; |
||
629 | |||
630 | |||
631 | // USART_Init (UART_BAUD_SELECT(PKT_Baudrate,F_CPU)); |
||
632 | // uart1_init( UART_BAUD_SELECT(PKT_Baudrate,F_CPU) ); |
||
633 | //TODO: SetUart0_Wi232(PKT_Baudrate); |
||
634 | // SetUart1_Wi232(PKT_Baudrate); |
||
635 | |||
636 | |||
637 | lcd_cls (); |
||
638 | // SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
||
639 | |||
640 | |||
641 | lcd_printpns_at (0, 0, PSTR("Wi.232 to FC "),0); |
||
642 | lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0); |
||
643 | lcd_printpns_at (0, 2, PSTR("und Mikrokoptertool"),0); |
||
644 | lcd_printpns_at (0, 3, PSTR("starten"),0); |
||
645 | lcd_printpns_at (17, 7, PSTR("Exit"),0); |
||
646 | |||
647 | c1 = 0; |
||
648 | |||
649 | for(;;) |
||
650 | { |
||
651 | c0 = uart1_getc(); /* from USB*/ |
||
652 | if ( c0 & UART_NO_DATA ) |
||
653 | { |
||
654 | c1 = USART_getc(); |
||
655 | if (c1 == 0) |
||
656 | {} |
||
657 | else |
||
658 | { |
||
659 | // lcd_print_hex(c1,0); |
||
660 | uart1_putc (c1); /*to USB*/; |
||
661 | |||
662 | } |
||
663 | } |
||
664 | else |
||
665 | { |
||
666 | USART_putc(c0 ); /* to Wi232*/ |
||
667 | // lcd_print_hex(c0,0); |
||
668 | // _delay_ms(1); |
||
669 | } |
||
670 | |||
671 | if ((get_key_press (1 << KEY_ENTER))) |
||
672 | { |
||
673 | return; |
||
674 | } |
||
675 | } |
||
676 | } |
||
677 | |||
678 | |||
679 | #endif |