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