source: firmware/FTU/counter/FTU_rate_counter.vhd@ 9890

Last change on this file since 9890 was 9890, checked in by weitzel, 14 years ago
overflow register implemented for FTU rate counter
File size: 4.3 KB
Line 
1----------------------------------------------------------------------------------
2-- Company: ETH Zurich, Institute for Particle Physics
3-- Engineer: Q. Weitzel, P. Vogler
4--
5-- Create Date: 10:38:40 08/18/2010
6-- Design Name:
7-- Module Name: FTU_rate_counter - Behavioral
8-- Project Name:
9-- Target Devices:
10-- Tool versions:
11-- Description: Entity to count trigger and sum patch rates of FTU board
12--
13-- Dependencies:
14--
15-- Revision:
16-- Revision 0.01 - File Created
17-- Additional Comments:
18--
19----------------------------------------------------------------------------------
20
21library IEEE;
22use IEEE.STD_LOGIC_1164.ALL;
23use IEEE.STD_LOGIC_ARITH.ALL;
24use IEEE.STD_LOGIC_UNSIGNED.ALL;
25
26library ftu_definitions;
27USE ftu_definitions.ftu_array_types.all;
28USE ftu_definitions.ftu_constants.all;
29
30---- Uncomment the following library declaration if instantiating
31---- any Xilinx primitives in this code.
32--library UNISIM;
33--use UNISIM.VComponents.all;
34
35entity FTU_rate_counter is
36 port(
37 clk : in std_logic;
38 cntr_reset : in std_logic;
39 trigger : in std_logic;
40 prescaling : in std_logic_vector(7 downto 0);
41 counts : out integer range 0 to 2**16 - 1 := 0;
42 overflow : out std_logic := '0';
43 new_rate : out std_logic
44 );
45end FTU_rate_counter;
46
47architecture Behavioral of FTU_rate_counter is
48
49 signal counting_period : integer range 0 to 128*COUNTER_FREQUENCY := 128*COUNTER_FREQUENCY;
50 signal period_finished : std_logic := '0';
51 signal trigger_counts : integer range 0 to 2**16 - 1 := 0;
52 signal clk_1M_sig : std_logic;
53 signal overflow_sig : std_logic := '0';
54 signal new_rate_sig : std_logic := '0';
55
56 component Clock_Divider
57 port(
58 clock_in : IN STD_LOGIC;
59 clock_out : OUT STD_LOGIC
60 );
61 end component;
62
63begin
64
65 Inst_Clock_Divider : Clock_Divider
66 port map (
67 clock_in => clk,
68 clock_out => clk_1M_sig
69 );
70
71 process(cntr_reset, clk_1M_sig)
72
73 variable clk_cntr : integer range 0 to 128*COUNTER_FREQUENCY := 0;
74
75 begin
76
77 if rising_edge(cntr_reset) then
78
79 --formula to calculate counting period from prescaling value
80 if (prescaling = "00000000") then
81 counting_period <= COUNTER_FREQUENCY / (2 * CNTR_FREQ_DIVIDER);
82 else
83 counting_period <= ((conv_integer(unsigned(prescaling)) + 1) / 2) * (COUNTER_FREQUENCY / CNTR_FREQ_DIVIDER);
84 end if;
85
86 clk_cntr := 0;
87 period_finished <= '1';
88 new_rate_sig <= '0';
89 counts <= 0;
90 overflow <= '0';
91
92 elsif rising_edge(clk_1M_sig) then
93 if (clk_cntr < counting_period - 1) then
94 clk_cntr := clk_cntr + 1;
95 period_finished <= '0';
96 new_rate_sig <= '0';
97 else
98 clk_cntr := 0;
99 period_finished <= '1';
100 new_rate_sig <= '1';
101 counts <= trigger_counts;
102 overflow <= overflow_sig;
103 end if;
104 end if;
105 end process;
106
107 process(trigger, period_finished)
108 begin
109 if rising_edge(period_finished) then
110 trigger_counts <= 0;
111 overflow_sig <= '0';
112 else
113 if rising_edge(trigger) then
114 if (trigger_counts < 2**16 - 1) then
115 trigger_counts <= trigger_counts + 1;
116 else
117 trigger_counts <= 0;
118 overflow_sig <= '1';
119 end if;
120 end if;
121 end if;
122 end process;
123
124 new_rate <= new_rate_sig;
125
126end Behavioral;
127
128----------------------------------------------------------------------------------
129
130library IEEE;
131use IEEE.STD_LOGIC_1164.ALL;
132use IEEE.STD_LOGIC_ARITH.ALL;
133use IEEE.STD_LOGIC_UNSIGNED.ALL;
134
135library ftu_definitions;
136USE ftu_definitions.ftu_array_types.all;
137USE ftu_definitions.ftu_constants.all;
138
139entity Clock_Divider is
140 generic(
141 divider : integer := INT_CLK_FREQUENCY / COUNTER_FREQUENCY
142 );
143 port(
144 clock_in : in std_logic;
145 clock_out : out std_logic := '0'
146 );
147end entity Clock_Divider;
148
149architecture RTL of Clock_Divider is
150
151begin
152
153 process (clock_in)
154 variable Z: integer range 0 to divider - 1;
155 begin
156 if rising_edge(clock_in) then
157 if (Z < divider - 1) then
158 Z := Z + 1;
159 else
160 Z := 0;
161 end if;
162 if (Z = 0) then
163 clock_out <= '1';
164 end if;
165 if (Z = divider / 2) then
166 clock_out <= '0';
167 end if;
168 end if;
169 end process;
170
171end architecture RTL;
Note: See TracBrowser for help on using the repository browser.