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