Subversion Repositories Projects

Rev

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

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