]> sigrok.org Git - libsigrokdecode.git/blame - decoders/onewire_network/pd.py
decoders: Add/update tags for each PD.
[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
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
9cfb16e8
IJ
18##
19
9cfb16e8
IJ
20import sigrokdecode as srd
21
6188e4e4 22# Dictionary of ROM commands and their names, next state.
758d2ea9 23command = {
1fe967f2
UH
24 0x33: ['Read ROM' , 'GET ROM' ],
25 0x0f: ['Conditional read ROM' , 'GET ROM' ],
26 0xcc: ['Skip ROM' , 'TRANSPORT' ],
27 0x55: ['Match ROM' , 'GET ROM' ],
28 0xf0: ['Search ROM' , 'SEARCH ROM'],
29 0xec: ['Conditional search ROM', 'SEARCH ROM'],
30 0x3c: ['Overdrive skip ROM' , 'TRANSPORT' ],
31 0x69: ['Overdrive match ROM' , 'GET ROM' ],
758d2ea9 32}
9cfb16e8
IJ
33
34class Decoder(srd.Decoder):
b197383c 35 api_version = 3
9cfb16e8
IJ
36 id = 'onewire_network'
37 name = '1-Wire network layer'
6188e4e4 38 longname = '1-Wire serial communication bus (network layer)'
9cfb16e8
IJ
39 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
40 license = 'gplv2+'
41 inputs = ['onewire_link']
42 outputs = ['onewire_network']
d6d8a8a4 43 tags = ['Embedded/industrial']
da9bcbd9
BV
44 annotations = (
45 ('text', 'Human-readable text'),
46 )
9cfb16e8 47
92b7b49f 48 def __init__(self):
10aeb8ea
GS
49 self.reset()
50
51 def reset(self):
486b19ce
UH
52 self.ss_block = 0
53 self.es_block = 0
6188e4e4 54 self.state = 'COMMAND'
9cfb16e8 55 self.bit_cnt = 0
6188e4e4
UH
56 self.search = 'P'
57 self.data_p = 0x0
58 self.data_n = 0x0
59 self.data = 0x0
48b59746 60 self.rom = 0x0000000000000000
9cfb16e8 61
8915b346 62 def start(self):
c515eed7 63 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 64 self.out_ann = self.register(srd.OUTPUT_ANN)
9cfb16e8 65
6188e4e4
UH
66 def putx(self, data):
67 # Helper function for most annotations.
486b19ce 68 self.put(self.ss_block, self.es_block, self.out_ann, data)
6188e4e4
UH
69
70 def puty(self, data):
71 # Helper function for most protocol packets.
486b19ce 72 self.put(self.ss_block, self.es_block, self.out_python, data)
6188e4e4 73
9cfb16e8 74 def decode(self, ss, es, data):
6188e4e4 75 code, val = data
9cfb16e8
IJ
76
77 # State machine.
6188e4e4
UH
78 if code == 'RESET/PRESENCE':
79 self.search = 'P'
9cfb16e8 80 self.bit_cnt = 0
6188e4e4 81 self.put(ss, es, self.out_ann,
3f302d51 82 [0, ['Reset/presence: %s' % ('true' if val else 'false')]])
c515eed7 83 self.put(ss, es, self.out_python, ['RESET/PRESENCE', val])
6188e4e4 84 self.state = 'COMMAND'
48b59746
UH
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:
3f302d51 96 self.putx([0, ['ROM command: 0x%02x \'%s\''
48b59746
UH
97 % (self.data, command[self.data][0])]])
98 self.state = command[self.data][1]
9cfb16e8 99 else:
3f302d51
UH
100 self.putx([0, ['ROM command: 0x%02x \'%s\''
101 % (self.data, 'unrecognized')]])
48b59746
UH
102 self.state = 'COMMAND ERROR'
103 elif self.state == 'GET ROM':
104 # A 64 bit device address is selected.
3f302d51 105 # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte)
48b59746
UH
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.
3f302d51 114 # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte)
48b59746
UH
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
3f302d51 125 self.putx([0, ['Data: 0x%02x' % self.data]])
48b59746
UH
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
3f302d51 131 self.putx([0, ['ROM error data: 0x%02x' % self.data]])
9cfb16e8 132
48b59746 133 # Data collector.
6188e4e4
UH
134 def onewire_collect(self, length, val, ss, es):
135 # Storing the sample this sequence begins with.
730e07bc 136 if self.bit_cnt == 0:
486b19ce 137 self.ss_block = ss
9cfb16e8 138 self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
6188e4e4
UH
139 self.bit_cnt += 1
140 # Storing the sample this sequence ends with.
141 # In case the full length of the sequence is received, return 1.
142 if self.bit_cnt == length:
486b19ce 143 self.es_block = es
6188e4e4
UH
144 self.data = self.data & ((1 << length) - 1)
145 self.bit_cnt = 0
146 return 1
9cfb16e8 147 else:
6188e4e4 148 return 0
9cfb16e8 149
48b59746 150 # Search collector.
6188e4e4
UH
151 def onewire_search(self, length, val, ss, es):
152 # Storing the sample this sequence begins with.
153 if (self.bit_cnt == 0) and (self.search == 'P'):
486b19ce 154 self.ss_block = ss
6188e4e4
UH
155
156 if self.search == 'P':
157 # Master receives an original address bit.
158 self.data_p = self.data_p & ~(1 << self.bit_cnt) | \
159 (val << self.bit_cnt)
160 self.search = 'N'
161 elif self.search == 'N':
162 # Master receives a complemented address bit.
163 self.data_n = self.data_n & ~(1 << self.bit_cnt) | \
164 (val << self.bit_cnt)
165 self.search = 'D'
166 elif self.search == 'D':
167 # Master transmits an address bit.
168 self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
169 self.search = 'P'
170 self.bit_cnt += 1
171
172 # Storing the sample this sequence ends with.
173 # In case the full length of the sequence is received, return 1.
174 if self.bit_cnt == length:
486b19ce 175 self.es_block = es
6188e4e4
UH
176 self.data_p = self.data_p & ((1 << length) - 1)
177 self.data_n = self.data_n & ((1 << length) - 1)
178 self.data = self.data & ((1 << length) - 1)
179 self.search = 'P'
180 self.bit_cnt = 0
181 return 1
9cfb16e8 182 else:
6188e4e4 183 return 0