Subversion Repositories Projects

Rev

Rev 736 | Rev 761 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 736 Rev 738
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009-2010 by Claas Anders "CaScAdE" Rathje               *
2
 *   Copyright (C) 2009-2010 by Claas Anders "CaScAdE" Rathje               *
3
 *   admiralcascade@gmail.com                                               *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
5
 *                                                                          *
6
 *   This program is free software; you can redistribute it and/or modify   *
6
 *   This program is free software; you can redistribute it and/or modify   *
7
 *   it under the terms of the GNU General Public License as published by   *
7
 *   it under the terms of the GNU General Public License as published by   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 *                                                                          *
19
 *                                                                          *
20
 *                                                                          *
20
 *                                                                          *
21
 *   Credits to:                                                            *
21
 *   Credits to:                                                            *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
24
 *   Klaus "akku" Buettner for the hardware                                 *
24
 *   Klaus "akku" Buettner for the hardware                                 *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
26
 ****************************************************************************/
26
 ****************************************************************************/
27
 
27
 
28
#include <avr/io.h>
28
#include <avr/io.h>
29
#include <avr/interrupt.h>
29
#include <avr/interrupt.h>
30
#include <util/delay.h>
30
#include <util/delay.h>
31
#include <avr/pgmspace.h>
31
#include <avr/pgmspace.h>
32
#include "main.h"
32
#include "main.h"
33
#include "max7456_software_spi.h"
33
#include "max7456_software_spi.h"
34
#include "usart1.h"
34
#include "usart1.h"
35
#include "osd_helpers.h"
35
#include "osd_helpers.h"
36
#include "config.h"
36
#include "config.h"
37
#include "spi.h"
37
#include "spi.h"
38
#include "buttons.h"
38
#include "buttons.h"
39
#include "ppm.h"
39
#include "ppm.h"
40
#include "osd_ncmode_default.h"
40
#include "osd_ncmode_default.h"
41
#include "osd_ncmode_minimal.h"
41
#include "osd_ncmode_minimal.h"
42
#include "osd_fcmode_default.h"
42
#include "osd_fcmode_default.h"
43
#include "osd_fcmode_jopl.h"
43
#include "osd_fcmode_jopl.h"
44
 
44
 
45
#if WRITECHARS != -1
45
#if WRITECHARS != -1
46
#include "characters.h"
46
#include "characters.h"
47
#endif
47
#endif
48
 
48
 
49
/* TODO:
49
/* TODO:
50
 * - verifiy correctness of values
50
 * - verifiy correctness of values
51
 * - clean up code :)
51
 * - clean up code :)
52
 */
52
 */
53
 
53
 
54
 
54
 
55
/* ##########################################################################
55
/* ##########################################################################
56
 * global definitions and global vars
56
 * global definitions and global vars
57
 * ##########################################################################*/
57
 * ##########################################################################*/
58
 
58
 
59
volatile NaviData_t naviData;
59
volatile NaviData_t naviData;
60
volatile DebugOut_t debugData;
60
volatile DebugOut_t debugData;
61
 
61
 
62
// cache old vars for blinking attribute, checkup is faster than full
62
// cache old vars for blinking attribute, checkup is faster than full
63
// attribute write each time
63
// attribute write each time
64
volatile uint8_t last_UBat = 255;
64
volatile uint8_t last_UBat = 255;
65
volatile uint8_t last_RC_Quality = 255;
65
volatile uint8_t last_RC_Quality = 255;
66
 
66
 
67
// 16bit should be enough, normal LiPos don't last that long
67
// 16bit should be enough, normal LiPos don't last that long
68
volatile uint16_t uptime = 0;
68
volatile uint16_t uptime = 0;
69
volatile uint16_t timer = 0;
69
volatile uint16_t timer = 0;
70
volatile uint16_t flytime_fc = 0;
70
volatile uint16_t flytime_fc = 0;
71
 
71
 
