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