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