]> sigrok.org Git - libsigrokdecode.git/blob - decoders/adxl345/pd.py
avr_isp: Add more parts
[libsigrokdecode.git] / decoders / adxl345 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2020 Analog Devices Inc.
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 3 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 from common.srdhelper import SrdIntEnum
22 from .lists import *
23
24 WORD_SIZE = 8
25
26 class Channel():
27     MISO, MOSI = range(2)
28
29 class Operation():
30     READ, WRITE = range(2)
31
32 class BitType():
33     ENABLE = {1: ['Enable %s', 'En %s', '%s '], 0: ['Disable %s', 'Dis %s', '!%s '],}
34     SOURCE = {1: ['Involve %s', 'Inv %s', '%s'], 0: ['Not involve %s', 'Not inv %s', '!%s'],}
35     INTERRUPT = {1: ['INT2 %s', 'I2: %s '], 0: ['INT1 %s', 'I1:%s '],}
36     AC_DC = {1: ['%s ac', 'ac'], 0: ['%s dc', 'dc'],}
37     UNUSED = {1: ['N/A'], 0: ['N/A'],}
38     OTHER = 0
39
40 class Bit():
41     def __init__(self, name, type, values=None):
42         self.value = 0
43         self.name = name
44         self.type = type
45         self.values = values
46
47     def set_value(self, value):
48         self.value = value
49
50     def get_bit_annotation(self):
51         if self.type == BitType.OTHER:
52             annotation = self.values[self.value].copy()
53         else:
54             annotation = self.type[self.value].copy()
55
56         for index in range(len(annotation)):
57             if '%s' in annotation[index]:
58                 annotation[index] = str(annotation[index] % self.name)
59         return annotation
60
61 Ann = SrdIntEnum.from_str('Ann', 'READ WRITE MB REG_ADDRESS REG_DATA WARNING')
62
63 St = SrdIntEnum.from_str('St', 'IDLE ADDRESS_BYTE DATA')
64
65 class Decoder(srd.Decoder):
66     api_version = 3
67     id = 'adxl345'
68     name = 'ADXL345'
69     longname = 'Analog Devices ADXL345'
70     desc = 'Analog Devices ADXL345 3-axis accelerometer.'
71     license = 'gplv2+'
72     inputs = ['spi']
73     outputs = []
74     tags = ['IC', 'Sensor']
75     annotations = (
76         ('read', 'Read'),
77         ('write', 'Write'),
78         ('mb', 'Multiple bytes'),
79         ('reg-address', 'Register address'),
80         ('reg-data', 'Register data'),
81         ('warning', 'Warning'),
82     )
83     annotation_rows = (
84         ('reg', 'Registers', (Ann.READ, Ann.WRITE, Ann.MB, Ann.REG_ADDRESS)),
85         ('data', 'Data', (Ann.REG_DATA, Ann.WARNING)),
86     )
87
88     def __init__(self):
89         self.reset()
90
91     def reset(self):
92         self.mosi, self.miso = [], []
93         self.reg = []
94         self.operation = None
95         self.address = 0
96         self.data = -1
97         self.state = St.IDLE
98         self.ss, self.es = -1, -1
99         self.samples_per_bit = 0
100
101     def start(self):
102         self.out_ann = self.register(srd.OUTPUT_ANN)
103
104     def putx(self, data):
105         self.put(self.ss, self.es, self.out_ann, data)
106
107     def putb(self, data, index):
108         start = self.ss + (self.samples_per_bit * index)
109         self.put(start, start + self.samples_per_bit, self.out_ann, data)
110
111     def putbs(self, data, start_index, stop_index):
112         start_index = self.reverse_bit_index(start_index, WORD_SIZE)
113         stop_index = self.reverse_bit_index(stop_index, WORD_SIZE)
114         start = self.ss + (self.samples_per_bit * start_index)
115         stop = start + (self.samples_per_bit * (stop_index - start_index + 1))
116         self.put(start, stop, self.out_ann, data)
117
118     def handle_reg_with_scaling_factor(self, data, factor, name, unit, error_msg):
119         if data == 0 and error_msg is not None:
120             self.putx([Ann.WARNING, error_msg])
121         else:
122             result = (data * factor) / 1000
123             self.putx([Ann.REG_DATA, ['%s: %f %s' % (name, result, unit), '%f %s' % (result, unit)]])
124
125     def handle_reg_bit_msg(self, bit, index, en_msg, dis_msg):
126         self.putb([Ann.REG_DATA, [en_msg if bit else dis_msg]], index)
127
128     def interpret_bits(self, data, bits):
129         bits_values = []
130         for offset in range(8):
131             bits_values.insert(0, (data & (1 << offset)) >> offset)
132
133         for index in range(len(bits)):
134             if bits[index] is None:
135                 continue
136             bit = bits[index]
137             bit.set_value(bits_values[index])
138             self.putb([Ann.REG_DATA, bit.get_bit_annotation()], index)
139
140         return list(reversed(bits_values))
141
142     def reverse_bit_index(self, index, word_size):
143         return word_size - index - 1
144
145     def get_decimal_number(self, bits, start_index, stop_index):
146         number = 0
147         interval = range(start_index, stop_index + 1, 1)
148         for index, offset in zip(interval, range(len(interval))):
149             bit = bits[index]
150             number = number | (bit << offset)
151         return number
152
153     def get_axis_value(self, data, axis):
154         if self.data != - 1:
155             data <<= 8
156             self.data |= data
157             self.put(self.start_index, self.es, self.out_ann,
158                 [Ann.REG_DATA, ['%s: 0x%04X' % (axis, self.data), str(data)]])
159             self.data = -1
160         else:
161             self.putx([Ann.REG_DATA, [str(data)]])
162
163     def handle_reg_0x1d(self, data):
164         self.handle_reg_with_scaling_factor(data, 62.5, 'Threshold', 'g',
165             error_messages['undesirable'])
166
167     def handle_reg_0x1e(self, data):
168         self.handle_reg_with_scaling_factor(data, 15.6, 'OFSX', 'g', None)
169
170     def handle_reg_0x1f(self, data):
171         self.handle_reg_with_scaling_factor(data, 15.6, 'OFSY', 'g', None)
172
173     def handle_reg_0x20(self, data):
174         self.handle_reg_with_scaling_factor(data, 15.6, 'OFSZ', 'g', None)
175
176     def handle_reg_0x21(self, data):
177         self.handle_reg_with_scaling_factor(data, 0.625, 'Duration', 's',
178             error_messages['dis_single_double'])
179
180     def handle_reg_0x22(self, data):
181         self.handle_reg_with_scaling_factor(data, 1.25, 'Latency', 's',
182             error_messages['dis_double'])
183
184     def handle_reg_0x23(self, data):
185         self.handle_reg_with_scaling_factor(data, 1.25, 'Window', 's',
186             error_messages['dis_double'])
187
188     def handle_reg_0x24(self, data):
189         self.handle_reg_0x1d(data)
190
191     def handle_reg_0x25(self, data):
192         self.handle_reg_0x1d(data)
193
194     def handle_reg_0x26(self, data):
195         self.handle_reg_with_scaling_factor(data, 1000, 'Time', 's',
196             error_messages['interrupt'])
197
198     def handle_reg_0x27(self, data):
199         bits = [Bit('ACT', BitType.AC_DC),
200                 Bit('ACT_X', BitType.ENABLE),
201                 Bit('ACT_Y', BitType.ENABLE),
202                 Bit('ACT_Z', BitType.ENABLE),
203                 Bit('INACT', BitType.AC_DC),
204                 Bit('INACT_X', BitType.ENABLE),
205                 Bit('INACT_Y', BitType.ENABLE),
206                 Bit('INACT_Z', BitType.ENABLE)]
207         self.interpret_bits(data, bits)
208
209     def handle_reg_0x28(self, data):
210         self.handle_reg_0x1d(data)
211
212     def handle_reg_0x29(self, data):
213         self.handle_reg_with_scaling_factor(data, 5, 'Time', 's',
214             error_messages['undesirable'])
215
216     def handle_reg_0x2a(self, data):
217         bits = [Bit('', BitType.UNUSED),
218                 Bit('', BitType.UNUSED),
219                 Bit('', BitType.UNUSED),
220                 Bit('', BitType.UNUSED),
221                 Bit('', BitType.OTHER, {1: ['Suppressed', 'Suppr', 'S'],
222                     0: ['Unsuppressed', 'Unsuppr', 'Uns'],}),
223                 Bit('TAP_X', BitType.ENABLE),
224                 Bit('TAP_Y', BitType.ENABLE),
225                 Bit('TAP_Z', BitType.ENABLE)]
226         self.interpret_bits(data, bits)
227
228     def handle_reg_0x2b(self, data):
229         bits = [Bit('', BitType.UNUSED),
230                 Bit('ACT_X', BitType.SOURCE),
231                 Bit('ACT_Y', BitType.SOURCE),
232                 Bit('ACT_Z', BitType.SOURCE),
233                 Bit('', BitType.OTHER, {1: ['Asleep', 'Asl'],
234                     0: ['Not asleep', 'Not asl', '!Asl'],}),
235                 Bit('TAP_X', BitType.SOURCE),
236                 Bit('TAP_Y', BitType.SOURCE),
237                 Bit('TAP_Z', BitType.SOURCE)]
238         self.interpret_bits(data, bits)
239
240     def handle_reg_0x2c(self, data):
241         bits = [Bit('', BitType.UNUSED),
242                 Bit('', BitType.UNUSED),
243                 Bit('', BitType.UNUSED),
244                 Bit('', BitType.OTHER, {1: ['Reduce power', 'Reduce pw', 'Red pw'], 0: ['Normal operation', 'Normal op', 'Norm op'],})]
245         bits_values = self.interpret_bits(data, bits)
246
247         start_index, stop_index = 0, 3
248         rate = self.get_decimal_number(bits_values, start_index, stop_index)
249         self.putbs([Ann.REG_DATA, ['%f' % rate_code[rate]]], stop_index, start_index)
250
251     def handle_reg_0x2d(self, data):
252         bits = [Bit('', BitType.UNUSED),
253                 Bit('', BitType.UNUSED),
254                 Bit('', BitType.OTHER, {1: ['Link'], 0: ['Unlink'], }),
255                 Bit('AUTO_SLEEP', BitType.ENABLE),
256                 Bit('', BitType.OTHER, {1: ['Measurement mode', 'Measurement', 'Meas'], 0: ['Standby mode', 'Standby'], }),
257                 Bit('', BitType.OTHER, {1: ['Sleep mode', 'Sleep', 'Slp'], 0: ['Normal mode', 'Normal', 'Nrm'],})]
258         bits_values = self.interpret_bits(data, bits)
259
260         start_index, stop_index = 0, 1
261         wakeup = self.get_decimal_number(bits_values, start_index, stop_index)
262         frequency = 2 ** (~wakeup & 0x03)
263         self.putbs([Ann.REG_DATA, ['%d Hz' % frequency]], stop_index, start_index)
264
265     def handle_reg_0x2e(self, data):
266         bits = [Bit('DATA_READY', BitType.ENABLE),
267                 Bit('SINGLE_TAP', BitType.ENABLE),
268                 Bit('DOUBLE_TAP', BitType.ENABLE),
269                 Bit('Activity', BitType.ENABLE),
270                 Bit('Inactivity', BitType.ENABLE),
271                 Bit('FREE_FALL', BitType.ENABLE),
272                 Bit('Watermark', BitType.ENABLE),
273                 Bit('Overrun', BitType.ENABLE)]
274         self.interpret_bits(data, bits)
275
276     def handle_reg_0x2f(self, data):
277         bits = [Bit('DATA_READY', BitType.INTERRUPT),
278                 Bit('SINGLE_TAP', BitType.INTERRUPT),
279                 Bit('DOUBLE_TAP', BitType.INTERRUPT),
280                 Bit('Activity', BitType.INTERRUPT),
281                 Bit('Inactivity', BitType.INTERRUPT),
282                 Bit('FREE_FALL', BitType.INTERRUPT),
283                 Bit('Watermark', BitType.INTERRUPT),
284                 Bit('Overrun', BitType.INTERRUPT)]
285         self.interpret_bits(data, bits)
286
287     def handle_reg_0x30(self, data):
288         bits = [Bit('DATA_READY', BitType.SOURCE),
289                 Bit('SINGLE_TAP', BitType.SOURCE),
290                 Bit('DOUBLE_TAP', BitType.SOURCE),
291                 Bit('Activity', BitType.SOURCE),
292                 Bit('Inactivity', BitType.SOURCE),
293                 Bit('FREE_FALL', BitType.SOURCE),
294                 Bit('Watermark', BitType.SOURCE),
295                 Bit('Overrun', BitType.SOURCE)]
296         self.interpret_bits(data, bits)
297
298     def handle_reg_0x31(self, data):
299         bits = [Bit('SELF_TEST', BitType.ENABLE),
300                 Bit('', BitType.OTHER, {1: ['3-wire SPI', '3-SPI'], 0: ['4-wire SPI', '4-SPI'],}),
301                 Bit('', BitType.OTHER, {1: ['INT ACT LOW', 'INT LOW'], 0: ['INT ACT HIGH', 'INT HIGH'],}),
302                 Bit('', BitType.UNUSED),
303                 Bit('', BitType.OTHER, {1: ['Full resolution', 'Full res'], 0: ['10-bit mode', '10-bit'],}),
304                 Bit('', BitType.OTHER, {1: ['MSB mode', 'MSB'], 0: ['LSB mode', 'LSB'],})]
305         bits_values = self.interpret_bits(data, bits)
306
307         start_index, stop_index = 0, 1
308         range_g = self.get_decimal_number(bits_values, start_index, stop_index)
309         result = 2 ** (range_g + 1)
310         self.putbs([Ann.REG_DATA, ['+/-%d g' % result]], stop_index, start_index)
311
312     def handle_reg_0x32(self, data):
313         self.data = data
314         self.putx([Ann.REG_DATA, [str(data)]])
315
316     def handle_reg_0x33(self, data):
317         self.get_axis_value(data, 'X')
318
319     def handle_reg_0x34(self, data):
320         self.handle_reg_0x32(data)
321
322     def handle_reg_0x35(self, data):
323         self.get_axis_value(data, 'Y')
324
325     def handle_reg_0x36(self, data):
326         self.handle_reg_0x32(data)
327
328     def handle_reg_0x37(self, data):
329         self.get_axis_value(data, 'Z')
330
331     def handle_reg_0x38(self, data):
332         bits = [None,
333                 None,
334                 Bit('', BitType.OTHER, {1: ['Trig-INT2', 'INT2'], 0: ['Trig-INT1', 'INT1'], })]
335         bits_values = self.interpret_bits(data, bits)
336
337         start_index, stop_index = 6, 7
338         fifo = self.get_decimal_number(bits_values, start_index, stop_index)
339         self.putbs([Ann.REG_DATA, [fifo_modes[fifo]]], stop_index, start_index)
340
341         start_index, stop_index = 0, 4
342         samples = self.get_decimal_number(bits_values, start_index, stop_index)
343         self.putbs([Ann.REG_DATA, ['Samples: %d' % samples, '%d' % samples]], stop_index, start_index)
344
345     def handle_reg_0x39(self, data):
346         bits = [Bit('', BitType.OTHER, {1: ['Triggered', 'Trigg'], 0: ['Not triggered', 'Not trigg'],}),
347                 Bit('', BitType.UNUSED)]
348         bits_values = self.interpret_bits(data, bits)
349
350         start_index, stop_index = 0, 5
351         entries = self.get_decimal_number(bits_values, start_index, stop_index)
352         self.putbs([Ann.REG_DATA, ['Entries: %d' % entries, '%d' % entries]], stop_index, start_index)
353
354     def get_bit(self, channel):
355         if (channel == Channel.MOSI and self.mosi is None) or \
356                 (channel == Channel.MISO and self.miso is None):
357             raise Exception('No available data')
358
359         mosi_bit, miso_bit = 0, 0
360         if self.miso is not None:
361             if len(self.mosi) < 0:
362                 raise Exception('No available data')
363             miso_bit = self.miso.pop(0)
364         if self.miso is not None:
365             if len(self.miso) < 0:
366                 raise Exception('No available data')
367             mosi_bit = self.mosi.pop(0)
368
369         if channel == Channel.MOSI:
370             return mosi_bit
371         return miso_bit
372
373     def decode(self, ss, es, data):
374         ptype = data[0]
375
376         if ptype == 'CS-CHANGE':
377             cs_old, cs_new = data[1:]
378             if cs_old is not None and cs_old == 1 and cs_new == 0:
379                 self.ss, self.es = ss, es
380                 self.state = St.ADDRESS_BYTE
381             else:
382                 self.state = St.IDLE
383
384         elif ptype == 'BITS':
385             if data[1] is not None:
386                 self.mosi = list(reversed(data[1]))
387             if data[2] is not None:
388                 self.miso = list(reversed(data[2]))
389
390             if self.mosi is None and self.miso is None:
391                 return
392
393             if self.state == St.ADDRESS_BYTE:
394                 # OPERATION BIT
395                 op_bit = self.get_bit(Channel.MOSI)
396                 self.put(op_bit[1], op_bit[2], self.out_ann,
397                     [Ann.READ if op_bit[0] else Ann.WRITE, operations[op_bit[0]]])
398                 self.operation = Operation.READ if op_bit[0] else Operation.WRITE
399                 # MULTIPLE-BYTE BIT
400                 mb_bit = self.get_bit(Channel.MOSI)
401                 self.put(mb_bit[1], mb_bit[2], self.out_ann, [Ann.MB, number_bytes[mb_bit[0]]])
402
403                 # REGISTER 6-BIT ADDRESS
404                 self.address = 0
405                 start_sample = self.mosi[0][1]
406                 addr_bit = []
407                 for i in range(6):
408                     addr_bit = self.get_bit(Channel.MOSI)
409                     self.address |= addr_bit[0]
410                     self.address <<= 1
411                 self.address >>= 1
412                 self.put(start_sample, addr_bit[2], self.out_ann,
413                     [Ann.REG_ADDRESS, ['ADDRESS: 0x%02X' % self.address, 'ADDR: 0x%02X'
414                     % self.address, '0x%02X' % self.address]])
415                 self.ss = -1
416                 self.state = St.DATA
417
418             elif self.state == St.DATA:
419                 self.reg.extend(self.mosi if self.operation == Operation.WRITE else self.miso)
420
421                 self.mosi, self.miso = [], []
422                 if self.ss == -1:
423                     self.ss, self.es = self.reg[0][1], es
424                     self.samples_per_bit = self.reg[0][2] - self.ss
425
426                 if len(self.reg) < 8:
427                     return
428                 else:
429                     reg_value = 0
430                     reg_bit = []
431                     for offset in range(7, -1, -1):
432                         reg_bit = self.reg.pop(0)
433
434                         mask = reg_bit[0] << offset
435                         reg_value |= mask
436
437                     if self.address < 0x00 or self.address > 0x39:
438                         return
439
440                     if self.address in [0x32, 0x34, 0x36]:
441                         self.start_index = self.ss
442
443                     if 0x1D > self.address >= 0x00:
444                         self.put(self.ss, reg_bit[2], self.out_ann, [Ann.REG_ADDRESS, [str(self.address)]])
445                         self.put(self.ss, reg_bit[2], self.out_ann, [Ann.REG_DATA, [str(reg_value)]])
446                     else:
447                         self.put(self.ss, reg_bit[2], self.out_ann, [Ann.REG_ADDRESS, registers[self.address]])
448                         handle_reg = getattr(self, 'handle_reg_0x%02x' % self.address)
449                         handle_reg(reg_value)
450
451                     self.reg = []
452                     self.address += 1
453                     self.ss = -1