Subversion Repositories Projects

Rev

Rev 736 | Rev 761 | 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
 
339 cascade 226
    // set up Atmega162 Ports
321 cascade 227
    DDRA |= (1 << PA1); // PA1 output (/CS)
228
    MAX_CS_HIGH
229
    DDRA |= (1 << PA2); // PA2 output (SDIN)
230
    MAX_SDIN_LOW
231
    DDRA |= (1 << PA3); // PA3 output (SCLK)
232
    MAX_SCLK_LOW
233
    DDRA |= (1 << PA5); // PA5 output (RESET)
234
    MAX_RESET_HIGH
235
 
236
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
237
    LED1_OFF
238
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
239
    LED2_OFF
240
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
241
    LED3_OFF
242
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
243
    LED4_OFF
244
 
245
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
246
    PORTC |= (1 << PC4); // pullup
247
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
248
    PORTC |= (1 << PC5); // pullup
249
 
339 cascade 250
    // reset the MAX7456 to be sure any undefined states do no harm
321 cascade 251
    MAX_RESET_LOW
252
    MAX_RESET_HIGH
253
 
254
    // give the FC/NC and the maxim time to come up
255
    LED4_ON
736 cascade 256
    _delay_ms(1000);
321 cascade 257
    LED4_OFF
258
 
339 cascade 259
    //Pushing NEW chars to the MAX7456
329 cascade 260
#if (WRITECHARS != -1)
474 cascade 261
        // DISABLE display (VM0)
262
        spi_send_byte(0x00, 0b00000000);
514 cascade 263
        learn_all_chars_pgm();
474 cascade 264
#else
265
        // read out config for NTSC/PAL distinguishing
266
        get_eeprom(0);
455 cascade 267
#endif
321 cascade 268
 
474 cascade 269
        // Setup Video Mode
528 cascade 270
        if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
474 cascade 271
            // NTSC + enable display immediately (VM0)
272
            spi_send_byte(0x00, 0b00001000);
321 cascade 273
 
474 cascade 274
                bottom_line = 12;
275
        } else {
276
            // PAL + enable display immediately (VM0)
277
            spi_send_byte(0x00, 0b01001000);
278
 
279
                bottom_line = 14;
280
        }
281
 
404 cascade 282
    /*// clear all display-mem (DMM)
403 cascade 283
    spi_send_byte(0x04, 0b00000100);
284
 
285
    // clearing takes 12uS according to maxim so lets wait longer
286
    _delay_us(120);
287
 
321 cascade 288
    // 8bit mode
404 cascade 289
    spi_send_byte(0x04, 0b01000000);*/
321 cascade 290
 
404 cascade 291
    // clear display memory and set to 8bit mode
321 cascade 292
    clear();
293
 
329 cascade 294
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
321 cascade 295
    // init usart
296
    usart1_init();
297
 
738 cascade 298
    // keep serial port clean
299
    usart1_DisableTXD();
300
 
321 cascade 301
    // set up timer
489 woggle 302
        // CTC, Prescaler /64
303
        TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
321 cascade 304
 
489 woggle 305
        TCNT0 = 0;
306
        OCR0 = 250;
307
 
308
        // enable timer output compare interrupt
309
        TIMSK &= ~(1 << TOIE0);
310
        TIMSK |= (1 << OCIE0);
311
 
477 cascade 312
        // SPI setup
466 cascade 313
    DDRD |= (1 << PD2); // PD2 output (INT0)
314
    SpiMasterInit();
465 cascade 315
 
477 cascade 316
        // PPM detection setup
317
        ppm_init();
318
 
326 cascade 319
    // enable interrupts
321 cascade 320
    sei();
321
#endif
322
 
323
    //write_ascii_string(2,  7, "         CaScAdE          ");
324
    //write_ascii_string(2,  8, "is TESTING his open source");
325
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
326
 
327
    // we are ready
328
    LED3_ON
329
 
329 cascade 330
#if ALLCHARSDEBUG | (WRITECHARS != -1)
471 cascade 331
        clear();
685 cascade 332
        write_all_chars();
333
        LED1_ON
334
        LED2_ON
