]>
Commit | Line | Data |
---|---|---|
0e501c70 JS |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com> | |
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 | ||
f04964c6 | 23 | class ChannelError(Exception): |
0e501c70 JS |
24 | pass |
25 | ||
26 | regs = { | |
27 | # addr: ('name', size) | |
28 | 0x00: ('CONFIG', 1), | |
29 | 0x01: ('EN_AA', 1), | |
30 | 0x02: ('EN_RXADDR', 1), | |
31 | 0x03: ('SETUP_AW', 1), | |
32 | 0x04: ('SETUP_RETR', 1), | |
33 | 0x05: ('RF_CH', 1), | |
34 | 0x06: ('RF_SETUP', 1), | |
35 | 0x07: ('STATUS', 1), | |
36 | 0x08: ('OBSERVE_TX', 1), | |
37 | 0x09: ('RPD', 1), | |
38 | 0x0a: ('RX_ADDR_P0', 5), | |
39 | 0x0b: ('RX_ADDR_P1', 5), | |
40 | 0x0c: ('RX_ADDR_P2', 1), | |
41 | 0x0d: ('RX_ADDR_P3', 1), | |
42 | 0x0e: ('RX_ADDR_P4', 1), | |
43 | 0x0f: ('RX_ADDR_P5', 1), | |
44 | 0x10: ('TX_ADDR', 5), | |
45 | 0x11: ('RX_PW_P0', 1), | |
46 | 0x12: ('RX_PW_P1', 1), | |
47 | 0x13: ('RX_PW_P2', 1), | |
48 | 0x14: ('RX_PW_P3', 1), | |
49 | 0x15: ('RX_PW_P4', 1), | |
50 | 0x16: ('RX_PW_P5', 1), | |
51 | 0x17: ('FIFO_STATUS', 1), | |
52 | 0x1c: ('DYNPD', 1), | |
35b380b1 | 53 | 0x1d: ('FEATURE', 1), |
0e501c70 JS |
54 | } |
55 | ||
56 | class Decoder(srd.Decoder): | |
57 | api_version = 2 | |
58 | id = 'nrf24l01' | |
35b380b1 | 59 | name = 'nRF24L01(+)' |
0e501c70 | 60 | longname = 'Nordic Semiconductor nRF24L01/nRF24L01+' |
35b380b1 | 61 | desc = '2.4GHz transceiver chip.' |
0e501c70 JS |
62 | license = 'gplv2+' |
63 | inputs = ['spi'] | |
64 | outputs = ['nrf24l01'] | |
65 | annotations = ( | |
35b380b1 UH |
66 | # Sent from the host to the chip. |
67 | ('cmd', 'Commands sent to the device'), | |
68 | ('tx-data', 'Payload sent to the device'), | |
0e501c70 | 69 | |
35b380b1 UH |
70 | # Returned by the chip. |
71 | ('register', 'Registers read from the device'), | |
72 | ('rx-data', 'Payload read from the device'), | |
0e501c70 JS |
73 | |
74 | ('warning', 'Warnings'), | |
75 | ) | |
76 | ann_cmd = 0 | |
77 | ann_tx = 1 | |
78 | ann_reg = 2 | |
79 | ann_rx = 3 | |
80 | ann_warn = 4 | |
81 | annotation_rows = ( | |
82 | ('commands', 'Commands', (ann_cmd, ann_tx)), | |
83 | ('responses', 'Responses', (ann_reg, ann_rx)), | |
84 | ('warnings', 'Warnings', (ann_warn,)), | |
85 | ) | |
86 | ||
87 | def __init__(self, **kwargs): | |
88 | self.next() | |
3cc4c4a9 | 89 | self.requirements_met = True |
0e501c70 JS |
90 | |
91 | def start(self): | |
92 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
93 | ||
94 | def warn(self, pos, msg): | |
95 | '''Put a warning message 'msg' at 'pos'.''' | |
96 | self.put(pos[0], pos[1], self.out_ann, [self.ann_warn, [msg]]) | |
97 | ||
98 | def putp(self, pos, ann, msg): | |
99 | '''Put an annotation message 'msg' at 'pos'.''' | |
100 | self.put(pos[0], pos[1], self.out_ann, [ann, [msg]]) | |
101 | ||
102 | def next(self): | |
103 | '''Resets the decoder after a complete command was decoded.''' | |
35b380b1 | 104 | # 'True' for the first byte after CS went low. |
0e501c70 JS |
105 | self.first = True |
106 | ||
35b380b1 UH |
107 | # The current command, and the minimum and maximum number |
108 | # of data bytes to follow. | |
0e501c70 JS |
109 | self.cmd = None |
110 | self.min = 0 | |
111 | self.max = 0 | |
112 | ||
35b380b1 UH |
113 | # Used to collect the bytes after the command byte |
114 | # (and the start/end sample number). | |
0e501c70 JS |
115 | self.mb = [] |
116 | self.mb_s = -1 | |
117 | self.mb_e = -1 | |
118 | ||
119 | def mosi_bytes(self): | |
120 | '''Returns the collected MOSI bytes of a multi byte command.''' | |
121 | return [b[0] for b in self.mb] | |
122 | ||
123 | def miso_bytes(self): | |
124 | '''Returns the collected MISO bytes of a multi byte command.''' | |
125 | return [b[1] for b in self.mb] | |
126 | ||
127 | def decode_command(self, pos, b): | |
128 | '''Decodes the command byte 'b' at position 'pos' and prepares | |
129 | the decoding of the following data bytes.''' | |
130 | c = self.parse_command(b) | |
35b380b1 | 131 | if c is None: |
0e501c70 JS |
132 | self.warn(pos, 'unknown command') |
133 | return | |
134 | ||
135 | self.cmd, self.dat, self.min, self.max = c | |
136 | ||
137 | if self.cmd in ('W_REGISTER', 'ACTIVATE'): | |
35b380b1 UH |
138 | # Don't output anything now, the command is merged with |
139 | # the data bytes following it. | |
0e501c70 JS |
140 | self.mb_s = pos[0] |
141 | else: | |
142 | self.putp(pos, self.ann_cmd, self.format_command()) | |
143 | ||
144 | def format_command(self): | |
145 | '''Returns the label for the current command.''' | |
146 | if self.cmd == 'R_REGISTER': | |
147 | reg = regs[self.dat][0] if self.dat in regs else 'unknown register' | |
8aeedf9e | 148 | return 'Cmd R_REGISTER "{}"'.format(reg) |
0e501c70 | 149 | else: |
8aeedf9e | 150 | return 'Cmd {}'.format(self.cmd) |
0e501c70 JS |
151 | |
152 | def parse_command(self, b): | |
153 | '''Parses the command byte. | |
154 | ||
155 | Returns a tuple consisting of: | |
156 | - the name of the command | |
157 | - additional data needed to dissect the following bytes | |
158 | - minimum number of following bytes | |
159 | - maximum number of following bytes | |
160 | ''' | |
161 | ||
162 | if (b & 0xe0) in (0b00000000, 0b00100000): | |
163 | c = 'R_REGISTER' if not (b & 0xe0) else 'W_REGISTER' | |
164 | d = b & 0x1f | |
165 | m = regs[d][1] if d in regs else 1 | |
166 | return (c, d, 1, m) | |
167 | if b == 0b01010000: | |
168 | # nRF24L01 only | |
169 | return ('ACTIVATE', None, 1, 1) | |
170 | if b == 0b01100001: | |
171 | return ('R_RX_PAYLOAD', None, 1, 32) | |
172 | if b == 0b01100000: | |
173 | return ('R_RX_PL_WID', None, 1, 1) | |
174 | if b == 0b10100000: | |
175 | return ('W_TX_PAYLOAD', None, 1, 32) | |
176 | if b == 0b10110000: | |
177 | return ('W_TX_PAYLOAD_NOACK', None, 1, 32) | |
178 | if (b & 0xf8) == 0b10101000: | |
179 | return ('W_ACK_PAYLOAD', b & 0x07, 1, 32) | |
180 | if b == 0b11100001: | |
181 | return ('FLUSH_TX', None, 0, 0) | |
182 | if b == 0b11100010: | |
183 | return ('FLUSH_RX', None, 0, 0) | |
184 | if b == 0b11100011: | |
185 | return ('REUSE_TX_PL', None, 0, 0) | |
186 | if b == 0b11111111: | |
187 | return ('NOP', None, 0, 0) | |
188 | ||
189 | def decode_register(self, pos, ann, regid, data): | |
190 | '''Decodes a register. | |
191 | ||
192 | pos -- start and end sample numbers of the register | |
193 | ann -- is the annotation number that is used to output the register. | |
194 | regid -- may be either an integer used as a key for the 'regs' | |
195 | dictionary, or a string directly containing a register name.' | |
196 | data -- is the register content. | |
197 | ''' | |
198 | ||
199 | if type(regid) == int: | |
35b380b1 | 200 | # Get the name of the register. |
0e501c70 JS |
201 | if regid not in regs: |
202 | self.warn(pos, 'unknown register') | |
203 | return | |
0e501c70 JS |
204 | name = regs[regid][0] |
205 | else: | |
206 | name = regid | |
207 | ||
35b380b1 | 208 | # Multi byte register come LSByte first. |
0e501c70 JS |
209 | data = reversed(data) |
210 | ||
211 | if self.cmd == 'W_REGISTER' and ann == self.ann_cmd: | |
35b380b1 | 212 | # The 'W_REGISTER' command is merged with the following byte(s). |
0e501c70 JS |
213 | label = '{}: {}'.format(self.format_command(), name) |
214 | else: | |
8aeedf9e | 215 | label = 'Reg {}'.format(name) |
0e501c70 | 216 | |
8aeedf9e | 217 | self.decode_mb_data(pos, ann, data, label, True) |
0e501c70 | 218 | |
8aeedf9e | 219 | def decode_mb_data(self, pos, ann, data, label, always_hex): |
0e501c70 | 220 | '''Decodes the data bytes 'data' of a multibyte command at position |
8aeedf9e JS |
221 | 'pos'. The decoded data is prefixed with 'label'. If 'always_hex' is |
222 | True, all bytes are decoded as hex codes, otherwise only non | |
223 | printable characters are escaped.''' | |
224 | ||
225 | if always_hex: | |
226 | def escape(b): | |
227 | return '{:02X}'.format(b) | |
228 | else: | |
229 | def escape(b): | |
230 | c = chr(b) | |
231 | if not str.isprintable(c): | |
232 | return '\\x{:02X}'.format(b) | |
233 | return c | |
0e501c70 JS |
234 | |
235 | data = ''.join([escape(b) for b in data]) | |
236 | text = '{} = "{}"'.format(label, data) | |
237 | self.putp(pos, ann, text) | |
238 | ||
239 | def finish_command(self, pos): | |
240 | '''Decodes the remaining data bytes at position 'pos'.''' | |
241 | ||
242 | if self.cmd == 'R_REGISTER': | |
243 | self.decode_register(pos, self.ann_reg, | |
35b380b1 | 244 | self.dat, self.miso_bytes()) |
0e501c70 JS |
245 | elif self.cmd == 'W_REGISTER': |
246 | self.decode_register(pos, self.ann_cmd, | |
35b380b1 | 247 | self.dat, self.mosi_bytes()) |
0e501c70 JS |
248 | elif self.cmd == 'R_RX_PAYLOAD': |
249 | self.decode_mb_data(pos, self.ann_rx, | |
250 | self.miso_bytes(), 'RX payload', False) | |
251 | elif (self.cmd == 'W_TX_PAYLOAD' or | |
252 | self.cmd == 'W_TX_PAYLOAD_NOACK'): | |
253 | self.decode_mb_data(pos, self.ann_tx, | |
254 | self.mosi_bytes(), 'TX payload', False) | |
255 | elif self.cmd == 'W_ACK_PAYLOAD': | |
256 | lbl = 'ACK payload for pipe {}'.format(self.dat) | |
257 | self.decode_mb_data(pos, self.ann_tx, | |
258 | self.mosi_bytes(), lbl, False) | |
259 | elif self.cmd == 'R_RX_PL_WID': | |
260 | msg = 'Payload width = {}'.format(self.mb[0][1]) | |
261 | self.putp(pos, self.ann_reg, msg) | |
262 | elif self.cmd == 'ACTIVATE': | |
263 | self.putp(pos, self.ann_cmd, self.format_command()) | |
264 | if self.mosi_bytes()[0] != 0x73: | |
265 | self.warn(pos, 'wrong data for "ACTIVATE" command') | |
266 | ||
267 | def decode(self, ss, es, data): | |
3cc4c4a9 JS |
268 | if not self.requirements_met: |
269 | return | |
270 | ||
0e501c70 JS |
271 | ptype, data1, data2 = data |
272 | ||
273 | if ptype == 'CS-CHANGE': | |
3cc4c4a9 JS |
274 | if data1 == -1: |
275 | if data2 == -1: | |
276 | self.requirements_met = False | |
277 | raise ChannelError('CS# pin required.') | |
278 | ||
0e501c70 | 279 | if data1 == 0 and data2 == 1: |
35b380b1 UH |
280 | # Rising edge, the complete command is transmitted, process |
281 | # the bytes that were send after the command byte. | |
0e501c70 | 282 | if self.cmd: |
35b380b1 UH |
283 | # Check if we got the minimum number of data bytes |
284 | # after the command byte. | |
0e501c70 JS |
285 | if len(self.mb) < self.min: |
286 | self.warn((ss, ss), 'missing data bytes') | |
287 | elif self.mb: | |
288 | self.finish_command((self.mb_s, self.mb_e)) | |
289 | ||
290 | self.next() | |
291 | elif ptype == 'DATA': | |
35b380b1 | 292 | mosi, miso = data1, data2 |
0e501c70 JS |
293 | pos = (ss, es) |
294 | ||
35b380b1 | 295 | if miso is None or mosi is None: |
3cc4c4a9 | 296 | self.requirements_met = False |
f04964c6 | 297 | raise ChannelError('Both MISO and MOSI pins required.') |
0e501c70 JS |
298 | |
299 | if self.first: | |
300 | self.first = False | |
35b380b1 | 301 | # First MOSI byte is always the command. |
0e501c70 | 302 | self.decode_command(pos, mosi) |
35b380b1 | 303 | # First MISO byte is always the status register. |
0e501c70 JS |
304 | self.decode_register(pos, self.ann_reg, 'STATUS', [miso]) |
305 | else: | |
306 | if not self.cmd or len(self.mb) >= self.max: | |
307 | self.warn(pos, 'excess byte') | |
308 | else: | |
35b380b1 | 309 | # Collect the bytes after the command byte. |
0e501c70 JS |
310 | if self.mb_s == -1: |
311 | self.mb_s = ss | |
312 | self.mb_e = es | |
313 | self.mb.append((mosi, miso)) |