| 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: vctcxo_tamer |
| 30 |
-- Standard: VHDL-2008 |
| 31 |
-- Description: |
| 32 |
-- Using a known reference (1PPS or 10 MHz), this module logs the number |
| 33 |
-- of VCTCXO clock cycles that have occurred over the previous 1-second, |
| 34 |
-- 10-second, and 100-second intervals. The actual counts for each interval |
| 35 |
-- are compared against the ideal to determine the error. If this error |
| 36 |
-- exceeds the given tolerance, an interrupt is sent to the NIOS processor, |
| 37 |
-- which reads the error and makes adjustments to the VCTCXO trim DAC as |
| 38 |
-- necessary. For best results, all counters should be reset after changing |
| 39 |
-- the trim DAC. |
| 40 |
-- ----------------------------------------------------------------------------- |
| 41 |
entity vctcxo_tamer is |
| 42 |
port( |
| 43 |
-- Physical Interface |
| 44 |
tune_ref : in std_logic; |
| 45 |
vctcxo_clock : in std_logic; |
| 46 |
|
| 47 |
-- Avalon-MM Interface |
| 48 |
mm_clock : in std_logic; |
| 49 |
mm_reset : in std_logic; |
| 50 |
mm_rd_req : in std_logic; |
| 51 |
mm_wr_req : in std_logic; |
| 52 |
mm_addr : in std_logic_vector(7 downto 0); |
| 53 |
mm_wr_data : in std_logic_vector(7 downto 0); |
| 54 |
mm_rd_data : out std_logic_vector(7 downto 0); |
| 55 |
mm_rd_datav : out std_logic; |
| 56 |
mm_wait_req : out std_logic := '0'; |
| 57 |
|
| 58 |
-- Avalon Interrupts |
| 59 |
mm_irq : out std_logic := '0' |
| 60 |
); |
| 61 |
end entity; |
| 62 |
|
| 63 |
architecture arch of vctcxo_tamer is |
| 64 |
|
| 65 |
-- Register Addresses |
| 66 |
constant CONTROL_ADDR : natural := 16#00#; |
| 67 |
constant PPS_ERR_STATUS : natural := 16#01#; |
| 68 |
-- Reserved: 0x02 - 0x03 |
| 69 |
constant PPS_ERR_1S_ADDR0 : natural := 16#04#; |
| 70 |
constant PPS_ERR_1S_ADDR1 : natural := 16#05#; |
| 71 |
constant PPS_ERR_1S_ADDR2 : natural := 16#06#; |
| 72 |
constant PPS_ERR_1S_ADDR3 : natural := 16#07#; |
| 73 |
-- Reserved: 0x08 - 0x0B |
| 74 |
constant PPS_ERR_10S_ADDR0 : natural := 16#0C#; |
| 75 |
constant PPS_ERR_10S_ADDR1 : natural := 16#0D#; |
| 76 |
constant PPS_ERR_10S_ADDR2 : natural := 16#0E#; |
| 77 |
constant PPS_ERR_10S_ADDR3 : natural := 16#0F#; |
| 78 |
-- Reserved: 0x10 - 0x13 |
| 79 |
constant PPS_ERR_100S_ADDR0 : natural := 16#14#; |
| 80 |
constant PPS_ERR_100S_ADDR1 : natural := 16#15#; |
| 81 |
constant PPS_ERR_100S_ADDR2 : natural := 16#16#; |
| 82 |
constant PPS_ERR_100S_ADDR3 : natural := 16#17#; |
| 83 |
-- Reserved: 0x18 - 0xFF |
| 84 |
|
| 85 |
-- Error tolerance on each clock count, calculated for a goal of < 10 PPB |
| 86 |
-- err_counts = (seconds * nominal_vctcxo_freq) * (10 * 1e-9) |
| 87 |
constant PPS_1S_ERROR_TOL : signed(31 downto 0) := to_signed(1 , 32); |
| 88 |
constant PPS_10S_ERROR_TOL : signed(31 downto 0) := to_signed(4 , 32); |
| 89 |
constant PPS_100S_ERROR_TOL : signed(31 downto 0) := to_signed(38 , 32); |
| 90 |
|
| 91 |
type tune_mode_t is ( DISABLED, PPS, \10MHZ\ ); |
| 92 |
|
| 93 |
function unpack( x : std_logic_vector(1 downto 0) ) return tune_mode_t is |
| 94 |
variable rv : tune_mode_t := DISABLED; |
| 95 |
begin |
| 96 |
case( x ) is |
| 97 |
when "01" => rv := PPS; |
| 98 |
when "10" => rv := \10MHZ\; |
| 99 |
when others => rv := DISABLED; |
| 100 |
end case; |
| 101 |
return rv; |
| 102 |
end function; |
| 103 |
|
| 104 |
-- Counter data |
| 105 |
type count_t is record |
| 106 |
target : signed(63 downto 0); |
| 107 |
count : signed(63 downto 0); |
| 108 |
error : signed(31 downto 0); |
| 109 |
error_v : std_logic; |
| 110 |
count_v : std_logic; |
| 111 |
end record; |
| 112 |
|
| 113 |
signal pps_1s : count_t := ( target => x"0000_0000_0249_F000", --384e5 |
| 114 |
count => (others => '0'), |
| 115 |
error => (others => '0'), |
| 116 |
error_v => '0', |
| 117 |
count_v => '0' ); |
| 118 |
|
| 119 |
signal pps_10s : count_t := ( target => x"0000_0000_16E3_6000", -- 384e6 |
| 120 |
count => (others => '0'), |
| 121 |
error => (others => '0'), |
| 122 |
error_v => '0', |
| 123 |
count_v => '0' ); |
| 124 |
|
| 125 |
signal pps_100s : count_t := ( target => x"0000_0000_E4E1_C000", -- 384e7 |
| 126 |
count => (others => '0'), |
| 127 |
error => (others => '0'), |
| 128 |
error_v => '0', |
| 129 |
count_v => '0' ); |
| 130 |
|
| 131 |
-- Asynchronous |
| 132 |
signal ref_1pps : std_logic := '0'; |
| 133 |
|
| 134 |
-- Tune_ref-synchronous signals |
| 135 |
signal tune_ref_reset : std_logic := '1'; |
| 136 |
signal ref_10mhz_pps : std_logic := '0'; |
| 137 |
signal tune_ref_mode_update_req : std_logic := '0'; |
| 138 |
signal tune_ref_mode_update_ack : std_logic := '0'; |
| 139 |
signal tune_ref_mode : tune_mode_t := DISABLED; |
| 140 |
signal tune_ref_mode_hs : tune_mode_t := DISABLED; |
| 141 |
|
| 142 |
-- VCTCXO-synchronous signals |
| 143 |
signal ref_1pps_sync : std_logic := '0'; |
| 144 |
signal ref_1pps_pulse : std_logic := '0'; |
| 145 |
signal vctcxo_reset : std_logic := '1'; |
| 146 |
|
| 147 |
-- System-synchronous signals |
| 148 |
signal mm_control_reg : std_logic_vector(7 downto 0) := x"21"; |
| 149 |
alias mm_tune_mode : std_logic_vector(1 downto 0) is mm_control_reg(7 downto 6); |
| 150 |
alias mm_pps_irq_clear : std_logic is mm_control_reg(5); |
| 151 |
alias mm_pps_irq_enable : std_logic is mm_control_reg(4); |
| 152 |
alias mm_vctcxo_reset : std_logic is mm_control_reg(0); |
| 153 |
|
| 154 |
begin |
| 155 |
|
| 156 |
-- If the input reference is 10 MHz, use it as a clock to increment a |
| 157 |
-- counter that will generate a single pulse every second. |
| 158 |
ref_10mhz_count_proc : process( tune_ref, tune_ref_reset ) |
| 159 |
constant REF_10MHZ_RESET_VAL : natural range 0 to 2**24-1 := 10e6; |
| 160 |
constant REF_10MHZ_PULSE_DURATION : natural range 0 to 2**24-1 := 1e3; |
| 161 |
variable v_10mhz_count : natural range 0 to 2**24-1 := 10e6; |
| 162 |
variable v_tune_mode : tune_mode_t := DISABLED; |
| 163 |
begin |
| 164 |
if( tune_ref_reset = '1' ) then |
| 165 |
ref_10mhz_pps <= '0'; |
| 166 |
v_10mhz_count := REF_10MHZ_RESET_VAL; |
| 167 |
v_tune_mode := DISABLED; |
| 168 |
elsif( rising_edge(tune_ref) ) then |
| 169 |
ref_10mhz_pps <= '0'; |
| 170 |
if( v_tune_mode /= tune_ref_mode ) then |
| 171 |
v_10mhz_count := REF_10MHZ_RESET_VAL; |
| 172 |
elsif( tune_ref_mode = \10MHZ\ ) then |
| 173 |
v_10mhz_count := v_10mhz_count - 1; |
| 174 |
if( v_10mhz_count = 0 ) then |
| 175 |
v_10mhz_count := REF_10MHZ_RESET_VAL; |
| 176 |
end if; |
| 177 |
if( v_10mhz_count <= REF_10MHZ_PULSE_DURATION ) then |
| 178 |
ref_10mhz_pps <= '1'; |
| 179 |
end if; |
| 180 |
end if; |
| 181 |
v_tune_mode := tune_ref_mode; |
| 182 |
end if; |
| 183 |
end process; |
| 184 |
|
| 185 |
-- Tuning reference MUX |
| 186 |
ref_1pps <= tune_ref when ( unpack(mm_tune_mode) = PPS ) else |
| 187 |
ref_10mhz_pps when ( unpack(mm_tune_mode) = \10MHZ\ ) else |
| 188 |
'0'; |
| 189 |
|
| 190 |
-- Bring the 1PPS into the VCTCXO clock domain |
| 191 |
U_pps_sync : entity work.synchronizer |
| 192 |
generic map ( |
| 193 |
RESET_LEVEL => '0' |
| 194 |
) port map ( |
| 195 |
reset => vctcxo_reset, |
| 196 |
clock => vctcxo_clock, |
| 197 |
async => ref_1pps, |
| 198 |
sync => ref_1pps_sync |
| 199 |
); |
| 200 |
|
| 201 |
-- Generate a single-cycle version of the 1PPS signal |
| 202 |
U_edge_detector_pps : entity work.edge_detector |
| 203 |
generic map ( |
| 204 |
EDGE_RISE => '1', |
| 205 |
EDGE_FALL => '0' |
| 206 |
) |
| 207 |
port map ( |
| 208 |
clock => vctcxo_clock, |
| 209 |
reset => vctcxo_reset, |
| 210 |
sync_in => ref_1pps_sync, |
| 211 |
pulse_out => ref_1pps_pulse |
| 212 |
); |
| 213 |
|
| 214 |
-- Count number of VCTCXO clock cycles in the last 1 second |
| 215 |
U_pps_counter_1s : entity work.pps_counter |
| 216 |
generic map ( |
| 217 |
COUNT_WIDTH => pps_1s.count'length, |
| 218 |
PPS_PULSES => 1 |
| 219 |
) |
| 220 |
port map ( |
| 221 |
sys_clock => mm_clock, |
| 222 |
sys_reset => mm_reset or mm_vctcxo_reset, |
| 223 |
sys_count => pps_1s.count, |
| 224 |
sys_count_v => pps_1s.count_v, |
| 225 |
vctcxo_clock => vctcxo_clock, |
| 226 |
vctcxo_reset => vctcxo_reset, |
| 227 |
vctcxo_pps => ref_1pps_pulse |
| 228 |
); |
| 229 |
|
| 230 |
-- Count number of VCTCXO clock cycles in the last 10 seconds |
| 231 |
U_pps_counter_10s : entity work.pps_counter |
| 232 |
generic map ( |
| 233 |
COUNT_WIDTH => pps_10s.count'length, |
| 234 |
PPS_PULSES => 10 |
| 235 |
) |
| 236 |
port map ( |
| 237 |
sys_clock => mm_clock, |
| 238 |
sys_reset => mm_reset or mm_vctcxo_reset, |
| 239 |
sys_count => pps_10s.count, |
| 240 |
sys_count_v => pps_10s.count_v, |
| 241 |
vctcxo_clock => vctcxo_clock, |
| 242 |
vctcxo_reset => vctcxo_reset, |
| 243 |
vctcxo_pps => ref_1pps_pulse |
| 244 |
); |
| 245 |
|
| 246 |
-- Count number of VCTCXO clock cycles in the last 100 seconds |
| 247 |
U_pps_counter_100s : entity work.pps_counter |
| 248 |
generic map ( |
| 249 |
COUNT_WIDTH => pps_100s.count'length, |
| 250 |
PPS_PULSES => 100 |
| 251 |
) |
| 252 |
port map ( |
| 253 |
sys_clock => mm_clock, |
| 254 |
sys_reset => mm_reset or mm_vctcxo_reset, |
| 255 |
sys_count => pps_100s.count, |
| 256 |
sys_count_v => pps_100s.count_v, |
| 257 |
vctcxo_clock => vctcxo_clock, |
| 258 |
vctcxo_reset => vctcxo_reset, |
| 259 |
vctcxo_pps => ref_1pps_pulse |
| 260 |
); |
| 261 |
|
| 262 |
-- Interrupt Request |
| 263 |
int_req_proc : process( mm_clock ) |
| 264 |
variable tmp_1s_err : signed(PPS_1S_ERROR_TOL'range) := (others => '0'); |
| 265 |
variable tmp_10s_err : signed(PPS_10S_ERROR_TOL'range) := (others => '0'); |
| 266 |
variable tmp_100s_err : signed(PPS_100S_ERROR_TOL'range) := (others => '0'); |
| 267 |
begin |
| 268 |
if( rising_edge(mm_clock) ) then |
| 269 |
|
| 270 |
if( (pps_1s.count_v = '1') and (mm_pps_irq_enable = '1') ) then |
| 271 |
tmp_1s_err := resize( (pps_1s.count - pps_1s.target), 32 ); |
| 272 |
pps_1s.error <= tmp_1s_err; |
| 273 |
if( abs(tmp_1s_err) > PPS_1S_ERROR_TOL ) then |
| 274 |
pps_1s.error_v <= '1'; |
| 275 |
mm_irq <= '1'; |
| 276 |
end if; |
| 277 |
end if; |
| 278 |
|
| 279 |
if( (pps_10s.count_v = '1') and (mm_pps_irq_enable = '1') ) then |
| 280 |
tmp_10s_err := resize( (pps_10s.count - pps_10s.target), 32 ); |
| 281 |
pps_10s.error <= tmp_10s_err; |
| 282 |
if( abs(tmp_10s_err) > PPS_10S_ERROR_TOL ) then |
| 283 |
pps_10s.error_v <= '1'; |
| 284 |
mm_irq <= '1'; |
| 285 |
end if; |
| 286 |
end if; |
| 287 |
|
| 288 |
if( (pps_100s.count_v = '1') and (mm_pps_irq_enable = '1') ) then |
| 289 |
tmp_100s_err := resize( (pps_100s.count - pps_100s.target), 32 ); |
| 290 |
pps_100s.error <= tmp_100s_err; |
| 291 |
if( abs(tmp_100s_err) > PPS_100S_ERROR_TOL ) then |
| 292 |
pps_100s.error_v <= '1'; |
| 293 |
mm_irq <= '1'; |
| 294 |
end if; |
| 295 |
end if; |
| 296 |
|
| 297 |
if( mm_pps_irq_clear = '1' ) then |
| 298 |
-- Don't need to clear out the error count |
| 299 |
-- because we are invalidating it here. |
| 300 |
pps_1s.error_v <= '0'; |
| 301 |
pps_10s.error_v <= '0'; |
| 302 |
pps_100s.error_v <= '0'; |
| 303 |
mm_irq <= '0'; |
| 304 |
end if; |
| 305 |
|
| 306 |
end if; |
| 307 |
end process; |
| 308 |
|
| 309 |
-- Avalon-MM Read Process |
| 310 |
mm_read_proc : process( mm_clock ) |
| 311 |
begin |
| 312 |
if( rising_edge(mm_clock) ) then |
| 313 |
|
| 314 |
-- Data valid is just a one-cycle delay of read request |
| 315 |
mm_rd_datav <= mm_rd_req; |
| 316 |
mm_wait_req <= '0'; |
| 317 |
|
| 318 |
case to_integer(unsigned(mm_addr)) is |
| 319 |
|
| 320 |
-- Control Register |
| 321 |
when CONTROL_ADDR => |
| 322 |
mm_rd_data <= mm_control_reg; |
| 323 |
|
| 324 |
-- PPS Error Status |
| 325 |
when PPS_ERR_STATUS => |
| 326 |
mm_rd_data(7 downto 3) <= (others => '0'); |
| 327 |
mm_rd_data(2) <= pps_100s.error_v; |
| 328 |
mm_rd_data(1) <= pps_10s.error_v; |
| 329 |
mm_rd_data(0) <= pps_1s.error_v; |
| 330 |
|
| 331 |
-- 1 Second Count Error |
| 332 |
when PPS_ERR_1S_ADDR0 => |
| 333 |
mm_rd_data <= std_logic_vector(pps_1s.error(7 downto 0)); |
| 334 |
|
| 335 |
when PPS_ERR_1S_ADDR1 => |
| 336 |
mm_rd_data <= std_logic_vector(pps_1s.error(15 downto 8)); |
| 337 |
|
| 338 |
when PPS_ERR_1S_ADDR2 => |
| 339 |
mm_rd_data <= std_logic_vector(pps_1s.error(23 downto 16)); |
| 340 |
|
| 341 |
when PPS_ERR_1S_ADDR3 => |
| 342 |
mm_rd_data <= std_logic_vector(pps_1s.error(31 downto 24)); |
| 343 |
|
| 344 |
-- 10 Second Count Error |
| 345 |
when PPS_ERR_10S_ADDR0 => |
| 346 |
mm_rd_data <= std_logic_vector(pps_10s.error(7 downto 0)); |
| 347 |
|
| 348 |
when PPS_ERR_10S_ADDR1 => |
| 349 |
mm_rd_data <= std_logic_vector(pps_10s.error(15 downto 8)); |
| 350 |
|
| 351 |
when PPS_ERR_10S_ADDR2 => |
| 352 |
mm_rd_data <= std_logic_vector(pps_10s.error(23 downto 16)); |
| 353 |
|
| 354 |
when PPS_ERR_10S_ADDR3 => |
| 355 |
mm_rd_data <= std_logic_vector(pps_10s.error(31 downto 24)); |
| 356 |
|
| 357 |
-- 100 Second Count Error |
| 358 |
when PPS_ERR_100S_ADDR0 => |
| 359 |
mm_rd_data <= std_logic_vector(pps_100s.error(7 downto 0)); |
| 360 |
|
| 361 |
when PPS_ERR_100S_ADDR1 => |
| 362 |
mm_rd_data <= std_logic_vector(pps_100s.error(15 downto 8)); |
| 363 |
|
| 364 |
when PPS_ERR_100S_ADDR2 => |
| 365 |
mm_rd_data <= std_logic_vector(pps_100s.error(23 downto 16)); |
| 366 |
|
| 367 |
when PPS_ERR_100S_ADDR3 => |
| 368 |
mm_rd_data <= std_logic_vector(pps_100s.error(31 downto 24)); |
| 369 |
|
| 370 |
when others => |
| 371 |
null; |
| 372 |
|
| 373 |
end case; |
| 374 |
end if; |
| 375 |
end process; |
| 376 |
|
| 377 |
-- Avalon-MM Write Process |
| 378 |
mm_write_proc : process( mm_clock ) |
| 379 |
begin |
| 380 |
if( rising_edge(mm_clock) ) then |
| 381 |
mm_pps_irq_clear <= '0'; |
| 382 |
if( mm_wr_req = '1' ) then |
| 383 |
case to_integer(unsigned(mm_addr)) is |
| 384 |
when CONTROL_ADDR => |
| 385 |
mm_control_reg <= mm_wr_data; |
| 386 |
when others => |
| 387 |
null; |
| 388 |
end case; |
| 389 |
end if; |
| 390 |
end if; |
| 391 |
end process; |
| 392 |
|
| 393 |
-- Asynchronous reset, synchronous deassertion of reset |
| 394 |
U_reset_sync_vctcxo : entity work.reset_synchronizer |
| 395 |
generic map ( |
| 396 |
INPUT_LEVEL => '1', |
| 397 |
OUTPUT_LEVEL => '1' |
| 398 |
) |
| 399 |
port map ( |
| 400 |
clock => vctcxo_clock, |
| 401 |
async => mm_vctcxo_reset or mm_reset, |
| 402 |
sync => vctcxo_reset |
| 403 |
); |
| 404 |
|
| 405 |
-- Asynchronous reset, synchronous deassertion of reset |
| 406 |
U_reset_sync_tune_ref : entity work.reset_synchronizer |
| 407 |
generic map ( |
| 408 |
INPUT_LEVEL => '1', |
| 409 |
OUTPUT_LEVEL => '1' |
| 410 |
) |
| 411 |
port map ( |
| 412 |
clock => tune_ref, |
| 413 |
async => mm_vctcxo_reset or mm_reset, |
| 414 |
sync => tune_ref_reset |
| 415 |
); |
| 416 |
|
| 417 |
-- Tune Mode Updater: Keep requesting tune_mode updates |
| 418 |
tune_mode_updater_proc : process( tune_ref, tune_ref_reset ) |
| 419 |
begin |
| 420 |
if( tune_ref_reset = '1' ) then |
| 421 |
tune_ref_mode_update_req <= '0'; |
| 422 |
tune_ref_mode <= DISABLED; |
| 423 |
elsif( rising_edge(tune_ref) ) then |
| 424 |
if( tune_ref_mode_update_ack = '1' ) then |
| 425 |
tune_ref_mode_update_req <= '0'; |
| 426 |
tune_ref_mode <= tune_ref_mode_hs; |
| 427 |
else |
| 428 |
tune_ref_mode_update_req <= '1'; |
| 429 |
end if; |
| 430 |
end if; |
| 431 |
end process; |
| 432 |
|
| 433 |
-- Get the tune mode into the tune_ref clock domain |
| 434 |
U_handshake_tune_mode : entity work.handshake |
| 435 |
generic map ( |
| 436 |
DATA_WIDTH => 2 |
| 437 |
) |
| 438 |
port map ( |
| 439 |
source_reset => mm_reset or mm_vctcxo_reset, |
| 440 |
source_clock => mm_clock, |
| 441 |
source_data => mm_tune_mode, |
| 442 |
dest_reset => tune_ref_reset, |
| 443 |
dest_clock => tune_ref, |
| 444 |
unpack(dest_data) => tune_ref_mode_hs, |
| 445 |
dest_req => tune_ref_mode_update_req, |
| 446 |
dest_ack => tune_ref_mode_update_ack |
| 447 |
); |
| 448 |
|
| 449 |
end architecture; |