Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
2136 - 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
//############################################################################
36
//# HISTORY  Wi232.c
37
//#
38
//# 15.06.2014 OG
39
//# -chg: umgestellt auf PKT_Progress_Init() / PKT_Progress_Next()
40
//#
41
//# 10.06.2014 OG
42
//#        KOMPLETTE SANIERUNG ab v3.80cX5
43
//#        (ehemaliger Code in v3.80cX4 oder frueher)
44
//# - add: Wi232_ConfigPC() - ehemals in connect.c als Port_USB2CFG_Wi()
45
//#
46
//# 06.06.2014 OG
47
//# - add: Wi232_Write_Progress()
48
//# - chg: Codeformattierung
49
//#
50
//# 05.06.2014 OG
51
//# -chg: Codeformattierung
52
//#
53
//# 14.05.2014 OG
54
//# - chg: InitErrorWi232(), InitWi232() - umgestellt auf dezimale Anzeige des
55
//#        Fehlercodes damit der einfacher im Source zu identifizieren ist
56
//# - add: Source-Historie ergaenzt
57
//############################################################################
58
 
59
 
60
#include "../cpu.h"
61
#include <avr/io.h>
62
#include <avr/pgmspace.h>
63
#include <util/delay.h>
64
#include <stdlib.h>
65
#include <string.h>
66
#include "../messages.h"
67
#include "../lcd/lcd.h"
68
#include "../uart/usart.h"
69
#include "../uart/uart1.h"
70
#include "../main.h"
71
#include "../wi232/Wi232.h"
72
#include "../timer/timer.h"
73
#include "../eeprom/eeprom.h"
74
#include "../setup/setup.h"
75
#include "../pkt/pkt.h"
76
#include "../utils/xutils.h"
77
#include "../lipo/lipo.h"
78
#include "../utils/menuctrl.h"
79
 
80
 
81
//############################################################################
82
//############################################################################
83
 
84
 
85
static const char STR_WI232INIT[]    PROGMEM = "Wi.232 Init";
86
 
87
 
88
//############################################################################
89
//############################################################################
90
 
91
 
92
//--------------------------------------------------------------
93
// lError = WriteWi232(  Wi232Register, RegisterValue)
94
//
95
// set Register to Wi232, Register, Value
96
//
97
// Returns:
98
//   0 = ACK, FF = NAK
99
//--------------------------------------------------------------
100
uint8_t Wi232_Write( uint8_t Wi232Register, uint8_t RegisterValue )
101
{
102
    uint8_t timeout = 10;
103
    uint8_t tc      = 0;
104
    unsigned int v;
105
 
106
 
107
    if( RegisterValue < 0x80 )
108
    {
109
        USART_putc(0xff);
110
        USART_putc(0x02);
111
        USART_putc(Wi232Register);
112
        USART_putc(RegisterValue);
113
    }
114
    else    // RegisterValue >= 0x80
115
    {
116
        USART_putc(0xff);
117
        USART_putc(0x03);
118
        USART_putc(Wi232Register);
119
        USART_putc(0xfe);
120
        USART_putc(RegisterValue - 0x80);
121
    }
122
 
123
 
124
    do
125
    {
126
        v = USART_getc();                                                   // ACK erwartet
127
        _delay_ms(100);
128
        tc++;
129
    } while( (v == 0) && (tc != timeout) );
130
 
131
 
132
    if( v != 0x06 )
133
    {
134
      //PKT_Message_P( text, error, timeout, beep, clearscreen)
135
      //PKT_Message_P( PSTR("Wi.232 NAK"), true, 2000, true, true);         // ERROR: max. 20 Sekunden anzeigen (wird nicht mehr angezeigt da uebernommen von hoeheren Funktionen)
136
        return 0xFF;                                                        // return: ERROR
137
    }
138
 
139
    return 0;                                                               // return: OK  (v==0x06)
140
}
141
 
142
 
143
 
144
//--------------------------------------------------------------
145
//--------------------------------------------------------------
146
uint8_t Wi232_Write_Progress( uint8_t Wi232Register, uint8_t RegisterValue, uint8_t errorcode )
147
{
148
    uint8_t nError;
149
 
150
    // nError = 0: ok
151
    // nError > 0: Fehler
152
    nError = Wi232_Write( Wi232Register, RegisterValue );
153
 
154
    PKT_Progress_Next();                                        // Progressbar
155
 
156
    if( nError )
157
    {
158
        nError = errorcode;
159
    }
160
 
161
    return nError;
162
}
163
 
164
 
165
 
