USART revised
This commit is contained in:
parent
2d26632f41
commit
787fc6f784
7 changed files with 108 additions and 191 deletions
141
firmware/usart.h
141
firmware/usart.h
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue