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