Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
1053 - 1
/*****************************************************************************
2
 *   Copyright (C) 2011 Christian "Cebra" Brandtner, brandtner@brandtner.net *
3
 *                                                                           *
4
 *   This program is free software; you can redistribute it and/or modify    *
5
 *   it under the terms of the GNU General Public License as published by    *
6
 *   the Free Software Foundation; either version 2 of the License.          *
7
 *                                                                           *
8
 *   This program is distributed in the hope that it will be useful,         *
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
11
 *   GNU General Public License for more details.                            *
12
 *                                                                           *
13
 *   You should have received a copy of the GNU General Public License       *
14
 *   along with this program; if not, write to the                           *
15
 *   Free Software Foundation, Inc.,                                         *
16
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
17
 *                                                                           *
18
 *****************************************************************************/
19
 
20
/*
21
 * Wi232.c
22
 * Funktionen für den Zugriff auf Radiotronix Wi.232EUR Modul
23
 *  Created on: 11.06.2011
24
 *      Author: cebra
25
 */
26
 
27
 
28
 
29
#include <avr/io.h>
30
#include <avr/pgmspace.h>
31
#include <util/delay.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include "lcd.h"
35
#include "usart.h"
36
#include "uart1.h"
37
#include "main.h"
38
#include "Wi232.h"
39
#include "timer.h"
40
//#include "ParaMenu.h"
41
 
42
 
43
uint8_t Wi232_hardware = 0;
44
/*************************************************************************
45
Function: discoverWI232()
46
Purpose:  check if Wi232 available
47
Returns:  Version or 0 = timeout
48
 
49
**************************************************************************/
50
void discoverWi232(void)
51
 
52
{
53
        int16_t RegisterWi232;
54
    set_WI232CMD();
55
    _delay_ms(100);
56
    set_LED4();
57
        SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
58
        USART_Init( UART_BAUD_SELECT(57600,F_CPU) );            /* erstmal mit 57600 versuchen*/
59
    lcd_printpns_at (0, 0, PSTR("search Wi.232 Modul"),0);
60
    lcd_printpns_at (0, 1, PSTR("with 57600 Baud"),0);
61
    RegisterWi232 = readWi232(regDiscover);
62
    Wi232_hardware = 1;
63
    if (RegisterWi232 == 0)
64
        {
65
        USART_Init( UART_BAUD_SELECT(2400,F_CPU) );    /* neues Modul mit 2400 suchen*/
66
        lcd_printpns_at (0, 1, PSTR("with 2400 Baud "),0);
67
        RegisterWi232 = readWi232(regDiscover);
68
        Wi232_hardware = 2;
69
        }
70
 
71
    if (RegisterWi232 == 0)
72
     {
73
       lcd_cls();
74
       lcd_printpns_at (0, 0, PSTR("no Wi.232 found"),0);
75
       Wi232_hardware = 0;
76
     }
77
    if (RegisterWi232 == 0xFF)
78
        {
79
          lcd_cls();
80
          lcd_printpns_at (0, 0, PSTR("Wi.232 Sytaxerror"),0);
81
        }
82
    if (RegisterWi232 != 0)
83
        {
84
          lcd_cls();
85
          if (Wi232_hardware ==1)
86
          lcd_printpns_at (0, 0, PSTR("Wi.232 found 57600"),0);
87
          if (Wi232_hardware ==2)
88
          {
89
          lcd_printpns_at (0, 0, PSTR("Wi.232 found 2400"),0);
90
           if (WriteWi232(regNVDATARATE,Wi232_57600)!=0) /* NV-Ram auf 57600 setzen*/
91
           {
92
                  lcd_printpns_at (0, 1, PSTR("Error set NV-RAM"),0);
93
           }
94
           else
95
           {
96
                _delay_ms(1000);
97
                lcd_printpns_at (0, 1, PSTR("NV-RAM set to 57600"),0);
98
                _delay_ms(2000);
99
                lcd_printpns_at (0, 1, PSTR("ok                 "),0);
100
           }
101
          }
102
          lcd_printpns_at (0, 1, PSTR("Version:"),0);
103
          lcd_print_hex_at(9,1,RegisterWi232,0);
104
 
105
        }
106
 
107
    clr_WI232CMD();
108
        clr_LED4();
109
}
110
 
111
 
112
/*************************************************************************
113
Function: InitWI232()
114
Purpose:  set Wi232Register for Mikrokopter
115
Returns:  0 = ACK, FF = NAK
116
 
117
**************************************************************************/
118
void InitWi232(void)
119
{
120
uint8_t InitErr=0;
121
 
122
        discoverWi232(); /*Check if Wi232 available*/
123
 
124
    if (Wi232_hardware !=0)
125
    {
126
          lcd_printpns_at (0, 2, PSTR("Init Wi232 wait...."),0);
127
          set_WI232CMD();
128
      _delay_ms(10);
129
      set_LED4();
130
          SwitchToWi232();      /* Serielle Kanäle Wi232 mit USB verbinden*/
131
         if  (WriteWi232(regTXCHANNEL,wChan1)!=0) /*TX Channel*/
132
                        InitErr =1;
133
         if  (WriteWi232(regRXCHANNEL,wChan1)!=0) /* RX Channel*/
134
                        InitErr =2;
135
         if  (WriteWi232(regSLPMODE ,Sleep_Awake)!=0) /* Sleepmode*/
136
                        InitErr =3;
137
         if  (WriteWi232(regPWRMODE,WbModeP13)!=0) /* Transceiver Mode/Powermode */
138
                        InitErr =4;
139
         if  (WriteWi232(regTXTO,TWaitTime16)!=0) /* UART Timeout */
140
                        InitErr =5;
141
         if  (WriteWi232(regUARTMTU,UartMTU64)!=0) /* UART Buffer*/
142
                        InitErr =6;
143
         if  (WriteWi232(regNETGRP,66)!=0) /* Networkgroup */
144
                        InitErr =7;
145
         if  (WriteWi232(regNETMODE,NetMode_Normal)!=0) /* Networkmode*/
146
                        InitErr =8;
147
         if  (WriteWi232(regUSECRC ,CRC_Enable)!=0) /* CRC*/
148
                        InitErr =9;
149
         if  (WriteWi232(regCSMAMODE,CSMA_En)!=0) /* CSMA*/
150
                        InitErr =10;
151
         if  (WriteWi232(regDATARATE,Wi232_57600)!=0) /* Baudrate*/
152
                        InitErr =11;
153
 
154
         if (InitErr !=0)
155
         {
156
                 lcd_printpns_at (0, 2, PSTR("Wi232 InitError  "),0);
157
                 lcd_print_hex(InitErr,0);
158
         }
159
         else
160
                 lcd_printpns_at (0, 2, PSTR("Wi232 Init ok...."),0);
161
 
162
          USART_Init (UART_BAUD_SELECT(57600,F_CPU));
163
      clr_WI232CMD();
164
      _delay_ms(10);
165
          clr_LED4();
166
    }
167
 
168
 
169
}
170
 
171
 
172
/*************************************************************************
173
Function: WriteWI232()
174
Purpose:  set Register to Wi232, Register, Value
175
Returns:  0 = ACK, FF = NAK
176
ACHTUNG nur für Value <0x80
177
**************************************************************************/
178
int16_t WriteWi232(uint8_t Wi232Register, uint8_t RegisterValue)
179
 
180
{
181
        uint8_t timeout=10;
182
        uint8_t tc=0;
183
        unsigned int v;
184
 
185
        USART_putc(0xff);
186
        USART_putc(0x02);
187
        USART_putc(Wi232Register);
188
        USART_putc(RegisterValue);
189
    do
190
        {
191
     v = USART_getc(); /*ACK erwartet*/
192
     _delay_ms(100);
193
     tc ++;
194
    }
195
    while (v==0 && tc!=timeout);
196
//    lcd_print_hex(v,0);
197
        if (v != 0x06)
198
        {
199
          lcd_printpns_at (0, 2, PSTR("Wi.232 NAK"),0);
200
          return 0xFF;
201
        }
202
 
203
        if (v==0x06)
204
        return 0;
205
 
206
        return 0xFF;
207
}
208
 
209
 
210
/*************************************************************************
211
Function: readWI232()
212
Purpose:  send Readcommand to Wi232,
213
Returns:  Registervalue, 0 = timeout 0xFF = Syntaxerror
214
 
215
**************************************************************************/
216
int16_t readWi232(uint16_t Wi232Register)
217
 
218
{
219
        uint8_t timeout=10;
220
        uint8_t tc=0;
221
 
222
 
223
        unsigned int v;
224
 
225
        v = USART_getc(); /*Zeichen löschen*/
226
        USART_putc(0xff);
227
        USART_putc(0x02);
228
        USART_putc(0xfe);
229
        USART_putc(Wi232Register);
230
    _delay_ms(50);
231
//    lcd_printpns_at (0, 2, PSTR("read Wi232"),0);
232
 
233
 
234
    do
235
        {
236
     v = USART_getc(); /*ACK erwartet*/
237
     _delay_ms(100);
238
     tc ++;
239
    }
240
    while (v==0 && tc!=timeout);
241
 
242
    if (tc == timeout)  return 0; /* Timeout*/
243
        if (v != 0x06) return 0xFF; /* Syntaxerror*/
244
    lcd_print_hex(v,0);
245
    v = USART_getc(); /*Register*/
246
    lcd_print_hex(v,0);
247
    v = USART_getc(); /*Value*/
248
    lcd_print_hex(v,0);
249
 
250
    return v;
251
 
252
}
253
 
254
 
255
 
256
/*************************************************************************
257
Function: EscapeString()
258
Purpose:
259
Returns:
260
Quelle: Radiotronix Wi.232 Manual
261
**************************************************************************/
262
 
263
 
