Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1932 - 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
#ifndef HAL_HW3_9_C_
36
#define HAL_HW3_9_C_
37
 
38
#include "cpu.h"
39
#include <inttypes.h>
40
#include <avr/io.h>
41
#include <avr/interrupt.h>
42
#include <avr/pgmspace.h>
43
#include <avr/eeprom.h>
44
#include <util/delay.h>
45
#include <stdbool.h>
46
#include <stdlib.h>
47
 
48
#include "main.h"
49
 
50
#if defined HWVERSION3_9
51
#include "eeprom/eeprom.h"
52
#include "messages.h"
53
#include "lcd/lcd.h"
54
#include "uart/usart.h"
55
#include "uart/uart1.h"
56
#include "display.h"
57
#include "timer/timer.h"
58
 
59
#include "wi232/Wi232.h"
60
#include "motortest/twimaster.h"
61
#include "bluetooth/bluetooth.h"
62
#include "bluetooth/error.h"
63
#include "connect.h"
64
#include "lipo/lipo.h"
65
#include "setup/setup.h"
66
#include "osd/osd.h"
67
#include "sound/pwmsine8bit.h"
68
#include "tracking/ng_servo.h"
69
 
70
 
71
 
72
volatile uint8_t USBBT;
73
volatile uint8_t U02SV2;
74
volatile uint8_t PCB_Version;
75
 
76
uint8_t PortA;
77
uint8_t PortB;
78
uint8_t PortC;
79
uint8_t PortD;
80
 
81
void CheckPCB(void)
82
{
83
 
84
 
85
  DDRA = 0xFF;
86
  DDRB = 0x00;
87
  DDRC = 0x00;
88
  DDRD = 0x00;
89
  PortA = PINA;
90
  PortB = PINB;
91
  PortC = PINC;
92
  PortD = PIND;
93
 
94
  if (PINC & (1<<PINC7) ) PCB_Version = PKT39m; else PCB_Version = PKT39x;
95
 
96
}
97
 
98
 
99
//--------------------------------------------------------------
100
void InitHWPorts(void)  // Initialisierung der Hardware fÃŒr die jeweilige Leiterplattenversion
101
 