72
// remember last time data was received
72
// remember last time data was received
73
volatile uint8_t seconds_since_last_data = 0;
73
volatile uint8_t seconds_since_last_data = 0;
74
 
74
 
75
// general PAL|NTSC distingiusch stuff
75
// general PAL|NTSC distingiusch stuff
76
uint8_t top_line = 1;
76
uint8_t top_line = 1;
77
uint8_t bottom_line = 14;
77
uint8_t bottom_line = 14;
78
 
78
 
79
// battery voltages
79
// battery voltages
80
uint8_t min_voltage = 0;
80
uint8_t min_voltage = 0;
81
uint8_t max_voltage = 0;
81
uint8_t max_voltage = 0;
82
 
82
 
83
// Flags
83
// Flags
84
uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0;
84
uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0;
85
 
85
 
86
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
86
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
87
 
87
 
88
// stats for after flight
88
// stats for after flight
89
int16_t max_Altimeter = 0;
89
int16_t max_Altimeter = 0;
90
uint8_t min_UBat = 255;
90
uint8_t min_UBat = 255;
91
uint16_t max_GroundSpeed = 0;
91
uint16_t max_GroundSpeed = 0;
92
int16_t max_Distance = 0;
92
int16_t max_Distance = 0;
93
uint16_t max_FlyingTime = 0;
93
uint16_t max_FlyingTime = 0;
94
 
94
 
95
// flags from last round to check for changes
95
// flags from last round to check for changes
96
uint8_t old_MKFlags = 0;
96
uint8_t old_MKFlags = 0;
97
 
97
 
98
// store stats description in progmem to save space
98
// store stats description in progmem to save space
99
const char stats_item_0[] PROGMEM = "max Altitude:";
99
const char stats_item_0[] PROGMEM = "max Altitude:";
100
const char stats_item_1[] PROGMEM = "max Speed   :";
100
const char stats_item_1[] PROGMEM = "max Speed   :";
101
const char stats_item_2[] PROGMEM = "max Distance:";
101
const char stats_item_2[] PROGMEM = "max Distance:";
102
const char stats_item_3[] PROGMEM = "min Voltage :";
102
const char stats_item_3[] PROGMEM = "min Voltage :";
103
const char stats_item_4[] PROGMEM = "max Time    :";
103
const char stats_item_4[] PROGMEM = "max Time    :";
104
const char stats_item_5[] PROGMEM = "longitude   :";
104
const char stats_item_5[] PROGMEM = "longitude   :";
105
const char stats_item_6[] PROGMEM = "latitude    :";
105
const char stats_item_6[] PROGMEM = "latitude    :";
106
const char stats_item_7[] PROGMEM = "max current :";
106
const char stats_item_7[] PROGMEM = "max current :";
107
const char *stats_item_pointers[8] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
107
const char *stats_item_pointers[8] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
108
    stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
108
    stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
109
 
109
 
110
//char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
110
//char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
111
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
111
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
112
const char str_NE[] PROGMEM = "NE";
112
const char str_NE[] PROGMEM = "NE";
113
const char str_E[] PROGMEM  = "E ";
113
const char str_E[] PROGMEM  = "E ";
114
const char str_SE[] PROGMEM = "SE";
114
const char str_SE[] PROGMEM = "SE";
115
const char str_S[] PROGMEM  = "S ";
115
const char str_S[] PROGMEM  = "S ";
116
const char str_SW[] PROGMEM = "SW";
116
const char str_SW[] PROGMEM = "SW";
117
const char str_W[] PROGMEM  = "W ";
117
const char str_W[] PROGMEM  = "W ";
118
const char str_NW[] PROGMEM = "NW";
118
const char str_NW[] PROGMEM = "NW";
119
const char str_N[] PROGMEM  = "N ";
119
const char str_N[] PROGMEM  = "N ";
120
const char *directions[8] PROGMEM = {
120
const char *directions[8] PROGMEM = {
121
        str_NE,
121
        str_NE,
122
        str_E,
122
        str_E,
123
        str_SE,
123
        str_SE,
124
        str_S,
124
        str_S,
125
        str_SW,
125
        str_SW,
126
        str_W,
126
        str_W,
127
        str_NW,
127
        str_NW,
128
        str_N};
128
        str_N};
