]>
Commit | Line | Data |
---|---|---|
f44d2db2 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
f44d2db2 | 3 | ## |
0bb7bcf3 | 4 | ## Copyright (C) 2011-2014 Uwe Hermann <uwe@hermann-uwe.de> |
f44d2db2 UH |
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 | ||
677d597b | 21 | import sigrokdecode as srd |
f44d2db2 | 22 | |
4cace3b8 | 23 | ''' |
c515eed7 | 24 | OUTPUT_PYTHON format: |
4cace3b8 | 25 | |
bf69977d UH |
26 | Packet: |
27 | [<ptype>, <rxtx>, <pdata>] | |
4cace3b8 | 28 | |
bf69977d | 29 | This is the list of <ptype>s and their respective <pdata> values: |
4cace3b8 UH |
30 | - 'STARTBIT': The data is the (integer) value of the start bit (0/1). |
31 | - 'DATA': The data is the (integer) value of the UART data. Valid values | |
32 | range from 0 to 512 (as the data can be up to 9 bits in size). | |
4aedd5b8 | 33 | - 'DATABITS': List of data bits and their ss/es numbers. |
4cace3b8 UH |
34 | - 'PARITYBIT': The data is the (integer) value of the parity bit (0/1). |
35 | - 'STOPBIT': The data is the (integer) value of the stop bit (0 or 1). | |
36 | - 'INVALID STARTBIT': The data is the (integer) value of the start bit (0/1). | |
37 | - 'INVALID STOPBIT': The data is the (integer) value of the stop bit (0/1). | |
38 | - 'PARITY ERROR': The data is a tuple with two entries. The first one is | |
39 | the expected parity value, the second is the actual parity value. | |
40 | - TODO: Frame error? | |
41 | ||
42 | The <rxtx> field is 0 for RX packets, 1 for TX packets. | |
43 | ''' | |
44 | ||
97cca21f UH |
45 | # Used for differentiating between the two data directions. |
46 | RX = 0 | |
47 | TX = 1 | |
48 | ||
f44d2db2 UH |
49 | # Given a parity type to check (odd, even, zero, one), the value of the |
50 | # parity bit, the value of the data, and the length of the data (5-9 bits, | |
51 | # usually 8 bits) return True if the parity is correct, False otherwise. | |
a7fc4c34 | 52 | # 'none' is _not_ allowed as value for 'parity_type'. |
f44d2db2 UH |
53 | def parity_ok(parity_type, parity_bit, data, num_data_bits): |
54 | ||
55 | # Handle easy cases first (parity bit is always 1 or 0). | |
a7fc4c34 | 56 | if parity_type == 'zero': |
f44d2db2 | 57 | return parity_bit == 0 |
a7fc4c34 | 58 | elif parity_type == 'one': |
f44d2db2 UH |
59 | return parity_bit == 1 |
60 | ||
61 | # Count number of 1 (high) bits in the data (and the parity bit itself!). | |
ac941bf9 | 62 | ones = bin(data).count('1') + parity_bit |
f44d2db2 UH |
63 | |
64 | # Check for odd/even parity. | |
a7fc4c34 | 65 | if parity_type == 'odd': |
ac941bf9 | 66 | return (ones % 2) == 1 |
a7fc4c34 | 67 | elif parity_type == 'even': |
ac941bf9 | 68 | return (ones % 2) == 0 |
f44d2db2 | 69 | |
21cda951 UH |
70 | class SamplerateError(Exception): |
71 | pass | |
72 | ||
f04964c6 UH |
73 | class ChannelError(Exception): |
74 | pass | |
75 | ||
677d597b | 76 | class Decoder(srd.Decoder): |
12851357 | 77 | api_version = 2 |
f44d2db2 UH |
78 | id = 'uart' |
79 | name = 'UART' | |
3d3da57d | 80 | longname = 'Universal Asynchronous Receiver/Transmitter' |
a465436e | 81 | desc = 'Asynchronous, serial bus.' |
f44d2db2 UH |
82 | license = 'gplv2+' |
83 | inputs = ['logic'] | |
84 | outputs = ['uart'] | |
6a15597a | 85 | optional_channels = ( |
f44d2db2 UH |
86 | # Allow specifying only one of the signals, e.g. if only one data |
87 | # direction exists (or is relevant). | |
29ed0f4c UH |
88 | {'id': 'rx', 'name': 'RX', 'desc': 'UART receive line'}, |
89 | {'id': 'tx', 'name': 'TX', 'desc': 'UART transmit line'}, | |
da9bcbd9 | 90 | ) |
84c1c0b5 BV |
91 | options = ( |
92 | {'id': 'baudrate', 'desc': 'Baud rate', 'default': 115200}, | |
93 | {'id': 'num_data_bits', 'desc': 'Data bits', 'default': 8, | |
94 | 'values': (5, 6, 7, 8, 9)}, | |
95 | {'id': 'parity_type', 'desc': 'Parity type', 'default': 'none', | |
96 | 'values': ('none', 'odd', 'even', 'zero', 'one')}, | |
97 | {'id': 'parity_check', 'desc': 'Check parity?', 'default': 'yes', | |
98 | 'values': ('yes', 'no')}, | |
99 | {'id': 'num_stop_bits', 'desc': 'Stop bits', 'default': 1.0, | |
100 | 'values': (0.0, 0.5, 1.0, 1.5)}, | |
101 | {'id': 'bit_order', 'desc': 'Bit order', 'default': 'lsb-first', | |
102 | 'values': ('lsb-first', 'msb-first')}, | |
103 | {'id': 'format', 'desc': 'Data format', 'default': 'ascii', | |
104 | 'values': ('ascii', 'dec', 'hex', 'oct', 'bin')}, | |
f44d2db2 | 105 | # TODO: Options to invert the signal(s). |
84c1c0b5 | 106 | ) |
da9bcbd9 BV |
107 | annotations = ( |
108 | ('rx-data', 'RX data'), | |
109 | ('tx-data', 'TX data'), | |
110 | ('rx-start', 'RX start bits'), | |
111 | ('tx-start', 'TX start bits'), | |
112 | ('rx-parity-ok', 'RX parity OK bits'), | |
113 | ('tx-parity-ok', 'TX parity OK bits'), | |
114 | ('rx-parity-err', 'RX parity error bits'), | |
115 | ('tx-parity-err', 'TX parity error bits'), | |
116 | ('rx-stop', 'RX stop bits'), | |
117 | ('tx-stop', 'TX stop bits'), | |
118 | ('rx-warnings', 'RX warnings'), | |
119 | ('tx-warnings', 'TX warnings'), | |
120 | ('rx-data-bits', 'RX data bits'), | |
121 | ('tx-data-bits', 'TX data bits'), | |
122 | ) | |
2ce20a91 | 123 | annotation_rows = ( |
4e3b276a | 124 | ('rx-data', 'RX', (0, 2, 4, 6, 8)), |
4aedd5b8 | 125 | ('rx-data-bits', 'RX bits', (12,)), |
4e3b276a | 126 | ('rx-warnings', 'RX warnings', (10,)), |
4aedd5b8 UH |
127 | ('tx-data', 'TX', (1, 3, 5, 7, 9)), |
128 | ('tx-data-bits', 'TX bits', (13,)), | |
4e3b276a | 129 | ('tx-warnings', 'TX warnings', (11,)), |
2ce20a91 | 130 | ) |
0bb7bcf3 UH |
131 | binary = ( |
132 | ('rx', 'RX dump'), | |
133 | ('tx', 'TX dump'), | |
134 | ('rxtx', 'RX/TX dump'), | |
135 | ) | |
f44d2db2 | 136 | |
97cca21f | 137 | def putx(self, rxtx, data): |
15ac6604 UH |
138 | s, halfbit = self.startsample[rxtx], int(self.bit_width / 2) |
139 | self.put(s - halfbit, self.samplenum + halfbit, self.out_ann, data) | |
140 | ||
4aedd5b8 UH |
141 | def putpx(self, rxtx, data): |
142 | s, halfbit = self.startsample[rxtx], int(self.bit_width / 2) | |
143 | self.put(s - halfbit, self.samplenum + halfbit, self.out_python, data) | |
144 | ||
15ac6604 UH |
145 | def putg(self, data): |
146 | s, halfbit = self.samplenum, int(self.bit_width / 2) | |
147 | self.put(s - halfbit, s + halfbit, self.out_ann, data) | |
148 | ||
149 | def putp(self, data): | |
150 | s, halfbit = self.samplenum, int(self.bit_width / 2) | |
c515eed7 | 151 | self.put(s - halfbit, s + halfbit, self.out_python, data) |
97cca21f | 152 | |
0bb7bcf3 UH |
153 | def putbin(self, rxtx, data): |
154 | s, halfbit = self.startsample[rxtx], int(self.bit_width / 2) | |
155 | self.put(s - halfbit, self.samplenum + halfbit, self.out_bin, data) | |
156 | ||
f44d2db2 | 157 | def __init__(self, **kwargs): |
f372d597 | 158 | self.samplerate = None |
f44d2db2 | 159 | self.samplenum = 0 |
97cca21f UH |
160 | self.frame_start = [-1, -1] |
161 | self.startbit = [-1, -1] | |
162 | self.cur_data_bit = [0, 0] | |
163 | self.databyte = [0, 0] | |
1ccef461 | 164 | self.paritybit = [-1, -1] |
97cca21f UH |
165 | self.stopbit1 = [-1, -1] |
166 | self.startsample = [-1, -1] | |
2b716038 | 167 | self.state = ['WAIT FOR START BIT', 'WAIT FOR START BIT'] |
83be7b83 UH |
168 | self.oldbit = [1, 1] |
169 | self.oldpins = [1, 1] | |
4aedd5b8 | 170 | self.databits = [[], []] |
f44d2db2 | 171 | |
f372d597 | 172 | def start(self): |
c515eed7 | 173 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
0bb7bcf3 | 174 | self.out_bin = self.register(srd.OUTPUT_BINARY) |
be465111 | 175 | self.out_ann = self.register(srd.OUTPUT_ANN) |
f44d2db2 | 176 | |
f372d597 BV |
177 | def metadata(self, key, value): |
178 | if key == srd.SRD_CONF_SAMPLERATE: | |
35b380b1 | 179 | self.samplerate = value |
f372d597 BV |
180 | # The width of one UART bit in number of samples. |
181 | self.bit_width = float(self.samplerate) / float(self.options['baudrate']) | |
f44d2db2 | 182 | |
f44d2db2 | 183 | # Return true if we reached the middle of the desired bit, false otherwise. |
97cca21f | 184 | def reached_bit(self, rxtx, bitnum): |
f44d2db2 UH |
185 | # bitpos is the samplenumber which is in the middle of the |
186 | # specified UART bit (0 = start bit, 1..x = data, x+1 = parity bit | |
187 | # (if used) or the first stop bit, and so on). | |
97cca21f | 188 | bitpos = self.frame_start[rxtx] + (self.bit_width / 2.0) |
f44d2db2 UH |
189 | bitpos += bitnum * self.bit_width |
190 | if self.samplenum >= bitpos: | |
191 | return True | |
192 | return False | |
193 | ||
97cca21f UH |
194 | def reached_bit_last(self, rxtx, bitnum): |
195 | bitpos = self.frame_start[rxtx] + ((bitnum + 1) * self.bit_width) | |
f44d2db2 UH |
196 | if self.samplenum >= bitpos: |
197 | return True | |
198 | return False | |
199 | ||
97cca21f | 200 | def wait_for_start_bit(self, rxtx, old_signal, signal): |
f44d2db2 UH |
201 | # The start bit is always 0 (low). As the idle UART (and the stop bit) |
202 | # level is 1 (high), the beginning of a start bit is a falling edge. | |
203 | if not (old_signal == 1 and signal == 0): | |
204 | return | |
205 | ||
206 | # Save the sample number where the start bit begins. | |
97cca21f | 207 | self.frame_start[rxtx] = self.samplenum |
f44d2db2 | 208 | |
2b716038 | 209 | self.state[rxtx] = 'GET START BIT' |
f44d2db2 | 210 | |
97cca21f | 211 | def get_start_bit(self, rxtx, signal): |
f44d2db2 | 212 | # Skip samples until we're in the middle of the start bit. |
97cca21f | 213 | if not self.reached_bit(rxtx, 0): |
1bb57ab8 | 214 | return |
f44d2db2 | 215 | |
97cca21f | 216 | self.startbit[rxtx] = signal |
f44d2db2 | 217 | |
5cc4b6a0 | 218 | # The startbit must be 0. If not, we report an error. |
97cca21f | 219 | if self.startbit[rxtx] != 0: |
15ac6604 | 220 | self.putp(['INVALID STARTBIT', rxtx, self.startbit[rxtx]]) |
5cc4b6a0 | 221 | # TODO: Abort? Ignore rest of the frame? |
f44d2db2 | 222 | |
97cca21f UH |
223 | self.cur_data_bit[rxtx] = 0 |
224 | self.databyte[rxtx] = 0 | |
225 | self.startsample[rxtx] = -1 | |
f44d2db2 | 226 | |
2b716038 | 227 | self.state[rxtx] = 'GET DATA BITS' |
f44d2db2 | 228 | |
15ac6604 | 229 | self.putp(['STARTBIT', rxtx, self.startbit[rxtx]]) |
2ce20a91 | 230 | self.putg([rxtx + 2, ['Start bit', 'Start', 'S']]) |
f44d2db2 | 231 | |
97cca21f | 232 | def get_data_bits(self, rxtx, signal): |
f44d2db2 | 233 | # Skip samples until we're in the middle of the desired data bit. |
97cca21f | 234 | if not self.reached_bit(rxtx, self.cur_data_bit[rxtx] + 1): |
1bb57ab8 | 235 | return |
f44d2db2 | 236 | |
15ac6604 | 237 | # Save the sample number of the middle of the first data bit. |
97cca21f UH |
238 | if self.startsample[rxtx] == -1: |
239 | self.startsample[rxtx] = self.samplenum | |
f44d2db2 UH |
240 | |
241 | # Get the next data bit in LSB-first or MSB-first fashion. | |
a7fc4c34 | 242 | if self.options['bit_order'] == 'lsb-first': |
97cca21f | 243 | self.databyte[rxtx] >>= 1 |
fd4aa8aa UH |
244 | self.databyte[rxtx] |= \ |
245 | (signal << (self.options['num_data_bits'] - 1)) | |
22fc7ace | 246 | else: |
97cca21f UH |
247 | self.databyte[rxtx] <<= 1 |
248 | self.databyte[rxtx] |= (signal << 0) | |
f44d2db2 | 249 | |
4aedd5b8 UH |
250 | self.putg([rxtx + 12, ['%d' % signal]]) |
251 | ||
252 | # Store individual data bits and their start/end samplenumbers. | |
253 | s, halfbit = self.samplenum, int(self.bit_width / 2) | |
254 | self.databits[rxtx].append([signal, s - halfbit, s + halfbit]) | |
255 | ||
f44d2db2 | 256 | # Return here, unless we already received all data bits. |
4a04ece4 | 257 | if self.cur_data_bit[rxtx] < self.options['num_data_bits'] - 1: |
97cca21f | 258 | self.cur_data_bit[rxtx] += 1 |
1bb57ab8 | 259 | return |
f44d2db2 | 260 | |
2b716038 | 261 | self.state[rxtx] = 'GET PARITY BIT' |
f44d2db2 | 262 | |
4aedd5b8 UH |
263 | self.putpx(rxtx, ['DATABITS', rxtx, self.databits[rxtx]]) |
264 | self.putpx(rxtx, ['DATA', rxtx, self.databyte[rxtx]]) | |
f44d2db2 | 265 | |
3006c663 UH |
266 | b, f = self.databyte[rxtx], self.options['format'] |
267 | if f == 'ascii': | |
e0a0123d | 268 | c = chr(b) if b in range(30, 126 + 1) else '[%02X]' % b |
8705ddc8 | 269 | self.putx(rxtx, [rxtx, [c]]) |
3006c663 | 270 | elif f == 'dec': |
6d6b32d6 | 271 | self.putx(rxtx, [rxtx, [str(b)]]) |
3006c663 | 272 | elif f == 'hex': |
6d6b32d6 | 273 | self.putx(rxtx, [rxtx, [hex(b)[2:].zfill(2).upper()]]) |
3006c663 | 274 | elif f == 'oct': |
6d6b32d6 | 275 | self.putx(rxtx, [rxtx, [oct(b)[2:].zfill(3)]]) |
3006c663 | 276 | elif f == 'bin': |
6d6b32d6 | 277 | self.putx(rxtx, [rxtx, [bin(b)[2:].zfill(8)]]) |
f44d2db2 | 278 | |
0bb7bcf3 UH |
279 | self.putbin(rxtx, (rxtx, bytes([b]))) |
280 | self.putbin(rxtx, (2, bytes([b]))) | |
281 | ||
4aedd5b8 UH |
282 | self.databits = [[], []] |
283 | ||
97cca21f | 284 | def get_parity_bit(self, rxtx, signal): |
f44d2db2 | 285 | # If no parity is used/configured, skip to the next state immediately. |
a7fc4c34 | 286 | if self.options['parity_type'] == 'none': |
2b716038 | 287 | self.state[rxtx] = 'GET STOP BITS' |
1bb57ab8 | 288 | return |
f44d2db2 UH |
289 | |
290 | # Skip samples until we're in the middle of the parity bit. | |
4a04ece4 | 291 | if not self.reached_bit(rxtx, self.options['num_data_bits'] + 1): |
1bb57ab8 | 292 | return |
f44d2db2 | 293 | |
97cca21f | 294 | self.paritybit[rxtx] = signal |
f44d2db2 | 295 | |
2b716038 | 296 | self.state[rxtx] = 'GET STOP BITS' |
f44d2db2 | 297 | |
ac941bf9 | 298 | if parity_ok(self.options['parity_type'], self.paritybit[rxtx], |
4a04ece4 | 299 | self.databyte[rxtx], self.options['num_data_bits']): |
15ac6604 | 300 | self.putp(['PARITYBIT', rxtx, self.paritybit[rxtx]]) |
2ce20a91 | 301 | self.putg([rxtx + 4, ['Parity bit', 'Parity', 'P']]) |
f44d2db2 | 302 | else: |
61132abd | 303 | # TODO: Return expected/actual parity values. |
15ac6604 | 304 | self.putp(['PARITY ERROR', rxtx, (0, 1)]) # FIXME: Dummy tuple... |
4e3b276a | 305 | self.putg([rxtx + 6, ['Parity error', 'Parity err', 'PE']]) |
f44d2db2 UH |
306 | |
307 | # TODO: Currently only supports 1 stop bit. | |
97cca21f | 308 | def get_stop_bits(self, rxtx, signal): |
f44d2db2 | 309 | # Skip samples until we're in the middle of the stop bit(s). |
a7fc4c34 | 310 | skip_parity = 0 if self.options['parity_type'] == 'none' else 1 |
4a04ece4 UH |
311 | b = self.options['num_data_bits'] + 1 + skip_parity |
312 | if not self.reached_bit(rxtx, b): | |
1bb57ab8 | 313 | return |
f44d2db2 | 314 | |
97cca21f | 315 | self.stopbit1[rxtx] = signal |
f44d2db2 | 316 | |
5cc4b6a0 | 317 | # Stop bits must be 1. If not, we report an error. |
97cca21f | 318 | if self.stopbit1[rxtx] != 1: |
15ac6604 | 319 | self.putp(['INVALID STOPBIT', rxtx, self.stopbit1[rxtx]]) |
4e3b276a | 320 | self.putg([rxtx + 8, ['Frame error', 'Frame err', 'FE']]) |
5cc4b6a0 | 321 | # TODO: Abort? Ignore the frame? Other? |
f44d2db2 | 322 | |
2b716038 | 323 | self.state[rxtx] = 'WAIT FOR START BIT' |
f44d2db2 | 324 | |
15ac6604 | 325 | self.putp(['STOPBIT', rxtx, self.stopbit1[rxtx]]) |
2ce20a91 | 326 | self.putg([rxtx + 4, ['Stop bit', 'Stop', 'T']]) |
f44d2db2 | 327 | |
decde15e | 328 | def decode(self, ss, es, data): |
21cda951 UH |
329 | if not self.samplerate: |
330 | raise SamplerateError('Cannot decode without samplerate.') | |
2fcd7c22 UH |
331 | for (self.samplenum, pins) in data: |
332 | ||
b0827236 UH |
333 | # Note: Ignoring identical samples here for performance reasons |
334 | # is not possible for this PD, at least not in the current state. | |
335 | # if self.oldpins == pins: | |
336 | # continue | |
2fcd7c22 | 337 | self.oldpins, (rx, tx) = pins, pins |
f44d2db2 | 338 | |
3dd546c1 UH |
339 | # Either RX or TX (but not both) can be omitted. |
340 | has_pin = [rx in (0, 1), tx in (0, 1)] | |
341 | if has_pin == [False, False]: | |
f04964c6 | 342 | raise ChannelError('Either TX or RX (or both) pins required.') |
3dd546c1 | 343 | |
f44d2db2 | 344 | # State machine. |
97cca21f | 345 | for rxtx in (RX, TX): |
3dd546c1 UH |
346 | # Don't try to handle RX (or TX) if not supplied. |
347 | if not has_pin[rxtx]: | |
348 | continue | |
349 | ||
97cca21f UH |
350 | signal = rx if (rxtx == RX) else tx |
351 | ||
2b716038 | 352 | if self.state[rxtx] == 'WAIT FOR START BIT': |
97cca21f | 353 | self.wait_for_start_bit(rxtx, self.oldbit[rxtx], signal) |
2b716038 | 354 | elif self.state[rxtx] == 'GET START BIT': |
97cca21f | 355 | self.get_start_bit(rxtx, signal) |
2b716038 | 356 | elif self.state[rxtx] == 'GET DATA BITS': |
97cca21f | 357 | self.get_data_bits(rxtx, signal) |
2b716038 | 358 | elif self.state[rxtx] == 'GET PARITY BIT': |
97cca21f | 359 | self.get_parity_bit(rxtx, signal) |
2b716038 | 360 | elif self.state[rxtx] == 'GET STOP BITS': |
97cca21f | 361 | self.get_stop_bits(rxtx, signal) |
97cca21f UH |
362 | |
363 | # Save current RX/TX values for the next round. | |
364 | self.oldbit[rxtx] = signal |