]> sigrok.org Git - libsigrokdecode.git/blame - scripts/i2c.py
Various small decoder script fixes.
[libsigrokdecode.git] / scripts / i2c.py
CommitLineData
0588ed70
UH
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21#
22# I2C protocol decoder
23#
24
25#
26# The Inter-Integrated Circuit (I2C) bus is a bidirectional, multi-master
27# bus using two signals (SCL = serial clock line, SDA = serial data line).
28#
29# There can be many devices on the same bus. Each device can potentially be
30# master or slave (and that can change during runtime). Both slave and master
31# can potentially play the transmitter or receiver role (this can also
32# change at runtime).
33#
34# Possible maximum data rates:
35# - Standard mode: 100 kbit/s
36# - Fast mode: 400 kbit/s
37# - Fast-mode Plus: 1 Mbit/s
38# - High-speed mode: 3.4 Mbit/s
39#
40# START condition (S): SDA = falling, SCL = high
41# Repeated START condition (Sr): same as S
42# STOP condition (P): SDA = rising, SCL = high
43#
33e72c54 44# All data bytes on SDA are exactly 8 bits long (transmitted MSB-first).
0588ed70
UH
45# Each byte has to be followed by a 9th ACK/NACK bit. If that bit is low,
46# that indicates an ACK, if it's high that indicates a NACK.
47#
48# After the first START condition, a master sends the device address of the
49# slave it wants to talk to. Slave addresses are 7 bits long (MSB-first).
33e72c54 50# After those 7 bits, a data direction bit is sent. If the bit is low that
0588ed70
UH
51# indicates a WRITE operation, if it's high that indicates a READ operation.
52#
53# Later an optional 10bit slave addressing scheme was added.
54#
55# Documentation:
56# http://www.nxp.com/acrobat/literature/9398/39340011.pdf (v2.1 spec)
57# http://www.nxp.com/acrobat/usermanuals/UM10204_3.pdf (v3 spec)
58# http://en.wikipedia.org/wiki/I2C
59#
60
61# TODO: Look into arbitration, collision detection, clock synchronisation, etc.
62# TODO: Handle clock stretching.
63# TODO: Handle combined messages / repeated START.
64# TODO: Implement support for 7bit and 10bit slave addresses.
65# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
66# TODO: Implement support for detecting various bus errors.
67
5c55017c 68def decode(inbuf):
0588ed70
UH
69 """I2C protocol decoder"""
70
a156f09e
UH
71 # FIXME: Get the data in the correct format in the first place.
72 inbuf = [ord(x) for x in inbuf]
73
0588ed70
UH
74 # FIXME: This should be passed in as metadata, not hardcoded here.
75 signals = (2, 5)
76 channels = 8
77
78 o = wr = ack = d = ''
79 bitcount = data = 0
33e72c54
UH
80 IDLE, START, ADDRESS, DATA = range(4)
81 state = IDLE
0588ed70
UH
82
83 # Get the bit number (and thus probe index) of the SCL/SDA signals.
84 scl_bit, sda_bit = signals
85
86 # Get SCL/SDA bit values (0/1 for low/high) of the first sample.
a156f09e 87 s = inbuf[0]
930bd9d9
UH
88 oldscl = (s & (1 << scl_bit)) >> scl_bit
89 oldsda = (s & (1 << sda_bit)) >> sda_bit
0588ed70
UH
90
91 # Loop over all samples.
92 # TODO: Handle LAs with more/less than 8 channels.
93 for samplenum, s in enumerate(inbuf[1:]): # We skip the first byte...
0588ed70 94 # Get SCL/SDA bit values (0/1 for low/high).
930bd9d9
UH
95 scl = (s & (1 << scl_bit)) >> scl_bit
96 sda = (s & (1 << sda_bit)) >> sda_bit
0588ed70
UH
97
98 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
99
100 # START condition (S): SDA = falling, SCL = high
101 if (oldsda == 1 and sda == 0) and scl == 1:
102 o += "%d\t\tSTART\n" % samplenum
33e72c54 103 state = ADDRESS
0588ed70
UH
104 bitcount = data = 0
105
106 # Data latching by transmitter: SCL = low
107 elif (scl == 0):
108 pass # TODO
109
110 # Data sampling of receiver: SCL = rising
111 elif (oldscl == 0 and scl == 1):
112 bitcount += 1
113
114 # o += "%d\t\tRECEIVED BIT %d: %d\n" % \
115 # (samplenum, 8 - bitcount, sda)
116
117 # Address and data are transmitted MSB-first.
118 data <<= 1
119 data |= sda
120
121 if bitcount != 9:
122 continue
123
124 # We received 8 address/data bits and the ACK/NACK bit.
125 data >>= 1 # Shift out unwanted ACK/NACK bit here.
33e72c54
UH
126 # o += "%d\t\t%s: " % (samplenum, state)
127 o += "%d\t\tTODO:STATE: " % samplenum
0588ed70 128 ack = (sda == 1) and 'NACK' or 'ACK'
33e72c54 129 d = (state == ADDRESS) and (data & 0xfe) or data
0588ed70 130 wr = ''
33e72c54 131 if state == ADDRESS:
0588ed70 132 wr = (data & 1) and ' (W)' or ' (R)'
33e72c54 133 state = DATA
0588ed70
UH
134 o += "0x%02x%s (%s)\n" % (d, wr, ack)
135 bitcount = data = 0
136
137 # STOP condition (P): SDA = rising, SCL = high
138 elif (oldsda == 0 and sda == 1) and scl == 1:
139 o += "%d\t\tSTOP\n" % samplenum
33e72c54 140 state = IDLE
0588ed70
UH
141
142 # Save current SDA/SCL values for the next round.
143 oldscl = scl
144 oldsda = sda
145
146 return o
147
23a13b21 148def register():
149 return {
0588ed70
UH
150 'id': 'i2c',
151 'name': 'I2C',
5c55017c 152 'desc': 'Inter-Integrated Circuit (I2C) bus',
0588ed70
UH
153 'inputformats': ['raw'],
154 'signalnames': {
155 'SCL': 'Serial clock line',
156 'SDA': 'Serial data line',
157 },
a156f09e 158 'outputformats': ['i2c'],
0588ed70 159 }
0588ed70 160
887d6cfa
UH
161# Use psyco (if available) as it results in huge performance improvements.
162try:
163 import psyco
5c55017c 164 psyco.bind(decode)
887d6cfa
UH
165except ImportError:
166 pass
167