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
//# 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"
135
#include "motortest/twimaster.h"
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
231
    I2C_Init(1);
232
 
233
    LCD_Init(0);                                                    // muss vor "ReadParameter" stehen
234
    ReadParameter();                                                // aktuelle Werte aus EEProm auslesen
235
 
236
    #ifdef USE_TRACKING
237
        servoInit(SERVO_PERIODE);
238
    #endif
239
 
240
    //servoSetDefaultPos();
241
    sei();
242
 
243
    Old_Baudrate = Config.PKT_Baudrate;
244
    SetBaudUart1( Config.PKT_Baudrate );
245
    SetBaudUart0( Config.PKT_Baudrate );
246
 
247
    // 10.11.2015 cebra
248
    // TODO cebra: diese Stelle muss bei HW_Sound überarbeitet werden
249
    //    if( (Config.HWSound==0) && (PCB_Version == PKT39m) )
250
    //    {
251
    //      Timer2_Init(); // Displaybeleuchtung oder Soundmodul
252
    //      OCR2A = Config.LCD_Helligkeit * 2.55;
253
    //    }
254
 
255
 
256
 
257
 
258
    #ifdef USE_SOUND
259
        if (Config.HWSound==1) InitSound();
260
    #endif
261
 
262
 
263
    LCD_Init(1);
264
    set_beep ( 400, 0x0080, BeepNormal);
265
 
266
    OSD_active = false;                                             // keine OSD Ausgabe
267
 
268
    ADC_Init();                                                     // ADC für Lipomessung// MartinR: verschoben
269
 
270
 
271
    //-------------------------
272
     // beim ersten Start (oder nach Reset) Sprache abfragen
273
    //-------------------------
274
    if( Config.DisplayLanguage > NUM_LANG )
275
    {
276
        Config.DisplayLanguage = 1;
277
      //Config.DisplayLanguage = Edit_generic( Config.DisplayLanguage,  0,  1, Language,1,   NULL,NULL);        // OG: ?? hierbei wird die falsche Sprache angezeigt - warum?
278
        Config.DisplayLanguage = Edit_generic( 2,  0,  1, Language,1,   NULL,NULL);
279
        WriteParameter();
280
    }
281
 
282
 
283
    //-------------------------
284
    // Anzeige Start-Screen
285
    //-------------------------
286
    PKT_TitlePKTlipo( false );                                      // Titel mit PKT-Version anzeigen (und clearscreen)
287
 
288
    yoffs = 11;
289
 
290
    if( PCB_SV2RxTxPatch )
291
    {
292
        lcdx_printp_center( 2, PSTR("SV2 Patch"), MNORMAL,  0,7);   // "SV2 Patch" (ehemals "Wifly Patch")
293
        yoffs = 5;
294
    }
295
 
296
    if( PCB_Version==PKT39m )
297
    {
298
        lcdx_printp_center( 1, PSTR("HW 3.9m (09.2011)"), MNORMAL,  0,yoffs);
299
    }
300
 
301
    if( PCB_Version==PKT39x )
302
    {
303
        lcdx_printp_center( 1, PSTR("HW 3.9x (11.2012)"), MNORMAL,  0,yoffs);
304
    }
305
 
306
    lcdx_printp_center( 5, strGet(BOOT1), MNORMAL,  0,2);           // "Taste 1 Sekunde"
307
    lcdx_printp_center( 6, strGet(BOOT2), MNORMAL,  0,4);           // "festhalten!"
308
    lcd_rect_round( 0, 5*8-2, 127, 2*8+8, 1, R2);                   // Rahmen
309
 
310
 
311
    //-------------------------
312
    //-------------------------
313
 
314
    #ifdef DEBUG_SV2_EXTENSION
315
        lcd_printpns_at (0, 7, PSTR("A:"),0);
316
             lcd_print_hex(PortA,0);
317
         lcd_printpns_at (5, 7, PSTR("B:"),0);
