-
Notifications
You must be signed in to change notification settings - Fork 1
/
fluart.vhdl
269 lines (221 loc) · 6.29 KB
/
fluart.vhdl
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
-- fluart: the featureless UART
-- Very simple UART, inspired by https://github.com/freecores/rs232_interface
-- Copyright 2019, 2020 Marc Joosen
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity fluart is
generic (
CLK_FREQ: integer := 50_000_000; -- main frequency (Hz)
SER_FREQ: integer := 115200; -- bit rate (bps), any number up to CLK_FREQ / 2
BRK_LEN: integer := 10 -- break duration (tx), minimum break duration (rx) in bits
);
port (
clk: in std_logic;
reset: in std_logic;
rxd: in std_logic;
txd: out std_logic;
tx_data: in std_logic_vector(7 downto 0);
tx_req: in std_logic;
tx_brk: in std_logic;
tx_busy: out std_logic;
tx_end: out std_logic;
rx_data: out std_logic_vector(7 downto 0);
rx_data_valid: out std_logic;
rx_brk: out std_logic;
rx_err: out std_logic
);
begin
assert BRK_LEN >= 10 report "BRK_LEN must be >= 10" severity failure;
end;
architecture rtl of fluart is
type state is (idle, start, data, stop1, stop2, break);
constant CLK_DIV_MAX: natural := CLK_FREQ / SER_FREQ - 1;
signal tx_state: state;
signal tx_clk_div: integer range 0 to CLK_DIV_MAX;
signal tx_data_tmp: std_logic_vector(7 downto 0);
signal tx_bit_cnt: integer range 0 to BRK_LEN;
signal rx_state: state;
signal rxd_d: std_logic_vector(3 downto 0);
signal rx_data_i: std_logic_vector(7 downto 0);
signal rx_clk_div: integer range 0 to CLK_DIV_MAX;
signal rx_bit_cnt: integer range 0 to BRK_LEN;
begin
tx_proc: process(clk)
begin
if rising_edge(clk) then
if reset = '1' then
tx_state <= idle;
tx_clk_div <= 0;
tx_busy <= '0';
tx_end <= '0';
txd <= '1';
tx_data_tmp <= (others => '0');
tx_bit_cnt <= 0;
elsif tx_state /= idle and tx_clk_div /= CLK_DIV_MAX then
tx_clk_div <= tx_clk_div + 1;
-- tx_end pulse coincides with last cycle of tx_busy
if (tx_state = stop2 or (tx_state = break and tx_bit_cnt = BRK_LEN - 1))
and tx_clk_div = CLK_DIV_MAX - 1 then
tx_end <= '1';
end if;
else -- tx_state = idle (ready to transmit), or at the end of a bit period
-- defaults
tx_clk_div <= 0;
tx_end <= '0';
case tx_state is
when idle =>
if tx_req = '1' then
-- send start bit
tx_busy <= '1';
txd <= '0';
tx_data_tmp <= tx_data;
tx_state <= data;
tx_bit_cnt <= 0;
elsif tx_brk = '1' then
tx_busy <= '1';
txd <= '0';
tx_state <= break;
tx_bit_cnt <= 0;
else
txd <= '1';
end if;
when data =>
txd <= tx_data_tmp(0);
if tx_bit_cnt = 7 then
tx_state <= stop1;
else
tx_data_tmp <= '0' & tx_data_tmp(7 downto 1);
tx_bit_cnt <= tx_bit_cnt + 1;
end if;
when stop1 =>
txd <= '1';
tx_state <= stop2;
when stop2 =>
txd <= '1';
tx_state <= idle;
tx_busy <= '0';
when break =>
txd <= '0';
if tx_bit_cnt = BRK_LEN - 1 then
tx_state <= idle;
txd <= '1';
tx_busy <= '0';
else
tx_bit_cnt <= tx_bit_cnt + 1;
end if;
when others =>
tx_state <= idle;
end case;
end if;
end if;
end process;
rx_proc: process(clk)
begin
if rising_edge(clk) then
if reset = '1' then
rx_state <= idle;
rxd_d <= (others => '1');
rx_data_i <= (others => '0');
rx_data_valid <= '0';
rx_err <= '0';
rx_brk <= '0';
rx_clk_div <= 0;
rx_bit_cnt <= 0;
else
-- double-latching
rxd_d <= rxd_d(2 downto 0) & rxd;
-- defaults
rx_data_valid <= '0';
rx_err <= '0';
rx_brk <= '0';
case rx_state is
when idle =>
if rxd_d(3) = '1' and rxd_d(2) = '0' then
rx_state <= start;
rx_clk_div <= 0;
end if;
when start =>
-- wait half a bit period
if rx_clk_div = CLK_DIV_MAX / 2 then
-- rxd still low?
if rxd_d(2) = '0' then
rx_state <= data;
rx_clk_div <= 0;
rx_bit_cnt <= 0;
rx_data_i <= (others => '0');
else
-- this was a glitch
rx_state <= idle;
rx_clk_div <= 0;
end if;
else
rx_clk_div <= rx_clk_div + 1;
end if;
when data =>
-- wait a full bit period
if rx_clk_div = CLK_DIV_MAX then
rx_clk_div <= 0;
rx_bit_cnt <= rx_bit_cnt + 1;
rx_data_i <= rxd_d(2) & rx_data_i(7 downto 1);
if rx_bit_cnt = 7 then
rx_state <= stop1;
end if;
else
rx_clk_div <= rx_clk_div + 1;
end if;
when stop1 =>
-- wait a full bit period
if rx_clk_div = CLK_DIV_MAX then
rx_clk_div <= 0;
rx_bit_cnt <= rx_bit_cnt + 1;
if rxd_d(2) = '1' then
-- valid word received
rx_state <= idle;
rx_data_valid <= '1';
elsif rx_data_i /= x"00" then
-- non-zero bits received but no stop bit -> framing error
rx_state <= idle;
rx_err <= '1';
else
-- all zeros received, start of break?
rx_state <= break;
end if;
else
rx_clk_div <= rx_clk_div + 1;
end if;
when break =>
if rx_bit_cnt = BRK_LEN - 1 then
-- proper break received
rx_state <= idle;
rx_brk <= '1';
elsif rxd_d(2) = '1' then
-- now we start checking every sample
rx_state <= idle;
rx_err <= '1';
elsif rx_clk_div = CLK_DIV_MAX then
rx_clk_div <= 0;
rx_bit_cnt <= rx_bit_cnt + 1;
else
rx_clk_div <= rx_clk_div + 1;
end if;
when others =>
rx_state <= idle;
end case;
end if;
end if;
end process;
rx_data <= rx_data_i;
end;