Subversion Repositories Projects

Rev

Rev 2147 | Go to most recent revision | Details | Compare with Previous | 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
//# HISTORY  HAL_HW3_9.c
36
//#
37
//#
38
//# 10.11.2014 cebra
39
//# - chg: Displaybeleuchtung aktiv einschalten beim Start
40
//#
41
//# 14.06.2014 OG
42
//# - chg: InitHWPorts() - umgestellt auf PKT_TitlePKTlipo(false) da eine
43
//#        richtige LiPo-Anzeige im Startbildschirm zu diesem Zeitpunkt noch
44
//#        nicht moeglich ist
45
//# - chg: InitHWPorts() - bt_init() umgestellt auf BTM222_Initalize()
46
//#
47
//# 11.06.2014 OG
48
//# - fix: InitHWPorts() einstellen der Sprache geht jetzt wieder
49
//#
50
//# 10.06.2014 OG
51
//# - chg: InitHWPorts() - umgestellt auf Wi232_Initalize()
52
//#
53
//# 08.06.2014 OG
54
//# - chg: InitHWPorts() - Aufruf von InitWi232() geaendert
55
//#
56
//# 28.05.2014 OG
57
//# - chg: InitHWPorts() - Bearbeiten der Sprache auskommentiert weil sich
58
//#        Edit_generic() geaendert hat und notwendige Texte aus einem Menue
59
//#        bezieht welches hier in der HAL nicht vorhanden ist
60
//#
61
//# 01.04.2014 OG
62
//# - chg: PCB_WiFlyPatch umbenannt zu PCB_SV2RxTxPatch
63
//#
64
//# 24.03.2014 OG
65
//# - chg: InitHWPorts(): den Begriff "WiFly" bzw. "WiFly Patch" geaendert
66
//#        zu "SV2 Patch"
67
//#
68
//# 21.02.2014 OG
69
//# - chg: InitHWPorts() auf PKT_TitlePKTVersion() angepasst
70
//#
71
//# 20.02.2014 OG
72
//# - chg: InitHWPorts() Anzeige vom Startscreen ueberarbeitet
73
//# - chg: Code-Formatierung
74
//#
75
//# 30.01.2014 OG
76
//# - add: Unterstuetzung fuer USE_BLUETOOTH
77
//#
78
//# 29.01.2014 OG
79
//# - del: #include "display.h"
80
//#
81
//# 25.08.2013 Cebra
82
//# - add: #ifdef USE_TRACKING vor Servo_init
83
//#
84
//# 07.07.2013 OG
85
//# - add: DEBUG_SV2_EXTENSION in InitHWPorts() bzgl. Anzeige von "A:.. B:.." usw.
86
//#
87
//# 26.06.2013 OG
88
//# - del: Code zu USE_PKT_STARTINFO
89
//#
90
//# 26.06.2013 Cebra
91
//# - add: Modulumschaltung für WiFlypatch umgebaut
92
//# - chg: Wartezeit nach Einschalten von BT auf 3 Sekunden verlängert
93
//#
94
//# 22.06.2013 cebra
95
//# - chg: Fehler in CheckPCB führte zu fehlerhafter Lipomessung, alle Pins von Port A waren auf Eingang
96
//#
97
//# 13.06.2013 cebra
98
//# - chg: Displaybeleuchtung ohne Helligkeitssteuerung
99
//#
100
//# 15.05.2013 OG
101
//# - chg: define USE_PKT_STARTINFO ergaenzt (siehe main.h)
102
//#
103
//# 28.04.2013 OG
104
//# - add: Unterstuetzung von define USE_FONT_BIG (Layoutanpassung)
105
//#
106
//# 12.04.2013 Cebra
107
//# - chg: Fehler bei der Umschaltung Wi232/Kabelverbindung beim Start des PKT
108
//###########################################################################
109
 
110
 
111
#ifndef HAL_HW3_9_C_
112
#define HAL_HW3_9_C_
113
 
114
#include "cpu.h"
115
#include <inttypes.h>
116
#include <avr/io.h>
117
#include <avr/interrupt.h>
118
#include <avr/pgmspace.h>
119
#include <avr/eeprom.h>
120
#include <util/delay.h>
121
#include <stdbool.h>
122
#include <stdlib.h>
123
 