318
             lcd_print_hex(PortB,0);
319
         lcd_printpns_at (10, 7, PSTR("C:"),0);
320
             lcd_print_hex(PortC,0);
321
         lcd_printpns_at (15, 7, PSTR("D:"),0);
322
             lcd_print_hex(PortD,0);
323
    #endif
324
 
325
 
326
    //_delay_ms(800);
327
    _delay_ms(900);
328
 
329
    if (PINA & (1<<PINA7))    // Spannung eingeschaltet lassen
330
        clr_V_On();
331
 
332
 
333
    _delay_ms(100);
334
 
335
    set_beep ( 400, 0x0080, BeepNormal);
336
    get_key_press(KEY_ALL);
337
 
338
    #ifdef USE_SOUND
339
    if (Config.HWSound==1);
340
    {
341
        playTone(505,100,Config.Volume);
342
        playTone(515,100,Config.Volume);
343
        playTone(525,100,Config.Volume);
344
        playTone(535,100,Config.Volume);
345
        playTone(525,100,Config.Volume);
346
        playTone(515,100,Config.Volume);
347
        playTone(505,100,Config.Volume);
348
    }
349
    #endif
350
 
351
    //lcd_cls();  // 20.02.2014 OG
352
 
353
 
354
    //  servoSetPosition(1,0);
355
    //  _delay_ms(250);
356
    //        servoSetPosition(0,0);
357
    //        _delay_ms(250);
358
    //  servoSetPosition(1,400);
359
    //        _delay_ms(250);
360
    //        servoSetPosition(0,400);
361
    //        _delay_ms(250);
362
    //        servoSetPosition(1,0);
363
    //        _delay_ms(250);
364
    //        servoSetPosition(0,0);
365
    //        _delay_ms(250);
366
    //        servoSetPosition(1,400);
367
    //        _delay_ms(250);
368
    //        servoSetPosition(0,400);
369
 
370
 
371
    set_BTOff();
372
    //set_Modul_On(USB);
373
 
374
    if( (Config.UseWi == true) && (Config.WiIsSet == false) )
375
    {
376
        Wi232_Initalize();                          // wenn Wi232 nicht initialisiert ist, dann jetzt tun
377
    }
378
 
379
 
380
    #ifdef USE_BLUETOOTH
381
    if ((Config.UseBT == true) && (Config.BTIsSet == false))    // BT ausschalten
382
    {
383
        //lcd_cls();
384
        BTM222_Initalize();
385
        // set_USBOn();
386
    }
387
    bt_start();
388
    #endif  // end: #ifdef USE_BLUETOOTH
389
 
390
 
391
    if( (Config.UseWi == true) && (Config.U02SV2 == 0) )
392
    {
393
        Change_Output(Uart02Wi);    // Verbindung zu Wi232 herstellen
394
    }
395
    else
396
    {
397
        Change_Output(Uart02FC);    // Verbindung zu SV" (Kabel) herstellen
398
    }
399
 
400
}  // InitHWPorts()
401
 
402
 
403
 
404
//#define set_BEEP()              (PORTC &= ~(1 << Summer))               // Summer
405
//#define clr_BEEP()              (PORTC |=  (1 << Summer))
406
 
407
 
408
//--------------------------------------------------------------
409
//--------------------------------------------------------------
410
void set_BEEP(void)
411
{
412
  if (PCB_Version == PKT39m)
413
    PORTC &= ~(1 << Summer);               // Summer
414
  if (PCB_Version == PKT39x)
415
    PORTD &= ~(1 << SummerV2);               // Summer
416
}
417
 
418
 
419
 
420
//--------------------------------------------------------------
421
//--------------------------------------------------------------
422
void clr_BEEP(void)
423
{
424
  if (PCB_Version == PKT39m)
425
    PORTC |=  (1 << Summer);               // Summer
426
  if (PCB_Version == PKT39x)
427
    PORTD |=  (1 << SummerV2);               // Summer
428
}
429
 
