Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1470 - 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
#if defined HWVERSION1_3W || defined HWVERSION3_9
49
 
50
uint8_t Wi232_hardware = 0;
51
uint8_t InitErr=0;
52
 
53
/*************************************************************************
54
Function: discoverWI232()
55
Purpose:  check if Wi232 available
56
Returns:  Version or 0 = timeout
57
 
58
**************************************************************************/
59
void discoverWi232(void)
60
 
61
{
62
        int16_t RegisterWi232;
63
 
64
        SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
65
        set_WI232CMD();
66
        _delay_ms(200);
67
 
68
        USART_Init( UART_BAUD_SELECT(57600,F_CPU) );            /* erstmal mit 57600 versuchen*/
69
        lcd_printpns_at (0, 0, PSTR("search Wi.232 Modul"),0);
70
        lcd_printpns_at (0, 1, PSTR("with 57600 Baud    "),0);
71
        RegisterWi232 = ReadWi232(regDiscover);
72
        Wi232_hardware = 1;
73
 
74
        if (RegisterWi232 == 0)
75
        {
76
                USART_Init( UART_BAUD_SELECT(2400,F_CPU) );     /* neues Modul mit 2400 suchen*/
77
                lcd_printpns_at (0, 1, PSTR("with 2400 Baud        "),0);
78
                RegisterWi232 = ReadWi232(regDiscover);
79
                Wi232_hardware = 2;
80
        }
81
 
82
        if (RegisterWi232 == 0)
83
        {
84
                lcd_cls();
85
                lcd_printpns_at (0, 0, PSTR("no Wi.232 found    "),0);
86
                Wi232_hardware = 0;
87
        }
88
 
89
        if (RegisterWi232 == 0xFF)
90
        {
91
                lcd_cls();
92
                lcd_printpns_at (0, 0, PSTR("Wi.232 Sytaxerror   "),0);
93
                BeepTime = 1000;
94
                BeepMuster = 0x0040;
95
                _delay_ms(2000);
96
        }
97
 
98
        if (RegisterWi232 != 0)
99
        {
100
                lcd_cls();
101
                if (Wi232_hardware ==1)
102
                        lcd_printpns_at (0, 0, PSTR("Wi.232 found 57600"),0);
103
 
104
                if (Wi232_hardware ==2) // bei 2400 Baud
105
                {
106
                        WiIsSet= false; //wenn hier 2400 gefunden wurde ist Wi232 nicht initialisiert
107
                        lcd_printpns_at (0, 0, PSTR("Wi.232 found 2400"),0);
108
                        if (WriteWi232(regNVDATARATE,Wi232_57600)!=0) /* NV-Ram auf 57600 setzen*/
109
                        {
110
                                lcd_printpns_at (0, 1, PSTR("Error set NV-RAM"),0);
111
                                BeepTime = 1000;
112
                                BeepMuster = 0x0040;
113
                                _delay_ms(2000);
114
                        }
115
                        else
116
                        {
117
                                _delay_ms(1000);
118
                                lcd_printpns_at (0, 1, PSTR("NV-RAM set to 57600"),0);
119
                                lcd_printpns_at (0, 2, PSTR("ok                 "),0);
120
                                _delay_ms(2000);
121
                        }
122
 
123
                        if (WriteWi232(regDATARATE,Wi232_57600)!=0) /* Ram auf 57600 setzen*/
124
                        {
125
                                lcd_printpns_at (0, 3, PSTR("Error set RAM     "),0);
126
                                BeepTime = 1000;
127
                                BeepMuster = 0x0040;
128
                                _delay_ms(2000);
129
                        }
130
                        else
131
                        {
132
                                _delay_ms(1000);
133
                                lcd_printpns_at (0, 1, PSTR("RAM set to 57600    "),0);
134
                                lcd_printpns_at (0, 2, PSTR("ok                  "),0);
135
                                _delay_ms(2000);
136
                        }
137
                        USART_Init( UART_BAUD_SELECT(57600,F_CPU) );
138
                }
139
                lcd_cls_line (0,1,21);
140
                lcd_printpns_at (0, 1, PSTR("Version:"),0);
141
                lcd_print_hex_at(9,1,RegisterWi232,0);
142
        }
143
        clr_WI232CMD();
144
}
145
 
