]> sigrok.org Git - libsigrokdecode.git/blob - decoders/adns5020/pd.py
0ee3f8b348b79ca111e9e741fec39a9299c7adff
[libsigrokdecode.git] / decoders / adns5020 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Karl Palsson <karlp@tweak.net.au>
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 regs = {
23         0: 'Product_ID',
24         1: 'Revision_ID',
25         2: 'Motion',
26         3: 'Delta_X',
27         4: 'Delta_Y',
28         5: 'SQUAL',
29         6: 'Shutter_Upper',
30         7: 'Shutter_Lower',
31         8: 'Maximum_Pixel',
32         9: 'Pixel_Sum',
33         0xa: 'Minimum_Pixel',
34         0xb: 'Pixel_Grab',
35         0xd: 'Mouse_Control',
36         0x3a: 'Chip_Reset',
37         0x3f: 'Inv_Rev_ID',
38         0x63: 'Motion_Burst',
39 }
40
41 class Decoder(srd.Decoder):
42     api_version = 3
43     id = 'adns5020'
44     name = 'ADNS-5020'
45     longname = 'Avago ADNS-5020 optical mouse sensor'
46     desc = 'Bidirectional command and data over an SPI-like protocol.'
47     license = 'gplv2+'
48     inputs = ['spi']
49     outputs = ['adns5020']
50     annotations = (
51         ('read', 'Register read commands'),
52         ('write', 'Register write commands'),
53         ('warning', 'Warnings'),
54     )
55     annotation_rows = (
56         ('read', 'Read', (0,)),
57         ('write', 'Write', (1,)),
58         ('warnings', 'Warnings', (2,)),
59     )
60
61     def __init__(self):
62         self.ss_cmd, self.es_cmd = 0, 0
63         self.mosi_bytes = []
64
65     def start(self):
66         self.out_ann = self.register(srd.OUTPUT_ANN)
67
68     def putx(self, data):
69         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
70
71     def put_warn(self, pos, msg):
72         self.put(pos[0], pos[1], self.out_ann, [2, [msg]])
73
74     def decode(self, ss, es, data):
75         ptype = data[0]
76         if ptype == 'CS-CHANGE':
77             # If we transition high mid-stream, toss out our data and restart.
78             cs_old, cs_new = data[1:]
79             if cs_old is not None and cs_old == 0 and cs_new == 1:
80                 if len(self.mosi_bytes) not in [0, 2]:
81                     self.put_warn([self.ss_cmd, es], 'Misplaced CS#!')
82                     self.mosi_bytes = []
83             return
84
85         # Don't care about anything else.
86         if ptype != 'DATA':
87             return
88         mosi, miso = data[1:]
89
90         self.ss, self.es = ss, es
91
92         if len(self.mosi_bytes) == 0:
93             self.ss_cmd = ss
94         self.mosi_bytes.append(mosi)
95
96         # Writes/reads are mostly two transfers (burst mode is different).
97         if len(self.mosi_bytes) != 2:
98             return
99
100         self.es_cmd = es
101         cmd, arg = self.mosi_bytes
102         write = cmd & 0x80
103         reg = cmd & 0x7f
104         reg_desc = regs.get(reg, 'Reserved %#x' % reg)
105         if reg > 0x63:
106             reg_desc = 'Unknown'
107         if write:
108             self.putx([1, ['%s: %#x' % (reg_desc, arg)]])
109         else:
110             self.putx([0, ['%s: %d' % (reg_desc, arg)]])
111
112         self.mosi_bytes = []