OpenCores
URL https://opencores.org/ocsvn/p9813_rgb_led_string_driver/p9813_rgb_led_string_driver/trunk

Subversion Repositories p9813_rgb_led_string_driver

[/] [p9813_rgb_led_string_driver/] [trunk/] [rtl/] [VHDL/] [uart_sqclk_pack.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jclaytons
--------------------------------------------------------------------------
2
-- Package of UART components
3
--
4
-- This UART uses a squarewave input for the BAUDRATE clock.  In other
5
-- words, the BAUD rate is exactly the same as the frequency of the
6
-- incoming clock.  This is in contrast to other UARTs which need a
7
-- Baud rate clock which is some multiple of the actual Baud rate
8
-- desired.  Because of the 1x nature of the Baud clock, the receiver
9
-- needs at least one Baud Clock interval in which to measure the
10
-- Baud clock versus the system clock, before it can start working.
11
-- Also, the system clock must be somewhat higher than the Baud clock
12
-- in order for the receiver to work.
13
--
14
-- This package contains the UART, plus individual async_tx and async_rx
15
-- modules, which are the transmit and receive sections of the UART.
16
--
17
--
18
 
19
library IEEE;
20
use IEEE.STD_LOGIC_1164.ALL;
21
use IEEE.NUMERIC_STD.ALL;
22
 
23
package uart_sqclk_pack is
24
 
25
-- Component declarations not provided any more.
26
-- With VHDL '93 and newer, component declarations are allowed,
27
-- but not required.
28
--
29
-- Please to try direct instantiation instead, for example:
30
--
31
--   instance_name : entity work.entity_name(beh)
32
--
33
 
34
end uart_sqclk_pack;
35
 
36
package body uart_sqclk_pack is
37
end uart_sqclk_pack;
38
 
39
 
40
-------------------------------------------------------------------------------
41
-- Asynchronous Transmitter With No Buffering
42
------------------------------------------------------------------------------- 
43
--
44
-- Author: John Clayton
45
-- Date  : Aug  08, 2013 Added this change log header, which was missing.
46
--                       Changed tx_done_o signal so that it pulses after
47
--                       the stop bit is finished.  How could this have
48
--                       remained so woefully incorrect for so long?!
49
--         Jan  02, 2014 Fixed a latent bug in the logic for asserting
50
--                       tx_done_o.  Prior to this fix, it was possible
51
--                       for a write that was coincident with do_txbit to
52
--                       be ignored!  Once again, how could this have
53
--                       remained so woefully incorrect all this time?!
54
--         Jan  07, 2014 Rewrote the startup logic to allow for cases
55
--                       when tx_wr_i='1' and tx_bcnt="0000" and do_txbit='1'
56
--                       Also rewrote the tx_done_o signal so that it is
57
--                       asserted earlier - when "tx_almost_done" is high.
58
--                       This is all calculated to allow the transmitter
59
--                       to send characters back-to-back using its own
60
--                       tx_done_o signal as a tx_wr_i signal.  This is
61
--                       actually getting pretty neat.  The unit sends out
62
--                       asynchronous characters, but insists on doing it
63
--                       in synchronism with the tx_clk_i input... so it
64
--                       isn't really very asynchronous in that sense!
65
--
66
-- Description
67
-------------------------------------------------------------------------------
68
-- Squarewave tx_clk_i input determines rate.
69
-- (tx_clk_i need not be a squarewave for this module, since only the rising
70
--  edge is used.  In the accompanying receiver, however, both edges are used.)
71
-- 
72
-- Description:
73
--   This block transmits asynchronous serial bytes.  The Baudrate and parity
74
-- are determined by inputs, but the number of bits per character is
75
-- fixed at eight.
76
--
77
-- NOTES:
78
-- Transmit starts when the transmitter is idle and tx_wr_i is detected high
79
-- at a rising sys_clk edge.
80
--
81
-- Once the transmit operation is completed, done_o latches high.
82
--
83
-- Since the baud clock might be asynchronous to the sys_clk, there are
84
-- syncronizing flip-flops on it inside this module.
85
 
86
library IEEE;
87
use IEEE.STD_LOGIC_1164.ALL;
88
use IEEE.NUMERIC_STD.ALL;
89
 
90
entity async_tx_sqclk is
91
    port (
92
 
93
      sys_rst_n    : in std_logic;
94
      sys_clk      : in std_logic;
95
      sys_clk_en   : in std_logic;
96
 
97
      -- rate and parity
98
      tx_parity_i  : in unsigned(1 downto 0); -- 0=none, 1=even, 2 or 3=odd
99
      tx_clk_i     : in std_logic;
100
 
101
      -- serial output
102
      tx_stream    : out std_logic;
103
 
104
      -- control and status
105
      tx_wr_i      : in  std_logic;        -- Starts Transmit
106
      tx_dat_i     : in  unsigned(7 downto 0);
107
      tx_done_o    : out std_logic
108
    );
109
end async_tx_sqclk;
110
 
111
architecture beh of async_tx_sqclk is
112
 
113
-- TX signals
114
  -- TX clock synchronizing flip-flops and rising edge detection
115
signal tx_clk_r1 : std_logic;
116
signal tx_clk_r2 : std_logic;
117
  -- TX clock enable, shift register and bit count
118
signal do_txbit       : std_logic;
119
signal tx_sr          : unsigned(9 downto 0);
120
signal tx_bcnt        : unsigned(3 downto 0); -- Number of bits
121
signal tx_almost_done : std_logic;
122
signal tx_done        : std_logic;
123
 
124
begin
125
 
126
  -- This process detects the rising edge of tx_clk_i
127
  tx_clk_edge_proc: Process(sys_rst_n,sys_clk)
128
  BEGIN
129
    if (sys_rst_n = '0') then
130
      tx_clk_r1 <= '0';
131
      tx_clk_r2 <= '0';
132
    elsif (sys_clk'event AND sys_clk='1') then
133
      if (sys_clk_en='1') then
134
        tx_clk_r1 <= tx_clk_i;
135
        tx_clk_r2 <= tx_clk_r1;
136
      end if;
137
    end if;
138
  END PROCESS tx_clk_edge_proc;
139
  do_txbit <= (tx_clk_r1 and not tx_clk_r2); -- rising edge detect
140
 
141
  -- This process loads the shift register, then counts as the bits transmit out.
142
  byte_tx: Process(sys_rst_n,sys_clk)
143
  BEGIN
144
    if (sys_rst_n = '0') then
145
      tx_sr     <= (others=>'0');
146
      tx_bcnt   <= (others=>'0');
147
      tx_stream <= '1';
148
      tx_done   <= '1';
149
    elsif (sys_clk'event and sys_clk='1') then
150
      if (sys_clk_en='1') then
151
        -- Start a new transmission when ready
152
        -- Case 1 is starting while do_txbit is high
153
        if tx_bcnt="0000" and do_txbit='1' and tx_wr_i='1' then
154
          tx_stream <= '0';                             -- Provide start bit
155
          tx_sr(7 downto 0) <= tx_dat_i;                -- Load the TX data
156
          tx_sr(8) <= '1';                              -- Default the parity bit to one
157
          if(tx_parity_i = "00") then                   --If no parity...
158
            tx_bcnt  <= "1001";                         -- send start, 8 data bits, and stop
159
          elsif (tx_parity_i = "01") then               --If even parity...
160
            tx_bcnt  <= "1010";                         -- send start, 8 data bits, parity, and stop
161
            tx_sr(8) <= tx_dat_i(0) XOR tx_dat_i(1) XOR tx_dat_i(2) XOR tx_dat_i(3) XOR
162
                        tx_dat_i(4) XOR tx_dat_i(5) XOR tx_dat_i(6) XOR tx_dat_i(7);
163
          else                                          --If odd parity...
164
            tx_bcnt  <= "1011";                         --send start, 8 data bits, parity, and stop
165
            tx_sr(8) <= NOT (tx_dat_i(0) XOR tx_dat_i(1) XOR tx_dat_i(2) XOR tx_dat_i(3) XOR
166
                             tx_dat_i(4) XOR tx_dat_i(5) XOR tx_dat_i(6) XOR tx_dat_i(7));
167
          end if;
168
          tx_done <= '0';
169
        -- Case 2 is starting while do_txbit is low
170
        elsif tx_done='1' and tx_wr_i='1' then -- Only allow loads when transmitter is idle
171
          tx_sr(0) <= '0';                              -- Load start bit
172
          tx_sr(8 downto 1) <= tx_dat_i;                -- Load the TX data
173
          tx_sr(9) <= '1';                              -- Default the parity bit to one
174
          if(tx_parity_i = "00") then                   --If no parity...
175
            tx_bcnt  <= "1010";                         -- send start, 8 data bits, and stop
176
          elsif (tx_parity_i = "01") then               --If even parity...
177
            tx_bcnt  <= "1011";                         -- send start, 8 data bits, parity, and stop
178
            tx_sr(9) <= tx_dat_i(0) XOR tx_dat_i(1) XOR tx_dat_i(2) XOR tx_dat_i(3) XOR
179
                        tx_dat_i(4) XOR tx_dat_i(5) XOR tx_dat_i(6) XOR tx_dat_i(7);
180
          else                                          --If odd parity...
181
            tx_bcnt  <= "1011";                         --send start, 8 data bits, parity, and stop
182
            tx_sr(9) <= NOT (tx_dat_i(0) XOR tx_dat_i(1) XOR tx_dat_i(2) XOR tx_dat_i(3) XOR
183
                             tx_dat_i(4) XOR tx_dat_i(5) XOR tx_dat_i(6) XOR tx_dat_i(7));
184
          end if;
185
          tx_done <= '0';
186
        -- Process through the remaining data
187
        elsif(tx_bcnt>"0000" and do_txbit='1') then     -- Still have bits to send?
188
          tx_bcnt <= tx_bcnt-1;
189
          tx_sr(8 downto 0) <= tx_sr(9 downto 1);       -- Right shift the data (send LSB first)
190
          tx_sr(9) <= '1';
191
          tx_stream <= tx_sr(0);
192
        end if;
193
        -- Assert tx_done when truly finished.
194
        if tx_almost_done='1' and tx_wr_i='0' then
195
          tx_done <= '1';
196
        end if;
197
      end if; -- sys_clk_en
198
    end if; -- sys_clk'event...
199
  END PROCESS byte_tx;
200
 
201
  tx_almost_done <= '1' when (tx_done='0' and tx_bcnt="0000" and do_txbit='1') else '0';
202
  tx_done_o <= '1' when tx_done='1' or tx_almost_done='1' else '0';
203
 
204
end beh;
205
 
206
 
207
-------------------------------------------------------------------------------
208
-- Asynchronous Receiver With Output Buffer
209
------------------------------------------------------------------------------- 
210
--
211
-- Author: John Clayton
212
-- Date  : Aug  05, 2013 Added this change log header, which was missing.
213
--                       Added first_edge signal to avoid erroneous initial
214
--                       baud interval measurement (John Clayton & Philip 
215
--                       Kasavan)
216
--         Jan.  2, 2014 Added output buffer, changed idle_prep to include
217
--                       the actual transition to IDLE state.  Added
218
--                       POST_RECV state, so that the rx_done_o signal will
219
--                       reflect the true end of the received character.
220
--                       This helps in applications where a received
221
--                       asynchronous input is "echoed back" directly,
222
--                       as the rx_wr_o signal can be used to switch the
223
--                       signal at the correct time.
224
--         Feb.  6, 2014 Added requirement for half_baud to be non-zero
225
--                       before leaving IDLE state.  This prevents leaving
226
--                       IDLE due to falling edges prior to the first Baud
227
--                       interval measurement.
228
--
229
-- Description
230
-------------------------------------------------------------------------------
231
-- Squarewave tx_clk_i input determines rate.
232
-- (tx_clk_i does not really need to be a squarewave.  Only the rising edges
233
--  are measured and used.)
234
-- 
235
-- Description:
236
--   This block receives asynchronous serial bytes.  The Baudrate and parity
237
-- are determined by inputs, but the number of bits per character is
238
-- fixed at eight.
239
--
240
-- NOTES:
241
-- The receive input and baudrate clock are passed through two layers of
242
-- synchronizing flip-flops to mitigate metastability, since those signals can
243
-- originate outside of the sys_clk clock domain.  All other logic connecting to 
244
-- input of this function are assumed to be within the same clock domain.
245
--
246
-- The receiver looks for a new start bit immediately following the rx_wr_o
247
-- pulse, but rx_done_o is delayed until the expected end of the received
248
-- character.  If a new start bit is detected just prior to the expected end
249
-- of the received character, then rx_done_o is not asserted.
250
--
251
-- Receive begins when the falling edge of the start bit is detected.
252
-- Then after 10 or 11 bit times have passed (depending on the parity setting)
253
-- the rx_wr_o signal will pulse high for one clock period, indicating rx_dat_o
254
-- contains valid receive data.  The rx_wr_o pulse is only issued if there is
255
-- no parity error, and the stop bit is actually detected high at the sampling
256
-- time.
257
-- The rx_dat_o outputs will hold the received data until the next rx_wr_o pulse.
258
-- The rx_dat_o output provides the received data, even in the presence of a
259
-- parity or stop-bit error.
260
-- Error flags are valid during rx_wr_o, but they remain latched, until
261
-- rx_restart_i.
262
--
263
-- Although the receiver immediately restarts itself to receive the next
264
-- character, the rx_restart_i input can clear the error indicators.  The rx_restart_i
265
-- input is like a synchronous reset in this respect since it will cause a receive
266
-- operation to abort.
267
--
268
 
269
library IEEE;
270
use IEEE.STD_LOGIC_1164.ALL;
271
use IEEE.NUMERIC_STD.ALL;
272
 
273
entity async_rx_sqclk is
274
    port (
275
 
276
      sys_rst_n    : in std_logic;
277
      sys_clk      : in std_logic;
278
      sys_clk_en   : in std_logic;
279
 
280
      -- rate and parity
281
      rx_parity_i  : in unsigned(1 downto 0); -- 0=none, 1=even, 2 or 3=odd
282
      rx_clk_i     : in std_logic;
283
 
284
      -- serial input
285
      rx_stream    : in std_logic;
286
 
287
      -- control and status
288
      rx_restart_i : in  std_logic;        -- High clears error flags, synchronously resets receiver
289
      rx_dat_o     : out unsigned(7 downto 0);
290
      rx_wr_o      : out std_logic;        -- High pulse means store rx_dat_o.
291
      rx_done_o    : out std_logic;        -- Indicates receiver is idle
292
      frame_err_o  : out std_logic;        -- High = error.  Reset when rx_restart_i asserted.
293
      parity_err_o : out std_logic         -- High = error.  Reset when rx_restart_i asserted.
294
    );
295
end async_rx_sqclk;
296
 
297
architecture beh of async_rx_sqclk is
298
 
299
 
300
-- RX signals
301
  -- rx_clk_i synchronizing flip-flops and rising edge detector
302
signal rx_clk_r1       : std_logic;
303
signal rx_clk_r2       : std_logic;
304
  -- RX input synchronizing flip flops
305
signal rx_stream_r1    : std_logic;
306
signal rx_stream_r2    : std_logic;
307
  -- RX signals
308
    -- RX State Machine
309
type RX_STATE_TYPE is (IDLE, CHECK_START_1, CHECK_START_2, RECV_DATA, POST_RECV);
310
signal rx_state        : RX_STATE_TYPE;
311
signal start_bit_start : std_logic;             -- Signals falling edge of rx_stream_i
312
signal rx_sr           : unsigned(8 downto 0);  -- Shift register
313
signal rx_bcnt         : unsigned(3 downto 0);  -- Number of bits left, counts down
314
signal rx_bcnt_start   : unsigned(3 downto 0);  -- Total number of bits
315
signal rx_parity_good  : std_logic;
316
  -- Timers have been sized to hold baud interval for speeds as slow as 9600 bps at 100MHz sys_clk
317
signal rx_timer        : unsigned(13 downto 0); -- Elapsed sys_clks from last bit time start
318
signal half_baud       : unsigned(13 downto 0); -- One half of full_baud
319
signal full_baud       : unsigned(13 downto 0); -- Baud interval, as measured from rx_clk_i
320
signal baud_timer      : unsigned(13 downto 0); -- Used to measure baud interval
321
signal bit_sampled     : std_logic;             -- High indicates bit is already sampled, don't allow resampling.
322
 
323
signal first_edge      : std_logic;
324
 
325
begin
326
 
327
  -- Synchronizing flip flops to avoid metastability issues...
328
  rx_stream_syncproc: Process(sys_rst_n,sys_clk)
329
  BEGIN
330
    if (sys_rst_n = '0') then
331
      rx_stream_r1 <= '1';
332
      rx_stream_r2 <= '1';
333
    elsif (sys_clk'event AND sys_clk='1') then
334
      if (sys_clk_en='1') then
335
        rx_stream_r2 <= rx_stream_r1;
336
        rx_stream_r1 <= rx_stream;
337
      end if;
338
    end if;
339
  END PROCESS rx_stream_syncproc;
340
  start_bit_start <= rx_stream_r2 and not rx_stream_r1;
341
 
342
  -- Synchronizing flip flops to avoid metastability issues...
343
  rx_clk_syncproc: Process(sys_rst_n,sys_clk)
344
  BEGIN
345
    if (sys_rst_n = '0') then
346
      rx_clk_r1 <= '0';
347
      rx_clk_r2 <= '0';
348
    elsif (sys_clk'event AND sys_clk='1') then
349
      if (sys_clk_en='1') then
350
        rx_clk_r2 <= rx_clk_r1;
351
        rx_clk_r1 <= rx_clk_i;
352
      end if;
353
    end if;
354
  END PROCESS rx_clk_syncproc;
355
 
356
  -- This is the baud interval measuring process.
357
  -- Measurements are only made between rising edges
358
  baud_measure_proc: Process(sys_rst_n,sys_clk)
359
  BEGIN
360
    if (sys_rst_n = '0') then
361
      full_baud  <= (others=>'0');
362
      baud_timer <= (others=>'0');
363
      first_edge <= '0';
364
    elsif (sys_clk'event AND sys_clk='1') then
365
      if (sys_clk_en='1') then
366
        if(first_edge = '1')then
367
          if (rx_clk_r1='1' and rx_clk_r2='0') then
368
            full_baud  <= baud_timer;
369
            baud_timer <= (others=>'0');
370
          else
371
            baud_timer <= baud_timer+1;
372
          end if;
373
        elsif(rx_clk_r1='1' and rx_clk_r2='0') then
374
          first_edge <= '1';
375
        end if;
376
      end if;
377
    end if;
378
  END PROCESS baud_measure_proc;
379
 
380
 
381
  -- This process handles the incoming bits
382
  uart_rx_bits: Process(sys_rst_n,sys_clk)
383
 
384
  procedure idle_prep is
385
  begin
386
    rx_done_o   <= '1';
387
    bit_sampled <= '0';
388
    rx_bcnt     <= (others=>'0');
389
    rx_timer    <= (others=>'0');
390
    rx_state    <= IDLE;
391
  end idle_prep;
392
 
393
  begin
394
    if (sys_rst_n = '0') then
395
      idle_prep;
396
      rx_sr        <= (others=>'0');
397
      frame_err_o  <= '0';
398
      parity_err_o <= '0';
399
      rx_wr_o      <= '0';
400
      rx_dat_o     <= (others=>'0');
401
    elsif (sys_clk'event AND sys_clk='1') then
402
      if (sys_clk_en='1') then
403
        -- Default values
404
        rx_wr_o      <= '0';           -- Default to no data write
405
        -- Handle incrementing the sample timer
406
        rx_timer<=rx_timer+1;
407
        -- State transitions
408
        case rx_state is
409
 
410
          when IDLE         =>
411
            rx_done_o   <= '1';           -- Indicate receive is done.
412
            bit_sampled <= '0';           -- Indicate bit is not yet sampled.
413
            if (rx_restart_i='1') then
414
              idle_prep;
415
              frame_err_o  <= '0';        -- At rx_restart, also clear error flags
416
              parity_err_o <= '0';
417
            elsif (half_baud/=0 and start_bit_start='1') then
418
              rx_timer  <= (others=>'0'); -- Reset timer back to zero
419
              rx_bcnt   <= rx_bcnt_start; -- Initialize bit counter
420
              rx_done_o <= '0';
421
              rx_state  <= CHECK_START_1;
422
            end if;
423
 
424
          when CHECK_START_1 =>
425
            if (rx_restart_i='1') then    -- Restart has very high priority
426
              idle_prep;
427
            elsif (rx_stream_r2='1') then -- High during this time is an error
428
              frame_err_o <= '1';
429
              idle_prep;
430
            elsif (rx_timer>=half_baud) then -- Must use >= since threshold may change downward
431
              rx_state  <= CHECK_START_2;
432
            end if;
433
 
434
          when CHECK_START_2 =>            -- During second half of start bit, don't verify low level
435
            if (rx_restart_i='1') then     -- Restart has very high priority
436
              idle_prep;
437
            elsif (rx_timer>=full_baud or rx_stream_r2='1') then -- Wait for end of start bit
438
              rx_timer <= (others=>'0'); -- Reset timer back to zero
439
              rx_state <= RECV_DATA;
440
            end if;
441
 
442
          when RECV_DATA     =>
443
            if (rx_restart_i='1') then     -- Restart has very high priority
444
              idle_prep;
445
            elsif (rx_timer>=full_baud) then -- Must use >= since threshold may change downward
446
              rx_timer <= (others=>'0'); -- Reset timer back to zero
447
              bit_sampled <= '0';
448
            elsif (rx_timer>=half_baud and bit_sampled='0') then -- Must use >= since threshold may change downward
449
              bit_sampled <= '1';
450
              if (rx_bcnt="0000") then
451
                rx_state <= POST_RECV;
452
                rx_dat_o <= rx_sr(7 downto 0);
453
                if (rx_parity_good='1' and rx_stream_r2='1') then
454
                  rx_wr_o <= '1';            -- If all is correct, create a one clock long pulse to store rx_dat_o.
455
                else
456
                  if (rx_stream_r2='0') then
457
                    frame_err_o <= '1';        -- Record error if there is a bad stop bit
458
                  end if;
459
                  if (rx_parity_good='0') then
460
                    parity_err_o <= '1';       -- Record error if there is bad parity
461
                  end if;
462
                end if;
463
              else -- Process a new bit
464
                rx_sr(7 downto 0) <= rx_sr(8 downto 1);
465
                if (rx_parity_i = "00") then
466
                  rx_sr(7) <= rx_stream_r2;  -- Store the new incoming bit
467
                else
468
                  rx_sr(8) <= rx_stream_r2;
469
                end if;
470
                rx_bcnt <= rx_bcnt-1;
471
              end if;
472
            end if;
473
 
474
          when POST_RECV => -- Wait out latter half of stop bit, checking for start bits...
475
            if (rx_restart_i='1') then
476
              bit_sampled  <= '0';
477
              frame_err_o  <= '0'; -- At rx_restart, also clear error flags
478
              parity_err_o <= '0';
479
              idle_prep;
480
            elsif (start_bit_start='1') then
481
              bit_sampled <= '0';
482
              rx_timer  <= (others=>'0'); -- Reset timer back to zero
483
              rx_bcnt   <= rx_bcnt_start; -- Initialize bit counter
484
              rx_done_o <= '0';
485
              rx_state  <= CHECK_START_1;
486
            elsif (rx_timer>=full_baud) then -- Wait for end of start bit
487
              bit_sampled <= '0'; -- Indicate bit is not yet sampled.
488
              idle_prep; -- Asserts rx_done_o to indicate completion
489
            end if;
490
 
491
          when others => null;
492
 
493
        end case;
494
      end if;
495
    end if;
496
  end process uart_rx_bits;
497
 
498
  -------------------------
499
  -- Assign number of bits to shift in.
500
  rx_bcnt_start <= "1000" when (rx_parity_i="00") else "1001";
501
 
502
  -------------------------
503
  -- Assign half baud period
504
  half_baud <= ('0' & full_baud(13 downto 1));
505
 
506
  -------------------------
507
  -- Parity check process
508
  rx_parity_check: process(rx_sr, rx_parity_i)
509
  begin
510
    if (rx_parity_i="00") then       -- No parity...
511
      rx_parity_good <= '1'; -- (always good.)
512
    elsif (rx_parity_i="01") then    -- Even parity...
513
      rx_parity_good <= not (rx_sr(0) XOR rx_sr(1) XOR rx_sr(2) XOR rx_sr(3) XOR rx_sr(4)
514
                                      XOR rx_sr(5) XOR rx_sr(6) XOR rx_sr(7) XOR rx_sr(8));
515
    else                     -- Odd parity...
516
      rx_parity_good <=     (rx_sr(0) XOR rx_sr(1) XOR rx_sr(2) XOR rx_sr(3) XOR rx_sr(4)
517
                                      XOR rx_sr(5) XOR rx_sr(6) XOR rx_sr(7) XOR rx_sr(8));
518
    end if;
519
  end process;
520
 
521
end beh;
522
 
523
 
524
-------------------------------------------------------------------------------
525
-- Half Duplex Switch -- automatic priority to TX, with "hang timer" and delay
526
------------------------------------------------------------------------------- 
527
--
528
-- Author: John Clayton
529
-- Date  : Oct. 09, 2015 Initial creation.
530
--         Oct. 20, 2015 Updated to include delay, thereby allowing for
531
--                       switching from TX to RX at the very end of the
532
--                       TX data, at the expense of waiting additional
533
--                       time for the TX data to emerge.
534
--
535
-- Description
536
-------------------------------------------------------------------------------
537
--
538
-- This unit implements a simple switch for routing both TX and RX signals
539
-- over a single wire, called "half duplex" communication.  This is needed
540
-- for 2 wire RS-232 or 3 wire RS-485/RS-422 communications.
541
--
542
-- The switch immediately goes into transmit mode when the non-idle state
543
-- is detected on the tx_i input.  After HANG_TIME rising edges of the
544
-- tick_i input, with the idle state continuously present on the tx_i input,
545
-- the switch reverts to the receive mode.  The tick_i may be connected
546
-- to the Baudrate clock, or any other desired clock, but it must not
547
-- be tied to a constant value, since the edges are needed to advance
548
-- the timer.
549
--
550
-- It is recommended to set HANG_TIME to exceed the time needed for one
551
-- character to be transmitted, since that will prevent mid-character
552
-- timeouts.
553
--
554
-- An additional parameter, DELAY, allows for the transmit data to be
555
-- delayed by DELAY rising edges of the tick_i input.  Note that this
556
-- delays the TX data after the activity check, so that the moment when
557
-- the "hang" timer expires can effectively be lined up with the end of
558
-- the final TX data byte.  Take care!  If DELAY is set greater than
559
-- HANG_TIME, then some of the TX data can be effectively curtailed.
560
--
561
-- Note that the delay shift register effectively samples tx_i data
562
-- on the detected rising edges of tick_i.  Thus, tick_i rising edges
563
-- should be a synchronized 1x Baud rate clock, or else an unsynchronized
564
-- clock at more than 2x the Baud rate, to satisfy Nyquist criteria. By
565
-- setting DELAY=0, this constraint is removed, since the shift register
566
-- is not used for zero DELAY, and no re-sampling of tx_i actually occurs.
567
--
568
-- The hd_drive_o signal is provided so that tristates may be kept at the top
569
-- level of the system.
570
--
571
-- No synchronization flip flops are implemented within this switch
572
-- module.  If needed, they are assumed to be included elsewhere.
573
--
574
 
575
library IEEE;
576
use IEEE.STD_LOGIC_1164.ALL;
577
use IEEE.NUMERIC_STD.ALL;
578
 
579
library work;
580
use work.function_pack.all;
581
 
582
entity half_duplex_switch is
583
    generic (
584
      HANG_TIME : natural := 12; -- Units of timer_i rising edges
585
      TX_DELAY  : natural := 12  -- Units of timer_i rising edges
586
    );
587
    port (
588
 
589
      sys_rst_n  : in  std_logic;
590
      sys_clk    : in  std_logic;
591
      sys_clk_en : in  std_logic;
592
 
593
      -- Full Duplex side
594
      tx_i       : in  std_logic;
595
      rx_o       : out std_logic;
596
 
597
      -- Half Duplex side
598
      hd_o       : out std_logic;
599
      hd_i       : in  std_logic;
600
      hd_drive_o : out std_logic;
601
 
602
      -- Timer
603
      tick_i     : in  std_logic
604
    );
605
end half_duplex_switch;
606
 
607
architecture beh of half_duplex_switch is
608
 
609
-- TX signals
610
signal timer    : unsigned(timer_width(HANG_TIME)-1 downto 0);
611
signal delay_sr : unsigned(15 downto 0);
612
signal tick_r1  : std_logic;
613
signal tx_data  : std_logic;
614
 
615
begin
616
 
617
  main_proc: Process(sys_rst_n,sys_clk)
618
  begin
619
    if (sys_rst_n = '0') then
620
      tick_r1  <= '0';
621
      timer    <= (others=>'0');
622
      delay_sr <= (others=>'1');
623
    elsif (sys_clk'event AND sys_clk='1') then
624
      if (sys_clk_en='1') then
625
        tick_r1 <= tick_i;
626
        -- Handle the delay shift register
627
        if (tick_r1='0' and tick_i='1') then
628
          delay_sr <= delay_sr(14 downto 0) & tx_i;
629
        end if;
630
        -- Handle the hang timer
631
        if (tx_i='0') then
632
          timer <= to_unsigned(HANG_TIME,timer'length);
633
        elsif (tick_r1='0' and tick_i='1') then
634
          if (timer>0) then
635
            timer <= timer-1;
636
          end if;
637
        end if;
638
      end if;
639
    end if;
640
  end process main_proc;
641
 
642
  tx_data <= tx_i when (TX_DELAY=0) else delay_sr(TX_DELAY-1);
643
 
644
  rx_o <= '1' when (timer>0) else hd_i;
645
  hd_o <= tx_data when (timer>0) else '1';
646
  hd_drive_o <= '1' when (timer>0) else '0';
647
 
648
end beh;
649
 
650
--------------------------------------------------------------------
651
-- UART.  Variable Speed, RX Buffer, but no TX buffer
652
-- High Speed Asynchronous Receiver & Transmitter
653
-- 
654
-- Description:
655
--   This block receives and transmits asynchronous serial bytes.  The Baudrate
656
-- and parity are selectable through inputs, but the number of bits per character
657
-- is fixed at eight.
658
--
659
-- NOTES:
660
-- Transmit starts when tx_wr_i is detected high at a rising clock edge.
661
-- Once the transmit operation is completed, tx_done_o latches high.
662
--
663
-- The receive input is passed through two layers of synchronizing flip-flops
664
-- to help mitigate metastability issues, since this signal can come from
665
-- outside of the sys_clk clock domain.  All other logic connecting to inputs
666
-- of this function are assumed to be within the same clock domain.
667
--
668
-- The receiver looks for a new start bit immediately following the rx_wr_o
669
-- pulse, but rx_done_o is delayed until the expected end of the received
670
-- character.  If a new start bit is detected just prior to the expected end
671
-- of the received character, then rx_done_o is not asserted.
672
--
673
-- Receive begins when the falling edge of the start bit is detected.
674
-- Then after 10 or 11 bit times have passed (depending on the parity setting)
675
-- the rx_wr_o signal will pulse high for one clock period, indicating rx_dat_o
676
-- contains valid receive data.  The rx_wr_o pulse is only issued if there is
677
-- no parity error, and the stop bit is actually detected high at the sampling
678
-- time.
679
-- The rx_dat_o outputs will hold the received data until the next rx_wr_o pulse.
680
-- The rx_dat_o output provides the received data, even in the presence of a
681
-- parity or stop-bit error.
682
-- Error flags are valid during rx_wr_o, but they remain latched, until
683
-- rx_restart_i.
684
--
685
-- The Baud rate is equal to the frequency of the rate_clk_i input.
686
-- It should be, as nearly as possible, a square wave of the desired
687
-- communications rate.
688
--
689
library IEEE;
690
use IEEE.STD_LOGIC_1164.ALL;
691
use IEEE.NUMERIC_STD.ALL;
692
 
693
entity uart_sqclk is
694
    port (
695
 
696
      sys_rst_n     : in std_logic;
697
      sys_clk       : in std_logic;
698
      sys_clk_en    : in std_logic;
699
 
700
      -- rate and parity
701
      parity_i      : in unsigned(1 downto 0); -- 0=none, 1=even, 2 or 3=odd
702
      rate_clk_i    : in std_logic;
703
 
704
      -- serial I/O
705
      tx_stream     : out std_logic;
706
      rx_stream     : in std_logic;
707
 
708
      --control and status
709
      tx_wr_i       : in  std_logic;        -- Starts Transmit
710
      tx_dat_i      : in  unsigned(7 downto 0);
711
      tx_done_o     : out std_logic;
712
      rx_restart_i  : in  std_logic;        -- High clears error flags, clears rx_done_o
713
      rx_dat_o      : out unsigned(7 downto 0);
714
      rx_wr_o       : out std_logic;        -- High pulse means store rx_dat_o.
715
      rx_done_o     : out std_logic;        -- Remains high after receive, until clk edge with rx_restart_i=1
716
      frame_err_o   : out std_logic;        -- High = error.  Reset when rx_restart_i asserted.
717
      parity_err_o  : out std_logic         -- High = error.  Reset when rx_restart_i asserted.
718
    );
719
end uart_sqclk;
720
 
721
architecture beh of uart_sqclk is
722
 
723
-- Components
724
 
725
begin
726
 
727
  tx1: entity work.async_tx_sqclk(beh)
728
    port map (
729
       sys_rst_n     => sys_rst_n,
730
       sys_clk       => sys_clk,
731
       sys_clk_en    => sys_clk_en,
732
 
733
       -- rate and parity
734
       tx_parity_i   => parity_i, -- 0=none, 1=even, 2 or 3=odd
735
       tx_clk_i      => rate_clk_i,
736
 
737
       -- serial output
738
       tx_stream     => tx_stream,
739
 
740
       -- control and status
741
       tx_wr_i       => tx_wr_i,       -- Starts Transmit
742
       tx_dat_i      => tx_dat_i,
743
       tx_done_o     => tx_done_o
744
    );
745
 
746
  rx1: entity work.async_rx_sqclk(beh)
747
    port map (
748
       sys_rst_n     => sys_rst_n,
749
       sys_clk       => sys_clk,
750
       sys_clk_en    => sys_clk_en,
751
 
752
       -- rate and parity
753
       rx_parity_i   => parity_i, -- 0=none, 1=even, 2 or 3=odd
754
       rx_clk_i      => rate_clk_i,
755
 
756
       -- serial input
757
       rx_stream     => rx_stream,
758
 
759
       -- control and status
760
       rx_restart_i  => rx_restart_i,  -- High clears error flags, clears rx_done_o
761
       rx_dat_o      => rx_dat_o,
762
       rx_wr_o       => rx_wr_o,       -- High pulse means store rx_dat_o.
763
       rx_done_o     => rx_done_o,     -- Remains high after receive, until rx_restart_i
764
       frame_err_o   => frame_err_o,   -- High = error.  Reset when rx_restart_i asserted.
765
       parity_err_o  => parity_err_o   -- High = error.  Reset when rx_restart_i asserted.
766
    );
767
 
768
end beh;
769
 
770
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.