1 |
2 |
amulder |
------------------------------------------------------------------------
|
2 |
|
|
-- uartcomponent.vhd
|
3 |
|
|
------------------------------------------------------------------------
|
4 |
|
|
-- Author: Dan Pederson
|
5 |
|
|
-- Copyright 2004 Digilent, Inc.
|
6 |
|
|
------------------------------------------------------------------------
|
7 |
|
|
-- Description: This file defines a UART which transfers data to and
|
8 |
|
|
-- from serial and parallel information. It requires two
|
9 |
|
|
-- major processes: receiving and transferring. The
|
10 |
|
|
-- receiving portion reads serially transmitted data, and
|
11 |
|
|
-- converts it into parallel data, while the transferring
|
12 |
|
|
-- portion reads parallel data, and transmits it as serial
|
13 |
|
|
-- data. There are three error signals provided with this
|
14 |
|
|
-- UART. They are frame error, parity error, and overwrite
|
15 |
|
|
-- error signals. This UART is configured to use an ODD
|
16 |
|
|
-- parity bit at a baud rate of 9600.
|
17 |
|
|
--
|
18 |
|
|
------------------------------------------------------------------------
|
19 |
|
|
-- Revision History:
|
20 |
|
|
-- 07/15/04 (DanP) Created
|
21 |
|
|
-- 05/24/05 (DanP) Updated commenting style
|
22 |
|
|
-- 06/06/05 (DanP) Synchronized state machines to fix timing bug
|
23 |
|
|
------------------------------------------------------------------------
|
24 |
|
|
|
25 |
|
|
library IEEE;
|
26 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
27 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
28 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
29 |
|
|
|
30 |
|
|
-------------------------------------------------------------------------
|
31 |
|
|
--
|
32 |
|
|
--Title: UARTcomponent entity
|
33 |
|
|
--
|
34 |
|
|
--Inputs: 7 : RXD
|
35 |
|
|
-- CLK
|
36 |
|
|
-- DBIN
|
37 |
|
|
-- RDA
|
38 |
|
|
-- RD
|
39 |
|
|
-- WR
|
40 |
|
|
-- RST
|
41 |
|
|
--
|
42 |
|
|
--Outputs: 7 : TXD
|
43 |
|
|
-- DBOUT
|
44 |
|
|
-- RDA
|
45 |
|
|
-- TBE
|
46 |
|
|
-- PE
|
47 |
|
|
-- FE
|
48 |
|
|
-- OE
|
49 |
|
|
--
|
50 |
|
|
--Description: This describes the UART component entity. The inputs are
|
51 |
|
|
-- the Pegasus 50 MHz clock, a reset button, The RXD from
|
52 |
|
|
-- the serial cable, an 8-bit data bus from the parallel
|
53 |
|
|
-- port, and Read Data Available (RDA)and Transfer Buffer
|
54 |
|
|
-- Empty(TBE) handshaking signals. The outputs are the TXD
|
55 |
|
|
-- signal for the serial port, an 8-bit data bus for the
|
56 |
|
|
-- parallel port, RDA and TBE handshaking signals, and three
|
57 |
|
|
-- error signals for parity, frame, and overwrite errors.
|
58 |
|
|
--
|
59 |
|
|
-------------------------------------------------------------------------
|
60 |
|
|
entity UARTcomponent is
|
61 |
|
|
Generic (
|
62 |
|
|
--@48MHz
|
63 |
|
|
-- BAUD_DIVIDE_G : integer := 26; --115200 baud
|
64 |
|
|
-- BAUD_RATE_G : integer := 417
|
65 |
|
|
|
66 |
|
|
--@26.6MHz
|
67 |
|
|
BAUD_DIVIDE_G : integer := 14; --115200 baud
|
68 |
|
|
BAUD_RATE_G : integer := 231
|
69 |
|
|
);
|
70 |
|
|
Port (
|
71 |
|
|
TXD : out std_logic := '1'; -- Transmitted serial data output
|
72 |
|
|
RXD : in std_logic; -- Received serial data input
|
73 |
|
|
CLK : in std_logic; -- Clock signal
|
74 |
|
|
DBIN : in std_logic_vector (7 downto 0); -- Input parallel data to be transmitted
|
75 |
|
|
DBOUT : out std_logic_vector (7 downto 0); -- Recevived parallel data output
|
76 |
|
|
RDA : inout std_logic; -- Read Data Available
|
77 |
|
|
TBE : out std_logic := '1'; -- Transfer Buffer Emty
|
78 |
|
|
RD : in std_logic; -- Read Strobe
|
79 |
|
|
WR : in std_logic; -- Write Strobe
|
80 |
|
|
PE : out std_logic; -- Parity error
|
81 |
|
|
FE : out std_logic; -- Frame error
|
82 |
|
|
OE : out std_logic; -- Overwrite error
|
83 |
|
|
RST : in std_logic := '0'); -- Reset signal
|
84 |
|
|
|
85 |
|
|
end UARTcomponent;
|
86 |
|
|
|
87 |
|
|
architecture Behavioral of UARTcomponent is
|
88 |
|
|
|
89 |
|
|
------------------------------------------------------------------------
|
90 |
|
|
-- Local Type and Signal Declarations
|
91 |
|
|
------------------------------------------------------------------------
|
92 |
|
|
|
93 |
|
|
-------------------------------------------------------------------------
|
94 |
|
|
--Title: Local Type Declarations
|
95 |
|
|
--
|
96 |
|
|
--Description: There are two state machines used in this entity. The
|
97 |
|
|
-- rstate is used to synchronize the receiving portion of
|
98 |
|
|
-- the UART, and the tstate is used to synchronize the
|
99 |
|
|
-- sending portion of the UART.
|
100 |
|
|
--
|
101 |
|
|
-------------------------------------------------------------------------
|
102 |
|
|
type rstate is (
|
103 |
|
|
strIdle,
|
104 |
|
|
strEightDelay,
|
105 |
|
|
strGetData,
|
106 |
|
|
strWaitFor0,
|
107 |
|
|
strWaitFor1,
|
108 |
|
|
strCheckStop
|
109 |
|
|
);
|
110 |
|
|
|
111 |
|
|
type tstate is (
|
112 |
|
|
sttIdle,
|
113 |
|
|
sttTransfer,
|
114 |
|
|
sttShift,
|
115 |
|
|
sttDelay,
|
116 |
|
|
sttWaitWrite
|
117 |
|
|
);
|
118 |
|
|
|
119 |
|
|
-------------------------------------------------------------------------
|
120 |
|
|
--
|
121 |
|
|
--Title: Local Signal Declarations
|
122 |
|
|
--
|
123 |
|
|
--Description: The constants and signals used by this entity are
|
124 |
|
|
-- described below:
|
125 |
|
|
--
|
126 |
|
|
-- -baudRate : This is the Baud Rate constant used to
|
127 |
|
|
-- synchronize the Pegasus 50 MHz clock with a
|
128 |
|
|
-- baud rate of 9600. To get this number, divide
|
129 |
|
|
-- 50MHz by 9600.
|
130 |
|
|
-- -baudDivide : This is the Baud Rate divider used to safely
|
131 |
|
|
-- read data transmitted at a baud rate of 9600.
|
132 |
|
|
-- It is simply the above described baudRate
|
133 |
|
|
-- constant divided by 16.
|
134 |
|
|
--
|
135 |
|
|
-- -rdReg : this is the receive holding register
|
136 |
|
|
-- -rdSReg : this is the receive shift register
|
137 |
|
|
-- -tfReg : this is the transfer holding register
|
138 |
|
|
-- -tfSReg : this is the transfer shifting register
|
139 |
|
|
-- -clkDiv : counter used to get rClk
|
140 |
|
|
-- -ctr : used for delay times
|
141 |
|
|
-- -tfCtr : used to delay in the transfer process
|
142 |
|
|
-- -dataCtr : counts the number of read data bits
|
143 |
|
|
-- -parError : parity error bit
|
144 |
|
|
-- -frameError : frame error bit
|
145 |
|
|
-- -CE : clock enable bit for the writing latch
|
146 |
|
|
-- -ctRst : reset for the ctr
|
147 |
|
|
-- -load : load signal used to load the transfer shift
|
148 |
|
|
-- register
|
149 |
|
|
-- -shift : shift signal used to unload the transfer
|
150 |
|
|
-- shift register
|
151 |
|
|
-- -par : represents the parity in the transfer
|
152 |
|
|
-- holding register
|
153 |
|
|
-- -tClkRST : reset for the tfCtr
|
154 |
|
|
-- -rShift : shift signal used to load the receive shift
|
155 |
|
|
-- register
|
156 |
|
|
-- -dataRST : reset for the dataCtr
|
157 |
|
|
-- -dataIncr : signal to increment the dataCtr
|
158 |
|
|
-- -tfIncr : signal to increment the tfCtr
|
159 |
|
|
-- -tDelayCtr : counter used to delay the transfer state
|
160 |
|
|
-- machine.
|
161 |
|
|
-- -tDelayRst : reset signal for the tDelayCtr counter.
|
162 |
|
|
--
|
163 |
|
|
-- The following signals are used by the two state machines
|
164 |
|
|
-- for state control:
|
165 |
|
|
-- -Receive State Machine : strCur, strNext
|
166 |
|
|
-- -Transfer State Machine : sttCur, sttNext
|
167 |
|
|
--
|
168 |
|
|
-------------------------------------------------------------------------
|
169 |
|
|
|
170 |
|
|
-- @26.7MHz
|
171 |
|
|
-- constant baudRate : std_logic_vector(12 downto 0) := "1 0100 0101 1000";
|
172 |
|
|
-- constant baudRate : std_logic_vector(12 downto 0) := conv_std_logic_vector(1406,13); -- 19200
|
173 |
|
|
-- constant baudRate : std_logic_vector(12 downto 0) := conv_std_logic_vector(703,13); -- 38400
|
174 |
|
|
-- constant baudRate : std_logic_vector(12 downto 0) := conv_std_logic_vector(469,13); -- 57600
|
175 |
|
|
-- constant baudRate : std_logic_vector(12 downto 0) := conv_std_logic_vector(417,13); --115200
|
176 |
|
|
|
177 |
|
|
-- @26.7MHz
|
178 |
|
|
-- constant baudDivide : std_logic_vector(8 downto 0) := conv_std_logic_vector(1,9); -- Used for simulation
|
179 |
|
|
-- constant baudDivide : std_logic_vector(8 downto 0) := conv_std_logic_vector(88,9); -- Used for 19 200 baud
|
180 |
|
|
-- constant baudDivide : std_logic_vector(8 downto 0) := conv_std_logic_vector(44,9); -- Used for 38 400 baud
|
181 |
|
|
-- constant baudDivide : std_logic_vector(8 downto 0) := conv_std_logic_vector(29,9); -- Used for 57 600 baud
|
182 |
|
|
-- constant baudDivide : std_logic_vector(8 downto 0) := conv_std_logic_vector(26,9); -- Used for 115 200 baud
|
183 |
|
|
|
184 |
|
|
constant baudRate : std_logic_vector(12 downto 0) := conv_std_logic_vector(BAUD_RATE_G,13); --115200
|
185 |
|
|
constant baudDivide : std_logic_vector(8 downto 0) := conv_std_logic_vector(BAUD_DIVIDE_G-1,9); -- Used for 115 200 baud
|
186 |
|
|
|
187 |
|
|
signal rdReg : std_logic_vector(7 downto 0) := "00000000";
|
188 |
|
|
signal rdSReg : std_logic_vector(9 downto 0) := "1111111111";
|
189 |
|
|
signal tfReg : std_logic_vector(7 downto 0);
|
190 |
|
|
signal tfSReg : std_logic_vector(10 downto 0) := "11111111111";
|
191 |
|
|
signal clkDiv : std_logic_vector(9 downto 0) := "0000000000";
|
192 |
|
|
signal ctr : std_logic_vector(3 downto 0) := "0000";
|
193 |
|
|
signal tfCtr : std_logic_vector(3 downto 0) := "0000";
|
194 |
|
|
signal dataCtr : std_logic_vector(3 downto 0) := "0000";
|
195 |
|
|
signal parError : std_logic;
|
196 |
|
|
signal frameError : std_logic;
|
197 |
|
|
signal CE : std_logic;
|
198 |
|
|
signal ctRst : std_logic := '0';
|
199 |
|
|
signal load : std_logic := '0';
|
200 |
|
|
signal shift : std_logic := '0';
|
201 |
|
|
signal par : std_logic;
|
202 |
|
|
signal tClkRST : std_logic := '0';
|
203 |
|
|
signal rShift : std_logic := '0';
|
204 |
|
|
signal dataRST : std_logic := '0';
|
205 |
|
|
signal dataIncr : std_logic := '0';
|
206 |
|
|
signal tfIncr : std_logic := '0';
|
207 |
|
|
signal tDelayCtr : std_logic_vector (12 downto 0);
|
208 |
|
|
signal tDelayRst : std_logic := '0';
|
209 |
|
|
|
210 |
|
|
signal strCur : rstate := strIdle;
|
211 |
|
|
signal strNext : rstate;
|
212 |
|
|
signal sttCur : tstate := sttIdle;
|
213 |
|
|
signal sttNext : tstate;
|
214 |
|
|
|
215 |
|
|
-------------------------------------------------------------------------
|
216 |
|
|
-- Module Implementation
|
217 |
|
|
-------------------------------------------------------------------------
|
218 |
|
|
begin
|
219 |
|
|
-------------------------------------------------------------------------
|
220 |
|
|
--
|
221 |
|
|
--Title: Initial signal definitions
|
222 |
|
|
--
|
223 |
|
|
--Description: The following lines of code define 4 internal and 1
|
224 |
|
|
-- external signal. The most significant bit of the rdSReg
|
225 |
|
|
-- signifies the frame error bit, so frameError is tied to
|
226 |
|
|
-- that signal. The parError is high if there is a parity
|
227 |
|
|
-- error, so it is set equal to the inverse of rdSReg(8)
|
228 |
|
|
-- XOR-ed with the data bits. In this manner, it can
|
229 |
|
|
-- determine if the parity bit found in rdSReg(8) matches
|
230 |
|
|
-- the data bits. The parallel information output is equal
|
231 |
|
|
-- to rdReg, so DBOUT is set equal to rdReg. Likewise, the
|
232 |
|
|
-- input parallel information is equal to DBIN, so tfReg is
|
233 |
|
|
-- set equal to DBIN. Because the tfSReg is used to shift
|
234 |
|
|
-- out transmitted data, the TXD port is set equal to the
|
235 |
|
|
-- first bit of tfsReg. Finally, the par signal represents
|
236 |
|
|
-- the parity of the data, so par is set to the inverse of
|
237 |
|
|
-- the data bits XOR-ed together. This UART can be changed
|
238 |
|
|
-- to use EVEN parity if the "not" is omitted from the par
|
239 |
|
|
-- definition.
|
240 |
|
|
--
|
241 |
|
|
-------------------------------------------------------------------------
|
242 |
|
|
frameError <= not rdSReg(9);
|
243 |
|
|
parError <= not ( rdSReg(8) xor (((rdSReg(0) xor rdSReg(1)) xor
|
244 |
|
|
(rdSReg(2) xor rdSReg(3))) xor ((rdSReg(4) xor rdSReg(5)) xor
|
245 |
|
|
(rdSReg(6) xor rdSReg(7)))) );
|
246 |
|
|
DBOUT <= rdReg;
|
247 |
|
|
tfReg <= DBIN;
|
248 |
|
|
TXD <= tfsReg(0);
|
249 |
|
|
par <= not ( ((tfReg(0) xor tfReg(1)) xor (tfReg(2) xor tfReg(3))) xor
|
250 |
|
|
((tfReg(4) xor tfReg(5)) xor (tfReg(6) xor tfReg(7))) );
|
251 |
|
|
-------------------------------------------------------------------------
|
252 |
|
|
--
|
253 |
|
|
--Title: Clock Divide counter
|
254 |
|
|
--
|
255 |
|
|
--Description: This process defines clkDiv as a signal that increments
|
256 |
|
|
-- with the clock up until it is either reset by ctRst, or
|
257 |
|
|
-- equals baudDivide. This signal is used to define a
|
258 |
|
|
-- counter called ctr that increments at the rate of the
|
259 |
|
|
-- divided baud rate.
|
260 |
|
|
--
|
261 |
|
|
-------------------------------------------------------------------------
|
262 |
|
|
process (CLK, clkDiv)
|
263 |
|
|
begin
|
264 |
|
|
if (CLK = '1' and CLK'event) then
|
265 |
|
|
if (clkDiv = baudDivide or ctRst = '1') then
|
266 |
|
|
clkDiv <= "0000000000";
|
267 |
|
|
else
|
268 |
|
|
clkDiv <= clkDiv +1;
|
269 |
|
|
end if;
|
270 |
|
|
end if;
|
271 |
|
|
end process;
|
272 |
|
|
-------------------------------------------------------------------------
|
273 |
|
|
--
|
274 |
|
|
--Title: Transfer delay counter
|
275 |
|
|
--
|
276 |
|
|
--Description: This process defines tDelayCtr as a counter that runs
|
277 |
|
|
-- until it equals baudRate, or until it is reset by
|
278 |
|
|
-- tDelayRst. This counter is used to measure delay times
|
279 |
|
|
-- when sending data out on the TXD signal. When the
|
280 |
|
|
-- counter is equal to baudRate, or is reset, it is set
|
281 |
|
|
-- equal to 0.
|
282 |
|
|
--
|
283 |
|
|
-------------------------------------------------------------------------
|
284 |
|
|
process (CLK, tDelayCtr)
|
285 |
|
|
begin
|
286 |
|
|
if (CLK = '1' and CLK'event) then
|
287 |
|
|
if (tDelayCtr = baudRate or tDelayRst = '1') then
|
288 |
|
|
tDelayCtr <= "0000000000000";
|
289 |
|
|
else
|
290 |
|
|
tDelayCtr <= tDelayCtr+1;
|
291 |
|
|
end if;
|
292 |
|
|
end if;
|
293 |
|
|
end process;
|
294 |
|
|
-------------------------------------------------------------------------
|
295 |
|
|
--
|
296 |
|
|
--Title: ctr set up
|
297 |
|
|
--
|
298 |
|
|
--Description: This process sets up ctr, which uses clkDiv to count
|
299 |
|
|
-- increase at a rate needed to properly receive data in
|
300 |
|
|
-- from RXD. If ctRst is strobed, the counter is reset. If
|
301 |
|
|
-- clkDiv is equal to baudDivide, then ctr is incremented
|
302 |
|
|
-- once. This signal is used by the receiving state machine
|
303 |
|
|
-- to measure delay times between RXD reads.
|
304 |
|
|
--
|
305 |
|
|
-------------------------------------------------------------------------
|
306 |
|
|
process (CLK)
|
307 |
|
|
begin
|
308 |
|
|
if CLK = '1' and CLK'Event then
|
309 |
|
|
if ctRst = '1' then
|
310 |
|
|
ctr <= "0000";
|
311 |
|
|
elsif clkDiv = baudDivide then
|
312 |
|
|
ctr <= ctr + 1;
|
313 |
|
|
else
|
314 |
|
|
ctr <= ctr;
|
315 |
|
|
end if;
|
316 |
|
|
end if;
|
317 |
|
|
end process;
|
318 |
|
|
-------------------------------------------------------------------------
|
319 |
|
|
--
|
320 |
|
|
--Title: transfer counter
|
321 |
|
|
--
|
322 |
|
|
--Description: This process makes tfCtr increment whenever the tfIncr
|
323 |
|
|
-- signal is strobed high. If the tClkRst signal is strobed
|
324 |
|
|
-- high, the tfCtr is reset to "0000." This counter is used
|
325 |
|
|
-- to keep track of how many data bits have been
|
326 |
|
|
-- transmitted.
|
327 |
|
|
--
|
328 |
|
|
-------------------------------------------------------------------------
|
329 |
|
|
process (CLK, tClkRST)
|
330 |
|
|
begin
|
331 |
|
|
if (CLK = '1' and CLK'event) then
|
332 |
|
|
if tClkRST = '1' then
|
333 |
|
|
tfCtr <= "0000";
|
334 |
|
|
elsif tfIncr = '1' then
|
335 |
|
|
tfCtr <= tfCtr +1;
|
336 |
|
|
end if;
|
337 |
|
|
end if;
|
338 |
|
|
end process;
|
339 |
|
|
-------------------------------------------------------------------------
|
340 |
|
|
--
|
341 |
|
|
--Title: Error and RDA flag controller
|
342 |
|
|
--
|
343 |
|
|
--Description: This process controls the error flags FE, OE, and PE, as
|
344 |
|
|
-- well as the Read Data Available (RDA) flag. When CE goes
|
345 |
|
|
-- high, it means that data has been read into the rdSReg.
|
346 |
|
|
-- This process then analyzes the read data for errors, sets
|
347 |
|
|
-- rdReg equal to the eight data bits in rdSReg, and flags
|
348 |
|
|
-- RDA to indicate that new data is present in rdReg. FE
|
349 |
|
|
-- and PE are simply equal to the frameError and parError
|
350 |
|
|
-- signals. OE is flagged high if RDA is already high when
|
351 |
|
|
-- CE is strobed. This means that unread data was still in
|
352 |
|
|
-- the rdReg when it was written over with the new data.
|
353 |
|
|
--
|
354 |
|
|
-------------------------------------------------------------------------
|
355 |
|
|
process (CLK, RST, RD, CE)
|
356 |
|
|
begin
|
357 |
|
|
if RD = '1' or RST = '1' then
|
358 |
|
|
FE <= '0';
|
359 |
|
|
OE <= '0';
|
360 |
|
|
RDA <= '0';
|
361 |
|
|
PE <= '0';
|
362 |
|
|
elsif CLK = '1' and CLK'event then
|
363 |
|
|
if CE = '1' then
|
364 |
|
|
FE <= frameError;
|
365 |
|
|
PE <= parError;
|
366 |
|
|
rdReg(7 downto 0) <= rdSReg (7 downto 0);
|
367 |
|
|
if RDA = '1' then
|
368 |
|
|
OE <= '1';
|
369 |
|
|
else
|
370 |
|
|
OE <= '0';
|
371 |
|
|
RDA <= '1';
|
372 |
|
|
end if;
|
373 |
|
|
end if;
|
374 |
|
|
end if;
|
375 |
|
|
end process;
|
376 |
|
|
-------------------------------------------------------------------------
|
377 |
|
|
--
|
378 |
|
|
--Title: Receiving shift register
|
379 |
|
|
--
|
380 |
|
|
--Description: This process controls the receiving shift register
|
381 |
|
|
-- (rdSReg). Whenever rShift is high, implying that data
|
382 |
|
|
-- needs to be shifted in, rdSReg is shifts in RXD to the
|
383 |
|
|
-- most significant bit, while shifting its existing data
|
384 |
|
|
-- right.
|
385 |
|
|
--
|
386 |
|
|
-------------------------------------------------------------------------
|
387 |
|
|
process (CLK, rShift)
|
388 |
|
|
begin
|
389 |
|
|
if CLK = '1' and CLK'Event then
|
390 |
|
|
if rShift = '1' then
|
391 |
|
|
rdSReg <= (RXD & rdSReg(9 downto 1));
|
392 |
|
|
end if;
|
393 |
|
|
end if;
|
394 |
|
|
end process;
|
395 |
|
|
-------------------------------------------------------------------------
|
396 |
|
|
--
|
397 |
|
|
--Title: Incoming Data counter
|
398 |
|
|
--
|
399 |
|
|
--Description: This process controls the dataCtr to keep track of
|
400 |
|
|
-- shifted values into the rdSReg. The dataCtr signal is
|
401 |
|
|
-- incremented once every time dataIncr is strobed high.
|
402 |
|
|
--
|
403 |
|
|
-------------------------------------------------------------------------
|
404 |
|
|
|
405 |
|
|
process (CLK, dataRST)
|
406 |
|
|
begin
|
407 |
|
|
if (CLK = '1' and CLK'event) then
|
408 |
|
|
if dataRST = '1' then
|
409 |
|
|
dataCtr <= "0000";
|
410 |
|
|
elsif dataIncr = '1' then
|
411 |
|
|
dataCtr <= dataCtr +1;
|
412 |
|
|
end if;
|
413 |
|
|
end if;
|
414 |
|
|
end process;
|
415 |
|
|
-------------------------------------------------------------------------
|
416 |
|
|
--
|
417 |
|
|
--Title: Receiving State Machine controller
|
418 |
|
|
--
|
419 |
|
|
--Description: This process takes care of the Receiving state machine
|
420 |
|
|
-- movement. It causes the next state to be evaluated on
|
421 |
|
|
-- each rising edge of CLK. If the RST signal is strobed,
|
422 |
|
|
-- the state is changed to the default starting state,
|
423 |
|
|
-- which is strIdle
|
424 |
|
|
--
|
425 |
|
|
-------------------------------------------------------------------------
|
426 |
|
|
process (CLK, RST)
|
427 |
|
|
begin
|
428 |
|
|
if CLK = '1' and CLK'Event then
|
429 |
|
|
if RST = '1' then -- najj
|
430 |
|
|
strCur <= strIdle;
|
431 |
|
|
else
|
432 |
|
|
strCur <= strNext;
|
433 |
|
|
end if;
|
434 |
|
|
end if;
|
435 |
|
|
end process;
|
436 |
|
|
-------------------------------------------------------------------------
|
437 |
|
|
--
|
438 |
|
|
--Title: Receiving State Machine
|
439 |
|
|
--
|
440 |
|
|
--Description: This process contains all of the next state logic for the
|
441 |
|
|
-- Receiving state machine.
|
442 |
|
|
--
|
443 |
|
|
-------------------------------------------------------------------------
|
444 |
|
|
process (strCur, ctr, RXD, dataCtr)
|
445 |
|
|
begin
|
446 |
|
|
case strCur is
|
447 |
|
|
-------------------------------------------------------------------------
|
448 |
|
|
--
|
449 |
|
|
--Title: strIdle state
|
450 |
|
|
--
|
451 |
|
|
--Description: This state is the idle and startup default stage for the
|
452 |
|
|
-- Receiving state machine. The machine stays in this state
|
453 |
|
|
-- until the RXD signal goes low. When this occurs, the
|
454 |
|
|
-- ctRst signal is strobed to reset ctr for the next state,
|
455 |
|
|
-- which is strEightDelay.
|
456 |
|
|
--
|
457 |
|
|
-------------------------------------------------------------------------
|
458 |
|
|
when strIdle =>
|
459 |
|
|
dataIncr <= '0';
|
460 |
|
|
rShift <= '0';
|
461 |
|
|
dataRst <= '1';
|
462 |
|
|
CE <= '0';
|
463 |
|
|
ctRst <= '1';
|
464 |
|
|
|
465 |
|
|
if RXD = '0' then
|
466 |
|
|
strNext <= strEightDelay;
|
467 |
|
|
else
|
468 |
|
|
strNext <= strIdle;
|
469 |
|
|
end if;
|
470 |
|
|
-------------------------------------------------------------------------
|
471 |
|
|
--
|
472 |
|
|
--Title: strEightDelay state
|
473 |
|
|
--
|
474 |
|
|
--Description: This state simply delays the state machine for eight clock
|
475 |
|
|
-- cycles. This is needed so that the incoming RXD data
|
476 |
|
|
-- signal is read in the middle of each data emission. This
|
477 |
|
|
-- ensures an accurate RXD signal reading. ctr counts from
|
478 |
|
|
-- 0 to 8 to keep track of rClk cycles. When it equals 8
|
479 |
|
|
-- (1000) the next state, strWaitFor0, is loaded. During
|
480 |
|
|
-- this state, the dataRst signal is strobed high to reset
|
481 |
|
|
-- the shift-in data counter (dataCtr).
|
482 |
|
|
--
|
483 |
|
|
-------------------------------------------------------------------------
|
484 |
|
|
when strEightDelay =>
|
485 |
|
|
dataIncr <= '0';
|
486 |
|
|
rShift <= '0';
|
487 |
|
|
dataRst <= '1';
|
488 |
|
|
CE <= '0';
|
489 |
|
|
ctRst <= '0';
|
490 |
|
|
|
491 |
|
|
if ctr(3 downto 0) = "1000" then
|
492 |
|
|
strNext <= strWaitFor0;
|
493 |
|
|
else
|
494 |
|
|
strNext <= strEightDelay;
|
495 |
|
|
end if;
|
496 |
|
|
-------------------------------------------------------------------------
|
497 |
|
|
--
|
498 |
|
|
--Title: strGetData state
|
499 |
|
|
--
|
500 |
|
|
--Description: In this state, the dataIncr and rShift signals are
|
501 |
|
|
-- strobed high for one clock cycle. By doing this, the
|
502 |
|
|
-- rdSReg shift register shifts in RXD once, while the
|
503 |
|
|
-- dataCtr is incremented by one. This state simply
|
504 |
|
|
-- captures the incoming data on RXD into the rdSReg shift
|
505 |
|
|
-- register. The next state loaded is strWaitFor0, which
|
506 |
|
|
-- starts the two delay states needed between data shifts.
|
507 |
|
|
--
|
508 |
|
|
-------------------------------------------------------------------------
|
509 |
|
|
when strGetData =>
|
510 |
|
|
CE <= '0';
|
511 |
|
|
dataRst <= '0';
|
512 |
|
|
ctRst <= '0';
|
513 |
|
|
dataIncr <= '1';
|
514 |
|
|
rShift <= '1';
|
515 |
|
|
|
516 |
|
|
strNext <= strWaitFor0;
|
517 |
|
|
-------------------------------------------------------------------------
|
518 |
|
|
--
|
519 |
|
|
--Title: strWaitFor0 state
|
520 |
|
|
--
|
521 |
|
|
--Description: This state is a delay state, which delays the receive
|
522 |
|
|
-- state machine if not all of the incoming serial data has
|
523 |
|
|
-- not been shifted in yet. If dataCtr does not equal 10
|
524 |
|
|
-- (1010), the state is stayed in until the fourth bit of
|
525 |
|
|
-- ctr is equal to 1. When this happens, half of the delay
|
526 |
|
|
-- has been achieved, and the second delay state is loaded,
|
527 |
|
|
-- which is strWaitFor1. If dataCtr does equal 10 (1010),
|
528 |
|
|
-- all of the needed data has been acquired, so the
|
529 |
|
|
-- strCheckStop state is loaded to check for errors and
|
530 |
|
|
-- reset the receive state machine.
|
531 |
|
|
--
|
532 |
|
|
-------------------------------------------------------------------------
|
533 |
|
|
when strWaitFor0 =>
|
534 |
|
|
CE <= '0';
|
535 |
|
|
dataRst <= '0';
|
536 |
|
|
ctRst <= '0';
|
537 |
|
|
dataIncr <= '0';
|
538 |
|
|
rShift <= '0';
|
539 |
|
|
|
540 |
|
|
if dataCtr = "1010" then
|
541 |
|
|
strNext <= strCheckStop;
|
542 |
|
|
elsif ctr(3) = '0' then
|
543 |
|
|
strNext <= strWaitFor1;
|
544 |
|
|
else
|
545 |
|
|
strNext <= strWaitFor0;
|
546 |
|
|
end if;
|
547 |
|
|
-------------------------------------------------------------------------
|
548 |
|
|
--
|
549 |
|
|
--Title: strEightDelay state
|
550 |
|
|
--
|
551 |
|
|
--Description: This state is much like strWaitFor0, except it waits for
|
552 |
|
|
-- the fourth bit of ctr to equal 1. Once this occurs, the
|
553 |
|
|
-- strGetData state is loaded in order to shift in the next
|
554 |
|
|
-- data bit from RXD. Because strWaitFor0 is the only state
|
555 |
|
|
-- that calls this state, no other signals need to be
|
556 |
|
|
-- checked.
|
557 |
|
|
--
|
558 |
|
|
-------------------------------------------------------------------------
|
559 |
|
|
when strWaitFor1 =>
|
560 |
|
|
CE <= '0';
|
561 |
|
|
dataRst <= '0';
|
562 |
|
|
ctRst <= '0';
|
563 |
|
|
dataIncr <= '0';
|
564 |
|
|
rShift <= '0';
|
565 |
|
|
|
566 |
|
|
if ctr(3) = '0' then
|
567 |
|
|
strNext <= strWaitFor1;
|
568 |
|
|
else
|
569 |
|
|
strNext <= strGetData;
|
570 |
|
|
end if;
|
571 |
|
|
-------------------------------------------------------------------------
|
572 |
|
|
--
|
573 |
|
|
--Title: strCheckStop state
|
574 |
|
|
--
|
575 |
|
|
--Description: This state allows the newly acquired data to be checked
|
576 |
|
|
-- for errors. The CE flag is strobed to start the
|
577 |
|
|
-- previously defined error checking process. This state is
|
578 |
|
|
-- passed straight through to the strIdle state.
|
579 |
|
|
--
|
580 |
|
|
-------------------------------------------------------------------------
|
581 |
|
|
when strCheckStop =>
|
582 |
|
|
dataIncr <= '0';
|
583 |
|
|
rShift <= '0';
|
584 |
|
|
dataRst <= '0';
|
585 |
|
|
ctRst <= '0';
|
586 |
|
|
CE <= '1';
|
587 |
|
|
strNext <= strIdle;
|
588 |
|
|
end case;
|
589 |
|
|
end process;
|
590 |
|
|
-------------------------------------------------------------------------
|
591 |
|
|
--
|
592 |
|
|
--Title: Transfer shift register controller
|
593 |
|
|
--
|
594 |
|
|
--Description: This process uses the load, shift, and clk signals to
|
595 |
|
|
-- control the transfer shift register (tfSReg). Once load
|
596 |
|
|
-- is equal to '1', the tfSReg gets a '1', the parity bit,
|
597 |
|
|
-- the data bits found in tfReg, and a '0'. Under this
|
598 |
|
|
-- format, the shift register can be used to shift out the
|
599 |
|
|
-- appropriate signal to serially transfer the data. The
|
600 |
|
|
-- data is shifted out of the tfSReg whenever shift = '1'.
|
601 |
|
|
--
|
602 |
|
|
-------------------------------------------------------------------------
|
603 |
|
|
process (load, shift, CLK, tfSReg)
|
604 |
|
|
begin
|
605 |
|
|
if CLK = '1' and CLK'Event then
|
606 |
|
|
if load = '1' then
|
607 |
|
|
tfSReg (10 downto 0) <= ('1' & par & tfReg(7 downto 0) &'0');
|
608 |
|
|
elsif shift = '1' then
|
609 |
|
|
tfSReg (10 downto 0) <= ('1' & tfSReg(10 downto 1));
|
610 |
|
|
end if;
|
611 |
|
|
end if;
|
612 |
|
|
end process;
|
613 |
|
|
-------------------------------------------------------------------------
|
614 |
|
|
--
|
615 |
|
|
--Title: Transfer State Machine controller
|
616 |
|
|
--
|
617 |
|
|
--Description: This process takes care of the Transfer state machine
|
618 |
|
|
-- movement. It causes the next state to be evaluated on
|
619 |
|
|
-- each rising edge of CLK. If the RST signal is strobed,
|
620 |
|
|
-- the state is changed to the default starting state, which
|
621 |
|
|
-- is sttIdle.
|
622 |
|
|
--
|
623 |
|
|
-------------------------------------------------------------------------
|
624 |
|
|
process (CLK, RST)
|
625 |
|
|
begin
|
626 |
|
|
if (CLK = '1' and CLK'Event) then
|
627 |
|
|
if RST = '1' then
|
628 |
|
|
sttCur <= sttIdle;
|
629 |
|
|
else
|
630 |
|
|
sttCur <= sttNext;
|
631 |
|
|
end if;
|
632 |
|
|
end if;
|
633 |
|
|
end process;
|
634 |
|
|
-------------------------------------------------------------------------
|
635 |
|
|
--
|
636 |
|
|
--Title: Transfer State Machine
|
637 |
|
|
--
|
638 |
|
|
--Description: This process controls the next state logic in the
|
639 |
|
|
-- transfer state machine. The transfer state machine
|
640 |
|
|
-- controls the shift and load signals that are used to load
|
641 |
|
|
-- and transmit the parallel data in a serial form. It also
|
642 |
|
|
-- controls the Transmit Buffer Empty (TBE) signal that
|
643 |
|
|
-- indicates if the transmit buffer (tfSReg) is in use or
|
644 |
|
|
-- not.
|
645 |
|
|
--
|
646 |
|
|
-------------------------------------------------------------------------
|
647 |
|
|
process (sttCur, tfCtr, WR, tDelayCtr)
|
648 |
|
|
begin
|
649 |
|
|
case sttCur is
|
650 |
|
|
-------------------------------------------------------------------------
|
651 |
|
|
--
|
652 |
|
|
--Title: sttIdle state
|
653 |
|
|
--
|
654 |
|
|
--Description: This state is the idle and startup default stage for the
|
655 |
|
|
-- transfer state machine. The state is stayed in until
|
656 |
|
|
-- the WR signal goes high. Once it goes high, the
|
657 |
|
|
-- sttTransfer state is loaded. The load and shift signals
|
658 |
|
|
-- are held low in the sttIdle state, while the TBE signal
|
659 |
|
|
-- is held high to indicate that the transmit buffer is not
|
660 |
|
|
-- currently in use. Once the idle state is left, the TBE
|
661 |
|
|
-- signal is held low to indicate that the transfer state
|
662 |
|
|
-- machine is using the transmit buffer.
|
663 |
|
|
--
|
664 |
|
|
-------------------------------------------------------------------------
|
665 |
|
|
when sttIdle =>
|
666 |
|
|
TBE <= '1';
|
667 |
|
|
tClkRST <= '0';
|
668 |
|
|
tfIncr <= '0';
|
669 |
|
|
shift <= '0';
|
670 |
|
|
load <= '0';
|
671 |
|
|
tDelayRst <= '1';
|
672 |
|
|
|
673 |
|
|
if WR = '0' then
|
674 |
|
|
sttNext <= sttIdle;
|
675 |
|
|
else
|
676 |
|
|
sttNext <= sttTransfer;
|
677 |
|
|
end if;
|
678 |
|
|
-------------------------------------------------------------------------
|
679 |
|
|
--
|
680 |
|
|
--Title: sttTransfer state
|
681 |
|
|
--
|
682 |
|
|
--Description: This state sets the load, tClkRST, and tDelayRst signals
|
683 |
|
|
-- high, while setting the TBE signal low. The load signal
|
684 |
|
|
-- is set high to load the transfer shift register with the
|
685 |
|
|
-- appropriate data, while the tClkRST and tDelayRst signals
|
686 |
|
|
-- are strobed to reset the tfCtr and tDelayCtr. The next
|
687 |
|
|
-- state loaded is the sttDelay state.
|
688 |
|
|
--
|
689 |
|
|
-------------------------------------------------------------------------
|
690 |
|
|
when sttTransfer =>
|
691 |
|
|
TBE <= '0';
|
692 |
|
|
shift <= '0';
|
693 |
|
|
load <= '1';
|
694 |
|
|
tClkRST <= '1';
|
695 |
|
|
tfIncr <= '0';
|
696 |
|
|
tDelayRst <= '1';
|
697 |
|
|
|
698 |
|
|
sttNext <= sttDelay;
|
699 |
|
|
-------------------------------------------------------------------------
|
700 |
|
|
--
|
701 |
|
|
--Title: sttShift state
|
702 |
|
|
--
|
703 |
|
|
--Description: This state strobes the shift and tfIncr signals high, and
|
704 |
|
|
-- checks the tfCtr to see if enough data has been
|
705 |
|
|
-- transmitted. By strobing the shift and tfIncr signals
|
706 |
|
|
-- high, the tfSReg is shifted, and the tfCtr is incremented
|
707 |
|
|
-- once. If tfCtr does not equal 9 (1001), then not all of
|
708 |
|
|
-- the bits have been transmitted, so the next state loaded
|
709 |
|
|
-- is the sttDelay state. If tfCtr does equal 9, the final
|
710 |
|
|
-- state, sttWaitWrite, is loaded.
|
711 |
|
|
--
|
712 |
|
|
-------------------------------------------------------------------------
|
713 |
|
|
when sttShift =>
|
714 |
|
|
TBE <= '0';
|
715 |
|
|
shift <= '1';
|
716 |
|
|
load <= '0';
|
717 |
|
|
tfIncr <= '1';
|
718 |
|
|
tClkRST <= '0';
|
719 |
|
|
tDelayRst <= '0';
|
720 |
|
|
|
721 |
|
|
if tfCtr = "1010" then
|
722 |
|
|
sttNext <= sttWaitWrite;
|
723 |
|
|
else
|
724 |
|
|
sttNext <= sttDelay;
|
725 |
|
|
end if;
|
726 |
|
|
-------------------------------------------------------------------------
|
727 |
|
|
--
|
728 |
|
|
--Title: sttDelay state
|
729 |
|
|
--
|
730 |
|
|
--Description: This state is responsible for delaying the transfer state
|
731 |
|
|
-- machine between transmissions. All signals are held low
|
732 |
|
|
-- while the tDelayCtr is tested. Once tDelayCtr is equal
|
733 |
|
|
-- to baudRate, the sttShift state is loaded.
|
734 |
|
|
--
|
735 |
|
|
-------------------------------------------------------------------------
|
736 |
|
|
when sttDelay =>
|
737 |
|
|
TBE <= '0';
|
738 |
|
|
shift <= '0';
|
739 |
|
|
load <= '0';
|
740 |
|
|
tClkRst <= '0';
|
741 |
|
|
tfIncr <= '0';
|
742 |
|
|
tDelayRst <= '0';
|
743 |
|
|
|
744 |
|
|
if tDelayCtr = baudRate then
|
745 |
|
|
sttNext <= sttShift;
|
746 |
|
|
else
|
747 |
|
|
sttNext <= sttDelay;
|
748 |
|
|
end if;
|
749 |
|
|
-------------------------------------------------------------------------
|
750 |
|
|
--
|
751 |
|
|
--Title: sttWaitWrite state
|
752 |
|
|
--
|
753 |
|
|
--Description: This state checks to make sure that the initial WR signal
|
754 |
|
|
-- that triggered the transfer state machine has been
|
755 |
|
|
-- brought back low. Without this state, a write signal
|
756 |
|
|
-- that is held high for a long time will result in multiple
|
757 |
|
|
-- transmissions. Once the WR signal is low, the sttIdle
|
758 |
|
|
-- state is loaded to reset the transfer state machine.
|
759 |
|
|
--
|
760 |
|
|
-------------------------------------------------------------------------
|
761 |
|
|
when sttWaitWrite =>
|
762 |
|
|
TBE <= '0';
|
763 |
|
|
shift <= '0';
|
764 |
|
|
load <= '0';
|
765 |
|
|
tClkRst <= '0';
|
766 |
|
|
tfIncr <= '0';
|
767 |
|
|
tDelayRst <= '0';
|
768 |
|
|
|
769 |
|
|
if WR = '1' then
|
770 |
|
|
sttNext <= sttWaitWrite;
|
771 |
|
|
else
|
772 |
|
|
sttNext <= sttIdle;
|
773 |
|
|
end if;
|
774 |
|
|
end case;
|
775 |
|
|
end process;
|
776 |
|
|
end Behavioral;
|