Subversion Repositories Projects

Rev

Rev 734 | Rev 736 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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