124
#include "main.h"
125
 
126
//#if defined HWVERSION3_9
127
#include "eeprom/eeprom.h"
128
#include "messages.h"
129
#include "lcd/lcd.h"
130
#include "uart/usart.h"
131
#include "uart/uart1.h"
132
#include "timer/timer.h"
133
 
134
#include "wi232/Wi232.h"
2194 - 135
#include "i2cmaster.h"
2136 - 136
#include "bluetooth/bluetooth.h"
137
#include "bluetooth/error.h"
138
#include "connect.h"
139
#include "lipo/lipo.h"
140
#include "pkt/pkt.h"
141
#include "setup/setup.h"
142
#include "osd/osd.h"
143
#include "sound/pwmsine8bit.h"
144
#include "tracking/ng_servo.h"
145
 
146
 
147
 
148
volatile uint8_t USBBT;
149
volatile uint8_t U02SV2;
150
volatile uint8_t PCB_Version;
151
volatile uint8_t PCB_SV2RxTxPatch;
152
 
153
 
154
uint8_t PortA;
155
uint8_t PortB;
156
uint8_t PortC;
157
uint8_t PortD;
158
 
159
 
160
 
161
//--------------------------------------------------------------
162
//--------------------------------------------------------------
163
void CheckPCB(void)
164
{
165
//
166
  DDRA = 0x00;
167
  DDRB = 0x00;
168
  DDRC = 0x00;
169
  DDRD = 0x00;
170
  PortA = PINA;
171
  PortB = PINB;
172
  PortC = PINC;
173
  PortD = PIND;
174
 
175
 
176
  DDRC = 0x00;
177
  PORTC &= ~(1<<PORTC7);                // Pullup setzen
178
  if (PINC & (1<<PINC7) ) PCB_Version = PKT39m; else PCB_Version = PKT39x;
179
  PORTC &= ~(1<<PORTC0);                // Pullup setzen
180
  if (PINC & (1<<PINC0) ) PCB_SV2RxTxPatch = false; else PCB_SV2RxTxPatch = true;
181
  PORTC |= (1<<PORTC0);                 // Pullup aus
182
 
183
}
184
 
185
 
186
 
