-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.vhd
More file actions
162 lines (143 loc) · 5.74 KB
/
Copy pathuart.vhd
File metadata and controls
162 lines (143 loc) · 5.74 KB
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
--------------------------------------------------------------------------------
-- PROJECT: SIMPLE UART FOR FPGA
--------------------------------------------------------------------------------
-- MODULE: UART TOP MODULE
-- AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
-- LICENSE: The MIT License (MIT), please read LICENSE file
-- WEBSITE: https://github.com/jakubcabal/uart_for_fpga
--------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use IEEE.MATH_REAL.ALL;
-- UART FOR FPGA REQUIRES: 1 START BIT, 8 DATA BITS, 1 STOP BIT!!!
-- OTHER PARAMETERS CAN BE SET USING GENERICS.
entity UART is
Generic (
CLK_FREQ : integer := 100e6; -- system clock frequency in Hz
BAUD_RATE : integer := 9600; -- baud rate value
PARITY_BIT : string := "none"; -- type of parity: "none", "even", "odd", "mark", "space"
USE_DEBOUNCER : boolean := True -- enable/disable debouncer
);
Port (
CLK : in std_logic; -- system clock
RST : in std_logic; -- high active synchronous reset
-- UART INTERFACE
UART_TXD : out std_logic; -- serial transmit data
UART_RXD : in std_logic; -- serial receive data
-- USER DATA INPUT INTERFACE
DATA_IN : in std_logic_vector(7 downto 0); -- input data
DATA_SEND : in std_logic; -- when DATA_SEND = 1, input data are valid and will be transmit
BUSY : out std_logic; -- when BUSY = 1, transmitter is busy and you must not set DATA_SEND to 1
-- USER DATA OUTPUT INTERFACE
DATA_OUT : out std_logic_vector(7 downto 0); -- output data
DATA_VLD : out std_logic; -- when DATA_VLD = 1, output data are valid
FRAME_ERROR : out std_logic -- when FRAME_ERROR = 1, stop bit was invalid
);
end UART;
architecture FULL of UART is
constant DIVIDER_VALUE : integer := CLK_FREQ/(16*BAUD_RATE);
constant CLK_CNT_WIDTH : integer := integer(ceil(log2(real(DIVIDER_VALUE))));
constant CLK_CNT_MAX : unsigned := to_unsigned(DIVIDER_VALUE-1, CLK_CNT_WIDTH);
signal uart_clk_cnt : unsigned(CLK_CNT_WIDTH-1 downto 0);
signal uart_clk_en : std_logic;
signal uart_rxd_shreg : std_logic_vector(3 downto 0);
signal uart_rxd_debounced : std_logic;
begin
-- -------------------------------------------------------------------------
-- UART CLOCK COUNTER AND CLOCK ENABLE FLAG
-- -------------------------------------------------------------------------
uart_clk_cnt_p : process (CLK)
begin
if (rising_edge(CLK)) then
if (RST = '1') then
uart_clk_cnt <= (others => '0');
else
if (uart_clk_cnt = CLK_CNT_MAX) then
uart_clk_cnt <= (others => '0');
else
uart_clk_cnt <= uart_clk_cnt + 1;
end if;
end if;
end if;
end process;
uart_clk_en_reg_p : process (CLK)
begin
if (rising_edge(CLK)) then
if (RST = '1') then
uart_clk_en <= '0';
elsif (uart_clk_cnt = CLK_CNT_MAX) then
uart_clk_en <= '1';
else
uart_clk_en <= '0';
end if;
end if;
end process;
-- -------------------------------------------------------------------------
-- UART RXD SHIFT REGISTER AND DEBAUNCER
-- -------------------------------------------------------------------------
use_debouncer_g : if (USE_DEBOUNCER = True) generate
uart_rxd_shreg_p : process (CLK)
begin
if (rising_edge(CLK)) then
if (RST = '1') then
uart_rxd_shreg <= (others => '1');
else
uart_rxd_shreg <= UART_RXD & uart_rxd_shreg(3 downto 1);
end if;
end if;
end process;
uart_rxd_debounced_reg_p : process (CLK)
begin
if (rising_edge(CLK)) then
if (RST = '1') then
uart_rxd_debounced <= '1';
else
uart_rxd_debounced <= uart_rxd_shreg(0) OR
uart_rxd_shreg(1) OR
uart_rxd_shreg(2) OR
uart_rxd_shreg(3);
end if;
end if;
end process;
end generate;
not_use_debouncer_g : if (USE_DEBOUNCER = False) generate
uart_rxd_debounced <= UART_RXD;
end generate;
-- -------------------------------------------------------------------------
-- UART TRANSMITTER
-- -------------------------------------------------------------------------
uart_tx_i: entity work.UART_TX
generic map (
PARITY_BIT => PARITY_BIT
)
port map (
CLK => CLK,
RST => RST,
-- UART INTERFACE
UART_CLK_EN => uart_clk_en,
UART_TXD => UART_TXD,
-- USER DATA INPUT INTERFACE
DATA_IN => DATA_IN,
DATA_SEND => DATA_SEND,
BUSY => BUSY
);
-- -------------------------------------------------------------------------
-- UART RECEIVER
-- -------------------------------------------------------------------------
uart_rx_i: entity work.UART_RX
generic map (
PARITY_BIT => PARITY_BIT
)
port map (
CLK => CLK,
RST => RST,
-- UART INTERFACE
UART_CLK_EN => uart_clk_en,
UART_RXD => uart_rxd_debounced,
-- USER DATA OUTPUT INTERFACE
DATA_OUT => DATA_OUT,
DATA_VLD => DATA_VLD,
FRAME_ERROR => FRAME_ERROR
);
end FULL;