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