2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
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.
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.
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
21 import sigrokdecode as srd
23 class MissingDataError(Exception):
27 # addr: ('name', size)
30 0x02: ('EN_RXADDR', 1),
31 0x03: ('SETUP_AW', 1),
32 0x04: ('SETUP_RETR', 1),
34 0x06: ('RF_SETUP', 1),
36 0x08: ('OBSERVE_TX', 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),
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),
56 class Decoder(srd.Decoder):
60 longname = 'Nordic Semiconductor nRF24L01/nRF24L01+'
61 desc = '2.4 GHz transceiver chip.'
64 outputs = ['nrf24l01']
66 # sent from the host to the chip
67 ('cmd', 'Commands send to the device.'),
68 ('tx-data', 'Payload send to the device.'),
70 # returned by the chip
71 ('register', 'Registers read from the device.'),
72 ('rx-data', 'Payload read from the device.'),
74 ('warning', 'Warnings'),
82 ('commands', 'Commands', (ann_cmd, ann_tx)),
83 ('responses', 'Responses', (ann_reg, ann_rx)),
84 ('warnings', 'Warnings', (ann_warn,)),
87 def __init__(self, **kwargs):
91 self.out_ann = self.register(srd.OUTPUT_ANN)
93 def warn(self, pos, msg):
94 '''Put a warning message 'msg' at 'pos'.'''
95 self.put(pos[0], pos[1], self.out_ann, [self.ann_warn, [msg]])
97 def putp(self, pos, ann, msg):
98 '''Put an annotation message 'msg' at 'pos'.'''
99 self.put(pos[0], pos[1], self.out_ann, [ann, [msg]])
102 '''Resets the decoder after a complete command was decoded.'''
103 # 'True' for the first byte after CS went low
106 # the current command, and the minimum and maximum number
107 # of data bytes to follow
112 # used to collect the bytes after the command byte
113 # (and the start/end sample number)
118 def mosi_bytes(self):
119 '''Returns the collected MOSI bytes of a multi byte command.'''
120 return [b[0] for b in self.mb]
122 def miso_bytes(self):
123 '''Returns the collected MISO bytes of a multi byte command.'''
124 return [b[1] for b in self.mb]
126 def decode_command(self, pos, b):
127 '''Decodes the command byte 'b' at position 'pos' and prepares
128 the decoding of the following data bytes.'''
129 c = self.parse_command(b)
131 self.warn(pos, 'unknown command')
134 self.cmd, self.dat, self.min, self.max = c
136 if self.cmd in ('W_REGISTER', 'ACTIVATE'):
137 # don't output anything now, the command is merged with
138 # the data bytes following it
141 self.putp(pos, self.ann_cmd, self.format_command())
143 def format_command(self):
144 '''Returns the label for the current command.'''
145 if self.cmd == 'R_REGISTER':
146 reg = regs[self.dat][0] if self.dat in regs else 'unknown register'
147 return 'Cmd. R_REGISTER "{}"'.format(reg)
149 return 'Cmd. {}'.format(self.cmd)
151 def parse_command(self, b):
152 '''Parses the command byte.
154 Returns a tuple consisting of:
155 - the name of the command
156 - additional data needed to dissect the following bytes
157 - minimum number of following bytes
158 - maximum number of following bytes
161 if (b & 0xe0) in (0b00000000, 0b00100000):
162 c = 'R_REGISTER' if not (b & 0xe0) else 'W_REGISTER'
164 m = regs[d][1] if d in regs else 1
168 return ('ACTIVATE', None, 1, 1)
170 return ('R_RX_PAYLOAD', None, 1, 32)
172 return ('R_RX_PL_WID', None, 1, 1)
174 return ('W_TX_PAYLOAD', None, 1, 32)
176 return ('W_TX_PAYLOAD_NOACK', None, 1, 32)
177 if (b & 0xf8) == 0b10101000:
178 return ('W_ACK_PAYLOAD', b & 0x07, 1, 32)
180 return ('FLUSH_TX', None, 0, 0)
182 return ('FLUSH_RX', None, 0, 0)
184 return ('REUSE_TX_PL', None, 0, 0)
186 return ('NOP', None, 0, 0)
188 def decode_register(self, pos, ann, regid, data):
189 '''Decodes a register.
191 pos -- start and end sample numbers of the register
192 ann -- is the annotation number that is used to output the register.
193 regid -- may be either an integer used as a key for the 'regs'
194 dictionary, or a string directly containing a register name.'
195 data -- is the register content.
198 if type(regid) == int:
199 # get the name of the register
200 if regid not in regs:
201 self.warn(pos, 'unknown register')
204 name = regs[regid][0]
208 # multi byte register come LSByte first
209 data = reversed(data)
211 if self.cmd == 'W_REGISTER' and ann == self.ann_cmd:
212 # the 'W_REGISTER' command is merged with the following byte(s)
213 label = '{}: {}'.format(self.format_command(), name)
215 label = 'Reg. {}'.format(name)
217 self.decode_mb_data(pos, ann, data, label)
219 def decode_mb_data(self, pos, ann, data, label, escape_all=True):
220 '''Decodes the data bytes 'data' of a multibyte command at position
221 'pos'. The decoded data is prefixed with 'label'. If 'excape_all' is
222 True, all data bytes are escaped as hex codes.'''
226 if escape_all or not str.isprintable(c):
227 return '\\x{:02X}'.format(b)
230 data = ''.join([escape(b) for b in data])
231 text = '{} = "{}"'.format(label, data)
232 self.putp(pos, ann, text)
234 def finish_command(self, pos):
235 '''Decodes the remaining data bytes at position 'pos'.'''
237 if self.cmd == 'R_REGISTER':
238 self.decode_register(pos, self.ann_reg,
239 self.dat, self.miso_bytes());
240 elif self.cmd == 'W_REGISTER':
241 self.decode_register(pos, self.ann_cmd,
242 self.dat, self.mosi_bytes());
243 elif self.cmd == 'R_RX_PAYLOAD':
244 self.decode_mb_data(pos, self.ann_rx,
245 self.miso_bytes(), 'RX payload', False)
246 elif (self.cmd == 'W_TX_PAYLOAD' or
247 self.cmd == 'W_TX_PAYLOAD_NOACK'):
248 self.decode_mb_data(pos, self.ann_tx,
249 self.mosi_bytes(), 'TX payload', False)
250 elif self.cmd == 'W_ACK_PAYLOAD':
251 lbl = 'ACK payload for pipe {}'.format(self.dat)
252 self.decode_mb_data(pos, self.ann_tx,
253 self.mosi_bytes(), lbl, False)
254 elif self.cmd == 'R_RX_PL_WID':
255 msg = 'Payload width = {}'.format(self.mb[0][1])
256 self.putp(pos, self.ann_reg, msg)
257 elif self.cmd == 'ACTIVATE':
258 self.putp(pos, self.ann_cmd, self.format_command())
259 if self.mosi_bytes()[0] != 0x73:
260 self.warn(pos, 'wrong data for "ACTIVATE" command')
262 def decode(self, ss, es, data):
263 ptype, data1, data2 = data
265 if ptype == 'CS-CHANGE':
266 if data1 == 0 and data2 == 1:
267 # rising edge, the complete command is transmitted, process
268 # the bytes that were send after the command byte
270 # check if we got the minimum number of data bytes
271 # after the command byte
272 if len(self.mb) < self.min:
273 self.warn((ss, ss), 'missing data bytes')
275 self.finish_command((self.mb_s, self.mb_e))
278 elif ptype == 'DATA':
283 if miso == None or mosi == None:
284 raise MissingDataError('Both MISO and MOSI pins required.')
288 # first MOSI byte is always the command
289 self.decode_command(pos, mosi)
290 # first MISO byte is always the status register
291 self.decode_register(pos, self.ann_reg, 'STATUS', [miso])
293 if not self.cmd or len(self.mb) >= self.max:
294 self.warn(pos, 'excess byte')
296 # collect the bytes after the command byte
300 self.mb.append((mosi, miso))