X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fi2c%2Fpd.py;h=4d77920842e319de98593f2f1eab55532aec608b;hp=2500ae4cc6e036a7620a1dd5e2c0c830a3b05af7;hb=c515eed7ef7a04a42b5b34abd308e08d6942835e;hpb=d94ff143ef7725388299ef9bd3be7ebc60e0dd2e diff --git a/decoders/i2c/pd.py b/decoders/i2c/pd.py index 2500ae4..4d77920 100644 --- a/decoders/i2c/pd.py +++ b/decoders/i2c/pd.py @@ -18,20 +18,36 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## -# I2C protocol decoder - # TODO: Look into arbitration, collision detection, clock synchronisation, etc. -# TODO: Handle clock stretching. -# TODO: Handle combined messages / repeated START. -# TODO: Implement support for 7bit and 10bit slave addresses. +# TODO: Implement support for 10bit slave addresses. # TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0). # TODO: Implement support for detecting various bus errors. -# TODO: I2C address of slaves. -# TODO: Handle multiple different I2C devices on same bus -# -> we need to decode multiple protocols at the same time. import sigrokdecode as srd +''' +OUTPUT_PYTHON format: + +I²C packet: +[, ] + + is one of: + - 'START' (START condition) + - 'START REPEAT' (Repeated START condition) + - 'ADDRESS READ' (Slave address, read) + - 'ADDRESS WRITE' (Slave address, write) + - 'DATA READ' (Data, read) + - 'DATA WRITE' (Data, write) + - 'STOP' (STOP condition) + - 'ACK' (ACK bit) + - 'NACK' (NACK bit) + + is the data or address byte associated with the 'ADDRESS*' and 'DATA*' +command. Slave addresses do not include bit 0 (the READ/WRITE indication bit). +For example, a slave address field could be 0x51 (instead of 0xa2). +For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' is None. +''' + # CMD: [annotation-type-index, long annotation, short annotation] proto = { 'START': [0, 'Start', 'S'], @@ -48,7 +64,7 @@ proto = { class Decoder(srd.Decoder): api_version = 1 id = 'i2c' - name = 'I2C' + name = 'I²C' longname = 'Inter-Integrated Circuit' desc = 'Two-wire, multi-master, serial bus.' license = 'gplv2+' @@ -60,23 +76,29 @@ class Decoder(srd.Decoder): ] optional_probes = [] options = { - 'addressing': ['Slave addressing (in bits)', 7], # 7 or 10 'address_format': ['Displayed slave address format', 'shifted'], } annotations = [ - ['Start', 'Start condition'], - ['Repeat start', 'Repeat start condition'], - ['Stop', 'Stop condition'], - ['ACK', 'ACK'], - ['NACK', 'NACK'], - ['Address read', 'Address read'], - ['Address write', 'Address write'], - ['Data read', 'Data read'], - ['Data write', 'Data write'], - ['Warnings', 'Human-readable warnings'], + ['start', 'Start condition'], + ['repeat-start', 'Repeat start condition'], + ['stop', 'Stop condition'], + ['ack', 'ACK'], + ['nack', 'NACK'], + ['address-read', 'Address read'], + ['address-write', 'Address write'], + ['data-read', 'Data read'], + ['data-write', 'Data write'], + ['warnings', 'Human-readable warnings'], ] + binary = ( + ('address-read', 'Address read'), + ('address-write', 'Address write'), + ('data-read', 'Data read'), + ('data-write', 'Data write'), + ) def __init__(self, **kwargs): + self.samplerate = None self.startsample = -1 self.samplenum = None self.bitcount = 0 @@ -87,19 +109,28 @@ class Decoder(srd.Decoder): self.oldscl = 1 self.oldsda = 1 self.oldpins = [1, 1] + self.pdu_start = None + self.pdu_bits = 0 - def start(self, metadata): - self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c') - self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c') + def metadata(self, key, value): + if key == srd.SRD_CONF_SAMPLERATE: + self.samplerate = value - def report(self): - pass + def start(self): + self.out_python = self.register(srd.OUTPUT_PYTHON) + self.out_ann = self.register(srd.OUTPUT_ANN) + self.out_binary = self.register(srd.OUTPUT_BINARY) + self.out_bitrate = self.register(srd.OUTPUT_META, + meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit')) def putx(self, data): self.put(self.startsample, self.samplenum, self.out_ann, data) def putp(self, data): - self.put(self.startsample, self.samplenum, self.out_proto, data) + self.put(self.startsample, self.samplenum, self.out_python, data) + + def putb(self, data): + self.put(self.startsample, self.samplenum, self.out_binary, data) def is_start_condition(self, scl, sda): # START condition (S): SDA = falling, SCL = high @@ -121,6 +152,8 @@ class Decoder(srd.Decoder): def found_start(self, scl, sda): self.startsample = self.samplenum + self.pdu_start = self.samplenum + self.pdu_bits = 0 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START' self.putp([cmd, None]) self.putx([proto[cmd][0], proto[cmd][1:]]) @@ -153,18 +186,24 @@ class Decoder(srd.Decoder): if self.options['address_format'] == 'shifted': d = d >> 1 + bin_class = -1 if self.state == 'FIND ADDRESS' and self.wr == 1: cmd = 'ADDRESS WRITE' + bin_class = 1 elif self.state == 'FIND ADDRESS' and self.wr == 0: cmd = 'ADDRESS READ' + bin_class = 0 elif self.state == 'FIND DATA' and self.wr == 1: cmd = 'DATA WRITE' + bin_class = 3 elif self.state == 'FIND DATA' and self.wr == 0: cmd = 'DATA READ' + bin_class = 2 self.putp([cmd, d]) self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d), '%s: %02X' % (proto[cmd][2], d), '%02X' % d]]) + self.putb((bin_class, bytes([d]))) # Done with this packet. self.startsample = -1 @@ -181,6 +220,11 @@ class Decoder(srd.Decoder): self.state = 'FIND DATA' def found_stop(self, scl, sda): + # Meta bitrate + elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1) + bitrate = int(1 / elapsed * self.pdu_bits) + self.put(self.startsample, self.samplenum, self.out_bitrate, bitrate) + self.startsample = self.samplenum cmd = 'STOP' self.putp([cmd, None]) @@ -190,6 +234,8 @@ class Decoder(srd.Decoder): self.wr = -1 def decode(self, ss, es, data): + if self.samplerate is None: + raise Exception("Cannot decode without samplerate.") for (self.samplenum, pins) in data: # Ignore identical samples early on (for performance reasons). @@ -197,6 +243,8 @@ class Decoder(srd.Decoder): continue self.oldpins, (scl, sda) = pins, pins + self.pdu_bits += 1 + # TODO: Wait until the bus is idle (SDA = SCL = 1) first? # State machine.