187
//--------------------------------------------------------------
188
//--------------------------------------------------------------
189
void InitHWPorts( void )  // Initialisierung der Hardware für die jeweilige Leiterplattenversion
190
{
191
    int8_t yoffs;
192
 
193
    CheckPCB();                                                     // Version der Leiterplatte prüfen
194
 
195
    if( PCB_SV2RxTxPatch )
196
    {
197
        DDRC |= (1<<DDC0)|(1<<DDC1);                                // Ausgang: C0 Reset BT, C1 Reset WiFly
198
        PORTC &= ~((1<<BT_Reset)|(1<<WiFly_Reset)|(1<<USB_Reset));  // Erst alle Module ausschalten
199
    }
200
 
201
    PORTA |= (1<<PORTA4)|(1<<PORTA5)|(1<<PORTA6)|(1<<PORTA7);       // Enable Pull Up for the 4 keys
202
    DDRA &= ~(1<<DDA4);                                             // Eingang: A4 auf Low setzen (Power On)
203
 
204
    if( Config.Lipomessung == true )
205
    {
206
        DDRA &= ~(1<<DDA1);                                         // Eingang: PKT Lipo Messung
207
        //PORTA &= ~(1<<PORTA1);
208
    }
209
 
210
    DDRB = 0xFF;                                                    // alles Ausgaenge
211
 
212
    PORTC |= (1<<PORTC4)|(1<<PORTC7);                               // Enable Pull Up for LBO + Summer
213
    DDRC |= (1<<DDC2)|(1<<DDC3)|(1<<DDC5)|(1<<DDC6|(1<<DDC7));      // Ausgang: Led2,Rs232Switch,Summer
214
    DDRC &= ~(1<<DDC4);                                             // Eingang: LowBat LTC1308
215
 
216
    if( PCB_SV2RxTxPatch )  clr_USBOn();
217
    else                    _BTOn();                                // Erstmal USB dektivieren, damit beim versehentlichen Einschalten USB im PC ruhig bleibt
218
 
219
 
220
    DDRD |= (1<<DDD4)|(1<<DDD5)|(1<<DDD6)|(1<<DDD7);                // Ausgang: PiepserTest, Servo, Wi232-CMD und Beleuchtung
221
    PORTD |= (1<<PORTD6);                                           // Wi232-CMD auf High schalten
222
    set_D_LIGHT();                                                  // Displaybeleuchtung an
223
 
224
 
225
    set_V_On();                                                     // Spannung mit T3 halten
226
    Timer0_Init();                                                  // system
227
 
228
    Display_on = 1;
229
    USART_Init (UART_BAUD_SELECT(USART_BAUD,F_CPU));
230
    uart1_init (UART_BAUD_SELECT(USART_BAUD,F_CPU));                // USB
2194 - 231
//    I2C_Init(1);
232
    i2c_init();
2136 - 233
 
234
    LCD_Init(0);                                                    // muss vor "ReadParameter" stehen
235
    ReadParameter();                                                // aktuelle Werte aus EEProm auslesen
236
 
237
    #ifdef USE_TRACKING
238
        servoInit(SERVO_PERIODE);
239
    #endif
240
 
241
    //servoSetDefaultPos();
242
    sei();
243
 
244
    Old_Baudrate = Config.PKT_Baudrate;
245
    SetBaudUart1( Config.PKT_Baudrate );
246
    SetBaudUart0( Config.PKT_Baudrate );
247
 
248
    // 10.11.2015 cebra
249
    // TODO cebra: diese Stelle muss bei HW_Sound überarbeitet werden
250
    //    if( (Config.HWSound==0) && (PCB_Version == PKT39m) )
251
    //    {
252
    //      Timer2_Init(); // Displaybeleuchtung oder Soundmodul
253
    //      OCR2A = Config.LCD_Helligkeit * 2.55;
254
    //    }
255
 
256
 
257
 
258
 
259
    #ifdef USE_SOUND
260
        if (Config.HWSound==1) InitSound();
261
    #endif
262
 
263
 
264
    LCD_Init(1);
265
    set_beep ( 400, 0x0080, BeepNormal);
266
 
267
    OSD_active = false;                                             // keine OSD Ausgabe
268
 
269
    ADC_Init();                                                     // ADC für Lipomessung// MartinR: verschoben
270
 
271
 
272
    //-------------------------
273
     // beim ersten Start (oder nach Reset) Sprache abfragen
274
    //-------------------------
275
    if( Config.DisplayLanguage > NUM_LANG )
276
    {
277
        Config.DisplayLanguage = 1;
278
      //Config.DisplayLanguage = Edit_generic( Config.DisplayLanguage,  0,  1, Language,1,   NULL,NULL);        // OG: ?? hierbei wird die falsche Sprache angezeigt - warum?
279
        Config.DisplayLanguage = Edit_generic( 2,  0,  1, Language,1,   NULL,NULL);
280
        WriteParameter();
281
    }
282
 
283
 
284
    //-------------------------
285
    // Anzeige Start-Screen
286
    //-------------------------
287
    PKT_TitlePKTlipo( false );                                      // Titel mit PKT-Version anzeigen (und clearscreen)
288
 
289
    yoffs = 11;
290
 
291
    if( PCB_SV2RxTxPatch )
292
    {
293
        lcdx_printp_center( 2, PSTR("SV2 Patch"), MNORMAL,  0,7);   // "SV2 Patch" (ehemals "Wifly Patch")
294
        yoffs = 5;
295
    }
296
 
297
    if( PCB_Version==PKT39m )
298
    {
299
        lcdx_printp_center( 1, PSTR("HW 3.9m (09.2011)"), MNORMAL,  0,yoffs);
300
    }
301
 
302
    if( PCB_Version==PKT39x )
303
    {
304
        lcdx_printp_center( 1, PSTR("HW 3.9x (11.2012)"), MNORMAL,  0,yoffs);
305
    }
306
 
307
    lcdx_printp_center( 5, strGet(BOOT1), MNORMAL,  0,2);           // "Taste 1 Sekunde"
308
    lcdx_printp_center( 6, strGet(BOOT2), MNORMAL,  0,4);           // "festhalten!"
309
    lcd_rect_round( 0, 5*8-2, 127, 2*8+8, 1, R2);                   // Rahmen
310
 
311
 
312
    //-------------------------
313
    //-------------------------
314
 
315
    #ifdef DEBUG_SV2_EXTENSION
316
        lcd_printpns_at (0, 7, PSTR("A:"),0);
317
             lcd_print_hex(PortA,0);
318
         lcd_printpns_at (5, 7, PSTR("B:"),0);
319
             lcd_print_hex(PortB,0);
320
         lcd_printpns_at (10, 7, PSTR("C:"),0);
321
             lcd_print_hex(PortC,0);
322
         lcd_printpns_at (15, 7, PSTR("D:"),0);
323
             lcd_print_hex(PortD,0);
324
    #endif
325
 
326
 
327
    //_delay_ms(800);
328
    _delay_ms(900);
329
 
330
    if (PINA & (1<<PINA7))    // Spannung eingeschaltet lassen
331
        clr_V_On();
332
 
333
 
334
    _delay_ms(100);
335
 
336
    set_beep ( 400, 0x0080, BeepNormal);
337
    get_key_press(KEY_ALL);
338
 
339
    #ifdef USE_SOUND
340
    if (Config.HWSound==1);
341
    {
342
        playTone(505,100,Config.Volume);
343
        playTone(515,100,Config.Volume);
344
        playTone(525,100,Config.Volume);
345
        playTone(535,100,Config.Volume);
346
        playTone(525,100,Config.Volume);
347
        playTone(515,100,Config.Volume);
348
        playTone(505,100,Config.Volume);
349
    }
350
    #endif
351
 
352
    //lcd_cls();  // 20.02.2014 OG
353
 
354
 
355
    //  servoSetPosition(1,0);
356
    //  _delay_ms(250);
357
    //        servoSetPosition(0,0);
358
    //        _delay_ms(250);
359
    //  servoSetPosition(1,400);
360
    //        _delay_ms(250);
361
    //        servoSetPosition(0,400);
362
    //        _delay_ms(250);
363
    //        servoSetPosition(1,0);
364
    //        _delay_ms(250);
365
    //        servoSetPosition(0,0);
366
    //        _delay_ms(250);
367
    //        servoSetPosition(1,400);
368
    //        _delay_ms(250);
369
    //        servoSetPosition(0,400);
370
 
371
 
372
    set_BTOff();
373
    //set_Modul_On(USB);
374
 
375
    if( (Config.UseWi == true) && (Config.WiIsSet == false) )
376
    {
377
        Wi232_Initalize();                          // wenn Wi232 nicht initialisiert ist, dann jetzt tun
378
    }
379
 
380
 
381
    #ifdef USE_BLUETOOTH
382
    if ((Config.UseBT == true) && (Config.BTIsSet == false))    // BT ausschalten
383
    {
384
        //lcd_cls();
385
        BTM222_Initalize();
386
        // set_USBOn();
387
    }
388
    bt_start();
389
    #endif  // end: #ifdef USE_BLUETOOTH
390
 
391
 
392
    if( (Config.UseWi == true) && (Config.U02SV2 == 0) )
393
    {
394
        Change_Output(Uart02Wi);    // Verbindung zu Wi232 herstellen
395
    }
396
    else
397
    {
398
        Change_Output(Uart02FC);    // Verbindung zu SV" (Kabel) herstellen
399
    }
400
 
401
}  // InitHWPorts()
402
 
