USART revised

This commit is contained in:
Tristan Krause 2019-04-03 14:51:33 +02:00
parent 2d26632f41
commit 787fc6f784
7 changed files with 108 additions and 191 deletions

View file

@ -10,10 +10,10 @@ set label at 4,3 'U_{GS} [V] = 460' left
set label at 4,7 'U_{GS} [V] = 480' left
set label at 3,13 'U_{GS} [V] = 500' left
set label at 2,22 'U_{GS} [V] = 520' left
set label at 1,32 'U_{GS} [V] = 540' left
set label at 0,39 'U_{GS} [V] = 560' left
set label at 0,39 'U_{GS} [V] = 580' left
set label at 0,39 'U_{GS} [V] = 600' left
set label at 1,33 'U_{GS} [V] = 540' left
set label at 0,38 'U_{GS} [V] = 560' left
set label at 0,38 'U_{GS} [V] = 580' left
set label at 0,38 'U_{GS} [V] = 600' left
unset output
set terminal qt
unset output

Binary file not shown.

Binary file not shown.

View file

@ -24,6 +24,7 @@ public:
uint16_t getValue(uint8_t channel) volatile;
private:
// semaphore
volatile bool active = false;
};

View file

@ -68,6 +68,8 @@ private:
// Variablen für die Pufferverwaltung
volatile uint8_t index = 0;
volatile uint8_t length = 0;
// semaphore
volatile bool active = false;
// Constants

View file

@ -12,8 +12,6 @@ void USART::init() volatile
// setze Baudrate
UBRR0H = (((F_CPU / (8UL * BAUDRATE))-1) >> 8) & 0xFF;
UBRR0L = ((F_CPU / (8UL * BAUDRATE))-1) & 0xFF;
send_active = false;
}
void USART::clearInputBuffer() volatile
@ -38,7 +36,7 @@ void USART::initRX(void) volatile
void USART::initTX(void) volatile
{
while(send_active);
while(active);
send_pos = 0;
send_crc = 0;
}
@ -71,7 +69,7 @@ void USART::handleTX(void) volatile
}
else
{
send_active = false;
active = false;
}
}
@ -82,7 +80,7 @@ void USART::flush(void) volatile
send_len = send_pos;
send_pos = 0;
send_active = true;
active = true;
handleTX();
}
@ -122,28 +120,6 @@ void USART::writeCRC(void) volatile
writeByte(send_crc);
}
uint8_t USART::writeBlock(uint8_t* ptr, uint8_t len) volatile
{
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();
}
uint8_t USART::readByte() volatile
{
return receive_buffer[receive_pos++];
@ -155,120 +131,3 @@ uint16_t USART::readInt() volatile
v |= readByte() << 8;
return v;
}
void USART::nextByte(uint8_t byte) volatile
{
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++;
}
void USART::readBlock(uint8_t* ptr, uint8_t offset) volatile
{
ptr += offset;
uint8_t crc = 0x7F;
do
{
uint8_t len = readByte();
if(len == 0x80) // out of sync, war bereits stoppbyte
{
writeByte(MSG_FAIL);
continue;
}
else if(len > MAX_BLOCK_SIZE)
len = 0;
crc = 0;
for(uint8_t k = 0; k <= len; k++) // len + 1 Durchgänge (+ crc)
{
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;
}
clearInputBuffer(); // leere Eingangspuffer
writeByte(crc == 0 ? MSG_OK : MSG_FAIL);
}
while(crc != 0);
}

View file

@ -3,77 +3,132 @@
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include <avr/interrupt.h>
class USART;
#include "global_vars.h"
#include "requests.h"
enum BlockSequence
{
IDLE = 0,
LEN = 1,
DATA = 2,
CRC = 3,
END = 4,
};
class USART
{
public:
// Steuerung
/*******************************
* Steuerung der Schnittstelle *
*******************************/
/**
* Initialisiert die SPI Register
*/
void init(void) volatile;
void clearInputBuffer(void) volatile;
/**
* Verwirft Daten im Hardware-Eingangspuffer
*/
void clearInputBuffer(void) volatile;
/**
* Bereitet Empfang der nächsten Request vor
*/
void initRX(void) volatile;
/**
* Bereitet Ausgangspuffer für nächste Übertragung vor
*/
void initTX(void) volatile;
/**
* Behandlungsroutine für USART0_RX interrupt
*/
void handleRX(void) volatile;
/**
* Behandlungsroutine für USART0_TX interrupt
*/
void handleTX(void) volatile;
/**
* Startet Senden des Ausgangspuffers
*/
void flush(void) volatile;
/*******************************/
// Sendefunktionen
void writeByte(uint8_t) volatile;
void writeInt(uint16_t) volatile;
void writeStr(const char*, uint8_t) volatile;
/******************
* Sendfunktionen *
******************/
/**
* Fügt ein Byte dem Ausgangspuffer hinzu
* \param b das Byte
*/
void writeByte(uint8_t b) volatile;
/**
* Fügt ein Integer dem Ausgangspuffer hinzu
* \param v das Integer
*/
void writeInt(uint16_t v) volatile;
/**
* Fügt ein String dem Ausgangspuffer hinzu
* \param str der String
* \param len Anzahl zu sendender Character
*/
void writeStr(const char* str, uint8_t len) volatile;
/**
* Fügt den aktuellen CRC Wert dem Ausgangspuffer hinzu
*/
void writeCRC(void) volatile;
/******************/
/**********************
* Empfangsfunktionen *
**********************/
// Empfangsfunktionen
/**
* Liest ein Byte aus dem Eingangspuffer
* \return gelesenes Byte
*/
uint8_t readByte(void) volatile;
/**
* Liest ein Integer aus dem Eingangspuffer
* \return gelesenes Integer
*/
uint16_t readInt(void) volatile;
// Blockgedöns
void nextByte(uint8_t byte) volatile;
uint8_t writeBlock(uint8_t*, uint8_t) volatile;
void readBlock(uint8_t*, uint8_t) volatile;
uint8_t block_pos = 0;
/**********************/
// constants
// public constants
constexpr static uint8_t MSG_OK = 0xFF;
constexpr static uint8_t MSG_FAIL = 0xFE;
private:
// Eingangspuffer
volatile uint8_t receive_buffer[128];
volatile uint8_t receive_pos;
// Ausgangspuffer
volatile uint8_t send_buffer[128];
volatile uint8_t send_pos;
volatile uint8_t send_len;
volatile uint8_t send_crc;
// semaphore
volatile bool active = false;
// constants
constexpr static uint32_t BAUDRATE = 115200; // 38400
constexpr static uint8_t CRC7_POLY = 0x91;
constexpr static uint8_t MAX_BLOCK_SIZE = 64;
constexpr static uint8_t BLOCK_END = 0x80;
constexpr static uint16_t US_PER_BIT = 1000000 / BAUDRATE;
private:
uint8_t block_buffer[MAX_BLOCK_SIZE + 3]; // don't store BLOCK_END byte
uint8_t crc;
volatile BlockSequence seq = BlockSequence::IDLE;
volatile uint8_t receive_buffer[128];
volatile uint8_t receive_pos;
volatile uint8_t send_buffer[128];
volatile uint8_t send_pos;
volatile uint8_t send_len;
volatile uint8_t send_crc;
volatile bool send_active;
};
#endif // USART_H