1 |
218 |
jshamlet |
-- Copyright (c)2006, 2016, 2020 Jeremy Seth Henry
|
2 |
207 |
jshamlet |
-- 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_tx
|
25 |
|
|
-- Description: Asynchronous transmitter 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_tx 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 |
|
|
Tx_Data : in std_logic_vector(7 downto 0);
|
57 |
|
|
Tx_Valid : in std_logic;
|
58 |
|
|
--
|
59 |
|
|
Tx_Out : out std_logic;
|
60 |
|
|
Tx_Done : out std_logic
|
61 |
216 |
jshamlet |
);
|
62 |
207 |
jshamlet |
end entity;
|
63 |
|
|
|
64 |
|
|
architecture behave of async_ser_tx is
|
65 |
|
|
|
66 |
208 |
jshamlet |
-- 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 |
207 |
jshamlet |
constant Tick_Base : integer := Clock_Divider - 1;
|
79 |
|
|
constant Tick_Bits : integer := ceil_log2(Tick_Base);
|
80 |
|
|
constant TICK_DIV : std_logic_vector(Tick_Bits - 1 downto 0) :=
|
81 |
|
|
conv_std_logic_vector(Tick_Base, Tick_Bits);
|
82 |
|
|
|
83 |
208 |
jshamlet |
signal Tick_Cntr : std_logic_vector(Tick_Bits - 1 downto 0) :=
|
84 |
|
|
(others => '0');
|
85 |
207 |
jshamlet |
|
86 |
208 |
jshamlet |
signal Tick_Trig : std_logic := '0';
|
87 |
|
|
signal Tx_Enable : std_logic := '0';
|
88 |
|
|
signal Tx_Buffer : std_logic_vector(7 downto 0) := x"00";
|
89 |
|
|
signal Tx_Parity : std_logic := '0';
|
90 |
|
|
signal Tx_State : std_logic_vector(3 downto 0) := x"0";
|
91 |
207 |
jshamlet |
alias Tx_Bit_Sel is Tx_State(2 downto 0);
|
92 |
|
|
|
93 |
|
|
-- State machine definitions
|
94 |
|
|
constant IO_RSV0 : std_logic_vector(3 downto 0) := "1011"; -- B
|
95 |
|
|
constant IO_RSV1 : std_logic_vector(3 downto 0) := "1100"; -- C
|
96 |
|
|
constant IO_RSV2 : std_logic_vector(3 downto 0) := "1101"; -- D
|
97 |
|
|
constant IO_IDLE : std_logic_vector(3 downto 0) := "1110"; -- E
|
98 |
|
|
constant IO_STRT : std_logic_vector(3 downto 0) := "1111"; -- F
|
99 |
|
|
constant IO_BIT0 : std_logic_vector(3 downto 0) := "0000"; -- 0
|
100 |
|
|
constant IO_BIT1 : std_logic_vector(3 downto 0) := "0001"; -- 1
|
101 |
|
|
constant IO_BIT2 : std_logic_vector(3 downto 0) := "0010"; -- 2
|
102 |
|
|
constant IO_BIT3 : std_logic_vector(3 downto 0) := "0011"; -- 3
|
103 |
|
|
constant IO_BIT4 : std_logic_vector(3 downto 0) := "0100"; -- 4
|
104 |
|
|
constant IO_BIT5 : std_logic_vector(3 downto 0) := "0101"; -- 5
|
105 |
|
|
constant IO_BIT6 : std_logic_vector(3 downto 0) := "0110"; -- 6
|
106 |
|
|
constant IO_BIT7 : std_logic_vector(3 downto 0) := "0111"; -- 7
|
107 |
|
|
constant IO_PARI : std_logic_vector(3 downto 0) := "1000"; -- 8
|
108 |
|
|
constant IO_STOP : std_logic_vector(3 downto 0) := "1001"; -- 9
|
109 |
|
|
constant IO_DONE : std_logic_vector(3 downto 0) := "1010"; -- A
|
110 |
|
|
|
111 |
|
|
begin
|
112 |
|
|
|
113 |
|
|
UART_Regs: process( Clock, Reset )
|
114 |
|
|
begin
|
115 |
|
|
if( Reset = Reset_Level )then
|
116 |
|
|
Tick_Cntr <= (others => '0');
|
117 |
|
|
Tick_Trig <= '0';
|
118 |
|
|
Tx_State <= IO_IDLE;
|
119 |
|
|
Tx_Enable <= '0';
|
120 |
|
|
Tx_Buffer <= (others => '0');
|
121 |
|
|
if( Enable_Parity )then
|
122 |
|
|
Tx_Parity <= '0';
|
123 |
|
|
end if;
|
124 |
|
|
Tx_Out <= '1';
|
125 |
|
|
Tx_Done <= '0';
|
126 |
|
|
elsif( rising_edge(Clock) )then
|
127 |
|
|
Tick_Cntr <= (others => '0');
|
128 |
|
|
Tick_Trig <= '0';
|
129 |
|
|
|
130 |
|
|
if( Tx_Enable = '1' )then
|
131 |
|
|
Tick_Cntr <= Tick_Cntr - 1;
|
132 |
|
|
Tick_Trig <= '0';
|
133 |
|
|
if( or_reduce(Tick_Cntr) = '0' )then
|
134 |
|
|
Tick_Cntr <= TICK_DIV;
|
135 |
|
|
Tick_Trig <= '1';
|
136 |
|
|
end if;
|
137 |
|
|
end if;
|
138 |
|
|
|
139 |
|
|
if( Tx_Valid = '1' )then
|
140 |
|
|
Tx_Buffer <= Tx_Data;
|
141 |
|
|
Tx_Enable <= '1';
|
142 |
|
|
end if;
|
143 |
|
|
|
144 |
|
|
Tx_State <= Tx_State + Tick_Trig;
|
145 |
|
|
Tx_Done <= '0';
|
146 |
|
|
Tx_Out <= '1';
|
147 |
|
|
|
148 |
|
|
case( Tx_State )is
|
149 |
|
|
when IO_IDLE =>
|
150 |
|
|
if( Enable_Parity )then
|
151 |
|
|
Tx_Parity <= Parity_Odd_Even_n;
|
152 |
|
|
end if;
|
153 |
|
|
|
154 |
|
|
when IO_STRT =>
|
155 |
|
|
Tx_Out <= '0';
|
156 |
|
|
|
157 |
|
|
when IO_BIT0 | IO_BIT1 | IO_BIT2 | IO_BIT3 |
|
158 |
|
|
IO_BIT4 | IO_BIT5 | IO_BIT6 | IO_BIT7 =>
|
159 |
|
|
Tx_Out <= Tx_Buffer(conv_integer(Tx_Bit_Sel));
|
160 |
|
|
if( Tick_Trig = '1' and Enable_Parity )then
|
161 |
|
|
Tx_Parity <= Tx_Parity xor Tx_Buffer(conv_integer(Tx_Bit_Sel));
|
162 |
|
|
end if;
|
163 |
|
|
|
164 |
|
|
when IO_PARI =>
|
165 |
|
|
if( Enable_Parity )then
|
166 |
|
|
Tx_Out <= Tx_Parity;
|
167 |
|
|
end if;
|
168 |
|
|
|
169 |
|
|
when IO_STOP =>
|
170 |
|
|
|
171 |
|
|
when IO_DONE =>
|
172 |
|
|
Tx_Done <= '1';
|
173 |
|
|
Tx_Enable <= '0';
|
174 |
|
|
Tx_State <= IO_IDLE;
|
175 |
|
|
|
176 |
|
|
when others =>
|
177 |
|
|
|
178 |
|
|
end case;
|
179 |
|
|
|
180 |
|
|
if( Tx_Enable = '0' )then
|
181 |
|
|
Tx_State <= IO_IDLE;
|
182 |
|
|
end if;
|
183 |
|
|
|
184 |
|
|
end if;
|
185 |
|
|
end process;
|
186 |
|
|
|
187 |
|
|
end architecture;
|