Debian Codegraph @

hdl/fpga/ip/nuand/command_uart/vhdl/command_uart.vhd

raw
1 -- Copyright (c) 2015 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 command_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
31 -- RS232 interface
32 rs232_sout : out std_logic ;
33 rs232_sin : in std_logic ;
34
35 -- Avalon-MM interface
36 addr : in std_logic_vector(4 downto 0) ;
37 dout : out std_logic_vector(31 downto 0) ;
38 din : in std_logic_vector(31 downto 0) ;
39 read : in std_logic ;
40 write : in std_logic ;
41 waitreq : out std_logic ;
42 readack : out std_logic ;
43 irq : out std_logic
44 ) ;
45 end entity ; -- command_uart
46
47 architecture arch of command_uart is
48
49 -- TODO: Make this a generic instead of a constant
50 constant CLOCKS_PER_BIT : natural := 20 ;
51
52 -- Send and receive state machine definitions
53 type send_fsm_t is (IDLE, SEND_START, SEND_DATA, SEND_STOP ) ;
54 type receive_fsm_t is (WAIT_FOR_START, CENTER_START, SAMPLE_DATA, WAIT_FOR_STOP) ;
55
56 -- FSM signals
57 signal send_fsm : send_fsm_t ;
58 signal receive_fsm : receive_fsm_t ;
59
60 -- Synchronization signals for asynchronous input
61 signal reg_sin : std_logic ;
62 signal sync_sin : std_logic ;
63
64 signal reg_response : std_logic_vector(127 downto 0) ;
65 signal reg_request : std_logic_vector(127 downto 0) ;
66
67 signal sin_data : std_logic_vector(7 downto 0) ;
68 signal sin_we : std_logic ;
69
70 signal sout_data : std_logic_vector(7 downto 0) ;
71 signal sout_we : std_logic ;
72
73 signal rs232_sout_reg : std_logic;
74
75 signal ack : std_logic ;
76
77 type magics_t is array(natural range <>) of std_logic_vector(7 downto 0) ;
78
79 -- These are all the magic header characters
80 constant magics : magics_t := (
81 std_logic_vector(to_unsigned(character'pos('A'),8)), -- 8x8
82 std_logic_vector(to_unsigned(character'pos('B'),8)), -- 8x16
83 std_logic_vector(to_unsigned(character'pos('C'),8)), -- 8x32
84 std_logic_vector(to_unsigned(character'pos('D'),8)), -- 8x64
85 std_logic_vector(to_unsigned(character'pos('E'),8)), -- 16x64
86 std_logic_vector(to_unsigned(character'pos('K'),8)), -- 32x32
87 std_logic_vector(to_unsigned(character'pos('N'),8)), -- Legacy
88 std_logic_vector(to_unsigned(character'pos('T'),8)), -- Retune
89 std_logic_vector(to_unsigned(character'pos('U'),8)) -- Retune2
90 ) ;
91
92 signal command_in : std_logic ;
93
94 signal control : std_logic_vector(7 downto 0) := (0 => '1', others =>'0') ;
95
96 alias isr_enable is control(0) ;
97
98 begin
99
100 readack <= ack ;
101 waitreq <= not(ack or write) ;
102
103 -- Avalon-MM interface for responses
104 mm_writes : process(clock)
105 begin
106 if( rising_edge(clock) ) then
107 command_in <= '0' ;
108 if( write = '1' ) then
109 case to_integer(unsigned(addr)) is
110 when 0| 1| 2| 3 => reg_response( 31 downto 0) <= din ; command_in <= '1' ;
111 when 4| 5| 6| 7 => reg_response( 63 downto 32) <= din ;
112 when 8| 9|10|11 => reg_response( 95 downto 64) <= din ;
113 when 12|13|14|15 => reg_response(127 downto 96) <= din ;
114 when others => control(7 downto 0) <= din(7 downto 0) ;
115 end case ;
116 end if ;
117 end if ;
118 end process ;
119
120 send_command : process(clock, reset)
121 type state_t is (WAITING_FOR_COMMAND, SEND_BYTE, WAITING_FOR_START, WAITING_FOR_DONE) ;
122 variable state : state_t ;
123 variable count : natural range 0 to 15 := 15 ;
124 begin
125 if( reset = '1' ) then
126 state := WAITING_FOR_COMMAND ;
127 sout_we <= '0' ;
128 elsif( rising_edge(clock) ) then
129 sout_we <= '0' ;
130 sout_data <= reg_response(count*8+7 downto count*8) ;
131 case state is
132 when WAITING_FOR_COMMAND =>
133 if( command_in = '1' ) then
134 state := SEND_BYTE ;
135 count := 0 ;
136 end if ;
137
138 when SEND_BYTE =>
139 sout_we <= '1' ;
140 state := WAITING_FOR_START ;
141
142 when WAITING_FOR_START =>
143 if( send_fsm /= IDLE ) then
144 state := WAITING_FOR_DONE ;
145 end if ;
146
147 when WAITING_FOR_DONE =>
148 if( send_fsm = IDLE ) then
149 if( count < 15 ) then
150 count := count + 1 ;
151 state := SEND_BYTE ;
152 else
153 state := WAITING_FOR_COMMAND ;
154 end if ;
155 end if ;
156 end case ;
157 end if ;
158 end process ;
159
160 -- Avanlon-MM interface for requests
161 mm_reads : process(clock)
162 begin
163 if( rising_edge(clock) ) then
164 ack <= read ;
165 if( read = '1' ) then
166 case to_integer(unsigned(addr)) is
167 when 0| 1| 2| 3 => dout <= reg_request( 31 downto 0) ;
168 when 4| 5| 6| 7 => dout <= reg_request( 63 downto 32) ;
169 when 8| 9|10|11 => dout <= reg_request( 95 downto 64) ;
170 when 12|13|14|15 => dout <= reg_request(127 downto 96) ;
171 when others => dout(7 downto 0) <= control ;
172 end case ;
173 end if ;
174 end if ;
175 end process ;
176
177 receive_command : process(clock, reset)
178 type state_t is (WAIT_FOR_MAGIC, CHECK_MAGIC, RECEIVE_COMMAND, INTERRUPT, WAIT_FOR_READ) ;
179 variable state : state_t ;
180 variable count : natural range 0 to 15 ;
181 begin
182 if( reset = '1' ) then
183 state := WAIT_FOR_MAGIC ;
184 count := 0 ;
185 irq <= '0' ;
186 elsif( rising_edge(clock) ) then
187 if( sin_we = '1' and state /= WAIT_FOR_READ ) then
188 reg_request(count*8+7 downto count*8) <= sin_data ;
189 end if ;
190 case state is
191 when WAIT_FOR_MAGIC =>
192 if( sin_we = '1' ) then
193 state := CHECK_MAGIC ;
194 end if ;
195
196 when CHECK_MAGIC =>
197 for i in magics'range loop
198 if( magics(i) = reg_request(7 downto 0) ) then
199 state := RECEIVE_COMMAND ;
200 count := count + 1 ;
201 end if ;
202 end loop ;
203 if( count = 0 ) then
204 state := WAIT_FOR_MAGIC ;
205 end if ;
206
207 when RECEIVE_COMMAND =>
208 if( sin_we = '1' ) then
209 if( count < 15 ) then
210 count := count + 1 ;
211 else
212 count := 0 ;
213 if( isr_enable = '1' ) then
214 state := INTERRUPT ;
215 else
216 state := WAIT_FOR_MAGIC ;
217 end if ;
218 end if ;
219 end if ;
220
221 when INTERRUPT =>
222 irq <= '1' ;
223 state := WAIT_FOR_READ ;
224
225 when WAIT_FOR_READ =>
226 if( read = '1' ) then
227 irq <= '0' ;
228 state := WAIT_FOR_MAGIC ;
229 end if ;
230 end case ;
231 end if ;
232 end process ;
233
234 -- Synchronize asynchronous inputs
235 reg_sin <= rs232_sin when rising_edge( clock ) ;
236 sync_sin <= reg_sin when rising_edge( clock ) ;
237
238 -- Receive data from the UART and put it in the FIFO
239 receive_from_uart : process(clock, reset)
240 variable bits : std_logic_vector(7 downto 0) ;
241 variable bit_count : natural range 0 to bits'length ;
242 variable clock_count : natural range 0 to CLOCKS_PER_BIT ;
243 variable sin_delay : std_logic ;
244 begin
245 if( reset = '1' ) then
246 receive_fsm <= WAIT_FOR_START ;
247 sin_we <= '0' ;
248 sin_data <= (others =>'0') ;
249 bits := (others =>'0') ;
250 bit_count := 0 ;
251 clock_count := 0 ;
252 sin_delay := '0' ;
253 elsif( rising_edge(clock) ) then
254 sin_we <= '0' ;
255 case receive_fsm is
256 -- Wait for the start signal
257 when WAIT_FOR_START =>
258 if( sync_sin = '0' and sin_delay = '1' ) then
259 receive_fsm <= CENTER_START ;
260 clock_count := CLOCKS_PER_BIT/2-1 ;
261 end if ;
262 sin_delay := sync_sin ;
263
264 -- Center the start bit
265 when CENTER_START =>
266 clock_count := clock_count - 1 ;
267 if( clock_count = 0 ) then
268 receive_fsm <= SAMPLE_DATA ;
269 clock_count := CLOCKS_PER_BIT ;
270 bit_count := bits'length ;
271 end if ;
272
273 -- Sample the bits
274 when SAMPLE_DATA =>
275 clock_count := clock_count - 1 ;
276 if( clock_count = 0 ) then
277 bits := sync_sin & bits(bits'high downto 1) ;
278 bit_count := bit_count - 1 ;
279 clock_count := CLOCKS_PER_BIT ;
280 if( bit_count = 0 ) then
281 receive_fsm <= WAIT_FOR_STOP ;
282 sin_data <= bits ;
283 sin_we <= '1' ;
284 end if ;
285 end if ;
286
287 -- Wait for the stop bit
288 when WAIT_FOR_STOP =>
289 clock_count := clock_count - 1 ;
290 if( clock_count = 0 ) then
291 receive_fsm <= WAIT_FOR_START ;
292 assert sync_sin = '1' report "STOP bit not found!" severity error ;
293 end if ;
294
295 when others =>
296 end case ;
297 end if ;
298 end process ;
299
300 -- Register outputs
301 register_sout : process(clock, reset) is
302 begin
303 if (reset = '1') then
304 rs232_sout <= '1';
305 elsif (rising_edge(clock)) then
306 rs232_sout <= rs232_sout_reg;
307 end if;
308 end process register_sout;
309
310 -- Send data to the UART from the FIFO
311 send_to_uart : process(clock, reset)
312 variable bits : std_logic_vector(7 downto 0) ;
313 variable bit_count : natural range 0 to bits'length ;
314 variable clock_count : natural range 0 to CLOCKS_PER_BIT ;
315 begin
316 if( reset = '1' ) then
317 send_fsm <= IDLE ;
318 rs232_sout_reg <= '1' ;
319 bit_count := 0 ;
320 clock_count := 0 ;
321 bits := (others =>'0') ;
322 elsif( rising_edge(clock) ) then
323 case send_fsm is
324 -- Wait until we have something to send
325 when IDLE =>
326 rs232_sout_reg <= '1' ;
327 if( sout_we = '1' ) then
328 send_fsm <= SEND_START ;
329 bit_count := bits'length ;
330 clock_count := CLOCKS_PER_BIT ;
331 end if ;
332
333 -- Send the start bit
334 when SEND_START =>
335 rs232_sout_reg <= '0' ;
336 clock_count := clock_count - 1 ;
337 if( clock_count = 0 ) then
338 send_fsm <= SEND_DATA ;
339 clock_count := CLOCKS_PER_BIT ;
340 elsif( clock_count = 9 ) then
341 bits := sout_data ;
342 end if ;
343 -- Send the data
344 when SEND_DATA =>
345 rs232_sout_reg <= bits(0) ;
346 clock_count := clock_count - 1 ;
347 if( clock_count = 0 ) then
348 bits := '0' & bits(bits'high downto 1) ;
349 bit_count := bit_count - 1 ;
350 clock_count := CLOCKS_PER_BIT ;
351 if( bit_count = 0 ) then
352 send_fsm <= SEND_STOP ;
353 end if ;
354 end if ;
355
356 -- Send the stop bit
357 when SEND_STOP =>
358 rs232_sout_reg <= '1' ;
359 clock_count := clock_count - 1 ;
360 if( clock_count = 0 ) then
361 send_fsm <= IDLE ;
362 end if ;
363
364 -- Weird
365 when others =>
366 send_fsm <= IDLE ;
367 rs232_sout_reg <= '1' ;
368 bit_count := 0 ;
369 clock_count := 0 ;
370 bits := (others =>'0') ;
371 end case ;
372 end if ;
373 end process ;
374
375 end architecture ; -- arch