]> sigrok.org Git - libsigrokdecode.git/commitdiff
timing: add edge-edge mode and delta calculation
authorKarl Palsson <redacted>
Fri, 20 Jan 2017 16:24:29 +0000 (16:24 +0000)
committerUwe Hermann <redacted>
Sat, 13 May 2017 14:11:27 +0000 (16:11 +0200)
Instead of only measuring the timing from any edge to any edge, provide
a mode to measure the times from falling->falling and rising->rising
edges. This works better to monitor timing of an IRQ pin for instance,
that drives one direction under hardware, and is only cleared by
software reading a value.

Include time delta measurements as an optional annotation to help track
jitter in measurements.

Signed-off-by: Karl Palsson <redacted>
decoders/timing/pd.py

index 01e3b016f5bb5c9e6654b3a23b11ca1da0bfb697..b694c1ddfdb75d53407d08d132355cd9bfc877cb 100644 (file)
@@ -60,13 +60,17 @@ class Decoder(srd.Decoder):
     annotations = (
         ('time', 'Time'),
         ('average', 'Average'),
+        ('delta', 'Delta'),
     )
     annotation_rows = (
         ('time', 'Time', (0,)),
         ('average', 'Average', (1,)),
+        ('delta', 'Delta', (2,)),
     )
     options = (
         { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
+        { 'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling') },
+        { 'id': 'delta', 'desc': 'Show delta from last', 'default': 'no', 'values': ('yes', 'no') },
     )
 
     def __init__(self):
@@ -75,6 +79,8 @@ class Decoder(srd.Decoder):
         self.last_samplenum = None
         self.last_n = deque()
         self.chunks = 0
+        self.level_changed = False
+        self.last_t = None
 
     def metadata(self, key, value):
         if key == srd.SRD_CONF_SAMPLERATE:
@@ -82,44 +88,39 @@ class Decoder(srd.Decoder):
 
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
+        self.edge = self.options['edge']
         self.initial_pins = [0]
 
     def decode(self):
         if not self.samplerate:
             raise SamplerateError('Cannot decode without samplerate.')
         while True:
-            pin = self.wait({0: 'e'})
+            if self.edge == 'rising':
+                pin = self.wait({0: 'r'})
+            elif self.edge == 'falling':
+                pin = self.wait({0: 'f'})
+            else:
+                pin = self.wait({0: 'e'})
 
-            if self.oldpin is None:
-                self.oldpin = pin
+            if not self.last_samplenum:
                 self.last_samplenum = self.samplenum
                 continue
+            samples = self.samplenum - self.last_samplenum
+            t = samples / self.samplerate
 
-            if self.oldpin != pin:
-                samples = self.samplenum - self.last_samplenum
-                t = samples / self.samplerate
-                self.chunks += 1
+            if t > 0:
+                self.last_n.append(t)
+            if len(self.last_n) > self.options['avg_period']:
+                self.last_n.popleft()
 
-                # Don't insert the first chunk into the averaging as it is
-                # not complete probably.
-                if self.last_samplenum is None or self.chunks < 2:
-                    # Report the timing normalized.
-                    self.put(self.last_samplenum, self.samplenum, self.out_ann,
-                             [0, [normalize_time(t)]])
-                else:
-                    if t > 0:
-                        self.last_n.append(t)
+            self.put(self.last_samplenum, self.samplenum, self.out_ann,
+                     [0, [normalize_time(t)]])
+            if self.options['avg_period'] > 0:
+                self.put(self.last_samplenum, self.samplenum, self.out_ann,
+                         [1, [normalize_time(sum(self.last_n) / len(self.last_n))]])
+            if self.last_t and self.options['delta'] == 'yes':
+                self.put(self.last_samplenum, self.samplenum, self.out_ann,
+                         [2, [normalize_time(t - self.last_t)]])
 
-                    if len(self.last_n) > self.options['avg_period']:
-                        self.last_n.popleft()
-
-                    # Report the timing normalized.
-                    self.put(self.last_samplenum, self.samplenum, self.out_ann,
-                             [0, [normalize_time(t)]])
-                    if self.options['avg_period'] > 0:
-                        self.put(self.last_samplenum, self.samplenum, self.out_ann,
-                                 [1, [normalize_time(sum(self.last_n) / len(self.last_n))]])
-
-                # Store data for next round.
-                self.last_samplenum = self.samplenum
-                self.oldpin = pin
+            self.last_t = t
+            self.last_samplenum = self.samplenum