b15f/firmware/usart.cpp

140 lines
2.3 KiB
C++
Raw Normal View History

2019-03-26 11:35:41 +01:00
#include "usart.h"
2019-04-02 16:19:42 +02:00
void USART::init() volatile
2019-03-26 11:35:41 +01:00
{
2019-03-27 11:45:30 +01:00
UCSR0A = _BV(U2X0);
2019-04-02 13:34:59 +02:00
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0) | _BV(TXCIE0);
2019-03-26 11:35:41 +01:00
// Einstellen des Datenformats: 8 Datenbits, 1 Stoppbit
UCSR0C = _BV(UCSZ00) |_BV(UCSZ01);// (1<<URSEL0)|(1<<UCSZ10)|(1<<UCSZ00);
// setze Baudrate
2019-03-27 11:45:30 +01:00
UBRR0H = (((F_CPU / (8UL * BAUDRATE))-1) >> 8) & 0xFF;
UBRR0L = ((F_CPU / (8UL * BAUDRATE))-1) & 0xFF;
2019-03-26 11:35:41 +01:00
}
2019-04-02 15:32:51 +02:00
void USART::clearInputBuffer() volatile
2019-03-29 13:35:18 +01:00
{
uint8_t dummy;
2019-04-01 08:37:00 +02:00
do
{
2019-03-29 13:35:18 +01:00
dummy = UDR0;
2019-04-03 09:34:22 +02:00
_delay_us(US_PER_BIT * 16); // Warte <20>bertragungszeit von 16 Bit ab
2019-04-01 08:37:00 +02:00
}
while (UCSR0A & (1<<RXC0));
2019-03-29 13:35:18 +01:00
if(dummy) // taeusche dummy Verwendung vor
return;
}
2019-04-02 16:19:42 +02:00
void USART::initRX(void) volatile
{
receive_pos = 0;
}
void USART::initTX(void) volatile
{
2019-04-03 14:51:33 +02:00
while(active);
2019-04-02 16:19:42 +02:00
send_pos = 0;
2019-04-03 09:34:22 +02:00
send_crc = 0;
2019-04-02 16:19:42 +02:00
}
2019-04-02 15:32:51 +02:00
void USART::handleRX(void) volatile
2019-03-26 11:35:41 +01:00
{
2019-04-02 16:19:42 +02:00
receive_buffer[receive_pos++] = UDR0;
2019-04-02 15:32:51 +02:00
2019-04-03 09:34:22 +02:00
if(receive_pos == 1)
{
// starte WDT, da neue Request angekommen ist
wdt_enable(WDT_TIMEOUT);
wdt_reset();
}
2019-04-02 16:19:42 +02:00
if(receive_pos >= rq_len[receive_buffer[0]]) // last byte of request
2019-04-02 15:32:51 +02:00
{
receive_pos = 0;
2019-04-02 16:19:42 +02:00
nextRequest = true;
2019-04-02 15:32:51 +02:00
}
}
void USART::handleTX(void) volatile
{
if(send_pos < send_len)
{
while (!(UCSR0A & (1<<UDRE0)));
UDR0 = send_buffer[send_pos++];
}
else
{
2019-04-03 14:51:33 +02:00
active = false;
2019-04-02 15:32:51 +02:00
}
}
void USART::flush(void) volatile
{
if(send_pos == 0)
return;
send_len = send_pos;
send_pos = 0;
2019-04-03 14:51:33 +02:00
active = true;
2019-04-02 15:32:51 +02:00
handleTX();
}
2019-04-02 16:19:42 +02:00
2019-04-02 15:32:51 +02:00
void USART::writeByte(uint8_t b) volatile
2019-04-02 13:34:59 +02:00
{
send_buffer[send_pos++] = b;
2019-04-03 09:34:22 +02: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 11:35:41 +01:00
}
2019-03-26 15:02:58 +01:00
2019-04-02 15:32:51 +02:00
void USART::writeInt(uint16_t v) volatile
2019-03-26 15:02:58 +01:00
{
2019-04-02 13:34:59 +02:00
writeByte(v & 0xFF);
2019-03-26 15:02:58 +01:00
v >>= 8;
2019-04-02 13:34:59 +02:00
writeByte(v & 0xFF);
2019-03-26 15:02:58 +01:00
}
2019-04-02 15:32:51 +02:00
void USART::writeStr(const char* str, uint8_t len) volatile
2019-03-27 15:48:36 +01:00
{
writeByte(len);
while(len--)
writeByte(*str++);
}
2019-04-03 09:34:22 +02:00
void USART::writeCRC(void) volatile
{
writeByte(send_crc);
}
2019-04-02 15:32:51 +02:00
uint8_t USART::readByte() volatile
2019-03-26 11:35:41 +01:00
{
2019-04-02 13:34:59 +02:00
return receive_buffer[receive_pos++];
2019-03-26 11:35:41 +01:00
}
2019-03-26 15:02:58 +01:00
2019-04-02 15:32:51 +02:00
uint16_t USART::readInt() volatile
2019-03-26 15:02:58 +01:00
{
uint16_t v = readByte();
v |= readByte() << 8;
return v;
}
2019-05-28 12:32:31 +02:00
uint32_t USART::readU32() volatile
{
uint32_t v = readInt();
v |= ((uint32_t) readInt()) << 16;
return v;
}