Subversion Repositories Projects

Rev

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

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