166
//--------------------------------------------------------------
167
// Registervalue = Wi232_Read( Wi232Register )
168
//
169
// send Readcommand to Wi232,
170
//
171
// Returns:
172
//   Registervalue: 0 = timeout 0xFF = Syntaxerror
173
//--------------------------------------------------------------
174
uint8_t Wi232_Read( uint8_t Wi232Register )
175
{
176
    uint8_t timeout = 10;
177
    uint8_t tc = 0;
178
    uint8_t v;
179
 
180
 
181
    v = USART_getc();                   // Zeichen loeschen
182
    USART_putc(0xff);
183
    USART_putc(0x02);
184
    USART_putc(0xfe);
185
    USART_putc(Wi232Register);
186
    _delay_ms(50);
187
  //lcd_printpns_at (0, 2, PSTR("read Wi232"),0);
188
 
189
 
190
    do
191
    {
192
        v = USART_getc();               //ACK erwartet
193
        _delay_ms(100);
194
        tc++;
195
    } while( v==0 && tc!=timeout );
196
 
197
    if( tc == timeout )
198
        return 0;                       // Timeout
199
 
200
    if( v != 0x06 )
201
        return 0xFF;                    // Syntaxerror
202
 
203
//  lcd_print_hex(v,0);
204
//  v = USART_getc(); /*Register*/
205
//  lcd_print_hex(v,0);
206
//  v = USART_getc(); /*Value*/
207
//  lcd_print_hex(v,0);
208
 
209
    return v;
210
}
211
 
212
 
213
 
214
//--------------------------------------------------------------
215
// foundbaud = Wi232_CheckBaud( Baudrate )
216
//
217
// RUECKGABE:
218
//  =0: Error (Wi.232 not found at given Baud)
219
//  >0: Baudrate
220
//
221
// INFO: Baudrate
222
//   Baud_9600       1
223
//   Baud_19200      2
224
//   Baud_38400      3
225
//   Baud_57600      4
226
//   Baud_115200     5
227
//   Baud_4800       6
228
//   Baud_2400       7
229
//--------------------------------------------------------------
230
uint16_t Wi232_CheckBaud( uint8_t Baudrate )                // Suche Wi232 mit entsprechender Baudrate
231
{
232
    uint8_t RegisterWi232;
233
 
234
  //lcdx_cls_row( y, mode, yoffs )
235
    lcdx_cls_row( 2, MNORMAL, 1 );                          // Zeile loeschen
236
    lcdx_printf_center_P( 2, MNORMAL, 0,1, PSTR("%S %lu Baud"), strGet(STR_SEARCH), Baud_to_uint32(Baudrate) );   // "suche nnn Baud"
237
 
238
    SetBaudUart0( Baudrate );                               // UART auf angegebene Baudrate setzen
239
 
240
    RegisterWi232 = Wi232_Read( regDiscover );
241
 
242
    //---------------------------------------------------
243
    // Hinweis zu RegisterWi232 nach regDiscover
244
    //   == 0   : "no Wi.232 found"
245
    //   == 0xFF: "Wi.232 Syntaxerror"
246
    //
247
    //   >0 && < 0xff: Wi.232 Version (Wi.232 gefunden)
248
    //---------------------------------------------------
249
    if( RegisterWi232 > 0 && RegisterWi232 < 0xFF )
250
    {
251
        return Baudrate;                                    // Wi.232 gefunden
252
    }
253
 
254
    return 0;                                               // Wi.232 nicht gefunden bei gegebener Baudrate
255
}
256
 
257
 
258
 
259
//--------------------------------------------------------------
260
// InitWI232( InitBaudrate )
261
//
262
// set Wi232Register for Mikrokopter
263
//
264
// Returns:
265
//   0 = ACK, FF = NAK     ?????????
266
//--------------------------------------------------------------
267
void Wi232_Initalize( void )
268
{
269
    uint8_t foundbaud = 0;
270
    uint8_t nError  = 0;
271
    uint8_t InitBaudrate = Config.PKT_Baudrate;
272
 
273
 
274
    //----------------------------
275
    // 1. Kopter ausgeschaltet?
276
    //----------------------------
277
    // Text1: "Kopter ausschalten!"
278
    // Text2: NULL
279
    // Head : "* ACHTUNG *"
280
    // Titel: "Wi.232 Init"
281
 
282
    if( !PKT_Ask_P( ASK_CANCEL_OK, strGet(STR_SWITCHOFFMK), NULL, strGet(STR_ATTENTION), STR_WI232INIT) )
283
    {
284
        return;
285
    }
286
 
287
 
288
    //----------------------------
289
    // 2. Wi.232 suchen
290
    //----------------------------
291
    PKT_Title_P( STR_WI232INIT, true, true );                               // Titel - mit ShowLipo und clearscreen
292
 
293
    Change_Output( Uart02Wi );                                              // Verbindung zu Wi232 herstellen
294
    SwitchToWi232();                                                        // Serielle Kanaele Wi232 mit USB verbinden
295
    set_WI232CMD();
296
    _delay_ms(100);
297
  //_delay_ms(200);
298
 
299
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Old_Baudrate );        // versuche zuerst mit der 'alten' Baudrate (hohe Trefferwahrscheinlichkeit)
300
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Baud_2400    );
301
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Baud_9600    );
302
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Baud_19200   );
303
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Baud_38400   );
304
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Baud_57600   );
305
    if( !foundbaud )    foundbaud = Wi232_CheckBaud( Baud_115200  );