335
        LED3_ON
502 cascade 336
        LED4_ON
321 cascade 337
#else
477 cascade 338
 
471 cascade 339
        // clear serial screen
340
        //usart1_puts("\x1B[2J\x1B[H");
477 cascade 341
        //usart1_puts("hello world!123\r\n");
342
 
321 cascade 343
    while (1) {
466 cascade 344
        // in case SPI is ready and there is nothing to send right now
345
        if (!icnt && spi_ready) {
346
            // correct transfer ends with d (done)
507 cascade 347
            if (SPI_buffer.buffer.chk == 'd') {
348
                                ampere = SPI_buffer.data.ampere;
349
                                ampere_wasted = SPI_buffer.data.mah;
350
                                s_volt = SPI_buffer.data.volt;
351
 
467 cascade 352
                                // if this is the first receival we should print the small A
528 cascade 353
                                if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) {
474 cascade 354
                                        clear();
528 cascade 355
                                        COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
467 cascade 356
                                        // update this flag
528 cascade 357
                        COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC;
467 cascade 358
                                }
466 cascade 359
            } else {
360
                // update flags
528 cascade 361
                COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC;
466 cascade 362
            }
507 cascade 363
                        StartTransfer(9);
466 cascade 364
        }
321 cascade 365
        if (rxd_buffer_locked) {
454 cascade 366
#if FCONLY
471 cascade 367
            if (rxd_buffer[2] == 'D') { // FC Data
368
                Decode64();
369
                debugData = *((DebugOut_t*) pRxData);
454 cascade 370
 
471 cascade 371
                // init on first data retrival, distinguished by last battery :)
372
                if (last_UBat == 255) {
735 cascade 373
                                        if (debugData.Analog[9] > 40) {
734 cascade 374
                        // fix for min_UBat
375
                        min_UBat = debugData.Analog[9];
376
                                                last_UBat = debugData.Analog[9];
377
                        init_cosd(last_UBat);
378
                                        }
677 cascade 379
                } else {
380
                                        osd_fcmode();
381
                                }
738 cascade 382
                                seconds_since_last_data = 0;
471 cascade 383
            }
454 cascade 384
#else
471 cascade 385
            if (rxd_buffer[2] == 'O') { // NC OSD Data
386
                Decode64();
387
                naviData = *((NaviData_t*) pRxData);
453 cascade 388
 
471 cascade 389
                // init on first data retrival, distinguished by last battery :)
390
                if (last_UBat == 255) {
734 cascade 391
                                        if (naviData.UBat > 40) {
392
                        // fix for min_UBat
393
                        min_UBat = naviData.UBat;
394
                                                last_UBat = naviData.UBat;
395
                        init_cosd(last_UBat);
396
                                        }
677 cascade 397
                } else {
398
                                        osd_ncmode();
399
                                }
738 cascade 400
                                //seconds_since_last_data = 0;
471 cascade 401
            }
454 cascade 402
#endif
321 cascade 403
            rxd_buffer_locked = 0;
404
        }
405
        // handle keypress
326 cascade 406
        if (s1_pressed()) {
339 cascade 407
            config_menu();
321 cascade 408
        }
738 cascade 409
        if (seconds_since_last_data > 0) {
410
                        usart1_EnableTXD();
411
                        //usart1_puts_pgm(PSTR("zu alt\r\n"));                  
454 cascade 412
#if FCONLY
455 cascade 413
            // request data ever 100ms from FC;
738 cascade 414
            //usart1_request_mk_data(0, 'd', 100);
415
                        usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
454 cascade 416
#else
455 cascade 417
            // request OSD Data from NC every 100ms
738 cascade 418
            //usart1_request_mk_data(1, 'o', 100);
419
                        usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
454 cascade 420
 
455 cascade 421
            // and disable debug...
738 cascade 422
            //usart1_request_mk_data(0, 'd', 0);                
423
#endif
477 cascade 424
                        // reset last time counter
425
                        seconds_since_last_data = 0;
738 cascade 426
                        usart1_DisableTXD();
339 cascade 427
        }
321 cascade 428
    }
429
#endif
430
    return 0;
431
}