146
 
147
/*************************************************************************
148
Function: InitWI232()
149
Purpose:  set Wi232Register for Mikrokopter
150
Returns:  0 = ACK, FF = NAK
151
 
152
**************************************************************************/
153
void InitWi232(void)
154
{
155
 
156
uint8_t i = 0;
157
 
158
        Change_Output(Uart02Wi);  // Verbindung zu Wi232 herstellen
159
 
160
        discoverWi232();  // Check if Wi232 available
161
 
162
        if (Wi232_hardware != 0)
163
        {
164
                lcd_printpns_at (0, 2, PSTR("Init Wi232 wait...."),0);
165
                set_WI232CMD();
166
                _delay_ms(200);
167
                SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
168
                USART_Init( UART_BAUD_SELECT(57600,F_CPU) );                    /* erstmal mit 57600 versuchen*/
169
 
170
                if (WriteWi232(regNETGRP,126)!=0) /*damit Wi232 nix mehr vom Kopter schickt erstmal Networkgroup ins Nirwana setzen */
171
                        lcd_printpns_at (i++,4,PSTR("."),0);
172
//              InitErr =12;
173
                                                                                // Grund:
174
                                                                                //If RF packets are received while the CMD line is active,
175
                                                                                //they are still processed and presented to the module’s UART for transmission.
176
 
177
                // wenn sich ein EEPROM-Wert ändert wird auch das Ram beschrieben damit die Änderung sofort wirksam wird
178
                if (WriteWi232(regNVTXCHANNEL,WiTXRXChannel)!=0)
179
                        InitErrorWi232(1); /*TX Channel*/
180
                lcd_printpns_at (i++,4,PSTR("."),0);
181
 
182
                if (WriteWi232(regTXCHANNEL,WiTXRXChannel)!=0)
183
                        InitErrorWi232(2);/*TX Channel*/
184
                lcd_printpns_at (i++,4,PSTR("."),0);
185
 
186
                if (WriteWi232(regNVRXCHANNEL,WiTXRXChannel)!=0)
187
                        InitErrorWi232(3);/* RX Channel*/
188
                lcd_printpns_at (i++,4,PSTR("."),0);
189
 
190
                if (WriteWi232(regRXCHANNEL,WiTXRXChannel)!=0)
191
                        InitErrorWi232(4);/* RX Channel*/
192
                lcd_printpns_at (i++,4,PSTR("."),0);
193
 
194
                if (WriteWi232(regNVSLPMODE ,Sleep_Awake)!=0)
195
                        InitErrorWi232(5);/* Sleepmode*/
196
                lcd_printpns_at (i++,4,PSTR("."),0);
197
 
198
                if (WriteWi232(regNVPWRMODE,WbModeP15)!=0)
199
                        InitErrorWi232(6);/* Transceiver Mode/Powermode */
200
                lcd_printpns_at (i++,4,PSTR("."),0);
201
 
202
                if (WriteWi232(regNVTXTO,WiTXTO)!=0)
203
                        InitErrorWi232(7);/* UART Timeout */
204
                lcd_printpns_at (i++,4,PSTR("."),0);
205
 
206
                if (WriteWi232(regTXTO,WiTXTO)!=0)
207
                        InitErrorWi232(8);/* UART Timeout */
208
                lcd_printpns_at (i++,4,PSTR("."),0);
209
 
210
                if (WriteWi232(regNVUARTMTU,WiUartMTU)!=0)
211
                        InitErrorWi232(9);/* UART Buffer*/
212
                lcd_printpns_at (i++,4,PSTR("."),0);
213
 
214
                if (WriteWi232(regUARTMTU,WiUartMTU)!=0)
215
                        InitErrorWi232(10);/* UART Buffer*/
216
                lcd_printpns_at (i++,4,PSTR("."),0);
217
 
218
                if (WriteWi232(regNVNETMODE,WiNetworkMode)!=0)
219
                        InitErrorWi232(11);/* Networkmode*/
220
                lcd_printpns_at (i++,4,PSTR("."),0);
221
 
222
                if (WriteWi232(regNETMODE,WiNetworkMode)!=0)
223
                        InitErrorWi232(12);/* Networkmode*/
224
                lcd_printpns_at (i++,4,PSTR("."),0);
225
 
226
                if (WriteWi232(regNVUSECRC ,CRC_Enable)!=0)
227
                        InitErrorWi232(13);/* CRC*/
228
                lcd_printpns_at (i++,4,PSTR("."),0);
229
 
230
                if (WriteWi232(regNVCSMAMODE,CSMA_En)!=0)
231
                        InitErrorWi232(14);/* CSMA*/
232
                lcd_printpns_at (i++,4,PSTR("."),0);
233
 
234
                if (WriteWi232(regNVDATARATE,Wi232_57600)!=0)
235
                        InitErrorWi232(15);/* Baudrate*/
236
                lcd_printpns_at (i++,4,PSTR("."),0);
237
 
238
                if (WriteWi232(regDATARATE,Wi232_57600)!=0)
239
                        InitErrorWi232(16);/* Baudrate*/
240
                lcd_printpns_at (i++,4,PSTR("."),0);
241
 
242
                if (WriteWi232(regNVNETGRP,WiNetworkGroup)!=0)
243
                        InitErrorWi232(17);/* Networkgroup */
244
                lcd_printpns_at (i++,4,PSTR("."),0);
245
 
246
                if (WriteWi232(regNETGRP,WiNetworkGroup)!=0)
247
                        InitErrorWi232(18);/* Networkgroup */
248
                lcd_printpns_at (i++,4,PSTR("."),0);
249
 
250
                clr_WI232CMD();
251
 
252
                if (InitErr !=0)
253
                {
254
                        lcd_printpns_at (0, 2, PSTR("Wi232 InitError  "),0);
255
                        lcd_print_hex(InitErr,0);
256
                        BeepTime = 1000;
257
                        BeepMuster = 0x0040;
258
                        _delay_ms(2000);
259
                }
260
                else
261
                  {
262
                    lcd_printpns_at (0, 2, PSTR("Wi232 Init ok...."),0);
263
                    WriteWiInitFlag();  // Init merken
264
                  }
265
                USART_Init (UART_BAUD_SELECT(57600,F_CPU));
266
                _delay_ms(2000);
267
        }
268
}
269
 
