Subversion Repositories Projects

Rev

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