Crossing Clock Domains in an FPGA

  Рет қаралды 66,772

nandland

nandland

7 жыл бұрын

NEW! Buy my book, the best FPGA book for beginners: nandland.com/book-getting-sta...
How to go from slow to fast, fast to slow clock domains inside of an FPGA with code examples. Also shows how to use FIFOs to cross boundaries with large amounts of data. I'll describe what to do about timing errors that happen when you cross clock domains.
This is applicable to designs in both VHDL and Verilog.
Support this channel! Buy a Go Board: www.nandland.com/goboard/intr...
Like my content? Help me make more at Patreon!
/ nandland

Пікірлер: 64
@asmi06
@asmi06 7 жыл бұрын
It's funny that you've posted a video about timing errors exactly as I was fixing one in my design :) I've found this snippet of SV code somewhere for crossing domains which I find kind of neat, and that's what I always use: bit [2:0] buf; always_ff @(posedge clk) buf
@asmi06
@asmi06 7 жыл бұрын
Oh, and I've forgot to mention that Xilinx has a bunch of macros designed specifically to deal with cross-clock syncronization issues - they are xmp_cdc_[array_single | async_rst | gray | handshake | pulse | single | sync_rst], described in UG953 - "Vivado Design Suite 7 Series FPGA and Zynq-7000 All Programmable SoC Libraries Guide", chapter 2.
@sourabhtapas
@sourabhtapas 5 жыл бұрын
Great video! Lucid explanation. Always gets something to learn. Keep posting.
@megha3678
@megha3678 4 жыл бұрын
Thank you for this video. Explanation on small things using coding is really helpful.
@electronash
@electronash 7 жыл бұрын
Just what I needed right now. ;) I've had to use registers for async handshake signals on a RAM controller before, and that was fairly hard to debug. I've also been messing with differing video timings recently, where a device (an N64) is using a pixel clock of ~12.5 MHz, and the actual output pixel clock is 27MHz (480p), so I had to use dual-port RAMs for the line buffers. The metastable stuff is weird, because they can calc the exact "MTBF", but it's usually in the range of a few million years. lol
@BorissGoyhman
@BorissGoyhman 7 жыл бұрын
Awesome video! You don't have to know the clock frequencies of the fast and the slow clocks to go from a fast clock to a slow clock. You can use feedback from the slow clock to the fast clock for the pulse stretch. Once the slow clock logic "sees" the pulse, it can signal to the fast clock logic to stop stretching...
@Nandland
@Nandland 7 жыл бұрын
Good idea! Thanks! This would require writing two timing constraints, one for each direction, but it's a good solution for writing robust portable code.
@bhuvaneshs.k638
@bhuvaneshs.k638 2 жыл бұрын
@@Nandland instead of stretching based on Freq .. can you try handshake synchronisation? Please do a video on tht
@steefant
@steefant 7 жыл бұрын
The explanations about metastability in the video are imprecise. Metastability refers to a state of unbounded time during which it is unknown which state will be adopted by the system eventually. During that (unbounded!) time the output of the system (e.g. FF) is undefined and might even oscillate. Also, not *every* setup or hold time violation will lead to metastability (it is actually rather rare and it is not that easy to actually provoke it to happen and make it visible in real hardware). However, even without Metastability, setup and hold violations are a problem because the FF might simply not capture the correct state at the right time even though its internal state (and output) is not metastable. *This* is the common reason that leads to problems when crossing clock domain boundaries (which often happens unknowingly) but is indistinguishable from short metastabilities anyway (shorter than the clock period). Also, by no means does using 2 FFs eradicate the possibility of metastability on the second output completely. It is mentioned briefly in the video but is largely ignored (in the video by describing the output of the first FF as metastable (which it is not necessarily but you have to treat it that way) and the 2nd FF as stable (which it isnt absolutely always but we treat it that way anyway). Cf. en.wikipedia.org/wiki/Metastability_in_electronics
@Nandland
@Nandland 7 жыл бұрын
Thanks for the reply! I'm actually planning to go more into details about Setup and Hold time and Metastability so I appreciate this comment. I will be more precise if I go into details about it, but you're right I did gloss over the specifics.
@greenavos
@greenavos 7 жыл бұрын
Actually, this video had super precise timing. I coincidentally had a lecture on this topic right after watching this video. It was super useful. When you say, "The explanations in the video are imprecise" and point out what is ignored, you sound frustrated. These are educational videos for learners. Please keep your comments clean and make statements that add educational value.
@brucenaylor9563
@brucenaylor9563 6 жыл бұрын
I wish Russel had made this back in the summer of 2014 when a project I was on failed for just this reason. 99% reliable is just not good enough for customers.
@loganj6203
@loganj6203 3 жыл бұрын
@@greenavos What did you have a lecture after watching this video? Could you tell me what it was because I'm still confused like you did.. please let me know! Thank you
@nkronert
@nkronert Жыл бұрын
I didn't get the part where having two sequential FFs driven by the same clock would eliminate metastability. I hope I'll find the video that goes into the details.
@virajpadwal7624
@virajpadwal7624 3 жыл бұрын
Very intuitive explanation thank you !
@uvw654
@uvw654 6 жыл бұрын
excellent video, explained very clear.
@varunrain8763
@varunrain8763 5 жыл бұрын
Hi Russel, Could you tell me what r1,r2,r3_data correspond to in the slow to fast domain diagram? I got confused coz of your in-code comments on metastability. it said r1 is metastable, but the output from the second register is the one showing metastability in the diagram. So, could you please explain which notation corresponds to what in the diagram
@bennguyen1313
@bennguyen1313 4 жыл бұрын
For a slow clock that updates a register, I can see how logic clocked with a fast clock could see data in flux.. and so passing it through a few dffs makes sense.. however, regarding the pulse stretching in the case of fast to slow... what if you're not dealing with pulses, i.e. the fast clock is updating data and it remains constant until updated again.. would the slow clock logic that acts on that data need to do anything?
@ioncasu9825
@ioncasu9825 2 жыл бұрын
Thank you very much.
@fakhar-eddinesabbari4594
@fakhar-eddinesabbari4594 3 жыл бұрын
Thank you !
@alexanderstadnitsky8549
@alexanderstadnitsky8549 3 жыл бұрын
thanks from russia fow great content. Very helpful.
@ninoporcino5790
@ninoporcino5790 4 жыл бұрын
thank you for the quick explanation. I was able to fix the emulation of a 3.6 MHz Z80 computer with a 10.7 MHz video controller. Previously I had to run the video at an integer multiple of the CPU clock. Still I don't get why on "real" computers there is no cross-clock domain.
@rakeshmp9060
@rakeshmp9060 4 жыл бұрын
hi, nice explanation. i have one doubt. A clock 100 Mhz and B clock domain 10 Mhz,(fast to slow). how can cross this with out using stretching or fifo
@alexscarbro796
@alexscarbro796 4 жыл бұрын
Thank you for posting these videos - I’m learning Verilog with the help of your videos. Looking at the VHDL code at 11:33 are the last two statements swapped? E.g. r_Stable
@Neverlent
@Neverlent 4 жыл бұрын
No, I don't believe so. Although, the order doesn't really matter because everything happens at the same time, or the same clock cycle, but it should still be kept the way nandland showed it for human readability sake. The signal w_Stretched is coming in from outside the process and we want to ensure anything else in that process uses r_Stable, because it's stable, therefore keeping the human readability - top to bottom, logically sound.
@curtisnotestine3134
@curtisnotestine3134 4 жыл бұрын
What are the timing constraints and how are they used to get the place and route score to zero? Also, what is the interpretation of a non-zero P&R score? My thanks in advance....
@babatundetaiwo2817
@babatundetaiwo2817 2 жыл бұрын
What are some examples of how I would "APPLY" clock domain crossing techniques.? Are there any modules where I could practice this technique.?
@electronash
@electronash 7 жыл бұрын
Is it not really robust enough to just use a single flop, then use the incoming async signal to directly check against the flop for a rising or falling edge? I usually just use two flops, then use a combinatorial (wire assign) to check for falling / rising, and it normally works fine? (usually when sampling an async /WRITE signal from an IDE interface, when the FPGA clock is around 50MHz, for example.)
@Nandland
@Nandland 7 жыл бұрын
Hm, interesting, I've never done it that way. I can't say for certain if it's bad. In general I use the rule of thumb that you should never use an async signal in your FPGA unless it's going to an async reset input on a FF. It's only 1 extra register to look for the rising edge.
@electronash
@electronash 7 жыл бұрын
True. I think I may have had issues with projects before that checked the original async signal before being registered, so I guess at least two flops is preferred. The only thing with adding too many flops is if the faster clock isn't too much higher than the slow clock, and there will be a bit of a clock delay. That may or may not matter in the specific design though, of course. ;) I still find FPGAs confusing though, as a lot of stuff has a delay of one clock. ie. you assign a value to a reg on one clock, but it doesn't update the output signal until the NEXT clock etc. That can be tricky with things like reading from an SRAM etc. Where you have to assert the /Output Enable on the first clock, then grab the data on the next clock.
@adumont
@adumont 6 жыл бұрын
Awesome channel. Wondering what people use to draw this kind of schematic diagram? Visio with some specific stencils for flip flops, registers,... ? Inkscape? Thanks in advance
@Nandland
@Nandland 6 жыл бұрын
I used visio to draw some of the basic elements. I made my own shapes, I didn't find any stencils that I liked. There's probably a better solution though.
@adumont
@adumont 6 жыл бұрын
nandland thanks. Are your shapes /designs online (as vsdx file) by chance?
@Nandland
@Nandland 6 жыл бұрын
Hm, no, maybe I could add them somewhere.
@AshishPatel-vy7mn
@AshishPatel-vy7mn 5 жыл бұрын
If I have corrected the timing errors for setup and hold time on first flip flop. Why do we still require second flip fllop? If setup and hold aren't violating at first flip, we won't face metastable problem. I know double flip flop synchronizer is standard way to synchronize a pulse signal. Can you tell me if I m correct or not?
@Nandland
@Nandland 5 жыл бұрын
Are you crossing clock domains? Are the clocks in-phase or out of phase? If they're out of phase, you can't guarantee that the timing relationships will be maintained. The clocks will drift in time from each other.
@Victor-Zaporotskiy
@Victor-Zaporotskiy 3 жыл бұрын
10:35 It's incorrect to directly pass a combinatorial w_Stretched signal into the i_Clk_25MHz domain, because this signal will exhibit glitchs on every r_Counter transition. First you should register this signal by i_Clk_100MHz to make it clean, free of glitches, and then pass this clean version of the signal into the i_Clk_25MHz process.
@brad8122
@brad8122 Жыл бұрын
So another double flop then? process(100MHz) registerIT
@Victor-Zaporotskiy
@Victor-Zaporotskiy Жыл бұрын
@@brad8122 a single flop on the w_Stretched signal in 100 MHz domain would be exactly what is needed. like this: process(i_Clk_100MHz) begin if rising_edge(i_Clk_100MHz) then w_Stretched_1
@user-tt8oh2ob1q
@user-tt8oh2ob1q Жыл бұрын
What if the source and destination clock frequencies are the same but the phase relationship is unknown?
@halilertugrul2005
@halilertugrul2005 4 жыл бұрын
Thank you for the video but I do not think that double buffering fixes the metastability issues for bus signals. It should be FIFOs advised for bus interfaces.
@matthiaswachter1450
@matthiaswachter1450 3 жыл бұрын
It fixes metastability but it adds the issue of consistency. That’s why the shown approach with double-sampling the std_logic_vector is a no-solution. I wish him happy debugging of bugs introduced like that!
@maheshjuja4622
@maheshjuja4622 5 жыл бұрын
Can you please explain the worst case scenario for why you used rcount as 8? Can't we use anything greater than 4clock cycles of 100MHz clk?
@MilanNedic94
@MilanNedic94 4 жыл бұрын
Jump to, let's say 8:46, pause and watch closely stretched 100MHz clock domain pulse. You see that pulse's rising edge is at the second rising edge of the 100MHz clock (on this diagram). Now, count rising edges of that 100MHz clock, starting from the edge at which pulse edge is there as well. You see if you make the counter count to 4 pulse gets low somewhere on the middle of logic one of the second 25MHz cycle -- not going to catch the pulse in slow domain! If you make the counter count to 5, pulse goes low on falling edge of second cycle in 25MHz domain. Similar goes for 6 and 7, pulse isn't high on rising oedge in the slow domain. Only 8 cycles long pulse in 100MHz will stretch over 2 cycles in 25MHz and thats enough to catch the pulse at least once. I hope I was clear enough.
@matthiaswachter1450
@matthiaswachter1450 3 жыл бұрын
This video is a good start but gets quite some things wrong. 1. Don’t (just) double-sample bus signals, like the std_logic_vector. Either use a proper FIFO or transfer its change by a single wire 2. Use toggle indication instead of transferring pulses. This avoids pulse stretching and allows a higher event indication rate before going full cycle (request-acknowledge) handshake 3. Consider a full-cycle request-acknowledge handshake (with toggle indication). This way the sending clock domain can feed back the event transfer to the sender 4. Only transfer a signal to the other clock domain that is output of a stable flip-flop. No logic, e.g., w_Stretched 5. Always consider the rate of events, not just with a FIFO, with the actual clock rates. Can you afford losing an event? Do you need to count them while the CDC transfer is busy? 6. Keeping setup/hold is not just fighting metastability but ensuring the FF does all of its jobs correctly. The actual window of a FF for entering MS is typically just a few femtoseconds wide, but you don’t know which part of the setup/hold margin 7. Proper RTL design is necessary but don’t forget constraints! Especially ensure that the second sampling stage r_Stable is never split into multiple registers 8. FFs spent properly on CC are well invested. Always.
@varunsac1017
@varunsac1017 Жыл бұрын
where to read up on this stuff?
@jeremigendron3120
@jeremigendron3120 5 жыл бұрын
10:55 is there a latch for the 100MHz block (r_Count = 0)?
@Nandland
@Nandland 5 жыл бұрын
You will never get a latch generated in a clocked (sequential) process. Since it's in a clocked process, the tools will push this logic into a Flip-Flop, not a latch. This is one reason why I like to use clocked processes as much as possible. (Avoiding latches, latches are evil)
@michellenicholes2087
@michellenicholes2087 2 жыл бұрын
If I wanted to test bench this code, where would I get it?
@spoorthim.n.2537
@spoorthim.n.2537 6 жыл бұрын
Can you make a video about how to add timing constraint to a design sometime soon please?
@Nandland
@Nandland 6 жыл бұрын
Good suggestion! Added to my to do list.
@prasannan2084
@prasannan2084 Жыл бұрын
if sending and receiving frequency are equal and different phase shift means?
@WoWPro71
@WoWPro71 Жыл бұрын
In fast to slow example, from where we receive i_pulse_fast?
@4explore
@4explore 6 ай бұрын
Stretching 8 time would be overkilled? I think (Fast/Slow)+1 would be good enough.
@bharatagarwal6330
@bharatagarwal6330 6 жыл бұрын
can you please provide codes in verilog language
@douggale5962
@douggale5962 6 жыл бұрын
You didn't seem to mention the most obvious solution to handling data going from a 50MHz domain to a 33MHz domain: simply make pairs of data from the 50MHz side and enqueue a double-width item at every odd clock. The 33MHz side will be fetching 2x width, effectively 66MHz worth of data.
@Nandland
@Nandland 6 жыл бұрын
Hm... can you elaborate a bit more on this? I'm not sure I completely understand. Is the pair of data sending the same data on 2 clock cycles in a row or are you expanding the number of data paths? Also how does this handle the fact that the two clocks will "walk" in time among eachother?
@douggale5962
@douggale5962 6 жыл бұрын
For example, if the 50MHz side is putting a 4-bit word in the FIFO every cycle, then the 33MHz side can't keep up if it reads 4 bits per cycle from the FIFO. Instead, the 50MHz side could hold even words in a holding register, and every odd cycle it could take that registered word, combine it with the current word, and insert an 8-bit value into the FIFO. The insert rate on the 50MHz side would be 25MHz.
@Nandland
@Nandland 6 жыл бұрын
Yes this is a good point. Thanks for bringing it to my attention. I usually don't think to change the width of the data but there's no reason that you couldn't do that!
@Wren6991
@Wren6991 6 жыл бұрын
Doug Gale this works, but the sticky part of clock domain crossing is phase relationship, not frequency relationship. This still leaves you with some problems to solve :)
@bhanusashankreddy5013
@bhanusashankreddy5013 4 жыл бұрын
is Asynchronous counter.....Synchronous sequential circuit??? No one ever answered me this question.......
What is a Clock in an FPGA?
18:58
nandland
Рет қаралды 52 М.
Scary Teacher 3D Nick Troll Squid Game in Brush Teeth White or Black Challenge #shorts
00:47
1 or 2?🐄
00:12
Kan Andrey
Рет қаралды 58 МЛН
- А что в креме? - Это кАкАооо! #КондитерДети
00:24
Телеканал ПЯТНИЦА
Рет қаралды 7 МЛН
What is a FIFO in an FPGA
17:47
nandland
Рет қаралды 72 М.
FPGA Timing Optimization: Optimization Strategies
42:39
Greg Stitt
Рет қаралды 8 М.
EEVblog #496 - What Is An FPGA?
37:44
EEVblog
Рет қаралды 758 М.
LDC23 - FPGA Timing Constraints Deep Dive
1:04:19
Lattice Semiconductor
Рет қаралды 671
⨘ } VLSI } 4 } Clock Domain Crossing (CDC) Techniques } LE PROFESSEUR }
26:24
How the Best Hackers Learn Their Craft
42:46
RSA Conference
Рет қаралды 2,5 МЛН
FPGA Timing Optimization: Background and Challenges
22:53
Greg Stitt
Рет қаралды 4,4 М.
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 351 М.
Собери ПК и Получи 10,000₽
1:00
build monsters
Рет қаралды 2,7 МЛН
КРУТОЙ ТЕЛЕФОН
0:16
KINO KAIF
Рет қаралды 5 МЛН
Easy Art with AR Drawing App - Step by step for Beginners
0:27
Melli Art School
Рет қаралды 14 МЛН
Samsung Galaxy Unpacked July 2024: Official Replay
1:8:53
Samsung
Рет қаралды 23 МЛН