430
 
431
 
432
//--------------------------------------------------------------
433
//--------------------------------------------------------------
434
void set_D_LIGHT(void)  /* Displaybeleuchtung ein*/
435
        {
436
        if (PCB_Version == PKT39m)
437
          {
438
//           if (Config.HWSound==0)
439
//              {
440
//              // PWM einschalten
441
//              TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);
442
//              TCCR2B |= (1 << CS20);
443
//              }
444
//            else
445
              clr_DISPLAYLIGHT();
446
            }
447
 
448
        if (PCB_Version == PKT39x)
449
          {
450
//            if (HWSound==0)
451
//              {
452
//              // PWM einschalten
453
//              TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);
454
//              TCCR2B |= (1 << CS20);
455
//              }
456
//            else
457
              clr_DISPLAYLIGHTV2();
458
            }
459
}
460
 
461
 
462
 
463
//--------------------------------------------------------------
464
//--------------------------------------------------------------
465
void clr_D_LIGHT(void)  /* Displaybeleuchtung aus*/
466
   {
467
        if (PCB_Version == PKT39m)
468
          {
469
//            if (Config.HWSound==0)
470
//              {
471
//              // PWM ausschalten
472
//              TCCR2A = 0;
473
//              TCCR2B = 0;
474
//              }
475
//            else
476
 
477
              set_DISPLAYLIGHT();
478
          }
479
        if (PCB_Version == PKT39x)
480
          {
481
//            if (HWSound==0)
482
//              {
483
//              // PWM ausschalten
484
//              TCCR2A = 0;
485
//              TCCR2B = 0;
486
//              }
487
//            else
488
              set_DISPLAYLIGHTV2();
489
          }
490
   }
491
uint8_t BTIsOn=0;
492
 
493
 
494
 
495
//--------------------------------------------------------------
496
//--------------------------------------------------------------
497
void set_Modul_On(uint8_t Modul)   // Umschaltfunktionen für Leiterplatte 3.9x mit WiFlypatch
498
{
499
 
500
  if (PCB_SV2RxTxPatch) PORTC &= ~((1<<BT_Reset)|(1<<WiFly_Reset)|(1<<USB_Reset));  // Erst alle Module ausschalten
501
 
502
  switch (Modul)
503
  {
504
   case USB:       if (PCB_SV2RxTxPatch) set_USBOn();
505
                     else set_USBOn();
506
                   BTIsOn = 0;
507
                   break;
508
 
509
   case Bluetooth: if (PCB_SV2RxTxPatch) set_BTM222On();
510
                     else _BTOn();
511
                   if (BTIsOn == 0)
512
                       {
513
                        BTIsOn = 1;
514
                        _delay_ms(3000);  // wait for BTM222 initialisation
515
                       }
516
                   break;
517
   case Wlan:     set_WiFlyOn();
518
                   BTIsOn = 0;
519
                   break;
520
  }
521
 
522
 
523
}
524
 
525
 
526
 
527
//--------------------------------------------------------------
528
//--------------------------------------------------------------
529
void set_BTOn(void)
530
{
531
  if (BTIsOn == 0)
532
    {
533
     if (PCB_SV2RxTxPatch) set_Modul_On(Bluetooth);
534
       else
535
         {
536
           _BTOn();
537
           BTIsOn = 1;
538
           _delay_ms(3000);
539
         }
540
    }
541
}
542
 
543
 
544
 
545
//--------------------------------------------------------------
546
//--------------------------------------------------------------
547
void set_BTOff(void)
548
{
549
    if( PCB_SV2RxTxPatch )
550
        set_Modul_On(USB);
551
    else
552
    {
553
        set_USBOn();
554
        BTIsOn = 0;
555
    }
556
}
557
 
558
 
559
 
560
#endif
561
//#endif // HAL_HW3_9_C_