/
async_usart.hpp
196 lines (166 loc) · 3.21 KB
/
async_usart.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef AVRLIB_ASYNC_USART_HPP
#define AVRLIB_ASYNC_USART_HPP
#include <stdint.h>
#include "nobootseq.hpp"
#include "buffer.hpp"
#include "usart_base.hpp"
namespace avrlib {
template <typename Usart, int RxBufferSize, int TxBufferSize, typename Bootseq = nobootseq, typename Overflow = uint32_t>
class async_usart
{
public:
typedef Usart usart_type;
typedef Overflow overflow_type;
typedef typename usart_type::value_type value_type;
typedef Bootseq bootseq_type;
async_usart()
{
}
template <typename T1>
async_usart(T1 const & t1)
{
m_usart.open(t1);
}
template <typename T1, typename T2>
async_usart(T1 const & t1, T2 const & t2)
{
m_usart.open(t1, t2);
}
bool empty() const
{
return m_rx_buffer.empty();
}
bool tx_empty() const
{
return m_tx_buffer.empty();
}
bool tx_ready() const
{
return !m_tx_buffer.full();
}
bool transmitted()
{
return m_usart.transmitted();
}
value_type read()
{
while (m_rx_buffer.empty())
{
cli();
this->process_rx();
sei();
}
value_type res = m_rx_buffer.top();
m_rx_buffer.pop();
return res;
}
uint8_t read_size() const
{
return m_rx_buffer.size();
}
void write(value_type v)
{
if(TxBufferSize != 0)
{
while (m_tx_buffer.full())
{
cli();
this->process_tx();
sei();
}
m_tx_buffer.push(v);
if(m_async_tx)
m_usart.dre_interrupt(uart_intr_med);
}
else
{
while(!m_usart.tx_empty())
{
cli();
__asm__ volatile ("nop");
sei();
}
m_usart.send(v);
}
}
void flush()
{
bool tx_empty = false;
while (!tx_empty)
{
cli();
this->process_tx();
tx_empty = m_tx_buffer.empty();
sei();
}
}
void process_rx()
{
if (!m_usart.rx_empty())
this->intr_rx();
}
bool intr_rx()
{
if(m_usart.overflow())
++m_overflow;
if(m_usart.frame_error())
{
m_usart.recv();
return false;
}
value_type v = m_bootseq.check(m_usart.recv());
if(!m_rx_buffer.push(v))
{
++m_overflow;
}
return true;
}
bool process_tx()
{
if (!m_tx_buffer.empty() && m_usart.tx_empty())
{
m_usart.send(m_tx_buffer.top());
m_tx_buffer.pop();
return true;
}
// TODO: flush the underlying port
return false;
}
bool intr_tx()
{
if (!m_tx_buffer.empty())
{
m_usart.send(m_tx_buffer.top());
m_tx_buffer.pop();
return true;
}
else
{
m_usart.dre_interrupt(uart_intr_off);
}
return false;
}
bool tx_reserve(uint8_t size)
{
return TxBufferSize - m_tx_buffer.size() > size;
}
overflow_type overflow() const { return m_overflow; }
void clear_overflow() { m_overflow = 0; }
typedef buffer<value_type, RxBufferSize> rx_buffer_type;
rx_buffer_type & rx_buffer() { return m_rx_buffer; }
typedef buffer<value_type, TxBufferSize> tx_buffer_type;
tx_buffer_type & tx_buffer() { return m_tx_buffer; }
usart_type & usart() { return m_usart; }
usart_type const & usart() const { return m_usart; }
void async_tx(const bool& en) { m_async_tx = en; }
bool async_tx() const { return m_async_tx; }
private:
usart_type m_usart;
buffer<value_type, RxBufferSize> m_rx_buffer;
buffer<value_type, TxBufferSize==0?1:TxBufferSize> m_tx_buffer;
bootseq_type m_bootseq;
volatile overflow_type m_overflow;
volatile bool m_async_tx;
};
}
#endif