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

Last change on this file since 9912 was 9911, checked in by weitzel, 14 years ago
FTU rate counting modified; now synthesis works
File size: 4.4 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 cntr_reset = '1' then
78
79 clk_cntr := 0;
80 period_finished <= '1';
81 new_rate_sig <= '0';
82 counts <= 0;
83 overflow <= '0';
84
85 elsif rising_edge(clk_1M_sig) then
86
87 if (clk_cntr < counting_period - 1) then
88 clk_cntr := clk_cntr + 1;
89 period_finished <= '0';
90 new_rate_sig <= '0';
91 else
92 clk_cntr := 0;
93 period_finished <= '1';
94 new_rate_sig <= '1';
95 counts <= trigger_counts;
96 overflow <= overflow_sig;
97 end if;
98 end if;
99 end process;
100
101 process(trigger, period_finished)
102 begin
103 if period_finished = '1' then
104 trigger_counts <= 0;
105 overflow_sig <= '0';
106 else
107 if rising_edge(trigger) then
108 if (trigger_counts < 2**16 - 1) then
109 trigger_counts <= trigger_counts + 1;
110 else
111 trigger_counts <= 0;
112 overflow_sig <= '1';
113 end if;
114 end if;
115 end if;
116 end process;
117
118 process(cntr_reset, prescaling)
119 begin
120 if rising_edge(cntr_reset) then
121 --formula to calculate counting period from prescaling value
122 if (prescaling = "00000000") then
123 counting_period <= COUNTER_FREQUENCY / (2 * CNTR_FREQ_DIVIDER);
124 else
125 counting_period <= ((conv_integer(unsigned(prescaling)) + 1) / 2) * (COUNTER_FREQUENCY / CNTR_FREQ_DIVIDER);
126 end if;
127 end if;
128 end process;
129
130 new_rate <= new_rate_sig;
131
132end Behavioral;
133
134----------------------------------------------------------------------------------
135
136library IEEE;
137use IEEE.STD_LOGIC_1164.ALL;
138use IEEE.STD_LOGIC_ARITH.ALL;
139use IEEE.STD_LOGIC_UNSIGNED.ALL;
140
141library ftu_definitions;
142USE ftu_definitions.ftu_array_types.all;
143USE ftu_definitions.ftu_constants.all;
144
145entity Clock_Divider is
146 generic(
147 divider : integer := INT_CLK_FREQUENCY / COUNTER_FREQUENCY
148 );
149 port(
150 clock_in : in std_logic;
151 clock_out : out std_logic := '0'
152 );
153end entity Clock_Divider;
154
155architecture RTL of Clock_Divider is
156
157begin
158
159 process (clock_in)
160 variable Z: integer range 0 to divider - 1;
161 begin
162 if rising_edge(clock_in) then
163 if (Z < divider - 1) then
164 Z := Z + 1;
165 else
166 Z := 0;
167 end if;
168 if (Z = 0) then
169 clock_out <= '1';
170 end if;
171 if (Z = divider / 2) then
172 clock_out <= '0';
173 end if;
174 end if;
175 end process;
176
177end architecture RTL;
Note: See TracBrowser for help on using the repository browser.