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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [rtl/] [misclib/] [dcom_uart.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
------------------------------------------------------------------------------
2
--  This file is a part of the GRLIB VHDL IP LIBRARY
3
--  Copyright (C) 2003 - 2008, Gaisler Research
4
--  Copyright (C) 2008 - 2014, Aeroflex Gaisler
5
--  Copyright (C) 2015 - 2018, Cobham Gaisler
6
--
7
--  This program is free software; you can redistribute it and/or modify
8
--  it under the terms of the GNU General Public License as published by
9
--  the Free Software Foundation; either version 2 of the License, or
10
--  (at your option) any later version.
11
--
12
--  This program is distributed in the hope that it will be useful,
13
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
--  GNU General Public License for more details.
16
--
17
--  You should have received a copy of the GNU General Public License
18
--  along with this program; if not, write to the Free Software
19
--  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20
-----------------------------------------------------------------------------
21
-- Entity:      dcom_uart
22
-- File:        dcom_uart.vhd
23
-- Author:      Jiri Gaisler - Gaisler Research
24
-- Description: Asynchronous UART with baud-rate detection.
25
------------------------------------------------------------------------------
26
 
27
library ieee;
28
use ieee.std_logic_1164.all;
29
library commonlib;
30
use commonlib.types_common.all;
31
--! AMBA system bus specific library.
32
library ambalib;
33
--! AXI4 configuration constants.
34
use ambalib.types_amba4.all;
35
library misclib;
36
use misclib.types_misc.all;
37
--pragma translate_off
38
use std.textio.all;
39
--pragma translate_on
40
 
41
entity dcom_uart is
42
  port (
43
    rst    : in  std_ulogic;
44
    clk    : in  std_ulogic;
45
    i_cfg_frame : in std_logic;
46
    i_cfg_ovf   : in std_logic;
47
    i_cfg_break : in std_logic;
48
    i_cfg_tcnt  : in std_logic_vector(1 downto 0);
49
    i_cfg_rxen  : in std_logic;
50
    i_cfg_brate : in std_logic_vector(17 downto 0);
51
    i_cfg_scaler : in std_logic_vector(17 downto 0);
52
    o_cfg_scaler : out std_logic_vector(31 downto 0);
53
    o_cfg_rxen : out std_logic;
54
    o_cfg_txen : out std_logic;
55
    o_cfg_flow : out std_logic;
56
 
57
    i_com_read          : in std_ulogic;
58
    i_com_write         : in std_ulogic;
59
    i_com_data          : in std_logic_vector(7 downto 0);
60
    o_com_dready        : out std_ulogic;
61
    o_com_tsempty       : out std_ulogic;
62
    o_com_thempty       : out std_ulogic;
63
    o_com_lock          : out std_ulogic;
64
    o_com_enable        : out std_ulogic;
65
    o_com_data          : out std_logic_vector(7 downto 0);
66
    ui     : in  uart_in_type;
67
    uo     : out uart_out_type
68
  );
69
end;
70
 
71
architecture rtl of dcom_uart is
72
 
73
type rxfsmtype is (idle, startbit, data, stopbit);
74
type txfsmtype is (idle, data);
75
 
76
type uartregs is record
77
  rxen          :  std_ulogic;  -- receiver enabled
78
  dready        :  std_ulogic;  -- data ready
79
  rsempty       :  std_ulogic;  -- receiver shift register empty (internal)
80
  tsempty       :  std_ulogic;  -- transmitter shift register empty
81
  thempty       :  std_ulogic;  -- transmitter hold register empty
82
  break         :  std_ulogic;  -- break detected
83
  ovf           :  std_ulogic;  -- receiver overflow
84
  frame         :  std_ulogic;  -- framing error
85
  rhold         :  std_logic_vector(7 downto 0);
86
  rshift        :  std_logic_vector(7 downto 0);
87
  tshift        :  std_logic_vector(9 downto 0);
88
  thold         :  std_logic_vector(7 downto 0);
89
  txstate       :  txfsmtype;
90
  txclk         :  std_logic_vector(2 downto 0);  -- tx clock divider
91
  txtick        :  std_ulogic;  -- tx clock (internal)
92
  rxstate       :  rxfsmtype;
93
  rxclk         :  std_logic_vector(2 downto 0); -- rx clock divider
94
  rxdb          :  std_logic_vector(1 downto 0);   -- rx data filtering buffer
95
  rxtick        :  std_ulogic;  -- rx clock (internal)
96
  tick          :  std_ulogic;  -- rx clock (internal)
97
  scaler        :  std_logic_vector(17 downto 0);
98
  brate         :  std_logic_vector(17 downto 0);
99
  tcnt          :  std_logic_vector(1 downto 0); -- autobaud counter
100
  rxf           :  std_logic_vector(4 downto 0); --  rx data filtering buffer
101
  fedge         :  std_ulogic;   -- rx falling edge
102
end record;
103
 
104
constant RESET_ALL : boolean := false;
105
constant RES : uartregs := (
106
  rxen => '0', dready => '0', rsempty => '1', tsempty => '1',  thempty => '1',
107
  break => '0', ovf => '0', frame => '0', rhold => (others => '0'),
108
  rshift => (others => '0'), tshift => (others => '1'), thold => (others => '0'),
109
  txstate => idle, txclk => (others => '0'), txtick => '0', rxstate => idle,
110
  rxclk => (others => '0'), rxdb => (others => '0'),  rxtick => '0', tick => '0',
111
  scaler => "111111111111111011", brate => (others => '1'), tcnt => (others => '0'),
112
  rxf => (others => '0'), fedge => '0');
113
 
114
signal r, rin : uartregs;
115
 
116
begin
117
 
118
  uartop : process(rst, r, ui, i_cfg_frame, i_cfg_ovf, i_cfg_break,
119
                  i_cfg_tcnt, i_cfg_rxen, i_cfg_brate, i_cfg_scaler,
120
                  i_com_read, i_com_write, i_com_data )
121
  variable scaler : std_logic_vector(17 downto 0);
122
  variable rxclk, txclk : std_logic_vector(2 downto 0);
123
  variable irxd : std_ulogic;
124
  variable v : uartregs;
125
 
126
  begin
127
 
128
    v := r;
129
    v.txtick := '0'; v.rxtick := '0'; v.tick := '0';
130
    v.rxdb(1) := r.rxdb(0);
131
 
132
-- scaler
133
 
134
    if r.tcnt = "11" then scaler := r.scaler - 1;
135
    else scaler := r.scaler + 1; end if;
136
 
137
    if r.tcnt /= "11" then
138
      if (r.rxdb(1) and not r.rxdb(0)) = '1' then v.fedge := '1'; end if;
139
      if (r.fedge) = '1' then
140
        v.scaler := scaler;
141
        if (v.scaler(17) and not r.scaler(16)) = '1' then
142
          v.scaler := "111111111111111011";
143
          v.fedge := '0'; v.tcnt := "00";
144
        end if;
145
      end if;
146
      if (r.rxdb(1) and r.fedge and not r.rxdb(0)) = '1' then
147
        if (r.brate(17 downto 4)> r.scaler(17 downto 4)) then
148
          v.brate := r.scaler; v.tcnt := "00";
149
        end if;
150
        v.scaler := "111111111111111011";
151
        if (r.brate(17 downto 4) = r.scaler(17 downto 4)) then
152
          v.tcnt := r.tcnt + 1;
153
          if r.tcnt = "10" then
154
            v.brate := "0000" & r.scaler(17 downto 4);
155
            v.scaler := v.brate; v.rxen := '1';
156
          end if;
157
        end if;
158
      end if;
159
    else
160
      if (r.break and r.rxdb(1)) = '1' then
161
        v.scaler := "111111111111111011";
162
        v.brate := (others => '1'); v.tcnt := "00";
163
        v.break := '0'; v.rxen := '0';
164
      end if;
165
    end if;
166
 
167
    if r.rxen = '1' then
168
      v.scaler := scaler;
169
      v.tick := scaler(15) and not r.scaler(15);
170
      if v.tick = '1' then v.scaler := r.brate; end if;
171
    end if;
172
 
173
-- read/write registers
174
 
175
    if i_com_read = '1' then v.dready := '0'; end if;
176
 
177
 
178
-- tx clock
179
 
180
    txclk := r.txclk + 1;
181
    if r.tick = '1' then
182
      v.txclk := txclk; v.txtick := r.txclk(2) and not txclk(2);
183
    end if;
184
 
185
-- rx clock
186
 
187
    rxclk := r.rxclk + 1;
188
    if r.tick = '1' then
189
      v.rxclk := rxclk; v.rxtick := r.rxclk(2) and not rxclk(2);
190
    end if;
191
 
192
-- filter rx data
193
 
194
    v.rxf(1 downto 0) := r.rxf(0) & ui.rd;     -- meta-stability filter
195
    if ((r.tcnt /= "11") and (r.scaler(0 downto 0) = "1")) or
196
       ((r.tcnt = "11") and (r.tick = '1'))
197
    then v.rxf(4 downto 2) := r.rxf(3 downto 1); end if;
198
    v.rxdb(0) := (r.rxf(4) and r.rxf(3)) or (r.rxf(4) and r.rxf(2)) or
199
                  (r.rxf(3) and r.rxf(2));
200
    irxd := r.rxdb(0);
201
 
202
-- transmitter operation
203
 
204
    case r.txstate is
205
    when idle =>        -- idle and stop bit state
206
      if (r.txtick = '1') then v.tsempty := '1'; end if;
207
      if (r.rxen and (not r.thempty) and r.txtick) = '1' then
208
        v.tshift := '0' & r.thold & '0'; v.txstate := data;
209
        v.thempty := '1';
210
        v.tsempty := '0'; v.txclk := "00" & r.tick; v.txtick := '0';
211
      end if;
212
    when data =>        -- transmit data frame
213
      if r.txtick = '1' then
214
        v.tshift := '1' & r.tshift(9 downto 1);
215
        if r.tshift(9 downto 1) = "111111110" then
216
        v.tshift(0) := '1'; v.txstate := idle;
217
        end if;
218
      end if;
219
    end case;
220
 
221
-- writing of tx data register must be done after tx fsm to get correct
222
-- operation of thempty flag
223
 
224
    if i_com_write = '1' and r.thempty = '1' then
225
      v.thold := i_com_data(7 downto 0); v.thempty := '0';
226
    end if;
227
 
228
-- receiver operation
229
 
230
    case r.rxstate is
231
    when idle =>        -- wait for start bit
232
      if ((not r.rsempty) and not r.dready) = '1' then
233
        v.rhold := r.rshift; v.rsempty := '1'; v.dready := '1';
234
      end if;
235
      if (r.rxen and r.rxdb(1) and (not irxd)) = '1' then
236
        v.rxstate := startbit; v.rshift := (others => '1'); v.rxclk := "100";
237
        if v.rsempty = '0' then v.ovf := '1'; end if;
238
        v.rsempty := '0'; v.rxtick := '0';
239
      end if;
240
    when startbit =>    -- check validity of start bit
241
      if r.rxtick = '1' then
242
        if irxd = '0' then
243
          v.rshift := irxd & r.rshift(7 downto 1); v.rxstate := data;
244
        else
245
          v.rxstate := idle;
246
        end if;
247
      end if;
248
    when data =>        -- receive data frame
249
      if r.rxtick = '1' then
250
        v.rshift := irxd & r.rshift(7 downto 1);
251
        if r.rshift(0) = '0' then
252
        v.rxstate := stopbit;
253
        end if;
254
      end if;
255
    when stopbit =>     -- receive stop bit
256
      if r.rxtick = '1' then
257
        if irxd = '1' then
258
          v.rsempty := '0';
259
          if v.dready = '0' then
260
            v.rhold := r.rshift; v.rsempty := '1'; v.dready := '1';
261
          end if;
262
        else
263
          if r.rshift = "00000000" then
264
            v.break := '1';              -- break
265
          else
266
            v.frame := '1';              -- framing error
267
          end if;
268
          v.rsempty := '1';
269
        end if;
270
        v.rxstate := idle;
271
      end if;
272
    when others =>
273
      v.rxstate := idle;
274
    end case;
275
 
276
-- reset operation
277
 
278
    if not RESET_ALL and rst = '0' then
279
      v.frame := i_cfg_frame;
280
      v.rsempty := RES.rsempty;
281
      v.ovf := i_cfg_ovf;
282
      v.break := i_cfg_break; v.thempty := RES.thempty;
283
      v.tsempty := RES.tsempty; v.dready := RES.dready; v.fedge := RES.fedge;
284
      v.txstate := RES.txstate; v.rxstate := RES.rxstate; v.tshift(0) := RES.tshift(0);
285
      v.scaler := i_cfg_scaler;
286
      v.brate := i_cfg_brate;
287
      v.rxen := i_cfg_rxen;
288
      v.tcnt := i_cfg_tcnt;
289
      v.txclk := RES.txclk; v.rxclk := RES.rxclk;
290
    end if;
291
 
292
-- update registers
293
 
294
    rin <= v;
295
 
296
-- drive outputs
297
    uo.rts <= '1';
298
    uo.td <= r.tshift(0);
299
 
300
    o_cfg_scaler(31 downto 18) <= (others => '0');
301
    o_cfg_scaler(17 downto 0) <= r.brate;
302
    o_cfg_rxen <= r.tcnt(1) and r.tcnt(0);
303
    o_cfg_txen <= '1';
304
    o_cfg_flow <= '0';
305
 
306
    o_com_dready <= r.dready;
307
    o_com_tsempty <= r.tsempty;
308
    o_com_thempty <= r.thempty;
309
    o_com_lock <= r.tcnt(1) and  r.tcnt(0);
310
    o_com_enable <= r.rxen;
311
    o_com_data <= r.rhold;
312
 
313
  end process;
314
 
315
  regs : process(clk)
316
  begin
317
    if rising_edge(clk) then
318
      r <= rin;
319
      if RESET_ALL and rst = '0' then
320
        r <= RES;
321
        -- Sync. registers not reset
322
        r.rxf <= rin.rxf;
323
      end if;
324
    end if;
325
  end process;
326
 
327
end;
328
 

powered by: WebSVN 2.1.0

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