129
 
129
 
130
/* ##########################################################################
130
/* ##########################################################################
131
 * Different display mode function pointers
131
 * Different display mode function pointers
132
 * ##########################################################################*/
132
 * ##########################################################################*/
133
const char str_1[] PROGMEM  = "default";
133
const char str_1[] PROGMEM  = "default";
134
const char str_2[] PROGMEM  = "minimal";
134
const char str_2[] PROGMEM  = "minimal";
135
const char str_3[] PROGMEM  = "   jopl";
135
const char str_3[] PROGMEM  = "   jopl";
136
 
136
 
137
const displaymode_t ncdisplaymodes[] PROGMEM = {
137
const displaymode_t ncdisplaymodes[] PROGMEM = {
138
   { osd_ncmode_default, (char *)str_1 },
138
   { osd_ncmode_default, (char *)str_1 },
139
   { osd_ncmode_minimal, (char *)str_2 }
139
   { osd_ncmode_minimal, (char *)str_2 }
140
};
140
};
141
 
141
 
142
const displaymode_t fcdisplaymodes[] PROGMEM = {
142
const displaymode_t fcdisplaymodes[] PROGMEM = {
143
   { osd_fcmode_default, (char *)str_1 },
143
   { osd_fcmode_default, (char *)str_1 },
144
   { osd_fcmode_jopl, (char *)str_3 }
144
   { osd_fcmode_jopl, (char *)str_3 }
145
};
145
};
146
 
146
 
147
int (*osd_ncmode)(void) = (int(*)(void)) &osd_ncmode_default;
147
int (*osd_ncmode)(void) = (int(*)(void)) &osd_ncmode_default;
148
int (*osd_fcmode)(void) = (int(*)(void)) &osd_fcmode_default;
148
int (*osd_fcmode)(void) = (int(*)(void)) &osd_fcmode_default;
149
#endif
149
#endif
150
 
150
 
151
/* ##########################################################################
151
/* ##########################################################################
152
 * Interrupt handler
152
 * Interrupt handler
153
 * ##########################################################################*/
153
 * ##########################################################################*/
154
 
154
 
155
/**
155
/**
156
 * handler for undefined Interrupts
156
 * handler for undefined Interrupts
157
 * if not defined AVR will reset in case any unhandled interrupts occur
157
 * if not defined AVR will reset in case any unhandled interrupts occur
158
 */
158
 */
159
ISR(__vector_default) {
159
ISR(__vector_default) {
160
    asm("nop");
160
    asm("nop");
161
}
161
}
162
 
162
 
163
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
163
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
164
/* ##########################################################################
164
/* ##########################################################################
165
 * timer stuff
165
 * timer stuff
166
 * ##########################################################################*/
166
 * ##########################################################################*/
167
 
167
 
168
static uint8_t delay_spi = 0;
168
static uint8_t delay_spi = 0;
169
 
169
 
170
/**
170
/**
171
 * timer kicks in every 1000uS ^= 1ms
171
 * timer kicks in every 1000uS ^= 1ms
172
 */
172
 */
173
ISR(TIMER0_COMP_vect) {
173
ISR(TIMER0_COMP_vect) {
174
    if (!timer--) {
174
    if (!timer--) {
175
        uptime++;
175
        uptime++;
176
 
176
 
177
                #if FCONLY
177
                #if FCONLY
178
                if (debugData.Analog[12]>10) {
178
                if (debugData.Analog[12]>10) {
179
                        flytime_fc++;
179
                        flytime_fc++;
180
                }
180
                }
181
                #endif
181
                #endif
182
 
182
 
183
        timer = 999;
183
        timer = 999;
184
        seconds_since_last_data++;
184
        seconds_since_last_data++;
185
    }
185
    }
186
    // in case there is still some spi data to send do it now
186
    // in case there is still some spi data to send do it now
187
        // delay to give the slave some time to compute values
187
        // delay to give the slave some time to compute values
188
    if (spi_ready && icnt) {
188
    if (spi_ready && icnt) {
189
                if (!delay_spi--) {
189
                if (!delay_spi--) {
190
                        delay_spi = 8;
190
                        delay_spi = 8;
191
                        spi_send_next();
191
                        spi_send_next();
192
                }
192
                }
193
    }
193
    }
194
}
194
}
195
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
195
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
196
 
