Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
321 cascade 1
/****************************************************************************
2225 - 2
 *   Copyright (C) 2009-2016 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) {
1951 - 337
                // debug ppm
338
                //write_ndigit_number_u(9, 4, ppm, 5, 0);
339
 
340
 
466 cascade 341
        // in case SPI is ready and there is nothing to send right now
342
        if (!icnt && spi_ready) {
343
            // correct transfer ends with d (done)
507 cascade 344
            if (SPI_buffer.buffer.chk == 'd') {
761 - 345
                ampere = SPI_buffer.data.ampere;
346
                ampere_wasted = SPI_buffer.data.mah;
347
                s_volt = SPI_buffer.data.volt;
507 cascade 348
 
761 - 349
                // if this is the first receival we should print the small A
350
                if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) {
351
                    clear();
352
                    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
353
                    // update this flag
354
                    COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC;
355
                }
466 cascade 356
            } else {
357
                // update flags
528 cascade 358
                COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC;
466 cascade 359
            }
761 - 360
            StartTransfer(9);
466 cascade 361
        }
321 cascade 362
        if (rxd_buffer_locked) {
454 cascade 363
#if FCONLY
471 cascade 364
            if (rxd_buffer[2] == 'D') { // FC Data
365
                Decode64();
2009 - 366
                //debugData = *((str_DebugOut*)pRxData);
367
                                memcpy((char*)(&debugData), (char*)pRxData, sizeof(str_DebugOut));
368
                                rxd_buffer_locked = 0;
454 cascade 369
 
471 cascade 370
                // init on first data retrival, distinguished by last battery :)
2041 - 371
                if (!(COSD_FLAGS_RUNTIME & COSD_OSD_STARTED)) {
761 - 372
                    if (debugData.Analog[9] > 40) {
373
                        // fix for min_UBat
374
                        min_UBat = debugData.Analog[9];
375
                        last_UBat = debugData.Analog[9];
376
                        init_cosd(last_UBat);
2041 - 377
                                                COSD_FLAGS_RUNTIME |= COSD_OSD_STARTED;
761 - 378
                    }
677 cascade 379
                } else {
761 - 380
                    osd_fcmode();
381
                }
382
                seconds_since_last_data = 0;
2009 - 383
            } else {                   
384
                                rxd_buffer_locked = 0;
385
                        }
454 cascade 386
#else
471 cascade 387
            if (rxd_buffer[2] == 'O') { // NC OSD Data
388
                Decode64();
1801 - 389
                //naviData = *((NaviData_t*)pRxData);
390
                                memcpy((char*)(&naviData), (char*)pRxData, sizeof(NaviData_t));
391
                                rxd_buffer_locked = 0;
453 cascade 392
 
1197 - 393
                #ifdef SHIFTBYminus45
394
                naviData.AngleNick = (int8_t)(((int16_t)naviData.AngleRoll + (int16_t)naviData.AngleNick) / 2);
395
                naviData.AngleRoll = (int8_t)(((int16_t)naviData.AngleRoll - (int16_t)naviData.AngleNick) / 2);
396
                naviData.CompassHeading = (naviData.CompassHeading + (360 - 45)) % 360;
397
                #endif
398
 
471 cascade 399
                // init on first data retrival, distinguished by last battery :)
2041 - 400
                                if (!(COSD_FLAGS_RUNTIME & COSD_OSD_STARTED)) {
761 - 401
                    if (naviData.UBat > 40) {
402
                        // fix for min_UBat
403
                        min_UBat = naviData.UBat;
404
                        last_UBat = naviData.UBat;
405
                        init_cosd(last_UBat);
2041 - 406
                                                COSD_FLAGS_RUNTIME |= COSD_OSD_STARTED;
761 - 407
                    }
677 cascade 408
                } else {
761 - 409
                    osd_ncmode();
410
                }
411
                //seconds_since_last_data = 0;
1801 - 412
//            } else if (rxd_buffer[2] == 'C') { // 3D and Stick Data
413
//                              Decode64();
414
//                              memcpy((char*)(&data3d), (char*)pRxData, sizeof(Data3D_t));                             
415
//                              rxd_buffer_locked = 0;
416
                        } else {
417
                                rxd_buffer_locked = 0;
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
1801 - 453
 
321 cascade 454
        }
455
        // handle keypress
326 cascade 456
        if (s1_pressed()) {
339 cascade 457
            config_menu();
321 cascade 458
        }
2039 - 459
 
826 - 460
        // reqest data untill there has been some answer
461
        if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) ||
462
            // or while not in passive mode
463
            (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) {
761 - 464
            usart1_EnableTXD();
465
            //usart1_puts_pgm(PSTR("zu alt\r\n"));
454 cascade 466
#if FCONLY
455 cascade 467
            // request data ever 100ms from FC;
738 cascade 468
            //usart1_request_mk_data(0, 'd', 100);
761 - 469
            usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
783 - 470
#else                   
455 cascade 471
            // request OSD Data from NC every 100ms
738 cascade 472
            //usart1_request_mk_data(1, 'o', 100);
761 - 473
            usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
454 cascade 474
 
1801 - 475
 
476
 
477
            // request 3D Data from NC every 100ms
478
            //usart1_request_mk_data(1, 'c', 100);
479
//                      _delay_ms(10);
480
//            usart1_puts_pgm(PSTR(REQUEST_3DDATA));
481
 
455 cascade 482
            // and disable debug...
800 - 483
            //usart1_request_mk_data(0, 'd', 0);
738 cascade 484
#endif
761 - 485
            // reset last time counter
486
            seconds_since_last_data = 0;
487
            usart1_DisableTXD();
835 - 488
 
489
            // do not spam too much
490
            if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) {
1801 - 491
                _delay_ms(100);
835 - 492
            }
339 cascade 493
        }
942 - 494
 
495
 
496
        if (SJ1_CLOSED && !(COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we do not want the HUD anymore
497
            if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
498
                clear();
499
            }
500
            COSD_FLAGS_MODES &= ~COSD_FLAG_HUD;
501
            COSD_FLAGS_RUNTIME |= COSD_BLANKBYSJ;
502
        } else if (!SJ1_CLOSED && (COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we want the HUD back again            
503
            if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) {
504
                COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
505
            }
506
            COSD_FLAGS_MODES |= COSD_FLAG_HUD;
507
            COSD_FLAGS_RUNTIME &= ~(COSD_BLANKBYSJ);
508
        }
509
 
321 cascade 510
    }
762 - 511
 
512
#else // character flashing...
800 - 513
    clear();
762 - 514
    write_all_chars();
515
    LED1_ON
516
    LED2_ON
517
    LED3_ON
518
    LED4_ON
800 - 519
    while (1) {
762 - 520
 
800 - 521
    };
321 cascade 522
#endif
762 - 523
 
524
 
321 cascade 525
    return 0;
526
}