Subversion Repositories Projects

Rev

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