]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire_transport/onewire_transport.py
onewire: added transport layer decoder, it does not do much for now
[libsigrokdecode.git] / decoders / onewire_transport / onewire_transport.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 Iztok Jeras <iztok.jeras@gmail.com>
5 ##
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.
10 ##
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.
15 ##
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
19 ##
20
21 # 1-Wire protocol decoder
22
23 import sigrokdecode as srd
24
25 command = {0x44: "TEMPERATURE CONVERSION",
26            0xbe: "READ SCRATCHPAD"}
27
28 class Decoder(srd.Decoder):
29     api_version = 1
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.'
34     license = 'gplv2+'
35     inputs = ['onewire_network']
36     outputs = []
37     probes = []
38     optional_probes = []
39     options = {}
40     annotations = [
41         ['Transport', 'Transport layer events'],
42     ]
43
44     def __init__(self, **kwargs):
45         # Event timing variables
46         self.trn_beg = 0
47         self.trn_end = 0
48         # Transport layer variables
49         self.state   = 'ROM'
50         self.rom     = 0x0000000000000000
51
52     def start(self, metadata):
53         self.out_ann   = self.add(srd.OUTPUT_ANN  , 'onewire_transport')
54
55     def report(self):
56         pass
57
58     def decode(self, ss, es, data):
59         [code, val] = data
60
61         # State machine.
62         if (code == "RESET/PRESENCE"):
63             self.put(ss, es, self.out_ann, [0, ['RESET/PRESENCE: %s' % ('True' if val else 'False')]])
64             self.state = "ROM"
65         elif (code == "ROM"):
66             self.rom = val
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"):
71                     if (val in command):
72                         self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, command[val])]])
73                         self.state = command[val]
74                     else:
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)]])
83             else:
84                 raise Exception('Invalid state: %s' % self.state)