]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire_network/pd.py
All PDs: Drop unneeded comments.
[libsigrokdecode.git] / decoders / onewire_network / pd.py
1 ##
2 ## This file is part of the libsigrokdecode 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 import sigrokdecode as srd
22
23 # Dictionary of ROM commands and their names, next state.
24 command = {
25     0x33: ['Read ROM'              , 'GET ROM'   ],
26     0x0f: ['Conditional read ROM'  , 'GET ROM'   ],
27     0xcc: ['Skip ROM'              , 'TRANSPORT' ],
28     0x55: ['Match ROM'             , 'GET ROM'   ],
29     0xf0: ['Search ROM'            , 'SEARCH ROM'],
30     0xec: ['Conditional search ROM', 'SEARCH ROM'],
31     0x3c: ['Overdrive skip ROM'    , 'TRANSPORT' ],
32     0x69: ['Overdrive match ROM'   , 'GET ROM'   ],
33 }
34
35 class Decoder(srd.Decoder):
36     api_version = 1
37     id = 'onewire_network'
38     name = '1-Wire network layer'
39     longname = '1-Wire serial communication bus (network layer)'
40     desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
41     license = 'gplv2+'
42     inputs = ['onewire_link']
43     outputs = ['onewire_network']
44     probes = []
45     optional_probes = []
46     options = {}
47     annotations = [
48         ['Text', 'Human-readable text'],
49     ]
50
51     def __init__(self, **kwargs):
52         self.beg = 0
53         self.end = 0
54         self.state = 'COMMAND'
55         self.bit_cnt = 0
56         self.search = 'P'
57         self.data_p = 0x0
58         self.data_n = 0x0
59         self.data = 0x0
60         self.rom = 0x0000000000000000
61
62     def start(self):
63         self.out_proto = self.register(srd.OUTPUT_PYTHON)
64         self.out_ann = self.register(srd.OUTPUT_ANN)
65
66     def putx(self, data):
67         # Helper function for most annotations.
68         self.put(self.beg, self.end, self.out_ann, data)
69
70     def puty(self, data):
71         # Helper function for most protocol packets.
72         self.put(self.beg, self.end, self.out_proto, data)
73
74     def decode(self, ss, es, data):
75         code, val = data
76
77         # State machine.
78         if code == 'RESET/PRESENCE':
79             self.search = 'P'
80             self.bit_cnt = 0
81             self.put(ss, es, self.out_ann,
82                      [0, ['Reset/presence: %s' % ('true' if val else 'false')]])
83             self.put(ss, es, self.out_proto, ['RESET/PRESENCE', val])
84             self.state = 'COMMAND'
85             return
86
87         # For now we're only interested in 'RESET/PRESENCE' and 'BIT' packets.
88         if code != 'BIT':
89             return
90
91         if self.state == 'COMMAND':
92             # Receiving and decoding a ROM command.
93             if self.onewire_collect(8, val, ss, es) == 0:
94                 return
95             if self.data in command:
96                 self.putx([0, ['ROM command: 0x%02x \'%s\''
97                           % (self.data, command[self.data][0])]])
98                 self.state = command[self.data][1]
99             else:
100                 self.putx([0, ['ROM command: 0x%02x \'%s\''
101                           % (self.data, 'unrecognized')]])
102                 self.state = 'COMMAND ERROR'
103         elif self.state == 'GET ROM':
104             # A 64 bit device address is selected.
105             # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte)
106             if self.onewire_collect(64, val, ss, es) == 0:
107                 return
108             self.rom = self.data & 0xffffffffffffffff
109             self.putx([0, ['ROM: 0x%016x' % self.rom]])
110             self.puty(['ROM', self.rom])
111             self.state = 'TRANSPORT'
112         elif self.state == 'SEARCH ROM':
113             # A 64 bit device address is searched for.
114             # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte)
115             if self.onewire_search(64, val, ss, es) == 0:
116                 return
117             self.rom = self.data & 0xffffffffffffffff
118             self.putx([0, ['ROM: 0x%016x' % self.rom]])
119             self.puty(['ROM', self.rom])
120             self.state = 'TRANSPORT'
121         elif self.state == 'TRANSPORT':
122             # The transport layer is handled in byte sized units.
123             if self.onewire_collect(8, val, ss, es) == 0:
124                 return
125             self.putx([0, ['Data: 0x%02x' % self.data]])
126             self.puty(['DATA', self.data])
127         elif self.state == 'COMMAND ERROR':
128             # Since the command is not recognized, print raw data.
129             if self.onewire_collect(8, val, ss, es) == 0:
130                 return
131             self.putx([0, ['ROM error data: 0x%02x' % self.data]])
132         else:
133             raise Exception('Invalid state: %s' % self.state)
134
135     # Data collector.
136     def onewire_collect(self, length, val, ss, es):
137         # Storing the sample this sequence begins with.
138         if self.bit_cnt == 1:
139             self.beg = ss
140         self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
141         self.bit_cnt += 1
142         # Storing the sample this sequence ends with.
143         # In case the full length of the sequence is received, return 1.
144         if self.bit_cnt == length:
145             self.end = es
146             self.data = self.data & ((1 << length) - 1)
147             self.bit_cnt = 0
148             return 1
149         else:
150             return 0
151
152     # Search collector.
153     def onewire_search(self, length, val, ss, es):
154         # Storing the sample this sequence begins with.
155         if (self.bit_cnt == 0) and (self.search == 'P'):
156             self.beg = ss
157
158         if self.search == 'P':
159             # Master receives an original address bit.
160             self.data_p = self.data_p & ~(1 << self.bit_cnt) | \
161                           (val << self.bit_cnt)
162             self.search = 'N'
163         elif self.search == 'N':
164             # Master receives a complemented address bit.
165             self.data_n = self.data_n & ~(1 << self.bit_cnt) | \
166                           (val << self.bit_cnt)
167             self.search = 'D'
168         elif self.search == 'D':
169             # Master transmits an address bit.
170             self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
171             self.search = 'P'
172             self.bit_cnt += 1
173
174         # Storing the sample this sequence ends with.
175         # In case the full length of the sequence is received, return 1.
176         if self.bit_cnt == length:
177             self.end = es
178             self.data_p = self.data_p & ((1 << length) - 1)
179             self.data_n = self.data_n & ((1 << length) - 1)
180             self.data = self.data & ((1 << length) - 1)
181             self.search = 'P'
182             self.bit_cnt = 0
183             return 1
184         else:
185             return 0