1 |
2 |
jakubcabal |
--------------------------------------------------------------------------------
|
2 |
|
|
-- PROJECT: SIMPLE UART FOR FPGA
|
3 |
|
|
--------------------------------------------------------------------------------
|
4 |
|
|
-- MODULE: UART TOP MODULE
|
5 |
|
|
-- AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
|
6 |
|
|
-- lICENSE: The MIT License (MIT)
|
7 |
|
|
-- WEBSITE: https://github.com/jakubcabal/uart_for_fpga
|
8 |
|
|
--------------------------------------------------------------------------------
|
9 |
|
|
|
10 |
|
|
library IEEE;
|
11 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
12 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
13 |
|
|
|
14 |
|
|
-- UART FOR FPGA REQUIRES: 1 START BIT, 8 DATA BITS, 1 STOP BIT!!!
|
15 |
|
|
-- OTHER PARAMETERS CAN BE SET USING GENERICS.
|
16 |
|
|
|
17 |
|
|
entity UART is
|
18 |
|
|
Generic (
|
19 |
|
|
CLK_FREQ : integer := 50e6; -- set system clock frequency in Hz
|
20 |
|
|
BAUD_RATE : integer := 115200; -- baud rate value
|
21 |
|
|
PARITY_BIT : string := "none" -- legal values: "none", "even", "odd", "mark", "space"
|
22 |
|
|
);
|
23 |
|
|
Port (
|
24 |
|
|
CLK : in std_logic; -- system clock
|
25 |
|
|
RST : in std_logic; -- high active synchronous reset
|
26 |
|
|
-- UART INTERFACE
|
27 |
|
|
UART_TXD : out std_logic;
|
28 |
|
|
UART_RXD : in std_logic;
|
29 |
|
|
-- USER DATA INPUT INTERFACE
|
30 |
|
|
DATA_IN : in std_logic_vector(7 downto 0);
|
31 |
|
|
DATA_SEND : in std_logic; -- when DATA_SEND = 1, data on DATA_IN will be transmit, DATA_SEND can set to 1 only when BUSY = 0
|
32 |
|
|
BUSY : out std_logic; -- when BUSY = 1 transiever is busy, you must not set DATA_SEND to 1
|
33 |
|
|
-- USER DATA OUTPUT INTERFACE
|
34 |
|
|
DATA_OUT : out std_logic_vector(7 downto 0);
|
35 |
|
|
DATA_VLD : out std_logic; -- when DATA_VLD = 1, data on DATA_OUT are valid
|
36 |
|
|
FRAME_ERROR : out std_logic -- when FRAME_ERROR = 1, stop bit was invalid, current and next data may be invalid
|
37 |
|
|
);
|
38 |
|
|
end UART;
|
39 |
|
|
|
40 |
|
|
architecture FULL of UART is
|
41 |
|
|
|
42 |
|
|
constant divider_value : integer := CLK_FREQ/(16*BAUD_RATE);
|
43 |
|
|
|
44 |
|
|
signal uart_ticks : integer range 0 to divider_value-1;
|
45 |
|
|
signal uart_clk_en : std_logic;
|
46 |
|
|
signal uart_rxd_shreg : std_logic_vector(3 downto 0);
|
47 |
|
|
signal uart_rxd_debounced : std_logic;
|
48 |
|
|
|
49 |
|
|
begin
|
50 |
|
|
|
51 |
|
|
-- -------------------------------------------------------------------------
|
52 |
|
|
-- UART OVERSAMPLING CLOCK DIVIDER
|
53 |
|
|
-- -------------------------------------------------------------------------
|
54 |
|
|
|
55 |
|
|
uart_oversampling_clk_divider : process (CLK)
|
56 |
|
|
begin
|
57 |
|
|
if (rising_edge(CLK)) then
|
58 |
|
|
if (RST = '1') then
|
59 |
|
|
uart_ticks <= 0;
|
60 |
|
|
uart_clk_en <= '0';
|
61 |
|
|
elsif (uart_ticks = divider_value-1) then
|
62 |
|
|
uart_ticks <= 0;
|
63 |
|
|
uart_clk_en <= '1';
|
64 |
|
|
else
|
65 |
|
|
uart_ticks <= uart_ticks + 1;
|
66 |
|
|
uart_clk_en <= '0';
|
67 |
|
|
end if;
|
68 |
|
|
end if;
|
69 |
|
|
end process;
|
70 |
|
|
|
71 |
|
|
-- -------------------------------------------------------------------------
|
72 |
|
|
-- UART RXD DEBAUNCER
|
73 |
|
|
-- -------------------------------------------------------------------------
|
74 |
|
|
|
75 |
|
|
uart_rxd_debouncer : process (CLK)
|
76 |
|
|
begin
|
77 |
|
|
if (rising_edge(CLK)) then
|
78 |
|
|
if (RST = '1') then
|
79 |
|
|
uart_rxd_shreg <= (others => '1');
|
80 |
|
|
uart_rxd_debounced <= '1';
|
81 |
|
|
else
|
82 |
|
|
uart_rxd_shreg <= UART_RXD & uart_rxd_shreg(3 downto 1);
|
83 |
|
|
uart_rxd_debounced <= uart_rxd_shreg(0) OR
|
84 |
|
|
uart_rxd_shreg(1) OR
|
85 |
|
|
uart_rxd_shreg(2) OR
|
86 |
|
|
uart_rxd_shreg(3);
|
87 |
|
|
end if;
|
88 |
|
|
end if;
|
89 |
|
|
end process;
|
90 |
|
|
|
91 |
|
|
-- -------------------------------------------------------------------------
|
92 |
|
|
-- UART TRANSMITTER
|
93 |
|
|
-- -------------------------------------------------------------------------
|
94 |
|
|
|
95 |
|
|
uart_tx_i: entity work.UART_TX
|
96 |
|
|
generic map (
|
97 |
|
|
PARITY_BIT => PARITY_BIT
|
98 |
|
|
)
|
99 |
|
|
port map (
|
100 |
|
|
CLK => CLK,
|
101 |
|
|
RST => RST,
|
102 |
|
|
-- UART INTERFACE
|
103 |
|
|
UART_CLK_EN => uart_clk_en,
|
104 |
|
|
UART_TXD => UART_TXD,
|
105 |
|
|
-- USER DATA INPUT INTERFACE
|
106 |
|
|
DATA_IN => DATA_IN,
|
107 |
|
|
DATA_SEND => DATA_SEND,
|
108 |
|
|
BUSY => BUSY
|
109 |
|
|
);
|
110 |
|
|
|
111 |
|
|
-- -------------------------------------------------------------------------
|
112 |
|
|
-- UART RECEIVER
|
113 |
|
|
-- -------------------------------------------------------------------------
|
114 |
|
|
|
115 |
|
|
uart_rx_i: entity work.UART_RX
|
116 |
|
|
generic map (
|
117 |
|
|
PARITY_BIT => PARITY_BIT
|
118 |
|
|
)
|
119 |
|
|
port map (
|
120 |
|
|
CLK => CLK,
|
121 |
|
|
RST => RST,
|
122 |
|
|
-- UART INTERFACE
|
123 |
|
|
UART_CLK_EN => uart_clk_en,
|
124 |
|
|
UART_RXD => uart_rxd_debounced,
|
125 |
|
|
-- USER DATA OUTPUT INTERFACE
|
126 |
|
|
DATA_OUT => DATA_OUT,
|
127 |
|
|
DATA_VLD => DATA_VLD,
|
128 |
|
|
FRAME_ERROR => FRAME_ERROR
|
129 |
|
|
);
|
130 |
|
|
|
131 |
|
|
end FULL;
|