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