Subversion Repositories Projects

Rev

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

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