60,6 → 60,7 |
// 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; |
90,7 → 91,7 |
uint8_t max_voltage = 0; |
|
// Flags |
uint8_t COSD_FLAGS = 0; |
uint8_t COSD_FLAGS = 0, COSD_FLAGS2; |
|
/* ########################################################################## |
* debounce buttons |
128,6 → 129,76 |
* timer stuff |
* ##########################################################################*/ |
|
|
/* ########################################################################## |
* STROM TEST |
* ##########################################################################*/ |
#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 |
} |
|
volatile size_t icnt = 0; |
volatile unsigned char * iptr; |
volatile unsigned char spi_cmd[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... */ |
if (icnt) { |
///_delay_ms(1); |
/* ...nächstes Byte senden. */ |
//SPDR = *iptr; |
spi_ready = 1; |
} |
else { |
/* ...andernfalls Interrupt Modus deaktivieren. */ |
SPCR &= ~_BV(SPIE); |
INT0_HIGH |
//_delay_ms(1); |
} |
} |
|
int TransferIsBusy(void) |
{ |
return SPCR & _BV(SPIE); |
} |
|
void StartTransfer(unsigned char *data, size_t len) |
{ |
INT0_LOW |
/* Interrupt Datenpointer und Zähler setzen. */ |
iptr = data; |
icnt = len; |
|
/* SPI Interrupt aktivieren und Transfer anstossen. */ |
SPCR |= _BV(SPIE); |
SPDR = *iptr; |
} |
|
|
/* ########################################################################## |
* / STROM TEST END |
* ##########################################################################*/ |
|
|
/** |
* timer kicks in every 1000uS ^= 1ms |
*/ |
138,9 → 209,12 |
timer = 999; |
seconds_since_last_data++; |
} |
short_timer++; |
if (spi_ready && icnt) { |
SPDR = *iptr; |
} |
} |
|
|
/* ########################################################################## |
* A simple config menu for the flags |
* ##########################################################################*/ |
333,12 → 407,13 |
COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
} |
|
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
#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); |
426,8 → 501,12 |
OCR0 = 15; // preload |
TIMSK |= (1 << TOIE0); // enable overflow timer0 |
|
DDRD |= (1 << PD2); // PD2 output (INT0) |
SpiMasterInit(); |
|
// enable interrupts |
sei(); |
|
#endif |
|
//write_ascii_string(2, 7, " CaScAdE "); |
470,7 → 549,27 |
//char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
#endif |
|
|
|
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); |
} |
if (rxd_buffer_locked) { |
if (COSD_FLAGS & COSD_FLAG_HUD) { |
#if FCONLY |