2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2012 Iztok Jeras <iztok.jeras@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 # 1-Wire protocol decoder
23 import sigrokdecode as srd
25 command = {0x44: "TEMPERATURE CONVERSION",
26 0xbe: "READ SCRATCHPAD"}
28 class Decoder(srd.Decoder):
30 id = 'onewire_transport'
31 name = '1-Wire transport layer'
32 longname = '1-Wire serial communication bus'
33 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
35 inputs = ['onewire_network']
41 ['Transport', 'Transport layer events'],
44 def __init__(self, **kwargs):
45 # Event timing variables
48 # Transport layer variables
50 self.rom = 0x0000000000000000
52 def start(self, metadata):
53 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire_transport')
58 def decode(self, ss, es, data):
62 if (code == "RESET/PRESENCE"):
63 self.put(ss, es, self.out_ann, [0, ['RESET/PRESENCE: %s' % ('True' if val else 'False')]])
67 self.put(ss, es, self.out_ann, [0, ['ROM: 0x%016x' % (val)]])
68 self.state = "COMMAND"
69 elif (code == "DATA"):
70 if (self.state == "COMMAND"):
72 self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, command[val])]])
73 self.state = command[val]
75 self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, 'UNRECOGNIZED')]])
76 self.state = "UNRECOGNIZED"
77 elif (self.state == "READ SCRATCHPAD"):
78 self.put(ss, es, self.out_ann, [0, ['SCRATCHPAD DATA: 0x%02x' % (val)]])
79 elif (self.state == "TEMPERATURE CONVERSION"):
80 self.put(ss, es, self.out_ann, [0, ['TEMPERATURE CONVERSION STATUS: 0x%02x' % (val)]])
81 elif (self.state == "UNRECOGNIZED"):
82 self.put(ss, es, self.out_ann, [0, ['UNRECOGNIZED: 0x%02x' % (val)]])
84 raise Exception('Invalid state: %s' % self.state)