-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.vhd
162 lines (136 loc) · 3.15 KB
/
uart.vhd
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
---------------------------------------
---------------------------------------
-- UART algorithm with VHDL --
-- Engineer: Sajad Hamzenejadi --
-- 2018 --
---------------------------------------
---------------------------------------
library ieee;
use ieee.std_logic_1164.all;
-- UART --
entity uart is
generic (
divisor : integer := 434 );
port (
clk : in std_logic;
reset : in std_logic;
--
txdata : in std_logic_vector(7 downto 0);
rxdata : out std_logic_vector(7 downto 0);
wr : in std_logic;
rd : in std_logic;
tx_avail : out std_logic;
tx_busy : out std_logic;
rx_avail : out std_logic;
rx_full : out std_logic;
rx_error : out std_logic;
--
uart_rxd : in std_logic;
uart_txd : out std_logic );
end uart;
-- implementation --
architecture rtl of uart is
-- component --
component uart_rx is
generic (
fullbit : integer );
port (
clk : in std_logic;
reset : in std_logic;
--
dout : out std_logic_vector(7 downto 0);
avail : out std_logic;
error : out std_logic;
clear : in std_logic;
--
rxd : in std_logic );
end component;
component uart_tx is
generic (
fullbit : integer );
port (
clk : in std_logic;
reset : in std_logic;
--
din : in std_logic_vector(7 downto 0);
wr : in std_logic;
busy : out std_logic;
--
txd : out std_logic );
end component;
-- local signals --
signal utx_busy : std_logic;
signal utx_wr : std_logic;
signal urx_dout : std_logic_vector(7 downto 0);
signal urx_avail : std_logic;
signal urx_clear : std_logic;
signal urx_error : std_logic;
signal txbuf : std_logic_vector(7 downto 0);
signal txbuf_full : std_logic;
signal rxbuf : std_logic_vector(7 downto 0);
signal rxbuf_full : std_logic;
begin
iotxproc: process(clk, reset) is
begin
if reset='1' then
utx_wr <= '0';
txbuf_full <= '0';
urx_clear <= '0';
rxbuf_full <= '0';
elsif clk'event and clk='1' then
-- TX Buffer Logic --
if wr='1' then
txbuf <= txdata;
txbuf_full <= '1';
end if;
if txbuf_full='1' and utx_busy='0' then
utx_wr <= '1';
txbuf_full <= '0';
else
utx_wr <= '0';
end if;
-- RX Buffer Logic --
if rd='1' then
rxbuf_full <= '0';
end if;
if urx_avail='1' and rxbuf_full='0' then
rxbuf <= urx_dout;
rxbuf_full <= '1';
urx_clear <= '1';
else
urx_clear <= '0';
end if;
end if;
end process;
uart_rx0: uart_rx
generic map (
fullbit => divisor )
port map (
clk => clk,
reset => reset,
--
dout => urx_dout,
avail => urx_avail,
error => urx_error,
clear => urx_clear,
--
rxd => uart_rxd );
uart_tx0: uart_tx
generic map (
fullbit => divisor )
port map (
clk => clk,
reset => reset,
--
din => txbuf,
wr => utx_wr,
busy => utx_busy,
--
txd => uart_txd );
rxdata <= rxbuf;
rx_avail <= rxbuf_full and not rd;
rx_full <= rxbuf_full and urx_avail and not rd;
rx_error <= urx_error;
tx_busy <= utx_busy or txbuf_full or wr;
tx_avail <= not txbuf_full;
end rtl;