270
/*************************************************************************
271
Function: InitErrorWI232()
272
Purpose:  Show Wi232 Error, Value
273
Returns:
274
**************************************************************************/
275
void InitErrorWi232(uint8_t Error)
276
{
277
 
278
        lcd_printpns_at (0, 3, PSTR("Wi232 InitError  "),0);
279
        lcd_print_hex(Error,0);
280
        InitErr=Error;
281
        BeepTime = 500;
282
        BeepMuster = 0x0040;
283
        _delay_ms(500);
284
 
285
}
286
 
287
 
288
 
289
 
290
/*************************************************************************
291
Function: WriteWI232()
292
Purpose:  set Register to Wi232, Register, Value
293
Returns:  0 = ACK, FF = NAK
294
ACHTUNG nur für Value <0x80
295
**************************************************************************/
296
int16_t WriteWi232(uint8_t Wi232Register, uint8_t RegisterValue)
297
 
298
{
299
        uint8_t timeout=10;
300
        uint8_t tc=0;
301
        unsigned int v;
302
 
303
        USART_putc(0xff);
304
        USART_putc(0x02);
305
        USART_putc(Wi232Register);
306
        USART_putc(RegisterValue);
307
//      lcd_print_hex(Wi232Register,0);
308
//      lcd_print_hex(RegisterValue,0);
309
 
310
        do
311
        {
312
                v = USART_getc(); /*ACK erwartet*/
313
                _delay_ms(100);
314
                tc ++;
315
        }
316
        while (v==0 && tc!=timeout);
317
 
318
//      lcd_print_hex(v,0);
319
        if (v != 0x06)
320
        {
321
                lcd_printpns_at (0, 2, PSTR("Wi.232 NAK"),0);
322
                BeepTime = 1000;
323
                BeepMuster = 0x0040;
324
                _delay_ms(2000);
325
                return 0xFF;
326
        }
327
 
328
        if (v==0x06)
329
                return 0;
330
 
331
        return 0xFF;
332
}
333
 
