]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ebr30a_i2c_demux/ebr30a_i2c_demux.py
srd: Move all protocol docs to __init__.py files.
[libsigrokdecode.git] / decoders / ebr30a_i2c_demux / ebr30a_i2c_demux.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 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 # TrekStor EBR30-a I2C demux protocol decoder
22
23 import sigrokdecode as srd
24
25 # I2C devices
26 AXP199 = 0
27 H8563S = 1
28 ACCEL = 2
29
30 class Decoder(srd.Decoder):
31     api_version = 1
32     id = 'ebr30a_i2c_demux'
33     name = 'EBR30-a I2C demux'
34     longname = 'TrekStor EBR30-a I2C demux'
35     desc = 'TODO.'
36     longdesc = 'TODO.'
37     license = 'gplv2+'
38     inputs = ['i2c']
39     outputs = ['i2c-axp199', 'i2c-h8563s', 'i2c-accel'] # TODO: type vs. inst.
40     probes = []
41     optional_probes = []
42     options = {}
43     annotations = []
44
45     def __init__(self, **kwargs):
46         self.packets = []
47         self.stream = -1
48
49     def start(self, metadata):
50         self.out_proto = []
51         self.out_proto.append(self.add(srd.OUTPUT_PROTO, 'i2c-axp199'))
52         self.out_proto.append(self.add(srd.OUTPUT_PROTO, 'i2c-h8563s'))
53         self.out_proto.append(self.add(srd.OUTPUT_PROTO, 'i2c-accel'))
54         # TODO: Annotations?
55
56     def report(self):
57         pass
58
59     # Grab I2C packets into a local cache, until an I2C STOP condition
60     # packet comes along. At some point before that STOP condition, there
61     # will have been an ADDRESS READ or ADDRESS WRITE which contains the
62     # I2C address of the slave that the master wants to talk to.
63     # We use this slave address to figure out which output stream should
64     # get the whole chunk of packets (from START to STOP).
65     def decode(self, ss, es, data):
66
67         cmd, databyte, ack_bit = data
68
69         # Add the I2C packet to our local cache.
70         self.packets += [[ss, es, data]]
71
72         if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
73             # print(hex(databyte))
74             if databyte == 0x34:
75                 self.stream = AXP199
76             elif databyte == 0x51:
77                 self.stream = H8563S
78             elif databyte == 0x15:
79                 self.stream = ACCEL
80             else:
81                 pass # TODO: Error?
82             # TODO: Can there be two ADDRESS READ/WRITE with two different
83             #       slave addresses before any STOP occurs?
84         elif cmd == 'STOP':
85             if self.stream != -1:
86                 # Send the whole chunk of I2C packets to the correct stream.
87                 for p in self.packets:
88                     # print(self.out_proto[self.stream], p)
89                     self.put(p[0], p[1], self.out_proto[self.stream], p[2])
90             else:
91                 print('Error: Could not determine correct stream!') # FIXME
92             self.packets = []
93             self.stream = -1
94         else:
95             pass # Do nothing, only add the I2C packet to our cache.
96