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

Subversion Repositories mips_enhanced

[/] [mips_enhanced/] [trunk/] [grlib-gpl-1.0.19-b3188/] [lib/] [gaisler/] [uart/] [apbuart.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dimamali
------------------------------------------------------------------------------
2
--  This file is a part of the GRLIB VHDL IP LIBRARY
3
--  Copyright (C) 2003, Gaisler Research
4
--
5
--  This program is free software; you can redistribute it and/or modify
6
--  it under the terms of the GNU General Public License as published by
7
--  the Free Software Foundation; either version 2 of the License, or
8
--  (at your option) any later version.
9
--
10
--  This program is distributed in the hope that it will be useful,
11
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
--  GNU General Public License for more details.
14
--
15
--  You should have received a copy of the GNU General Public License
16
--  along with this program; if not, write to the Free Software
17
--  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
18
-----------------------------------------------------------------------------
19
-- Entity:      uart
20
-- File:        uart.vhd
21
-- Authors:     Jiri Gaisler - Gaisler Research
22
--              Marko Isomaki - Gaisler Research
23
-- Description: Asynchronous UART. Implements 8-bit data frame with one stop-bit.
24
------------------------------------------------------------------------------
25
 
26
library ieee;
27
use ieee.std_logic_1164.all;
28
--use ieee.numeric_std.all;
29
library grlib;
30
use grlib.amba.all;
31
use grlib.stdlib.all;
32
use grlib.devices.all;
33
library gaisler;
34
use gaisler.uart.all;
35
--pragma translate_off
36
use std.textio.all;
37
--pragma translate_on
38
 
39
entity apbuart is
40
  generic (
41
    pindex   : integer := 0;
42
    paddr    : integer := 0;
43
    pmask    : integer := 16#fff#;
44
    console  : integer := 0;
45
    pirq     : integer := 0;
46
    parity   : integer := 1;
47
    flow     : integer := 1;
48
    fifosize : integer range 1 to 32 := 1;
49
    abits    : integer := 8);
50
  port (
51
    rst    : in  std_ulogic;
52
    clk    : in  std_ulogic;
53
    apbi   : in  apb_slv_in_type;
54
    apbo   : out apb_slv_out_type;
55
    uarti  : in  uart_in_type;
56
    uarto  : out uart_out_type);
57
end;
58
 
59
architecture rtl of apbuart is
60
 
61
constant REVISION : integer := 1;
62
 
63
constant pconfig : apb_config_type := (
64
 
65
  1 => apb_iobar(paddr, pmask));
66
 
67
type rxfsmtype is (idle, startbit, data, cparity, stopbit);
68
type txfsmtype is (idle, data, cparity, stopbit);
69
 
70
type fifo is array (0 to fifosize - 1) of std_logic_vector(7 downto 0);
71
 
72
type uartregs is record
73
  rxen          :  std_ulogic;  -- receiver enabled
74
  txen          :  std_ulogic;  -- transmitter enabled
75
  rirqen        :  std_ulogic;  -- receiver irq enable
76
  tirqen        :  std_ulogic;  -- transmitter irq enable
77
  oen           :  std_ulogic;  -- output enable
78
  parsel        :  std_ulogic;  -- parity select
79
  paren         :  std_ulogic;  -- parity select
80
  flow          :  std_ulogic;  -- flow control enable
81
  loopb         :  std_ulogic;  -- loop back mode enable
82
  debug         :  std_ulogic;  -- debug mode enable
83
  rsempty       :  std_ulogic;  -- receiver shift register empty (internal)
84
  tsempty       :  std_ulogic;  -- transmitter shift register empty
85
  break         :  std_ulogic;  -- break detected
86
  ovf           :  std_ulogic;  -- receiver overflow
87
  parerr        :  std_ulogic;  -- parity error
88
  frame         :  std_ulogic;  -- framing error
89
  ctsn          :  std_logic_vector(1 downto 0); -- clear to send
90
  rtsn          :  std_ulogic;  -- request to send
91
  extclken      :  std_ulogic;  -- use external baud rate clock
92
  extclk        :  std_ulogic;  -- rising edge detect register
93
  rhold         :  fifo;
94
  rshift        :  std_logic_vector(7 downto 0);
95
  tshift        :  std_logic_vector(10 downto 0);
96
  thold         :  fifo;
97
  irq           :  std_ulogic;  -- tx/rx interrupt (internal)
98
  tpar          :  std_ulogic;  -- tx data parity (internal)
99
  txstate       :  txfsmtype;
100
  txclk         :  std_logic_vector(2 downto 0);  -- tx clock divider
101
  txtick        :  std_ulogic;  -- tx clock (internal)
102
  rxstate       :  rxfsmtype;
103
  rxclk         :  std_logic_vector(2 downto 0); -- rx clock divider
104
  rxdb          :  std_logic_vector(1 downto 0);  -- rx delay
105
  dpar          :  std_ulogic;  -- rx data parity (internal)
106
  rxtick        :  std_ulogic;  -- rx clock (internal)
107
  tick          :  std_ulogic;  -- rx clock (internal)
108
  scaler        :  std_logic_vector(11 downto 0);
109
  brate         :  std_logic_vector(11 downto 0);
110
  rxf           :  std_logic_vector(4 downto 0); --  rx data filtering buffer
111
  txd           :  std_ulogic;  -- transmitter data
112
  rfifoirqen    :  std_ulogic;  -- receiver fifo interrupt enable
113
  tfifoirqen    :  std_ulogic;  -- transmitter fifo interrupt enable
114
 --fifo counters
115
  rwaddr        :  std_logic_vector(log2x(fifosize) - 1 downto 0);
116
  rraddr        :  std_logic_vector(log2x(fifosize) - 1 downto 0);
117
  traddr        :  std_logic_vector(log2x(fifosize) - 1 downto 0);
118
  twaddr        :  std_logic_vector(log2x(fifosize) - 1 downto 0);
119
  rcnt          :  std_logic_vector(log2x(fifosize) downto 0);
120
  tcnt          :  std_logic_vector(log2x(fifosize) downto 0);
121
end record;
122
 
123
constant  rcntzero : std_logic_vector(log2x(fifosize) downto 0) := (others => '0');
124
 
125
signal r, rin : uartregs;
126
 
127
begin
128
  uartop : process(rst, r, apbi, uarti )
129
  variable rdata : std_logic_vector(31 downto 0);
130
  variable scaler : std_logic_vector(11 downto 0);
131
  variable rxclk, txclk : std_logic_vector(2 downto 0);
132
  variable rxd, ctsn : std_ulogic;
133
  variable irq : std_logic_vector(NAHBIRQ-1 downto 0);
134
  variable paddr : std_logic_vector(7 downto 2);
135
  variable v : uartregs;
136
  variable thalffull : std_ulogic;
137
  variable rhalffull : std_ulogic;
138
  variable rfull : std_ulogic;
139
  variable tfull : std_ulogic;
140
  variable dready : std_ulogic;
141
  variable thempty : std_ulogic;
142
--pragma translate_off
143
  variable L1 : line;
144
  variable CH : character;
145
  variable FIRST : boolean := true;
146
  variable pt : time := 0 ns;
147
--pragma translate_on
148
 
149
  begin
150
 
151
    v := r; irq := (others => '0'); irq(pirq) := r.irq;
152
    v.irq := '0'; v.txtick := '0'; v.rxtick := '0'; v.tick := '0';
153
    rdata := (others => '0'); v.rxdb(1) := r.rxdb(0);
154
    dready := '0'; thempty := '1'; thalffull := '1'; rhalffull := '0';
155
    v.ctsn := r.ctsn(0) & uarti.ctsn;
156
 
157
    if fifosize = 1 then
158
      dready := r.rcnt(0); rfull := dready; tfull := r.tcnt(0);
159
      thempty := not tfull;
160
    else
161
      tfull := r.tcnt(log2x(fifosize)); rfull := r.rcnt(log2x(fifosize));
162
      if (r.rcnt(log2x(fifosize)) or r.rcnt(log2x(fifosize) - 1)) = '1' then
163
        rhalffull := '1';
164
      end if;
165
      if ((r.tcnt(log2x(fifosize)) or r.tcnt(log2x(fifosize) - 1))) = '1' then
166
        thalffull := '0';
167
      end if;
168
      if r.rcnt /= rcntzero then dready := '1'; end if;
169
      if r.tcnt /= rcntzero then thempty := '0'; end if;
170
    end if;
171
 
172
-- scaler
173
 
174
    scaler := r.scaler - 1;
175
    if (r.rxen or r.txen) = '1' then
176
      v.scaler := scaler;
177
      v.tick := scaler(11) and not r.scaler(11);
178
      if v.tick = '1' then v.scaler := r.brate; end if;
179
    end if;
180
 
181
-- optional external uart clock
182
    v.extclk := uarti.extclk;
183
    if r.extclken = '1' then v.tick := r.extclk and not uarti.extclk; end if;
184
 
185
-- read/write registers
186
 
187
  if (apbi.psel(pindex) and apbi.penable and (not apbi.pwrite)) = '1' then
188
    case paddr(7 downto 2) is
189
    when "000000" =>
190
      rdata(7 downto 0) := r.rhold(conv_integer(r.rraddr));
191
        if fifosize = 1 then v.rcnt(0) := '0';
192
        else
193
          if r.rcnt /= rcntzero then
194
            v.rraddr := r.rraddr + 1; v.rcnt := r.rcnt - 1;
195
          end if;
196
        end if;
197
    when "000001" =>
198
      if fifosize /= 1 then
199
        rdata (26 + log2x(fifosize) downto 26) := r.rcnt;
200
        rdata (20 + log2x(fifosize) downto 20) := r.tcnt;
201
        rdata (10 downto 7) := rfull & tfull & rhalffull & thalffull;
202
      end if;
203
 
204
      rdata(6 downto 0) := r.frame & r.parerr & r.ovf &
205
                r.break & thempty & r.tsempty & dready;
206
--pragma translate_off
207
      if CONSOLE = 1 then rdata(2 downto 1) := "11"; end if;
208
--pragma translate_on
209
    when "000010" =>
210
      if fifosize > 1 then
211
        rdata(31) := '1';
212
      end if;
213
      rdata(12) := r.oen;
214
      rdata(11) := r.debug;
215
      if fifosize /= 1 then
216
        rdata(10 downto 9) := r.rfifoirqen & r.tfifoirqen;
217
      end if;
218
      rdata(8 downto 0) := r.extclken & r.loopb &
219
           r.flow & r.paren & r.parsel & r.tirqen & r.rirqen & r.txen & r.rxen;
220
    when "000011" =>
221
      rdata(11 downto 0) := r.brate;
222
    when "000100" =>
223
 
224
        -- Read TX FIFO.
225
        if r.debug = '1' and r.tcnt /= rcntzero then
226
            rdata(7 downto 0) := r.thold(conv_integer(r.traddr));
227
            if fifosize = 1 then
228
                v.tcnt(0) := '0';
229
            else
230
                v.traddr := r.traddr + 1;
231
                v.tcnt := r.tcnt - 1;
232
            end if;
233
        end if;
234
    when others =>
235
      null;
236
    end case;
237
  end if;
238
 
239
    paddr := "000000"; paddr(abits-1 downto 2) := apbi.paddr(abits-1 downto 2);
240
    if (apbi.psel(pindex) and apbi.penable and apbi.pwrite) = '1' then
241
      case paddr(7 downto 2) is
242
      when "000000" =>
243
      when "000001" =>
244
        v.frame      := apbi.pwdata(6);
245
        v.parerr     := apbi.pwdata(5);
246
        v.ovf        := apbi.pwdata(4);
247
        v.break      := apbi.pwdata(3);
248
      when "000010" =>
249
        v.oen        := apbi.pwdata(12);
250
        v.debug      := apbi.pwdata(11);
251
        if fifosize /= 1 then
252
          v.rfifoirqen := apbi.pwdata(10);
253
          v.tfifoirqen := apbi.pwdata(9);
254
        end if;
255
        v.extclken   := apbi.pwdata(8);
256
        v.loopb      := apbi.pwdata(7);
257
        v.flow       := apbi.pwdata(6);
258
        v.paren      := apbi.pwdata(5);
259
        v.parsel     := apbi.pwdata(4);
260
        v.tirqen     := apbi.pwdata(3);
261
        v.rirqen     := apbi.pwdata(2);
262
        v.txen       := apbi.pwdata(1);
263
        v.rxen       := apbi.pwdata(0);
264
      when "000011" =>
265
        v.brate      := apbi.pwdata(11 downto 0);
266
        v.scaler     := apbi.pwdata(11 downto 0);
267
      when "000100" =>
268
        -- Write RX fifo and generate irq
269
        if flow /= 0 then
270
          v.rhold(conv_integer(r.rwaddr)) := apbi.pwdata(7 downto 0);
271
          if fifosize = 1 then v.rcnt(0) := '1';
272
          else v.rwaddr := r.rwaddr + 1; v.rcnt := v.rcnt + 1; end if;
273
 
274
          if r.debug = '1' then
275
              v.irq := v.irq or r.rirqen;
276
          end if;
277
 
278
        end if;
279
      when others =>
280
        null;
281
      end case;
282
    end if;
283
 
284
-- tx clock
285
 
286
    txclk := r.txclk + 1;
287
    if r.tick = '1' then
288
      v.txclk := txclk;
289
      v.txtick := r.txclk(2) and not txclk(2);
290
    end if;
291
 
292
-- rx clock
293
 
294
    rxclk := r.rxclk + 1;
295
    if r.tick = '1' then
296
      v.rxclk := rxclk;
297
      v.rxtick := r.rxclk(2) and not rxclk(2);
298
    end if;
299
 
300
-- filter rx data
301
 
302
--    v.rxf := r.rxf(6 downto 0) & uarti.rxd;
303
--    if ((r.rxf(7) & r.rxf(7) & r.rxf(7) & r.rxf(7) & r.rxf(7) & r.rxf(7) &
304
--       r.rxf(7)) = r.rxf(6 downto 0))
305
--    then v.rxdb(0) := r.rxf(7); end if;
306
 
307
    v.rxf(1 downto 0) := r.rxf(0) & uarti.rxd;    -- meta-stability filter
308
    if r.tick = '1' then
309
      v.rxf(4 downto 2) := r.rxf(3 downto 1);
310
    end if;
311
    v.rxdb(0) := (r.rxf(4) and r.rxf(3)) or (r.rxf(4) and r.rxf(2)) or
312
                  (r.rxf(3) and r.rxf(2));
313
-- loop-back mode
314
    if r.loopb = '1' then
315
      v.rxdb(0) := r.tshift(0); ctsn := dready and not r.rsempty;
316
    elsif (flow = 1) then ctsn := r.ctsn(1); else ctsn := '0'; end if;
317
    rxd := r.rxdb(0);
318
 
319
-- transmitter operation
320
 
321
    case r.txstate is
322
    when idle =>        -- idle state
323
      if (r.txtick = '1') then v.tsempty := '1'; end if;
324
 
325
      if ((not r.debug and r.txen and (not thempty) and r.txtick) and
326
          ((not ctsn) or not r.flow)) = '1' then
327
          v.txstate := data;
328
          v.tpar := r.parsel; v.tsempty := '0';
329
          v.txclk := "00" & r.tick; v.txtick := '0';
330
          v.tshift := "10" & r.thold(conv_integer(r.traddr)) & '0';
331
          if fifosize = 1 then
332
              v.irq := r.irq or r.tirqen; v.tcnt(0) := '0';
333
          else
334
              v.traddr := r.traddr + 1;
335
              v.tcnt := r.tcnt - 1;
336
          end if;
337
      end if;
338
 
339
    when data =>        -- transmit data frame
340
      if r.txtick = '1' then
341
        v.tpar := r.tpar xor r.tshift(1);
342
        v.tshift := '1' & r.tshift(10 downto 1);
343
        if r.tshift(10 downto 1) = "1111111110" then
344
          if r.paren = '1' then
345
            v.tshift(0) := r.tpar; v.txstate := cparity;
346
          else
347
            v.tshift(0) := '1'; v.txstate := stopbit;
348
          end if;
349
        end if;
350
      end if;
351
    when cparity =>     -- transmit parity bit
352
      if r.txtick = '1' then
353
        v.tshift := '1' & r.tshift(10 downto 1); v.txstate := stopbit;
354
      end if;
355
    when stopbit =>     -- transmit stop bit
356
      if r.txtick = '1' then
357
        v.tshift := '1' & r.tshift(10 downto 1); v.txstate := idle;
358
      end if;
359
 
360
    end case;
361
 
362
-- writing of tx data register must be done after tx fsm to get correct
363
-- operation of thempty flag
364
 
365
    if (apbi.psel(pindex) and apbi.penable and apbi.pwrite) = '1' then
366
      case paddr(4 downto 2) is
367
      when "000" =>
368
        if fifosize = 1 then
369
          v.thold(0) := apbi.pwdata(7 downto 0); v.tcnt(0) := '1';
370
        else
371
          v.thold(conv_integer(r.twaddr)) := apbi.pwdata(7 downto 0);
372
          if not (tfull = '1') then
373
            v.twaddr := r.twaddr + 1; v.tcnt :=  v.tcnt + 1;
374
          end if;
375
        end if;
376
--pragma translate_off
377
        if CONSOLE = 1 then
378
          if first then L1:= new string'(""); first := false; end if; --'
379
          if apbi.penable'event then    --'
380
            CH := character'val(conv_integer(apbi.pwdata(7 downto 0))); --'
381
            if CH  = CR then
382
              std.textio.writeline(OUTPUT, L1);
383
            elsif CH /= LF then
384
              std.textio.write(L1,CH);
385
            end if;
386
            pt := now;
387
          end if;
388
        end if;
389
--pragma translate_on
390
      when others => null;
391
      end case;
392
    end if;
393
 
394
-- receiver operation
395
 
396
    case r.rxstate is
397
    when idle =>        -- wait for start bit
398
      if ((r.rsempty = '0') and not (rfull = '1')) then
399
          v.rsempty := '1';
400
          v.rhold(conv_integer(r.rwaddr)) := r.rshift;
401
          if fifosize = 1 then v.rcnt(0) := '1';
402
          else v.rwaddr := r.rwaddr + 1; v.rcnt := v.rcnt + 1; end if;
403
      end if;
404
      if (r.rxen and r.rxdb(1) and (not rxd)) = '1' then
405
        v.rxstate := startbit; v.rshift := (others => '1'); v.rxclk := "100";
406
        if v.rsempty = '0' then v.ovf := '1'; end if;
407
        v.rsempty := '0'; v.rxtick := '0';
408
      end if;
409
    when startbit =>    -- check validity of start bit
410
      if r.rxtick = '1' then
411
        if rxd = '0' then
412
          v.rshift := rxd & r.rshift(7 downto 1); v.rxstate := data;
413
          v.dpar := r.parsel;
414
        else
415
          v.rxstate := idle;
416
        end if;
417
      end if;
418
    when data =>        -- receive data frame
419
      if r.rxtick = '1' then
420
        v.dpar := r.dpar xor rxd;
421
        v.rshift := rxd & r.rshift(7 downto 1);
422
        if r.rshift(0) = '0' then
423
          if r.paren = '1' then v.rxstate := cparity;
424
          else v.rxstate := stopbit; v.dpar := '0'; end if;
425
        end if;
426
      end if;
427
    when cparity =>     -- receive parity bit
428
      if r.rxtick = '1' then
429
        v.dpar := r.dpar xor rxd; v.rxstate := stopbit;
430
      end if;
431
    when stopbit =>     -- receive stop bit
432
      if r.rxtick = '1' then
433
        v.irq := v.irq or r.rirqen; -- make sure no tx irqs are lost !
434
        if rxd = '1' then
435
          v.parerr := r.parerr or r.dpar; v.rsempty := r.dpar;
436
          if not (rfull = '1') and (r.dpar = '0') then
437
            v.rsempty := '1';
438
            v.rhold(conv_integer(r.rwaddr)) := r.rshift;
439
            if fifosize = 1 then v.rcnt(0) := '1';
440
            else v.rwaddr := r.rwaddr + 1; v.rcnt := v.rcnt + 1; end if;
441
          end if;
442
        else
443
          if r.rshift = "00000000" then v.break := '1';
444
          else v.frame := '1'; end if;
445
          v.rsempty := '1';
446
        end if;
447
        v.rxstate := idle;
448
      end if;
449
    end case;
450
 
451
    if r.rxtick = '1' then
452
      v.rtsn := (rfull and not r.rsempty) or r.loopb;
453
    end if;
454
 
455
    v.txd := r.tshift(0) or r.loopb or r.debug;
456
 
457
    if fifosize /= 1 then
458
      if thempty = '0' and v.tcnt = rcntzero then
459
        v.irq := v.irq or r.tirqen;
460
      end if;
461
      v.irq := v.irq or (r.tfifoirqen and r.txen and thalffull);
462
      v.irq := v.irq or (r.rfifoirqen and r.rxen and rhalffull);
463
    end if;
464
 
465
 
466
 
467
-- reset operation
468
 
469
    if rst = '0' then
470
      v.frame := '0'; v.rsempty := '1';
471
      v.parerr := '0'; v.ovf := '0'; v.break := '0';
472
      v.tsempty := '1'; v.txen := '0'; v.rxen := '0';
473
      v.txstate := idle; v.rxstate := idle; v.tshift(0) := '1';
474
      v.extclken := '0'; v.rtsn := '1'; v.flow := '0';
475
      v.txclk := (others => '0'); v.rxclk := (others => '0');
476
      v.rcnt := (others => '0'); v.tcnt := (others => '0');
477
      v.rwaddr := (others => '0'); v.twaddr := (others => '0');
478
      v.rraddr := (others => '0'); v.traddr := (others => '0');
479
    end if;
480
 
481
-- update registers
482
 
483
    rin <= v;
484
 
485
-- drive outputs
486
 
487
    uarto.txd <= r.txd; uarto.rtsn <= r.rtsn;
488
    uarto.scaler <= "000000" & r.scaler;
489
    uarto.txen <= r.oen; uarto.rxen <= r.rxen;
490
    apbo.prdata <= rdata; apbo.pirq <= irq;
491
    apbo.pindex <= pindex;
492
    uarto.txen <= r.txen; uarto.rxen <= r.rxen;
493
 
494
  end process;
495
 
496
  apbo.pconfig <= pconfig;
497
 
498
  regs : process(clk)
499
  begin if rising_edge(clk) then r <= rin; end if; end process;
500
 
501
-- pragma translate_off
502
    bootmsg : report_version
503
    generic map ("apbuart" & tost(pindex) &
504
        ": Generic UART rev " & tost(REVISION) & ", fifo " & tost(fifosize) &
505
        ", irq " & tost(pirq));
506
-- pragma translate_on
507
 
508
end;

powered by: WebSVN 2.1.0

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