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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.6/] [rtl/] [vlib/] [serport/] [serport_uart_rx.vhd] - Blame information for rev 24

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 wfjm
-- $Id: serport_uart_rx.vhd 421 2011-11-07 21:23:50Z mueller $
2 2 wfjm
--
3 13 wfjm
-- Copyright 2007-2011 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 2 wfjm
--
5
-- This program is free software; you may redistribute and/or modify it under
6
-- the terms of the GNU General Public License as published by the Free
7
-- Software Foundation, either version 2, or at your option any later version.
8
--
9
-- This program is distributed in the hope that it will be useful, but
10
-- WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
-- for complete details.
13
--
14
-- The uart expects CLKDIV+1 wide input bit symbols.
15
-- This implementation counts the number of 1's in the first CLKDIV clock
16
-- cycles, and checks in the last cycle of the symbol time whether the
17
-- number of 1's was > CLKDIV/2. This supresses short glitches nicely,
18
-- especially for larger clock dividers.
19
--
20
------------------------------------------------------------------------------
21
-- Module Name:    serport_uart_rx - syn
22
-- Description:    serial port UART - receiver
23
--
24
-- Dependencies:   -
25
-- Test bench:     tb/tb_serport_uart_rxtx
26
-- Target Devices: generic
27 13 wfjm
-- Tool versions:  xst 8.2, 9.1, 9.2, 13.1; ghdl 0.18-0.29
28 2 wfjm
-- Revision History: 
29
-- Date         Rev Version  Comment
30 13 wfjm
-- 2011-10-22   417   2.0.3  now numeric_std clean
31 2 wfjm
-- 2009-07-12   233   2.0.2  remove snoopers
32
-- 2008-03-02   121   2.0.1  comment out snoopers
33
-- 2007-10-21    91   2.0    re-designed and -implemented with state machine.
34
--                           allow CLKDIV=0 with 1 stop bit; allow max. CLKDIV
35
--                           (all 1's); aborts bad start bit after 1/2 cell;
36
--                           accepts stop bit after 1/2 cell, permits tx clock
37
--                           be ~3 percent faster than rx clock.
38
--                           for 3s1000ft256: 50 -> 58 slices for CDWIDTH=13
39
-- 2007-10-14    89   1.1    almost full rewrite, handles now CLKDIV=0 properly
40
--                           for 3s1000ft256: 43 -> 50 slices for CDWIDTH=13
41
-- 2007-10-12    88   1.0.1  avoid ieee.std_logic_unsigned, use cast to unsigned
42
-- 2007-06-30    62   1.0    Initial version 
43
------------------------------------------------------------------------------
44
 
45
library ieee;
46
use ieee.std_logic_1164.all;
47 13 wfjm
use ieee.numeric_std.all;
48 2 wfjm
 
49
use work.slvtypes.all;
50
 
51
entity serport_uart_rx is               -- serial port uart: receive part
52
  generic (
53
    CDWIDTH : positive := 13);          -- clk divider width
54
  port (
55
    CLK : in slbit;                     -- clock
56
    RESET : in slbit;                   -- reset
57
    CLKDIV : in slv(CDWIDTH-1 downto 0); -- clock divider setting
58
    RXSD : in slbit;                    -- receive serial data (uart view)
59
    RXDATA : out slv8;                  -- receiver data out
60
    RXVAL : out slbit;                  -- receiver data valid
61
    RXERR : out slbit;                  -- receiver data error (frame error)
62
    RXACT : out slbit                   -- receiver active
63
  );
64
end serport_uart_rx;
65
 
66
 
67
architecture syn of serport_uart_rx is
68
 
69
  type state_type is (
70
    s_idle,                             -- s_idle:  idle
71
    s_colb0,                            -- s_colb0: collect b0 (start bit)
72
    s_endb0,                            -- s_endb0: finish  b0 (start bit)
73
    s_colbx,                            -- s_colbx: collect bx
74
    s_endbx,                            -- s_endbx: finish  bx
75
    s_colb9,                            -- s_colb9: collect bx (stop bit)
76
    s_endb9                             -- s_endb9: finish  bx (stop bit)
77
  );
78
 
79
  type regs_type is record
80
    state : state_type;                 -- state
81
    ccnt : slv(CDWIDTH-1 downto 0);     -- clock divider counter
82
    dcnt : slv(CDWIDTH   downto 0);     -- data '1' counter
83
    bcnt : slv4;                        -- bit counter
84
    sreg : slv8;                        -- input shift register
85
  end record regs_type;
86
 
87
  constant ccntzero : slv(CDWIDTH-1 downto 0) := (others=>'0');
88
  constant dcntzero : slv(CDWIDTH   downto 0) := (others=>'0');
89
  constant regs_init : regs_type := (
90 13 wfjm
    s_idle,                             -- state
91
    ccntzero,                           -- ccnt
92
    dcntzero,                           -- dcnt
93
    (others=>'0'),                      -- bcnt
94
    (others=>'0')                       -- sreg
95 2 wfjm
  );
96
 
97
  signal R_REGS : regs_type := regs_init;  -- state registers
98
  signal N_REGS : regs_type := regs_init;  -- next value state regs
99
 
100
begin
101
 
102
  proc_regs: process (CLK)
103
  begin
104
 
105 13 wfjm
    if rising_edge(CLK) then
106 2 wfjm
      R_REGS <= N_REGS;
107
    end if;
108
 
109
  end process proc_regs;
110
 
111
  proc_next: process (R_REGS, RESET, CLKDIV, RXSD)
112
 
113
    variable r : regs_type := regs_init;
114
    variable n : regs_type := regs_init;
115
 
116
    variable dbit : slbit := '0';
117
    variable ld_ccnt : slbit := '0';
118
    variable tc_ccnt : slbit := '0';
119
    variable tc_bcnt : slbit := '0';
120
    variable ld_dcnt : slbit := '0';
121
    variable ld_bcnt : slbit := '0';
122
    variable ce_bcnt : slbit := '0';
123
    variable iact : slbit := '0';
124
    variable ival : slbit := '0';
125
    variable ierr : slbit := '0';
126
 
127
  begin
128
 
129
    r := R_REGS;
130
    n := R_REGS;
131
 
132
    dbit := '0';
133
    ld_ccnt := '0';
134
    tc_ccnt := '0';
135
    tc_bcnt := '0';
136
    ld_dcnt := '0';
137
    ld_bcnt := '0';
138
    ce_bcnt := '0';
139
    iact := '1';
140
    ival := '0';
141
    ierr := '0';
142
 
143
    if unsigned(r.ccnt) = 0 then
144
      tc_ccnt := '1';
145
    end if;
146
    if unsigned(r.bcnt) = 9 then
147
      tc_bcnt := '1';
148
    end if;
149
 
150
    if unsigned(r.dcnt) > unsigned("00" & CLKDIV(CDWIDTH-1 downto 1)) then
151
      dbit := '1';
152
    end if;
153
 
154
    case r.state is
155
 
156
      when s_idle =>                    -- s_idle: idle ----------------------
157
        iact := '0';
158
        ld_dcnt := '1';                   -- always keep dcnt in reset
159
        if RXSD = '0' then                -- if start bit seen
160
          if tc_ccnt = '1' then
161
            n.state := s_endb0;             -- finish b0
162
            ld_ccnt := '1';                 -- start next bit
163
            ce_bcnt := '1';
164
          else
165
            n.state := s_colb0;             -- collect b0
166
          end if;
167
        else                              -- otherwise
168
          ld_ccnt := '1';                   -- keep all counters in reset
169
          ld_bcnt := '1';
170
        end if;
171
 
172
      when s_colb0 =>                   -- s_colb0: collect b0 (start bit) ---
173
        if tc_ccnt = '1' then           -- last cycle of b0 ?
174
          n.state := s_endb0;             -- finish b0
175
          ld_ccnt := '1';                 -- "
176
          ce_bcnt := '1';
177
        else                            -- continue in b0 ?
178
          if dbit='1' and RXSD='1' then   -- too many 1's ?
179
            n.state := s_idle;              -- abort to idle
180
            ld_dcnt := '1';                 -- put counters in reset
181
            ld_ccnt := '1';
182
            ld_bcnt := '1';
183
          end if;
184
        end if;
185
 
186
      when s_endb0 =>                   -- s_endb0: finish  b0 (start bit) ---
187
        ld_dcnt := '1';                 -- start next bit
188
        if dbit = '1' then              -- was it a 1 ?
189
          n.state := s_idle;              -- abort to idle
190
          ld_ccnt := '1';                 -- put counters in reset
191
          ld_bcnt := '1';
192
        else
193
          if tc_ccnt = '1' then           -- last cycle of bx ?
194
            n.state := s_endbx;             -- finish bx
195
            ld_ccnt := '1';
196
            ce_bcnt := '1';
197
          else                            -- continue in b0 ?
198
            n.state := s_colbx;             -- collect bx
199
          end if;
200
        end if;
201
 
202
      when s_colbx =>                   -- s_colbx: collect bx ---------------
203
        if tc_ccnt = '1' then           -- last cycle of bx ?
204
          n.state := s_endbx;             -- finish bx
205
          ld_ccnt := '1';
206
          ce_bcnt := '1';
207
        end if;
208
 
209
      when s_endbx =>                   -- s_endbx: finish  bx ---------------
210
        ld_dcnt := '1';                 -- start next bit
211
        n.sreg := dbit & r.sreg(7 downto 1);
212
        if tc_ccnt = '1' then           -- last cycle of bx ?
213
          if tc_bcnt = '1' then
214
            n.state := s_endb9;             -- finish b9
215
            ld_bcnt := '1';                 -- and wrap bcnt
216
          else
217
            n.state := s_endbx;             -- finish bx
218
            ce_bcnt := '1';
219
          end if;
220
          ld_ccnt := '1';
221
        else                            -- continue in bx ?
222
          if tc_bcnt = '1' then
223
            n.state := s_colb9;             -- collect b9
224
          else
225
            n.state := s_colbx;             -- collect bx
226
          end if;
227
        end if;
228
 
229
      when s_colb9 =>                   -- s_colb9: collect bx (stop bit) ----
230
        if tc_ccnt = '1' then           -- last cycle of b9 ?
231
          n.state := s_endb9;             -- finish b9
232
          ld_ccnt := '1';                 -- "
233
          ld_bcnt := '1';                 -- and wrap bcnt
234
        else                            -- continue in b9 ?
235
          if dbit='1' and RXSD='1' then   -- already enough 1's ?
236
            n.state := s_idle;              -- finish to idle
237
            ld_dcnt := '1';                 -- put counters in reset
238
            ld_ccnt := '1';
239
            ld_bcnt := '1';
240
            ival := '1';
241
          end if;
242
        end if;
243
 
244
      when s_endb9 =>                   -- s_endb9: finish  bx (stop bit) ----
245
        ld_dcnt := '1';                 -- start next bit
246
        if dbit = '1' then              -- was it a valid stop bit ?
247
          ival := '1';
248
        else
249
          ierr := '1';
250
        end if;
251
        if RXSD = '1' then              -- line in idle state ?
252
          n.state := s_idle;              -- finish to idle state
253
          ld_ccnt := '1';                 -- and put counters in reset
254
          ld_bcnt := '1';                 -- "
255
        else
256
          if tc_ccnt = '1' then           -- last cycle of b9 ?
257
            n.state := s_endb0;             -- finish b0
258
            ld_ccnt := '1';                 -- "
259
            ce_bcnt := '1';
260
          else                            -- continue in b0 ?
261
            n.state := s_colb0;             -- collect bx
262
          end if;
263
        end if;
264
 
265
      when others => null;              -- -----------------------------------
266
 
267
    end case;
268
 
269
    if RESET = '1' then                 -- RESET seen
270
      ld_ccnt := '1';                     -- keep all counters in reset
271
      ld_dcnt := '1';
272
      ld_bcnt := '1';
273
      n.state := s_idle;
274
    end if;
275
 
276
    if ld_ccnt = '1' then               -- implement ccnt
277
      n.ccnt := CLKDIV;
278
    else
279 13 wfjm
      n.ccnt := slv(unsigned(r.ccnt) - 1);
280 2 wfjm
    end if;
281
 
282
    if ld_dcnt = '1' then               -- implement dcnt
283
      n.dcnt(CDWIDTH downto 1) := (others=>'0');
284
      n.dcnt(0) := RXSD;
285
    else
286
      if RXSD = '1' then
287 13 wfjm
        n.dcnt := slv(unsigned(r.dcnt) + 1);
288 2 wfjm
      end if;
289
    end if;
290
 
291
    if ld_bcnt = '1' then               -- implement bcnt
292
      n.bcnt := (others=>'0');
293
    else
294
      if ce_bcnt = '1' then
295 13 wfjm
        n.bcnt := slv(unsigned(r.bcnt) + 1);
296 2 wfjm
      end if;
297
    end if;
298
 
299
    N_REGS <= n;
300
 
301
    RXDATA  <= r.sreg;
302
    RXACT   <= iact;
303
    RXVAL   <= ival;
304
    RXERR   <= ierr;
305
 
306
  end process proc_next;
307
 
308
end syn;

powered by: WebSVN 2.1.0

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