334
 
335
/*************************************************************************
336
Function: ReadWI232()
337
Purpose:  send Readcommand to Wi232,
338
Returns:  Registervalue, 0 = timeout 0xFF = Syntaxerror
339
 
340
**************************************************************************/
341
int16_t ReadWi232(uint16_t Wi232Register)
342
 
343
{
344
        uint8_t timeout=10;
345
        uint8_t tc=0;
346
 
347
 
348
        unsigned int v;
349
 
350
        v = USART_getc(); /*Zeichen löschen*/
351
        USART_putc(0xff);
352
        USART_putc(0x02);
353
        USART_putc(0xfe);
354
        USART_putc(Wi232Register);
355
        _delay_ms(50);
356
//      lcd_printpns_at (0, 2, PSTR("read Wi232"),0);
357
 
358
 
359
        do
360
        {
361
                v = USART_getc(); /*ACK erwartet*/
362
                _delay_ms(100);
363
                tc ++;
364
        }
365
        while (v==0 && tc!=timeout);
366
 
367
        if (tc == timeout)
368
                return 0; /* Timeout*/
369
 
370
        if (v != 0x06)
371
                return 0xFF; /* Syntaxerror*/
372
 
373
        lcd_print_hex(v,0);
374
        v = USART_getc(); /*Register*/
375
//      lcd_print_hex(v,0);
376
        v = USART_getc(); /*Value*/
377
//      lcd_print_hex(v,0);
378
 
379
        return v;
380
 
381
}
382
 
383
 
384
 
385
/*************************************************************************
386
Function: EscapeString()
387
Purpose:
388
Returns:
389
Quelle: Radiotronix Wi.232 Manual
390
**************************************************************************/
391
 
392
 
393
int EscapeString(char *src, char src_len, char *dest)
394
{
395
        // The following function copies and encodes the first
396
        // src_len characters from *src into *dest. This
397
        // encoding is necessary for Wi.232 command formats.
398
        // The resulting string is null terminated. The size
399
        // of this string is the function return value.
400
        // ---------------------------------------------------
401
        uint8_t src_idx, dest_idx;
402
        // Save space for the command header and size bytes
403
        // ------------------------------------------------
404
        dest_idx = 2;
405
        // Loop through source string and copy/encode
406
        // ------------------------------------------
407
        for (src_idx = 0; src_idx < src_len; src_idx++)
408
        {
409
                if (src[src_idx] > 127)
410
                {
411
                        dest[dest_idx++] = 0xFE;
412
                }
413
                dest[dest_idx++] = (src[src_idx] & 0x7F);
414
        }
415
        // Add null terminator
416
        // -------------------
417
        dest[dest_idx] = 0;
418
        // Add command header
419
        // ------------------
420
        dest[0] = 0xFF;
421
        dest[1] = dest_idx-2;
422
 
423
        // Return escape string size
424
        // -------------------------
425
        return dest_idx;
426
}
427
 
428
 
429
//#if defined HWVERSION1_3W || defined HWVERSION3_9
430
/*************************************************************************
431
Function: Wi232USB()
432
Purpose:  Connect Wi232 Programmmode to PKT USB,
433
Returns:
434
 
435
**************************************************************************/
436
void Wi232_USB(void)
437
 
