4 from tmc.dxplore import dxplore
5 from tmc.decode import d_usb_stream
9 # Clock recovery: we assume that each change in the wave is triggered by a
10 # clock edge. We know the clock's nominal period and resynchronize on each
11 # edge. Additionally, we can obtain a list of times when a timing violation
14 # Note that the timing violations logic doesn't make much sense in its present
15 # form, since it mainly measures noise (particularly if we're digitizing slow
16 # edges) and not clock drift.
18 # A more useful metric would be accumulated error from some point of reference
19 # or at least the timing of same edges, to eliminate (generally harmless) time
20 # offsets introduced by digitizing.
22 # So it would probably make more sense for "recover" not to check for timing
23 # violations at all, and leave this to more specialized functions.
25 def recover(self, period, min = None, max = None, t0 = None):
36 while t0 < t-period/2:
47 return res, violations
51 # Load the analog waves saved by get.py
57 # Digitize the waves and save the result.
59 dp = wv[0].digitize(1.5, 1.8)
60 dm = wv[1].digitize(1.5, 1.8)
61 wv = waves(dp, dm, dp-dm)
65 # Also record the differential signal.
68 dd = wd.digitize(-0.5, 0.5)
72 # Run clock recovery on D+/D-. We only need one, but check both to be sure.
76 dp_t, viol = recover(dp, p, p*0.9, p*1.1)
78 dm_t, viol = recover(dm, p, p*.9, p*1.1, t0 = dp.data[0])
82 # Shift the clock by half a period, add a few periods to get steady state and
83 # SE0s (if any), and then sample the data lines.
85 clk = map(lambda t: t+p/2, dp_t)
86 clk.extend((clk[-1]+p, clk[-1]+2*p, clk[-1]+3*p))
91 # Save a wave with the recovered clock to make it easier to find the bits in
98 # For decoding, we need a fake bit clock. We generate it by doubling each data
99 # bit and generating a L->H transition during this bit.
105 # err, silly, seems that we've mixed up D+ and D- all over the place :-)
106 print d_usb_stream(dm_bv[:], dp_bv[:])
119 # Display the reconstructed digital signal. Note that the absolute time is only
120 # correct at the beginning and that relative time is only accurate over
121 # intervals in which no significant clock resynchronization has occurred.
123 # In fact, dxplore should probably have an option to either turn off time
124 # entirely or to display a user-provided time axis. The latter may be a bit
125 # tricky to implement.
127 dxplore((dmd, dpd, dck), 0, p/2, labels = ("D+", "D-", "CLK"))