-- $Id: serport_uart_rx.vhd 421 2011-11-07 21:23:50Z mueller $
|
-- $Id: serport_uart_rx.vhd 421 2011-11-07 21:23:50Z mueller $
|
--
|
--
|
-- Copyright 2007-2011 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
-- Copyright 2007-2011 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
--
|
--
|
-- This program is free software; you may redistribute and/or modify it under
|
-- This program is free software; you may redistribute and/or modify it under
|
-- the terms of the GNU General Public License as published by the Free
|
-- the terms of the GNU General Public License as published by the Free
|
-- Software Foundation, either version 2, or at your option any later version.
|
-- Software Foundation, either version 2, or at your option any later version.
|
--
|
--
|
-- This program is distributed in the hope that it will be useful, but
|
-- This program is distributed in the hope that it will be useful, but
|
-- WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
-- WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
-- for complete details.
|
-- for complete details.
|
--
|
--
|
-- The uart expects CLKDIV+1 wide input bit symbols.
|
-- The uart expects CLKDIV+1 wide input bit symbols.
|
-- This implementation counts the number of 1's in the first CLKDIV clock
|
-- This implementation counts the number of 1's in the first CLKDIV clock
|
-- cycles, and checks in the last cycle of the symbol time whether the
|
-- cycles, and checks in the last cycle of the symbol time whether the
|
-- number of 1's was > CLKDIV/2. This supresses short glitches nicely,
|
-- number of 1's was > CLKDIV/2. This supresses short glitches nicely,
|
-- especially for larger clock dividers.
|
-- especially for larger clock dividers.
|
--
|
--
|
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
-- Module Name: serport_uart_rx - syn
|
-- Module Name: serport_uart_rx - syn
|
-- Description: serial port UART - receiver
|
-- Description: serial port UART - receiver
|
--
|
--
|
-- Dependencies: -
|
-- Dependencies: -
|
-- Test bench: tb/tb_serport_uart_rxtx
|
-- Test bench: tb/tb_serport_uart_rxtx
|
-- Target Devices: generic
|
-- Target Devices: generic
|
-- Tool versions: xst 8.2, 9.1, 9.2, 13.1; ghdl 0.18-0.29
|
-- Tool versions: xst 8.2, 9.1, 9.2, 13.1; ghdl 0.18-0.29
|
-- Revision History:
|
-- Revision History:
|
-- Date Rev Version Comment
|
-- Date Rev Version Comment
|
-- 2011-10-22 417 2.0.3 now numeric_std clean
|
-- 2011-10-22 417 2.0.3 now numeric_std clean
|
-- 2009-07-12 233 2.0.2 remove snoopers
|
-- 2009-07-12 233 2.0.2 remove snoopers
|
-- 2008-03-02 121 2.0.1 comment out snoopers
|
-- 2008-03-02 121 2.0.1 comment out snoopers
|
-- 2007-10-21 91 2.0 re-designed and -implemented with state machine.
|
-- 2007-10-21 91 2.0 re-designed and -implemented with state machine.
|
-- allow CLKDIV=0 with 1 stop bit; allow max. CLKDIV
|
-- allow CLKDIV=0 with 1 stop bit; allow max. CLKDIV
|
-- (all 1's); aborts bad start bit after 1/2 cell;
|
-- (all 1's); aborts bad start bit after 1/2 cell;
|
-- accepts stop bit after 1/2 cell, permits tx clock
|
-- accepts stop bit after 1/2 cell, permits tx clock
|
-- be ~3 percent faster than rx clock.
|
-- be ~3 percent faster than rx clock.
|
-- for 3s1000ft256: 50 -> 58 slices for CDWIDTH=13
|
-- for 3s1000ft256: 50 -> 58 slices for CDWIDTH=13
|
-- 2007-10-14 89 1.1 almost full rewrite, handles now CLKDIV=0 properly
|
-- 2007-10-14 89 1.1 almost full rewrite, handles now CLKDIV=0 properly
|
-- for 3s1000ft256: 43 -> 50 slices for CDWIDTH=13
|
-- for 3s1000ft256: 43 -> 50 slices for CDWIDTH=13
|
-- 2007-10-12 88 1.0.1 avoid ieee.std_logic_unsigned, use cast to unsigned
|
-- 2007-10-12 88 1.0.1 avoid ieee.std_logic_unsigned, use cast to unsigned
|
-- 2007-06-30 62 1.0 Initial version
|
-- 2007-06-30 62 1.0 Initial version
|
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
|
|
library ieee;
|
library ieee;
|
use ieee.std_logic_1164.all;
|
use ieee.std_logic_1164.all;
|
use ieee.numeric_std.all;
|
use ieee.numeric_std.all;
|
|
|
use work.slvtypes.all;
|
use work.slvtypes.all;
|
|
|
entity serport_uart_rx is -- serial port uart: receive part
|
entity serport_uart_rx is -- serial port uart: receive part
|
generic (
|
generic (
|
CDWIDTH : positive := 13); -- clk divider width
|
CDWIDTH : positive := 13); -- clk divider width
|
port (
|
port (
|
CLK : in slbit; -- clock
|
CLK : in slbit; -- clock
|
RESET : in slbit; -- reset
|
RESET : in slbit; -- reset
|
CLKDIV : in slv(CDWIDTH-1 downto 0); -- clock divider setting
|
CLKDIV : in slv(CDWIDTH-1 downto 0); -- clock divider setting
|
RXSD : in slbit; -- receive serial data (uart view)
|
RXSD : in slbit; -- receive serial data (uart view)
|
RXDATA : out slv8; -- receiver data out
|
RXDATA : out slv8; -- receiver data out
|
RXVAL : out slbit; -- receiver data valid
|
RXVAL : out slbit; -- receiver data valid
|
RXERR : out slbit; -- receiver data error (frame error)
|
RXERR : out slbit; -- receiver data error (frame error)
|
RXACT : out slbit -- receiver active
|
RXACT : out slbit -- receiver active
|
);
|
);
|
end serport_uart_rx;
|
end serport_uart_rx;
|
|
|
|
|
architecture syn of serport_uart_rx is
|
architecture syn of serport_uart_rx is
|
|
|
type state_type is (
|
type state_type is (
|
s_idle, -- s_idle: idle
|
s_idle, -- s_idle: idle
|
s_colb0, -- s_colb0: collect b0 (start bit)
|
s_colb0, -- s_colb0: collect b0 (start bit)
|
s_endb0, -- s_endb0: finish b0 (start bit)
|
s_endb0, -- s_endb0: finish b0 (start bit)
|
s_colbx, -- s_colbx: collect bx
|
s_colbx, -- s_colbx: collect bx
|
s_endbx, -- s_endbx: finish bx
|
s_endbx, -- s_endbx: finish bx
|
s_colb9, -- s_colb9: collect bx (stop bit)
|
s_colb9, -- s_colb9: collect bx (stop bit)
|
s_endb9 -- s_endb9: finish bx (stop bit)
|
s_endb9 -- s_endb9: finish bx (stop bit)
|
);
|
);
|
|
|
type regs_type is record
|
type regs_type is record
|
state : state_type; -- state
|
state : state_type; -- state
|
ccnt : slv(CDWIDTH-1 downto 0); -- clock divider counter
|
ccnt : slv(CDWIDTH-1 downto 0); -- clock divider counter
|
dcnt : slv(CDWIDTH downto 0); -- data '1' counter
|
dcnt : slv(CDWIDTH downto 0); -- data '1' counter
|
bcnt : slv4; -- bit counter
|
bcnt : slv4; -- bit counter
|
sreg : slv8; -- input shift register
|
sreg : slv8; -- input shift register
|
end record regs_type;
|
end record regs_type;
|
|
|
constant ccntzero : slv(CDWIDTH-1 downto 0) := (others=>'0');
|
constant ccntzero : slv(CDWIDTH-1 downto 0) := (others=>'0');
|
constant dcntzero : slv(CDWIDTH downto 0) := (others=>'0');
|
constant dcntzero : slv(CDWIDTH downto 0) := (others=>'0');
|
constant regs_init : regs_type := (
|
constant regs_init : regs_type := (
|
s_idle, -- state
|
s_idle, -- state
|
ccntzero, -- ccnt
|
ccntzero, -- ccnt
|
dcntzero, -- dcnt
|
dcntzero, -- dcnt
|
(others=>'0'), -- bcnt
|
(others=>'0'), -- bcnt
|
(others=>'0') -- sreg
|
(others=>'0') -- sreg
|
);
|
);
|
|
|
signal R_REGS : regs_type := regs_init; -- state registers
|
signal R_REGS : regs_type := regs_init; -- state registers
|
signal N_REGS : regs_type := regs_init; -- next value state regs
|
signal N_REGS : regs_type := regs_init; -- next value state regs
|
|
|
begin
|
begin
|
|
|
proc_regs: process (CLK)
|
proc_regs: process (CLK)
|
begin
|
begin
|
|
|
if rising_edge(CLK) then
|
if rising_edge(CLK) then
|
R_REGS <= N_REGS;
|
R_REGS <= N_REGS;
|
end if;
|
end if;
|
|
|
end process proc_regs;
|
end process proc_regs;
|
|
|
proc_next: process (R_REGS, RESET, CLKDIV, RXSD)
|
proc_next: process (R_REGS, RESET, CLKDIV, RXSD)
|
|
|
variable r : regs_type := regs_init;
|
variable r : regs_type := regs_init;
|
variable n : regs_type := regs_init;
|
variable n : regs_type := regs_init;
|
|
|
variable dbit : slbit := '0';
|
variable dbit : slbit := '0';
|
variable ld_ccnt : slbit := '0';
|
variable ld_ccnt : slbit := '0';
|
variable tc_ccnt : slbit := '0';
|
variable tc_ccnt : slbit := '0';
|
variable tc_bcnt : slbit := '0';
|
variable tc_bcnt : slbit := '0';
|
variable ld_dcnt : slbit := '0';
|
variable ld_dcnt : slbit := '0';
|
variable ld_bcnt : slbit := '0';
|
variable ld_bcnt : slbit := '0';
|
variable ce_bcnt : slbit := '0';
|
variable ce_bcnt : slbit := '0';
|
variable iact : slbit := '0';
|
variable iact : slbit := '0';
|
variable ival : slbit := '0';
|
variable ival : slbit := '0';
|
variable ierr : slbit := '0';
|
variable ierr : slbit := '0';
|
|
|
begin
|
begin
|
|
|
r := R_REGS;
|
r := R_REGS;
|
n := R_REGS;
|
n := R_REGS;
|
|
|
dbit := '0';
|
dbit := '0';
|
ld_ccnt := '0';
|
ld_ccnt := '0';
|
tc_ccnt := '0';
|
tc_ccnt := '0';
|
tc_bcnt := '0';
|
tc_bcnt := '0';
|
ld_dcnt := '0';
|
ld_dcnt := '0';
|
ld_bcnt := '0';
|
ld_bcnt := '0';
|
ce_bcnt := '0';
|
ce_bcnt := '0';
|
iact := '1';
|
iact := '1';
|
ival := '0';
|
ival := '0';
|
ierr := '0';
|
ierr := '0';
|
|
|
if unsigned(r.ccnt) = 0 then
|
if unsigned(r.ccnt) = 0 then
|
tc_ccnt := '1';
|
tc_ccnt := '1';
|
end if;
|
end if;
|
if unsigned(r.bcnt) = 9 then
|
if unsigned(r.bcnt) = 9 then
|
tc_bcnt := '1';
|
tc_bcnt := '1';
|
end if;
|
end if;
|
|
|
if unsigned(r.dcnt) > unsigned("00" & CLKDIV(CDWIDTH-1 downto 1)) then
|
if unsigned(r.dcnt) > unsigned("00" & CLKDIV(CDWIDTH-1 downto 1)) then
|
dbit := '1';
|
dbit := '1';
|
end if;
|
end if;
|
|
|
case r.state is
|
case r.state is
|
|
|
when s_idle => -- s_idle: idle ----------------------
|
when s_idle => -- s_idle: idle ----------------------
|
iact := '0';
|
iact := '0';
|
ld_dcnt := '1'; -- always keep dcnt in reset
|
ld_dcnt := '1'; -- always keep dcnt in reset
|
if RXSD = '0' then -- if start bit seen
|
if RXSD = '0' then -- if start bit seen
|
if tc_ccnt = '1' then
|
if tc_ccnt = '1' then
|
n.state := s_endb0; -- finish b0
|
n.state := s_endb0; -- finish b0
|
ld_ccnt := '1'; -- start next bit
|
ld_ccnt := '1'; -- start next bit
|
ce_bcnt := '1';
|
ce_bcnt := '1';
|
else
|
else
|
n.state := s_colb0; -- collect b0
|
n.state := s_colb0; -- collect b0
|
end if;
|
end if;
|
else -- otherwise
|
else -- otherwise
|
ld_ccnt := '1'; -- keep all counters in reset
|
ld_ccnt := '1'; -- keep all counters in reset
|
ld_bcnt := '1';
|
ld_bcnt := '1';
|
end if;
|
end if;
|
|
|
when s_colb0 => -- s_colb0: collect b0 (start bit) ---
|
when s_colb0 => -- s_colb0: collect b0 (start bit) ---
|
if tc_ccnt = '1' then -- last cycle of b0 ?
|
if tc_ccnt = '1' then -- last cycle of b0 ?
|
n.state := s_endb0; -- finish b0
|
n.state := s_endb0; -- finish b0
|
ld_ccnt := '1'; -- "
|
ld_ccnt := '1'; -- "
|
ce_bcnt := '1';
|
ce_bcnt := '1';
|
else -- continue in b0 ?
|
else -- continue in b0 ?
|
if dbit='1' and RXSD='1' then -- too many 1's ?
|
if dbit='1' and RXSD='1' then -- too many 1's ?
|
n.state := s_idle; -- abort to idle
|
n.state := s_idle; -- abort to idle
|
ld_dcnt := '1'; -- put counters in reset
|
ld_dcnt := '1'; -- put counters in reset
|
ld_ccnt := '1';
|
ld_ccnt := '1';
|
ld_bcnt := '1';
|
ld_bcnt := '1';
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
when s_endb0 => -- s_endb0: finish b0 (start bit) ---
|
when s_endb0 => -- s_endb0: finish b0 (start bit) ---
|
ld_dcnt := '1'; -- start next bit
|
ld_dcnt := '1'; -- start next bit
|
if dbit = '1' then -- was it a 1 ?
|
if dbit = '1' then -- was it a 1 ?
|
n.state := s_idle; -- abort to idle
|
n.state := s_idle; -- abort to idle
|
ld_ccnt := '1'; -- put counters in reset
|
ld_ccnt := '1'; -- put counters in reset
|
ld_bcnt := '1';
|
ld_bcnt := '1';
|
else
|
else
|
if tc_ccnt = '1' then -- last cycle of bx ?
|
if tc_ccnt = '1' then -- last cycle of bx ?
|
n.state := s_endbx; -- finish bx
|
n.state := s_endbx; -- finish bx
|
ld_ccnt := '1';
|
ld_ccnt := '1';
|
ce_bcnt := '1';
|
ce_bcnt := '1';
|
else -- continue in b0 ?
|
else -- continue in b0 ?
|
n.state := s_colbx; -- collect bx
|
n.state := s_colbx; -- collect bx
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
when s_colbx => -- s_colbx: collect bx ---------------
|
when s_colbx => -- s_colbx: collect bx ---------------
|
if tc_ccnt = '1' then -- last cycle of bx ?
|
if tc_ccnt = '1' then -- last cycle of bx ?
|
n.state := s_endbx; -- finish bx
|
n.state := s_endbx; -- finish bx
|
ld_ccnt := '1';
|
ld_ccnt := '1';
|
ce_bcnt := '1';
|
ce_bcnt := '1';
|
end if;
|
end if;
|
|
|
when s_endbx => -- s_endbx: finish bx ---------------
|
when s_endbx => -- s_endbx: finish bx ---------------
|
ld_dcnt := '1'; -- start next bit
|
ld_dcnt := '1'; -- start next bit
|
n.sreg := dbit & r.sreg(7 downto 1);
|
n.sreg := dbit & r.sreg(7 downto 1);
|
if tc_ccnt = '1' then -- last cycle of bx ?
|
if tc_ccnt = '1' then -- last cycle of bx ?
|
if tc_bcnt = '1' then
|
if tc_bcnt = '1' then
|
n.state := s_endb9; -- finish b9
|
n.state := s_endb9; -- finish b9
|
ld_bcnt := '1'; -- and wrap bcnt
|
ld_bcnt := '1'; -- and wrap bcnt
|
else
|
else
|
n.state := s_endbx; -- finish bx
|
n.state := s_endbx; -- finish bx
|
ce_bcnt := '1';
|
ce_bcnt := '1';
|
end if;
|
end if;
|
ld_ccnt := '1';
|
ld_ccnt := '1';
|
else -- continue in bx ?
|
else -- continue in bx ?
|
if tc_bcnt = '1' then
|
if tc_bcnt = '1' then
|
n.state := s_colb9; -- collect b9
|
n.state := s_colb9; -- collect b9
|
else
|
else
|
n.state := s_colbx; -- collect bx
|
n.state := s_colbx; -- collect bx
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
when s_colb9 => -- s_colb9: collect bx (stop bit) ----
|
when s_colb9 => -- s_colb9: collect bx (stop bit) ----
|
if tc_ccnt = '1' then -- last cycle of b9 ?
|
if tc_ccnt = '1' then -- last cycle of b9 ?
|
n.state := s_endb9; -- finish b9
|
n.state := s_endb9; -- finish b9
|
ld_ccnt := '1'; -- "
|
ld_ccnt := '1'; -- "
|
ld_bcnt := '1'; -- and wrap bcnt
|
ld_bcnt := '1'; -- and wrap bcnt
|
else -- continue in b9 ?
|
else -- continue in b9 ?
|
if dbit='1' and RXSD='1' then -- already enough 1's ?
|
if dbit='1' and RXSD='1' then -- already enough 1's ?
|
n.state := s_idle; -- finish to idle
|
n.state := s_idle; -- finish to idle
|
ld_dcnt := '1'; -- put counters in reset
|
ld_dcnt := '1'; -- put counters in reset
|
ld_ccnt := '1';
|
ld_ccnt := '1';
|
ld_bcnt := '1';
|
ld_bcnt := '1';
|
ival := '1';
|
ival := '1';
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
when s_endb9 => -- s_endb9: finish bx (stop bit) ----
|
when s_endb9 => -- s_endb9: finish bx (stop bit) ----
|
ld_dcnt := '1'; -- start next bit
|
ld_dcnt := '1'; -- start next bit
|
if dbit = '1' then -- was it a valid stop bit ?
|
if dbit = '1' then -- was it a valid stop bit ?
|
ival := '1';
|
ival := '1';
|
else
|
else
|
ierr := '1';
|
ierr := '1';
|
end if;
|
end if;
|
if RXSD = '1' then -- line in idle state ?
|
if RXSD = '1' then -- line in idle state ?
|
n.state := s_idle; -- finish to idle state
|
n.state := s_idle; -- finish to idle state
|
ld_ccnt := '1'; -- and put counters in reset
|
ld_ccnt := '1'; -- and put counters in reset
|
ld_bcnt := '1'; -- "
|
ld_bcnt := '1'; -- "
|
else
|
else
|
if tc_ccnt = '1' then -- last cycle of b9 ?
|
if tc_ccnt = '1' then -- last cycle of b9 ?
|
n.state := s_endb0; -- finish b0
|
n.state := s_endb0; -- finish b0
|
ld_ccnt := '1'; -- "
|
ld_ccnt := '1'; -- "
|
ce_bcnt := '1';
|
ce_bcnt := '1';
|
else -- continue in b0 ?
|
else -- continue in b0 ?
|
n.state := s_colb0; -- collect bx
|
n.state := s_colb0; -- collect bx
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
when others => null; -- -----------------------------------
|
when others => null; -- -----------------------------------
|
|
|
end case;
|
end case;
|
|
|
if RESET = '1' then -- RESET seen
|
if RESET = '1' then -- RESET seen
|
ld_ccnt := '1'; -- keep all counters in reset
|
ld_ccnt := '1'; -- keep all counters in reset
|
ld_dcnt := '1';
|
ld_dcnt := '1';
|
ld_bcnt := '1';
|
ld_bcnt := '1';
|
n.state := s_idle;
|
n.state := s_idle;
|
end if;
|
end if;
|
|
|
if ld_ccnt = '1' then -- implement ccnt
|
if ld_ccnt = '1' then -- implement ccnt
|
n.ccnt := CLKDIV;
|
n.ccnt := CLKDIV;
|
else
|
else
|
n.ccnt := slv(unsigned(r.ccnt) - 1);
|
n.ccnt := slv(unsigned(r.ccnt) - 1);
|
end if;
|
end if;
|
|
|
if ld_dcnt = '1' then -- implement dcnt
|
if ld_dcnt = '1' then -- implement dcnt
|
n.dcnt(CDWIDTH downto 1) := (others=>'0');
|
n.dcnt(CDWIDTH downto 1) := (others=>'0');
|
n.dcnt(0) := RXSD;
|
n.dcnt(0) := RXSD;
|
else
|
else
|
if RXSD = '1' then
|
if RXSD = '1' then
|
n.dcnt := slv(unsigned(r.dcnt) + 1);
|
n.dcnt := slv(unsigned(r.dcnt) + 1);
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
if ld_bcnt = '1' then -- implement bcnt
|
if ld_bcnt = '1' then -- implement bcnt
|
n.bcnt := (others=>'0');
|
n.bcnt := (others=>'0');
|
else
|
else
|
if ce_bcnt = '1' then
|
if ce_bcnt = '1' then
|
n.bcnt := slv(unsigned(r.bcnt) + 1);
|
n.bcnt := slv(unsigned(r.bcnt) + 1);
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
N_REGS <= n;
|
N_REGS <= n;
|
|
|
RXDATA <= r.sreg;
|
RXDATA <= r.sreg;
|
RXACT <= iact;
|
RXACT <= iact;
|
RXVAL <= ival;
|
RXVAL <= ival;
|
RXERR <= ierr;
|
RXERR <= ierr;
|
|
|
end process proc_next;
|
end process proc_next;
|
|
|
end syn;
|
end syn;
|
|
|