2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 SÅ‚awek Piotrowski <sentinel@atteo.org>
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
22 class Decoder(srd.Decoder):
26 longname = 'RFM12 control protocol'
27 desc = 'HopeRF RFM12 wireless transceiver control protocol.'
33 ('params', 'Command parameters'),
34 ('disabled', 'Disabled bits'),
35 ('return', 'Returned values'),
36 ('disabled_return', 'Disabled returned values'),
37 ('interpretation', 'Interpretation'),
40 ('commands', 'Commands', (0, 1, 2)),
41 ('return', 'Return', (3, 4)),
42 ('interpretation', 'Interpretation', (5,)),
46 self.mosi_bytes, self.miso_bytes = [], []
47 self.mosi_bits, self.miso_bits = [], []
48 self.row_pos = [0, 0, 0]
50 self.ann_to_row = [0, 0, 0, 1, 1, 2]
52 # Initialize with Power-On-Reset values.
53 self.last_status = [0x00, 0x00]
54 self.last_config = 0x08
55 self.last_power = 0x08
56 self.last_freq = 0x680
57 self.last_data_rate = 0x23
58 self.last_fifo_and_reset = 0x80
60 self.last_transceiver = 0x00
63 def advance_ann(self, ann, length):
64 row = self.ann_to_row[ann]
65 self.row_pos[row] += length
67 def putx(self, ann, length, description):
68 if not isinstance(description, list):
69 description = [description]
70 row = self.ann_to_row[ann]
71 bit = self.row_pos[row]
72 self.put(self.mosi_bits[bit][1], self.mosi_bits[bit + length - 1][2],
73 self.out_ann, [ann, description])
75 self.row_pos[row] = bit
77 def describe_bits(self, data, names):
78 i = 0x01 << len(names) - 1
82 self.putx(1 if (data & i) else 2, 1, names[bit])
86 def describe_return_bits(self, data, names):
87 i = 0x01 << len(names) - 1
91 self.putx(3 if (data & i) else 4, 1, names[bit])
93 self.advance_ann(3, 1)
97 def describe_changed_bits(self, data, old_data, names):
98 changes = data ^ old_data
99 i = 0x01 << (len(names) - 1)
102 if names[bit] != '' and changes & i:
103 s = ['+', 'Turning on'] if (data & i) else ['-', 'Turning off']
106 self.advance_ann(5, 1)
111 self.out_ann = self.register(srd.OUTPUT_ANN)
113 def handle_configuration_cmd(self, cmd, ret):
114 self.putx(0, 8, ['Configuration command', 'Configuration'])
115 NAMES = [['Internal data register', 'el'], ['FIFO mode', 'ef']]
117 bits = (cmd[1] & 0xC0) >> 6
118 old_bits = (self.last_config & 0xC0) >> 6
119 self.describe_bits(bits, NAMES)
120 self.describe_changed_bits(bits, old_bits, NAMES)
122 FREQUENCIES = ['315', '433', '868', '915']
123 f = FREQUENCIES[(cmd[1] & 0x30) >> 4] + 'MHz'
124 self.putx(1, 2, ['Frequency: ' + f, f])
125 if cmd[1] & 0x30 != self.last_config & 0x30:
126 self.putx(5, 2, ['Changed', '~'])
128 c = '%.1fpF' % (8.5 + (cmd[1] & 0xF) * 0.5)
129 self.putx(1, 4, ['Capacitance: ' + c, c])
130 if cmd[1] & 0xF != self.last_config & 0xF:
131 self.putx(5, 4, ['Changed', '~'])
133 self.last_config = cmd[1]
135 def handle_power_management_cmd(self, cmd, ret):
136 self.putx(0, 8, ['Power management', 'Power'])
137 NAMES = [['Receiver chain', 'er'], ['Baseband circuit', 'ebb'],
138 ['Transmission', 'et'], ['Synthesizer', 'es'],
139 ['Crystal oscillator', 'ex'], ['Low battery detector', 'eb'],
140 ['Wake-up timer', 'ew'], ['Clock output off switch', 'dc']]
142 self.describe_bits(cmd[1], NAMES)
146 # Some bits imply other, even if they are set to 0.
151 self.describe_changed_bits(power, self.last_power, NAMES)
153 self.last_power = power
155 def handle_frequency_setting_cmd(self, cmd, ret):
156 self.putx(0, 4, ['Frequency setting', 'Frequency'])
157 f = ((cmd[1] & 0xF) << 8) + cmd[2]
158 self.putx(0, 12, ['F = %3.4f' % f])
160 if self.last_freq != f:
161 self.putx(5, 12, ['Changing', '~'])
164 def handle_data_rate_cmd(self, cmd, ret):
165 self.putx(0, 8, ['Data rate command', 'Data rate'])
167 cs = (cmd[1] & 0x80) >> 7
168 rate = 10000 / 29.0 / (r + 1) / (1 + 7 * cs)
169 self.putx(0, 8, ['%3.1fkbps' % rate])
170 if self.last_data_rate != cmd[1]:
171 self.putx(5, 8, ['Changing', '~'])
172 self.last_data_rate = cmd[1]
174 def handle_receiver_control_cmd(self, cmd, ret):
175 self.putx(0, 5, ['Receiver control command'])
176 s = 'interrupt input' if (cmd[0] & 0x04) else 'VDI output'
177 self.putx(0, 1, ['pin16 = ' + s])
178 VDI_NAMES = ['Fast', 'Medium', 'Slow', 'Always on']
179 vdi_speed = VDI_NAMES[cmd[0] & 0x3]
180 self.putx(0, 2, ['VDI: %s' % vdi_speed])
181 BANDWIDTH_NAMES = ['Reserved', '400kHz', '340kHz', '270kHz', '200kHz',
182 '134kHz', '67kHz', 'Reserved']
183 bandwidth = BANDWIDTH_NAMES[(cmd[1] & 0xE0) >> 5]
184 self.putx(0, 3, ['Bandwidth: %s' % bandwidth])
185 LNA_GAIN_NAMES = [0, -6, -14, -20]
186 lna_gain = LNA_GAIN_NAMES[(cmd[1] & 0x18) >> 3]
187 self.putx(0, 2, ['LNA gain: %ddB' % lna_gain])
188 RSSI_THRESHOLD_NAMES = ['-103', '-97', '-91', '-85', '-79', '-73',
189 'Reserved', 'Reserved']
190 rssi_threshold = RSSI_THRESHOLD_NAMES[cmd[1] & 0x7]
191 self.putx(0, 3, ['RSSI threshold: %s' % rssi_threshold])
193 def handle_data_filter_cmd(self, cmd, ret):
194 self.putx(0, 8, ['Data filter command'])
196 clock_recovery = 'auto'
198 clock_recovery = 'fast'
200 clock_recovery = 'slow'
201 self.putx(0, 2, ['Clock recovery: %s mode' % clock_recovery])
202 self.advance_ann(0, 1) # Should always be 1.
203 s = 'analog' if (cmd[1] & 0x10) else 'digital'
204 self.putx(0, 1, ['Data filter: ' + s])
205 self.advance_ann(0, 1) # Should always be 1.
206 self.putx(0, 3, ['DQD threshold: %d' % (cmd[1] & 0x7)])
208 def handle_fifo_and_reset_cmd(self, cmd, ret):
209 self.putx(0, 8, ['FIFO and reset command'])
210 fifo_level = (cmd[1] & 0xF0) >> 4
211 self.putx(0, 4, ['FIFO trigger level: %d' % fifo_level])
212 last_fifo_level = (self.last_fifo_and_reset & 0xF0) >> 4
213 if fifo_level != last_fifo_level:
214 self.putx(5, 4, ['Changing', '~'])
216 self.advance_ann(5, 4)
217 s = 'one byte' if (cmd[1] & 0x08) else 'two bytes'
218 self.putx(0, 1, ['Synchron length: ' + s])
219 if (cmd[1] & 0x08) != (self.last_fifo_and_reset & 0x08):
220 self.putx(5, 1, ['Changing', '~'])
222 self.advance_ann(5, 1)
227 fifo_fill = 'After synchron pattern'
230 self.putx(0, 2, ['FIFO fill: %s' % fifo_fill])
231 if (cmd[1] & 0x06) != (self.last_fifo_and_reset & 0x06):
232 self.putx(5, 2, ['Changing', '~'])
234 self.advance_ann(5, 2)
236 s = 'non-sensitive' if (cmd[1] & 0x01) else 'sensitive'
237 self.putx(0, 1, ['Reset mode: ' + s])
238 if (cmd[1] & 0x01) != (self.last_fifo_and_reset & 0x01):
239 self.putx(5, 1, ['Changing', '~'])
241 self.advance_ann(5, 1)
243 self.last_fifo_and_reset = cmd[1]
245 def handle_synchron_pattern_cmd(self, cmd, ret):
246 self.putx(0, 8, ['Synchron pattern command'])
247 if self.last_fifo_and_reset & 0x08:
248 self.putx(0, 8, ['Pattern: 0x2D%02X' % pattern])
250 self.putx(0, 8, ['Pattern: %02X' % pattern])
252 def handle_fifo_read_cmd(self, cmd, ret):
253 self.putx(0, 8, ['FIFO read command', 'FIFO read'])
254 self.putx(3, 8, ['Data: %02X' % ret[1]])
256 def handle_afc_cmd(self, cmd, ret):
257 self.putx(0, 8, ['AFC command'])
258 MODES = ['Off', 'Once', 'During receiving', 'Always']
259 mode = (cmd[1] & 0xC0) >> 6
260 self.putx(0, 2, ['Mode: %s' % MODES[mode]])
261 if (cmd[1] & 0xC0) != (self.last_afc & 0xC0):
262 self.putx(5, 2, ['Changing', '~'])
264 self.advance_ann(5, 2)
266 range_limit = (cmd[1] & 0x30) >> 4
267 FREQ_TABLE = [0.0, 2.5, 5.0, 7.5]
268 freq_delta = FREQ_TABLE[(self.last_config & 0x30) >> 4]
271 self.putx(0, 2, ['Range: No limit'])
272 elif range_limit == 1:
273 self.putx(0, 2, ['Range: +/-%dkHz' % (15 * freq_delta)])
274 elif range_limit == 2:
275 self.putx(0, 2, ['Range: +/-%dkHz' % (7 * freq_delta)])
276 elif range_limit == 3:
277 self.putx(0, 2, ['Range: +/-%dkHz' % (3 * freq_delta)])
279 if (cmd[1] & 0x30) != (self.last_afc & 0x30):
280 self.putx(5, 2, ['Changing', '~'])
282 self.advance_ann(5, 2)
284 NAMES = ['Strobe edge', 'High accuracy mode', 'Enable offset register',
285 'Enable offset calculation']
286 self.describe_bits(cmd[1] & 0xF, NAMES)
287 self.describe_changed_bits(cmd[1] & 0xF, self.last_afc & 0xF, NAMES)
289 self.last_afc = cmd[1]
291 def handle_transceiver_control_cmd(self, cmd, ret):
292 self.putx(0, 8, ['Transceiver control command'])
293 self.putx(0, 4, ['FSK frequency delta: %dkHz' % (15 * ((cmd[1] & 0xF0) >> 4))])
294 if cmd[1] & 0xF0 != self.last_transceiver & 0xF0:
295 self.putx(5, 4, ['Changing', '~'])
297 self.advance_ann(5, 4)
299 POWERS = [0, -2.5, -5, -7.5, -10, -12.5, -15, -17.5]
300 self.advance_ann(0, 1)
301 self.advance_ann(5, 1)
302 self.putx(0,3, ['Relative power: %dB' % (cmd[1] & 0x07)])
303 if (cmd[1] & 0x07) != (self.last_transceiver & 0x07):
304 self.putx(5, 3, ['Changing', '~'])
306 self.advance_ann(5, 3)
307 self.last_transceiver = cmd[1]
309 def handle_pll_setting_cmd(self, cmd, ret):
310 self.putx(0, 8, ['PLL setting command'])
311 self.advance_ann(0, 1)
312 self.putx(0, 2, ['Clock buffer rise and fall time'])
313 self.advance_ann(0, 1)
314 self.advance_ann(5, 4)
315 NAMES = [['Delay in phase detector', 'dly'], ['Disable dithering', 'ddit']]
316 self.describe_bits((cmd[1] & 0xC) >> 2, NAMES)
317 self.describe_changed_bits((cmd[1] & 0xC) >> 2, (self.last_pll & 0xC) >> 2, NAMES)
318 s = '256kbps, high' if (cmd[1] & 0x01) else '86.2kbps, low'
319 self.putx(0, 1, ['Max bit rate: %s noise' % s])
321 self.advance_ann(5, 1)
322 if (cmd[1] & 0x01) != (self.last_pll & 0x01):
323 self.putx(5, 1, ['Changing', '~'])
325 self.last_pll = cmd[1]
327 def handle_transmitter_register_cmd(self, cmd, ret):
328 self.putx(0, 8, ['Transmitter register command', 'Transmit'])
329 self.putx(0, 8, ['Data: %s' % cmd[1], '%s' % cmd[1]])
331 def handle_software_reset_cmd(self, cmd, ret):
332 self.putx(0, 16, ['Software reset command'])
334 def handle_wake_up_timer_cmd(self, cmd, ret):
335 self.putx(0, 3, ['Wake-up timer command', 'Timer'])
338 time = 1.03 * m * pow(2, r) + 0.5
339 self.putx(0, 13, ['Time: %7.2f' % time])
341 def handle_low_duty_cycle_cmd(self, cmd, ret):
342 self.putx(0, 16, ['Low duty cycle command'])
344 def handle_low_battery_detector_cmd(self, cmd, ret):
345 self.putx(0, 8, ['Low battery detector command'])
346 NAMES = ['1', '1.25', '1.66', '2', '2.5', '3.33', '5', '10']
347 clock = NAMES[(cmd[1] & 0xE0) >> 5]
348 self.putx(0, 3, ['Clock output: %sMHz' % clock, '%sMHz' % clock])
349 self.advance_ann(0, 1)
350 v = 2.25 + (cmd[1] & 0x0F) * 0.1
351 self.putx(0, 4, ['Low battery voltage: %1.2fV' % v, '%1.2fV' % v])
353 def handle_status_read_cmd(self, cmd, ret):
354 self.putx(0, 8, ['Status read command', 'Status'])
355 NAMES = ['RGIT/FFIT', 'POR', 'RGUR/FFOV', 'WKUP', 'EXT', 'LBD',
356 'FFEM', 'RSSI/ATS', 'DQD', 'CRL', 'ATGL']
357 status = (ret[0] << 3) + (ret[1] >> 5)
360 self.describe_return_bits(status, NAMES)
361 receiver_enabled = (self.last_power & 0x80) >> 7
365 s = 'Received data in FIFO'
367 s = 'Transmit register ready'
370 self.advance_ann(5, 1)
372 self.putx(5, 1, 'Power on Reset')
374 self.advance_ann(5, 1)
377 s = 'RX FIFO overflow'
379 s = 'Transmit register under run'
382 self.advance_ann(5, 1)
384 self.putx(5, 1, 'Wake-up timer')
386 self.advance_ann(5, 1)
388 self.putx(5, 1, 'External interrupt')
390 self.advance_ann(5, 1)
392 self.putx(5, 1, 'Low battery')
394 self.advance_ann(5, 1)
396 self.putx(5, 1, 'FIFO is empty')
398 self.advance_ann(5, 1)
401 s = 'Incoming signal above limit'
403 s = 'Antenna detected RF signal'
406 self.advance_ann(5, 1)
408 self.putx(5, 1, 'Data quality detector')
410 self.advance_ann(5, 1)
412 self.putx(5, 1, 'Clock recovery locked')
414 self.advance_ann(5, 1)
415 self.advance_ann(5, 1)
417 self.putx(3, 5, ['AFC offset'])
418 if (self.last_status[1] & 0x1F) != (ret[1] & 0x1F):
419 self.putx(5, 5, ['Changed', '~'])
420 self.last_status = ret
422 def handle_cmd(self, cmd, ret):
424 self.handle_configuration_cmd(cmd, ret)
426 self.handle_power_management_cmd(cmd, ret)
427 elif cmd[0] & 0xF0 == 0xA0:
428 self.handle_frequency_setting_cmd(cmd, ret)
430 self.handle_data_rate_cmd(cmd, ret)
431 elif cmd[0] & 0xF8 == 0x90:
432 self.handle_receiver_control_cmd(cmd, ret)
434 self.handle_data_filter_cmd(cmd, ret)
436 self.handle_fifo_and_reset_cmd(cmd, ret)
438 self.handle_synchron_pattern_cmd(cmd, ret)
440 self.handle_fifo_read_cmd(cmd, ret)
442 self.handle_afc_cmd(cmd, ret)
443 elif cmd[0] & 0xFE == 0x98:
444 self.handle_transceiver_control_cmd(cmd, ret)
446 self.handle_pll_setting_cmd(cmd, ret)
448 self.handle_transmitter_register_cmd(cmd, ret)
450 self.handle_software_reset_cmd(cmd, ret)
451 elif cmd[0] & 0xE0 == 0xE0:
452 self.handle_wake_up_timer_cmd(cmd, ret)
454 self.handle_low_duty_cycle_cmd(cmd, ret)
456 self.handle_low_battery_detector_cmd(cmd, ret)
458 self.handle_status_read_cmd(cmd, ret)
460 c = '%02x %02x' % tuple(cmd)
461 r = '%02x %02x' % tuple(ret)
462 self.putx(0, 16, ['Unknown command: %s (reply: %s)!' % (c, r)])
464 def decode(self, ss, es, data):
465 ptype, mosi, miso = data
467 # For now, only use DATA and BITS packets.
468 if ptype not in ('DATA', 'BITS'):
471 # Store the individual bit values and ss/es numbers. The next packet
472 # is guaranteed to be a 'DATA' packet belonging to this 'BITS' one.
475 self.mosi_bits.extend(reversed(mosi))
477 self.miso_bits.extend(reversed(miso))
481 self.mosi_bytes.append(mosi)
482 self.miso_bytes.append(miso)
484 # All commands consist of 2 bytes.
485 if len(self.mosi_bytes) < 2:
488 self.row_pos = [0, 8, 8]
490 self.handle_cmd(self.mosi_bytes, self.miso_bytes)
492 self.mosi_bytes, self.miso_bytes = [], []
493 self.mosi_bits, self.miso_bits = [], []