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