102
{
103
        CheckPCB();                                                     // Version der Leiterplatte prüfen
104
        PORTA |= (1<<PORTA4)|(1<<PORTA5)|(1<<PORTA6)|(1<<PORTA7);       // Enable Pull Up for the 4 keys
105
        DDRA &= ~(1<<DDA4);                                             // Eingang: A4 auf Low setzen (Power On)
106
        //DDRA &= ~(1<<DDA1);                                             // Eingang: PKT Lipo Messung // MartinR so war es
107
        if (Config.Lipomessung == true)
108
              {
109
              DDRA &= ~(1<<DDA1);       // Eingang: PKT Lipo Messung
110
              PORTA &= ~(1<<PORTA1);
111
              }
112
 
113
        DDRB = 0xFF;                                                    // Alles AusgÀnge
114
 
115
        PORTC |= (1<<PORTC4)|(1<<PORTC7);                               // Enable Pull Up for LBO + Summer
116
        DDRC |= (1<<DDC2)|(1<<DDC3)|(1<<DDC5)|(1<<DDC6|(1<<DDC7));      // Ausgang: Led2,Rs232Switch,Summer
117
        DDRC &= ~(1<<DDC4);                                             // Eingang: LowBat LTC1308
118
        _BTOn();                                                     // Erstmal USB dektivieren, damit beim versehentlichen Einschalten USB im PC ruhig bleibt
119
 
120
        PORTD |= (1<<PORTD6);                                           // Wi232-CMD auf High schalten
121
        DDRD |= (1<<DDD4)|(1<<DDD5)|(1<<DDD6)|(1<<DDD7);                // Ausgang: PiepserTest, Servo, Wi232-CMD und Beleuchtung
122
        set_V_On();                     // Spannung mit T3 halten
123
        Timer0_Init ();         // system
124
 
125
        Display_on = 1;
126
        USART_Init (UART_BAUD_SELECT(USART_BAUD,F_CPU));
127
        uart1_init (UART_BAUD_SELECT(USART_BAUD,F_CPU));        // USB
128
        I2C_Init(1);
129
 
130
        LCD_Init (0);           // muss vor "ReadParameter" stehen
131
        ReadParameter ();       // Aktuelle Werte aus EEProm auslesen
132
        servoInit(SERVO_PERIODE);
133
//        servoSetDefaultPos();
134
        sei ();
135
 
136
 
137
 
138
 
139
 
140
        Old_Baudrate = Config.PKT_Baudrate;
141
        SetBaudUart1(Config.PKT_Baudrate);
142
        SetBaudUart0(Config.PKT_Baudrate);
143
        if (Config.DisplayLanguage > NUM_LANG)    // Beim ersten Start Sprache abfragen
144
          { Config.DisplayLanguage = 1;
145
          Config.DisplayLanguage = Edit_generic(Config.DisplayLanguage,0,3,DISPLAY3,Language);
146
//            WriteParameter();
147
          }
148
 
149
        if ((Config.HWSound==0)&&(PCB_Version == PKT39m))
150
          {
151
            Timer2_Init(); // Displaybeleuchtung
152
            OCR2A = Config.LCD_Helligkeit * 2.55;
153
          }
154
        if (Config.HWSound==1) InitSound();
155
        LCD_Init (1);
156
        set_beep ( 400, 0x0080, BeepNormal);
157
 
158
        OSD_active = false;     //keine OSD Ausgabe
159
        ADC_Init();             // ADC für Lipomessung// MartinR: verschoben
160
 
161
         if (PCB_Version==PKT39m)
162
           {
163
           lcd_printp_at (2, 0, PSTR("Hardware PKT 3.9m"),0);
164
           lcd_printp_at (7, 1, PSTR("09.2011"),0);
165
           }
166
         if (PCB_Version==PKT39x)
167
           {
168
           lcd_printp_at (2, 0, PSTR("Hardware PKT 3.9x"),0);
169
           lcd_printp_at (7, 1, PSTR("11.2012"),0);
170
           }
171
         lcd_printp_at (0, 3, PSTR("Software:"),0);
172
//         lcd_printpj_at (8, 3, PSTR(PKTSWVersion),0);
173
         lcd_printp_at (8, 3, PSTR(PKTSWVersion), MBIG);
174
 
175
 
176
         lcd_puts_at(0, 5, strGet(BOOT1), 0);
177
         lcd_puts_at(0, 6, strGet(BOOT2), 0);
178
 
179
        _delay_ms(800);
180
 
181
        if (PINA & (1<<PINA7))    // Spannung eingeschaltet lassen
182
                clr_V_On();
183
 
184
        _delay_ms(100);
185
 
186
        set_beep ( 400, 0x0080, BeepNormal);
187
        get_key_press(KEY_ALL);
188
        if (Config.HWSound==1);
189
        {
190
        playTone(505,100,Config.Volume);
191
        playTone(515,100,Config.Volume);
192
        playTone(525,100,Config.Volume);
193
        playTone(535,100,Config.Volume);
194
        playTone(525,100,Config.Volume);
195
        playTone(515,100,Config.Volume);
196
        playTone(505,100,Config.Volume);
197
        }
198
        lcd_cls();
199
 
200
 
201
//      servoSetPosition(1,0);
202
//      _delay_ms(250);
203
//        servoSetPosition(0,0);
204
//        _delay_ms(250);
205
//      servoSetPosition(1,400);
206
//        _delay_ms(250);
207
//        servoSetPosition(0,400);
208
//        _delay_ms(250);
209
//        servoSetPosition(1,0);
210
//        _delay_ms(250);
211
//        servoSetPosition(0,0);
212
//        _delay_ms(250);
213
//        servoSetPosition(1,400);
214
//        _delay_ms(250);
215
//        servoSetPosition(0,400);
216
 
217
        set_BTOff();
218
        if ((Config.UseWi == true) && (Config.WiIsSet == false))
219
        {
220
                InitWi232(Config.PKT_Baudrate);    // wenn Wi232 nicht initialisiert ist, dann jetzt tun
221
        }
222
 
223
        lcd_cls();
224
                                           // BT ausschalten
225
        if ((Config.UseBT == true) && (Config.BTIsSet == false))
226
        {
227
                bt_init();
228
//              set_USBOn();
229
        }
230
        bt_start();
231
        lcd_cls();
232
 
233
        if ((Config.UseWi == true) && (U02SV2 == 0))
234
        {
235
                Change_Output(Uart02Wi);    // Verbindung zu Wi232 herstellen
236
                if (Config.PKT_StartInfo == true)
237
                {
238
                         lcd_puts_at(0, 0, strGet(BOOT_WI1), 0);
239
                         lcd_puts_at(0, 1, strGet(BOOT_WI2), 0);
240
                        _delay_ms(2000);
241
                }
242
        }
243
        else
244
        {
245
                Change_Output(Uart02FC);    // Verbindung zu SV" (Kabel) herstellen
246
                if (Config.PKT_StartInfo == true)
247
                {
248
                         lcd_puts_at(0, 0, strGet(BOOT_WI1), 0);
249
                         lcd_puts_at(0, 1, strGet(BOOT_SV), 0);
250
                        _delay_ms(2000);
251
                }
252
        }
253
 
254
        lcd_cls();
255
 
256
}
257
 
