1 |
20 |
magro732 |
-------------------------------------------------------------------------------
|
2 |
|
|
--
|
3 |
|
|
-- RapidIO IP Library Core
|
4 |
|
|
--
|
5 |
|
|
-- This file is part of the RapidIO IP library project
|
6 |
|
|
-- http://www.opencores.org/cores/rio/
|
7 |
|
|
--
|
8 |
|
|
-- Description
|
9 |
|
|
-- This file contains a PCS (Physical Control Sublayer) that can transfer
|
10 |
|
|
-- RapidIO symbols accross a 2Mbit 8-bit UART transmission channel.
|
11 |
|
|
-- The coding is similar to the coding used by PPP and uses flags (0x7e)
|
12 |
|
|
-- and escape-sequences (0x7d) to encode special characters.
|
13 |
|
|
--
|
14 |
|
|
-- To Do:
|
15 |
|
|
-- -
|
16 |
|
|
--
|
17 |
|
|
-- Author(s):
|
18 |
|
|
-- - Magnus Rosenius, magro732@opencores.org
|
19 |
|
|
--
|
20 |
|
|
-------------------------------------------------------------------------------
|
21 |
|
|
--
|
22 |
|
|
-- Copyright (C) 2013 Authors and OPENCORES.ORG
|
23 |
|
|
--
|
24 |
|
|
-- This source file may be used and distributed without
|
25 |
|
|
-- restriction provided that this copyright statement is not
|
26 |
|
|
-- removed from the file and that any derivative work contains
|
27 |
|
|
-- the original copyright notice and the associated disclaimer.
|
28 |
|
|
--
|
29 |
|
|
-- This source file is free software; you can redistribute it
|
30 |
|
|
-- and/or modify it under the terms of the GNU Lesser General
|
31 |
|
|
-- Public License as published by the Free Software Foundation;
|
32 |
|
|
-- either version 2.1 of the License, or (at your option) any
|
33 |
|
|
-- later version.
|
34 |
|
|
--
|
35 |
|
|
-- This source is distributed in the hope that it will be
|
36 |
|
|
-- useful, but WITHOUT ANY WARRANTY; without even the implied
|
37 |
|
|
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
38 |
|
|
-- PURPOSE. See the GNU Lesser General Public License for more
|
39 |
|
|
-- details.
|
40 |
|
|
--
|
41 |
|
|
-- You should have received a copy of the GNU Lesser General
|
42 |
|
|
-- Public License along with this source; if not, download it
|
43 |
|
|
-- from http://www.opencores.org/lgpl.shtml
|
44 |
|
|
--
|
45 |
|
|
-------------------------------------------------------------------------------
|
46 |
|
|
|
47 |
|
|
-------------------------------------------------------------------------------
|
48 |
|
|
-- RioPcsUart
|
49 |
|
|
-------------------------------------------------------------------------------
|
50 |
|
|
library ieee;
|
51 |
|
|
use ieee.std_logic_1164.all;
|
52 |
|
|
use ieee.numeric_std.all;
|
53 |
|
|
use work.rio_common.all;
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
-------------------------------------------------------------------------------
|
57 |
|
|
-- Entity for RioPcsUart.
|
58 |
|
|
-------------------------------------------------------------------------------
|
59 |
|
|
entity RioPcsUart is
|
60 |
|
|
generic(
|
61 |
|
|
DIVISOR_WIDTH : natural);
|
62 |
|
|
port(
|
63 |
|
|
clk : in std_logic;
|
64 |
|
|
areset_n : in std_logic;
|
65 |
|
|
|
66 |
|
|
divisor_i : in std_logic_vector(DIVISOR_WIDTH-1 downto 0);
|
67 |
|
|
|
68 |
|
|
portInitialized_o : out std_logic;
|
69 |
|
|
outboundSymbolEmpty_i : in std_logic;
|
70 |
|
|
outboundSymbolRead_o : out std_logic;
|
71 |
|
|
outboundSymbol_i : in std_logic_vector(33 downto 0);
|
72 |
|
|
inboundSymbolFull_i : in std_logic;
|
73 |
|
|
inboundSymbolWrite_o : out std_logic;
|
74 |
|
|
inboundSymbol_o : out std_logic_vector(33 downto 0);
|
75 |
|
|
|
76 |
|
|
serial_o : out std_logic;
|
77 |
|
|
serial_i : in std_logic);
|
78 |
|
|
end entity;
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
-------------------------------------------------------------------------------
|
82 |
|
|
-- Architecture for RioPcsUart.
|
83 |
|
|
-------------------------------------------------------------------------------
|
84 |
|
|
architecture RioPcsUartImpl of RioPcsUart is
|
85 |
|
|
|
86 |
|
|
component RioSymbolConverter is
|
87 |
|
|
port(
|
88 |
|
|
clk : in std_logic;
|
89 |
|
|
areset_n : in std_logic;
|
90 |
|
|
|
91 |
|
|
portInitialized_o : out std_logic;
|
92 |
|
|
outboundSymbolEmpty_i : in std_logic;
|
93 |
|
|
outboundSymbolRead_o : out std_logic;
|
94 |
|
|
outboundSymbol_i : in std_logic_vector(33 downto 0);
|
95 |
|
|
inboundSymbolFull_i : in std_logic;
|
96 |
|
|
inboundSymbolWrite_o : out std_logic;
|
97 |
|
|
inboundSymbol_o : out std_logic_vector(33 downto 0);
|
98 |
|
|
|
99 |
|
|
uartEmpty_i : in std_logic;
|
100 |
|
|
uartRead_o : out std_logic;
|
101 |
|
|
uartData_i : in std_logic_vector(7 downto 0);
|
102 |
|
|
uartFull_i : in std_logic;
|
103 |
|
|
uartWrite_o : out std_logic;
|
104 |
|
|
uartData_o : out std_logic_vector(7 downto 0));
|
105 |
|
|
end component;
|
106 |
|
|
|
107 |
|
|
component Uart is
|
108 |
|
|
generic(
|
109 |
|
|
DIVISOR_WIDTH : natural;
|
110 |
|
|
DATA_WIDTH : natural);
|
111 |
|
|
port(
|
112 |
|
|
clk : in std_logic;
|
113 |
|
|
areset_n : in std_logic;
|
114 |
|
|
|
115 |
|
|
divisor_i : in std_logic_vector(DIVISOR_WIDTH-1 downto 0);
|
116 |
|
|
|
117 |
|
|
serial_i : in std_logic;
|
118 |
|
|
serial_o : out std_logic;
|
119 |
|
|
|
120 |
|
|
empty_o : out std_logic;
|
121 |
|
|
read_i : in std_logic;
|
122 |
|
|
data_o : out std_logic_vector(DATA_WIDTH-1 downto 0);
|
123 |
|
|
|
124 |
|
|
full_o : out std_logic;
|
125 |
|
|
write_i : in std_logic;
|
126 |
|
|
data_i : in std_logic_vector(DATA_WIDTH-1 downto 0));
|
127 |
|
|
end component;
|
128 |
|
|
|
129 |
|
|
signal uartEmpty : std_logic;
|
130 |
|
|
signal uartRead : std_logic;
|
131 |
|
|
signal uartReadData : std_logic_vector(7 downto 0);
|
132 |
|
|
signal uartFull : std_logic;
|
133 |
|
|
signal uartWrite : std_logic;
|
134 |
|
|
signal uartWriteData : std_logic_vector(7 downto 0);
|
135 |
|
|
|
136 |
|
|
begin
|
137 |
|
|
|
138 |
|
|
SymbolConverter: RioSymbolConverter
|
139 |
|
|
port map(
|
140 |
|
|
clk=>clk, areset_n=>areset_n,
|
141 |
|
|
portInitialized_o=>portInitialized_o,
|
142 |
|
|
outboundSymbolEmpty_i=>outboundSymbolEmpty_i,
|
143 |
|
|
outboundSymbolRead_o=>outboundSymbolRead_o, outboundSymbol_i=>outboundSymbol_i,
|
144 |
|
|
inboundSymbolFull_i=>inboundSymbolFull_i,
|
145 |
|
|
inboundSymbolWrite_o=>inboundSymbolWrite_o, inboundSymbol_o=>inboundSymbol_o,
|
146 |
|
|
uartEmpty_i=>uartEmpty, uartRead_o=>uartRead, uartData_i=>uartReadData,
|
147 |
|
|
uartFull_i=>uartFull, uartWrite_o=>uartWrite, uartData_o=>uartWriteData);
|
148 |
|
|
|
149 |
|
|
UartInst: Uart
|
150 |
|
|
generic map(DIVISOR_WIDTH=>DIVISOR_WIDTH, DATA_WIDTH=>8)
|
151 |
|
|
port map(
|
152 |
|
|
clk=>clk, areset_n=>areset_n,
|
153 |
|
|
divisor_i=>divisor_i,
|
154 |
|
|
serial_i=>serial_i, serial_o=>serial_o,
|
155 |
|
|
empty_o=>uartEmpty, read_i=>uartRead, data_o=>uartReadData,
|
156 |
|
|
full_o=>uartFull, write_i=>uartWrite, data_i=>uartWriteData);
|
157 |
|
|
|
158 |
|
|
end architecture;
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
-------------------------------------------------------------------------------
|
163 |
|
|
-- This module encodes and decodes RapidIO symbols for transmission on a 8-bit
|
164 |
|
|
-- UART using HDLC-like framing (see PPP).
|
165 |
|
|
-- When an idle-symbol is received it will be preceeded by an idle link for a
|
166 |
|
|
-- few micro seconds. This idle link time is used to synchronize the receiver
|
167 |
|
|
-- in the link partner that needs to know when a character is starting and not.
|
168 |
|
|
-------------------------------------------------------------------------------
|
169 |
|
|
library ieee;
|
170 |
|
|
use ieee.std_logic_1164.all;
|
171 |
|
|
use ieee.numeric_std.all;
|
172 |
|
|
use work.rio_common.all;
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
-------------------------------------------------------------------------------
|
176 |
|
|
-- Entity for RioSymbolConverter.
|
177 |
|
|
-------------------------------------------------------------------------------
|
178 |
|
|
entity RioSymbolConverter is
|
179 |
|
|
port(
|
180 |
|
|
clk : in std_logic;
|
181 |
|
|
areset_n : in std_logic;
|
182 |
|
|
|
183 |
|
|
portInitialized_o : out std_logic;
|
184 |
|
|
outboundSymbolEmpty_i : in std_logic;
|
185 |
|
|
outboundSymbolRead_o : out std_logic;
|
186 |
|
|
outboundSymbol_i : in std_logic_vector(33 downto 0);
|
187 |
|
|
inboundSymbolFull_i : in std_logic;
|
188 |
|
|
inboundSymbolWrite_o : out std_logic;
|
189 |
|
|
inboundSymbol_o : out std_logic_vector(33 downto 0);
|
190 |
|
|
|
191 |
|
|
uartEmpty_i : in std_logic;
|
192 |
|
|
uartRead_o : out std_logic;
|
193 |
|
|
uartData_i : in std_logic_vector(7 downto 0);
|
194 |
|
|
uartFull_i : in std_logic;
|
195 |
|
|
uartWrite_o : out std_logic;
|
196 |
|
|
uartData_o : out std_logic_vector(7 downto 0));
|
197 |
|
|
end entity;
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
-------------------------------------------------------------------------------
|
201 |
|
|
-- Architecture for RioSymbolConverter.
|
202 |
|
|
-------------------------------------------------------------------------------
|
203 |
|
|
architecture RioSymbolConverterImpl of RioSymbolConverter is
|
204 |
|
|
|
205 |
|
|
-- Define the flag sequence and the control escape sequence.
|
206 |
|
|
constant FLAG_SEQUENCE : std_logic_vector(7 downto 0) := x"7e";
|
207 |
|
|
constant CONTROL_ESCAPE : std_logic_vector(7 downto 0) := x"7d";
|
208 |
|
|
constant SILENCE_TIME : natural := 4095;
|
209 |
|
|
constant IDLE_SYMBOL_TIME : natural := 256;
|
210 |
|
|
constant LINK_LOST_TIME : natural := 4095;
|
211 |
|
|
|
212 |
|
|
type TxStateType is (STATE_SILENCE, STATE_IDLE_0, STATE_IDLE_1,
|
213 |
|
|
STATE_BUSY_1, STATE_BUSY_2, STATE_SEND_FLAG);
|
214 |
|
|
signal txState : TxStateType;
|
215 |
|
|
signal txStateCounter : unsigned(1 downto 0);
|
216 |
|
|
signal outboundSymbolData : std_logic_vector(7 downto 0);
|
217 |
|
|
|
218 |
|
|
type RxStateType is (STATE_INIT, STATE_NORMAL);
|
219 |
|
|
signal rxState : RxStateType;
|
220 |
|
|
signal rxStateCounter : unsigned(1 downto 0);
|
221 |
|
|
signal escapeFound : std_logic;
|
222 |
|
|
|
223 |
|
|
signal txTimerReset : std_logic;
|
224 |
|
|
signal txTimerEnable : std_logic;
|
225 |
|
|
signal txTimerCounter : unsigned(11 downto 0);
|
226 |
|
|
signal silenceTxTimerDone : std_logic;
|
227 |
|
|
signal idleTxTimerDone : std_logic;
|
228 |
|
|
|
229 |
|
|
signal rxTimerReset : std_logic;
|
230 |
|
|
signal rxTimerEnable : std_logic;
|
231 |
|
|
signal rxTimerCounter : unsigned(11 downto 0);
|
232 |
|
|
signal lostRxTimerDone : std_logic;
|
233 |
|
|
|
234 |
|
|
signal uartWrite : std_logic;
|
235 |
|
|
signal uartRead : std_logic;
|
236 |
|
|
|
237 |
|
|
begin
|
238 |
|
|
|
239 |
|
|
-- Set the port initialized once the receiver enters its normal state.
|
240 |
|
|
portInitialized_o <= '1' when (rxState = STATE_NORMAL) else '0';
|
241 |
|
|
|
242 |
|
|
-----------------------------------------------------------------------------
|
243 |
|
|
-- Timer functionallity.
|
244 |
|
|
-----------------------------------------------------------------------------
|
245 |
|
|
|
246 |
|
|
silenceTxTimerDone <= '1' when (txTimerCounter = SILENCE_TIME) else '0';
|
247 |
|
|
idleTxTimerDone <= '1' when (txTimerCounter = IDLE_SYMBOL_TIME) else '0';
|
248 |
|
|
|
249 |
|
|
process(areset_n, clk)
|
250 |
|
|
begin
|
251 |
|
|
if (areset_n = '0') then
|
252 |
|
|
txTimerCounter <= (others => '0');
|
253 |
|
|
elsif (clk'event and clk = '1') then
|
254 |
|
|
if (txTimerReset = '1') then
|
255 |
|
|
txTimerCounter <= (others => '0');
|
256 |
|
|
elsif (txTimerEnable = '1') then
|
257 |
|
|
txTimerCounter <= txTimerCounter + 1;
|
258 |
|
|
end if;
|
259 |
|
|
end if;
|
260 |
|
|
end process;
|
261 |
|
|
|
262 |
|
|
lostRxTimerDone <= '1' when (rxTimerCounter = LINK_LOST_TIME) else '0';
|
263 |
|
|
|
264 |
|
|
process(areset_n, clk)
|
265 |
|
|
begin
|
266 |
|
|
if (areset_n = '0') then
|
267 |
|
|
rxTimerCounter <= (others => '0');
|
268 |
|
|
elsif (clk'event and clk = '1') then
|
269 |
|
|
if (rxTimerReset = '1') then
|
270 |
|
|
rxTimerCounter <= (others => '0');
|
271 |
|
|
elsif (rxTimerEnable = '1') then
|
272 |
|
|
rxTimerCounter <= rxTimerCounter + 1;
|
273 |
|
|
end if;
|
274 |
|
|
end if;
|
275 |
|
|
end process;
|
276 |
|
|
|
277 |
|
|
-----------------------------------------------------------------------------
|
278 |
|
|
-- Link symbol encoder process.
|
279 |
|
|
-----------------------------------------------------------------------------
|
280 |
|
|
outboundSymbolData <= outboundSymbol_i(31 downto 24) when txStateCounter = 0 else
|
281 |
|
|
outboundSymbol_i(23 downto 16) when txStateCounter = 1 else
|
282 |
|
|
outboundSymbol_i(15 downto 8) when txStateCounter = 2 else
|
283 |
|
|
outboundSymbol_i(7 downto 0);
|
284 |
|
|
|
285 |
|
|
uartWrite_o <= uartWrite;
|
286 |
|
|
Outbound: process(areset_n, clk)
|
287 |
|
|
begin
|
288 |
|
|
if (areset_n = '0') then
|
289 |
|
|
txState <= STATE_SILENCE;
|
290 |
|
|
txStateCounter <= (others => '0');
|
291 |
|
|
|
292 |
|
|
txTimerReset <= '0';
|
293 |
|
|
txTimerEnable <= '0';
|
294 |
|
|
|
295 |
|
|
outboundSymbolRead_o <= '0';
|
296 |
|
|
|
297 |
|
|
uartWrite <= '0';
|
298 |
|
|
uartData_o <= (others => '0');
|
299 |
|
|
elsif (clk'event and clk = '1') then
|
300 |
|
|
txTimerReset <= '0';
|
301 |
|
|
outboundSymbolRead_o <= '0';
|
302 |
|
|
uartWrite <= '0';
|
303 |
|
|
|
304 |
|
|
-- Check if the UART is ready for new data.
|
305 |
|
|
if (uartFull_i = '0') and (uartWrite = '0') then
|
306 |
|
|
-- The UART want new data to transmitt.
|
307 |
|
|
|
308 |
|
|
-- Check the transmission state.
|
309 |
|
|
case txState is
|
310 |
|
|
|
311 |
|
|
when STATE_SILENCE =>
|
312 |
|
|
-------------------------------------------------------------------
|
313 |
|
|
-- Wait for a while to let the linkpartner detect a link break.
|
314 |
|
|
-------------------------------------------------------------------
|
315 |
|
|
-- Check if the silence timer has expired.
|
316 |
|
|
if (silenceTxTimerDone = '1') then
|
317 |
|
|
-- Silence timer expired.
|
318 |
|
|
-- Reset the timer and proceed to transmitting symbols.
|
319 |
|
|
txTimerReset <= '1';
|
320 |
|
|
txTimerEnable <= '0';
|
321 |
|
|
txState <= STATE_IDLE_0;
|
322 |
|
|
else
|
323 |
|
|
txTimerEnable <= '1';
|
324 |
|
|
end if;
|
325 |
|
|
|
326 |
|
|
when STATE_IDLE_0 =>
|
327 |
|
|
-----------------------------------------------------------------
|
328 |
|
|
-- Wait for a new symbol to be received. An idle symbol is followed
|
329 |
|
|
-- by a small inter-character idle time to let the receiver in the
|
330 |
|
|
-- link partner synchronize itself to the link.
|
331 |
|
|
-----------------------------------------------------------------
|
332 |
|
|
|
333 |
|
|
-- Reset the state counter for the symbol generation.
|
334 |
|
|
txStateCounter <= "00";
|
335 |
|
|
|
336 |
|
|
-- Check if a new symbol is available.
|
337 |
|
|
if (outboundSymbolEmpty_i = '0') then
|
338 |
|
|
-- A new symbol is available.
|
339 |
|
|
|
340 |
|
|
-- Check if the new symbol is idle, control or data.
|
341 |
|
|
if (outboundSymbol_i(33 downto 32) /= SYMBOL_IDLE) then
|
342 |
|
|
-- Control or data symbol.
|
343 |
|
|
txState <= STATE_BUSY_1;
|
344 |
|
|
else
|
345 |
|
|
-- Send idle sequence.
|
346 |
|
|
txState <= STATE_IDLE_1;
|
347 |
|
|
end if;
|
348 |
|
|
else
|
349 |
|
|
-- No new symbols are ready.
|
350 |
|
|
-- Dont do anything.
|
351 |
|
|
end if;
|
352 |
|
|
|
353 |
|
|
when STATE_IDLE_1 =>
|
354 |
|
|
-------------------------------------------------------------------
|
355 |
|
|
-- Wait until the idle timer has expired to let the link be idle in
|
356 |
|
|
-- between idle symbols.
|
357 |
|
|
-------------------------------------------------------------------
|
358 |
|
|
|
359 |
|
|
-- Check if the idle timer has expired.
|
360 |
|
|
if (idleTxTimerDone = '1') then
|
361 |
|
|
-- Idle timer has expired.
|
362 |
|
|
-- Reset the timer and disable it.
|
363 |
|
|
txTimerReset <= '1';
|
364 |
|
|
txTimerEnable <= '0';
|
365 |
|
|
|
366 |
|
|
-- Write a flag to indicate idle link.
|
367 |
|
|
uartWrite <= '1';
|
368 |
|
|
uartData_o <= FLAG_SEQUENCE;
|
369 |
|
|
|
370 |
|
|
-- Get a new symbol.
|
371 |
|
|
outboundSymbolRead_o <= '1';
|
372 |
|
|
txState <= STATE_IDLE_0;
|
373 |
|
|
else
|
374 |
|
|
-- Idle timer has not expired yet.
|
375 |
|
|
txTimerEnable <= '1';
|
376 |
|
|
end if;
|
377 |
|
|
|
378 |
|
|
when STATE_BUSY_1 =>
|
379 |
|
|
-----------------------------------------------------------------
|
380 |
|
|
-- Encode a control or data symbol. If stuffing is needed the next
|
381 |
|
|
-- busy state is called.
|
382 |
|
|
-----------------------------------------------------------------
|
383 |
|
|
|
384 |
|
|
-- Check if the octet is a flag or escape character.
|
385 |
|
|
if ((outboundSymbolData = FLAG_SEQUENCE) or
|
386 |
|
|
(outboundSymbolData = CONTROL_ESCAPE)) then
|
387 |
|
|
-- Flag or escape octet.
|
388 |
|
|
uartWrite <= '1';
|
389 |
|
|
uartData_o <= CONTROL_ESCAPE;
|
390 |
|
|
txState <= STATE_BUSY_2;
|
391 |
|
|
else
|
392 |
|
|
-- Ordinary octet.
|
393 |
|
|
|
394 |
|
|
-- Write the octet to the uart.
|
395 |
|
|
uartWrite <= '1';
|
396 |
|
|
uartData_o <= outboundSymbolData;
|
397 |
|
|
|
398 |
|
|
-- Update to the next octet in the symbol.
|
399 |
|
|
txStateCounter <= txStateCounter + 1;
|
400 |
|
|
|
401 |
|
|
-- Check if the symbol has been sent.
|
402 |
|
|
if (txStateCounter = 3) then
|
403 |
|
|
-- Data symbol sent.
|
404 |
|
|
outboundSymbolRead_o <= '1';
|
405 |
|
|
txState <= STATE_IDLE_0;
|
406 |
|
|
elsif ((txStateCounter = 2) and
|
407 |
|
|
(outboundSymbol_i(33 downto 32) /= SYMBOL_DATA)) then
|
408 |
|
|
-- Control symbol sent.
|
409 |
|
|
txState <= STATE_SEND_FLAG;
|
410 |
|
|
else
|
411 |
|
|
-- Symbol not completly sent.
|
412 |
|
|
txState <= STATE_BUSY_1;
|
413 |
|
|
end if;
|
414 |
|
|
end if;
|
415 |
|
|
|
416 |
|
|
when STATE_BUSY_2 =>
|
417 |
|
|
-----------------------------------------------------------------
|
418 |
|
|
-- Byte stuff a flag or escape sequence found in the symbol data
|
419 |
|
|
-- content.
|
420 |
|
|
-----------------------------------------------------------------
|
421 |
|
|
|
422 |
|
|
-- Byte stuff the control character.
|
423 |
|
|
uartWrite <= '1';
|
424 |
|
|
uartData_o <= outboundSymbolData xor x"20";
|
425 |
|
|
|
426 |
|
|
-- Update to the next symbol.
|
427 |
|
|
txStateCounter <= txStateCounter + 1;
|
428 |
|
|
|
429 |
|
|
-- Check if the symbol has been sent.
|
430 |
|
|
if (txStateCounter = 3) then
|
431 |
|
|
-- Data symbol sent.
|
432 |
|
|
outboundSymbolRead_o <= '1';
|
433 |
|
|
txState <= STATE_IDLE_0;
|
434 |
|
|
elsif ((txStateCounter = 2) and
|
435 |
|
|
(outboundSymbol_i(33 downto 32) /= SYMBOL_DATA)) then
|
436 |
|
|
-- Control symbol sent.
|
437 |
|
|
txState <= STATE_SEND_FLAG;
|
438 |
|
|
else
|
439 |
|
|
-- Symbol not completly sent.
|
440 |
|
|
txState <= STATE_BUSY_1;
|
441 |
|
|
end if;
|
442 |
|
|
|
443 |
|
|
when STATE_SEND_FLAG =>
|
444 |
|
|
-----------------------------------------------------------------
|
445 |
|
|
-- Force a flag to be written to the link.
|
446 |
|
|
-----------------------------------------------------------------
|
447 |
|
|
|
448 |
|
|
uartWrite <= '1';
|
449 |
|
|
uartData_o <= FLAG_SEQUENCE;
|
450 |
|
|
outboundSymbolRead_o <= '1';
|
451 |
|
|
txState <= STATE_IDLE_0;
|
452 |
|
|
|
453 |
|
|
when others =>
|
454 |
|
|
-----------------------------------------------------------------
|
455 |
|
|
-- Unknown state.
|
456 |
|
|
-----------------------------------------------------------------
|
457 |
|
|
txState <= STATE_IDLE_0;
|
458 |
|
|
|
459 |
|
|
end case;
|
460 |
|
|
else
|
461 |
|
|
-- The UART is busy transmitting.
|
462 |
|
|
-- Wait for the UART to complete.
|
463 |
|
|
end if;
|
464 |
|
|
end if;
|
465 |
|
|
end process;
|
466 |
|
|
|
467 |
|
|
|
468 |
|
|
-----------------------------------------------------------------------------
|
469 |
|
|
-- Link symbol decoder process.
|
470 |
|
|
-----------------------------------------------------------------------------
|
471 |
|
|
uartRead_o <= uartRead;
|
472 |
|
|
Inbound: process(areset_n, clk)
|
473 |
|
|
begin
|
474 |
|
|
if (areset_n = '0') then
|
475 |
|
|
rxState <= STATE_INIT;
|
476 |
|
|
rxStateCounter <= (others => '0');
|
477 |
|
|
escapeFound <= '0';
|
478 |
|
|
|
479 |
|
|
rxTimerReset <= '0';
|
480 |
|
|
rxTimerEnable <= '0';
|
481 |
|
|
|
482 |
|
|
inboundSymbolWrite_o <= '0';
|
483 |
|
|
inboundSymbol_o <= (others => '0');
|
484 |
|
|
|
485 |
|
|
uartRead <= '0';
|
486 |
|
|
elsif (clk'event and clk = '1') then
|
487 |
|
|
rxTimerReset <= '0';
|
488 |
|
|
inboundSymbolWrite_o <= '0';
|
489 |
|
|
uartRead <= '0';
|
490 |
|
|
|
491 |
|
|
case rxState is
|
492 |
|
|
|
493 |
|
|
when STATE_INIT =>
|
494 |
|
|
-------------------------------------------------------------------
|
495 |
|
|
-- Wait for a flag to be received.
|
496 |
|
|
-------------------------------------------------------------------
|
497 |
|
|
-- Check if any new data is ready.
|
498 |
|
|
if (uartRead = '0') and (uartEmpty_i = '0') then
|
499 |
|
|
-- New data is ready from the uart.
|
500 |
|
|
|
501 |
|
|
-- Check if a flag has been received.
|
502 |
|
|
if (uartData_i = FLAG_SEQUENCE) then
|
503 |
|
|
-- A flag has been received.
|
504 |
|
|
-- Considder the port to be initialized.
|
505 |
|
|
rxState <= STATE_NORMAL;
|
506 |
|
|
rxStateCounter <= (others => '0');
|
507 |
|
|
escapeFound <= '0';
|
508 |
|
|
rxTimerReset <= '1';
|
509 |
|
|
rxTimerEnable <= '1';
|
510 |
|
|
uartRead <= '1';
|
511 |
|
|
else
|
512 |
|
|
-- Something that is not a flag has been received.
|
513 |
|
|
-- Discard the data and wait for a flag.
|
514 |
|
|
uartRead <= '1';
|
515 |
|
|
end if;
|
516 |
|
|
else
|
517 |
|
|
-- Waiting for inbound data.
|
518 |
|
|
-- Dont do anything.
|
519 |
|
|
end if;
|
520 |
|
|
|
521 |
|
|
when STATE_NORMAL =>
|
522 |
|
|
-------------------------------------------------------------------
|
523 |
|
|
-- Parse the incoming stream and create symbols.
|
524 |
|
|
-------------------------------------------------------------------
|
525 |
|
|
|
526 |
|
|
-- Check if the link lost timer has expired.
|
527 |
|
|
if (lostRxTimerDone = '1') then
|
528 |
|
|
-- The link lost timer has expired.
|
529 |
|
|
-- Reset the timer, disable it and go back to the initial state.
|
530 |
|
|
rxTimerReset <= '1';
|
531 |
|
|
rxTimerEnable <= '0';
|
532 |
|
|
rxState <= STATE_INIT;
|
533 |
|
|
else
|
534 |
|
|
-- The link lost timer has not expired.
|
535 |
|
|
|
536 |
|
|
-- Check if any new data is ready.
|
537 |
|
|
if (uartRead = '0') and (uartEmpty_i = '0') then
|
538 |
|
|
-- New data is ready from the uart.
|
539 |
|
|
|
540 |
|
|
-- Reset the link lost timer.
|
541 |
|
|
rxTimerReset <= '1';
|
542 |
|
|
|
543 |
|
|
-- Check if a flag has been received.
|
544 |
|
|
if (uartData_i /= FLAG_SEQUENCE) then
|
545 |
|
|
-- The received octet was not a flag.
|
546 |
|
|
|
547 |
|
|
-- Check if the octet was a contol character.
|
548 |
|
|
if (uartData_i /= CONTROL_ESCAPE) then
|
549 |
|
|
-- The octet was not a control character.
|
550 |
|
|
|
551 |
|
|
-- Check where in a symbol the reception is.
|
552 |
|
|
case rxStateCounter is
|
553 |
|
|
|
554 |
|
|
when "00" =>
|
555 |
|
|
inboundSymbol_o(33 downto 32) <= SYMBOL_IDLE;
|
556 |
|
|
if (escapeFound = '0') then
|
557 |
|
|
inboundSymbol_o(31 downto 24) <= uartData_i;
|
558 |
|
|
else
|
559 |
|
|
inboundSymbol_o(31 downto 24) <= uartData_i xor x"20";
|
560 |
|
|
end if;
|
561 |
|
|
rxStateCounter <= rxStateCounter + 1;
|
562 |
|
|
|
563 |
|
|
when "01" =>
|
564 |
|
|
inboundSymbol_o(33 downto 32) <= SYMBOL_IDLE;
|
565 |
|
|
if (escapeFound = '0') then
|
566 |
|
|
inboundSymbol_o(23 downto 16) <= uartData_i;
|
567 |
|
|
else
|
568 |
|
|
inboundSymbol_o(23 downto 16) <= uartData_i xor x"20";
|
569 |
|
|
end if;
|
570 |
|
|
rxStateCounter <= rxStateCounter + 1;
|
571 |
|
|
|
572 |
|
|
when "10" =>
|
573 |
|
|
inboundSymbol_o(33 downto 32) <= SYMBOL_CONTROL;
|
574 |
|
|
if (escapeFound = '0') then
|
575 |
|
|
inboundSymbol_o(15 downto 8) <= uartData_i;
|
576 |
|
|
else
|
577 |
|
|
inboundSymbol_o(15 downto 8) <= uartData_i xor x"20";
|
578 |
|
|
end if;
|
579 |
|
|
rxStateCounter <= rxStateCounter + 1;
|
580 |
|
|
|
581 |
|
|
when "11" =>
|
582 |
|
|
inboundSymbol_o(33 downto 32) <= SYMBOL_DATA;
|
583 |
|
|
if (escapeFound = '0') then
|
584 |
|
|
inboundSymbol_o(7 downto 0) <= uartData_i;
|
585 |
|
|
else
|
586 |
|
|
inboundSymbol_o(7 downto 0) <= uartData_i xor x"20";
|
587 |
|
|
end if;
|
588 |
|
|
rxStateCounter <= rxStateCounter + 1;
|
589 |
|
|
inboundSymbolWrite_o <= '1';
|
590 |
|
|
|
591 |
|
|
when others =>
|
592 |
|
|
rxStateCounter <= "00";
|
593 |
|
|
|
594 |
|
|
end case;
|
595 |
|
|
|
596 |
|
|
-- Read the octet from the uart.
|
597 |
|
|
uartRead <= '1';
|
598 |
|
|
escapeFound <= '0';
|
599 |
|
|
else
|
600 |
|
|
-- Control escape received.
|
601 |
|
|
|
602 |
|
|
-- Read the octet and indicate that an escape character has been received.
|
603 |
|
|
uartRead <= '1';
|
604 |
|
|
escapeFound <= '1';
|
605 |
|
|
end if;
|
606 |
|
|
else
|
607 |
|
|
-- Flag received.
|
608 |
|
|
|
609 |
|
|
-- Check if there are any unsent symbols pending.
|
610 |
|
|
if (rxStateCounter = 0) then
|
611 |
|
|
-- No pending symbol.
|
612 |
|
|
-- Send an idle symbol.
|
613 |
|
|
inboundSymbolWrite_o <= '1';
|
614 |
|
|
inboundSymbol_o(33 downto 32) <= SYMBOL_IDLE;
|
615 |
|
|
else
|
616 |
|
|
-- Pending symbol.
|
617 |
|
|
-- Send the pending symbol.
|
618 |
|
|
inboundSymbolWrite_o <= '1';
|
619 |
|
|
end if;
|
620 |
|
|
|
621 |
|
|
-- Read and discard the octet.
|
622 |
|
|
uartRead <= '1';
|
623 |
|
|
rxStateCounter <= "00";
|
624 |
|
|
end if;
|
625 |
|
|
else
|
626 |
|
|
-- Waiting for inbound data.
|
627 |
|
|
-- Dont do anything.
|
628 |
|
|
end if;
|
629 |
|
|
end if;
|
630 |
|
|
|
631 |
|
|
when others =>
|
632 |
|
|
-------------------------------------------------------------------
|
633 |
|
|
-- Unknown state.
|
634 |
|
|
-------------------------------------------------------------------
|
635 |
|
|
null;
|
636 |
|
|
|
637 |
|
|
end case;
|
638 |
|
|
end if;
|
639 |
|
|
end process;
|
640 |
|
|
|
641 |
|
|
end architecture;
|