Debian Codegraph @

hdl/fpga/ip/nuand/vctcxo_tamer/vhdl/pps_counter.vhd

raw
1 -- From Altera Solution ID rd05312011_49, need the following for Qsys to use VHDL 2008:
2 -- altera vhdl_input_version vhdl_2008
3
4 -- Copyright (c) 2015 Nuand LLC
5 --
6 -- Permission is hereby granted, free of charge, to any person obtaining a copy
7 -- of this software and associated documentation files (the "Software"), to deal
8 -- in the Software without restriction, including without limitation the rights
9 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 -- copies of the Software, and to permit persons to whom the Software is
11 -- furnished to do so, subject to the following conditions:
12 --
13 -- The above copyright notice and this permission notice shall be included in
14 -- all copies or substantial portions of the Software.
15 --
16 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 -- THE SOFTWARE.
23
24 library ieee;
25 use ieee.std_logic_1164.all;
26 use ieee.numeric_std.all;
27
28 -- -----------------------------------------------------------------------------
29 -- Entity: pps_counter
30 -- Standard: VHDL-2008
31 -- Description: Counts number of VCTCXO clock cycles that have occurred between
32 -- the specified number of 1PPS pulses. The count is output
33 -- in the system clock domain along with a valid signal.
34 -- -----------------------------------------------------------------------------
35 entity pps_counter is
36 generic(
37 COUNT_WIDTH : positive := 64;
38 PPS_PULSES : positive range 1 to 1023
39 );
40 port(
41 -- System-synchronous signals
42 sys_clock : in std_logic;
43 sys_reset : in std_logic;
44 sys_count : out signed(COUNT_WIDTH-1 downto 0);
45 sys_count_v : out std_logic;
46
47 -- Measurement signals
48 vctcxo_clock : in std_logic;
49 vctcxo_reset : in std_logic;
50 vctcxo_pps : in std_logic
51 );
52 end entity;
53
54 architecture arch of pps_counter is
55
56 -- FSM States
57 type fsm_t is (
58 IDLE,
59 INCREMENT,
60 HANDSHAKE_REQ
61 );
62
63 -- State of internal signals
64 type state_t is record
65 state : fsm_t;
66 pps_count : natural range 0 to 1023;
67 handshake_d : signed(COUNT_WIDTH-1 downto 0);
68 handshake_dv : std_logic;
69 end record;
70
71 constant reset_value : state_t := (
72 state => IDLE,
73 pps_count => 0,
74 handshake_d => (others => '-'),
75 handshake_dv => '0'
76 );
77
78 signal current : state_t;
79 signal future : state_t;
80
81 -- System clock domain
82 signal sys_hs_req : std_logic := '0';
83 signal sys_hs_ack : std_logic := '0';
84
85 -- VCTCXO clock domain
86 signal vctcxo_reset_hold : std_logic := '1';
87 signal vctcxo_hs_ack : std_logic := '0';
88
89 begin
90
91 -- Counter reset control process. When the counter is reset, it may have
92 -- been because of a trim DAC retune. Need to keep the counter FSM in reset
93 -- for a period of time to allow the trim DAC to settle into its new value.
94 -- A retune takes about 800 us for a full swing transition. Using the VCTCXO
95 -- clock here because it's slower (easier to meet timing, fewer counter
96 -- bits), and the change in precision during a retune doesn't matter here.
97 reset_proc : process( vctcxo_clock, vctcxo_reset )
98 -- Number of VCTCXO clock cycles before releasing the counter reset.
99 -- A 15-bit counter starting at 0x7FFF will take ~850 us at 38.4 MHz.
100 variable rst_cnt_v : unsigned(14 downto 0) := (others => '1');
101 begin
102 if( vctcxo_reset = '1' ) then
103 rst_cnt_v := (others => '1');
104 vctcxo_reset_hold <= '1';
105 elsif( rising_edge( vctcxo_clock ) ) then
106 if( rst_cnt_v /= 0 ) then
107 rst_cnt_v := rst_cnt_v - 1;
108 vctcxo_reset_hold <= '1';
109 else
110 vctcxo_reset_hold <= '0';
111 end if;
112 end if;
113 end process;
114
115 sync_proc : process( vctcxo_clock, vctcxo_reset_hold )
116 begin
117 if( vctcxo_reset_hold = '1' ) then
118 current <= reset_value;
119 elsif( rising_edge(vctcxo_clock) ) then
120 current <= future;
121 end if;
122 end process;
123
124 comb_proc : process( all )
125 begin
126
127 -- Next state defaults
128 future <= current;
129 future.handshake_dv <= '0';
130
131 case( current.state ) is
132 when IDLE =>
133 future.handshake_d <= (others => '0');
134 future.pps_count <= 0;
135 if( vctcxo_pps = '1' ) then
136 future.state <= INCREMENT;
137 end if;
138 when INCREMENT =>
139 if( vctcxo_pps = '1' ) then
140 future.pps_count <= current.pps_count + 1;
141 end if;
142 if( current.pps_count < PPS_PULSES ) then
143 future.handshake_d <= current.handshake_d + 1;
144 else
145 future.handshake_dv <= '1';
146 future.state <= HANDSHAKE_REQ;
147 end if;
148 when HANDSHAKE_REQ =>
149 future.handshake_dv <= '1';
150 if( vctcxo_hs_ack = '1' ) then
151 future.handshake_dv <= '0';
152 future.state <= IDLE;
153 end if;
154 end case;
155 end process;
156
157 -- Bring the handshake ack into VCTCXO clock domain
158 U_sync_hs_ack : entity work.synchronizer
159 generic map (
160 RESET_LEVEL => '0'
161 ) port map (
162 reset => vctcxo_reset,
163 clock => vctcxo_clock,
164 async => sys_hs_ack,
165 sync => vctcxo_hs_ack
166 );
167
168 -- Bring the handshake req into System clock domain
169 U_sync_hs_req : entity work.synchronizer
170 generic map (
171 RESET_LEVEL => '0'
172 ) port map (
173 reset => sys_reset,
174 clock => sys_clock,
175 async => current.handshake_dv,
176 sync => sys_hs_req
177 );
178
179 -- Use handshaking to get the clock count value into the system clock domain
180 U_handshake : entity work.handshake
181 generic map (
182 DATA_WIDTH => COUNT_WIDTH
183 ) port map (
184 source_reset => vctcxo_reset_hold,
185 source_clock => vctcxo_clock,
186 source_data => std_logic_vector(current.handshake_d),
187 dest_reset => sys_reset,
188 dest_clock => sys_clock,
189 signed(dest_data) => sys_count,
190 dest_req => sys_hs_req,
191 dest_ack => sys_hs_ack
192 );
193
194 -- Generate a single-cycle 'count valid' pulse
195 U_edge_detector_count_v : entity work.edge_detector
196 generic map (
197 EDGE_RISE => '1',
198 EDGE_FALL => '0'
199 )
200 port map (
201 clock => sys_clock,
202 reset => sys_reset,
203 sync_in => sys_hs_ack,
204 pulse_out => sys_count_v
205 );
206
207 end architecture;