]>
Commit | Line | Data |
---|---|---|
d997c01a UH |
1 | ## |
2 | ## This file is part of the sigrok 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 | # ... | |
26 | status = { | |
27 | # SH[1:0] | |
28 | 'sh': { | |
29 | 0b00: 'no shake event', | |
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 | } | |
42 | ||
43 | class Decoder(srd.Decoder): | |
44 | api_version = 1 | |
45 | id = 'mxc6225xu' | |
46 | name = 'MXC6225XU' | |
47 | longname = 'MEMSIC MXC6225XU' | |
48 | desc = 'Digital Thermal Orientation Sensor (DTOS) protocol' | |
49 | license = 'gplv2+' | |
50 | inputs = ['i2c'] | |
51 | outputs = ['mxc6225xu'] | |
52 | probes = [] | |
53 | optional_probes = [ | |
54 | {'id': 'int', 'name': 'INT', 'desc': 'DTOS interrupt output pin'}, | |
55 | ] | |
56 | options = {} | |
57 | annotations = [ | |
58 | ['TODO', 'TODO'], | |
59 | ] | |
60 | ||
61 | def __init__(self, **kwargs): | |
62 | self.state = 'IDLE' | |
63 | ||
64 | def start(self, metadata): | |
65 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mxc6225xu') | |
66 | self.out_ann = self.add(srd.OUTPUT_ANN, 'mxc6225xu') | |
67 | ||
68 | def report(self): | |
69 | pass | |
70 | ||
71 | def putx(self, data): | |
72 | self.put(self.ss, self.es, self.out_ann, data) | |
73 | ||
74 | def handle_reg_0x00(self, b): | |
75 | # XOUT: 8-bit x-axis acceleration output. | |
76 | # Data is in 2's complement, values range from -128 to 127. | |
77 | self.putx([0, ['XOUT: ' + str(b)]]) | |
78 | ||
79 | def handle_reg_0x01(self, b): | |
80 | # YOUT: 8-bit y-axis acceleration output. | |
81 | # Data is in 2's complement, values range from -128 to 127. | |
82 | self.putx([0, ['YOUT: ' + str(b)]]) | |
83 | ||
84 | def handle_reg_0x02(self, b): | |
85 | # STATUS: Orientation and shake status. | |
86 | ||
87 | # Bits [7:7]: INT | |
88 | int_val = (b >> 7) & 1 | |
89 | s = 'unchanged and no' if (int_val == 0) else 'changed or' | |
90 | ann = 'INT = %d: Orientation %s shake event occured\n' % (int_val, s) | |
91 | ||
92 | # Bits[6:5]: SH[1:0] | |
93 | sh = (((b >> 6) & 1) << 1) | ((b >> 5) & 1) | |
94 | ann += 'SH[1:0] = %s: %s\n' % (bin(sh)[2:], status['sh'][sh]) | |
95 | ||
96 | # Bits[4:4]: TILT | |
97 | tilt = (b >> 7) & 1 | |
98 | s = '' if (tilt == 0) else 'not ' | |
99 | ann += 'TILT = %d: Orientation measurement is %svalid\n' % (tilt, s) | |
100 | ||
101 | # Bits[3:2]: ORI[1:0] | |
102 | ori = (((b >> 3) & 1) << 1) | ((b >> 2) & 1) | |
103 | ann += 'ORI[1:0] = %s: %s\n' % (bin(ori)[2:], status['ori'][ori]) | |
104 | ||
105 | # Bits[1:0]: OR[1:0] | |
106 | or_val = (((b >> 1) & 1) << 1) | ((b >> 0) & 1) | |
107 | ann += 'OR[1:0] = %s: %s\n' % (bin(or_val)[2:], status['ori'][or_val]) | |
108 | ||
109 | # ann += 'b = %s\n' % (bin(b)) | |
110 | ||
111 | self.putx([0, [ann]]) | |
112 | ||
113 | def handle_reg_0x03(self, b): | |
114 | # DETECTION: Powerdown, orientation and shake detection parameters. | |
115 | # Note: This is a write-only register. | |
116 | ||
117 | # Bits [7:7]: PD | |
118 | pd = (b >> 7) & 1 | |
119 | s = 'Do not power down' if (int_val == 0) else 'Power down' | |
120 | ann = 'PD = %d: %s the device\n' % (pd, s) | |
121 | ||
122 | # Bits [6:6]: SHM | |
123 | shm = (b >> 6) & 1 | |
124 | s = 'TODO' if (shm == 0) else 'TODO' | |
125 | ann = 'SHM = %d: %s\n' % (pd, s) | |
126 | ||
127 | # TODO | |
128 | # self.putx([0, ['TODO: DETECTION']]) | |
129 | ||
130 | # TODO: Fixup, this is copy-pasted from another PD. | |
131 | def decode(self, ss, es, data): | |
132 | cmd, databyte = data | |
133 | ||
134 | # Store the start/end samples of this I2C packet. | |
135 | self.ss, self.es = ss, es | |
136 | ||
137 | # State machine. | |
138 | if self.state == 'IDLE': | |
139 | # Wait for an I2C START condition. | |
140 | if cmd != 'START': | |
141 | return | |
142 | self.state = 'GET SLAVE ADDR' | |
143 | self.block_start_sample = ss | |
144 | elif self.state == 'GET SLAVE ADDR': | |
145 | # Wait for an address write operation. | |
146 | # TODO: We should only handle packets to the slave (0x2a/TODO). | |
147 | if cmd != 'ADDRESS WRITE': | |
148 | return | |
149 | self.state = 'GET REG ADDR' | |
150 | elif self.state == 'GET REG ADDR': | |
151 | # Wait for a data write (master selects the slave register). | |
152 | if cmd != 'DATA WRITE': | |
153 | return | |
154 | self.reg = databyte | |
155 | self.state = 'WRITE RTC REGS' | |
156 | elif self.state == 'WRITE RTC REGS': | |
157 | # If we see a Repeated Start here, it's probably an RTC read. | |
158 | if cmd == 'START REPEAT': | |
159 | self.state = 'READ RTC REGS' | |
160 | return | |
161 | # Otherwise: Get data bytes until a STOP condition occurs. | |
162 | if cmd == 'DATA WRITE': | |
163 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) | |
164 | handle_reg(databyte) | |
165 | self.reg += 1 | |
166 | # TODO: Check for NACK! | |
167 | elif cmd == 'STOP': | |
168 | # TODO | |
169 | self.state = 'IDLE' | |
170 | else: | |
171 | pass # TODO | |
172 | elif self.state == 'READ RTC REGS': | |
173 | # Wait for an address read operation. | |
174 | # TODO: We should only handle packets to the RTC slave (0xa2/0xa3). | |
175 | if cmd == 'ADDRESS READ': | |
176 | self.state = 'READ RTC REGS2' | |
177 | return | |
178 | else: | |
179 | pass # TODO | |
180 | elif self.state == 'READ RTC REGS2': | |
181 | if cmd == 'DATA READ': | |
182 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) | |
183 | handle_reg(databyte) | |
184 | self.reg += 1 | |
185 | # TODO: Check for NACK! | |
186 | elif cmd == 'STOP': | |
187 | # TODO | |
188 | self.state = 'IDLE' | |
189 | else: | |
190 | pass # TODO? | |
191 | else: | |
192 | raise Exception('Invalid state: %d' % self.state) | |
193 |