Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. from migen.fhdl.std import *
  2. from migen.fhdl import verilog
  3.  
  4.  
  5. class FreqCounter(Module):
  6. # measured_sig: Signal to measure frequency
  7. # ref_clk: counts up to a gate_time number of cycles. It should be at least
  8. # twice as fast as the maximum frequency of the measured signal to avoid
  9. # samples being missed.
  10. # storage: should be an already instantiated (a)synchronous FIFO
  11. # posedge: If true, edge transitions from low to high count as a frequency event.
  12. # Otherwise, high to low transitions count.
  13. def __init__(self, measured_sig, ref_clk, gate_time=50000000, storage, posedge=True):
  14. self.clock_domains.cd_ref_clk = ClockDomain()
  15.  
  16. event = Signal(1)
  17. prev_sig = Signal(1)
  18. gate_count = Signal(max=gate_time)
  19. freq_count = Signal(max=gate_time)
  20. storage_strobe = Signal(1)
  21.  
  22. self.sync.ref_clk += [prev_sig.eq(measured_sig)]
  23.  
  24. if posedge:
  25. self.comb += [event.eq(measured_sig & ~prev_sig)]
  26. else:
  27. self.comb += [event.eq(~measured_sig & prev_sig)]
  28.  
  29. self.sync.ref_clk += [storage_strobe.eq(0),
  30. If(gate_count == gate_time - 1,
  31. gate_count.eq(0), storage_strobe.eq(1),
  32. freq_count.eq(0)).
  33. Else(
  34. # TODO: We should probably count an edge transition detected
  35. # on the leading edge just before the gate_count resets.
  36. If(event,
  37. freq_count.eq(freq_count + 1))
  38. )]
  39.  
  40. # print(verilog.convert(Fre
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement