]> sigrok.org Git - libsigrokdecode.git/blob - decoders/mrf24j40/pd.py
58d3dfde1677fd419dc05554d81c360a9692ee7d
[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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21 from .lists import *
22
23 TX, RX = range(2)
24
25 class Decoder(srd.Decoder):
26     api_version = 3
27     id = 'mrf24j40'
28     name = 'MRF24J40'
29     longname = 'Microchip MRF24J40'
30     desc = 'IEEE 802.15.4 2.4 GHz RF tranceiver chip.'
31     license = 'gplv2+'
32     inputs = ['spi']
33     outputs = []
34     tags = ['IC', 'Wireless/RF']
35     annotations = (
36         ('sread', 'Short register read commands'),
37         ('swrite', 'Short register write commands'),
38         ('lread', 'Long register read commands'),
39         ('lwrite', 'Long register write commands'),
40         ('warning', 'Warnings'),
41         ('tx-frame', 'TX frame'),
42         ('rx-frame', 'RX frame'),
43     )
44     annotation_rows = (
45         ('read', 'Read', (0, 2)),
46         ('write', 'Write', (1, 3)),
47         ('warnings', 'Warnings', (4,)),
48         ('tx-frames', 'TX frames', (5,)),
49         ('rx-frames', 'RX frames', (6,)),
50     )
51
52     def __init__(self):
53         self.reset()
54
55     def reset(self):
56         self.ss_cmd, self.es_cmd = 0, 0
57         self.ss_frame, self.es_frame = [0, 0], [0, 0]
58         self.mosi_bytes, self.miso_bytes = [], []
59         self.framecache = [[], []]
60
61     def start(self):
62         self.out_ann = self.register(srd.OUTPUT_ANN)
63
64     def putx(self, data):
65         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
66
67     def putw(self, pos, msg):
68         self.put(pos[0], pos[1], self.out_ann, [4, [msg]])
69
70     def reset_data(self):
71         self.mosi_bytes = []
72         self.miso_bytes = []
73
74     def handle_short(self):
75         write = self.mosi_bytes[0] & 0x1
76         reg = (self.mosi_bytes[0] >> 1) & 0x3f
77         reg_desc = sregs.get(reg, 'illegal')
78         for rxtx in (RX, TX):
79             if self.framecache[rxtx] == []:
80                 continue
81             bit0 = self.mosi_bytes[1] & (1 << 0)
82             if rxtx == TX and not (reg_desc == 'TXNCON' and bit0 == 1):
83                 continue
84             if rxtx == RX and not (reg_desc == 'RXFLUSH' and bit0 == 1):
85                 continue
86             idx = 5 if rxtx == TX else 6
87             xmitdir = 'TX' if rxtx == TX else 'RX'
88             frame = ' '.join(['%02X' % b for b in self.framecache[rxtx]])
89             self.put(self.ss_frame[rxtx], self.es_frame[rxtx], self.out_ann,
90                 [idx, ['%s frame: %s' % (xmitdir, frame)]])
91             self.framecache[rxtx] = []
92         if write:
93             self.putx([1, ['%s: %#x' % (reg_desc, self.mosi_bytes[1])]])
94         else:
95             self.putx([0, ['%s: %#x' % (reg_desc, self.miso_bytes[1])]])
96
97     def handle_long(self):
98         dword = self.mosi_bytes[0] << 8 | self.mosi_bytes[1]
99         write = dword & (0x1 << 4)
100         reg = dword >> 5 & 0x3ff
101         if reg >= 0x0:
102             reg_desc = 'TX:%#x' % reg
103         if reg >= 0x80:
104             reg_desc = 'TX beacon:%#x' % reg
105         if reg >= 0x100:
106             reg_desc = 'TX GTS1:%#x' % reg
107         if reg >= 0x180:
108             reg_desc = 'TX GTS2:%#x' % reg
109         if reg >= 0x200:
110             reg_desc = lregs.get(reg, 'illegal')
111         if reg >= 0x280:
112             reg_desc = 'Security keys:%#x' % reg
113         if reg >= 0x2c0:
114             reg_desc = 'Reserved:%#x' % reg
115         if reg >= 0x300:
116             reg_desc = 'RX:%#x' % reg
117
118         if write:
119             self.putx([3, ['%s: %#x' % (reg_desc, self.mosi_bytes[2])]])
120         else:
121             self.putx([2, ['%s: %#x' % (reg_desc, self.miso_bytes[2])]])
122
123         for rxtx in (RX, TX):
124             if rxtx == RX and reg_desc[:3] != 'RX:':
125                 continue
126             if rxtx == TX and reg_desc[:3] != 'TX:':
127                 continue
128             if len(self.framecache[rxtx]) == 0:
129                 self.ss_frame[rxtx] = self.ss_cmd
130             self.es_frame[rxtx] = self.es_cmd
131             self.framecache[rxtx] += [self.mosi_bytes[2]] if rxtx == TX else [self.miso_bytes[2]]
132
133     def decode(self, ss, es, data):
134         ptype = data[0]
135         if ptype == 'CS-CHANGE':
136             # If we transition high mid-stream, toss out our data and restart.
137             cs_old, cs_new = data[1:]
138             if cs_old is not None and cs_old == 0 and cs_new == 1:
139                 if len(self.mosi_bytes) not in (0, 2, 3):
140                     self.putw([self.ss_cmd, es], 'Misplaced CS!')
141                     self.reset_data()
142             return
143
144         # Don't care about anything else.
145         if ptype != 'DATA':
146             return
147         mosi, miso = data[1:]
148
149         self.ss, self.es = ss, es
150
151         if len(self.mosi_bytes) == 0:
152             self.ss_cmd = ss
153         self.mosi_bytes.append(mosi)
154         self.miso_bytes.append(miso)
155
156         # Everything is either 2 bytes or 3 bytes.
157         if len(self.mosi_bytes) < 2:
158             return
159
160         if self.mosi_bytes[0] & 0x80:
161             if len(self.mosi_bytes) == 3:
162                 self.es_cmd = es
163                 self.handle_long()
164                 self.reset_data()
165         else:
166             self.es_cmd = es
167             self.handle_short()
168             self.reset_data()