Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
972 - 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
Function: InitWI232()
113
Purpose:  set Wi232Register for Mikrokopter
114
Returns:  0 = ACK, FF = NAK
115
 
116
**************************************************************************/
117
void InitWi232(void)
118
{
119
uint8_t InitErr=0;
120
 
121
        discoverWi232(); /*Check if Wi232 available*/
122
 
123
    if (Wi232_hardware !=0)
124
    {
125
          lcd_printpns_at (0, 2, PSTR("Init Wi232 wait...."),0);
126
          set_WI232CMD();
127
      _delay_ms(10);
128
      set_LED4();
129
          SwitchToWi232();      /* Serielle Kanäle Wi232 mit USB verbinden*/
130
         if  (WriteWi232(regTXCHANNEL,wChan1)!=0) /*TX Channel*/
131
                        InitErr =1;
132
         if  (WriteWi232(regRXCHANNEL,wChan1)!=0) /* RX Channel*/
133
                        InitErr =2;
134
         if  (WriteWi232(regSLPMODE ,Sleep_Awake)!=0) /* Sleepmode*/
135
                        InitErr =3;
136
         if  (WriteWi232(regPWRMODE,WbModeP13)!=0) /* Transceiver Mode/Powermode */
137
                        InitErr =4;
138
         if  (WriteWi232(regTXTO,TWaitTime16)!=0) /* UART Timeout */
139
                        InitErr =5;
140
         if  (WriteWi232(regUARTMTU,UartMTU64)!=0) /* UART Buffer*/
141
                        InitErr =6;
142
         if  (WriteWi232(regNETGRP,66)!=0) /* Networkgroup */
143
                        InitErr =7;
144
         if  (WriteWi232(regNETMODE,NetMode_Normal)!=0) /* Networkmode*/
145
                        InitErr =8;
146
         if  (WriteWi232(regUSECRC ,CRC_Enable)!=0) /* CRC*/
147
                        InitErr =9;
148
         if  (WriteWi232(regCSMAMODE,CSMA_En)!=0) /* CSMA*/
149
                        InitErr =10;
150
         if  (WriteWi232(regDATARATE,Wi232_57600)!=0) /* Baudrate*/
151
                        InitErr =11;
152
 
153
         if (InitErr !=0)
154
         {
155
                 lcd_printpns_at (0, 2, PSTR("Wi232 InitError  "),0);
156
                 lcd_print_hex(InitErr,0);
157
         }
158
         else
159
                 lcd_printpns_at (0, 2, PSTR("Wi232 Init ok...."),0);
160
 
161
          USART_Init (UART_BAUD_SELECT(57600,F_CPU));
162
      clr_WI232CMD();
163
      _delay_ms(10);
164
          clr_LED4();
165
    }
166
 
167
 
168
}
169
 
170
 
171
/*************************************************************************
172
Function: WriteWI232()
173
Purpose:  set Register to Wi232, Register, Value
174
Returns:  0 = ACK, FF = NAK
175
ACHTUNG nur für Value <0x80
176
**************************************************************************/
177
int16_t WriteWi232(uint8_t Wi232Register, uint8_t RegisterValue)
178
 
179
{
180
        uint8_t timeout=10;
181
        uint8_t tc=0;
182
        unsigned int v;
183
 
184
        USART_putc(0xff);
185
        USART_putc(0x02);
186
        USART_putc(Wi232Register);
187
        USART_putc(RegisterValue);
188
    do
189
        {
190
     v = USART_getc(); /*ACK erwartet*/
191
     _delay_ms(100);
192
     tc ++;
193
    }
194
    while (v==0 && tc!=timeout);
195
//    lcd_print_hex(v,0);
196
        if (v != 0x06)
197
        {
198
          lcd_printpns_at (0, 2, PSTR("Wi.232 NAK"),0);
199
          return 0xFF;
200
        }
201
 
202
        if (v==0x06)
203
        return 0;
204
 
205
        return 0xFF;
206
}
207
 
208
 
209
/*************************************************************************
210
Function: readWI232()
211
Purpose:  send Readcommand to Wi232,
212
Returns:  Registervalue, 0 = timeout 0xFF = Syntaxerror
213
 
214
**************************************************************************/
215
int16_t readWi232(uint16_t Wi232Register)
216
 
217
{
218
        uint8_t timeout=10;
219
        uint8_t tc=0;
220
 
221
 
222
        unsigned int v;
223
 
224
        v = USART_getc(); /*Zeichen löschen*/
225
        USART_putc(0xff);
226
        USART_putc(0x02);
227
        USART_putc(0xfe);
228
        USART_putc(Wi232Register);
229
    _delay_ms(50);
230
//    lcd_printpns_at (0, 2, PSTR("read Wi232"),0);
231
 
232
 
233
    do
234
        {
235
     v = USART_getc(); /*ACK erwartet*/
236
     _delay_ms(100);
237
     tc ++;
238
    }
239
    while (v==0 && tc!=timeout);
240
 
241
    if (tc == timeout)  return 0; /* Timeout*/
242
        if (v != 0x06) return 0xFF; /* Syntaxerror*/
243
    lcd_print_hex(v,0);
244
    v = USART_getc(); /*Register*/
245
    lcd_print_hex(v,0);
246
    v = USART_getc(); /*Value*/
247
    lcd_print_hex(v,0);
248
 
249
    return v;
250
 
251
}
252
 
253
 
254
 
255
/*************************************************************************
256
Function: EscapeString()
257
Purpose:
258
Returns:
259
Quelle: Radiotronix Wi.232 Manual
260
**************************************************************************/
261
 
