]>
Commit | Line | Data |
---|---|---|
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 | class Decoder(srd.Decoder): | |
24 | api_version = 3 | |
25 | id = 'mrf24j40' | |
26 | name = 'MRF24J40' | |
27 | longname = 'Microchip MRF24J40' | |
28 | desc = 'IEEE 802.15.4 2.4 GHz RF tranceiver chip.' | |
29 | license = 'gplv2+' | |
30 | inputs = ['spi'] | |
31 | outputs = [] | |
32 | tags = ['IC', 'Wireless/RF'] | |
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.reset() | |
48 | ||
49 | def reset(self): | |
50 | self.ss_cmd, self.es_cmd = 0, 0 | |
51 | self.mosi_bytes = [] | |
52 | self.miso_bytes = [] | |
53 | ||
54 | def start(self): | |
55 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
56 | ||
57 | def putx(self, data): | |
58 | self.put(self.ss_cmd, self.es_cmd, self.out_ann, data) | |
59 | ||
60 | def putw(self, pos, msg): | |
61 | self.put(pos[0], pos[1], self.out_ann, [4, [msg]]) | |
62 | ||
63 | def reset_data(self): | |
64 | self.mosi_bytes = [] | |
65 | self.miso_bytes = [] | |
66 | ||
67 | def handle_short(self): | |
68 | write = self.mosi_bytes[0] & 0x1 | |
69 | reg = (self.mosi_bytes[0] >> 1) & 0x3f | |
70 | reg_desc = sregs.get(reg, 'illegal') | |
71 | if write: | |
72 | self.putx([1, ['%s: %#x' % (reg_desc, self.mosi_bytes[1])]]) | |
73 | else: | |
74 | self.putx([0, ['%s: %#x' % (reg_desc, self.miso_bytes[1])]]) | |
75 | ||
76 | def handle_long(self): | |
77 | dword = self.mosi_bytes[0] << 8 | self.mosi_bytes[1] | |
78 | write = dword & (0x1 << 4) | |
79 | reg = dword >> 5 & 0x3ff | |
80 | if reg >= 0x0: | |
81 | reg_desc = 'TX:%#x' % reg | |
82 | if reg >= 0x80: | |
83 | reg_desc = 'TX beacon:%#x' % reg | |
84 | if reg >= 0x100: | |
85 | reg_desc = 'TX GTS1:%#x' % reg | |
86 | if reg >= 0x180: | |
87 | reg_desc = 'TX GTS2:%#x' % reg | |
88 | if reg >= 0x200: | |
89 | reg_desc = lregs.get(reg, 'illegal') | |
90 | if reg >= 0x280: | |
91 | reg_desc = 'Security keys:%#x' % reg | |
92 | if reg >= 0x2c0: | |
93 | reg_desc = 'Reserved:%#x' % reg | |
94 | if reg >= 0x300: | |
95 | reg_desc = 'RX:%#x' % reg | |
96 | ||
97 | if write: | |
98 | self.putx([3, ['%s: %#x' % (reg_desc, self.mosi_bytes[2])]]) | |
99 | else: | |
100 | self.putx([2, ['%s: %#x' % (reg_desc, self.miso_bytes[2])]]) | |
101 | ||
102 | def decode(self, ss, es, data): | |
103 | ptype = data[0] | |
104 | if ptype == 'CS-CHANGE': | |
105 | # If we transition high mid-stream, toss out our data and restart. | |
106 | cs_old, cs_new = data[1:] | |
107 | if cs_old is not None and cs_old == 0 and cs_new == 1: | |
108 | if len(self.mosi_bytes) not in (0, 2, 3): | |
109 | self.putw([self.ss_cmd, es], 'Misplaced CS!') | |
110 | self.reset_data() | |
111 | return | |
112 | ||
113 | # Don't care about anything else. | |
114 | if ptype != 'DATA': | |
115 | return | |
116 | mosi, miso = data[1:] | |
117 | ||
118 | self.ss, self.es = ss, es | |
119 | ||
120 | if len(self.mosi_bytes) == 0: | |
121 | self.ss_cmd = ss | |
122 | self.mosi_bytes.append(mosi) | |
123 | self.miso_bytes.append(miso) | |
124 | ||
125 | # Everything is either 2 bytes or 3 bytes. | |
126 | if len(self.mosi_bytes) < 2: | |
127 | return | |
128 | ||
129 | if self.mosi_bytes[0] & 0x80: | |
130 | if len(self.mosi_bytes) == 3: | |
131 | self.es_cmd = es | |
132 | self.handle_long() | |
133 | self.reset_data() | |
134 | else: | |
135 | self.es_cmd = es | |
136 | self.handle_short() | |
137 | self.reset_data() |