Subversion Repositories Projects

Rev

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