Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 736 → Rev 737

/branches/V0.68d Code Redesign killagreg/uart.c
24,10 → 24,9
uint8_t DebugGetAnforderung = 0, DebugDisplayAnforderung = 0, DebugDataAnforderung = 0, GetVersionAnforderung = 0;
 
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
volatile uint8_t rxd_buffer_locked = FALSE;
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
 
volatile uint8_t RxDataProcessed = 0;
volatile uint8_t txd_complete = 1;
volatile uint8_t txd_complete = TRUE;
volatile uint8_t ReceivedBytes = 0;
 
uint8_t RemotePollDisplayLine = 0;
139,6 → 138,7
// enable TX-Interrupt
UCSR0B |= (1 << TXCIE0);
 
rxd_buffer_locked = FALSE;
// restore global interrupt flags
SREG = sreg;
}
172,67 → 172,70
ISR(USART0_RX_vect)
{
static uint16_t crc;
static uint8_t crc1, crc2, ptr_rxd_buffer = 0;
static uint8_t UartState = 0;
uint8_t CrcOkay = 0;
static uint8_t ptr_rxd_buffer = 0;
uint8_t crc1, crc2;
uint8_t c;
 
c = UDR0; // catch the received byte
if(ptr_rxd_buffer >= RXD_BUFFER_LEN) UartState = 0; // rxd buffer overflow -> reset rx uart state
if(c == '\r' && UartState == 2) // termination character received during data collection state
c = UDR0; // catch the received byte
 
 
if(rxd_buffer_locked) return; // if txd buffer is locked immediately return
 
// the rxd buffer is unlocked
if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
{
UartState = 0; //reset rxd uart state
// the last 2 bytes are no subject for checksum calculation
// they are the checksum itself
crc -= rxd_buffer[ptr_rxd_buffer-2];
crc -= rxd_buffer[ptr_rxd_buffer-1];
// calculate checksum from transmitted data
crc %= 4096;
crc1 = '=' + crc / 64;
crc2 = '=' + crc % 64;
CrcOkay = 0;
// compare with transmitted checksum bytes
if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) CrcOkay = 1;
else CrcOkay = 0;
 
if(RxDataProcessed && CrcOkay) // data already processed and CRC OK?
rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
crc = c; // init crc
}
#if 0
else if (ptr_rxd_buffer == 1) // handle address
{
rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
crc += c; // update crc
}
#endif
else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
{
if(c != '\r') // no termination character
{
RxDataProcessed = FALSE; // reset flag
ReceivedBytes = ptr_rxd_buffer; // store number of received bytes
rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
// if 2nd byte is an 'R' enable watchdog that will result in an reset
if(rxd_buffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
crc += c; // update crc
}
}
else // termination character was received
{
// the last 2 bytes are no subject for checksum calculation
// they are the checksum itself
crc -= rxd_buffer[ptr_rxd_buffer-2];
crc -= rxd_buffer[ptr_rxd_buffer-1];
// calculate checksum from transmitted data
crc %= 4096;
crc1 = '=' + crc / 64;
crc2 = '=' + crc % 64;
// compare checksum to transmitted checksum bytes
if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
{ // checksum valid
rxd_buffer_locked = TRUE; // lock the rxd buffer
ReceivedBytes = ptr_rxd_buffer; // store number of received bytes
rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
// if 2nd byte is an 'R' enable watchdog that will result in an reset
if(rxd_buffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
}
else
{ // checksum invalid
rxd_buffer_locked = FALSE; // unlock rxd buffer
}
ptr_rxd_buffer = 0; // reset txd buffer
}
} // buffer overrun
else
switch(UartState)
{
case 0: // reset rxd buffer
if(c == '#' && RxDataProcessed) UartState = 1; // start character recieved and previous data already processed
ptr_rxd_buffer = 0; // reset buffer pointer
rxd_buffer[ptr_rxd_buffer++] = c; // write character to rxd buffer
crc = c; // initialize crc
break;
case 1: // check address
UartState++; // switch to next state
rxd_buffer[ptr_rxd_buffer++] = c; // write character to rxd buffer
crc += c; // update crc
break;
case 2: // collect received data
rxd_buffer[ptr_rxd_buffer] = c; // write character to rxd buffer
// if buffer overflow -> reset buffer
if(ptr_rxd_buffer < RXD_BUFFER_LEN) ptr_rxd_buffer++;
else UartState = 0;
crc += c; // update checksum
break;
default:
UartState = 0;
break;
ptr_rxd_buffer = 0; // reset rxd buffer
rxd_buffer_locked = FALSE; // unlock rxd buffer
}
 
}
 
 
 
// --------------------------------------------------------------------------
void AddCRC(uint16_t datalen)
{
304,8 → 307,8
// --------------------------------------------------------------------------
void ProcessRxData(void)
{
// if data in the rxd buffer are alredy processed immediately return
if(RxDataProcessed) return;
// if data in the rxd buffer are not locked immediately return
if(!rxd_buffer_locked) return;
 
uint8_t tmp_char_arr2[2]; // local buffer
 
375,7 → 378,8
 
 
}
RxDataProcessed = TRUE;
// unlock the rxd buffer after processing
rxd_buffer_locked = FALSE;
}
 
//############################################################################