2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Marco Geisler <m-sigrok@mageis.de>
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, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
23 ANN_STROBE, ANN_SINGLE_READ, ANN_SINGLE_WRITE, ANN_BURST_READ, \
24 ANN_BURST_WRITE, ANN_STATUS_READ, ANN_STATUS, ANN_WARN = range(8)
26 class Decoder(srd.Decoder):
30 longname = 'Texas Instruments CC1101'
31 desc = 'Low-power sub-1GHz RF transceiver chip.'
35 tags = ['IC', 'Wireless/RF']
37 ('strobe', 'Command strobe'),
38 ('single_read', 'Single register read'),
39 ('single_write', 'Single register write'),
40 ('burst_read', 'Burst register read'),
41 ('burst_write', 'Burst register write'),
42 ('status', 'Status register'),
43 ('warning', 'Warning'),
46 ('cmd', 'Commands', (ANN_STROBE,)),
47 ('data', 'Data', (ANN_SINGLE_READ, ANN_SINGLE_WRITE, ANN_BURST_READ,
48 ANN_BURST_WRITE, ANN_STATUS_READ)),
49 ('status', 'Status register', (ANN_STATUS,)),
50 ('warnings', 'Warnings', (ANN_WARN,)),
58 self.requirements_met = True
59 self.cs_was_released = False
62 self.out_ann = self.register(srd.OUTPUT_ANN)
64 def warn(self, pos, msg):
65 '''Put a warning message 'msg' at 'pos'.'''
66 self.put(pos[0], pos[1], self.out_ann, [ANN_WARN, [msg]])
68 def putp(self, pos, ann, msg):
69 '''Put an annotation message 'msg' at 'pos'.'''
70 self.put(pos[0], pos[1], self.out_ann, [ann, [msg]])
72 def putp2(self, pos, ann, msg1, msg2):
73 '''Put an annotation message 'msg' at 'pos'.'''
74 self.put(pos[0], pos[1], self.out_ann, [ann, [msg1, msg2]])
77 '''Resets the decoder after a complete command was decoded.'''
78 # 'True' for the first byte after CS# went low.
81 # The current command, and the minimum and maximum number
82 # of data bytes to follow.
87 # Used to collect the bytes after the command byte
88 # (and the start/end sample number).
94 '''Returns the collected MOSI bytes of a multi byte command.'''
95 return [b[0] for b in self.mb]
98 '''Returns the collected MISO bytes of a multi byte command.'''
99 return [b[1] for b in self.mb]
101 def decode_command(self, pos, b):
102 '''Decodes the command byte 'b' at position 'pos' and prepares
103 the decoding of the following data bytes.'''
104 c = self.parse_command(b)
106 self.warn(pos, 'unknown command')
109 self.cmd, self.dat, self.min, self.max = c
111 if self.cmd in ('STROBE_CMD',):
112 self.putp(pos, ANN_STROBE, self.format_command())
114 # Don't output anything now, the command is merged with
115 # the data bytes following it.
118 def format_command(self):
119 '''Returns the label for the current command.'''
120 if self.cmd == 'SINGLE_READ':
122 if self.cmd == 'BURST_READ':
124 if self.cmd == 'SINGLE_WRITE':
126 if self.cmd == 'BURST_WRITE':
128 if self.cmd == 'STATUS_READ':
130 if self.cmd == 'STROBE_CMD':
131 reg = strobes.get(self.dat, 'unknown strobe')
132 return 'STROBE "{}"'.format(reg)
134 return 'TODO Cmd {}'.format(self.cmd)
136 def parse_command(self, b):
137 '''Parses the command byte.
139 Returns a tuple consisting of:
140 - the name of the command
141 - additional data needed to dissect the following bytes
142 - minimum number of following bytes
143 - maximum number of following bytes (None for infinite)
147 if (addr < 0x30) or (addr == 0x3E) or (addr == 0x3F):
148 if (b & 0xC0) == 0x00:
149 return ('SINGLE_WRITE', addr, 1, 1)
150 if (b & 0xC0) == 0x40:
151 return ('BURST_WRITE', addr, 1, 99999)
152 if (b & 0xC0) == 0x80:
153 return ('SINGLE_READ', addr, 1, 1)
154 if (b & 0xC0) == 0xC0:
155 return ('BURST_READ', addr, 1, 99999)
157 self.warn(pos, 'unknown address/command combination')
159 if (b & 0x40) == 0x00:
160 return ('STROBE_CMD', addr, 0, 0)
161 if (b & 0xC0) == 0xC0:
162 return ('STATUS_READ', addr, 1, 99999)
164 self.warn(pos, 'unknown address/command combination')
166 def decode_register(self, pos, ann, regid, data):
167 '''Decodes a register.
169 pos -- start and end sample numbers of the register
170 ann -- the annotation number that is used to output the register.
171 regid -- may be either an integer used as a key for the 'regs'
172 dictionary, or a string directly containing a register name.'
173 data -- the register content.
176 if type(regid) == int:
177 # Get the name of the register.
178 if regid not in regs:
179 self.warn(pos, 'unknown register')
181 name = '{} (0x{:02X})'.format(regs[regid], regid)
185 if regid == 'STATUS' and ann == ANN_STATUS:
187 self.decode_status_reg(pos, ann, data, label)
189 if self.cmd in ('SINGLE_WRITE', 'SINGLE_READ', 'STATUS_READ', 'BURST_READ', 'BURST_WRITE'):
190 label = '{}: {}'.format(self.format_command(), name)
192 label = 'Reg ({}) {}'.format(self.cmd, name)
193 self.decode_mb_data(pos, ann, data, label)
195 def decode_status_reg(self, pos, ann, data, label):
196 '''Decodes the data bytes 'data' of a status register at position
197 'pos'. The decoded data is prefixed with 'label'.'''
199 # bit 7 --> CHIP_RDYn
200 if status & 0b10000000 == 0b10000000:
201 longtext_chiprdy = 'CHIP_RDYn is high! '
203 longtext_chiprdy = ''
205 state = (status & 0x70) >> 4
206 longtext_state = 'STATE is {}, '.format(status_reg_states[state])
207 # bits 3:0 --> FIFO_BYTES_AVAILABLE
208 fifo_bytes = status & 0x0F
209 if self.cmd in ('SINGLE_READ', 'STATUS_READ', 'BURST_READ'):
210 longtext_fifo = '{} bytes available in RX FIFO'.format(fifo_bytes)
212 longtext_fifo = '{} bytes free in TX FIFO'.format(fifo_bytes)
214 text = '{} = "0x{:02X}"'.format(label, status)
215 longtext = ''.join([text, '; ', longtext_chiprdy, longtext_state, longtext_fifo])
216 self.putp2(pos, ann, longtext, text)
218 def decode_mb_data(self, pos, ann, data, label):
219 '''Decodes the data bytes 'data' of a multibyte command at position
220 'pos'. The decoded data is prefixed with 'label'.'''
223 return '{:02X}'.format(b)
225 data = ' '.join([escape(b) for b in data])
226 text = '{} = "0x{}"'.format(label, data)
227 self.putp(pos, ann, text)
229 def finish_command(self, pos):
230 '''Decodes the remaining data bytes at position 'pos'.'''
232 if self.cmd == 'SINGLE_WRITE':
233 self.decode_register(pos, ANN_SINGLE_WRITE,
234 self.dat, self.mosi_bytes())
235 elif self.cmd == 'BURST_WRITE':
236 self.decode_register(pos, ANN_BURST_WRITE,
237 self.dat, self.mosi_bytes())
238 elif self.cmd == 'SINGLE_READ':
239 self.decode_register(pos, ANN_SINGLE_READ,
240 self.dat, self.miso_bytes())
241 elif self.cmd == 'BURST_READ':
242 self.decode_register(pos, ANN_BURST_READ,
243 self.dat, self.miso_bytes())
244 elif self.cmd == 'STROBE_CMD':
245 self.decode_register(pos, ANN_STROBE,
246 self.dat, self.mosi_bytes())
247 elif self.cmd == 'STATUS_READ':
248 self.decode_register(pos, ANN_STATUS_READ,
249 self.dat, self.miso_bytes())
251 self.warn(pos, 'unhandled command')
253 def decode(self, ss, es, data):
254 if not self.requirements_met:
257 ptype, data1, data2 = data
259 if ptype == 'CS-CHANGE':
262 self.requirements_met = False
263 raise ChannelError('CS# pin required.')
265 self.cs_was_released = True
267 if data1 == 0 and data2 == 1:
268 # Rising edge, the complete command is transmitted, process
269 # the bytes that were sent after the command byte.
271 # Check if we got the minimum number of data bytes
272 # after the command byte.
273 if len(self.mb) < self.min:
274 self.warn((ss, ss), 'missing data bytes')
276 self.finish_command((self.ss_mb, self.es_mb))
279 self.cs_was_released = True
281 elif ptype == 'DATA' and self.cs_was_released:
282 mosi, miso = data1, data2
285 if miso is None or mosi is None:
286 self.requirements_met = False
287 raise ChannelError('Both MISO and MOSI pins required.')
291 # First MOSI byte is always the command.
292 self.decode_command(pos, mosi)
293 # First MISO byte is always the status register.
294 self.decode_register(pos, ANN_STATUS, 'STATUS', [miso])
296 if not self.cmd or len(self.mb) >= self.max:
297 self.warn(pos, 'excess byte')
299 # Collect the bytes after the command byte.
303 self.mb.append((mosi, miso))