Subversion Repositories Projects

Rev

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

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