b15f/firmware/usart.h

135 lines
2.7 KiB
C
Raw Normal View History

2019-03-26 10:35:41 +00:00
#ifndef USART_H
#define USART_H
#include <avr/io.h>
2019-03-27 09:33:26 +00:00
#include <util/delay.h>
2019-03-26 10:35:41 +00:00
2019-04-02 13:32:51 +00:00
class USART;
#include "global_vars.h"
#include "requests.h"
2019-04-02 11:34:59 +00:00
2019-03-26 10:35:41 +00:00
class USART
{
2019-04-02 13:32:51 +00:00
public:
2019-04-03 12:51:33 +00:00
/*******************************
* Steuerung der Schnittstelle *
*******************************/
/**
* Initialisiert die SPI Register
*/
2019-04-02 14:19:42 +00:00
void init(void) volatile;
2019-04-03 12:51:33 +00:00
/**
* Verwirft Daten im Hardware-Eingangspuffer
*/
void clearInputBuffer(void) volatile;
/**
* Bereitet Empfang der nächsten Request vor
*/
2019-04-02 14:19:42 +00:00
void initRX(void) volatile;
2019-04-03 12:51:33 +00:00
/**
* Bereitet Ausgangspuffer für nächste Übertragung vor
*/
2019-04-02 14:19:42 +00:00
void initTX(void) volatile;
2019-04-03 12:51:33 +00:00
/**
* Behandlungsroutine für USART0_RX interrupt
*/
2019-04-02 14:19:42 +00:00
void handleRX(void) volatile;
2019-04-03 12:51:33 +00:00
/**
* Behandlungsroutine für USART0_TX interrupt
*/
2019-04-03 07:34:22 +00:00
void handleTX(void) volatile;
2019-04-03 12:51:33 +00:00
/**
* Startet Senden des Ausgangspuffers
*/
2019-04-02 14:19:42 +00:00
void flush(void) volatile;
2019-04-03 12:51:33 +00:00
/*******************************/
2019-03-26 14:02:58 +00:00
2019-04-02 11:34:59 +00:00
2019-04-02 14:19:42 +00:00
2019-04-03 12:51:33 +00:00
/******************
* 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 *
**********************/
2019-04-02 08:59:37 +00:00
2019-04-03 12:51:33 +00:00
/**
* Liest ein Byte aus dem Eingangspuffer
* \return gelesenes Byte
*/
uint8_t readByte(void) volatile;
2019-04-02 14:19:42 +00:00
2019-04-03 12:51:33 +00:00
/**
* Liest ein Integer aus dem Eingangspuffer
* \return gelesenes Integer
*/
uint16_t readInt(void) volatile;
2019-04-02 13:32:51 +00:00
2019-04-03 12:51:33 +00:00
/**********************/
2019-04-02 13:32:51 +00:00
2019-04-03 12:51:33 +00:00
// public constants
2019-04-03 07:34:22 +00:00
constexpr static uint8_t MSG_OK = 0xFF;
constexpr static uint8_t MSG_FAIL = 0xFE;
2019-04-02 13:32:51 +00:00
2019-04-02 08:59:37 +00:00
private:
2019-04-03 12:51:33 +00:00
// Eingangspuffer
2019-04-02 14:19:42 +00:00
volatile uint8_t receive_buffer[128];
2019-04-02 13:32:51 +00:00
volatile uint8_t receive_pos;
2019-04-03 12:51:33 +00:00
// Ausgangspuffer
2019-04-02 14:19:42 +00:00
volatile uint8_t send_buffer[128];
2019-04-02 13:32:51 +00:00
volatile uint8_t send_pos;
volatile uint8_t send_len;
2019-04-03 07:34:22 +00:00
volatile uint8_t send_crc;
2019-04-03 12:51:33 +00:00
// 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;
2019-03-26 10:35:41 +00:00
};
#endif // USART_H