403
 
404
 
405
//#define set_BEEP()              (PORTC &= ~(1 << Summer))               // Summer
406
//#define clr_BEEP()              (PORTC |=  (1 << Summer))
407
 
408
 
409
//--------------------------------------------------------------
410
//--------------------------------------------------------------
411
void set_BEEP(void)
412
{
413
  if (PCB_Version == PKT39m)
414
    PORTC &= ~(1 << Summer);               // Summer
415
  if (PCB_Version == PKT39x)
416
    PORTD &= ~(1 << SummerV2);               // Summer
417
}
418
 
419
 
420
 
421
//--------------------------------------------------------------
422
//--------------------------------------------------------------
423
void clr_BEEP(void)
424
{
425
  if (PCB_Version == PKT39m)
426
    PORTC |=  (1 << Summer);               // Summer
427
  if (PCB_Version == PKT39x)
428
    PORTD |=  (1 << SummerV2);               // Summer
429
}
430
 
431
 
432
 
433
//--------------------------------------------------------------
434
//--------------------------------------------------------------
435
void set_D_LIGHT(void)  /* Displaybeleuchtung ein*/
436
        {
437
        if (PCB_Version == PKT39m)
438
          {
439
//           if (Config.HWSound==0)
440
//              {
441
//              // PWM einschalten
442
//              TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);
443
//              TCCR2B |= (1 << CS20);
444
//              }
445
//            else
446
              clr_DISPLAYLIGHT();
447
            }
448
 
449
        if (PCB_Version == PKT39x)
450
          {
451
//            if (HWSound==0)
452
//              {
453
//              // PWM einschalten
454
//              TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);
455
//              TCCR2B |= (1 << CS20);
456
//              }
457
//            else
458
              clr_DISPLAYLIGHTV2();
459
            }
460
}
461
 
462
 
463
 
464
//--------------------------------------------------------------
465
//--------------------------------------------------------------
466
void clr_D_LIGHT(void)  /* Displaybeleuchtung aus*/
467
   {
468
        if (PCB_Version == PKT39m)
469
          {
470
//            if (Config.HWSound==0)
471
//              {
472
//              // PWM ausschalten
473
//              TCCR2A = 0;
474
//              TCCR2B = 0;
475
//              }
476
//            else
477
 
478
              set_DISPLAYLIGHT();
479
          }
480
        if (PCB_Version == PKT39x)
481
          {
482
//            if (HWSound==0)
483
//              {
484
//              // PWM ausschalten
485
//              TCCR2A = 0;
486
//              TCCR2B = 0;
487
//              }
488
//            else
489
              set_DISPLAYLIGHTV2();
490
          }
491
   }
492
uint8_t BTIsOn=0;
493
 
494
 
495
 
496
//--------------------------------------------------------------
497
//--------------------------------------------------------------
498
void set_Modul_On(uint8_t Modul)   // Umschaltfunktionen für Leiterplatte 3.9x mit WiFlypatch
499
{
500
 
501
  if (PCB_SV2RxTxPatch) PORTC &= ~((1<<BT_Reset)|(1<<WiFly_Reset)|(1<<USB_Reset));  // Erst alle Module ausschalten
502
 
503
  switch (Modul)
504
  {
505
   case USB:       if (PCB_SV2RxTxPatch) set_USBOn();
506
                     else set_USBOn();
507
                   BTIsOn = 0;
508
                   break;
509
 
510
   case Bluetooth: if (PCB_SV2RxTxPatch) set_BTM222On();
511
                     else _BTOn();
512
                   if (BTIsOn == 0)
513
                       {
514
                        BTIsOn = 1;
515
                        _delay_ms(3000);  // wait for BTM222 initialisation
516
                       }
517
                   break;
518
   case Wlan:     set_WiFlyOn();
519
                   BTIsOn = 0;
520
                   break;
521
  }
522
 
523
 
524
}
525
 
526
 
527
 
528
//--------------------------------------------------------------
529
//--------------------------------------------------------------
530
void set_BTOn(void)
531
{
532
  if (BTIsOn == 0)
533
    {
534
     if (PCB_SV2RxTxPatch) set_Modul_On(Bluetooth);
535
       else
536
         {
537
           _BTOn();
538
           BTIsOn = 1;
539
           _delay_ms(3000);
540
         }
541
    }
542
}
543
 
544
 
545
 
546
//--------------------------------------------------------------
547
//--------------------------------------------------------------
548
void set_BTOff(void)
549
{
550
    if( PCB_SV2RxTxPatch )
551
        set_Modul_On(USB);
552
    else
553
    {
554
        set_USBOn();
555
        BTIsOn = 0;
556
    }
557
}
558
 
559
 
560
 
561
#endif
562
//#endif // HAL_HW3_9_C_