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