196
 
197
/* ##########################################################################
197
/* ##########################################################################
198
 * MAIN
198
 * MAIN
199
 * ##########################################################################*/
199
 * ##########################################################################*/
200
int main(void) {
200
int main(void) {
201
        // set up FLAGS
201
        // set up FLAGS
202
        COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0;
202
        COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0;
203
        #if NTSC
203
        #if NTSC
204
        COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC;
204
        COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC;
205
        #endif
205
        #endif
206
        #if HUD
206
        #if HUD
207
        COSD_FLAGS_MODES |= COSD_FLAG_HUD;
207
        COSD_FLAGS_MODES |= COSD_FLAG_HUD;
208
        #endif
208
        #endif
209
        #if ARTHORIZON
209
        #if ARTHORIZON
210
        COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON;
210
        COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON;
211
        #endif
211
        #endif
212
        #if BIGVARIO
212
        #if BIGVARIO
213
        COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO;
213
        COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO;
214
        #endif
214
        #endif
215
        #if STATS
215
        #if STATS
216
        COSD_FLAGS_MODES |= COSD_FLAG_STATS;
216
        COSD_FLAGS_MODES |= COSD_FLAG_STATS;
217
        #endif
217
        #endif
218
        #if WARNINGS
218
        #if WARNINGS
219
        COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS;
219
        COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS;
220
        #endif
220
        #endif
221
        #if FCONLY
221
        #if FCONLY
222
        COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE;
222
        COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE;
223
        #endif
223
        #endif
224
 
224
 
225
 
-
 
226
 
225
 
227
    // set up Atmega162 Ports
226
    // set up Atmega162 Ports
228
    DDRA |= (1 << PA1); // PA1 output (/CS)
227
    DDRA |= (1 << PA1); // PA1 output (/CS)
229
    MAX_CS_HIGH
228
    MAX_CS_HIGH
230
    DDRA |= (1 << PA2); // PA2 output (SDIN)
229
    DDRA |= (1 << PA2); // PA2 output (SDIN)
231
    MAX_SDIN_LOW
230
    MAX_SDIN_LOW
232
    DDRA |= (1 << PA3); // PA3 output (SCLK)
231
    DDRA |= (1 << PA3); // PA3 output (SCLK)
233
    MAX_SCLK_LOW
232
    MAX_SCLK_LOW
234
    DDRA |= (1 << PA5); // PA5 output (RESET)
233
    DDRA |= (1 << PA5); // PA5 output (RESET)
235
    MAX_RESET_HIGH
234
    MAX_RESET_HIGH
236
 
235
 
237
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
236
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
238
    LED1_OFF
237
    LED1_OFF
239
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
238
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
240
    LED2_OFF
239
    LED2_OFF
241
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
240
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
242
    LED3_OFF
241
    LED3_OFF
243
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
242
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
244
    LED4_OFF
243
    LED4_OFF
245
 
244
 
246
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
245
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
247
    PORTC |= (1 << PC4); // pullup
246
    PORTC |= (1 << PC4); // pullup
248
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
247
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
249
    PORTC |= (1 << PC5); // pullup
248
    PORTC |= (1 << PC5); // pullup
250
 
249
 
251
    // reset the MAX7456 to be sure any undefined states do no harm
250
    // reset the MAX7456 to be sure any undefined states do no harm
252
    MAX_RESET_LOW
251
    MAX_RESET_LOW
253
    MAX_RESET_HIGH
252
    MAX_RESET_HIGH
254
 
253
 
255
    // give the FC/NC and the maxim time to come up
254
    // give the FC/NC and the maxim time to come up
256
    LED4_ON
255
    LED4_ON
257
    _delay_ms(1000);
256
    _delay_ms(1000);
258
    LED4_OFF
257
    LED4_OFF
259
 
258
 
260
    //Pushing NEW chars to the MAX7456
259
    //Pushing NEW chars to the MAX7456
261
#if (WRITECHARS != -1)
260
#if (WRITECHARS != -1)
262
        // DISABLE display (VM0)
261
        // DISABLE display (VM0)
263
        spi_send_byte(0x00, 0b00000000);
262
        spi_send_byte(0x00, 0b00000000);
264
        learn_all_chars_pgm();
263
        learn_all_chars_pgm();
265
#else
264
#else
266
        // read out config for NTSC/PAL distinguishing
265
        // read out config for NTSC/PAL distinguishing
267
        get_eeprom(0);
266
        get_eeprom(0);
268
#endif
267
#endif
269
 
268
 
270
        // Setup Video Mode
269
        // Setup Video Mode
271
        if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
270
        if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
272
            // NTSC + enable display immediately (VM0)
271
            // NTSC + enable display immediately (VM0)
273
            spi_send_byte(0x00, 0b00001000);
272
            spi_send_byte(0x00, 0b00001000);
274
 
273
 
275
                bottom_line = 12;
274
                bottom_line = 12;
276
        } else {
275
        } else {
277
            // PAL + enable display immediately (VM0)
276
            // PAL + enable display immediately (VM0)
278
            spi_send_byte(0x00, 0b01001000);
277
            spi_send_byte(0x00, 0b01001000);
279
 
278
 
280
                bottom_line = 14;
279
                bottom_line = 14;
281
        }
280
        }
