2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
22 # Definitions of various bits in MXC6225XU registers.
31 # ORI[1:0] and OR[1:0] (same format)
33 0b00: 'vertical in upright orientation',
34 0b01: 'rotated 90 degrees clockwise',
35 0b10: 'vertical in inverted orientation',
36 0b11: 'rotated 90 degrees counterclockwise',
61 class Decoder(srd.Decoder):
65 longname = 'MEMSIC MXC6225XU'
66 desc = 'Digital Thermal Orientation Sensor (DTOS) protocol.'
69 outputs = ['mxc6225xu']
71 ('text', 'Human-readable text'),
78 self.out_ann = self.register(srd.OUTPUT_ANN)
81 self.put(self.ss, self.es, self.out_ann, data)
83 def handle_reg_0x00(self, b):
84 # XOUT: 8-bit x-axis acceleration output.
85 # Data is in 2's complement, values range from -128 to 127.
86 self.putx([0, ['XOUT: %d' % b]])
88 def handle_reg_0x01(self, b):
89 # YOUT: 8-bit y-axis acceleration output.
90 # Data is in 2's complement, values range from -128 to 127.
91 self.putx([0, ['YOUT: %d' % b]])
93 def handle_reg_0x02(self, b):
94 # STATUS: Orientation and shake status.
97 int_val = (b >> 7) & 1
98 s = 'unchanged and no' if (int_val == 0) else 'changed or'
99 ann = 'INT = %d: Orientation %s shake event occurred\n' % (int_val, s)
102 sh = (((b >> 6) & 1) << 1) | ((b >> 5) & 1)
103 ann += 'SH[1:0] = %s: Shake event: %s\n' % \
104 (bin(sh)[2:], status['sh'][sh])
108 s = '' if (tilt == 0) else 'not '
109 ann += 'TILT = %d: Orientation measurement is %svalid\n' % (tilt, s)
111 # Bits[3:2]: ORI[1:0]
112 ori = (((b >> 3) & 1) << 1) | ((b >> 2) & 1)
113 ann += 'ORI[1:0] = %s: %s\n' % (bin(ori)[2:], status['ori'][ori])
116 or_val = (((b >> 1) & 1) << 1) | ((b >> 0) & 1)
117 ann += 'OR[1:0] = %s: %s\n' % (bin(or_val)[2:], status['ori'][or_val])
119 # ann += 'b = %s\n' % (bin(b))
121 self.putx([0, [ann]])
123 def handle_reg_0x03(self, b):
124 # DETECTION: Powerdown, orientation and shake detection parameters.
125 # Note: This is a write-only register.
129 s = 'Do not power down' if (pd == 0) else 'Power down'
130 ann = 'PD = %d: %s the device (into a low-power state)\n' % (pd, s)
134 ann = 'SHM = %d: Set shake mode to %d\n' % (shm, shm)
136 # Bits[5:4]: SHTH[1:0]
137 shth = (((b >> 5) & 1) << 1) | ((b >> 4) & 1)
138 ann += 'SHTH[1:0] = %s: Set shake threshold to %s\n' \
139 % (bin(shth)[2:], status['shth'][shth])
141 # Bits[3:2]: SHC[1:0]
142 shc = (((b >> 3) & 1) << 1) | ((b >> 2) & 1)
143 ann += 'SHC[1:0] = %s: Set shake count to %s readings\n' \
144 % (bin(shc)[2:], status['shc'][shc])
146 # Bits[1:0]: ORC[1:0]
147 orc = (((b >> 1) & 1) << 1) | ((b >> 0) & 1)
148 ann += 'ORC[1:0] = %s: Set orientation count to %s readings\n' \
149 % (bin(orc)[2:], status['orc'][orc])
151 self.putx([0, [ann]])
153 # TODO: Fixup, this is copy-pasted from another PD.
154 # TODO: Handle/check the ACKs/NACKs.
155 def decode(self, ss, es, data):
158 # Store the start/end samples of this I²C packet.
159 self.ss, self.es = ss, es
162 if self.state == 'IDLE':
163 # Wait for an I²C START condition.
166 self.state = 'GET SLAVE ADDR'
167 elif self.state == 'GET SLAVE ADDR':
168 # Wait for an address write operation.
169 # TODO: We should only handle packets to the slave(?)
170 if cmd != 'ADDRESS WRITE':
172 self.state = 'GET REG ADDR'
173 elif self.state == 'GET REG ADDR':
174 # Wait for a data write (master selects the slave register).
175 if cmd != 'DATA WRITE':
178 self.state = 'WRITE REGS'
179 elif self.state == 'WRITE REGS':
180 # If we see a Repeated Start here, it's a multi-byte read.
181 if cmd == 'START REPEAT':
182 self.state = 'READ REGS'
184 # Otherwise: Get data bytes until a STOP condition occurs.
185 if cmd == 'DATA WRITE':
186 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
189 # TODO: Check for NACK!
195 elif self.state == 'READ REGS':
196 # Wait for an address read operation.
197 # TODO: We should only handle packets to the slave(?)
198 if cmd == 'ADDRESS READ':
199 self.state = 'READ REGS2'
203 elif self.state == 'READ REGS2':
204 if cmd == 'DATA READ':
205 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
208 # TODO: Check for NACK!