Rev 477 |
Rev 495 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/****************************************************************************
* Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje *
* admiralcascade@gmail.com *
* Project-URL: http://www.mylifesucks.de/oss/c-osd/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
* *
* Credits to: *
* Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN *
* Gregor "killagreg" Stobrawa for making the MK code readable *
* Klaus "akku" Buettner for the hardware *
* Manuel "KeyOz" Schrape for explaining the MK protocol to me *
****************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "main.h"
#include "max7456_software_spi.h"
#include "usart1.h"
#include "osd_helpers.h"
#include "config.h"
#include "spi.h"
#include "buttons.h"
#include "ppm.h"
/* TODO:
* - verifiy correctness of values
* - clean up code :)
*/
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
// data structs not needed for character flashing
#include "mk-data-structs.h"
/* ##########################################################################
* global definitions and global vars
* ##########################################################################*/
volatile uint16_t setsReceived = 0;
volatile NaviData_t naviData;
volatile DebugOut_t debugData;
// cache old vars for blinking attribute, checkup is faster than full
// attribute write each time
volatile uint8_t last_UBat = 255;
volatile uint8_t last_RC_Quality = 255;
// 16bit should be enough, normal LiPos don't last that long
volatile uint16_t uptime = 0;
volatile uint16_t timer = 0;
// remember last time data was received
volatile uint8_t seconds_since_last_data = 0;
// store stats description in progmem to save space
const char stats_item_0[] PROGMEM = "max Altitude:";
const char stats_item_1[] PROGMEM = "max Speed :";
const char stats_item_2[] PROGMEM = "max Distance:";
const char stats_item_3[] PROGMEM = "min Voltage :";
const char stats_item_4[] PROGMEM = "max Time :";
const char stats_item_5[] PROGMEM = "longitude :";
const char stats_item_6[] PROGMEM = "latitude :";
const char stats_item_7[] PROGMEM = "max current :";
const char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
#if !(FCONLY)
//char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
//char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
const char str_NE[] PROGMEM = "NE";
const char str_E[] PROGMEM = "E ";
const char str_SE[] PROGMEM = "SE";
const char str_S[] PROGMEM = "S ";
const char str_SW[] PROGMEM = "SW";
const char str_W[] PROGMEM = "W ";
const char str_NW[] PROGMEM = "NW";
const char str_N[] PROGMEM = "N ";
const char *directions[8] PROGMEM = {
str_NE,
str_E,
str_SE,
str_S,
str_SW,
str_W,
str_NW,
str_N};
#endif
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
// general PAL|NTSC distingiusch stuff
uint8_t top_line = 1;
uint8_t bottom_line = 14;
// battery voltages
uint8_t min_voltage = 0;
uint8_t max_voltage = 0;
// Flags
uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0;
/* ##########################################################################
* Interrupt handler
* ##########################################################################*/
/**
* handler for undefined Interrupts
* if not defined AVR will reset in case any unhandled interrupts occur
*/
ISR(__vector_default) {
asm("nop");
}
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
/* ##########################################################################
* timer stuff
* ##########################################################################*/
/**
* timer kicks in every 1000uS ^= 1ms
*/
ISR(TIMER0_COMP_vect) {
if (!timer--) {
uptime++;
timer = 999;
seconds_since_last_data++;
}
// in case there is still some spi data to send do it now
if (spi_ready && icnt) {
SPDR = *iptr;
}
}
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
/* ##########################################################################
* MAIN
* ##########################################################################*/
int main(void) {
// set up FLAGS, compiler should flatten this one
COSD_FLAGS = (NTSC << 0);
COSD_FLAGS |= (HUD << 1);
COSD_FLAGS |= (ARTHORIZON << 2);
COSD_FLAGS |= (BIGVARIO << 3);
COSD_FLAGS |= (STATS << 4);
COSD_FLAGS |= (WARNINGS << 5);
// set up Atmega162 Ports
DDRA |= (1 << PA1); // PA1 output (/CS)
MAX_CS_HIGH
DDRA |= (1 << PA2); // PA2 output (SDIN)
MAX_SDIN_LOW
DDRA |= (1 << PA3); // PA3 output (SCLK)
MAX_SCLK_LOW
DDRA |= (1 << PA5); // PA5 output (RESET)
MAX_RESET_HIGH
DDRC |= (1 << PC0); // PC0 output (LED1 gn)
LED1_OFF
DDRC |= (1 << PC1); // PC1 output (LED2 rt)
LED2_OFF
DDRC |= (1 << PC2); // PC2 output (LED3 gn)
LED3_OFF
DDRC |= (1 << PC3); // PC3 output (LED4 rt)
LED4_OFF
DDRC &= ~(1 << PC4); // PC4 input (MODE)
PORTC |= (1 << PC4); // pullup
DDRC &= ~(1 << PC5); // PC5 input (SET)
PORTC |= (1 << PC5); // pullup
// reset the MAX7456 to be sure any undefined states do no harm
MAX_RESET_LOW
MAX_RESET_HIGH
// give the FC/NC and the maxim time to come up
LED4_ON
_delay_ms(2000);
LED4_OFF
//Pushing NEW chars to the MAX7456
#if (WRITECHARS != -1)
// DISABLE display (VM0)
spi_send_byte(0x00, 0b00000000);
#include "characters.c"
#else
// read out config for NTSC/PAL distinguishing
get_eeprom(0);
#endif
// Setup Video Mode
if (COSD_FLAGS & COSD_FLAG_NTSC) {
// NTSC + enable display immediately (VM0)
spi_send_byte(0x00, 0b00001000);
bottom_line = 12;
} else {
// PAL + enable display immediately (VM0)
spi_send_byte(0x00, 0b01001000);
bottom_line = 14;
}
/*// clear all display-mem (DMM)
spi_send_byte(0x04, 0b00000100);
// clearing takes 12uS according to maxim so lets wait longer
_delay_us(120);
// 8bit mode
spi_send_byte(0x04, 0b01000000);*/
// clear display memory and set to 8bit mode
clear();
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
// init usart
usart1_init();
// set up timer
// CTC, Prescaler /64
TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
TCNT0 = 0;
OCR0 = 250;
// enable timer output compare interrupt
TIMSK &= ~(1 << TOIE0);
TIMSK |= (1 << OCIE0);
// SPI setup
DDRD |= (1 << PD2); // PD2 output (INT0)
SpiMasterInit();
// PPM detection setup
ppm_init();
// enable interrupts
sei();
#endif
//write_ascii_string(2, 7, " CaScAdE ");
//write_ascii_string(2, 8, "is TESTING his open source");
//write_ascii_string(2, 9, " EPi OSD Firmware");
// we are ready
LED3_ON
#if ALLCHARSDEBUG | (WRITECHARS != -1)
clear();
write_all_chars();
#else
// clear serial screen
//usart1_puts("\x1B[2J\x1B[H");
//usart1_puts("hello world!123\r\n");
#if FCONLY
// request data ever 100ms from FC;
usart1_request_mk_data(0, 'd', 100);
#else
// request OSD Data from NC every 100ms
usart1_request_mk_data(1, 'o', 100);
// and disable debug...
usart1_request_mk_data(0, 'd', 0);
#endif
// stats for after flight
int16_t max_Altimeter = 0;
uint8_t min_UBat = 255;
#if !(FCONLY)
uint16_t max_GroundSpeed = 0;
int16_t max_Distance = 0;
uint16_t max_FlyingTime = 0;
// flags from last round to check for changes
uint8_t old_MKFlags = 0;
#endif
while (1) {
// in case SPI is ready and there is nothing to send right now
if (!icnt && spi_ready) {
// correct transfer ends with d (done)
if (spi_cmd_buffer[0] == 'd') {
ampere = spi_cmd_buffer[1] << 8;
ampere |= spi_cmd_buffer[2];
ampere_wasted = spi_cmd_buffer[3];
ampere_wasted = ampere_wasted << 8;
ampere_wasted |= spi_cmd_buffer[4];
ampere_wasted = ampere_wasted << 8;
ampere_wasted |= spi_cmd_buffer[5];
ampere_wasted = ampere_wasted << 8;
ampere_wasted |= spi_cmd_buffer[6];
//ampere = *((int16_t *) spi_cmd_buffer + 1);
//ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3);
// if this is the first receival we should print the small A
if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) {
clear();
COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
// update this flag
COSD_FLAGS2 |= COSD_FLAG_STROMREC;
}
//write_ascii_char(8+4*30, 'v'); // valid
} else {
// update flags
COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
//write_ascii_char(8+4*30, 'i'); // invalid
}
spi_cmd_buffer[0] = 'A';
spi_cmd_buffer[1] = 'B';
spi_cmd_buffer[2] = 'C';
spi_cmd_buffer[3] = 'D';
spi_cmd_buffer[4] = 'E';
spi_cmd_buffer[5] = 'F';
spi_cmd_buffer[6] = 'G';
StartTransfer((unsigned char*) spi_cmd_buffer, 7);
}
if (rxd_buffer_locked) {
#if FCONLY
if (rxd_buffer[2] == 'D') { // FC Data
Decode64();
debugData = *((DebugOut_t*) pRxData);
// init on first data retrival, distinguished by last battery :)
if (last_UBat == 255) {
// fix for min_UBat
min_UBat = debugData.Analog[9];
init_cosd(debugData.Analog[9]);
}
#include "osd_fcmode_default.c"
}
#else
if (rxd_buffer[2] == 'O') { // NC OSD Data
Decode64();
naviData = *((NaviData_t*) pRxData);
// init on first data retrival, distinguished by last battery :)
if (last_UBat == 255) {
// fix for min_UBat
min_UBat = naviData.UBat;
init_cosd(naviData.UBat);
}
#include "osd_ncmode_default.c"
}
#endif
rxd_buffer_locked = 0;
}
// handle keypress
if (s1_pressed()) {
config_menu();
}
if (seconds_since_last_data > 2) {
#if FCONLY
// request data ever 100ms from FC;
usart1_request_mk_data(0, 'd', 100);
#else
// request OSD Data from NC every 100ms
usart1_request_mk_data(1, 'o', 100);
// and disable debug...
usart1_request_mk_data(0, 'd', 0);
// reset last time counter
seconds_since_last_data = 0;
#endif
}
}
#endif
return 0;
}