Debian Codegraph @

hdl/fpga/ip/nuand/synthesis/uart.vhd

raw
1 -- Copyright (c) 2013 Nuand LLC
2 --
3 -- Permission is hereby granted, free of charge, to any person obtaining a copy
4 -- of this software and associated documentation files (the "Software"), to deal
5 -- in the Software without restriction, including without limitation the rights
6 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 -- copies of the Software, and to permit persons to whom the Software is
8 -- furnished to do so, subject to the following conditions:
9 --
10 -- The above copyright notice and this permission notice shall be included in
11 -- all copies or substantial portions of the Software.
12 --
13 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 -- THE SOFTWARE.
20
21 library ieee ;
22 use ieee.std_logic_1164.all ;
23 use ieee.numeric_std.all ;
24
25 entity uart is
26 port (
27 -- FPGA internal interface to the TX and RX FIFOs
28 clock : in std_logic ;
29 reset : in std_logic ;
30 enable : in std_logic ;
31
32 -- RS232 interface
33 rs232_rxd : out std_logic ;
34 rs232_txd : in std_logic ;
35
36 -- UART -> FIFO interface
37 txd_we : out std_logic ;
38 txd_full : in std_logic ;
39 txd_data : out std_logic_vector(7 downto 0) ;
40
41 -- FIFO -> UART interface
42 rxd_re : out std_logic ;
43 rxd_empty : in std_logic ;
44 rxd_data : in std_logic_vector(7 downto 0)
45 ) ;
46 end entity ; -- uart
47
48 architecture arch of uart is
49
50 -- TODO: Make this a generic instead of a constant
51 constant CLOCKS_PER_BIT : natural := 10 ;
52
53 -- Send and receive state machine definitions
54 type send_fsm_t is (IDLE, SEND_START, SEND_DATA, SEND_STOP ) ;
55 type receive_fsm_t is (WAIT_FOR_START, CENTER_START, SAMPLE_DATA, WAIT_FOR_STOP) ;
56
57 -- FSM signals
58 signal send_fsm : send_fsm_t ;
59 signal receive_fsm : receive_fsm_t ;
60
61 -- Synchronization signals for asynchronous input
62 signal reg_txd : std_logic ;
63 signal sync_txd : std_logic ;
64
65 begin
66
67 -- Synchronize asynchronous inputs
68 reg_txd <= rs232_txd when rising_edge( clock ) ;
69 sync_txd <= reg_txd when rising_edge( clock ) ;
70
71 -- Receive data from the UART and put it in the FIFO
72 receive_from_uart : process(all)
73 variable bits : std_logic_vector(7 downto 0) ;
74 variable bit_count : natural range 0 to bits'length ;
75 variable clock_count : natural range 0 to CLOCKS_PER_BIT ;
76 variable txd_delay : std_logic ;
77 begin
78 if( reset = '1' ) then
79 receive_fsm <= WAIT_FOR_START ;
80 txd_we <= '0' ;
81 txd_data <= (others =>'0') ;
82 bits := (others =>'0') ;
83 bit_count := 0 ;
84 clock_count := 0 ;
85 txd_delay := '0' ;
86 elsif( rising_edge(clock) ) then
87 if( enable = '0' ) then
88 txd_we <= '0' ;
89 txd_data <= (others =>'0') ;
90 bits := (others =>'0') ;
91 bit_count := 0 ;
92 clock_count := 0 ;
93 txd_delay := '0' ;
94 else
95 txd_we <= '0' ;
96 case receive_fsm is
97 -- Wait for the start signal
98 when WAIT_FOR_START =>
99 if( sync_txd = '0' and txd_delay = '1' ) then
100 receive_fsm <= CENTER_START ;
101 clock_count := CLOCKS_PER_BIT/2-1 ;
102 end if ;
103 txd_delay := sync_txd ;
104
105 -- Center the start bit
106 when CENTER_START =>
107 clock_count := clock_count - 1 ;
108 if( clock_count = 0 ) then
109 receive_fsm <= SAMPLE_DATA ;
110 clock_count := CLOCKS_PER_BIT ;
111 bit_count := bits'length ;
112 end if ;
113
114 -- Sample the bits
115 when SAMPLE_DATA =>
116 clock_count := clock_count - 1 ;
117 if( clock_count = 0 ) then
118 bits := sync_txd & bits(bits'high downto 1) ;
119 bit_count := bit_count - 1 ;
120 clock_count := CLOCKS_PER_BIT ;
121 if( bit_count = 0 ) then
122 receive_fsm <= WAIT_FOR_STOP ;
123 txd_data <= bits ;
124 txd_we <= '1' ;
125 end if ;
126 end if ;
127
128 -- Wait for the stop bit
129 when WAIT_FOR_STOP =>
130 clock_count := clock_count - 1 ;
131 if( clock_count = 0 ) then
132 receive_fsm <= WAIT_FOR_START ;
133 assert sync_txd = '1' report "STOP bit not found!" severity error ;
134 end if ;
135
136 when others =>
137 end case ;
138 end if ;
139 end if ;
140 end process ;
141
142 -- Send data to the UART from the FIFO
143 send_to_uart : process(all)
144 variable bits : std_logic_vector(7 downto 0) ;
145 variable bit_count : natural range 0 to bits'length ;
146 variable clock_count : natural range 0 to CLOCKS_PER_BIT ;
147 begin
148 if( reset = '1' ) then
149 send_fsm <= IDLE ;
150 rxd_re <= '0' ;
151 rs232_rxd <= '1' ;
152 bit_count := 0 ;
153 clock_count := 0 ;
154 bits := (others =>'0') ;
155 elsif( rising_edge(clock) ) then
156 if( enable = '0' ) then
157 send_fsm <= IDLE ;
158 rxd_re <= '0' ;
159 rs232_rxd <= '1' ;
160 bit_count := 0 ;
161 clock_count := 0 ;
162 bits := (others =>'0') ;
163 else
164 rxd_re <= '0' ;
165 case send_fsm is
166 -- Wait until we have something to send
167 when IDLE =>
168 rs232_rxd <= '1' ;
169 if( rxd_empty = '0' ) then
170 send_fsm <= SEND_START ;
171 rxd_re <= '1' ;
172 bit_count := bits'length ;
173 clock_count := CLOCKS_PER_BIT ;
174 end if ;
175
176 -- Send the start bit
177 when SEND_START =>
178 rs232_rxd <= '0' ;
179 -- bits := rxd_data ;
180 clock_count := clock_count - 1 ;
181 if( clock_count = 0 ) then
182 send_fsm <= SEND_DATA ;
183 clock_count := CLOCKS_PER_BIT ;
184 elsif( clock_count = 9 ) then
185 bits := rxd_data ;
186 end if ;
187 -- Send the data
188 when SEND_DATA =>
189 rs232_rxd <= bits(0) ;
190 clock_count := clock_count - 1 ;
191 if( clock_count = 0 ) then
192 bits := '0' & bits(bits'high downto 1) ;
193 bit_count := bit_count - 1 ;
194 clock_count := CLOCKS_PER_BIT ;
195 if( bit_count = 0 ) then
196 send_fsm <= SEND_STOP ;
197 end if ;
198 end if ;
199
200 -- Send the stop bit
201 when SEND_STOP =>
202 rs232_rxd <= '1' ;
203 clock_count := clock_count - 1 ;
204 if( clock_count = 0 ) then
205 send_fsm <= IDLE ;
206 end if ;
207
208 -- Weird
209 when others =>
210 send_fsm <= IDLE ;
211 rs232_rxd <= '1' ;
212 bit_count := 0 ;
213 clock_count := 0 ;
214 bits := (others =>'0') ;
215 end case ;
216 end if ;
217 end if ;
218 end process ;
219
220 end architecture ; -- arch