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

Subversion Repositories uart2bus

[/] [uart2bus/] [trunk/] [vhdl/] [rtl/] [uartRx.vhd] - Blame information for rev 12

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 smuller
-----------------------------------------------------------------------------------------
2
-- uart receive module  
3
--
4
-----------------------------------------------------------------------------------------
5 11 smuller
library ieee;
6
use ieee.std_logic_1164.ALL;
7
use ieee.std_logic_unsigned.ALL;
8 6 smuller
 
9
entity uartRx is
10
  port ( clr       : in  std_logic;                    -- global reset input
11
         clk       : in  std_logic;                    -- global clock input
12
         ce16      : in  std_logic;                    -- baud rate multiplyed by 16 - generated by baud module
13
         serIn     : in  std_logic;                    -- serial data input
14
         rxData    : out std_logic_vector(7 downto 0); -- data byte received
15
         newRxData : out std_logic);                   -- signs that a new byte was received
16
end uartRx;
17
 
18
architecture Behavioral of uartRx is
19
 
20
  signal ce1      : std_logic;                    -- clock enable at bit rate
21
  signal ce1Mid   : std_logic;                    -- clock enable at the middle of each bit - used to sample data
22
  signal inSync   : std_logic_vector(1 downto 0);
23
  signal count16  : std_logic_vector(3 downto 0);
24
  signal rxBusy   : std_logic;
25
  signal bitCount : std_logic_vector(3 downto 0);
26
  signal dataBuf  : std_logic_vector(7 downto 0);
27
 
28
  begin
29
    -- input async input is sampled twice
30
    process (clr, clk)
31
    begin
32
      if (clr = '1') then
33
        inSync <= (others => '1');
34
      elsif (rising_edge(clk)) then
35
        inSync <= inSync(0) & serIn;
36
      end if;
37
    end process;
38
    -- a counter to count 16 pulses of ce_16 to generate the ce_1 and ce_1_mid pulses.
39
    -- this counter is used to detect the start bit while the receiver is not receiving and
40
    -- signs the sampling cycle during reception.
41
    process (clr, clk)
42
    begin
43
      if (clr = '1') then
44
        count16 <= (others => '0');
45
      elsif (rising_edge(clk)) then
46
        if (ce16 = '1') then
47
          if ((rxBusy = '1') or (inSync(1) = '0')) then
48
            count16 <= count16 + 1;
49
          else
50
            count16 <= (others => '0');
51
          end if;
52
        end if;
53
      end if;
54
    end process;
55
    -- receiving busy flag
56
    process (clr, clk)
57
    begin
58
      if (clr = '1') then
59
        rxBusy <= '0';
60
      elsif (rising_edge(clk)) then
61
        if ((rxBusy = '0') and (ce1Mid = '1')) then
62
          rxBusy <= '1';
63 11 smuller
        elsif ((rxBusy = '1') and (bitCount = "1000") and (ce1Mid = '1')) then
64 6 smuller
          rxBusy <= '0';
65
        end if;
66
      end if;
67
    end process;
68
    -- bit counter
69
    process (clr, clk)
70
    begin
71
      if (clr = '1') then
72
        bitCount <= (others => '0');
73
      elsif (rising_edge(clk)) then
74
        if (rxBusy = '0') then
75
          bitCount <= (others => '0');
76
        elsif ((rxBusy = '1') and (ce1Mid = '1')) then
77
          bitCount <= bitCount + 1;
78
        end if;
79
      end if;
80
    end process;
81
    -- data buffer shift register
82
    process (clr, clk)
83
    begin
84
      if (clr = '1') then
85
        dataBuf <= (others => '0');
86
      elsif (rising_edge(clk)) then
87
        if ((rxBusy = '1') and (ce1Mid = '1')) then
88
          dataBuf <= inSync(1) & dataBuf(7 downto 1);
89
        end if;
90
      end if;
91
    end process;
92
    -- data output and flag
93
    process (clr, clk)
94
    begin
95
      if (clr = '1') then
96
        rxData <= (others => '0');
97
        newRxData <= '0';
98
      elsif (rising_edge(clk)) then
99
        if ((rxBusy = '1') and (bitCount = "1000") and (ce1 = '1')) then
100
          rxData <= dataBuf;
101
          newRxData <= '1';
102
        else
103
          newRxData <= '0';
104
        end if;
105
      end if;
106
    end process;
107
    -- ce_1 pulse indicating expected end of current bit
108
    ce1 <= '1' when ((count16 = "1111") and (ce16 = '1')) else '0';
109
    -- ce_1_mid pulse indication the sampling clock cycle of the current data bit
110
    ce1Mid <= '1' when ((count16 = "0111") and (ce16 = '1')) else '0';
111
  end Behavioral;

powered by: WebSVN 2.1.0

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