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

Last change on this file since 10009 was 9939, checked in by weitzel, 14 years ago
FTU RS485 interface is now connected to main control
File size: 4.5 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 --calculate counting period from prescaling value
122 --default is 0.5s - 128s if CNTR_FREQ_DIVIDER = 1
123 if (prescaling = "00000000") then
124 counting_period <= COUNTER_FREQUENCY / (2 * CNTR_FREQ_DIVIDER);
125 elsif (prescaling = "11111111") then
126 counting_period <= 128 * (COUNTER_FREQUENCY / CNTR_FREQ_DIVIDER);
127 else
128 counting_period <= ((conv_integer(unsigned(prescaling)) + 1) / 2) * (COUNTER_FREQUENCY / CNTR_FREQ_DIVIDER);
129 end if;
130 end if;
131 end process;
132
133 new_rate <= new_rate_sig;
134
135end Behavioral;
136
137----------------------------------------------------------------------------------
138
139library IEEE;
140use IEEE.STD_LOGIC_1164.ALL;
141use IEEE.STD_LOGIC_ARITH.ALL;
142use IEEE.STD_LOGIC_UNSIGNED.ALL;
143
144library ftu_definitions;
145USE ftu_definitions.ftu_array_types.all;
146USE ftu_definitions.ftu_constants.all;
147
148entity Clock_Divider is
149 generic(
150 divider : integer := INT_CLK_FREQUENCY / COUNTER_FREQUENCY
151 );
152 port(
153 clock_in : in std_logic;
154 clock_out : out std_logic := '0'
155 );
156end entity Clock_Divider;
157
158architecture RTL of Clock_Divider is
159
160begin
161
162 process (clock_in)
163 variable Z: integer range 0 to divider - 1;
164 begin
165 if rising_edge(clock_in) then
166 if (Z < divider - 1) then
167 Z := Z + 1;
168 else
169 Z := 0;
170 end if;
171 if (Z = 0) then
172 clock_out <= '1';
173 end if;
174 if (Z = divider / 2) then
175 clock_out <= '0';
176 end if;
177 end if;
178 end process;
179
180end architecture RTL;
Note: See TracBrowser for help on using the repository browser.