306
 
307
    if( !foundbaud )                                                        // gefunden?
308
    {
309
      //PKT_Message_P( text, error, timeout, beep, clearscreen)
310
        PKT_Message_P( PSTR("Wi.232 not found"), true, 2000, true, true);   // ERROR: max. 20 Sekunden anzeigen
311
    }
312
 
313
 
314
    //----------------------------
315
    // 3. Wi.232 initialisieren
316
    //----------------------------
317
    if( foundbaud > 0 )
318
    {
319
        //--------------
320
        // Screen
321
        //--------------
322
        PKT_Title_P( STR_WI232INIT, true, true );                           // Titel - mit ShowLipo und clearscreen
323
        lcdx_printf_center_P( 2, MNORMAL, 0,1, STR_WI232INIT );
324
 
325
      //PKT_Progress_Init( max, yoffs, width, height);
326
        PKT_Progress_Init( 18, 0,  0,0);                                    // 18 Progress Steps
327
 
328
 
329
        // DEBUG - hier kann ggf. die _gefundene_ Baudrate vom Wi.232 angezeigt werden
330
        //lcdx_printf_center_P( 6, MNORMAL, 0,-4, PSTR("(found %lu Baud)"), Baud_to_uint32(foundbaud) );   // zeige Baudrate
331
        // DEBUG - und hier auf welche Baudrate das Wi.232 gesetzt werden soll
332
        //lcdx_printf_center_P( 7, MNORMAL, 0,-2, PSTR("(set %lu Baud)"), Baud_to_uint32(Config.PKT_Baudrate) );   // zeige Baudrate
333
 
334
 
335
        SetBaudUart0( foundbaud );
336
 
337
        // wenn sich ein EEPROM-Wert ändert wird auch das Ram beschrieben damit die Änderung sofort wirksam wird
338
 
339
        //--------------------------------------------------------------------------------------------------------------------
340
        //                                                                                + Errorcode
341
        //                                                                                |
342
        //                                                                                |
343
        if( !nError ) nError = Wi232_Write_Progress( regNVTXCHANNEL,Config.WiTXRXChannel, 1);   // TX Channel
344
        if( !nError ) nError = Wi232_Write_Progress( regTXCHANNEL,Config.WiTXRXChannel  , 2);   // TX Channel
345
        if( !nError ) nError = Wi232_Write_Progress( regNVRXCHANNEL,Config.WiTXRXChannel, 3);   // RX Channel
346
        if( !nError ) nError = Wi232_Write_Progress( regRXCHANNEL,Config.WiTXRXChannel  , 4);   // RX Channel
347
        if( !nError ) nError = Wi232_Write_Progress( regNVSLPMODE ,Sleep_Awake          , 5);   // Sleepmode
348
        if( !nError ) nError = Wi232_Write_Progress( regNVPWRMODE,WbModeP15             , 6);   // Transceiver Mode/Powermode
349
 
350
 
351
        //---------
352
        // 06.06.2014 OG: nicht mehr unterstuetzt via setup.c (ggf. spaeter festen Standardwert einsetzen statt Config.xyz)
353
        // Standard: TWaitTime16 (=0x10  // 16 ms Delay (default))
354
        if( !nError ) nError = Wi232_Write_Progress( regNVTXTO,Config.WiTXTO            , 7);   // UART Timeout
355
        if( !nError ) nError = Wi232_Write_Progress( regTXTO,Config.WiTXTO              , 8);   // UART Timeout
356
 
357
        //---------
358
        // 06.06.2014 OG: nicht mehr unterstuetzt via setup.c (ggf. spaeter festen Standardwert einsetzen statt Config.xyz)
359
        // Standard: UartMTU64 (=64  // default=64, valid 1-144)
360
        if( !nError ) nError = Wi232_Write_Progress( regNVUARTMTU,Config.WiUartMTU      , 9);   // UART Buffer
361
        if( !nError ) nError = Wi232_Write_Progress( regUARTMTU,Config.WiUartMTU        ,10);   // UART Buffer
362
 
363
        //---------
364
        // 06.06.2014 OG: nicht mehr unterstuetzt via setup.c (ggf. spaeter festen Standardwert einsetzen statt Config.xyz)
365
        // Standard: NetMode_Normal (=0x01  // Normalmode (default))
366
        if( !nError ) nError = Wi232_Write_Progress( regNVNETMODE,Config.WiNetworkMode  ,11);   // Networkmode
367
        if( !nError ) nError = Wi232_Write_Progress( regNETMODE,Config.WiNetworkMode    ,12);   // Networkmode
368
 
369
        if( !nError ) nError = Wi232_Write_Progress( regNVUSECRC ,CRC_Enable            ,13);   // CRC
370
        if( !nError ) nError = Wi232_Write_Progress( regNVCSMAMODE,CSMA_En              ,14);   // CSMA
371
        if( !nError ) nError = Wi232_Write_Progress( regNVNETGRP,Config.WiNetworkGroup  ,15);   // Networkgroup
372
        if( !nError ) nError = Wi232_Write_Progress( regNETGRP,Config.WiNetworkGroup    ,16);   // Networkgroup
373
        if( !nError ) nError = Wi232_Write_Progress( regNVDATARATE,InitBaudrate         ,17);   // Baudrate
374
        _delay_ms(200);
375
        if( !nError ) nError = Wi232_Write_Progress( regDATARATE,InitBaudrate           ,18);   // Baudrate
376
        _delay_ms(250);
377
 
378
        //_delay_ms(6000);  // DEBUG
379
 
380
 
381
        if( nError )
382
        {
383
          //PKT_Message( text, error, timeout, beep, clearscreen)
384
            PKT_Message( buffered_sprintf_P(PSTR("Wi.232 Error: %d"),nError), true, 2000, true, true);      // ERROR: 2000 = max. 20 Sekunden anzeigen
385
        }
386
        else
387
        {
388
          //PKT_Message_P( text, error, timeout, beep, clearscreen)
389
            PKT_Message_P( PSTR("Wi.232 Init OK!"), false, 1000, true, true);                               // OK: 1000 = max. 10 Sekunden anzeigen
390
            WriteWiInitFlag();                                                                              // erfolgreiches Init merken
391
        }
392
    }