438
 
439
{
440
        unsigned int c0,c1;
441
 
442
        if (Wi232_hardware==1)
443
        {
444
//              USART_Init (UART_BAUD_SELECT(57600,F_CPU));
445
//              uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
446
//              USART_Init (UART_BAUD_SELECT(2400,F_CPU));
447
//              uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
448
        }
449
 
450
        if (Wi232_hardware==2)
451
        {
452
                USART_Init (UART_BAUD_SELECT(2400,F_CPU));
453
                uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
454
        }
455
 
456
        lcd_cls ();
457
//      SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
458
 
459
        set_WI232CMD();
460
 
461
        lcd_printpns_at (0, 0, PSTR("Wi.232 Konfiguration "),0);
462
        lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0);
463
        lcd_printpns_at (0, 2, PSTR("Wi.232"),0);
464
        lcd_printpns_at (0, 3, PSTR("Programm starten"),0);
465
        lcd_printpns_at (17, 7, PSTR("Exit"),0);
466
 
467
        c1 = 0;
468
 
469
        for(;;)
470
        {
471
                c0 = uart1_getc(); /* from USB*/
472
                if ( c0 & UART_NO_DATA )
473
                {
474
                        c1 = USART_getc();
475
 
476
                        if (c1 == 0)
477
                        {}
478
                        else
479
                        {
480
//                              lcd_print_hex(c1,0);
481
                                uart1_putc (c1);          /*to USB*/;
482
                        }
483
                }
484
                else
485
                {
486
                        USART_putc(c0 ); /* to Wi232*/
487
//                      lcd_print_hex(c0,0);
488
//                      _delay_ms(1);
489
                }
490
 
491
                if ((get_key_press (1 << KEY_ENTER)))
492
                {
493
                        clr_WI232CMD();
494
//                      uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
495
//                      USART_Init( UART_BAUD_SELECT(57600,F_CPU) );
496
//                      SwitchToFC();
497
                        return;
498
                }
499
 
500
        }
501
}
502
/*************************************************************************
503
Function: Wi232_FC()
504
Purpose:  Connect Wi232 to PKT USB, Transparent
505
Returns:
506
 
507
**************************************************************************/
508
void Wi232_FC(void)
509
 
510
 
511
{
512
        unsigned int c0,c1;
513
 
514
 
515
        USART_Init (UART_BAUD_SELECT(57600,F_CPU));
516
        uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
517
//      USART_Init (UART_BAUD_SELECT(2400,F_CPU));
518
//      uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
519
 
520
        lcd_cls ();
521
//      SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
522
 
523
 
524
        lcd_printpns_at (0, 0, PSTR("Wi.232 to FC "),0);
525
        lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0);
526
        lcd_printpns_at (0, 2, PSTR("und Mikrokoptertool"),0);
527
        lcd_printpns_at (0, 3, PSTR("starten"),0);
528
        lcd_printpns_at (17, 7, PSTR("Exit"),0);
529
 
530
        c1 = 0;
531
 
532
        for(;;)
533
        {
534
                c0 = uart1_getc(); /* from USB*/
535
                if ( c0 & UART_NO_DATA )
536
                {
537
                        c1 = USART_getc();
538
                        if (c1 == 0)
539
                        {}
540
                        else
541
                        {
542
//                              lcd_print_hex(c1,0);
543
                                uart1_putc (c1);          /*to USB*/;
544
 
545
                        }
546
                }
547
                else
548
                {
549
                        USART_putc(c0 ); /* to Wi232*/
550
//                      lcd_print_hex(c0,0);
551
//                      _delay_ms(1);
552
                }
553
 
554
                if ((get_key_press (1 << KEY_ENTER)))
555
                {
556
                        return;
557
                }
558
        }
559
}
560
 
561
 
562
#endif