cross clock domain

Crossing clock domains - Signal

From: http://www.fpga4fun.com/CrossClockDomain.html

A signal to another clock domain

Let's say a signal from clkA domain is needed in clkB domain. It needs to be "synchronized" to clkB domain, so we want to build a "synchronizer" design, which takes a signal from clkA domain, and creates a new signal into clkB domain.


In this first design, we assume that the signal-in changes "slowly" compared to both clkA and clkB clock speeds. Typically all you need to do is to use two flip-flops to move the signal from clkA to clkB (to learn why, get back to the links).


 1module Signal_CrossDomain(
 2
 3    clkA, SignalIn,
 4
 5    clkB, SignalOut);
 6
 7 
 8
 9// clkA domain signals
10
11input clkA;
12
13input SignalIn;
14
15 
16
17// clkB domain signals
18
19input clkB;
20
21output SignalOut;
22
23 
24
25// Now let's transfer SignalIn into the clkB clock domain
26
27// We use a two-stages shift-register to synchronize the signal
28
29reg [1:0] SyncA_clkB;
30
31always @(posedge clkB) SyncA_clkB[0<= SignalIn;      // notice that we use clkB
32
33always @(posedge clkB) SyncA_clkB[1<= SyncA_clkB[0]; // notice that we use clkB
34
35 
36
37assign SignalOut = SyncA_clkB[1]; // new signal synchronized to (=ready to be used in) clkB domain
38
39endmodule

 

The two flip-flops have the side-effect of delaying the signal. For example, here are waveforms where you can see the slow moving signal being synchronized (and delayed) to clkB domain by the two flip-flops:


Crossing clock domains - Flag

A flag to another clock domain

If the signal that needs to cross the clock domains is just a pulse (i.e. it lasts just one clock cycle), we call it a "flag". The previous design usually doesn't work (the flag might be missed, or be seen for too long, depending on the ratio of the clocks used).

We still want to use a synchronizer, but one that works for flags.


The trick is to transform the flags into level changes, and then use the two flip-flops technique.

module Flag_CrossDomain(

    clkA, FlagIn_clkA,

    clkB, FlagOut_clkB);

 

// clkA domain signals

input clkA, FlagIn_clkA;

 

// clkB domain signals

input clkB;

output FlagOut_clkB;

 

reg FlagToggle_clkA;

reg [2:0] SyncA_clkB;

 

// this changes level when a flag is seen

always @(posedge clkA) if(FlagIn_clkA) FlagToggle_clkA <= ~FlagToggle_clkA;

 

// which can then be sync-ed to clkB

always @(posedge clkB) SyncA_clkB <= {SyncA_clkB[1:0], FlagToggle_clkA};

 

// and recreate the flag from the level change

assign FlagOut_clkB = (SyncA_clkB[2] ^ SyncA_clkB[1]);

endmodule

Now if you want the clkA domain to receive an acknowledgment (that clkB received the flag), add a busy signal.



 1module FlagAck_CrossDomain(
 2
 3    clkA, FlagIn_clkA, Busy_clkA,
 4
 5    clkB, FlagOut_clkB);
 6
 7 
 8
 9// clkA domain signals
10
11input clkA, FlagIn_clkA;
12
13output Busy_clkA;
14
15 
16
17// clkB domain signals
18
19input clkB;
20
21output FlagOut_clkB;
22
23 
24
25reg FlagToggle_clkA;
26
27reg [2:0] SyncA_clkB;
28
29reg [1:0] SyncB_clkA;
30
31 
32
33always @(posedge clkA) if(FlagIn_clkA & ~Busy_clkA) FlagToggle_clkA <= ~FlagToggle_clkA;
34
35always @(posedge clkB) SyncA_clkB <= {SyncA_clkB[1:0], FlagToggle_clkA};
36
37always @(posedge clkA) SyncB_clkA <= {SyncB_clkA[0], SyncA_clkB[2]};
38
39 
40
41assign FlagOut_clkB = (SyncA_clkB[2^ SyncA_clkB[1]);
42
43assign Busy_clkA = FlagToggle_clkA ^ SyncB_clkA[1];
44
45endmodule

 

Crossing clock domains - Task

Getting a task done in another clock domain

If the clkA domain has a task that needs to be completed in the clkB domain, you can use the following design.


Here's one way to do it


 1module TaskAck_CrossDomain(
 2
 3    clkA, TaskStart_clkA, TaskBusy_clkA, TaskDone_clkA,
 4
 5    clkB, TaskStart_clkB, TaskBusy_clkB, TaskDone_clkB);
 6
 7 
 8
 9// clkA domain signals
10
11input clkA;
12
13input TaskStart_clkA;
14
15output TaskBusy_clkA, TaskDone_clkA;
16
17 
18
19// clkB domain signals
20
21input clkB;
22
23output TaskBusy_clkB, TaskStart_clkB;
24
25input TaskDone_clkB;
26
27 
28
29reg FlagToggle_clkA, FlagToggle_clkB, Busyhold_clkB;
30
31reg [2:0] SyncA_clkB, SyncB_clkA;
32
33 
34
35always @(posedge clkA) if(TaskStart_clkA & ~TaskBusy_clkA) FlagToggle_clkA <= ~FlagToggle_clkA;
36
37 
38
39always @(posedge clkB) SyncA_clkB <= {SyncA_clkB[1:0], FlagToggle_clkA};
40
41assign TaskStart_clkB = (SyncA_clkB[2^ SyncA_clkB[1]);
42
43assign TaskBusy_clkB = TaskStart_clkB | Busyhold_clkB;
44
45always @(posedge clkB) Busyhold_clkB <= ~TaskDone_clkB & TaskBusy_clkB;
46
47always @(posedge clkB) if(TaskBusy_clkB & TaskDone_clkB) FlagToggle_clkB <= FlagToggle_clkA;
48
49 
50
51always @(posedge clkA) SyncB_clkA <= {SyncB_clkA[1:0], FlagToggle_clkB};
52
53assign TaskBusy_clkA = FlagToggle_clkA ^ SyncB_clkA[2];
54
55assign TaskDone_clkA = SyncB_clkA[2^ SyncB_clkA[1];
56
57endmodule

and here's a matching simulation waveform


Crossing clock domains - Data bus

Data bus to another clock domain

To move a data bus (2 bits wide or more) from one clock domain to another, we have several techniques to our disposal.
Here are a few ideas.

  1. Gray code: If the data bus is a monotonic counter (i.e. only incrementing or decrementing), we can convert it to a gray code, which has the ability to cross clock domains (under certain timing conditions).
  2. Data freeze: If the data bus is non-monotonic, use a flag to signal the other domain to capture the value (while it is frozen in the source clock domain).
  3. Data burst: If the data bus has many consecutive values that need to cross the clock domain, use an asynchronous FIFO, where you push values from the source clock domain, and read back values at the other domain.

 

posted on 2009-11-25 13:23  Homography Matrix  阅读(1106)  评论(0编辑  收藏  举报

导航