282
 
281
 
283
    /*// clear all display-mem (DMM)
282
    /*// clear all display-mem (DMM)
284
    spi_send_byte(0x04, 0b00000100);
283
    spi_send_byte(0x04, 0b00000100);
285
 
284
 
286
    // clearing takes 12uS according to maxim so lets wait longer
285
    // clearing takes 12uS according to maxim so lets wait longer
287
    _delay_us(120);
286
    _delay_us(120);
288
 
287
 
289
    // 8bit mode
288
    // 8bit mode
290
    spi_send_byte(0x04, 0b01000000);*/
289
    spi_send_byte(0x04, 0b01000000);*/
291
 
290
 
292
    // clear display memory and set to 8bit mode
291
    // clear display memory and set to 8bit mode
293
    clear();
292
    clear();
294
 
293
 
295
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
294
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
296
    // init usart
295
    // init usart
297
    usart1_init();
296
    usart1_init();
-
 
297
 
-
 
298
    // keep serial port clean
-
 
299
    usart1_DisableTXD();
298
 
300
 
299
    // set up timer
301
    // set up timer
300
        // CTC, Prescaler /64
302
        // CTC, Prescaler /64
301
        TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
303
        TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
302
 
304
 
303
        TCNT0 = 0;
305
        TCNT0 = 0;
304
        OCR0 = 250;
306
        OCR0 = 250;
305
 
307
 
306
        // enable timer output compare interrupt
308
        // enable timer output compare interrupt
307
        TIMSK &= ~(1 << TOIE0);
309
        TIMSK &= ~(1 << TOIE0);
308
        TIMSK |= (1 << OCIE0);
310
        TIMSK |= (1 << OCIE0);
309
 
311
 
310
        // SPI setup
312
        // SPI setup
311
    DDRD |= (1 << PD2); // PD2 output (INT0)
313
    DDRD |= (1 << PD2); // PD2 output (INT0)
312
    SpiMasterInit();
314
    SpiMasterInit();
