b15f/firmware/usart.h

61 lines
1.3 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
#include <stdint.h>
2019-04-02 11:34:59 +00:00
extern volatile uint8_t receive_buffer[32];
extern volatile uint8_t receive_pos;
extern volatile uint8_t send_buffer[32];
extern volatile uint8_t send_pos;
extern volatile uint8_t send_len;
2019-04-02 08:59:37 +00:00
enum BlockSequence
{
IDLE = 0,
LEN = 1,
DATA = 2,
CRC = 3,
END = 4,
};
2019-03-26 10:35:41 +00:00
class USART
{
public:
void init(void);
2019-04-02 08:59:37 +00:00
void clearInputBuffer(void);
2019-03-26 14:02:58 +00:00
2019-03-26 10:35:41 +00:00
void writeByte(uint8_t);
2019-03-26 14:02:58 +00:00
void writeInt(uint16_t);
void writeLong(uint32_t);
2019-03-27 14:48:36 +00:00
void writeStr(const char*, uint8_t);
2019-04-02 11:34:59 +00:00
2019-03-28 14:22:17 +00:00
uint8_t writeBlock(uint8_t*, uint8_t);
2019-03-26 10:35:41 +00:00
uint8_t readByte(void);
2019-03-26 14:02:58 +00:00
uint16_t readInt(void);
uint32_t readLong(void);
2019-04-02 11:34:59 +00:00
void write(void);
2019-04-02 08:59:37 +00:00
void nextByte(uint8_t byte);
2019-03-29 12:35:18 +00:00
void readBlock(uint8_t*, uint8_t);
2019-03-26 10:35:41 +00:00
constexpr static uint8_t MSG_OK = 0xFF;
constexpr static uint8_t MSG_FAIL = 0xFE;
2019-04-02 08:59:37 +00:00
uint8_t block_pos = 0;
// 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;
private:
uint8_t block_buffer[MAX_BLOCK_SIZE + 3]; // don't store BLOCK_END byte
uint8_t crc;
BlockSequence seq = BlockSequence::IDLE;
2019-03-26 10:35:41 +00:00
};
#endif // USART_H