blob: 57031d73040643565cd6a6d5766bbc9058d3f0af [file] [log] [blame]
Uwe Hermannf44d2db2011-12-07 02:03:25 +01001##
Uwe Hermann50bd5d22013-04-23 22:27:20 +02002## This file is part of the libsigrokdecode project.
Uwe Hermannf44d2db2011-12-07 02:03:25 +01003##
Uwe Hermann0bb7bcf2014-01-20 17:43:01 +01004## Copyright (C) 2011-2014 Uwe Hermann <uwe@hermann-uwe.de>
Uwe Hermannf44d2db2011-12-07 02:03:25 +01005##
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
Uwe Hermann677d5972012-01-10 21:10:23 +010021import sigrokdecode as srd
Uwe Hermannf44d2db2011-12-07 02:03:25 +010022
Uwe Hermann4cace3b2013-09-12 09:07:53 +020023'''
Uwe Hermannc515eed2014-01-30 22:37:29 +010024OUTPUT_PYTHON format:
Uwe Hermann4cace3b2013-09-12 09:07:53 +020025
26UART packet:
27[<packet-type>, <rxtx>, <packet-data>]
28
29This is the list of <packet-type>s and their respective <packet-data>:
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).
Uwe Hermann4aedd5b2014-02-12 22:55:37 +010033 - 'DATABITS': List of data bits and their ss/es numbers.
Uwe Hermann4cace3b2013-09-12 09:07:53 +020034 - '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
42The <rxtx> field is 0 for RX packets, 1 for TX packets.
43'''
44
Uwe Hermann97cca212012-01-14 22:51:07 +010045# Used for differentiating between the two data directions.
46RX = 0
47TX = 1
48
Uwe Hermannf44d2db2011-12-07 02:03:25 +010049# 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.
Uwe Hermanna7fc4c32012-02-01 19:47:50 +010052# 'none' is _not_ allowed as value for 'parity_type'.
Uwe Hermannf44d2db2011-12-07 02:03:25 +010053def parity_ok(parity_type, parity_bit, data, num_data_bits):
54
55 # Handle easy cases first (parity bit is always 1 or 0).
Uwe Hermanna7fc4c32012-02-01 19:47:50 +010056 if parity_type == 'zero':
Uwe Hermannf44d2db2011-12-07 02:03:25 +010057 return parity_bit == 0
Uwe Hermanna7fc4c32012-02-01 19:47:50 +010058 elif parity_type == 'one':
Uwe Hermannf44d2db2011-12-07 02:03:25 +010059 return parity_bit == 1
60
61 # Count number of 1 (high) bits in the data (and the parity bit itself!).
Uwe Hermannac941bf2012-01-25 22:25:25 +010062 ones = bin(data).count('1') + parity_bit
Uwe Hermannf44d2db2011-12-07 02:03:25 +010063
64 # Check for odd/even parity.
Uwe Hermanna7fc4c32012-02-01 19:47:50 +010065 if parity_type == 'odd':
Uwe Hermannac941bf2012-01-25 22:25:25 +010066 return (ones % 2) == 1
Uwe Hermanna7fc4c32012-02-01 19:47:50 +010067 elif parity_type == 'even':
Uwe Hermannac941bf2012-01-25 22:25:25 +010068 return (ones % 2) == 0
Uwe Hermannf44d2db2011-12-07 02:03:25 +010069 else:
70 raise Exception('Invalid parity type: %d' % parity_type)
71
Uwe Hermann677d5972012-01-10 21:10:23 +010072class Decoder(srd.Decoder):
Uwe Hermanna2c2afd2012-01-15 20:41:46 +010073 api_version = 1
Uwe Hermannf44d2db2011-12-07 02:03:25 +010074 id = 'uart'
75 name = 'UART'
Uwe Hermann3d3da572012-01-14 21:41:41 +010076 longname = 'Universal Asynchronous Receiver/Transmitter'
Uwe Hermanna4654362012-05-09 00:35:30 +020077 desc = 'Asynchronous, serial bus.'
Uwe Hermannf44d2db2011-12-07 02:03:25 +010078 license = 'gplv2+'
79 inputs = ['logic']
80 outputs = ['uart']
Uwe Hermann6a155972014-04-13 19:57:43 +020081 optional_channels = (
Uwe Hermannf44d2db2011-12-07 02:03:25 +010082 # Allow specifying only one of the signals, e.g. if only one data
83 # direction exists (or is relevant).
Uwe Hermann29ed0f42012-01-05 16:27:15 +010084 {'id': 'rx', 'name': 'RX', 'desc': 'UART receive line'},
85 {'id': 'tx', 'name': 'TX', 'desc': 'UART transmit line'},
Bert Vermeulenda9bcbd2014-03-10 12:23:38 +010086 )
Bert Vermeulen84c1c0b2014-03-09 23:48:27 +010087 options = (
88 {'id': 'baudrate', 'desc': 'Baud rate', 'default': 115200},
89 {'id': 'num_data_bits', 'desc': 'Data bits', 'default': 8,
90 'values': (5, 6, 7, 8, 9)},
91 {'id': 'parity_type', 'desc': 'Parity type', 'default': 'none',
92 'values': ('none', 'odd', 'even', 'zero', 'one')},
93 {'id': 'parity_check', 'desc': 'Check parity?', 'default': 'yes',
94 'values': ('yes', 'no')},
95 {'id': 'num_stop_bits', 'desc': 'Stop bits', 'default': 1.0,
96 'values': (0.0, 0.5, 1.0, 1.5)},
97 {'id': 'bit_order', 'desc': 'Bit order', 'default': 'lsb-first',
98 'values': ('lsb-first', 'msb-first')},
99 {'id': 'format', 'desc': 'Data format', 'default': 'ascii',
100 'values': ('ascii', 'dec', 'hex', 'oct', 'bin')},
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100101 # TODO: Options to invert the signal(s).
Bert Vermeulen84c1c0b2014-03-09 23:48:27 +0100102 )
Bert Vermeulenda9bcbd2014-03-10 12:23:38 +0100103 annotations = (
104 ('rx-data', 'RX data'),
105 ('tx-data', 'TX data'),
106 ('rx-start', 'RX start bits'),
107 ('tx-start', 'TX start bits'),
108 ('rx-parity-ok', 'RX parity OK bits'),
109 ('tx-parity-ok', 'TX parity OK bits'),
110 ('rx-parity-err', 'RX parity error bits'),
111 ('tx-parity-err', 'TX parity error bits'),
112 ('rx-stop', 'RX stop bits'),
113 ('tx-stop', 'TX stop bits'),
114 ('rx-warnings', 'RX warnings'),
115 ('tx-warnings', 'TX warnings'),
116 ('rx-data-bits', 'RX data bits'),
117 ('tx-data-bits', 'TX data bits'),
118 )
Uwe Hermann2ce20a92014-01-30 22:26:39 +0100119 annotation_rows = (
Uwe Hermann4e3b2762014-02-01 18:20:45 +0100120 ('rx-data', 'RX', (0, 2, 4, 6, 8)),
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100121 ('rx-data-bits', 'RX bits', (12,)),
Uwe Hermann4e3b2762014-02-01 18:20:45 +0100122 ('rx-warnings', 'RX warnings', (10,)),
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100123 ('tx-data', 'TX', (1, 3, 5, 7, 9)),
124 ('tx-data-bits', 'TX bits', (13,)),
Uwe Hermann4e3b2762014-02-01 18:20:45 +0100125 ('tx-warnings', 'TX warnings', (11,)),
Uwe Hermann2ce20a92014-01-30 22:26:39 +0100126 )
Uwe Hermann0bb7bcf2014-01-20 17:43:01 +0100127 binary = (
128 ('rx', 'RX dump'),
129 ('tx', 'TX dump'),
130 ('rxtx', 'RX/TX dump'),
131 )
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100132
Uwe Hermann97cca212012-01-14 22:51:07 +0100133 def putx(self, rxtx, data):
Uwe Hermann15ac6602013-09-11 19:15:51 +0200134 s, halfbit = self.startsample[rxtx], int(self.bit_width / 2)
135 self.put(s - halfbit, self.samplenum + halfbit, self.out_ann, data)
136
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100137 def putpx(self, rxtx, data):
138 s, halfbit = self.startsample[rxtx], int(self.bit_width / 2)
139 self.put(s - halfbit, self.samplenum + halfbit, self.out_python, data)
140
Uwe Hermann15ac6602013-09-11 19:15:51 +0200141 def putg(self, data):
142 s, halfbit = self.samplenum, int(self.bit_width / 2)
143 self.put(s - halfbit, s + halfbit, self.out_ann, data)
144
145 def putp(self, data):
146 s, halfbit = self.samplenum, int(self.bit_width / 2)
Uwe Hermannc515eed2014-01-30 22:37:29 +0100147 self.put(s - halfbit, s + halfbit, self.out_python, data)
Uwe Hermann97cca212012-01-14 22:51:07 +0100148
Uwe Hermann0bb7bcf2014-01-20 17:43:01 +0100149 def putbin(self, rxtx, data):
150 s, halfbit = self.startsample[rxtx], int(self.bit_width / 2)
151 self.put(s - halfbit, self.samplenum + halfbit, self.out_bin, data)
152
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100153 def __init__(self, **kwargs):
Bert Vermeulenf372d592013-10-30 22:25:45 +0100154 self.samplerate = None
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100155 self.samplenum = 0
Uwe Hermann97cca212012-01-14 22:51:07 +0100156 self.frame_start = [-1, -1]
157 self.startbit = [-1, -1]
158 self.cur_data_bit = [0, 0]
159 self.databyte = [0, 0]
Uwe Hermann1ccef462012-02-01 19:07:11 +0100160 self.paritybit = [-1, -1]
Uwe Hermann97cca212012-01-14 22:51:07 +0100161 self.stopbit1 = [-1, -1]
162 self.startsample = [-1, -1]
Uwe Hermann2b716032012-03-04 10:13:29 +0100163 self.state = ['WAIT FOR START BIT', 'WAIT FOR START BIT']
Uwe Hermann83be7b82013-09-11 19:20:15 +0200164 self.oldbit = [1, 1]
165 self.oldpins = [1, 1]
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100166 self.databits = [[], []]
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100167
Bert Vermeulenf372d592013-10-30 22:25:45 +0100168 def start(self):
Uwe Hermannc515eed2014-01-30 22:37:29 +0100169 self.out_python = self.register(srd.OUTPUT_PYTHON)
Uwe Hermann0bb7bcf2014-01-20 17:43:01 +0100170 self.out_bin = self.register(srd.OUTPUT_BINARY)
Bert Vermeulenbe465112013-11-14 17:25:53 +0100171 self.out_ann = self.register(srd.OUTPUT_ANN)
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100172
Bert Vermeulenf372d592013-10-30 22:25:45 +0100173 def metadata(self, key, value):
174 if key == srd.SRD_CONF_SAMPLERATE:
175 self.samplerate = value;
176 # The width of one UART bit in number of samples.
177 self.bit_width = float(self.samplerate) / float(self.options['baudrate'])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100178
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100179 # Return true if we reached the middle of the desired bit, false otherwise.
Uwe Hermann97cca212012-01-14 22:51:07 +0100180 def reached_bit(self, rxtx, bitnum):
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100181 # bitpos is the samplenumber which is in the middle of the
182 # specified UART bit (0 = start bit, 1..x = data, x+1 = parity bit
183 # (if used) or the first stop bit, and so on).
Uwe Hermann97cca212012-01-14 22:51:07 +0100184 bitpos = self.frame_start[rxtx] + (self.bit_width / 2.0)
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100185 bitpos += bitnum * self.bit_width
186 if self.samplenum >= bitpos:
187 return True
188 return False
189
Uwe Hermann97cca212012-01-14 22:51:07 +0100190 def reached_bit_last(self, rxtx, bitnum):
191 bitpos = self.frame_start[rxtx] + ((bitnum + 1) * self.bit_width)
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100192 if self.samplenum >= bitpos:
193 return True
194 return False
195
Uwe Hermann97cca212012-01-14 22:51:07 +0100196 def wait_for_start_bit(self, rxtx, old_signal, signal):
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100197 # The start bit is always 0 (low). As the idle UART (and the stop bit)
198 # level is 1 (high), the beginning of a start bit is a falling edge.
199 if not (old_signal == 1 and signal == 0):
200 return
201
202 # Save the sample number where the start bit begins.
Uwe Hermann97cca212012-01-14 22:51:07 +0100203 self.frame_start[rxtx] = self.samplenum
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100204
Uwe Hermann2b716032012-03-04 10:13:29 +0100205 self.state[rxtx] = 'GET START BIT'
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100206
Uwe Hermann97cca212012-01-14 22:51:07 +0100207 def get_start_bit(self, rxtx, signal):
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100208 # Skip samples until we're in the middle of the start bit.
Uwe Hermann97cca212012-01-14 22:51:07 +0100209 if not self.reached_bit(rxtx, 0):
Uwe Hermann1bb57ab2012-01-07 18:57:47 +0100210 return
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100211
Uwe Hermann97cca212012-01-14 22:51:07 +0100212 self.startbit[rxtx] = signal
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100213
Uwe Hermann5cc4b6a2012-01-07 21:06:16 +0100214 # The startbit must be 0. If not, we report an error.
Uwe Hermann97cca212012-01-14 22:51:07 +0100215 if self.startbit[rxtx] != 0:
Uwe Hermann15ac6602013-09-11 19:15:51 +0200216 self.putp(['INVALID STARTBIT', rxtx, self.startbit[rxtx]])
Uwe Hermann5cc4b6a2012-01-07 21:06:16 +0100217 # TODO: Abort? Ignore rest of the frame?
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100218
Uwe Hermann97cca212012-01-14 22:51:07 +0100219 self.cur_data_bit[rxtx] = 0
220 self.databyte[rxtx] = 0
221 self.startsample[rxtx] = -1
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100222
Uwe Hermann2b716032012-03-04 10:13:29 +0100223 self.state[rxtx] = 'GET DATA BITS'
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100224
Uwe Hermann15ac6602013-09-11 19:15:51 +0200225 self.putp(['STARTBIT', rxtx, self.startbit[rxtx]])
Uwe Hermann2ce20a92014-01-30 22:26:39 +0100226 self.putg([rxtx + 2, ['Start bit', 'Start', 'S']])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100227
Uwe Hermann97cca212012-01-14 22:51:07 +0100228 def get_data_bits(self, rxtx, signal):
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100229 # Skip samples until we're in the middle of the desired data bit.
Uwe Hermann97cca212012-01-14 22:51:07 +0100230 if not self.reached_bit(rxtx, self.cur_data_bit[rxtx] + 1):
Uwe Hermann1bb57ab2012-01-07 18:57:47 +0100231 return
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100232
Uwe Hermann15ac6602013-09-11 19:15:51 +0200233 # Save the sample number of the middle of the first data bit.
Uwe Hermann97cca212012-01-14 22:51:07 +0100234 if self.startsample[rxtx] == -1:
235 self.startsample[rxtx] = self.samplenum
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100236
237 # Get the next data bit in LSB-first or MSB-first fashion.
Uwe Hermanna7fc4c32012-02-01 19:47:50 +0100238 if self.options['bit_order'] == 'lsb-first':
Uwe Hermann97cca212012-01-14 22:51:07 +0100239 self.databyte[rxtx] >>= 1
Uwe Hermannfd4aa8a2012-01-28 19:08:13 +0100240 self.databyte[rxtx] |= \
241 (signal << (self.options['num_data_bits'] - 1))
Uwe Hermanna7fc4c32012-02-01 19:47:50 +0100242 elif self.options['bit_order'] == 'msb-first':
Uwe Hermann97cca212012-01-14 22:51:07 +0100243 self.databyte[rxtx] <<= 1
244 self.databyte[rxtx] |= (signal << 0)
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100245 else:
Uwe Hermanna7fc4c32012-02-01 19:47:50 +0100246 raise Exception('Invalid bit order value: %s',
Uwe Hermann4a04ece2012-01-20 01:16:47 +0100247 self.options['bit_order'])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100248
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100249 self.putg([rxtx + 12, ['%d' % signal]])
250
251 # Store individual data bits and their start/end samplenumbers.
252 s, halfbit = self.samplenum, int(self.bit_width / 2)
253 self.databits[rxtx].append([signal, s - halfbit, s + halfbit])
254
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100255 # Return here, unless we already received all data bits.
Uwe Hermann4a04ece2012-01-20 01:16:47 +0100256 if self.cur_data_bit[rxtx] < self.options['num_data_bits'] - 1:
Uwe Hermann97cca212012-01-14 22:51:07 +0100257 self.cur_data_bit[rxtx] += 1
Uwe Hermann1bb57ab2012-01-07 18:57:47 +0100258 return
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100259
Uwe Hermann2b716032012-03-04 10:13:29 +0100260 self.state[rxtx] = 'GET PARITY BIT'
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100261
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100262 self.putpx(rxtx, ['DATABITS', rxtx, self.databits[rxtx]])
263 self.putpx(rxtx, ['DATA', rxtx, self.databyte[rxtx]])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100264
Uwe Hermann3006c662013-09-12 08:31:59 +0200265 b, f = self.databyte[rxtx], self.options['format']
266 if f == 'ascii':
Uwe Hermanne0a01232014-01-30 15:26:06 +0100267 c = chr(b) if b in range(30, 126 + 1) else '[%02X]' % b
Uwe Hermann8705ddc2013-12-03 14:46:23 +0100268 self.putx(rxtx, [rxtx, [c]])
Uwe Hermann3006c662013-09-12 08:31:59 +0200269 elif f == 'dec':
Uwe Hermann6d6b32d2013-09-12 09:40:40 +0200270 self.putx(rxtx, [rxtx, [str(b)]])
Uwe Hermann3006c662013-09-12 08:31:59 +0200271 elif f == 'hex':
Uwe Hermann6d6b32d2013-09-12 09:40:40 +0200272 self.putx(rxtx, [rxtx, [hex(b)[2:].zfill(2).upper()]])
Uwe Hermann3006c662013-09-12 08:31:59 +0200273 elif f == 'oct':
Uwe Hermann6d6b32d2013-09-12 09:40:40 +0200274 self.putx(rxtx, [rxtx, [oct(b)[2:].zfill(3)]])
Uwe Hermann3006c662013-09-12 08:31:59 +0200275 elif f == 'bin':
Uwe Hermann6d6b32d2013-09-12 09:40:40 +0200276 self.putx(rxtx, [rxtx, [bin(b)[2:].zfill(8)]])
Uwe Hermann3006c662013-09-12 08:31:59 +0200277 else:
278 raise Exception('Invalid data format option: %s' % f)
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100279
Uwe Hermann0bb7bcf2014-01-20 17:43:01 +0100280 self.putbin(rxtx, (rxtx, bytes([b])))
281 self.putbin(rxtx, (2, bytes([b])))
282
Uwe Hermann4aedd5b2014-02-12 22:55:37 +0100283 self.databits = [[], []]
284
Uwe Hermann97cca212012-01-14 22:51:07 +0100285 def get_parity_bit(self, rxtx, signal):
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100286 # If no parity is used/configured, skip to the next state immediately.
Uwe Hermanna7fc4c32012-02-01 19:47:50 +0100287 if self.options['parity_type'] == 'none':
Uwe Hermann2b716032012-03-04 10:13:29 +0100288 self.state[rxtx] = 'GET STOP BITS'
Uwe Hermann1bb57ab2012-01-07 18:57:47 +0100289 return
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100290
291 # Skip samples until we're in the middle of the parity bit.
Uwe Hermann4a04ece2012-01-20 01:16:47 +0100292 if not self.reached_bit(rxtx, self.options['num_data_bits'] + 1):
Uwe Hermann1bb57ab2012-01-07 18:57:47 +0100293 return
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100294
Uwe Hermann97cca212012-01-14 22:51:07 +0100295 self.paritybit[rxtx] = signal
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100296
Uwe Hermann2b716032012-03-04 10:13:29 +0100297 self.state[rxtx] = 'GET STOP BITS'
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100298
Uwe Hermannac941bf2012-01-25 22:25:25 +0100299 if parity_ok(self.options['parity_type'], self.paritybit[rxtx],
Uwe Hermann4a04ece2012-01-20 01:16:47 +0100300 self.databyte[rxtx], self.options['num_data_bits']):
Uwe Hermann15ac6602013-09-11 19:15:51 +0200301 self.putp(['PARITYBIT', rxtx, self.paritybit[rxtx]])
Uwe Hermann2ce20a92014-01-30 22:26:39 +0100302 self.putg([rxtx + 4, ['Parity bit', 'Parity', 'P']])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100303 else:
Uwe Hermann61132ab2012-01-07 22:06:28 +0100304 # TODO: Return expected/actual parity values.
Uwe Hermann15ac6602013-09-11 19:15:51 +0200305 self.putp(['PARITY ERROR', rxtx, (0, 1)]) # FIXME: Dummy tuple...
Uwe Hermann4e3b2762014-02-01 18:20:45 +0100306 self.putg([rxtx + 6, ['Parity error', 'Parity err', 'PE']])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100307
308 # TODO: Currently only supports 1 stop bit.
Uwe Hermann97cca212012-01-14 22:51:07 +0100309 def get_stop_bits(self, rxtx, signal):
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100310 # Skip samples until we're in the middle of the stop bit(s).
Uwe Hermanna7fc4c32012-02-01 19:47:50 +0100311 skip_parity = 0 if self.options['parity_type'] == 'none' else 1
Uwe Hermann4a04ece2012-01-20 01:16:47 +0100312 b = self.options['num_data_bits'] + 1 + skip_parity
313 if not self.reached_bit(rxtx, b):
Uwe Hermann1bb57ab2012-01-07 18:57:47 +0100314 return
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100315
Uwe Hermann97cca212012-01-14 22:51:07 +0100316 self.stopbit1[rxtx] = signal
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100317
Uwe Hermann5cc4b6a2012-01-07 21:06:16 +0100318 # Stop bits must be 1. If not, we report an error.
Uwe Hermann97cca212012-01-14 22:51:07 +0100319 if self.stopbit1[rxtx] != 1:
Uwe Hermann15ac6602013-09-11 19:15:51 +0200320 self.putp(['INVALID STOPBIT', rxtx, self.stopbit1[rxtx]])
Uwe Hermann4e3b2762014-02-01 18:20:45 +0100321 self.putg([rxtx + 8, ['Frame error', 'Frame err', 'FE']])
Uwe Hermann5cc4b6a2012-01-07 21:06:16 +0100322 # TODO: Abort? Ignore the frame? Other?
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100323
Uwe Hermann2b716032012-03-04 10:13:29 +0100324 self.state[rxtx] = 'WAIT FOR START BIT'
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100325
Uwe Hermann15ac6602013-09-11 19:15:51 +0200326 self.putp(['STOPBIT', rxtx, self.stopbit1[rxtx]])
Uwe Hermann2ce20a92014-01-30 22:26:39 +0100327 self.putg([rxtx + 4, ['Stop bit', 'Stop', 'T']])
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100328
Uwe Hermanndecde152012-01-25 22:11:38 +0100329 def decode(self, ss, es, data):
Bert Vermeulenf372d592013-10-30 22:25:45 +0100330 if self.samplerate is None:
331 raise Exception("Cannot decode without samplerate.")
Uwe Hermann2fcd7c22012-07-11 22:19:31 +0200332 for (self.samplenum, pins) in data:
333
Uwe Hermannb0827232012-08-31 11:34:46 +0200334 # Note: Ignoring identical samples here for performance reasons
335 # is not possible for this PD, at least not in the current state.
336 # if self.oldpins == pins:
337 # continue
Uwe Hermann2fcd7c22012-07-11 22:19:31 +0200338 self.oldpins, (rx, tx) = pins, pins
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100339
Uwe Hermann3dd546c2014-01-31 00:45:56 +0100340 # Either RX or TX (but not both) can be omitted.
341 has_pin = [rx in (0, 1), tx in (0, 1)]
342 if has_pin == [False, False]:
343 raise Exception('Either TX or RX (or both) pins required.')
344
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100345 # State machine.
Uwe Hermann97cca212012-01-14 22:51:07 +0100346 for rxtx in (RX, TX):
Uwe Hermann3dd546c2014-01-31 00:45:56 +0100347 # Don't try to handle RX (or TX) if not supplied.
348 if not has_pin[rxtx]:
349 continue
350
Uwe Hermann97cca212012-01-14 22:51:07 +0100351 signal = rx if (rxtx == RX) else tx
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100352
Uwe Hermann2b716032012-03-04 10:13:29 +0100353 if self.state[rxtx] == 'WAIT FOR START BIT':
Uwe Hermann97cca212012-01-14 22:51:07 +0100354 self.wait_for_start_bit(rxtx, self.oldbit[rxtx], signal)
Uwe Hermann2b716032012-03-04 10:13:29 +0100355 elif self.state[rxtx] == 'GET START BIT':
Uwe Hermann97cca212012-01-14 22:51:07 +0100356 self.get_start_bit(rxtx, signal)
Uwe Hermann2b716032012-03-04 10:13:29 +0100357 elif self.state[rxtx] == 'GET DATA BITS':
Uwe Hermann97cca212012-01-14 22:51:07 +0100358 self.get_data_bits(rxtx, signal)
Uwe Hermann2b716032012-03-04 10:13:29 +0100359 elif self.state[rxtx] == 'GET PARITY BIT':
Uwe Hermann97cca212012-01-14 22:51:07 +0100360 self.get_parity_bit(rxtx, signal)
Uwe Hermann2b716032012-03-04 10:13:29 +0100361 elif self.state[rxtx] == 'GET STOP BITS':
Uwe Hermann97cca212012-01-14 22:51:07 +0100362 self.get_stop_bits(rxtx, signal)
363 else:
Uwe Hermann0eeeb542012-11-21 22:51:31 +0100364 raise Exception('Invalid state: %s' % self.state[rxtx])
Uwe Hermann97cca212012-01-14 22:51:07 +0100365
366 # Save current RX/TX values for the next round.
367 self.oldbit[rxtx] = signal
Uwe Hermannf44d2db2011-12-07 02:03:25 +0100368