1 |
215 |
ja_rd |
--##############################################################################
|
2 |
|
|
-- uart.vhdl -- Basic, hardwired RS232 UART.
|
3 |
|
|
--
|
4 |
|
|
-- Most operational parameters are hardcoded: 8 bit words, no parity, 1 stop
|
5 |
|
|
-- bit. The only parameter that can be configured in run time is the baud rate.
|
6 |
|
|
--
|
7 |
|
|
-- The receiver logic is a simplified copy of the 8051 UART. The bit period is
|
8 |
|
|
-- split in 16 sampling periods, and 3 samples are taken at the center of each
|
9 |
|
|
-- bit period. The bit value is decided by majority. The receiver logic has some
|
10 |
|
|
-- error recovery capability that should make this core reliable enough for
|
11 |
|
|
-- actual application use -- yet, the core does not have a format test bench.
|
12 |
|
|
--
|
13 |
|
|
-- See usage notes below.
|
14 |
|
|
--
|
15 |
|
|
--------------------------------------------------------------------------------
|
16 |
|
|
-- This file is free software (See COPYING.TXT)
|
17 |
|
|
--##############################################################################
|
18 |
|
|
|
19 |
|
|
library ieee;
|
20 |
|
|
use ieee.std_logic_1164.all;
|
21 |
|
|
use ieee.numeric_std.all;
|
22 |
|
|
|
23 |
|
|
--------------------------------------------------------------------------------
|
24 |
|
|
-- UART programmer model
|
25 |
|
|
--------------------------------------------------------------------------------
|
26 |
|
|
--
|
27 |
|
|
-- The UART has a number of configuration registers addressable with input
|
28 |
|
|
-- signal addr_*_i:
|
29 |
|
|
--
|
30 |
|
|
-- [00] => Data buffer, both transmission and reception.
|
31 |
|
|
-- [01] => Status/control register (r/w).
|
32 |
|
|
-- [10] => Bit period register, low byte.
|
33 |
|
|
-- [11] => Bit period register, high byte.
|
34 |
|
|
--
|
35 |
|
|
--
|
36 |
|
|
-- Data buffers:
|
37 |
|
|
----------------
|
38 |
|
|
--
|
39 |
|
|
-- The same address [00b] is used for both the receive buffer and the
|
40 |
|
|
-- transmision buffer.
|
41 |
|
|
--
|
42 |
|
|
-- Writing to the data buffer when flag TxRdy is high will trigger a
|
43 |
|
|
-- transmission and clear flag TxRdy.
|
44 |
|
|
-- Writing to the data buffer when flag TxRdy is clear will have no effect.
|
45 |
|
|
--
|
46 |
|
|
-- Reading the data register when flag RxRdy is high will return the last
|
47 |
|
|
-- received data byte, and will clear flag RxRdy but NOT RxIrq.
|
48 |
|
|
-- Reading the register when flag RxRdy is clear will return indeterminate data,
|
49 |
|
|
-- which in practice will usually be the last byte received.
|
50 |
|
|
--
|
51 |
|
|
-- Interrupts:
|
52 |
|
|
--------------
|
53 |
|
|
--
|
54 |
|
|
-- The core has two interrupt sources tied to a single external irq line. The
|
55 |
|
|
-- sources are these:
|
56 |
|
|
--
|
57 |
|
|
-- -# Receiver interrupt: Raised when the stop bit is sampled and determined
|
58 |
|
|
-- to be valid (about the middle of the bit period).
|
59 |
|
|
-- If the stop bit is not valid (not high) then the interrupt is not
|
60 |
|
|
-- triggered. If a start bit is determined to be spurious (i.e. the falling
|
61 |
|
|
-- edge is detected but the bit value when sampled is not 0) then the
|
62 |
|
|
-- interrupt is not triggered.
|
63 |
|
|
-- This interrupt sets flag RxIrw in the status register.
|
64 |
|
|
-- -# Transmitter interrupt: Raised at the end of the transmission of the stop
|
65 |
|
|
-- bit.
|
66 |
|
|
-- This interrupt sets flag TxIrq in the status register 1 clock cycle after
|
67 |
|
|
-- the interrupt is raised.
|
68 |
|
|
--
|
69 |
|
|
-- The core does not have any interrupt enable mask. If any interrupt source
|
70 |
|
|
-- triggers, the output irq_o is asserted for one cycle. This is all the extent
|
71 |
|
|
-- of the interrupt processing done by this module.
|
72 |
|
|
--
|
73 |
|
|
-- Error detection:
|
74 |
|
|
-------------------
|
75 |
|
|
--
|
76 |
|
|
-- The core is capable of detecting and recovering from these error conditions:
|
77 |
|
|
--
|
78 |
|
|
-- -# When a start bit is determined to be spurious (i.e. the falling edge is
|
79 |
|
|
-- detected but the bit value when sampled is not 0) then the core returns to
|
80 |
|
|
-- its idle state (waiting for a new start bit).
|
81 |
|
|
-- -# If a stop bit is determined to be invalid (not 1 when sampled), the
|
82 |
|
|
-- reception interrupt is not triggered and the received byte is discarded.
|
83 |
|
|
-- -# When the 3 samples taken from the center of a bit period are not equal,
|
84 |
|
|
-- the bit value is decided by majority.
|
85 |
|
|
--
|
86 |
|
|
-- In none of the 3 cases does the core raise any error flag. It would be very
|
87 |
|
|
-- easy to include those flags in the core, but it would take a lot more time
|
88 |
|
|
-- to test them minimally and that's why they haven't been included.
|
89 |
|
|
--
|
90 |
|
|
-- Status register flags:
|
91 |
|
|
-------------------------
|
92 |
|
|
--
|
93 |
|
|
-- 7 6 5 4 3 2 1 0
|
94 |
|
|
-- +-------+-------+-------+-------+-------+-------+-------+-------+
|
95 |
|
|
-- | 0 | 0 | RxIrq | TxIrq | 0 | 0 | RxRdy | TxRdy |
|
96 |
|
|
-- +-------+-------+-------+-------+-------+-------+-------+-------+
|
97 |
|
|
-- h h W1C W1C h h r r
|
98 |
|
|
--
|
99 |
|
|
-- Bits marked 'h' are hardwired and can't be modified.
|
100 |
|
|
-- Bits marked 'r' are read only; they are set and clear by the UART core.
|
101 |
|
|
-- Bits marked W1C ('Write 1 Clear') are set by the UART core when an interrupt
|
102 |
|
|
-- has been triggered and must be cleared by the software by writing a '1'.
|
103 |
|
|
--
|
104 |
|
|
-- -# Status bit TxRdy is high when there isn't any transmission in progress.
|
105 |
|
|
-- It is cleared when data is written to the transmission buffer and is
|
106 |
|
|
-- raised at the same time the transmission interrupt is triggered.
|
107 |
|
|
-- -# Status bit RxRdy is raised at the same time the receive interrupt is
|
108 |
|
|
-- triggered and is cleared when the data register is read.
|
109 |
|
|
-- -# Status bit TxIrq is raised when the transmission interrupt is triggered
|
110 |
|
|
-- and is cleared when a 1 is written to it.
|
111 |
|
|
-- -# Status bit RxIrq is raised when the reception interrupt is triggered
|
112 |
|
|
-- and is cleared when a 1 is written to it.
|
113 |
|
|
--
|
114 |
|
|
-- When writing to the status/control registers, only flags TxIrq and RxIrq are
|
115 |
|
|
-- affected, and only when writing a '1' as explained above. All other flags
|
116 |
|
|
-- are read-only.
|
117 |
|
|
--
|
118 |
|
|
-- Baud rate configuration:
|
119 |
|
|
---------------------------
|
120 |
|
|
--
|
121 |
|
|
-- The baud rate is determined by the value of 14-bit register 'bit_period_reg'.
|
122 |
|
|
-- This register holds the length of the bit period in clock cycles and its
|
123 |
|
|
-- value may be hardcoded or configured at run time.
|
124 |
|
|
--
|
125 |
|
|
-- When generic HARDWIRED is true, bit_period_reg is hardwired with a value
|
126 |
|
|
-- computed from the value of generic BAUD_RATE. The bit period computation
|
127 |
|
|
-- needs to know the master clock rate, which should be given in generic
|
128 |
|
|
-- CLOCK_RATE.
|
129 |
|
|
-- Writes to the baud registers when HARDWIRED is true will be ignored.
|
130 |
|
|
--
|
131 |
|
|
-- When generic HARDWIRED is false, generics BAUD_RATE and CLOCK_RATE determine
|
132 |
|
|
-- the reset value of bit_period_reg, but the register can be changed at run
|
133 |
|
|
-- time by writing at addresses [10b] and [11b], which access the low and high
|
134 |
|
|
-- bytes of the register, respectively.
|
135 |
|
|
-- Reading from those register addresses returns the value of the status
|
136 |
|
|
-- register (a LUT saving measure) so the registers are effectively write-only.
|
137 |
|
|
--
|
138 |
|
|
--------------------------------------------------------------------------------
|
139 |
|
|
-- Core interface signals:
|
140 |
|
|
--
|
141 |
|
|
-- clk_i: Clock input, active rising edge.
|
142 |
|
|
-- reset_i: Synchronous reset.
|
143 |
|
|
-- txd_o: TxD UART output.
|
144 |
|
|
-- rxd_i: RxD UART input -- synchronization logic included.
|
145 |
|
|
-- irq_o: Interrupt output, asserted for 1 cycle when triggered.
|
146 |
|
|
-- data_i: Data bus, input.
|
147 |
|
|
-- data_o: Data bus, output.
|
148 |
|
|
-- addr_rd_i: Register selection address, read access (see above).
|
149 |
|
|
-- addr_wr_i: Register selection address, write access (see above).
|
150 |
|
|
-- wr_i: Write enable input.
|
151 |
|
|
-- rd_i: Read enable input.
|
152 |
|
|
-- ce_i: Chip enable, must be active at the same time as wr_i or rd_i.
|
153 |
|
|
--
|
154 |
|
|
--
|
155 |
|
|
-- No detailed explanation of the i/f timing can be given for lack of time.
|
156 |
|
|
-- The core reads and writes like a synchronous memory.
|
157 |
|
|
--------------------------------------------------------------------------------
|
158 |
|
|
|
159 |
|
|
entity uart is
|
160 |
|
|
generic (
|
161 |
|
|
HARDWIRED : boolean := true; -- Baud rate hardwired to constant value
|
162 |
|
|
BAUD_RATE : integer := 19200; -- Default (or hardwired) baud rate
|
163 |
|
|
CLOCK_FREQ : integer := 50E6); -- Clock rate
|
164 |
|
|
port (
|
165 |
|
|
rxd_i : in std_logic;
|
166 |
|
|
txd_o : out std_logic;
|
167 |
|
|
|
168 |
|
|
irq_o : out std_logic;
|
169 |
|
|
|
170 |
|
|
data_i : in std_logic_vector(7 downto 0);
|
171 |
|
|
data_o : out std_logic_vector(7 downto 0);
|
172 |
|
|
|
173 |
|
|
addr_rd_i : in std_logic_vector(1 downto 0);
|
174 |
|
|
addr_wr_i : in std_logic_vector(1 downto 0);
|
175 |
|
|
wr_i : in std_logic;
|
176 |
|
|
rd_i : in std_logic;
|
177 |
|
|
ce_i : in std_logic;
|
178 |
|
|
|
179 |
|
|
clk_i : in std_logic;
|
180 |
|
|
reset_i : in std_logic);
|
181 |
|
|
end uart;
|
182 |
|
|
|
183 |
|
|
architecture hardwired of uart is
|
184 |
|
|
|
185 |
|
|
-- Bit period expressed in master clock cycles
|
186 |
|
|
constant DEFAULT_BIT_PERIOD : integer := (CLOCK_FREQ / BAUD_RATE);
|
187 |
|
|
|
188 |
|
|
-- Bit sampling period is 1/16 of the baud rate.
|
189 |
|
|
constant DEFAULT_SAMPLING_PERIOD : integer := DEFAULT_BIT_PERIOD / 16;
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
--##############################################################################
|
194 |
|
|
|
195 |
|
|
-- Common signals
|
196 |
|
|
|
197 |
|
|
signal reset : std_logic;
|
198 |
|
|
signal clk : std_logic;
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
signal bit_period_reg : unsigned(13 downto 0);
|
202 |
|
|
signal sampling_period : unsigned(9 downto 0);
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
-- Interrupt & status register signals
|
206 |
|
|
|
207 |
|
|
signal tx_irq_flag : std_logic;
|
208 |
|
|
signal rx_irq_flag : std_logic;
|
209 |
|
|
signal load_stat_reg : std_logic;
|
210 |
|
|
signal load_tx_reg : std_logic;
|
211 |
|
|
|
212 |
|
|
-- Receiver signals
|
213 |
|
|
signal rxd_q : std_logic;
|
214 |
|
|
signal tick_ctr : unsigned(3 downto 0);
|
215 |
|
|
signal state : unsigned(3 downto 0);
|
216 |
|
|
signal next_state : unsigned(3 downto 0);
|
217 |
|
|
signal start_bit_detected : std_logic;
|
218 |
|
|
signal reset_tick_ctr : std_logic;
|
219 |
|
|
signal stop_bit_sampled : std_logic;
|
220 |
|
|
signal load_rx_buffer : std_logic;
|
221 |
|
|
signal stop_error : std_logic;
|
222 |
|
|
signal samples : std_logic_vector(2 downto 0);
|
223 |
|
|
signal sampled_bit : std_logic;
|
224 |
|
|
signal do_shift : std_logic;
|
225 |
|
|
signal rx_buffer : std_logic_vector(7 downto 0);
|
226 |
|
|
signal rx_shift_reg : std_logic_vector(9 downto 0);
|
227 |
|
|
signal tick_ctr_enable : std_logic;
|
228 |
|
|
signal tick_baud_ctr : unsigned(10 downto 0);
|
229 |
|
|
|
230 |
|
|
signal rx_rdy_flag : std_logic;
|
231 |
|
|
signal rx_irq : std_logic;
|
232 |
|
|
signal set_rx_rdy_flag : std_logic;
|
233 |
|
|
signal rxd : std_logic;
|
234 |
|
|
|
235 |
|
|
signal read_rx : std_logic;
|
236 |
|
|
signal status : std_logic_vector(7 downto 0);
|
237 |
|
|
|
238 |
|
|
-- Transmitter signals
|
239 |
|
|
|
240 |
|
|
signal tx_counter : unsigned(13 downto 0);
|
241 |
|
|
signal tx_data : std_logic_vector(10 downto 0);
|
242 |
|
|
signal tx_ctr_bit : unsigned(3 downto 0);
|
243 |
|
|
signal tx_busy : std_logic;
|
244 |
|
|
signal tx_irq : std_logic;
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
begin
|
249 |
|
|
|
250 |
|
|
-- Rename the most commonly used inputs to get rid of the i/o suffix
|
251 |
|
|
clk <= clk_i;
|
252 |
|
|
reset <= reset_i;
|
253 |
|
|
rxd <= rxd_i;
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
-- Serial port status byte -- only 2 status flags
|
257 |
|
|
status <=
|
258 |
|
|
"00" & rx_irq_flag & tx_irq_flag & -- Interrupt flags
|
259 |
|
|
"00" & rx_rdy_flag & (not tx_busy); -- State flags
|
260 |
|
|
|
261 |
|
|
-- Read register multiplexor
|
262 |
|
|
with addr_rd_i select data_o <=
|
263 |
|
|
rx_buffer when "00",
|
264 |
|
|
status when others;
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
load_tx_reg <= '1' when wr_i = '1' and ce_i = '1' and addr_wr_i = "00" else '0';
|
268 |
|
|
load_stat_reg <= '1' when wr_i = '1' and ce_i = '1' and addr_wr_i = "01" else '0';
|
269 |
218 |
ja_rd |
read_rx <= '1' when rd_i = '1' and ce_i = '1' and addr_rd_i = "00" else '0';
|
270 |
215 |
ja_rd |
|
271 |
|
|
rx_irq <= set_rx_rdy_flag;
|
272 |
|
|
|
273 |
|
|
irq_o <= rx_irq or tx_irq;
|
274 |
|
|
|
275 |
|
|
interrupt_flags:
|
276 |
|
|
process(clk)
|
277 |
|
|
begin
|
278 |
|
|
if clk'event and clk='1' then
|
279 |
|
|
if reset = '1' then
|
280 |
|
|
rx_irq_flag <= '0';
|
281 |
|
|
tx_irq_flag <= '0';
|
282 |
|
|
else
|
283 |
|
|
if set_rx_rdy_flag='1' then
|
284 |
|
|
rx_irq_flag <= '1';
|
285 |
|
|
elsif load_stat_reg='1' and data_i(5)='1' then
|
286 |
|
|
rx_irq_flag <= '0';
|
287 |
|
|
end if;
|
288 |
|
|
if tx_irq='1' then
|
289 |
|
|
tx_irq_flag <= '1';
|
290 |
|
|
elsif load_stat_reg='1' and data_i(4)='1' then
|
291 |
|
|
tx_irq_flag <= '0';
|
292 |
|
|
end if;
|
293 |
|
|
end if;
|
294 |
|
|
end if;
|
295 |
|
|
end process interrupt_flags;
|
296 |
|
|
|
297 |
|
|
|
298 |
|
|
baud_rate_registers:
|
299 |
|
|
process(clk)
|
300 |
|
|
begin
|
301 |
|
|
if clk'event and clk='1' then
|
302 |
|
|
if reset = '1' then
|
303 |
|
|
bit_period_reg <= to_unsigned(DEFAULT_BIT_PERIOD,14);
|
304 |
|
|
else
|
305 |
|
|
if wr_i = '1' and ce_i = '1' then
|
306 |
|
|
if addr_wr_i = "10" then
|
307 |
|
|
bit_period_reg(7 downto 0) <= unsigned(data_i);
|
308 |
|
|
elsif addr_wr_i = "11" then
|
309 |
|
|
bit_period_reg(13 downto 8) <= unsigned(data_i(5 downto 0));
|
310 |
|
|
end if;
|
311 |
|
|
end if;
|
312 |
|
|
end if;
|
313 |
|
|
end if;
|
314 |
|
|
end process baud_rate_registers;
|
315 |
|
|
|
316 |
|
|
sampling_period <= bit_period_reg(13 downto 4);
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
-- Receiver --------------------------------------------------------------------
|
320 |
|
|
|
321 |
|
|
baud_counter:
|
322 |
|
|
process(clk)
|
323 |
|
|
begin
|
324 |
|
|
if clk'event and clk='1' then
|
325 |
|
|
if reset='1' then
|
326 |
|
|
tick_baud_ctr <= (others => '0');
|
327 |
|
|
else
|
328 |
|
|
if tick_baud_ctr=sampling_period then
|
329 |
|
|
tick_baud_ctr <= (others => '0');
|
330 |
|
|
else
|
331 |
|
|
tick_baud_ctr <= tick_baud_ctr + 1;
|
332 |
|
|
end if;
|
333 |
|
|
end if;
|
334 |
|
|
end if;
|
335 |
|
|
end process baud_counter;
|
336 |
|
|
|
337 |
|
|
tick_ctr_enable<= '1' when tick_baud_ctr=sampling_period else '0';
|
338 |
|
|
|
339 |
|
|
-- Register RxD at the bit sampling rate -- 16 times the baud rate.
|
340 |
|
|
rxd_input_register:
|
341 |
|
|
process(clk)
|
342 |
|
|
begin
|
343 |
|
|
if clk'event and clk='1' then
|
344 |
|
|
if reset='1' then
|
345 |
|
|
rxd_q <= '0';
|
346 |
|
|
else
|
347 |
|
|
if tick_ctr_enable='1' then
|
348 |
|
|
rxd_q <= rxd;
|
349 |
|
|
end if;
|
350 |
|
|
end if;
|
351 |
|
|
end if;
|
352 |
|
|
end process rxd_input_register;
|
353 |
|
|
|
354 |
|
|
-- We detect the start bit when...
|
355 |
|
|
start_bit_detected <= '1' when
|
356 |
|
|
state="0000" and -- ...we're waiting for the start bit...
|
357 |
|
|
rxd_q='1' and rxd='0' -- ...and we see RxD going 1-to-0
|
358 |
|
|
else '0';
|
359 |
|
|
|
360 |
|
|
-- As soon as we detect the start bit we synchronize the bit sampler to
|
361 |
|
|
-- the start bit's falling edge.
|
362 |
|
|
reset_tick_ctr <= '1' when start_bit_detected='1' else '0';
|
363 |
|
|
|
364 |
|
|
-- We have seen the end of the stop bit when...
|
365 |
|
|
stop_bit_sampled <= '1' when
|
366 |
|
|
state="1010" and -- ...we're in the stop bit period...
|
367 |
|
|
tick_ctr="1011" -- ...and we get the 11th sample in the bit period
|
368 |
|
|
else '0';
|
369 |
|
|
|
370 |
|
|
-- Load the RX buffer with the shift register when...
|
371 |
|
|
load_rx_buffer <= '1' when
|
372 |
|
|
stop_bit_sampled='1' and -- ...we've just seen the end of the stop bit...
|
373 |
|
|
sampled_bit='1' -- ...and its value is correct (1)
|
374 |
|
|
else '0';
|
375 |
|
|
|
376 |
|
|
-- Conversely, we detect a stop bit error when...
|
377 |
|
|
stop_error <= '1' when
|
378 |
|
|
stop_bit_sampled='1' and -- ...we've just seen the end of the stop bit...
|
379 |
|
|
sampled_bit='0' -- ...and its value is incorrect (0)
|
380 |
|
|
else '0';
|
381 |
|
|
|
382 |
|
|
-- tick_ctr is a counter 16 times faster than the baud rate that is aligned to
|
383 |
|
|
-- the falling edge of the start bit, so that when tick_ctr=0 we're close to
|
384 |
|
|
-- the start of a bit period.
|
385 |
|
|
bit_sample_counter:
|
386 |
|
|
process(clk)
|
387 |
|
|
begin
|
388 |
|
|
if clk'event and clk='1' then
|
389 |
|
|
if reset='1' then
|
390 |
|
|
tick_ctr <= "0000";
|
391 |
|
|
else
|
392 |
|
|
if tick_ctr_enable='1' then
|
393 |
|
|
-- Restart counter when it reaches 15 OR when the falling edge
|
394 |
|
|
-- of the start bit is detected; this is how we synchronize to the
|
395 |
|
|
-- start bit.
|
396 |
|
|
if tick_ctr="1111" or reset_tick_ctr='1' then
|
397 |
|
|
tick_ctr <= "0000";
|
398 |
|
|
else
|
399 |
|
|
tick_ctr <= tick_ctr + 1;
|
400 |
|
|
end if;
|
401 |
|
|
end if;
|
402 |
|
|
end if;
|
403 |
|
|
end if;
|
404 |
|
|
end process bit_sample_counter;
|
405 |
|
|
|
406 |
|
|
-- Main RX state machine:
|
407 |
|
|
-- 0 -> waiting for start bit
|
408 |
|
|
-- 1 -> sampling start bit
|
409 |
|
|
-- 2..9 -> sampling data bit 0 to 7
|
410 |
|
|
-- 10 -> sampling stop bit
|
411 |
|
|
next_state <=
|
412 |
|
|
-- Start sampling the start bit when we detect the falling edge
|
413 |
|
|
"0001" when state="0000" and start_bit_detected='1' else
|
414 |
|
|
-- Return to idle state if the start bit is not a clean 0
|
415 |
|
|
"0000" when state="0001" and tick_ctr="1010" and sampled_bit='1' else
|
416 |
|
|
-- Return to idle state at the end of the stop bit period
|
417 |
|
|
"0000" when state="1010" and tick_ctr="1111" else
|
418 |
|
|
-- Otherwise, proceed to next bit period at the end of each period
|
419 |
|
|
state + 1 when tick_ctr="1111" and do_shift='1' else
|
420 |
|
|
state;
|
421 |
|
|
|
422 |
|
|
rx_state_machine_register:
|
423 |
|
|
process(clk)
|
424 |
|
|
begin
|
425 |
|
|
if clk'event and clk='1' then
|
426 |
|
|
if reset='1' then
|
427 |
|
|
state <= "0000";
|
428 |
|
|
else
|
429 |
|
|
if tick_ctr_enable='1' then
|
430 |
|
|
state <= next_state;
|
431 |
|
|
end if;
|
432 |
|
|
end if;
|
433 |
|
|
end if;
|
434 |
|
|
end process rx_state_machine_register;
|
435 |
|
|
|
436 |
|
|
-- Collect 3 RxD samples from the 3 central sampling periods of the bit period.
|
437 |
|
|
rx_sampler:
|
438 |
|
|
process(clk)
|
439 |
|
|
begin
|
440 |
|
|
if clk'event and clk='1' then
|
441 |
|
|
if reset='1' then
|
442 |
|
|
samples <= "000";
|
443 |
|
|
else
|
444 |
|
|
if tick_ctr_enable='1' then
|
445 |
|
|
if tick_ctr="0111" then
|
446 |
|
|
samples(0) <= rxd;
|
447 |
|
|
end if;
|
448 |
|
|
if tick_ctr="1000" then
|
449 |
|
|
samples(1) <= rxd;
|
450 |
|
|
end if;
|
451 |
|
|
if tick_ctr="1001" then
|
452 |
|
|
samples(2) <= rxd;
|
453 |
|
|
end if;
|
454 |
|
|
end if;
|
455 |
|
|
end if;
|
456 |
|
|
end if;
|
457 |
|
|
end process rx_sampler;
|
458 |
|
|
|
459 |
|
|
-- Decide the value of the RxD bit by majority
|
460 |
|
|
with samples select
|
461 |
|
|
sampled_bit <= '0' when "000",
|
462 |
|
|
'0' when "001",
|
463 |
|
|
'0' when "010",
|
464 |
|
|
'1' when "011",
|
465 |
|
|
'0' when "100",
|
466 |
|
|
'1' when "101",
|
467 |
|
|
'1' when "110",
|
468 |
|
|
'1' when others;
|
469 |
|
|
|
470 |
|
|
rx_buffer_register:
|
471 |
|
|
process(clk)
|
472 |
|
|
begin
|
473 |
|
|
if clk'event and clk='1' then
|
474 |
|
|
if reset='1' then
|
475 |
|
|
rx_buffer <= "00000000";
|
476 |
|
|
set_rx_rdy_flag <= '0';
|
477 |
|
|
else
|
478 |
|
|
if tick_ctr_enable='1' and load_rx_buffer='1' and rx_rdy_flag='0' then
|
479 |
|
|
rx_buffer <= rx_shift_reg(8 downto 1);
|
480 |
|
|
set_rx_rdy_flag <= '1';
|
481 |
|
|
else
|
482 |
|
|
set_rx_rdy_flag <= '0';
|
483 |
|
|
end if;
|
484 |
|
|
end if;
|
485 |
|
|
end if;
|
486 |
|
|
end process rx_buffer_register;
|
487 |
|
|
|
488 |
|
|
rx_flag:
|
489 |
|
|
process(clk)
|
490 |
|
|
begin
|
491 |
|
|
if clk'event and clk='1' then
|
492 |
|
|
if reset='1' then
|
493 |
|
|
rx_rdy_flag <= '0';
|
494 |
|
|
else
|
495 |
|
|
if set_rx_rdy_flag='1' then
|
496 |
|
|
rx_rdy_flag <= '1';
|
497 |
|
|
else
|
498 |
|
|
if read_rx = '1' then
|
499 |
|
|
rx_rdy_flag <= '0';
|
500 |
|
|
end if;
|
501 |
|
|
end if;
|
502 |
|
|
end if;
|
503 |
|
|
end if;
|
504 |
|
|
end process rx_flag;
|
505 |
|
|
|
506 |
|
|
-- RX shifter control: shift in any state other than idle state (0)
|
507 |
|
|
do_shift <= state(0) or state(1) or state(2) or state(3);
|
508 |
|
|
|
509 |
|
|
rx_shift_register:
|
510 |
|
|
process(clk)
|
511 |
|
|
begin
|
512 |
|
|
if clk'event and clk='1' then
|
513 |
|
|
if reset='1' then
|
514 |
|
|
rx_shift_reg <= "1111111111";
|
515 |
|
|
else
|
516 |
|
|
if tick_ctr_enable='1' then
|
517 |
|
|
if tick_ctr="1010" and do_shift='1' then
|
518 |
|
|
rx_shift_reg(9) <= sampled_bit;
|
519 |
|
|
rx_shift_reg(8 downto 0) <= rx_shift_reg(9 downto 1);
|
520 |
|
|
end if;
|
521 |
|
|
end if;
|
522 |
|
|
end if;
|
523 |
|
|
end if;
|
524 |
|
|
end process rx_shift_register;
|
525 |
|
|
|
526 |
|
|
|
527 |
|
|
-- Transmitter -----------------------------------------------------------------
|
528 |
|
|
|
529 |
|
|
|
530 |
|
|
main_tx_process:
|
531 |
|
|
process(clk)
|
532 |
|
|
begin
|
533 |
|
|
if clk'event and clk='1' then
|
534 |
|
|
|
535 |
|
|
if reset='1' then
|
536 |
|
|
tx_data <= "10111111111";
|
537 |
|
|
tx_busy <= '0';
|
538 |
|
|
tx_irq <= '0';
|
539 |
|
|
tx_ctr_bit <= "0000";
|
540 |
|
|
tx_counter <= (others => '0');
|
541 |
|
|
elsif load_tx_reg='1' and tx_busy='0' then
|
542 |
|
|
tx_data <= "1"&data_i&"01";
|
543 |
|
|
tx_busy <= '1';
|
544 |
|
|
else
|
545 |
|
|
if tx_busy='1' then
|
546 |
|
|
if tx_counter = bit_period_reg then
|
547 |
|
|
tx_counter <= (others => '0');
|
548 |
|
|
tx_data(9 downto 0) <= tx_data(10 downto 1);
|
549 |
|
|
tx_data(10) <= '1';
|
550 |
|
|
if tx_ctr_bit = "1010" then
|
551 |
|
|
tx_busy <= '0';
|
552 |
|
|
tx_irq <= '1';
|
553 |
|
|
tx_ctr_bit <= "0000";
|
554 |
|
|
else
|
555 |
|
|
tx_ctr_bit <= tx_ctr_bit + 1;
|
556 |
|
|
end if;
|
557 |
|
|
else
|
558 |
|
|
tx_counter <= tx_counter + 1;
|
559 |
|
|
end if;
|
560 |
|
|
else
|
561 |
|
|
tx_irq <= '0';
|
562 |
|
|
end if;
|
563 |
|
|
end if;
|
564 |
|
|
end if;
|
565 |
|
|
end process main_tx_process;
|
566 |
|
|
|
567 |
|
|
txd_o <= tx_data(0);
|
568 |
|
|
|
569 |
|
|
end hardwired;
|