]> sigrok.org Git - libsigrokdecode.git/blame - decoders/mxc6225xu/pd.py
s/out_proto/out_python/.
[libsigrokdecode.git] / decoders / mxc6225xu / pd.py
CommitLineData
d997c01a 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
d997c01a
UH
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
d997c01a
UH
21import sigrokdecode as srd
22
17160de7 23# Definitions of various bits in MXC6225XU registers.
d997c01a
UH
24status = {
25 # SH[1:0]
26 'sh': {
17160de7 27 0b00: 'none',
d997c01a
UH
28 0b01: 'shake left',
29 0b10: 'shake right',
30 0b11: 'undefined',
31 },
32 # ORI[1:0] and OR[1:0] (same format)
33 'ori': {
34 0b00: 'vertical in upright orientation',
35 0b01: 'rotated 90 degrees clockwise',
36 0b10: 'vertical in inverted orientation',
37 0b11: 'rotated 90 degrees counterclockwise',
38 },
17160de7
UH
39 # SHTH[1:0]
40 'shth': {
41 0b00: '0.5g',
42 0b01: '1.0g',
43 0b10: '1.5g',
44 0b11: '2.0g',
45 },
46 # SHC[1:0]
47 'shc': {
48 0b00: '16',
49 0b01: '32',
50 0b10: '64',
51 0b11: '128',
52 },
53 # ORC[1:0]
54 'orc': {
55 0b00: '16',
56 0b01: '32',
57 0b10: '64',
58 0b11: '128',
59 },
d997c01a
UH
60}
61
62class Decoder(srd.Decoder):
63 api_version = 1
64 id = 'mxc6225xu'
65 name = 'MXC6225XU'
66 longname = 'MEMSIC MXC6225XU'
a465436e 67 desc = 'Digital Thermal Orientation Sensor (DTOS) protocol.'
d997c01a
UH
68 license = 'gplv2+'
69 inputs = ['i2c']
70 outputs = ['mxc6225xu']
71 probes = []
72 optional_probes = [
73 {'id': 'int', 'name': 'INT', 'desc': 'DTOS interrupt output pin'},
74 ]
75 options = {}
76 annotations = [
9f2f42c0 77 ['text', 'Human-readable text'],
d997c01a
UH
78 ]
79
80 def __init__(self, **kwargs):
81 self.state = 'IDLE'
82
8915b346 83 def start(self):
c515eed7 84 # self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 85 self.out_ann = self.register(srd.OUTPUT_ANN)
d997c01a 86
d997c01a
UH
87 def putx(self, data):
88 self.put(self.ss, self.es, self.out_ann, data)
89
90 def handle_reg_0x00(self, b):
91 # XOUT: 8-bit x-axis acceleration output.
92 # Data is in 2's complement, values range from -128 to 127.
17160de7 93 self.putx([0, ['XOUT: %d' % b]])
d997c01a
UH
94
95 def handle_reg_0x01(self, b):
96 # YOUT: 8-bit y-axis acceleration output.
97 # Data is in 2's complement, values range from -128 to 127.
17160de7 98 self.putx([0, ['YOUT: %d' % b]])
d997c01a
UH
99
100 def handle_reg_0x02(self, b):
101 # STATUS: Orientation and shake status.
102
17160de7 103 # Bits[7:7]: INT
d997c01a
UH
104 int_val = (b >> 7) & 1
105 s = 'unchanged and no' if (int_val == 0) else 'changed or'
106 ann = 'INT = %d: Orientation %s shake event occured\n' % (int_val, s)
107
108 # Bits[6:5]: SH[1:0]
109 sh = (((b >> 6) & 1) << 1) | ((b >> 5) & 1)
17160de7
UH
110 ann += 'SH[1:0] = %s: Shake event: %s\n' % \
111 (bin(sh)[2:], status['sh'][sh])
d997c01a
UH
112
113 # Bits[4:4]: TILT
17160de7 114 tilt = (b >> 4) & 1
d997c01a
UH
115 s = '' if (tilt == 0) else 'not '
116 ann += 'TILT = %d: Orientation measurement is %svalid\n' % (tilt, s)
117
118 # Bits[3:2]: ORI[1:0]
119 ori = (((b >> 3) & 1) << 1) | ((b >> 2) & 1)
120 ann += 'ORI[1:0] = %s: %s\n' % (bin(ori)[2:], status['ori'][ori])
121
122 # Bits[1:0]: OR[1:0]
123 or_val = (((b >> 1) & 1) << 1) | ((b >> 0) & 1)
124 ann += 'OR[1:0] = %s: %s\n' % (bin(or_val)[2:], status['ori'][or_val])
125
126 # ann += 'b = %s\n' % (bin(b))
127
128 self.putx([0, [ann]])
129
130 def handle_reg_0x03(self, b):
131 # DETECTION: Powerdown, orientation and shake detection parameters.
132 # Note: This is a write-only register.
133
17160de7 134 # Bits[7:7]: PD
d997c01a 135 pd = (b >> 7) & 1
17160de7
UH
136 s = 'Do not power down' if (pd == 0) else 'Power down'
137 ann = 'PD = %d: %s the device (into a low-power state)\n' % (pd, s)
d997c01a 138
17160de7 139 # Bits[6:6]: SHM
d997c01a 140 shm = (b >> 6) & 1
17160de7
UH
141 ann = 'SHM = %d: Set shake mode to %d\n' % (shm, shm)
142
143 # Bits[5:4]: SHTH[1:0]
144 shth = (((b >> 5) & 1) << 1) | ((b >> 4) & 1)
145 ann += 'SHTH[1:0] = %s: Set shake threshold to %s\n' \
146 % (bin(shth)[2:], status['shth'][shth])
d997c01a 147
17160de7
UH
148 # Bits[3:2]: SHC[1:0]
149 shc = (((b >> 3) & 1) << 1) | ((b >> 2) & 1)
150 ann += 'SHC[1:0] = %s: Set shake count to %s readings\n' \
151 % (bin(shc)[2:], status['shc'][shc])
152
153 # Bits[1:0]: ORC[1:0]
154 orc = (((b >> 1) & 1) << 1) | ((b >> 0) & 1)
155 ann += 'ORC[1:0] = %s: Set orientation count to %s readings\n' \
156 % (bin(orc)[2:], status['orc'][orc])
157
158 self.putx([0, [ann]])
d997c01a
UH
159
160 # TODO: Fixup, this is copy-pasted from another PD.
17160de7 161 # TODO: Handle/check the ACKs/NACKs.
d997c01a
UH
162 def decode(self, ss, es, data):
163 cmd, databyte = data
164
31f1a296 165 # Store the start/end samples of this I²C packet.
d997c01a
UH
166 self.ss, self.es = ss, es
167
168 # State machine.
169 if self.state == 'IDLE':
31f1a296 170 # Wait for an I²C START condition.
d997c01a
UH
171 if cmd != 'START':
172 return
173 self.state = 'GET SLAVE ADDR'
174 self.block_start_sample = ss
175 elif self.state == 'GET SLAVE ADDR':
176 # Wait for an address write operation.
17160de7 177 # TODO: We should only handle packets to the slave(?)
d997c01a
UH
178 if cmd != 'ADDRESS WRITE':
179 return
180 self.state = 'GET REG ADDR'
181 elif self.state == 'GET REG ADDR':
182 # Wait for a data write (master selects the slave register).
183 if cmd != 'DATA WRITE':
184 return
185 self.reg = databyte
17160de7
UH
186 self.state = 'WRITE REGS'
187 elif self.state == 'WRITE REGS':
188 # If we see a Repeated Start here, it's a multi-byte read.
d997c01a 189 if cmd == 'START REPEAT':
17160de7 190 self.state = 'READ REGS'
d997c01a
UH
191 return
192 # Otherwise: Get data bytes until a STOP condition occurs.
193 if cmd == 'DATA WRITE':
194 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
195 handle_reg(databyte)
196 self.reg += 1
197 # TODO: Check for NACK!
198 elif cmd == 'STOP':
199 # TODO
200 self.state = 'IDLE'
201 else:
202 pass # TODO
17160de7 203 elif self.state == 'READ REGS':
d997c01a 204 # Wait for an address read operation.
17160de7 205 # TODO: We should only handle packets to the slave(?)
d997c01a 206 if cmd == 'ADDRESS READ':
17160de7 207 self.state = 'READ REGS2'
d997c01a
UH
208 return
209 else:
210 pass # TODO
17160de7 211 elif self.state == 'READ REGS2':
d997c01a
UH
212 if cmd == 'DATA READ':
213 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
214 handle_reg(databyte)
215 self.reg += 1
216 # TODO: Check for NACK!
217 elif cmd == 'STOP':
218 # TODO
219 self.state = 'IDLE'
220 else:
221 pass # TODO?
222 else:
0eeeb544 223 raise Exception('Invalid state: %s' % self.state)
d997c01a 224