40,7 → 40,7 |
*/ |
|
#if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
// data structs not needed for character flashin |
// data structs not needed for character flashing |
#include "mk-data-structs.h" |
|
/* ########################################################################## |
60,7 → 60,6 |
// 16bit should be enough, normal LiPos don't last that long |
volatile uint16_t uptime = 0; |
volatile uint16_t timer = 0; |
volatile uint8_t short_timer = 0; |
|
// remember last time data was received |
volatile uint8_t seconds_since_last_data = 0; |
136,61 → 135,58 |
#define INT0_HIGH PORTD |= (1 << PD2); |
#define INT0_LOW PORTD &= ~(1 << PD2); |
|
void SpiMasterInit(void) |
{ |
volatile char IOReg; |
// set PB4(/SS), PB5(MOSI), PB7(SCK) as output |
DDRB = (1<<PB4)|(1<<PB5)|(1<<PB7); |
PORTB |= (1 << PB4); // pullup SS |
// enable SPI Interrupt and SPI in Master Mode with SCK = CK/128 |
SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1); |
IOReg = SPSR; // clear SPIF bit in SPSR |
IOReg = SPDR; |
//sei(); // we do it later |
void SpiMasterInit(void) { |
volatile char IOReg; |
// set PB4(/SS), PB5(MOSI), PB7(SCK) as output |
DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7); |
PORTB |= (1 << PB4); // pullup SS |
// enable SPI Interrupt and SPI in Master Mode with SCK = CK/128 |
SPCR = (1 << SPIE) | (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << SPR1); |
IOReg = SPSR; // clear SPIF bit in SPSR |
IOReg = SPDR; |
//sei(); // we do it later |
} |
|
volatile size_t icnt = 0; |
volatile unsigned char * iptr; |
volatile unsigned char spi_cmd[5]; |
volatile unsigned char spi_cmd_buffer[5]; |
volatile uint8_t spi_ready = 1; |
int16_t ampere = 0; |
|
ISR(SPI_STC_vect) |
{ |
/* Empfangenes Byte im Puffer ablegen. */ |
*iptr++ = SPDR; |
/* Zähler dekrementieren. */ |
icnt--; |
/* Falls Zähler noch nicht 0 ist... */ |
/** |
* SPI interrupt handler |
*/ |
ISR(SPI_STC_vect) { |
*iptr++ = SPDR; // safe received byte to buffer |
icnt--; // dec length |
if (icnt) { |
///_delay_ms(1); |
/* ...nächstes Byte senden. */ |
//SPDR = *iptr; |
spi_ready = 1; |
//SPDR = *iptr; // send next byte |
spi_ready = 1; // we _should_ send later because the slave needs more time |
} else { |
SPCR &= ~_BV(SPIE); // deactivate interrupt |
INT0_HIGH // transfer is done, slave does not need to listen |
} |
else { |
/* ...andernfalls Interrupt Modus deaktivieren. */ |
SPCR &= ~_BV(SPIE); |
INT0_HIGH |
//_delay_ms(1); |
} |
} |
|
int TransferIsBusy(void) |
{ |
/** |
* check if SPI transfer is still busy |
*/ |
int TransferIsBusy(void) { |
return SPCR & _BV(SPIE); |
} |
|
void StartTransfer(unsigned char *data, size_t len) |
{ |
INT0_LOW |
/* Interrupt Datenpointer und Zähler setzen. */ |
/** |
* start a new transfer of <data> with length <len> |
*/ |
void StartTransfer(unsigned char *data, size_t len) { |
INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen |
|
// set up pointer and length for interrupt handler |
iptr = data; |
icnt = len; |
|
/* SPI Interrupt aktivieren und Transfer anstossen. */ |
SPCR |= _BV(SPIE); |
SPDR = *iptr; |
|
SPCR |= _BV(SPIE); // enable spi interrupt |
SPDR = *iptr; // start transfer by first bye |
} |
|
|
198,7 → 194,6 |
* / STROM TEST END |
* ##########################################################################*/ |
|
|
/** |
* timer kicks in every 1000uS ^= 1ms |
*/ |
209,10 → 204,10 |
timer = 999; |
seconds_since_last_data++; |
} |
short_timer++; |
if (spi_ready && icnt) { |
SPDR = *iptr; |
} |
// in case there is still some spi data to send do it now |
if (spi_ready && icnt) { |
SPDR = *iptr; |
} |
} |
|
/* ########################################################################## |
383,13 → 378,13 |
#else |
write_ascii_string(2, 3, "NaviCtrl Mode"); |
#endif |
write_ascii_string(2, 4, BUILDDATE); |
write_ascii_string(2, 4, BUILDDATE); |
uint8_t cellnum = 0; |
if (CELL_NUM == -1) { |
write_ascii_string(2, 6, "Guessing Number of Cells"); |
do { |
cellnum++; |
} while (UBat > ((cellnum * CELL_VOLT_MAX) + 15)); |
} while (UBat > ((cellnum * CELL_VOLT_MAX) + 23)); |
} else { |
cellnum = CELL_NUM; |
} |
413,7 → 408,7 |
* MAIN |
* ##########################################################################*/ |
int main(void) { |
|
|
// set up FLAGS, compiler should flatten this one |
COSD_FLAGS = (NTSC << 0); |
COSD_FLAGS |= (HUD << 1); |
466,9 → 461,9 |
|
//Pushing NEW chars to the MAX7456 |
#if (WRITECHARS != -1) |
// DISABLE display (VM0) |
spi_send_byte(0x00, 0b00000000); |
#include "characters.c" |
// DISABLE display (VM0) |
spi_send_byte(0x00, 0b00000000); |
#include "characters.c" |
#endif |
|
// Setup Video Mode |
501,12 → 496,12 |
OCR0 = 15; // preload |
TIMSK |= (1 << TOIE0); // enable overflow timer0 |
|
DDRD |= (1 << PD2); // PD2 output (INT0) |
SpiMasterInit(); |
DDRD |= (1 << PD2); // PD2 output (INT0) |
SpiMasterInit(); |
|
// enable interrupts |
sei(); |
|
|
#endif |
|
//write_ascii_string(2, 7, " CaScAdE "); |
517,18 → 512,18 |
LED3_ON |
|
#if ALLCHARSDEBUG | (WRITECHARS != -1) |
clear(); |
clear(); |
write_all_chars(); |
#else |
// clear serial screen |
//usart1_puts("\x1B[2J\x1B[H"); |
//usart1_puts("hello world!\r\n"); |
// clear serial screen |
//usart1_puts("\x1B[2J\x1B[H"); |
//usart1_puts("hello world!\r\n"); |
#if FCONLY |
// request data ever 100ms from FC; |
usart1_request_mk_data(0, 'd', 100); |
// 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); |
// request OSD Data from NC every 100ms |
usart1_request_mk_data(1, 'o', 100); |
|
// and disable debug... |
usart1_request_mk_data(0, 'd', 0); |
538,7 → 533,7 |
int16_t max_Altimeter = 0; |
uint8_t min_UBat = 255; |
#if !(FCONLY) |
uint16_t max_GroundSpeed = 0; |
uint16_t max_GroundSpeed = 0; |
int16_t max_Distance = 0; |
uint16_t max_FlyingTime = 0; |
|
552,24 → 547,25 |
|
|
while (1) { |
if (!icnt && spi_ready) { |
short_timer = 0; |
ampere = spi_cmd[1] << 8; |
ampere |= spi_cmd[2]; |
//write_ascii_char(7+4*30, spi_cmd[0]); |
|
if (spi_cmd[0] == 'd') { |
COSD_FLAGS2 |= COSD_FLAG_STROMREC; |
//write_ascii_char(8+4*30, 'v'); // valid |
} else { |
COSD_FLAGS2 &= ~COSD_FLAG_STROMREC; |
//write_ascii_char(8+4*30, 'i'); // invalid |
} |
spi_cmd[0] = 'A'; |
spi_cmd[1] = 'B'; |
spi_cmd[2] = 'C'; |
StartTransfer((unsigned char*)spi_cmd, 3); |
} |
// 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]; |
// update flags |
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'; |
StartTransfer((unsigned char*) spi_cmd_buffer, 3); |
} |
if (rxd_buffer_locked) { |
if (COSD_FLAGS & COSD_FLAG_HUD) { |
#if FCONLY |
579,25 → 575,25 |
|
// init on first data retrival, distinguished by last battery :) |
if (last_UBat == 255) { |
// fix for min_UBat |
min_UBat = debugData.Analog[9]; |
// fix for min_UBat |
min_UBat = debugData.Analog[9]; |
auto_config(debugData.Analog[9]); |
} |
#include "osd_fcmode_default.c" |
#include "osd_fcmode_default.c" |
} |
#else |
if (rxd_buffer[2] == 'O') { // NC OSD Data |
Decode64(); |
naviData = *((NaviData_t*) pRxData); |
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; |
auto_config(naviData.UBat); |
} |
#include "osd_ncmode_default.c" |
} |
// init on first data retrival, distinguished by last battery :) |
if (last_UBat == 255) { |
// fix for min_UBat |
min_UBat = naviData.UBat; |
auto_config(naviData.UBat); |
} |
#include "osd_ncmode_default.c" |
} |
#endif |
} |
rxd_buffer_locked = 0; |