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