Subversion Repositories Projects

Rev

Rev 728 | Rev 735 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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