SURF  1.0
Debouncer.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : Debouncer.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2013-04-30
5 -- Last update: 2013-08-02
6 -------------------------------------------------------------------------------
7 -- Description: Debouncer for pushbutton switches
8 -------------------------------------------------------------------------------
9 -- This file is part of 'SLAC Firmware Standard Library'.
10 -- It is subject to the license terms in the LICENSE.txt file found in the
11 -- top-level directory of this distribution and at:
12 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
13 -- No part of 'SLAC Firmware Standard Library', including this file,
14 -- may be copied, modified, propagated, or distributed except according to
15 -- the terms contained in the LICENSE.txt file.
16 -------------------------------------------------------------------------------
17 
18 library ieee;
19 use ieee.std_logic_1164.all;
20 use ieee.numeric_std.all;
21 use work.StdRtlPkg.all;
22 
23 --! @see entity
24  --! @ingroup base_general
25 entity Debouncer is
26 
27  generic (
28  TPD_G : time := 1 ns;
29  RST_POLARITY_G : sl := '1'; -- '1' for active high rst, '0' for active low
30  RST_ASYNC_G : boolean := false;
31  INPUT_POLARITY_G : sl := '0';
33  FILTER_SIZE_G : positive := 16;
34  FILTER_INIT_G : slv := X"0000";
35  SYNCHRONIZE_G : boolean := true); -- Run input through 2 FFs before filtering
36 
37  port (
38  clk : in sl;
39  rst : in sl := not RST_POLARITY_G;
40  i : in sl;
41  o : out sl);
42 end entity Debouncer;
43 
44 architecture rtl of Debouncer is
45 
46  type RegType is record
47  filter : slv(FILTER_SIZE_G-1 downto 0);
48  o : sl;
49  end record RegType;
50 
51  constant REG_RESET_C : RegType :=
52  (filter => FILTER_INIT_G,
53  o => not OUTPUT_POLARITY_G);
54 
55  signal r : RegType := REG_RESET_C;
56  signal rin : RegType;
57  signal iSynced : sl := INPUT_POLARITY_G;
58 
59 begin
60 
61  assert (FILTER_INIT_G'length = FILTER_SIZE_G) report "FILTER_INIT_G length must = FILTER_SIZE_G" severity failure;
62 
63  SynchronizerGen : if (SYNCHRONIZE_G) generate
64  Synchronizer_1 : entity work.Synchronizer
65  generic map (
66  TPD_G => TPD_G,
69  STAGES_G => 2,
70  INIT_G => "00")
71  port map (
72  clk => clk,
73  rst => rst,
74  dataIn => i,
75  dataOut => iSynced);
76  end generate SynchronizerGen;
77 
78  comb : process (r, i, iSynced, rst) is
79  variable v : RegType;
80  begin
81  v := r;
82 
83  if (SYNCHRONIZE_G) then
84  v.filter := r.filter(FILTER_SIZE_G-2 downto 0) & iSynced;
85  else
86  v.filter := r.filter(FILTER_SIZE_G-2 downto 0) & i;
87  end if;
88 
89  if (allBits(r.filter, INPUT_POLARITY_G)) then
90  v.o := OUTPUT_POLARITY_G;
91  elsif (noBits(r.filter, INPUT_POLARITY_G)) then
92  v.o := not OUTPUT_POLARITY_G;
93  -- else v.o retains current value
94  end if;
95 
96  -- Synchronous Reset
97  if (RST_ASYNC_G = false and rst = RST_POLARITY_G) then
98  v := REG_RESET_C;
99  end if;
100 
101  rin <= v;
102  o <= r.o;
103 
104  end process comb;
105 
106  seq : process (clk, rst) is
107  begin
108  if (RST_ASYNC_G and rst = RST_POLARITY_G) then
109  r <= REG_RESET_C after TPD_G;
110  elsif (rising_edge(clk)) then
111  r <= rin after TPD_G;
112  end if;
113  end process seq;
114 
115 end architecture rtl;
INIT_Gslv := "0"
FILTER_SIZE_Gpositive := 16
Definition: Debouncer.vhd:33
std_logic sl
Definition: StdRtlPkg.vhd:28
in rstsl :=not RST_POLARITY_G
in isl
Definition: Debouncer.vhd:40
STAGES_Gpositive := 2
RST_POLARITY_Gsl := '1'
TPD_Gtime := 1 ns
Definition: Debouncer.vhd:28
out dataOutsl
FILTER_INIT_Gslv := X"0000"
Definition: Debouncer.vhd:34
in clksl
Definition: Debouncer.vhd:38
RST_ASYNC_Gboolean := false
Definition: Debouncer.vhd:30
out osl
Definition: Debouncer.vhd:41
SYNCHRONIZE_Gboolean := true
Definition: Debouncer.vhd:35
RST_POLARITY_Gsl := '1'
Definition: Debouncer.vhd:29
TPD_Gtime := 1 ns
OUTPUT_POLARITY_Gsl := '1'
Definition: Debouncer.vhd:32
RST_ASYNC_Gboolean := false
in rstsl :=not RST_POLARITY_G
Definition: Debouncer.vhd:39
INPUT_POLARITY_Gsl := '0'
Definition: Debouncer.vhd:31
std_logic_vector slv
Definition: StdRtlPkg.vhd:29