Subversion Repositories Projects

Rev

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

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