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 |
321 |
jshamlet |
-- Seth Henry 06/08/23 Fixed issue where parity flag could get stuck high
|
39 |
207 |
jshamlet |
|
40 |
|
|
library ieee;
|
41 |
|
|
use ieee.std_logic_1164.all;
|
42 |
|
|
use ieee.std_logic_unsigned.all;
|
43 |
|
|
use ieee.std_logic_arith.all;
|
44 |
|
|
use ieee.std_logic_misc.all;
|
45 |
|
|
|
46 |
|
|
entity async_ser_rx is
|
47 |
|
|
generic(
|
48 |
215 |
jshamlet |
Reset_Level : std_logic;
|
49 |
|
|
Enable_Parity : boolean;
|
50 |
|
|
Parity_Odd_Even_n : std_logic;
|
51 |
|
|
Clock_Divider : integer
|
52 |
207 |
jshamlet |
);
|
53 |
|
|
port(
|
54 |
215 |
jshamlet |
Clock : in std_logic;
|
55 |
|
|
Reset : in std_logic;
|
56 |
|
|
--
|
57 |
|
|
Rx_In : in std_logic;
|
58 |
|
|
--
|
59 |
|
|
Rx_Data : out std_logic_vector(7 downto 0);
|
60 |
|
|
Rx_Valid : out std_logic;
|
61 |
|
|
Rx_PErr : out std_logic
|
62 |
207 |
jshamlet |
);
|
63 |
|
|
end entity;
|
64 |
|
|
|
65 |
|
|
architecture behave of async_ser_rx is
|
66 |
|
|
|
67 |
|
|
-- The ceil_log2 function returns the minimum register width required to
|
68 |
|
|
-- hold the supplied integer.
|
69 |
|
|
function ceil_log2 (x : in natural) return natural is
|
70 |
|
|
variable retval : natural;
|
71 |
|
|
begin
|
72 |
|
|
retval := 1;
|
73 |
|
|
while ((2**retval) - 1) < x loop
|
74 |
|
|
retval := retval + 1;
|
75 |
|
|
end loop;
|
76 |
|
|
return retval;
|
77 |
|
|
end ceil_log2;
|
78 |
|
|
|
79 |
|
|
-- Period of each bit in sub-clocks (subtract one to account for zero)
|
80 |
|
|
constant Half_Per_i : integer := (Clock_Divider / 2) - 1;
|
81 |
|
|
constant Full_Per_i : integer := Clock_Divider - 1;
|
82 |
|
|
constant Baud_Bits : integer := ceil_log2(Full_Per_i);
|
83 |
|
|
|
84 |
|
|
constant HALF_PERIOD : std_logic_vector(Baud_Bits - 1 downto 0) :=
|
85 |
|
|
conv_std_logic_vector(Half_Per_i, Baud_Bits);
|
86 |
|
|
constant FULL_PERIOD : std_logic_vector(Baud_Bits - 1 downto 0) :=
|
87 |
|
|
conv_std_logic_vector(Full_Per_i, Baud_Bits);
|
88 |
|
|
|
89 |
208 |
jshamlet |
signal Rx_Baud_Cntr : std_logic_vector(Baud_Bits - 1 downto 0) :=
|
90 |
|
|
(others => '0');
|
91 |
207 |
jshamlet |
|
92 |
208 |
jshamlet |
signal Rx_In_SR : std_logic_vector(3 downto 0) := x"0";
|
93 |
207 |
jshamlet |
alias Rx_In_Q is Rx_In_SR(3);
|
94 |
|
|
|
95 |
208 |
jshamlet |
signal Rx_Buffer : std_logic_vector(7 downto 0) := x"00";
|
96 |
|
|
signal Rx_Parity : std_logic := '0';
|
97 |
|
|
signal Rx_PErr_int : std_logic := '0';
|
98 |
207 |
jshamlet |
|
99 |
208 |
jshamlet |
signal Rx_State : std_logic_vector(3 downto 0) := x"0";
|
100 |
207 |
jshamlet |
alias Rx_Bit_Sel is Rx_State(2 downto 0);
|
101 |
|
|
|
102 |
|
|
-- State machine definitions
|
103 |
|
|
constant IO_RSV0 : std_logic_vector(3 downto 0) := "1011"; -- B
|
104 |
|
|
constant IO_RSV1 : std_logic_vector(3 downto 0) := "1100"; -- C
|
105 |
|
|
constant IO_STRT : std_logic_vector(3 downto 0) := "1101"; -- D
|
106 |
|
|
constant IO_IDLE : std_logic_vector(3 downto 0) := "1110"; -- E
|
107 |
|
|
constant IO_SYNC : std_logic_vector(3 downto 0) := "1111"; -- F
|
108 |
|
|
constant IO_BIT0 : std_logic_vector(3 downto 0) := "0000"; -- 0
|
109 |
|
|
constant IO_BIT1 : std_logic_vector(3 downto 0) := "0001"; -- 1
|
110 |
|
|
constant IO_BIT2 : std_logic_vector(3 downto 0) := "0010"; -- 2
|
111 |
|
|
constant IO_BIT3 : std_logic_vector(3 downto 0) := "0011"; -- 3
|
112 |
|
|
constant IO_BIT4 : std_logic_vector(3 downto 0) := "0100"; -- 4
|
113 |
|
|
constant IO_BIT5 : std_logic_vector(3 downto 0) := "0101"; -- 5
|
114 |
|
|
constant IO_BIT6 : std_logic_vector(3 downto 0) := "0110"; -- 6
|
115 |
|
|
constant IO_BIT7 : std_logic_vector(3 downto 0) := "0111"; -- 7
|
116 |
|
|
constant IO_PARI : std_logic_vector(3 downto 0) := "1000"; -- 8
|
117 |
|
|
constant IO_STOP : std_logic_vector(3 downto 0) := "1001"; -- 9
|
118 |
|
|
constant IO_DONE : std_logic_vector(3 downto 0) := "1010"; -- A
|
119 |
|
|
|
120 |
|
|
begin
|
121 |
|
|
|
122 |
|
|
UART_Regs: process( Clock, Reset )
|
123 |
|
|
begin
|
124 |
|
|
if( Reset = Reset_Level )then
|
125 |
|
|
Rx_In_SR <= (others => '0');
|
126 |
|
|
Rx_State <= IO_IDLE;
|
127 |
|
|
Rx_Baud_Cntr <= (others => '0');
|
128 |
|
|
Rx_Buffer <= (others => '0');
|
129 |
|
|
Rx_Parity <= '0';
|
130 |
|
|
Rx_Data <= (others => '0');
|
131 |
|
|
Rx_Valid <= '0';
|
132 |
321 |
jshamlet |
Rx_Perr <= '0';
|
133 |
207 |
jshamlet |
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 |
321 |
jshamlet |
Rx_Perr <= '0';
|
139 |
207 |
jshamlet |
case( Rx_State )is
|
140 |
|
|
when IO_STRT =>
|
141 |
|
|
if( Rx_In_Q = '1' )then
|
142 |
|
|
Rx_State <= Rx_State + 1;
|
143 |
|
|
end if;
|
144 |
|
|
|
145 |
|
|
when IO_IDLE =>
|
146 |
|
|
Rx_Baud_Cntr <= HALF_PERIOD;
|
147 |
295 |
jshamlet |
Rx_Parity <= Parity_Odd_Even_n;
|
148 |
207 |
jshamlet |
if( Rx_In_Q = '0' )then
|
149 |
|
|
Rx_State <= Rx_State + 1;
|
150 |
|
|
end if;
|
151 |
|
|
|
152 |
|
|
when IO_SYNC =>
|
153 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
154 |
|
|
if( Rx_Baud_Cntr = 0)then
|
155 |
|
|
Rx_Baud_Cntr <= FULL_PERIOD;
|
156 |
|
|
Rx_State <= Rx_State + 1;
|
157 |
|
|
if( Rx_In_Q = '1' )then -- RxD going low was spurious
|
158 |
|
|
Rx_State <= IO_IDLE;
|
159 |
|
|
end if;
|
160 |
|
|
end if;
|
161 |
|
|
|
162 |
|
|
when IO_BIT0 | IO_BIT1 | IO_BIT2 | IO_BIT3 |
|
163 |
|
|
IO_BIT4 | IO_BIT5 | IO_BIT6 | IO_BIT7 =>
|
164 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
165 |
|
|
if( Rx_Baud_Cntr = 0 )then
|
166 |
|
|
Rx_Baud_Cntr <= FULL_PERIOD;
|
167 |
|
|
Rx_Buffer(conv_integer(Rx_Bit_Sel)) <= Rx_In_Q;
|
168 |
|
|
if( Enable_Parity )then
|
169 |
|
|
Rx_Parity <= Rx_Parity xor Rx_In_Q;
|
170 |
|
|
Rx_State <= Rx_State + 1;
|
171 |
|
|
else
|
172 |
|
|
Rx_PErr_int <= '0';
|
173 |
|
|
Rx_State <= Rx_State + 2;
|
174 |
|
|
end if;
|
175 |
|
|
end if;
|
176 |
|
|
|
177 |
|
|
when IO_PARI =>
|
178 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
179 |
|
|
if( Rx_Baud_Cntr = 0 )then
|
180 |
|
|
Rx_Baud_Cntr <= FULL_PERIOD;
|
181 |
|
|
Rx_PErr_int <= Rx_Parity xor Rx_In_Q;
|
182 |
|
|
Rx_State <= Rx_State + 1;
|
183 |
|
|
end if;
|
184 |
|
|
|
185 |
|
|
when IO_STOP =>
|
186 |
|
|
Rx_Baud_Cntr <= Rx_Baud_Cntr - 1;
|
187 |
|
|
if( Rx_Baud_Cntr = 0 )then
|
188 |
|
|
Rx_State <= Rx_State + 1;
|
189 |
|
|
end if;
|
190 |
|
|
|
191 |
|
|
when IO_DONE =>
|
192 |
|
|
Rx_Data <= Rx_Buffer;
|
193 |
|
|
Rx_Valid <= not Rx_PErr_int;
|
194 |
321 |
jshamlet |
Rx_Perr <= Rx_PErr_int;
|
195 |
207 |
jshamlet |
Rx_State <= Rx_State + 1;
|
196 |
|
|
|
197 |
|
|
when others =>
|
198 |
|
|
Rx_State <= IO_IDLE;
|
199 |
|
|
|
200 |
|
|
end case;
|
201 |
|
|
|
202 |
|
|
end if;
|
203 |
|
|
end process;
|
204 |
|
|
|
205 |
|
|
end architecture;
|