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

Subversion Repositories layer2

[/] [layer2/] [trunk/] [vhdl/] [rs232/] [rtl/] [uartr.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
--------------------------------------------------------------------------------
2
-- UART Receiver 19200/8N1                                                    --
3
--------------------------------------------------------------------------------
4
-- Copyright (C)2011  Mathias Hörtnagl <mathias.hoertnagl@gmail.comt>         --
5
--                                                                            --
6
-- This program is free software: you can redistribute it and/or modify       --
7
-- it under the terms of the GNU General Public License as published by       --
8
-- the Free Software Foundation, either version 3 of the License, or          --
9
-- (at your option) any later version.                                        --
10
--                                                                            --
11
-- This program is distributed in the hope that it will be useful,            --
12
-- but WITHOUT ANY WARRANTY; without even the implied warranty of             --
13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              --
14
-- GNU General Public License for more details.                               --
15
--                                                                            --
16
-- You should have received a copy of the GNU General Public License          --
17
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.      --
18
--------------------------------------------------------------------------------
19
library ieee;
20
use ieee.std_logic_1164.all;
21
use ieee.numeric_std.all;
22
 
23
library work;
24
use work.iwb.all;
25
use work.iuart.all;
26
 
27
entity uartr is
28
   port(
29
      si            : in  slave_in_t;
30
      so            : out slave_out_t;
31
   -- Non-Wishbone Signals
32
      RS232_DCE_RXD : in  std_logic
33
   );
34
end uartr;
35
 
36
architecture rtl of uartr is
37
 
38
   type state_t is (Idle, Start, Data, Stop, Ack, Ack2);
39
 
40
   type receiver_t is record
41
      s : state_t;                           -- Receiver state.
42
      n : natural range 0 to 15;             -- Tick counter.
43
      m : natural range 0 to 7;              -- Data bits counter.
44
      d : std_logic_vector(7 downto 0);      -- Data bits shift register.
45
   end record;
46
 
47
   type rx_t is record
48
      tick : std_logic;
49
      rst  : std_logic;
50
      ack  : std_logic;
51
   end record;
52
 
53
   signal rcv, rcvin : receiver_t;
54
   signal rx : rx_t;
55
begin
56
 
57
   -----------------------------------------------------------------------------
58
   -- Receiver Rate Generator                                                 --
59
   -----------------------------------------------------------------------------
60
   rx_rate : counter
61
      generic map(
62
         FREQ => 50,
63
         RATE => 19200
64
      )
65
      port map(
66
         clk  => si.clk,
67
         rst  => rx.rst,
68
         tick => rx.tick
69
      );
70
 
71
   -----------------------------------------------------------------------------
72
   -- Receiver Controller                                                     --
73
   -----------------------------------------------------------------------------
74
   receiver : process(RS232_DCE_RXD, rcv, rx.tick, si)
75
   begin
76
 
77
      rcvin  <= rcv;
78
      rx.rst <= '0';
79
      rx.ack <= '0';
80
 
81
      case rcv.s is
82
 
83
         -- Wait for receive signal to be set low - Start of new data package.
84
         when Idle =>
85
            rx.rst <= '1';
86
            if RS232_DCE_RXD = '0' then
87
               rcvin.n <= 0;
88
               rcvin.s <= Start;
89
            end if;
90
 
91
         when Start =>
92
            if rx.tick = '1' then
93
               if rcv.n = 7 then
94
                  rcvin.n <= 0;
95
                  rcvin.m <= 0;
96
                  rcvin.s <= Data;
97
               else
98
                  rcvin.n <= rcv.n + 1;
99
               end if;
100
            end if;
101
 
102
         -- Shift in all data bits. Least significant bit first.
103
         when Data =>
104
            if rx.tick = '1' then
105
               if rcv.n = 15 then
106
                  rcvin.n <= 0;
107
                  rcvin.d <= RS232_DCE_RXD & rcv.d(7 downto 1);
108
                  if rcv.m = 7 then
109
                     rcvin.s <= Stop;
110
                  else
111
                     rcvin.m <= rcv.m + 1;
112
                  end if;
113
               else
114
                  rcvin.n <= rcv.n + 1;
115
               end if;
116
            end if;
117
 
118
         when Stop =>
119
            if rx.tick = '1' then
120
               if rcv.n = 15 then
121
                  rcvin.s <= Ack;
122
               else
123
                  rcvin.n <= rcv.n + 1;
124
               end if;
125
            end if;
126
 
127
         when Ack =>
128
            if wb_read(si) then
129
               rx.ack <= '1';
130
               rcvin.s <= Ack2;
131
            end if;
132
 
133
         when Ack2 =>
134
            rx.ack <= '1';
135
            if si.stb = '0' then
136
               rcvin.s <= Idle;
137
            end if;
138
      end case;
139
   end process;
140
 
141
   so.dat <= x"0000" & rx.ack & "0000000" & rcv.d;
142
   so.ack <= rx.ack;
143
 
144
   -----------------------------------------------------------------------------
145
   -- Registers                                                               --
146
   -----------------------------------------------------------------------------
147
   reg : process(si.clk)
148
   begin
149
      if rising_edge(si.clk) then
150
         rcv <= rcvin;
151
         if si.rst = '1' then
152
            rcv.s <= Idle;
153
         end if;
154
      end if;
155
   end process;
156
end rtl;

powered by: WebSVN 2.1.0

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