b15f/firmware/usart.cpp

275 lines
4.4 KiB
C++
Raw Normal View History

2019-03-26 10:35:41 +00:00
#include "usart.h"
2019-04-02 14:19:42 +00:00
void USART::init() volatile
2019-03-26 10:35:41 +00:00
{
2019-03-27 10:45:30 +00:00
UCSR0A = _BV(U2X0);
2019-04-02 11:34:59 +00:00
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0) | _BV(TXCIE0);
2019-03-26 10:35:41 +00:00
// Einstellen des Datenformats: 8 Datenbits, 1 Stoppbit
UCSR0C = _BV(UCSZ00) |_BV(UCSZ01);// (1<<URSEL0)|(1<<UCSZ10)|(1<<UCSZ00);
// setze Baudrate
2019-03-27 10:45:30 +00:00
UBRR0H = (((F_CPU / (8UL * BAUDRATE))-1) >> 8) & 0xFF;
UBRR0L = ((F_CPU / (8UL * BAUDRATE))-1) & 0xFF;
2019-03-26 10:35:41 +00:00
2019-04-02 13:32:51 +00:00
send_active = false;
2019-03-26 10:35:41 +00:00
}
2019-04-02 13:32:51 +00:00
void USART::clearInputBuffer() volatile
2019-03-29 12:35:18 +00:00
{
uint8_t dummy;
2019-04-01 06:37:00 +00:00
do
{
2019-03-29 12:35:18 +00:00
dummy = UDR0;
2019-04-03 07:34:22 +00:00
_delay_us(US_PER_BIT * 16); // Warte <20>bertragungszeit von 16 Bit ab
2019-04-01 06:37:00 +00:00
}
while (UCSR0A & (1<<RXC0));
2019-03-29 12:35:18 +00:00
if(dummy) // taeusche dummy Verwendung vor
return;
}
2019-04-02 14:19:42 +00:00
void USART::initRX(void) volatile
{
receive_pos = 0;
}
void USART::initTX(void) volatile
{
while(send_active);
send_pos = 0;
2019-04-03 07:34:22 +00:00
send_crc = 0;
2019-04-02 14:19:42 +00:00
}
2019-04-02 13:32:51 +00:00
void USART::handleRX(void) volatile
2019-03-26 10:35:41 +00:00
{
2019-04-02 14:19:42 +00:00
receive_buffer[receive_pos++] = UDR0;
2019-04-02 13:32:51 +00:00
2019-04-03 07:34:22 +00:00
if(receive_pos == 1)
{
// starte WDT, da neue Request angekommen ist
wdt_enable(WDT_TIMEOUT);
wdt_reset();
}
2019-04-02 14:19:42 +00:00
if(receive_pos >= rq_len[receive_buffer[0]]) // last byte of request
2019-04-02 13:32:51 +00:00
{
receive_pos = 0;
2019-04-02 14:19:42 +00:00
nextRequest = true;
2019-04-02 13:32:51 +00:00
}
}
void USART::handleTX(void) volatile
{
2019-04-03 06:40:14 +00:00
dio0.writePortA(send_pos);
2019-04-02 13:32:51 +00:00
if(send_pos < send_len)
{
while (!(UCSR0A & (1<<UDRE0)));
UDR0 = send_buffer[send_pos++];
}
else
{
send_active = false;
}
}
void USART::flush(void) volatile
{
if(send_pos == 0)
return;
send_len = send_pos;
send_pos = 0;
send_active = true;
handleTX();
}
2019-04-02 14:19:42 +00:00
2019-04-02 13:32:51 +00:00
void USART::writeByte(uint8_t b) volatile
2019-04-02 11:34:59 +00:00
{
send_buffer[send_pos++] = b;
2019-04-03 07:34:22 +00:00
// calc crc
send_crc ^= b;
for (uint8_t i = 0; i < 8; i++)
{
if (send_crc & 1)
send_crc ^= CRC7_POLY;
send_crc >>= 1;
}
2019-03-26 10:35:41 +00:00
}
2019-03-26 14:02:58 +00:00
2019-04-02 13:32:51 +00:00
void USART::writeInt(uint16_t v) volatile
2019-03-26 14:02:58 +00:00
{
2019-04-02 11:34:59 +00:00
writeByte(v & 0xFF);
2019-03-26 14:02:58 +00:00
v >>= 8;
2019-04-02 11:34:59 +00:00
writeByte(v & 0xFF);
2019-03-26 14:02:58 +00:00
}
2019-04-02 13:32:51 +00:00
void USART::writeStr(const char* str, uint8_t len) volatile
2019-03-27 14:48:36 +00:00
{
writeByte(len);
while(len--)
writeByte(*str++);
}
2019-04-03 07:34:22 +00:00
void USART::writeCRC(void) volatile
{
writeByte(send_crc);
}
2019-04-02 13:32:51 +00:00
uint8_t USART::writeBlock(uint8_t* ptr, uint8_t len) volatile
2019-03-28 14:22:17 +00:00
{
writeByte(len);
uint8_t crc = 0;
while(len--)
{
writeByte(*ptr);
crc ^= *ptr++;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 1)
crc ^= CRC7_POLY;
crc >>= 1;
}
}
writeByte(crc);
return readByte();
}
2019-04-02 13:32:51 +00:00
uint8_t USART::readByte() volatile
2019-03-26 10:35:41 +00:00
{
2019-04-02 11:34:59 +00:00
return receive_buffer[receive_pos++];
2019-03-26 10:35:41 +00:00
}
2019-03-26 14:02:58 +00:00
2019-04-02 13:32:51 +00:00
uint16_t USART::readInt() volatile
2019-03-26 14:02:58 +00:00
{
uint16_t v = readByte();
v |= readByte() << 8;
return v;
}
2019-03-29 12:35:18 +00:00
2019-04-02 14:19:42 +00:00
2019-04-02 13:32:51 +00:00
void USART::nextByte(uint8_t byte) volatile
2019-04-02 08:59:37 +00:00
{
switch(seq)
{
case IDLE:
{
if(byte > MAX_BLOCK_SIZE)
{
clearInputBuffer();
writeByte(MSG_FAIL);
seq = BlockSequence::IDLE;
}
else
{
block_buffer[0] = byte;
crc = 0;
block_pos = 0;
seq = BlockSequence::LEN;
}
break;
}
case LEN:
{
block_buffer[block_pos] = byte;
seq = BlockSequence::DATA;
break;
}
case DATA:
{
block_buffer[block_pos] = byte;
crc ^= byte;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 1)
crc ^= CRC7_POLY;
crc >>= 1;
}
if(block_pos == block_buffer[0])
seq = BlockSequence::CRC;
else if(block_pos >= block_buffer[0])
{
clearInputBuffer();
writeByte(MSG_FAIL);
seq = BlockSequence::IDLE;
}
break;
}
case CRC:
{
block_buffer[block_pos] = byte;
crc ^= byte;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 1)
crc ^= CRC7_POLY;
crc >>= 1;
}
seq = BlockSequence::END;
break;
}
case END:
{
clearInputBuffer();
writeByte(crc == 0 ? MSG_OK : MSG_FAIL);
seq = BlockSequence::IDLE;
break;
}
}
block_pos++;
}
2019-04-02 13:32:51 +00:00
void USART::readBlock(uint8_t* ptr, uint8_t offset) volatile
2019-03-29 12:35:18 +00:00
{
ptr += offset;
2019-04-01 14:08:32 +00:00
uint8_t crc = 0x7F;
2019-04-01 06:37:00 +00:00
do
2019-03-29 12:35:18 +00:00
{
2019-04-01 06:37:00 +00:00
uint8_t len = readByte();
2019-04-01 14:08:32 +00:00
if(len == 0x80) // out of sync, war bereits stoppbyte
{
writeByte(MSG_FAIL);
continue;
}
else if(len > MAX_BLOCK_SIZE)
2019-04-01 06:37:00 +00:00
len = 0;
2019-03-29 12:35:18 +00:00
2019-04-01 14:08:32 +00:00
crc = 0;
2019-04-01 06:37:00 +00:00
for(uint8_t k = 0; k <= len; k++) // len + 1 Durchg<68>nge (+ crc)
2019-03-29 12:35:18 +00:00
{
2019-04-01 06:37:00 +00:00
uint8_t next = readByte();
crc ^= next;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 1)
crc ^= CRC7_POLY;
crc >>= 1;
}
if(k < len)
ptr[k] = next;
2019-03-29 12:35:18 +00:00
}
2019-04-01 06:37:00 +00:00
2019-04-02 08:59:37 +00:00
clearInputBuffer(); // leere Eingangspuffer
2019-04-01 06:37:00 +00:00
writeByte(crc == 0 ? MSG_OK : MSG_FAIL);
2019-03-29 12:35:18 +00:00
}
2019-04-01 06:37:00 +00:00
while(crc != 0);
2019-03-29 12:35:18 +00:00
}