Subversion Repositories Projects

Rev

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

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