393
 
394
 
395
    clr_WI232CMD();
396
    SetBaudUart0( Config.PKT_Baudrate );                                // die richtige UART Baudrate wiederherstellen !!
397
    clear_key_all();
398
}
399
 
400
 
401
 
402
//--------------------------------------------------------------
403
// Wi232_ConfigPC()
404
//
405
// Connect USB direct to Wi.232 in Progmode
406
//--------------------------------------------------------------
407
void Wi232_ConfigPC( void )
408
{
409
    lcd_cls ();
410
 
411
    lcd_frect( 0, 0, 127, 7, 1);                                            // Titelzeile Invers
412
    lcdx_printf_at_P( 1, 0, MINVERS, 0,0, strGet(STR_WI232CONFIG) );        // Titeltext "Wi.232 Konfig."
413
 
414
    lcdx_printp_center( 2, strGet(STR_USBCONNECTED), MNORMAL,  0,-2);       // "mit USB verbunden"
415
 
416
    lcdx_printp_center( 4, strGet(STR_SEEMKWIKI)   , MNORMAL,  0,0);        // "siehe MK-Wiki:"
417
    lcdx_printp_center( 5, PSTR("\"RadioTronix\"") , MNORMAL,  0,2);        // "RadioTronix"
418
    lcd_rect_round( 0, 28, 127, 24, 1, R2);                                 // Rahmen um Text
419
 
420
    lcd_printp_at(12, 7, strGet(ENDE), MNORMAL);                            // Keyline "Ende"
421
 
422
 
423
    Change_Output(USB2Wi);
424
 
425
    set_WI232CMD();  // Port D6 = CMD
426
 
427
    while(!get_key_press(1 << KEY_ESC))
428
    {
429
        show_Lipo();
430
    }
431
 
432
    clr_WI232CMD();  // Port D6 = CMD
433
 
434
    if( Config.U02SV2 == 1 )
435
        Change_Output(Uart02FC);
436
    else
437
        Change_Output(Uart02Wi);
438
 
439
 
440
    // 10.06.2014 OG: das Folgende war im orginalen Port_USB2CFG_Wi() nicht
441
    //                vorhanden - das hatte den Effekt, dass wenn man diese
442
    //                Funktion aufgerufen und wieder beendet hatte das
443
    //                PKT-Updatetool kein PKT-Update mehr via PKT_CtrlHook()
444
    //                durchfuehren konnte. Die USB Umschaltung war wohl nicht sauber.
445
    //
446
    uart1_init( UART_BAUD_SELECT(USART_BAUD,F_CPU) );   // USB wieder zuruecksetzen damit PKT wieder normal via USB erreichbar (u.a. fuer PKT-Updatetool)
447
    SetBaudUart1( Config.PKT_Baudrate );                // USB wieder zuruecksetzen damit PKT wieder normal via USB erreichbar (u.a. fuer PKT-Updatetool)
448
}
449
 
450
 
451
//#endif