X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fi2c%2Fi2c.py;h=270e0f3a9f3faeaf1bb041630dbe1a5a32907f97;hp=f0a95b79fd334b1dc6d9e2ca875b0dee988694fe;hb=c49750787d045582dc08419b1312770a57fb845f;hpb=64c29e28e0efa184319f7831b3eca18c7f73f7d0 diff --git a/decoders/i2c/i2c.py b/decoders/i2c/i2c.py index f0a95b7..270e0f3 100644 --- a/decoders/i2c/i2c.py +++ b/decoders/i2c/i2c.py @@ -116,6 +116,7 @@ FIND_ADDRESS = 1 FIND_DATA = 2 class Decoder(srd.Decoder): + api_version = 1 id = 'i2c' name = 'I2C' longname = 'Inter-Integrated Circuit' @@ -143,11 +144,11 @@ class Decoder(srd.Decoder): ] def __init__(self, **kwargs): - self.samplecnt = 0 + self.startsample = -1 + self.samplenum = None self.bitcount = 0 self.databyte = 0 self.wr = -1 - self.startsample = -1 self.is_repeat_start = 0 self.state = FIND_START self.oldscl = None @@ -160,9 +161,6 @@ class Decoder(srd.Decoder): self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c') self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c') - def report(self): - pass - def is_start_condition(self, scl, sda): # START condition (S): SDA = falling, SCL = high if (self.oldsda == 1 and sda == 0) and scl == 1: @@ -182,8 +180,9 @@ class Decoder(srd.Decoder): return False def found_start(self, scl, sda): - cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START' + self.startsample = self.samplenum + cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START' self.put(self.out_proto, [cmd, None, None]) self.put(self.out_ann, [ANN_SHIFTED, [protocol[cmd][0]]]) self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol[cmd][1]]]) @@ -193,19 +192,17 @@ class Decoder(srd.Decoder): self.is_repeat_start = 1 self.wr = -1 + # Gather 8 bits of data plus the ACK/NACK bit. def found_address_or_data(self, scl, sda): - # Gather 8 bits of data plus the ACK/NACK bit. - - if self.startsample == -1: - # TODO: Should be samplenum, as received from the feed. - self.startsample = self.samplecnt - self.bitcount += 1 - # Address and data are transmitted MSB-first. self.databyte <<= 1 self.databyte |= sda + if self.bitcount == 0: + self.startsample = self.samplenum + # Return if we haven't collected all 8 + 1 bits, yet. + self.bitcount += 1 if self.bitcount != 9: return @@ -255,6 +252,8 @@ class Decoder(srd.Decoder): pass def found_stop(self, scl, sda): + self.startsample = self.samplenum + self.put(self.out_proto, ['STOP', None, None]) self.put(self.out_ann, [ANN_SHIFTED, [protocol['STOP'][0]]]) self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol['STOP'][1]]]) @@ -265,12 +264,11 @@ class Decoder(srd.Decoder): def put(self, output_id, data): # Inject sample range into the call up to sigrok. - # TODO: 0-0 sample range for now. - super(Decoder, self).put(0, 0, output_id, data) + super(Decoder, self).put(self.startsample, self.samplenum, output_id, data) def decode(self, ss, es, data): for samplenum, (scl, sda) in data: - self.samplecnt += 1 + self.samplenum = samplenum # First sample: Save SCL/SDA value. if self.oldscl == None: @@ -295,8 +293,8 @@ class Decoder(srd.Decoder): elif self.is_stop_condition(scl, sda): self.found_stop(scl, sda) else: - # TODO: Error? - pass + # Shouldn't happen. + raise Exception("unknown state %d" % self.STATE) # Save current SDA/SCL values for the next round. self.oldscl = scl