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