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