Subversion Repositories Projects

Compare Revisions

Regard whitespace Rev 465 → Rev 466

/C-OSD/trunk/main.c
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,8 → 135,7
#define INT0_HIGH PORTD |= (1 << PD2);
#define INT0_LOW PORTD &= ~(1 << PD2);
 
void SpiMasterInit(void)
{
void SpiMasterInit(void) {
volatile char IOReg;
// set PB4(/SS), PB5(MOSI), PB7(SCK) as output
DDRB = (1<<PB4)|(1<<PB5)|(1<<PB7);
151,46 → 149,44
 
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,7 → 204,7
timer = 999;
seconds_since_last_data++;
}
short_timer++;
// in case there is still some spi data to send do it now
if (spi_ready && icnt) {
SPDR = *iptr;
}
389,7 → 384,7
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;
}
552,23 → 547,24
 
 
while (1) {
// in case SPI is ready and there is nothing to send right now
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') {
// 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[0] = 'A';
spi_cmd[1] = 'B';
spi_cmd[2] = 'C';
StartTransfer((unsigned char*)spi_cmd, 3);
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) {