262
 
263
int EscapeString(char *src, char src_len, char *dest)
264
{
265
// The following function copies and encodes the first
266
// src_len characters from *src into *dest. This
267
// encoding is necessary for Wi.232 command formats.
268
// The resulting string is null terminated. The size
269
// of this string is the function return value.
270
// ---------------------------------------------------
271
uint8_t src_idx, dest_idx;
272
// Save space for the command header and size bytes
273
// ------------------------------------------------
274
dest_idx = 2;
275
// Loop through source string and copy/encode
276
// ------------------------------------------
277
for (src_idx = 0; src_idx < src_len; src_idx++)
278
{
279
if (src[src_idx] > 127)
280
{
281
dest[dest_idx++] = 0xFE;
282
}/*if*/
283
dest[dest_idx++] = (src[src_idx] & 0x7F);
284
}/*for*/
285
// Add null terminator
286
// -------------------
287
dest[dest_idx] = 0;
288
// Add command header
289
// ------------------
290
dest[0] = 0xFF;
291
dest[1] = dest_idx-2;
292
 
293
// Return escape string size
294
// -------------------------
295
return dest_idx;
296
}
297
 
298
/*************************************************************************
299
Function: Wi232USB()
300
Purpose:  Connect Wi232 Programmmode to PKT USB,
301
Returns:
302
 
303
**************************************************************************/
304
void Wi232_USB(void)
305
 
306
 
307
{
308
        unsigned int c0,c1;
309
 
310
        if (Wi232_hardware==1)
311
        {
312
                USART_Init (UART_BAUD_SELECT(57600,F_CPU));
313
            uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
314
//              USART_Init (UART_BAUD_SELECT(2400,F_CPU));
315
//          uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
316
        }
317
        if (Wi232_hardware==2)
318
                {
319
                        USART_Init (UART_BAUD_SELECT(2400,F_CPU));
320
                    uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
321
                }
322
 
323
        lcd_cls ();
324
        SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
325
 
326
    set_WI232CMD();
327
    set_LED4();
328
 
329
    lcd_printpns_at (0, 0, PSTR("Wi.232 Konfiguration "),0);
330
    lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0);
331
    lcd_printpns_at (0, 2, PSTR("Wi.232"),0);
332
    lcd_printpns_at (0, 3, PSTR("Programm starten"),0);
333
    lcd_printpns_at (17, 7, PSTR("Exit"),0);
334
 
335
    c1 = 0;
336
 
337
         for(;;)
338
            {
339
 
340
                c0 = uart1_getc(); /* from USB*/
341
 
342
                if ( c0 & UART_NO_DATA )
343
                {
344
                {
345
                 c1 = USART_getc();
346
 
347
                 if (c1 == 0)
348
                        {}
349
                else
350
                {
351
                        lcd_print_hex(c1,0);
352
                        uart1_putc (c1);          /*to USB*/;
353
 
354
                }
355
             }
356
                }
357
 
358
                else
359
                {
360
                 USART_putc(c0 ); /* to Wi232*/
361
//               lcd_print_hex(c0,0);
362
//               _delay_ms(1);
363
                }
364
 
365
 
366
 
367
                if ((get_key_press (1 << KEY_ENTER)))
368
                         {
369
                          clr_WI232CMD();
370
                      clr_LED4();
371
//                    uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
372
//                    USART_Init( UART_BAUD_SELECT(57600,F_CPU) );
373
 
374
                      return;
375
                         }
376
 
377
            }
378
}
379
/*************************************************************************
380
Function: Wi232_FC()
381
Purpose:  Connect Wi232 to PKT USB, Transparent
382
Returns:
383
 
384
**************************************************************************/
385
void Wi232_FC(void)
386
 
387
 
388
{
389
        unsigned int c0,c1;
390
 
391
 
392
        USART_Init (UART_BAUD_SELECT(57600,F_CPU));
393
    uart1_init( UART_BAUD_SELECT(57600,F_CPU) );
394
//      USART_Init (UART_BAUD_SELECT(2400,F_CPU));
395
 //   uart1_init( UART_BAUD_SELECT(2400,F_CPU) );
396
 
397
        lcd_cls ();
398
        SwitchToWi232();        /* Serielle Kanäle Wi232 mit USB verbinden*/
399
 
400
 
401
    lcd_printpns_at (0, 0, PSTR("Wi.232 to FC "),0);
402
    lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0);
403
    lcd_printpns_at (0, 2, PSTR("und Mikrokoptertool"),0);
404
    lcd_printpns_at (0, 3, PSTR("starten"),0);
405
    lcd_printpns_at (17, 7, PSTR("Exit"),0);
406
 
407
    c1 = 0;
408
 
409
         for(;;)
410
            {
411
 
412
                c0 = uart1_getc(); /* from USB*/
413
 
414
                if ( c0 & UART_NO_DATA )
415
                {
416
                {
417
                 c1 = USART_getc();
418
 
419
                 if (c1 == 0)
420
                        {}
421
                else
422
              {
423
                        lcd_print_hex(c1,0);
424
                        uart1_putc (c1);          /*to USB*/;
425
 
426
                }
427
             }
428
                }
429
 
430
                else
431
                {
432
                 USART_putc(c0 ); /* to Wi232*/
433
                 lcd_print_hex(c0,0);
434
//               _delay_ms(1);
435
                }
436
 
437
 
438
 
439
                if ((get_key_press (1 << KEY_ENTER)))
440
                         {
441
 
442
                      return;
443
                         }
444
 
445
            }
446
}