Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - Timing Problems with counter

 
Thread Tools Search this Thread
Old 03-10-2009, 06:44 PM   #1
Default Timing Problems with counter



Hello,

what i need is a counter that counts from a preset value down to 0
with a clock outside of the system clock domain. The counter run
on a Cyclone III, 15 out of 16 of the counters work well, but one
counts much too slow. Here is the relevant part of the code:


-- transfer clock domain, fcntnext is in the system clockdomain
cntrclk: process (clk, rst)
begin
if (rst = '1') then
fcntnext <= '0';
fcnttoggle <= '0';
elsif falling_edge(clk) then
fcntnext <= '0';
if fcnttoggle /= fcounter then
fcnttoggle <= fcnttoggle xor '1';
if fcnttoggle = '1' then
fcntnext <= '1';
end if;
end if;
end if;
end process cntrclk;

delaycounter: process (clk, rst)
begin
if (rst = '1') then
dcntFin <= '0';
dcounter <= (others => '0');
elsif rising_edge(clk) then
if cntr_reload = '1' then
dcntFin <= '0';
-- Load counter with previous stored value
dcounter <= dcounterLoadVal;
elsif dcounter = std_logic_vector(to_unsigned(0, dcounter_bits)) then
dcntFin <= '1';
elsif fcntnext = '1' then
dcounter <= std_logic_vector(unsigned(dcounter) - 1);
end if;
end if;
end process delaycounter;

all signals are std_logic, and dcounter, dcounterLoadVal are std_logic_vector.

The FPGA runs with a system clock (clk) of 100 MHz.
The fcntnext signal is one clock cycle high (from falling_edge to
falling_edge) after every rising clock edge of the fcounter clock.
The fcounter clock is much slower than the system clock (max 1 MHz
with a Duty-Cycle of 50%).
Has anyone advices?

Best regards,

Steffen



Steffen Koepf
  Reply With Quote
Old 03-11-2009, 05:22 AM   #2
Mike Treseler
 
Posts: n/a
Default Re: Timing Problems with counter
Steffen Koepf wrote:

> what i need is a counter that counts from a preset value down to 0
> with a clock outside of the system clock domain.


A clock outside of the system clock domain
must be used as in input and synchronized
to the system clock.

-- Mike Treseler


Mike Treseler
  Reply With Quote
Old 03-12-2009, 09:28 AM   #3
Pieter Hulshoff
 
Posts: n/a
Default Re: Timing Problems with counter
Hello Steffen,

> what i need is a counter that counts from a preset value down to 0
> with a clock outside of the system clock domain. The counter run
> on a Cyclone III, 15 out of 16 of the counters work well, but one
> counts much too slow. Here is the relevant part of the code:


Please don't use both flanks of a clock, and properly transfer signals from one
clock domain to the next before using them or you will run into timing and/or
meta-stability issues. As an example (I hope I understood your code correctly,
and I don't use asynchronous resets):

delaycounter: PROCESS IS
BEGIN
WAIT UNTIL clk = '1';
fcounter_c2c <= fcounter;
fcounter_meta <= fcounter_c2c;
fcounter_d <= fcounter_meta;
IF cntr_reload = '1' THEN
dcntFin <= '0';
dcounter <= dcounterLoadVal;
ELSIF unsigned( dcounter ) = 0 THEN
dcntFin <= '1';
ELSIF fcounter_meta = '1' AND fcounter_d <= '0' THEN -- rising edge fcounter
dcounter <= std_logic_vector( unsigned( dcounter ) - 1 );
END IF;
IF rst = '1' THEN
dcntFin <= '0';
dcounter <= (OTHERS => '0');
END IF;
END PROCESS delaycounter;

Keep in mind that since cntr_reload is not synchronized with regards to
fcounter, your counter is never completely accurate. Also, are you sure you want
the reset value of dcntFin to be 0 in stead of 1? What is that signal used for
anyway?

Kind regards,

Pieter Hulshoff


Pieter Hulshoff
  Reply With Quote
Old 03-12-2009, 10:10 PM   #4
Steffen Koepf
 
Posts: n/a
Default Re: Timing Problems with counter
Hello Pieter,

Pieter Hulshoff <> wrote:
> Please don't use both flanks of a clock,


Why should one not use this? I know falling edges are not supported
on some devices, but when it is supported?

> clock domain to the next before using them or you will run into timing and/or
> meta-stability issues. As an example (I hope I understood your code correctly,
> and I don't use asynchronous resets):


In the mean time i improved my code a bit and added a rising-edge
synchronizer:

-- transfer clock domain, fcntnext is in the system clockdomain
cntrclk: process (clk, rst)
begin
if (rst = '1') then
clksyncstage1 <= '0';
clksyncstage2 <= '0';
edgedetectff <= '0';
elsif falling_edge(clk) then
clksyncstage1 <= fcounter;
clksyncstage2 <= clksyncstage1;
edgedetectff <= clksyncstage2;
end if;

end process cntrclk;


cntrclkedge: process (clksyncstage2, edgedetectff)
begin
fcntnext <= clksyncstage2 and (not edgedetectff);

end process cntrclkedge;

That solved my problem and the counter works fine now:

delaycounter: process (clk, rst)
begin
if (rst = '1') then
dcntFin <= '0';
dcounter <= (others => '0');
elsif rising_edge(clk) then
if cntr_reload = '1' then
dcntFin <= '0';
dcounter <= dcounterLoadVal; -- Load counter with previous stored val
elsif dcounter = std_logic_vector(to_unsigned(0, dcounter_bits)) then
dcntFin <= '1';
elsif fcntnext = '1' then
dcounter <= std_logic_vector(unsigned(dcounter) - 1);
end if;
end if;
end process delaycounter;

Your code should do the same, thank you.

> Keep in mind that since cntr_reload is not synchronized with regards to
> fcounter, your counter is never completely accurate. Also, are you sure you want
> the reset value of dcntFin to be 0 in stead of 1? What is that signal used for
> anyway?


The signal dcntFin is used as input for a gate for error-signals (from
an analogous comparator outside of the fpga). After a enable signal
is released, the counter starts to run. As long as enable is low
(then cntr_reload = 1) or the counter is running for a certain time
after enable switched to 1, the error-signals are "blanked out"
so that a overshoot of switching power supplies after switching
on (by enable) does not trigger an error.
The counter is a 16 bit counter and it is not important if one
or two clocks are not counted/wrong counted.


Thank you,

Steffen Koepf



Steffen Koepf
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Pioneer DVR-105 and Nero Problems grams@oldtown.com DVD Video 1 08-12-2003 04:17 AM
Re: Pioneer DVR-105 and Nero Problems Ron DVD Video 1 08-08-2003 06:33 PM
Re: Pioneer DVR-105 and Nero Problems Flossie DVD Video 0 08-07-2003 02:25 PM
Re: Pioneer DVR-105 and Nero Problems Flossie DVD Video 0 08-07-2003 08:11 AM
Re: Pioneer DVR-105 and Nero Problems CAM DVD Video 0 08-07-2003 02:30 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46