]> sigrok.org Git - libsigrokdecode.git/blame - decoders/onewire_network/pd.py
i2c: Add Python tests.
[libsigrokdecode.git] / decoders / onewire_network / pd.py
CommitLineData
9cfb16e8 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
9cfb16e8
IJ
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 = {
1fe967f2
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 0x69: ['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 = [
3f302d51 50 ['Text', 'Human-readable text'],
9cfb16e8
IJ
51 ]
52
53 def __init__(self, **kwargs):
48b59746
UH
54 self.beg = 0
55 self.end = 0
6188e4e4 56 self.state = 'COMMAND'
9cfb16e8 57 self.bit_cnt = 0
6188e4e4
UH
58 self.search = 'P'
59 self.data_p = 0x0
60 self.data_n = 0x0
61 self.data = 0x0
48b59746 62 self.rom = 0x0000000000000000
9cfb16e8 63
8915b346 64 def start(self):
be465111
BV
65 self.out_proto = self.register(srd.OUTPUT_PYTHON)
66 self.out_ann = self.register(srd.OUTPUT_ANN)
9cfb16e8 67
6188e4e4
UH
68 def putx(self, data):
69 # Helper function for most annotations.
48b59746 70 self.put(self.beg, self.end, self.out_ann, data)
6188e4e4
UH
71
72 def puty(self, data):
73 # Helper function for most protocol packets.
48b59746 74 self.put(self.beg, self.end, self.out_proto, data)
6188e4e4 75
9cfb16e8 76 def decode(self, ss, es, data):
6188e4e4 77 code, val = data
9cfb16e8
IJ
78
79 # State machine.
6188e4e4
UH
80 if code == 'RESET/PRESENCE':
81 self.search = 'P'
9cfb16e8 82 self.bit_cnt = 0
6188e4e4 83 self.put(ss, es, self.out_ann,
3f302d51 84 [0, ['Reset/presence: %s' % ('true' if val else 'false')]])
a81787f6 85 self.put(ss, es, self.out_proto, ['RESET/PRESENCE', val])
6188e4e4 86 self.state = 'COMMAND'
48b59746
UH
87 return
88
89 # For now we're only interested in 'RESET/PRESENCE' and 'BIT' packets.
90 if code != 'BIT':
91 return
92
93 if self.state == 'COMMAND':
94 # Receiving and decoding a ROM command.
95 if self.onewire_collect(8, val, ss, es) == 0:
96 return
97 if self.data in command:
3f302d51 98 self.putx([0, ['ROM command: 0x%02x \'%s\''
48b59746
UH
99 % (self.data, command[self.data][0])]])
100 self.state = command[self.data][1]
9cfb16e8 101 else:
3f302d51
UH
102 self.putx([0, ['ROM command: 0x%02x \'%s\''
103 % (self.data, 'unrecognized')]])
48b59746
UH
104 self.state = 'COMMAND ERROR'
105 elif self.state == 'GET ROM':
106 # A 64 bit device address is selected.
3f302d51 107 # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte)
48b59746
UH
108 if self.onewire_collect(64, val, ss, es) == 0:
109 return
110 self.rom = self.data & 0xffffffffffffffff
111 self.putx([0, ['ROM: 0x%016x' % self.rom]])
112 self.puty(['ROM', self.rom])
113 self.state = 'TRANSPORT'
114 elif self.state == 'SEARCH ROM':
115 # A 64 bit device address is searched for.
3f302d51 116 # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte)
48b59746
UH
117 if self.onewire_search(64, val, ss, es) == 0:
118 return
119 self.rom = self.data & 0xffffffffffffffff
120 self.putx([0, ['ROM: 0x%016x' % self.rom]])
121 self.puty(['ROM', self.rom])
122 self.state = 'TRANSPORT'
123 elif self.state == 'TRANSPORT':
124 # The transport layer is handled in byte sized units.
125 if self.onewire_collect(8, val, ss, es) == 0:
126 return
3f302d51 127 self.putx([0, ['Data: 0x%02x' % self.data]])
48b59746
UH
128 self.puty(['DATA', self.data])
129 elif self.state == 'COMMAND ERROR':
130 # Since the command is not recognized, print raw data.
131 if self.onewire_collect(8, val, ss, es) == 0:
132 return
3f302d51 133 self.putx([0, ['ROM error data: 0x%02x' % self.data]])
48b59746
UH
134 else:
135 raise Exception('Invalid state: %s' % self.state)
9cfb16e8 136
48b59746 137 # Data collector.
6188e4e4
UH
138 def onewire_collect(self, length, val, ss, es):
139 # Storing the sample this sequence begins with.
140 if self.bit_cnt == 1:
48b59746 141 self.beg = ss
9cfb16e8 142 self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
6188e4e4
UH
143 self.bit_cnt += 1
144 # Storing the sample this sequence ends with.
145 # In case the full length of the sequence is received, return 1.
146 if self.bit_cnt == length:
48b59746 147 self.end = es
6188e4e4
UH
148 self.data = self.data & ((1 << length) - 1)
149 self.bit_cnt = 0
150 return 1
9cfb16e8 151 else:
6188e4e4 152 return 0
9cfb16e8 153
48b59746 154 # Search collector.
6188e4e4
UH
155 def onewire_search(self, length, val, ss, es):
156 # Storing the sample this sequence begins with.
157 if (self.bit_cnt == 0) and (self.search == 'P'):
48b59746 158 self.beg = ss
6188e4e4
UH
159
160 if self.search == 'P':
161 # Master receives an original address bit.
162 self.data_p = self.data_p & ~(1 << self.bit_cnt) | \
163 (val << self.bit_cnt)
164 self.search = 'N'
165 elif self.search == 'N':
166 # Master receives a complemented address bit.
167 self.data_n = self.data_n & ~(1 << self.bit_cnt) | \
168 (val << self.bit_cnt)
169 self.search = 'D'
170 elif self.search == 'D':
171 # Master transmits an address bit.
172 self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
173 self.search = 'P'
174 self.bit_cnt += 1
175
176 # Storing the sample this sequence ends with.
177 # In case the full length of the sequence is received, return 1.
178 if self.bit_cnt == length:
48b59746 179 self.end = es
6188e4e4
UH
180 self.data_p = self.data_p & ((1 << length) - 1)
181 self.data_n = self.data_n & ((1 << length) - 1)
182 self.data = self.data & ((1 << length) - 1)
183 self.search = 'P'
184 self.bit_cnt = 0
185 return 1
9cfb16e8 186 else:
6188e4e4 187 return 0