313
 
315
 
314
        // PPM detection setup
316
        // PPM detection setup
315
        ppm_init();
317
        ppm_init();
316
 
318
 
317
    // enable interrupts
319
    // enable interrupts
318
    sei();
320
    sei();
319
#endif
321
#endif
320
 
322
 
321
    //write_ascii_string(2,  7, "         CaScAdE          ");
323
    //write_ascii_string(2,  7, "         CaScAdE          ");
322
    //write_ascii_string(2,  8, "is TESTING his open source");
324
    //write_ascii_string(2,  8, "is TESTING his open source");
323
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
325
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
324
 
326
 
325
    // we are ready
327
    // we are ready
326
    LED3_ON
328
    LED3_ON
327
 
329
 
328
#if ALLCHARSDEBUG | (WRITECHARS != -1)
330
#if ALLCHARSDEBUG | (WRITECHARS != -1)
329
        clear();
331
        clear();
330
        write_all_chars();
332
        write_all_chars();
331
        LED1_ON
333
        LED1_ON
332
        LED2_ON
334
        LED2_ON
333
        LED3_ON
335
        LED3_ON
334
        LED4_ON
336
        LED4_ON
335
#else
337
#else
336
 
338
 
337
        // clear serial screen
339
        // clear serial screen
338
        //usart1_puts("\x1B[2J\x1B[H");
340
        //usart1_puts("\x1B[2J\x1B[H");
339
        //usart1_puts("hello world!123\r\n");
341
        //usart1_puts("hello world!123\r\n");
340
 
-
 
341
#if FCONLY
-
 
342
        // request data ever 100ms from FC;
-
 
343
        usart1_request_mk_data(0, 'd', 100);
-
 
344
#else
-
 
345
        // request OSD Data from NC every 100ms
-
 
346
        usart1_request_mk_data(1, 'o', 100);
-
 
347
 
-
 
348
    // and disable debug...
-
 
349
    usart1_request_mk_data(0, 'd', 0);
-
 
350
#endif
-
 
351
 
342
 
352
    while (1) {
343
    while (1) {
353
        // in case SPI is ready and there is nothing to send right now
344
        // in case SPI is ready and there is nothing to send right now
354
        if (!icnt && spi_ready) {
345
        if (!icnt && spi_ready) {
355
            // correct transfer ends with d (done)
346
            // correct transfer ends with d (done)
356
            if (SPI_buffer.buffer.chk == 'd') {
347
            if (SPI_buffer.buffer.chk == 'd') {
357
                                ampere = SPI_buffer.data.ampere;
348
                                ampere = SPI_buffer.data.ampere;
358
                                ampere_wasted = SPI_buffer.data.mah;
349
                                ampere_wasted = SPI_buffer.data.mah;
359
                                s_volt = SPI_buffer.data.volt;
350
                                s_volt = SPI_buffer.data.volt;
360
 
351
 
361
                                // if this is the first receival we should print the small A
352
                                // if this is the first receival we should print the small A
362
                                if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) {
353
                                if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) {
363
                                        clear();
354
                                        clear();
364
                                        COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
355
                                        COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
365
                                        // update this flag
356
                                        // update this flag
366
                        COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC;
357
                        COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC;
367
                                }
358
                                }
368
            } else {
359
            } else {
369
                // update flags
360
                // update flags
370
                COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC;
361
                COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC;
371
            }
362
            }
372
                        StartTransfer(9);
363
                        StartTransfer(9);
373
        }
364
        }
