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 = 'HopeRF RFM12'
27 desc = 'HopeRF RFM12 wireless transceiver control protocol.'
31 tags = ['Wireless/RF']
34 ('param', 'Command parameter'),
35 ('disabled', 'Disabled bit'),
36 ('return', 'Returned value'),
37 ('disabled_return', 'Disabled returned value'),
38 ('interpretation', 'Interpretation'),
41 ('commands', 'Commands', (0, 1, 2)),
42 ('returns', 'Returns', (3, 4)),
43 ('interpretations', 'Interpretations', (5,)),
50 self.mosi_bytes, self.miso_bytes = [], []
51 self.mosi_bits, self.miso_bits = [], []
52 self.row_pos = [0, 0, 0]
54 self.ann_to_row = [0, 0, 0, 1, 1, 2]
56 # Initialize with Power-On-Reset values.
57 self.last_status = [0x00, 0x00]
58 self.last_config = 0x08
59 self.last_power = 0x08
60 self.last_freq = 0x680
61 self.last_data_rate = 0x23
62 self.last_fifo_and_reset = 0x80
64 self.last_transceiver = 0x00
67 def advance_ann(self, ann, length):
68 row = self.ann_to_row[ann]
69 self.row_pos[row] += length
71 def putx(self, ann, length, description):
72 if not isinstance(description, list):
73 description = [description]
74 row = self.ann_to_row[ann]
75 bit = self.row_pos[row]
76 self.put(self.mosi_bits[bit][1], self.mosi_bits[bit + length - 1][2],
77 self.out_ann, [ann, description])
79 self.row_pos[row] = bit
81 def describe_bits(self, data, names):
82 i = 0x01 << len(names) - 1
86 self.putx(1 if (data & i) else 2, 1, names[bit])
90 def describe_return_bits(self, data, names):
91 i = 0x01 << len(names) - 1
95 self.putx(3 if (data & i) else 4, 1, names[bit])
97 self.advance_ann(3, 1)
101 def describe_changed_bits(self, data, old_data, names):
102 changes = data ^ old_data
103 i = 0x01 << (len(names) - 1)
106 if names[bit] != '' and changes & i:
107 s = ['+', 'Turning on'] if (data & i) else ['-', 'Turning off']
110 self.advance_ann(5, 1)
115 self.out_ann = self.register(srd.OUTPUT_ANN)
117 def handle_configuration_cmd(self, cmd, ret):
118 self.putx(0, 8, ['Configuration command', 'Configuration'])
119 NAMES = [['Internal data register', 'el'], ['FIFO mode', 'ef']]
121 bits = (cmd[1] & 0xC0) >> 6
122 old_bits = (self.last_config & 0xC0) >> 6
123 self.describe_bits(bits, NAMES)
124 self.describe_changed_bits(bits, old_bits, NAMES)
126 FREQUENCIES = ['315', '433', '868', '915']
127 f = FREQUENCIES[(cmd[1] & 0x30) >> 4] + 'MHz'
128 self.putx(1, 2, ['Frequency: ' + f, f])
129 if cmd[1] & 0x30 != self.last_config & 0x30:
130 self.putx(5, 2, ['Changed', '~'])
132 c = '%.1fpF' % (8.5 + (cmd[1] & 0xF) * 0.5)
133 self.putx(1, 4, ['Capacitance: ' + c, c])
134 if cmd[1] & 0xF != self.last_config & 0xF:
135 self.putx(5, 4, ['Changed', '~'])
137 self.last_config = cmd[1]
139 def handle_power_management_cmd(self, cmd, ret):
140 self.putx(0, 8, ['Power management', 'Power'])
141 NAMES = [['Receiver chain', 'er'], ['Baseband circuit', 'ebb'],
142 ['Transmission', 'et'], ['Synthesizer', 'es'],
143 ['Crystal oscillator', 'ex'], ['Low battery detector', 'eb'],
144 ['Wake-up timer', 'ew'], ['Clock output off switch', 'dc']]
146 self.describe_bits(cmd[1], NAMES)
150 # Some bits imply other, even if they are set to 0.
155 self.describe_changed_bits(power, self.last_power, NAMES)
157 self.last_power = power
159 def handle_frequency_setting_cmd(self, cmd, ret):
160 self.putx(0, 4, ['Frequency setting', 'Frequency'])
161 f = ((cmd[1] & 0xF) << 8) + cmd[2]
162 self.putx(0, 12, ['F = %3.4f' % f])
164 if self.last_freq != f:
165 self.putx(5, 12, ['Changing', '~'])
168 def handle_data_rate_cmd(self, cmd, ret):
169 self.putx(0, 8, ['Data rate command', 'Data rate'])
171 cs = (cmd[1] & 0x80) >> 7
172 rate = 10000 / 29.0 / (r + 1) / (1 + 7 * cs)
173 self.putx(0, 8, ['%3.1fkbps' % rate])
174 if self.last_data_rate != cmd[1]:
175 self.putx(5, 8, ['Changing', '~'])
176 self.last_data_rate = cmd[1]
178 def handle_receiver_control_cmd(self, cmd, ret):
179 self.putx(0, 5, ['Receiver control command'])
180 s = 'interrupt input' if (cmd[0] & 0x04) else 'VDI output'
181 self.putx(0, 1, ['pin16 = ' + s])
182 VDI_NAMES = ['Fast', 'Medium', 'Slow', 'Always on']
183 vdi_speed = VDI_NAMES[cmd[0] & 0x3]
184 self.putx(0, 2, ['VDI: %s' % vdi_speed])
185 BANDWIDTH_NAMES = ['Reserved', '400kHz', '340kHz', '270kHz', '200kHz',
186 '134kHz', '67kHz', 'Reserved']
187 bandwidth = BANDWIDTH_NAMES[(cmd[1] & 0xE0) >> 5]
188 self.putx(0, 3, ['Bandwidth: %s' % bandwidth])
189 LNA_GAIN_NAMES = [0, -6, -14, -20]
190 lna_gain = LNA_GAIN_NAMES[(cmd[1] & 0x18) >> 3]
191 self.putx(0, 2, ['LNA gain: %ddB' % lna_gain])
192 RSSI_THRESHOLD_NAMES = ['-103', '-97', '-91', '-85', '-79', '-73',
193 'Reserved', 'Reserved']
194 rssi_threshold = RSSI_THRESHOLD_NAMES[cmd[1] & 0x7]
195 self.putx(0, 3, ['RSSI threshold: %s' % rssi_threshold])
197 def handle_data_filter_cmd(self, cmd, ret):
198 self.putx(0, 8, ['Data filter command'])
200 clock_recovery = 'auto'
202 clock_recovery = 'fast'
204 clock_recovery = 'slow'
205 self.putx(0, 2, ['Clock recovery: %s mode' % clock_recovery])
206 self.advance_ann(0, 1) # Should always be 1.
207 s = 'analog' if (cmd[1] & 0x10) else 'digital'
208 self.putx(0, 1, ['Data filter: ' + s])
209 self.advance_ann(0, 1) # Should always be 1.
210 self.putx(0, 3, ['DQD threshold: %d' % (cmd[1] & 0x7)])
212 def handle_fifo_and_reset_cmd(self, cmd, ret):
213 self.putx(0, 8, ['FIFO and reset command'])
214 fifo_level = (cmd[1] & 0xF0) >> 4
215 self.putx(0, 4, ['FIFO trigger level: %d' % fifo_level])
216 last_fifo_level = (self.last_fifo_and_reset & 0xF0) >> 4
217 if fifo_level != last_fifo_level:
218 self.putx(5, 4, ['Changing', '~'])
220 self.advance_ann(5, 4)
221 s = 'one byte' if (cmd[1] & 0x08) else 'two bytes'
222 self.putx(0, 1, ['Synchron length: ' + s])
223 if (cmd[1] & 0x08) != (self.last_fifo_and_reset & 0x08):
224 self.putx(5, 1, ['Changing', '~'])
226 self.advance_ann(5, 1)
231 fifo_fill = 'After synchron pattern'
234 self.putx(0, 2, ['FIFO fill: %s' % fifo_fill])
235 if (cmd[1] & 0x06) != (self.last_fifo_and_reset & 0x06):
236 self.putx(5, 2, ['Changing', '~'])
238 self.advance_ann(5, 2)
240 s = 'non-sensitive' if (cmd[1] & 0x01) else 'sensitive'
241 self.putx(0, 1, ['Reset mode: ' + s])
242 if (cmd[1] & 0x01) != (self.last_fifo_and_reset & 0x01):
243 self.putx(5, 1, ['Changing', '~'])
245 self.advance_ann(5, 1)
247 self.last_fifo_and_reset = cmd[1]
249 def handle_synchron_pattern_cmd(self, cmd, ret):
250 self.putx(0, 8, ['Synchron pattern command'])
251 if self.last_fifo_and_reset & 0x08:
252 self.putx(0, 8, ['Pattern: 0x2D%02X' % pattern])
254 self.putx(0, 8, ['Pattern: %02X' % pattern])
256 def handle_fifo_read_cmd(self, cmd, ret):
257 self.putx(0, 8, ['FIFO read command', 'FIFO read'])
258 self.putx(3, 8, ['Data: %02X' % ret[1]])
260 def handle_afc_cmd(self, cmd, ret):
261 self.putx(0, 8, ['AFC command'])
262 MODES = ['Off', 'Once', 'During receiving', 'Always']
263 mode = (cmd[1] & 0xC0) >> 6
264 self.putx(0, 2, ['Mode: %s' % MODES[mode]])
265 if (cmd[1] & 0xC0) != (self.last_afc & 0xC0):
266 self.putx(5, 2, ['Changing', '~'])
268 self.advance_ann(5, 2)
270 range_limit = (cmd[1] & 0x30) >> 4
271 FREQ_TABLE = [0.0, 2.5, 5.0, 7.5]
272 freq_delta = FREQ_TABLE[(self.last_config & 0x30) >> 4]
275 self.putx(0, 2, ['Range: No limit'])
276 elif range_limit == 1:
277 self.putx(0, 2, ['Range: +/-%dkHz' % (15 * freq_delta)])
278 elif range_limit == 2:
279 self.putx(0, 2, ['Range: +/-%dkHz' % (7 * freq_delta)])
280 elif range_limit == 3:
281 self.putx(0, 2, ['Range: +/-%dkHz' % (3 * freq_delta)])
283 if (cmd[1] & 0x30) != (self.last_afc & 0x30):
284 self.putx(5, 2, ['Changing', '~'])
286 self.advance_ann(5, 2)
288 NAMES = ['Strobe edge', 'High accuracy mode', 'Enable offset register',
289 'Enable offset calculation']
290 self.describe_bits(cmd[1] & 0xF, NAMES)
291 self.describe_changed_bits(cmd[1] & 0xF, self.last_afc & 0xF, NAMES)
293 self.last_afc = cmd[1]
295 def handle_transceiver_control_cmd(self, cmd, ret):
296 self.putx(0, 8, ['Transceiver control command'])
297 self.putx(0, 4, ['FSK frequency delta: %dkHz' % (15 * ((cmd[1] & 0xF0) >> 4))])
298 if cmd[1] & 0xF0 != self.last_transceiver & 0xF0:
299 self.putx(5, 4, ['Changing', '~'])
301 self.advance_ann(5, 4)
303 POWERS = [0, -2.5, -5, -7.5, -10, -12.5, -15, -17.5]
304 self.advance_ann(0, 1)
305 self.advance_ann(5, 1)
306 self.putx(0,3, ['Relative power: %dB' % (cmd[1] & 0x07)])
307 if (cmd[1] & 0x07) != (self.last_transceiver & 0x07):
308 self.putx(5, 3, ['Changing', '~'])
310 self.advance_ann(5, 3)
311 self.last_transceiver = cmd[1]
313 def handle_pll_setting_cmd(self, cmd, ret):
314 self.putx(0, 8, ['PLL setting command'])
315 self.advance_ann(0, 1)
316 self.putx(0, 2, ['Clock buffer rise and fall time'])
317 self.advance_ann(0, 1)
318 self.advance_ann(5, 4)
319 NAMES = [['Delay in phase detector', 'dly'], ['Disable dithering', 'ddit']]
320 self.describe_bits((cmd[1] & 0xC) >> 2, NAMES)
321 self.describe_changed_bits((cmd[1] & 0xC) >> 2, (self.last_pll & 0xC) >> 2, NAMES)
322 s = '256kbps, high' if (cmd[1] & 0x01) else '86.2kbps, low'
323 self.putx(0, 1, ['Max bit rate: %s noise' % s])
325 self.advance_ann(5, 1)
326 if (cmd[1] & 0x01) != (self.last_pll & 0x01):
327 self.putx(5, 1, ['Changing', '~'])
329 self.last_pll = cmd[1]
331 def handle_transmitter_register_cmd(self, cmd, ret):
332 self.putx(0, 8, ['Transmitter register command', 'Transmit'])
333 self.putx(0, 8, ['Data: %s' % cmd[1], '%s' % cmd[1]])
335 def handle_software_reset_cmd(self, cmd, ret):
336 self.putx(0, 16, ['Software reset command'])
338 def handle_wake_up_timer_cmd(self, cmd, ret):
339 self.putx(0, 3, ['Wake-up timer command', 'Timer'])
342 time = 1.03 * m * pow(2, r) + 0.5
343 self.putx(0, 13, ['Time: %7.2f' % time])
345 def handle_low_duty_cycle_cmd(self, cmd, ret):
346 self.putx(0, 16, ['Low duty cycle command'])
348 def handle_low_battery_detector_cmd(self, cmd, ret):
349 self.putx(0, 8, ['Low battery detector command'])
350 NAMES = ['1', '1.25', '1.66', '2', '2.5', '3.33', '5', '10']
351 clock = NAMES[(cmd[1] & 0xE0) >> 5]
352 self.putx(0, 3, ['Clock output: %sMHz' % clock, '%sMHz' % clock])
353 self.advance_ann(0, 1)
354 v = 2.25 + (cmd[1] & 0x0F) * 0.1
355 self.putx(0, 4, ['Low battery voltage: %1.2fV' % v, '%1.2fV' % v])
357 def handle_status_read_cmd(self, cmd, ret):
358 self.putx(0, 8, ['Status read command', 'Status'])
359 NAMES = ['RGIT/FFIT', 'POR', 'RGUR/FFOV', 'WKUP', 'EXT', 'LBD',
360 'FFEM', 'RSSI/ATS', 'DQD', 'CRL', 'ATGL']
361 status = (ret[0] << 3) + (ret[1] >> 5)
364 self.describe_return_bits(status, NAMES)
365 receiver_enabled = (self.last_power & 0x80) >> 7
369 s = 'Received data in FIFO'
371 s = 'Transmit register ready'
374 self.advance_ann(5, 1)
376 self.putx(5, 1, 'Power on Reset')
378 self.advance_ann(5, 1)
381 s = 'RX FIFO overflow'
383 s = 'Transmit register under run'
386 self.advance_ann(5, 1)
388 self.putx(5, 1, 'Wake-up timer')
390 self.advance_ann(5, 1)
392 self.putx(5, 1, 'External interrupt')
394 self.advance_ann(5, 1)
396 self.putx(5, 1, 'Low battery')
398 self.advance_ann(5, 1)
400 self.putx(5, 1, 'FIFO is empty')
402 self.advance_ann(5, 1)
405 s = 'Incoming signal above limit'
407 s = 'Antenna detected RF signal'
410 self.advance_ann(5, 1)
412 self.putx(5, 1, 'Data quality detector')
414 self.advance_ann(5, 1)
416 self.putx(5, 1, 'Clock recovery locked')
418 self.advance_ann(5, 1)
419 self.advance_ann(5, 1)
421 self.putx(3, 5, ['AFC offset'])
422 if (self.last_status[1] & 0x1F) != (ret[1] & 0x1F):
423 self.putx(5, 5, ['Changed', '~'])
424 self.last_status = ret
426 def handle_cmd(self, cmd, ret):
428 self.handle_configuration_cmd(cmd, ret)
430 self.handle_power_management_cmd(cmd, ret)
431 elif cmd[0] & 0xF0 == 0xA0:
432 self.handle_frequency_setting_cmd(cmd, ret)
434 self.handle_data_rate_cmd(cmd, ret)
435 elif cmd[0] & 0xF8 == 0x90:
436 self.handle_receiver_control_cmd(cmd, ret)
438 self.handle_data_filter_cmd(cmd, ret)
440 self.handle_fifo_and_reset_cmd(cmd, ret)
442 self.handle_synchron_pattern_cmd(cmd, ret)
444 self.handle_fifo_read_cmd(cmd, ret)
446 self.handle_afc_cmd(cmd, ret)
447 elif cmd[0] & 0xFE == 0x98:
448 self.handle_transceiver_control_cmd(cmd, ret)
450 self.handle_pll_setting_cmd(cmd, ret)
452 self.handle_transmitter_register_cmd(cmd, ret)
454 self.handle_software_reset_cmd(cmd, ret)
455 elif cmd[0] & 0xE0 == 0xE0:
456 self.handle_wake_up_timer_cmd(cmd, ret)
458 self.handle_low_duty_cycle_cmd(cmd, ret)
460 self.handle_low_battery_detector_cmd(cmd, ret)
462 self.handle_status_read_cmd(cmd, ret)
464 c = '%02x %02x' % tuple(cmd)
465 r = '%02x %02x' % tuple(ret)
466 self.putx(0, 16, ['Unknown command: %s (reply: %s)!' % (c, r)])
468 def decode(self, ss, es, data):
469 ptype, mosi, miso = data
471 # For now, only use DATA and BITS packets.
472 if ptype not in ('DATA', 'BITS'):
475 # Store the individual bit values and ss/es numbers. The next packet
476 # is guaranteed to be a 'DATA' packet belonging to this 'BITS' one.
479 self.mosi_bits.extend(reversed(mosi))
481 self.miso_bits.extend(reversed(miso))
485 self.mosi_bytes.append(mosi)
486 self.miso_bytes.append(miso)
488 # All commands consist of 2 bytes.
489 if len(self.mosi_bytes) < 2:
492 self.row_pos = [0, 8, 8]
494 self.handle_cmd(self.mosi_bytes, self.miso_bytes)
496 self.mosi_bytes, self.miso_bytes = [], []
497 self.mosi_bits, self.miso_bits = [], []