]> sigrok.org Git - libsigrokdecode.git/blob - decoders/mrf24j40/pd.py
Use consistent __init__() format across all PDs.
[libsigrokdecode.git] / decoders / mrf24j40 / 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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import sigrokdecode as srd
22 from .lists import *
23
24 class Decoder(srd.Decoder):
25     api_version = 2
26     id = 'mrf24j40'
27     name = 'MRF24J40'
28     longname = 'Microchip MRF24J40'
29     desc = 'IEEE 802.15.4 2.4 GHz RF tranceiver chip.'
30     license = 'gplv2+'
31     inputs = ['spi']
32     outputs = ['mrf24j40']
33     annotations = (
34         ('sread', 'Short register read commands'),
35         ('swrite', 'Short register write commands'),
36         ('lread', 'Long register read commands'),
37         ('lwrite', 'Long register write commands'),
38         ('warning', 'Warnings'),
39     )
40     annotation_rows = (
41         ('read', 'Read', (0, 2)),
42         ('write', 'Write', (1, 3)),
43         ('warnings', 'Warnings', (4,)),
44     )
45
46     def __init__(self):
47         self.ss_cmd, self.es_cmd = 0, 0
48         self.mosi_bytes = []
49         self.miso_bytes = []
50
51     def start(self):
52         self.out_ann = self.register(srd.OUTPUT_ANN)
53
54     def putx(self, data):
55         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
56
57     def putw(self, pos, msg):
58         self.put(pos[0], pos[1], self.out_ann, [4, [msg]])
59
60     def reset(self):
61         self.mosi_bytes = []
62         self.miso_bytes = []
63
64     def handle_short(self):
65         write = self.mosi_bytes[0] & 0x1
66         reg = (self.mosi_bytes[0] >> 1) & 0x3f
67         reg_desc = sregs.get(reg, 'illegal')
68         if write:
69             self.putx([1, ['%s: %#x' % (reg_desc, self.mosi_bytes[1])]])
70         else:
71             self.putx([0, ['%s: %#x' % (reg_desc, self.miso_bytes[1])]])
72
73     def handle_long(self):
74         dword = self.mosi_bytes[0] << 8 | self.mosi_bytes[1]
75         write = dword & (0x1 << 4)
76         reg = dword >> 5 & 0x3ff
77         if reg >= 0x0:
78             reg_desc = 'TX:%#x' % reg
79         if reg >= 0x80:
80             reg_desc = 'TX beacon:%#x' % reg
81         if reg >= 0x100:
82             reg_desc = 'TX GTS1:%#x' % reg
83         if reg >= 0x180:
84             reg_desc = 'TX GTS2:%#x' % reg
85         if reg >= 0x200:
86             reg_desc = lregs.get(reg, 'illegal')
87         if reg >= 0x280:
88             reg_desc = 'Security keys:%#x' % reg
89         if reg >= 0x2c0:
90             reg_desc = 'Reserved:%#x' % reg
91         if reg >= 0x300:
92             reg_desc = 'RX:%#x' % reg
93
94         if write:
95             self.putx([3, ['%s: %#x' % (reg_desc, self.mosi_bytes[2])]])
96         else:
97             self.putx([2, ['%s: %#x' % (reg_desc, self.miso_bytes[2])]])
98
99     def decode(self, ss, es, data):
100         ptype = data[0]
101         if ptype == 'CS-CHANGE':
102             # If we transition high mid-stream, toss out our data and restart.
103             cs_old, cs_new = data[1:]
104             if cs_old is not None and cs_old == 0 and cs_new == 1:
105                 if len(self.mosi_bytes) not in (0, 2, 3):
106                     self.putw([self.ss_cmd, es], 'Misplaced CS!')
107                     self.reset()
108             return
109
110         # Don't care about anything else.
111         if ptype != 'DATA':
112             return
113         mosi, miso = data[1:]
114
115         self.ss, self.es = ss, es
116
117         if len(self.mosi_bytes) == 0:
118             self.ss_cmd = ss
119         self.mosi_bytes.append(mosi)
120         self.miso_bytes.append(miso)
121
122         # Everything is either 2 bytes or 3 bytes.
123         if len(self.mosi_bytes) < 2:
124             return
125
126         if self.mosi_bytes[0] & 0x80:
127             if len(self.mosi_bytes) == 3:
128                 self.es_cmd = es
129                 self.handle_long()
130                 self.reset()
131         else:
132             self.es_cmd = es
133             self.handle_short()
134             self.reset()