258
//#define set_BEEP()              (PORTC &= ~(1 << Summer))               // Summer
259
//#define clr_BEEP()              (PORTC |=  (1 << Summer))
260
 
261
void set_BEEP(void)
262
{
263
  if (PCB_Version == PKT39m)
264
    PORTC &= ~(1 << Summer);               // Summer
265
  if (PCB_Version == PKT39x)
266
    PORTD &= ~(1 << SummerV2);               // Summer
267
}
268
 
269
void clr_BEEP(void)
270
{
271
  if (PCB_Version == PKT39m)
272
    PORTC |=  (1 << Summer);               // Summer
273
  if (PCB_Version == PKT39x)
274
    PORTD |=  (1 << SummerV2);               // Summer
275
}
276
 
277
void set_D_LIGHT(void)  /* Displaybeleuchtung ein*/
278
        {
279
        if (PCB_Version == PKT39m)
280
          {
281
           if (Config.HWSound==0)
282
              {
283
              // PWM einschalten
284
              TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);
285
              TCCR2B |= (1 << CS20);
286
              }
287
            else
288
              clr_DISPLAYLIGHT();
289
            }
290
 
291
        if (PCB_Version == PKT39x)
292
          {
293
//            if (HWSound==0)
294
//              {
295
//              // PWM einschalten
296
//              TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);
297
//              TCCR2B |= (1 << CS20);
298
//              }
299
//            else
300
              clr_DISPLAYLIGHTV2();
301
            }
302
}
303
 
304
void clr_D_LIGHT(void)  /* Displaybeleuchtung aus*/
305
   {
306
        if (PCB_Version == PKT39m)
307
          {
308
            if (Config.HWSound==0)
309
              {
310
              // PWM ausschalten
311
              TCCR2A = 0;
312
              TCCR2B = 0;
313
              }
314
            else
315
              set_DISPLAYLIGHT();
316
          }
317
        if (PCB_Version == PKT39x)
318
          {
319
//            if (HWSound==0)
320
//              {
321
//              // PWM ausschalten
322
//              TCCR2A = 0;
323
//              TCCR2B = 0;
324
//              }
325
//            else
326
              set_DISPLAYLIGHTV2();
327
          }
328
   }
329
 
330
 
331
 
332
 
333
uint8_t BTIsOn=0;
334
 
335
void set_BTOn(void)
336
{
337
  if (BTIsOn == 0)
338
    {
339
      _BTOn();
340
      BTIsOn = 1;
341
      _delay_ms(2000);
342
    }
343
}
344
void set_BTOff(void)
345
{
346
  set_USBOn();
347
  BTIsOn = 0;
348
}
349
 
350
 
351
 
352
#endif
353
#endif // HAL_HW3_9_C_