374
        if (rxd_buffer_locked) {
365
        if (rxd_buffer_locked) {
375
#if FCONLY
366
#if FCONLY
376
            if (rxd_buffer[2] == 'D') { // FC Data
367
            if (rxd_buffer[2] == 'D') { // FC Data
377
                Decode64();
368
                Decode64();
378
                debugData = *((DebugOut_t*) pRxData);
369
                debugData = *((DebugOut_t*) pRxData);
379
 
370
 
380
                // init on first data retrival, distinguished by last battery :)
371
                // init on first data retrival, distinguished by last battery :)
381
                if (last_UBat == 255) {
372
                if (last_UBat == 255) {
382
                                        if (debugData.Analog[9] > 40) {
373
                                        if (debugData.Analog[9] > 40) {
383
                        // fix for min_UBat
374
                        // fix for min_UBat
384
                        min_UBat = debugData.Analog[9];
375
                        min_UBat = debugData.Analog[9];
385
                                                last_UBat = debugData.Analog[9];
376
                                                last_UBat = debugData.Analog[9];
386
                        init_cosd(last_UBat);
377
                        init_cosd(last_UBat);
387
                                        }
378
                                        }
388
                } else {
379
                } else {
389
                                        osd_fcmode();
380
                                        osd_fcmode();
390
                                }
381
                                }
-
 
382
                                seconds_since_last_data = 0;
391
            }
383
            }
392
#else
384
#else
393
            if (rxd_buffer[2] == 'O') { // NC OSD Data
385
            if (rxd_buffer[2] == 'O') { // NC OSD Data
394
                Decode64();
386
                Decode64();
395
                naviData = *((NaviData_t*) pRxData);
387
                naviData = *((NaviData_t*) pRxData);
396
 
388
 
397
                // init on first data retrival, distinguished by last battery :)
389
                // init on first data retrival, distinguished by last battery :)
398
                if (last_UBat == 255) {
390
                if (last_UBat == 255) {
399
                                        if (naviData.UBat > 40) {
391
                                        if (naviData.UBat > 40) {
400
                        // fix for min_UBat
392
                        // fix for min_UBat
401
                        min_UBat = naviData.UBat;
393
                        min_UBat = naviData.UBat;
402
                                                last_UBat = naviData.UBat;
394
                                                last_UBat = naviData.UBat;
403
                        init_cosd(last_UBat);
395
                        init_cosd(last_UBat);
404
                                        }
396
                                        }
405
                } else {
397
                } else {
406
                                        osd_ncmode();
398
                                        osd_ncmode();
407
                                }
399
                                }
-
 
400
                                //seconds_since_last_data = 0;
408
            }
401
            }
409
#endif
402
#endif
410
            rxd_buffer_locked = 0;
403
            rxd_buffer_locked = 0;
411
        }
404
        }
412
        // handle keypress
405
        // handle keypress
413
        if (s1_pressed()) {
406
        if (s1_pressed()) {
414
            config_menu();
407
            config_menu();
415
        }
408
        }
416
        if (seconds_since_last_data > 2) {
409
        if (seconds_since_last_data > 0) {
-
 
410
                        usart1_EnableTXD();
-
 
411
                        //usart1_puts_pgm(PSTR("zu alt\r\n"));                  
417
#if FCONLY
412
#if FCONLY
418
            // request data ever 100ms from FC;
413
            // request data ever 100ms from FC;
419
            usart1_request_mk_data(0, 'd', 100);
414
            //usart1_request_mk_data(0, 'd', 100);
-
 
415
                        usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
420
#else
416
#else
421
            // request OSD Data from NC every 100ms
417
            // request OSD Data from NC every 100ms
422
            usart1_request_mk_data(1, 'o', 100);
418
            //usart1_request_mk_data(1, 'o', 100);
-
 
419
                        usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
423
 
420
 
424
            // and disable debug...
421
            // and disable debug...
425
            usart1_request_mk_data(0, 'd', 0);
422
            //usart1_request_mk_data(0, 'd', 0);                
426
 
423
#endif
427
                        // reset last time counter
424
                        // reset last time counter
428
                        seconds_since_last_data = 0;
425
                        seconds_since_last_data = 0;
429
#endif
426
                        usart1_DisableTXD();
430
        }
427
        }
431
    }
428
    }
432
#endif
429
#endif
433
    return 0;
430
    return 0;
434
}
431
}
435
 
432