2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2012 Iztok Jeras <iztok.jeras@gmail.com>
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.
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.
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
21 # 1-Wire protocol decoder (network layer)
23 import sigrokdecode as srd
25 # Dictionary of ROM commands and their names, next state.
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' ],
37 class Decoder(srd.Decoder):
39 id = 'onewire_network'
40 name = '1-Wire network layer'
41 longname = '1-Wire serial communication bus (network layer)'
42 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
44 inputs = ['onewire_link']
45 outputs = ['onewire_network']
50 ['Network', 'Network layer events (device addressing)'],
53 def __init__(self, **kwargs):
54 # Event timing variables
57 # Network layer variables
58 self.state = 'COMMAND'
64 self.net_rom = 0x0000000000000000
66 def start(self, metadata):
67 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire_network')
68 self.out_ann = self.add(srd.OUTPUT_ANN, 'onewire_network')
74 # Helper function for most annotations.
75 self.put(self.net_beg, self.net_end, self.out_ann, data)
78 # Helper function for most protocol packets.
79 self.put(self.net_beg, self.net_end, self.out_proto, data)
81 def decode(self, ss, es, data):
85 if code == 'RESET/PRESENCE':
88 self.put(ss, es, self.out_ann,
89 [0, ['RESET/PRESENCE: %s' % ('True' if val else 'False')]])
90 self.put(ss, es, self.out_proto, ['RESET/PRESENCE', val])
91 self.state = 'COMMAND'
93 if self.state == 'COMMAND':
94 # Receiving and decoding a ROM command.
95 if self.onewire_collect(8, val, ss, es):
96 if self.data in command:
97 self.putx([0, ['ROM COMMAND: 0x%02x \'%s\''
98 % (self.data, command[self.data][0])]])
99 self.state = command[self.data][1]
101 self.putx([0, ['ROM COMMAND: 0x%02x \'%s\''
102 % (self.data, 'UNRECOGNIZED')]])
103 self.state = 'COMMAND ERROR'
104 elif self.state == 'GET ROM':
105 # A 64 bit device address is selected.
106 # Family code (1B) + serial number (6B) + CRC (1B)
107 if self.onewire_collect(64, val, ss, es):
108 self.net_rom = self.data & 0xffffffffffffffff
109 self.putx([0, ['ROM: 0x%016x' % self.net_rom]])
110 self.puty(['ROM', self.net_rom])
111 self.state = 'TRANSPORT'
112 elif self.state == 'SEARCH ROM':
113 # A 64 bit device address is searched for.
114 # Family code (1B) + serial number (6B) + CRC (1B)
115 if self.onewire_search(64, val, ss, es):
116 self.net_rom = self.data & 0xffffffffffffffff
117 self.putx([0, ['ROM: 0x%016x' % self.net_rom]])
118 self.puty(['ROM', self.net_rom])
119 self.state = 'TRANSPORT'
120 elif self.state == 'TRANSPORT':
121 # The transport layer is handled in byte sized units.
122 if self.onewire_collect(8, val, ss, es):
123 self.putx([0, ['DATA: 0x%02x' % self.data]])
124 self.puty(['DATA', self.data])
125 elif self.state == 'COMMAND ERROR':
126 # Since the command is not recognized, print raw data.
127 if self.onewire_collect(8, val, ss, es):
128 self.putx([0, ['ROM ERROR DATA: 0x%02x' % self.data]])
130 raise Exception('Invalid state: %s' % self.state)
132 # Link/Network layer data collector.
133 def onewire_collect(self, length, val, ss, es):
134 # Storing the sample this sequence begins with.
135 if self.bit_cnt == 1:
137 self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
139 # Storing the sample this sequence ends with.
140 # In case the full length of the sequence is received, return 1.
141 if self.bit_cnt == length:
143 self.data = self.data & ((1 << length) - 1)
149 # Link/Network layer search collector.
150 def onewire_search(self, length, val, ss, es):
151 # Storing the sample this sequence begins with.
152 if (self.bit_cnt == 0) and (self.search == 'P'):
155 if self.search == 'P':
156 # Master receives an original address bit.
157 self.data_p = self.data_p & ~(1 << self.bit_cnt) | \
158 (val << self.bit_cnt)
160 elif self.search == 'N':
161 # Master receives a complemented address bit.
162 self.data_n = self.data_n & ~(1 << self.bit_cnt) | \
163 (val << self.bit_cnt)
165 elif self.search == 'D':
166 # Master transmits an address bit.
167 self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt)
171 # Storing the sample this sequence ends with.
172 # In case the full length of the sequence is received, return 1.
173 if self.bit_cnt == length:
175 self.data_p = self.data_p & ((1 << length) - 1)
176 self.data_n = self.data_n & ((1 << length) - 1)
177 self.data = self.data & ((1 << length) - 1)