1 |
207 |
jshamlet |
-- Copyright (c)2006, 2016, 2019 Jeremy Seth Henry
|
2 |
|
|
-- All rights reserved.
|
3 |
|
|
--
|
4 |
|
|
-- Redistribution and use in source and binary forms, with or without
|
5 |
|
|
-- modification, are permitted provided that the following conditions are met:
|
6 |
|
|
-- * Redistributions of source code must retain the above copyright
|
7 |
|
|
-- notice, this list of conditions and the following disclaimer.
|
8 |
|
|
-- * Redistributions in binary form must reproduce the above copyright
|
9 |
|
|
-- notice, this list of conditions and the following disclaimer in the
|
10 |
|
|
-- documentation and/or other materials provided with the distribution,
|
11 |
|
|
-- where applicable (as part of a user interface, debugging port, etc.)
|
12 |
|
|
--
|
13 |
|
|
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
|
14 |
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
15 |
|
|
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
16 |
|
|
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
|
17 |
|
|
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
18 |
|
|
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
19 |
|
|
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
20 |
|
|
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
21 |
220 |
jshamlet |
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
22 |
|
|
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
23 |
207 |
jshamlet |
--
|
24 |
|
|
-- VHDL Units : async_ser_rx
|
25 |
|
|
-- Description: Asynchronous receiver wired for 8[N/E/O]1 data. Parity mode
|
26 |
|
|
-- and bit rate are set with generics.
|
27 |
209 |
jshamlet |
--
|
28 |
|
|
-- Note: The baud rate generator will produce an approximate frequency. The
|
29 |
|
|
-- final bit rate should be within +/- 1% of the true bit rate to
|
30 |
|
|
-- ensure the receiver can successfully receive. With a sufficiently
|
31 |
|
|
-- high core clock, this is generally achievable for common PC serial
|
32 |
|
|
-- data rates.
|
33 |
218 |
jshamlet |
--
|
34 |
|
|
-- Revision History
|
35 |
|
|
-- Author Date Change
|
36 |
|
|
------------------ -------- ---------------------------------------------------
|
37 |
|
|
-- Seth Henry 04/14/20 Code cleanup and revision section added
|
38 |
207 |
jshamlet |
|
39 |
|
|
library ieee;
|
40 |
|
|
use ieee.std_logic_1164.all;
|
41 |
|
|
use ieee.std_logic_unsigned.all;
|
42 |
|
|
use ieee.std_logic_arith.all;
|
43 |
|
|
use ieee.std_logic_misc.all;
|
44 |
|
|
|
45 |
|
|
entity async_ser_rx is
|
46 |
|
|
generic(
|
47 |
215 |
jshamlet |
Reset_Level : std_logic;
|
48 |
|
|
Enable_Parity : boolean;
|
49 |
|
|
Parity_Odd_Even_n : std_logic;
|
50 |
|
|
Clock_Divider : integer
|
51 |
207 |
jshamlet |
);
|
52 |
|
|
port(
|
53 |
215 |
jshamlet |
Clock : in std_logic;
|
54 |
|
|
Reset : in std_logic;
|
55 |
|
|
--
|
56 |
|
|
Rx_In : in std_logic;
|
57 |
|
|
--
|
58 |
|
|
Rx_Data : out std_logic_vector(7 downto 0);
|
59 |
|
|
Rx_Valid : out std_logic;
|
60 |
|
|
Rx_PErr : out std_logic
|
61 |
207 |
jshamlet |
);
|
62 |
|
|
end entity;
|
63 |
|
|
|
64 |
|
|
architecture behave of async_ser_rx is
|
65 |
|
|
|
66 |
|
|
-- The ceil_log2 function returns the minimum register width required to
|
67 |
|
|
-- hold the supplied integer.
|
68 |
|
|
function ceil_log2 (x : in natural) return natural is
|
69 |
|
|
variable retval : natural;
|
70 |
|
|
begin
|
71 |
|
|
retval := 1;
|
72 |
|
|
while ((2**retval) - 1) < x loop
|
73 |
|
|
retval := retval + 1;
|
74 |
|
|
end loop;
|
75 |
|
|
return retval;
|
76 |
|
|
end ceil_log2;
|
77 |
|
|
|
78 |
|
|
-- Period of each bit in sub-clocks (subtract one to account for zero)
|
79 |
|
|
constant Half_Per_i : integer := (Clock_Divider / 2) - 1;
|
80 |
|
|
constant Full_Per_i : integer := Clock_Divider - 1;
|
81 |
|
|
constant Baud_Bits : integer := ceil_log2(Full_Per_i);
|
82 |
|
|
|
83 |
|
|
constant HALF_PERIOD : std_logic_vector(Baud_Bits - 1 downto 0) :=
|
84 |
|
|
conv_std_logic_vector(Half_Per_i, Baud_Bits);
|
85 |
|
|
constant FULL_PERIOD : std_logic_vector(Baud_Bits - 1 downto 0) :=
|
86 |
|
|
conv_std_logic_vector(Full_Per_i, Baud_Bits);
|
87 |
|
|
|
88 |
208 |
jshamlet |
signal Rx_Baud_Cntr : std_logic_vector(Baud_Bits - 1 downto 0) :=
|
89 |
|
|
(others => '0');
|
90 |
207 |
jshamlet |
|
91 |
208 |
jshamlet |
signal Rx_In_SR : std_logic_vector(3 downto 0) := x"0";
|
92 |
207 |
jshamlet |
alias Rx_In_Q is Rx_In_SR(3);
|
93 |
|
|
|
94 |
208 |
jshamlet |
signal Rx_Buffer : std_logic_vector(7 downto 0) := x"00";
|
95 |
|
|
signal Rx_Parity : std_logic := '0';
|
96 |
|
|
signal Rx_PErr_int : std_logic := '0';
|
97 |
207 |
jshamlet |
|
98 |
208 |
jshamlet |
signal Rx_State : std_logic_vector(3 downto 0) := x"0";
|
99 |
207 |
jshamlet |
alias Rx_Bit_Sel is Rx_State(2 downto 0);
|
100 |
|
|
|
101 |
|
|
-- State machine definitions
|
102 |
|
|
constant IO_RSV0 : std_logic_vector(3 downto 0) := "1011"; -- B
|
103 |
|
|
constant IO_RSV1 : std_logic_vector(3 downto 0) := "1100"; -- C
|
104 |
|
|
constant IO_STRT : std_logic_vector(3 downto 0) := "1101"; -- D
|
105 |
|
|
constant IO_IDLE : std_logic_vector(3 downto 0) := "1110"; -- E
|
106 |
|
|
constant IO_SYNC : std_logic_vector(3 downto 0) := "1111"; -- F
|
107 |
|
|
constant IO_BIT0 : std_logic_vector(3 downto 0) := "0000"; -- 0
|
108 |
|
|
constant IO_BIT1 : std_logic_vector(3 downto 0) := "0001"; -- 1
|
109 |
|
|
constant IO_BIT2 : std_logic_vector(3 downto 0) := "0010"; -- 2
|
110 |
|
|
constant IO_BIT3 : std_logic_vector(3 downto 0) := "0011"; -- 3
|
111 |
|
|
constant IO_BIT4 : std_logic_vector(3 downto 0) := "0100"; -- 4
|
112 |
|
|
constant IO_BIT5 : std_logic_vector(3 downto 0) := "0101"; -- 5
|
113 |
|
|
constant IO_BIT6 : std_logic_vector(3 downto 0) := "0110"; -- 6
|
114 |
|
|
constant IO_BIT7 : std_logic_vector(3 downto 0) := "0111"; -- 7
|
115 |
|
|
constant IO_PARI : std_logic_vector(3 downto 0) := "1000"; -- 8
|
116 |
|
|
constant IO_STOP : std_logic_vector(3 downto 0) := "1001"; -- 9
|
117 |
|
|
constant IO_DONE : std_logic_vector(3 downto 0) := "1010"; -- A
|
118 |
|
|
|
119 |
|
|
begin
|
120 |
|
|
|
121 |
|
|
Rx_Perr <= Rx_PErr_int;
|
122 |
|
|
|
123 |
|
|
UART_Regs: process( Clock, Reset )
|
124 |
|
|
begin
|
125 |
|
|
if( Reset = Reset_Level )then
|
126 |
|
|
Rx_In_SR <= (others => '0');
|
127 |
|
|
Rx_State <= IO_IDLE;
|
128 |
|
|
Rx_Baud_Cntr <= (others => '0');
|
129 |
|
|
Rx_Buffer <= (others => '0');
|
130 |
|
|
Rx_Parity <= '0';
|
131 |
|
|
Rx_Data <= (others => '0');
|
132 |
|
|
Rx_Valid <= '0';
|
133 |
|
|
Rx_PErr_int <= '0';
|
134 |
|
|
elsif( rising_edge(Clock) )then
|
135 |
|
|
Rx_In_SR <= Rx_In_SR(2 downto 0) & Rx_In;
|
136 |
|
|
|
137 |
|
|
Rx_Valid <= '0';
|
138 |
|
|
case( Rx_State )is
|
139 |
|
|
when IO_STRT =>
|
140 |
|
|
if( Rx_In_Q = '1' )then
|
141 |
|
|
Rx_State <= Rx_State + 1;
|
142 |
|
|
end if;
|
143 |
|
|
|
144 |
|
|
when IO_IDLE =>
|
145 |
|
|
Rx_Baud_Cntr <= HALF_PERIOD;
|
146 |
|
|
Rx_Parity <= Parity_Odd_Even_n;
|
147 |
|
|
if( Rx_In_Q = '0' )then
|
148 |
|
|
Rx_State <= Rx_State + 1;
|
149 |
|
|
end if;
|
150 |
|
|
|
151 |
|
|
when IO_SYNC =>
|
152 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
153 |
|
|
if( Rx_Baud_Cntr = 0)then
|
154 |
|
|
Rx_Baud_Cntr <= FULL_PERIOD;
|
155 |
|
|
Rx_State <= Rx_State + 1;
|
156 |
|
|
if( Rx_In_Q = '1' )then -- RxD going low was spurious
|
157 |
|
|
Rx_State <= IO_IDLE;
|
158 |
|
|
end if;
|
159 |
|
|
end if;
|
160 |
|
|
|
161 |
|
|
when IO_BIT0 | IO_BIT1 | IO_BIT2 | IO_BIT3 |
|
162 |
|
|
IO_BIT4 | IO_BIT5 | IO_BIT6 | IO_BIT7 =>
|
163 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
164 |
|
|
if( Rx_Baud_Cntr = 0 )then
|
165 |
|
|
Rx_Baud_Cntr <= FULL_PERIOD;
|
166 |
|
|
Rx_Buffer(conv_integer(Rx_Bit_Sel)) <= Rx_In_Q;
|
167 |
|
|
if( Enable_Parity )then
|
168 |
|
|
Rx_Parity <= Rx_Parity xor Rx_In_Q;
|
169 |
|
|
Rx_State <= Rx_State + 1;
|
170 |
|
|
else
|
171 |
|
|
Rx_PErr_int <= '0';
|
172 |
|
|
Rx_State <= Rx_State + 2;
|
173 |
|
|
end if;
|
174 |
|
|
end if;
|
175 |
|
|
|
176 |
|
|
when IO_PARI =>
|
177 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
178 |
|
|
if( Rx_Baud_Cntr = 0 )then
|
179 |
|
|
Rx_Baud_Cntr <= FULL_PERIOD;
|
180 |
|
|
Rx_PErr_int <= Rx_Parity xor Rx_In_Q;
|
181 |
|
|
Rx_State <= Rx_State + 1;
|
182 |
|
|
end if;
|
183 |
|
|
|
184 |
|
|
when IO_STOP =>
|
185 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
186 |
|
|
if( Rx_Baud_Cntr = 0 )then
|
187 |
|
|
Rx_State <= Rx_State + 1;
|
188 |
|
|
end if;
|
189 |
|
|
|
190 |
|
|
when IO_DONE =>
|
191 |
|
|
Rx_Data <= Rx_Buffer;
|
192 |
|
|
Rx_Valid <= not Rx_PErr_int;
|
193 |
|
|
Rx_State <= Rx_State + 1;
|
194 |
|
|
|
195 |
|
|
when others =>
|
196 |
|
|
Rx_State <= IO_IDLE;
|
197 |
|
|
|
198 |
|
|
end case;
|
199 |
|
|
|
200 |
|
|
end if;
|
201 |
|
|
end process;
|
202 |
|
|
|
203 |
|
|
end architecture;
|