]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire_transport/onewire_transport.py
67f5193deda48e76f79c4b336dc30204c598b325
[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 # a dictionary of FUNCTION commands and their names
26 command = {0x44: "TEMPERATURE CONVERSION",
27            0xbe: "READ SCRATCHPAD"}
28
29 class Decoder(srd.Decoder):
30     api_version = 1
31     id = 'onewire_transport'
32     name = '1-Wire transport layer'
33     longname = '1-Wire serial communication bus'
34     desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
35     license = 'gplv2+'
36     inputs = ['onewire_network']
37     outputs = []
38     probes = []
39     optional_probes = []
40     options = {}
41     annotations = [
42         ['Transport', 'Transport layer events'],
43     ]
44
45     def __init__(self, **kwargs):
46         # Event timing variables
47         self.trn_beg = 0
48         self.trn_end = 0
49         # Transport layer variables
50         self.state   = 'ROM'
51         self.rom     = 0x0000000000000000
52
53     def start(self, metadata):
54         self.out_ann   = self.add(srd.OUTPUT_ANN  , 'onewire_transport')
55
56     def report(self):
57         pass
58
59     def decode(self, ss, es, data):
60         [code, val] = data
61
62         # State machine.
63         if (code == "RESET/PRESENCE"):
64             self.put(ss, es, self.out_ann, [0, ['RESET/PRESENCE: %s' % ('True' if val else 'False')]])
65             self.state = "ROM"
66         elif (code == "ROM"):
67             self.rom = val
68             self.put(ss, es, self.out_ann, [0, ['ROM: 0x%016x' % (val)]])
69             self.state = "COMMAND"
70         elif (code == "DATA"):
71             if (self.state == "COMMAND"):
72                     if (val in command):
73                         self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, command[val])]])
74                         self.state = command[val]
75                     else:
76                         self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, 'UNRECOGNIZED')]])
77                         self.state = "UNRECOGNIZED"
78             elif (self.state == "READ SCRATCHPAD"):
79                 self.put(ss, es, self.out_ann, [0, ['SCRATCHPAD DATA: 0x%02x' % (val)]])
80             elif (self.state == "TEMPERATURE CONVERSION"):
81                 self.put(ss, es, self.out_ann, [0, ['TEMPERATURE CONVERSION STATUS: 0x%02x' % (val)]])
82             elif (self.state == "UNRECOGNIZED"):
83                 self.put(ss, es, self.out_ann, [0, ['UNRECOGNIZED: 0x%02x' % (val)]])
84             else:
85                 raise Exception('Invalid state: %s' % self.state)