]>
Commit | Line | Data |
---|---|---|
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 = 3 | |
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.reset() | |
49 | ||
50 | def reset(self): | |
51 | self.ss_block = 0 | |
52 | self.es_block = 0 | |
53 | self.state = 'COMMAND' | |
54 | self.bit_cnt = 0 | |
55 | self.search = 'P' | |
56 | self.data_p = 0x0 | |
57 | self.data_n = 0x0 | |
58 | self.data = 0x0 | |
59 | self.rom = 0x0000000000000000 | |
60 | ||
61 | def start(self): | |
62 | self.out_python = self.register(srd.OUTPUT_PYTHON) | |
63 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
64 | ||
65 | def putx(self, data): | |
66 | # Helper function for most annotations. | |
67 | self.put(self.ss_block, self.es_block, self.out_ann, data) | |
68 | ||
69 | def puty(self, data): | |
70 | # Helper function for most protocol packets. | |
71 | self.put(self.ss_block, self.es_block, self.out_python, data) | |
72 | ||
73 | def decode(self, ss, es, data): | |
74 | code, val = data | |
75 | ||
76 | # State machine. | |
77 | if code == 'RESET/PRESENCE': | |
78 | self.search = 'P' | |
79 | self.bit_cnt = 0 | |
80 | self.put(ss, es, self.out_ann, | |
81 | [0, ['Reset/presence: %s' % ('true' if val else 'false')]]) | |
82 | self.put(ss, es, self.out_python, ['RESET/PRESENCE', val]) | |
83 | self.state = 'COMMAND' | |
84 | return | |
85 | ||
86 | # For now we're only interested in 'RESET/PRESENCE' and 'BIT' packets. | |
87 | if code != 'BIT': | |
88 | return | |
89 | ||
90 | if self.state == 'COMMAND': | |
91 | # Receiving and decoding a ROM command. | |
92 | if self.onewire_collect(8, val, ss, es) == 0: | |
93 | return | |
94 | if self.data in command: | |
95 | self.putx([0, ['ROM command: 0x%02x \'%s\'' | |
96 | % (self.data, command[self.data][0])]]) | |
97 | self.state = command[self.data][1] | |
98 | else: | |
99 | self.putx([0, ['ROM command: 0x%02x \'%s\'' | |
100 | % (self.data, 'unrecognized')]]) | |
101 | self.state = 'COMMAND ERROR' | |
102 | elif self.state == 'GET ROM': | |
103 | # A 64 bit device address is selected. | |
104 | # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte) | |
105 | if self.onewire_collect(64, val, ss, es) == 0: | |
106 | return | |
107 | self.rom = self.data & 0xffffffffffffffff | |
108 | self.putx([0, ['ROM: 0x%016x' % self.rom]]) | |
109 | self.puty(['ROM', self.rom]) | |
110 | self.state = 'TRANSPORT' | |
111 | elif self.state == 'SEARCH ROM': | |
112 | # A 64 bit device address is searched for. | |
113 | # Family code (1 byte) + serial number (6 bytes) + CRC (1 byte) | |
114 | if self.onewire_search(64, val, ss, es) == 0: | |
115 | return | |
116 | self.rom = self.data & 0xffffffffffffffff | |
117 | self.putx([0, ['ROM: 0x%016x' % self.rom]]) | |
118 | self.puty(['ROM', self.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) == 0: | |
123 | return | |
124 | self.putx([0, ['Data: 0x%02x' % self.data]]) | |
125 | self.puty(['DATA', self.data]) | |
126 | elif self.state == 'COMMAND ERROR': | |
127 | # Since the command is not recognized, print raw data. | |
128 | if self.onewire_collect(8, val, ss, es) == 0: | |
129 | return | |
130 | self.putx([0, ['ROM error data: 0x%02x' % self.data]]) | |
131 | ||
132 | # Data collector. | |
133 | def onewire_collect(self, length, val, ss, es): | |
134 | # Storing the sample this sequence begins with. | |
135 | if self.bit_cnt == 0: | |
136 | self.ss_block = ss | |
137 | self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt) | |
138 | self.bit_cnt += 1 | |
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: | |
142 | self.es_block = es | |
143 | self.data = self.data & ((1 << length) - 1) | |
144 | self.bit_cnt = 0 | |
145 | return 1 | |
146 | else: | |
147 | return 0 | |
148 | ||
149 | # 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'): | |
153 | self.ss_block = ss | |
154 | ||
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) | |
159 | self.search = 'N' | |
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) | |
164 | self.search = 'D' | |
165 | elif self.search == 'D': | |
166 | # Master transmits an address bit. | |
167 | self.data = self.data & ~(1 << self.bit_cnt) | (val << self.bit_cnt) | |
168 | self.search = 'P' | |
169 | self.bit_cnt += 1 | |
170 | ||
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: | |
174 | self.es_block = es | |
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) | |
178 | self.search = 'P' | |
179 | self.bit_cnt = 0 | |
180 | return 1 | |
181 | else: | |
182 | return 0 |