264
int EscapeString(char *src, char src_len, char *dest)
265
{
266
// The following function copies and encodes the first
267
// src_len characters from *src into *dest. This
268
// encoding is necessary for Wi.232 command formats.
269
// The resulting string is null terminated. The size
270
// of this string is the function return value.
271
// ---------------------------------------------------
272
uint8_t src_idx, dest_idx;
273
// Save space for the command header and size bytes
274
// ------------------------------------------------
275
dest_idx = 2;
276
// Loop through source string and copy/encode
277
// ------------------------------------------
278
for (src_idx = 0; src_idx < src_len; src_idx++)
279
{
280
if (src[src_idx] > 127)
281
{
282
dest[dest_idx++] = 0xFE;
283
}/*if*/
284
dest[dest_idx++] = (src[src_idx] & 0x7F);
285
}/*for*/
286
// Add null terminator
287
// -------------------
288
dest[dest_idx] = 0;
289
// Add command header
290
// ------------------
291
dest[0] = 0xFF;
292
dest[1] = dest_idx-2;
293
 
294
// Return escape string size
295
// -------------------------
296
return dest_idx;
297
}
298
 
299
 
300
#if defined HWVERSION3_1 || defined HWVERSION1_3
301
/*************************************************************************
302
Function: Wi232USB()
303
Purpose:  Connect Wi232 Programmmode to PKT USB,
304
Returns:
305
 
306
**************************************************************************/
307
void Wi232_USB(void)
308
 
309
 
310
{
311
        unsigned int c0,c1;
312
 
313
        if (Wi232_hardware==1)
314
        {
315
                USART_Init (UART_BAUD_SELECT(57600,F_CPU));
316
            uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
317
//              USART_Init (UART_BAUD_SELECT(2400,F_CPU));
318
//          uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
319
        }
320
        if (Wi232_hardware==2)
321
                {
322
                        USART_Init (UART_BAUD_SELECT(2400,F_CPU));
323
                    uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
324
                }
325
 
326
        lcd_cls ();
327
        SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
328
 
329
    set_WI232CMD();
330
    set_LED4();
331
 
332
    lcd_printpns_at (0, 0, PSTR("Wi.232 Konfiguration "),0);
333
    lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0);
334
    lcd_printpns_at (0, 2, PSTR("Wi.232"),0);
335
    lcd_printpns_at (0, 3, PSTR("Programm starten"),0);
336
    lcd_printpns_at (17, 7, PSTR("Exit"),0);
337
 
338
    c1 = 0;
339
 
340
         for(;;)
341
            {
342
 
343
                c0 = uart1_getc(); /* from USB*/
344
 
345
                if ( c0 & UART_NO_DATA )
346
                {
347
                {
348
                 c1 = USART_getc();
349
 
350
                 if (c1 == 0)
351
                        {}
352
                else
353
                {
354
                        lcd_print_hex(c1,0);
355
                        uart1_putc (c1);          /*to USB*/;
356
 
357
                }
358
             }
359
                }
360
 
361
                else
362
                {
363
                 USART_putc(c0 ); /* to Wi232*/
364
//               lcd_print_hex(c0,0);
365
//               _delay_ms(1);
366
                }
367
 
368
 
369
 
370
                if ((get_key_press (1 << KEY_ENTER)))
371
                         {
372
                          clr_WI232CMD();
373
                      clr_LED4();
374
//                    uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
375
//                    USART_Init( UART_BAUD_SELECT(57600,F_CPU) );
376
 
377
                      return;
378
                         }
379
 
380
            }
381
}
382
/*************************************************************************
383
Function: Wi232_FC()
384
Purpose:  Connect Wi232 to PKT USB, Transparent
385
Returns:
386
 
387
**************************************************************************/
388
void Wi232_FC(void)
389
 
390
 
391
{
392
        unsigned int c0,c1;
393
 
394
 
395
        USART_Init (UART_BAUD_SELECT(57600,F_CPU));
396
    uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
397
//      USART_Init (UART_BAUD_SELECT(2400,F_CPU));
398
 //   uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
399
 
400
        lcd_cls ();
401
        SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
402
 
403
 
404
    lcd_printpns_at (0, 0, PSTR("Wi.232 to FC "),0);
405
    lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0);
406
    lcd_printpns_at (0, 2, PSTR("und Mikrokoptertool"),0);
407
    lcd_printpns_at (0, 3, PSTR("starten"),0);
408
    lcd_printpns_at (17, 7, PSTR("Exit"),0);
409
 
410
    c1 = 0;
411
 
412
         for(;;)
413
            {
414
 
415
                c0 = uart1_getc(); /* from USB*/
416
 
417
                if ( c0 & UART_NO_DATA )
418
                {
419
                {
420
                 c1 = USART_getc();
421
 
422
                 if (c1 == 0)
423
                        {}
424
                else
425
              {
426
                        lcd_print_hex(c1,0);
427
                        uart1_putc (c1);          /*to USB*/;
428
 
429
                }
430
             }
431
                }
432
 
433
                else
434
                {
435
                 USART_putc(c0 ); /* to Wi232*/
436
                 lcd_print_hex(c0,0);
437
//               _delay_ms(1);
438
                }
439
 
440
 
441
 
442
                if ((get_key_press (1 << KEY_ENTER)))
443
                         {
444
 
445
                      return;
446
                         }
447
 
448
            }
449
}
450
#endif