]> sigrok.org Git - libsigrokdecode.git/blame - decoders/uart/uart.py
Initial TI TLC5620 (8-bit quad DAC) protocol decoder.
[libsigrokdecode.git] / decoders / uart / uart.py
CommitLineData
f44d2db2
UH
1##
2## This file is part of the sigrok project.
3##
4a04ece4 4## Copyright (C) 2011-2012 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
f44d2db2 21# UART protocol decoder
f44d2db2 22
677d597b 23import sigrokdecode as srd
f44d2db2 24
97cca21f
UH
25# Used for differentiating between the two data directions.
26RX = 0
27TX = 1
28
1bb57ab8
UH
29# Annotation feed formats
30ANN_ASCII = 0
31ANN_DEC = 1
32ANN_HEX = 2
33ANN_OCT = 3
34ANN_BITS = 4
f44d2db2 35
f44d2db2
UH
36# Given a parity type to check (odd, even, zero, one), the value of the
37# parity bit, the value of the data, and the length of the data (5-9 bits,
38# usually 8 bits) return True if the parity is correct, False otherwise.
a7fc4c34 39# 'none' is _not_ allowed as value for 'parity_type'.
f44d2db2
UH
40def parity_ok(parity_type, parity_bit, data, num_data_bits):
41
42 # Handle easy cases first (parity bit is always 1 or 0).
a7fc4c34 43 if parity_type == 'zero':
f44d2db2 44 return parity_bit == 0
a7fc4c34 45 elif parity_type == 'one':
f44d2db2
UH
46 return parity_bit == 1
47
48 # Count number of 1 (high) bits in the data (and the parity bit itself!).
ac941bf9 49 ones = bin(data).count('1') + parity_bit
f44d2db2
UH
50
51 # Check for odd/even parity.
a7fc4c34 52 if parity_type == 'odd':
ac941bf9 53 return (ones % 2) == 1
a7fc4c34 54 elif parity_type == 'even':
ac941bf9 55 return (ones % 2) == 0
f44d2db2
UH
56 else:
57 raise Exception('Invalid parity type: %d' % parity_type)
58
677d597b 59class Decoder(srd.Decoder):
a2c2afd9 60 api_version = 1
f44d2db2
UH
61 id = 'uart'
62 name = 'UART'
3d3da57d 63 longname = 'Universal Asynchronous Receiver/Transmitter'
a465436e 64 desc = 'Asynchronous, serial bus.'
f44d2db2
UH
65 license = 'gplv2+'
66 inputs = ['logic']
67 outputs = ['uart']
29ed0f4c 68 probes = [
f44d2db2
UH
69 # Allow specifying only one of the signals, e.g. if only one data
70 # direction exists (or is relevant).
29ed0f4c
UH
71 {'id': 'rx', 'name': 'RX', 'desc': 'UART receive line'},
72 {'id': 'tx', 'name': 'TX', 'desc': 'UART transmit line'},
73 ]
b77614bc 74 optional_probes = []
f44d2db2 75 options = {
97cca21f 76 'baudrate': ['Baud rate', 115200],
f44d2db2 77 'num_data_bits': ['Data bits', 8], # Valid: 5-9.
a7fc4c34
UH
78 'parity_type': ['Parity type', 'none'],
79 'parity_check': ['Check parity?', 'yes'], # TODO: Bool supported?
80 'num_stop_bits': ['Stop bit(s)', '1'], # String! 0, 0.5, 1, 1.5.
81 'bit_order': ['Bit order', 'lsb-first'],
f44d2db2 82 # TODO: Options to invert the signal(s).
f44d2db2 83 }
e97b6ef5 84 annotations = [
97cca21f
UH
85 ['ASCII', 'Data bytes as ASCII characters'],
86 ['Decimal', 'Databytes as decimal, integer values'],
87 ['Hex', 'Data bytes in hex format'],
88 ['Octal', 'Data bytes as octal numbers'],
89 ['Bits', 'Data bytes in bit notation (sequence of 0/1 digits)'],
1bb57ab8 90 ]
f44d2db2 91
97cca21f
UH
92 def putx(self, rxtx, data):
93 self.put(self.startsample[rxtx], self.samplenum - 1, self.out_ann, data)
94
f44d2db2 95 def __init__(self, **kwargs):
f44d2db2 96 self.samplenum = 0
97cca21f
UH
97 self.frame_start = [-1, -1]
98 self.startbit = [-1, -1]
99 self.cur_data_bit = [0, 0]
100 self.databyte = [0, 0]
1ccef461 101 self.paritybit = [-1, -1]
97cca21f
UH
102 self.stopbit1 = [-1, -1]
103 self.startsample = [-1, -1]
2b716038 104 self.state = ['WAIT FOR START BIT', 'WAIT FOR START BIT']
97cca21f 105 self.oldbit = [None, None]
2fcd7c22 106 self.oldpins = None
f44d2db2
UH
107
108 def start(self, metadata):
f44d2db2 109 self.samplerate = metadata['samplerate']
56202222
UH
110 self.out_proto = self.add(srd.OUTPUT_PROTO, 'uart')
111 self.out_ann = self.add(srd.OUTPUT_ANN, 'uart')
f44d2db2 112
f44d2db2 113 # The width of one UART bit in number of samples.
4a04ece4
UH
114 self.bit_width = \
115 float(self.samplerate) / float(self.options['baudrate'])
f44d2db2
UH
116
117 def report(self):
118 pass
119
120 # Return true if we reached the middle of the desired bit, false otherwise.
97cca21f 121 def reached_bit(self, rxtx, bitnum):
f44d2db2
UH
122 # bitpos is the samplenumber which is in the middle of the
123 # specified UART bit (0 = start bit, 1..x = data, x+1 = parity bit
124 # (if used) or the first stop bit, and so on).
97cca21f 125 bitpos = self.frame_start[rxtx] + (self.bit_width / 2.0)
f44d2db2
UH
126 bitpos += bitnum * self.bit_width
127 if self.samplenum >= bitpos:
128 return True
129 return False
130
97cca21f
UH
131 def reached_bit_last(self, rxtx, bitnum):
132 bitpos = self.frame_start[rxtx] + ((bitnum + 1) * self.bit_width)
f44d2db2
UH
133 if self.samplenum >= bitpos:
134 return True
135 return False
136
97cca21f 137 def wait_for_start_bit(self, rxtx, old_signal, signal):
f44d2db2
UH
138 # The start bit is always 0 (low). As the idle UART (and the stop bit)
139 # level is 1 (high), the beginning of a start bit is a falling edge.
140 if not (old_signal == 1 and signal == 0):
141 return
142
143 # Save the sample number where the start bit begins.
97cca21f 144 self.frame_start[rxtx] = self.samplenum
f44d2db2 145
2b716038 146 self.state[rxtx] = 'GET START BIT'
f44d2db2 147
97cca21f 148 def get_start_bit(self, rxtx, signal):
f44d2db2 149 # Skip samples until we're in the middle of the start bit.
97cca21f 150 if not self.reached_bit(rxtx, 0):
1bb57ab8 151 return
f44d2db2 152
97cca21f 153 self.startbit[rxtx] = signal
f44d2db2 154
5cc4b6a0 155 # The startbit must be 0. If not, we report an error.
97cca21f
UH
156 if self.startbit[rxtx] != 0:
157 self.put(self.frame_start[rxtx], self.samplenum, self.out_proto,
b9e44d1e 158 ['INVALID STARTBIT', rxtx, self.startbit[rxtx]])
5cc4b6a0 159 # TODO: Abort? Ignore rest of the frame?
f44d2db2 160
97cca21f
UH
161 self.cur_data_bit[rxtx] = 0
162 self.databyte[rxtx] = 0
163 self.startsample[rxtx] = -1
f44d2db2 164
2b716038 165 self.state[rxtx] = 'GET DATA BITS'
f44d2db2 166
97cca21f 167 self.put(self.frame_start[rxtx], self.samplenum, self.out_proto,
b9e44d1e 168 ['STARTBIT', rxtx, self.startbit[rxtx]])
97cca21f 169 self.put(self.frame_start[rxtx], self.samplenum, self.out_ann,
5cc4b6a0 170 [ANN_ASCII, ['Start bit', 'Start', 'S']])
f44d2db2 171
97cca21f 172 def get_data_bits(self, rxtx, signal):
f44d2db2 173 # Skip samples until we're in the middle of the desired data bit.
97cca21f 174 if not self.reached_bit(rxtx, self.cur_data_bit[rxtx] + 1):
1bb57ab8 175 return
f44d2db2
UH
176
177 # Save the sample number where the data byte starts.
97cca21f
UH
178 if self.startsample[rxtx] == -1:
179 self.startsample[rxtx] = self.samplenum
f44d2db2
UH
180
181 # Get the next data bit in LSB-first or MSB-first fashion.
a7fc4c34 182 if self.options['bit_order'] == 'lsb-first':
97cca21f 183 self.databyte[rxtx] >>= 1
fd4aa8aa
UH
184 self.databyte[rxtx] |= \
185 (signal << (self.options['num_data_bits'] - 1))
a7fc4c34 186 elif self.options['bit_order'] == 'msb-first':
97cca21f
UH
187 self.databyte[rxtx] <<= 1
188 self.databyte[rxtx] |= (signal << 0)
f44d2db2 189 else:
a7fc4c34 190 raise Exception('Invalid bit order value: %s',
4a04ece4 191 self.options['bit_order'])
f44d2db2
UH
192
193 # Return here, unless we already received all data bits.
4a04ece4
UH
194 # TODO? Off-by-one?
195 if self.cur_data_bit[rxtx] < self.options['num_data_bits'] - 1:
97cca21f 196 self.cur_data_bit[rxtx] += 1
1bb57ab8 197 return
f44d2db2 198
2b716038 199 self.state[rxtx] = 'GET PARITY BIT'
f44d2db2 200
97cca21f 201 self.put(self.startsample[rxtx], self.samplenum - 1, self.out_proto,
b9e44d1e 202 ['DATA', rxtx, self.databyte[rxtx]])
f44d2db2 203
97cca21f
UH
204 s = 'RX: ' if (rxtx == RX) else 'TX: '
205 self.putx(rxtx, [ANN_ASCII, [s + chr(self.databyte[rxtx])]])
206 self.putx(rxtx, [ANN_DEC, [s + str(self.databyte[rxtx])]])
207 self.putx(rxtx, [ANN_HEX, [s + hex(self.databyte[rxtx]),
208 s + hex(self.databyte[rxtx])[2:]]])
209 self.putx(rxtx, [ANN_OCT, [s + oct(self.databyte[rxtx]),
210 s + oct(self.databyte[rxtx])[2:]]])
211 self.putx(rxtx, [ANN_BITS, [s + bin(self.databyte[rxtx]),
212 s + bin(self.databyte[rxtx])[2:]]])
f44d2db2 213
97cca21f 214 def get_parity_bit(self, rxtx, signal):
f44d2db2 215 # If no parity is used/configured, skip to the next state immediately.
a7fc4c34 216 if self.options['parity_type'] == 'none':
2b716038 217 self.state[rxtx] = 'GET STOP BITS'
1bb57ab8 218 return
f44d2db2
UH
219
220 # Skip samples until we're in the middle of the parity bit.
4a04ece4 221 if not self.reached_bit(rxtx, self.options['num_data_bits'] + 1):
1bb57ab8 222 return
f44d2db2 223
97cca21f 224 self.paritybit[rxtx] = signal
f44d2db2 225
2b716038 226 self.state[rxtx] = 'GET STOP BITS'
f44d2db2 227
ac941bf9 228 if parity_ok(self.options['parity_type'], self.paritybit[rxtx],
4a04ece4 229 self.databyte[rxtx], self.options['num_data_bits']):
f44d2db2 230 # TODO: Fix range.
1bb57ab8 231 self.put(self.samplenum, self.samplenum, self.out_proto,
b9e44d1e 232 ['PARITYBIT', rxtx, self.paritybit[rxtx]])
1bb57ab8 233 self.put(self.samplenum, self.samplenum, self.out_ann,
5cc4b6a0 234 [ANN_ASCII, ['Parity bit', 'Parity', 'P']])
f44d2db2 235 else:
1bb57ab8 236 # TODO: Fix range.
61132abd 237 # TODO: Return expected/actual parity values.
1bb57ab8 238 self.put(self.samplenum, self.samplenum, self.out_proto,
b9e44d1e 239 ['PARITY ERROR', rxtx, (0, 1)]) # FIXME: Dummy tuple...
1bb57ab8 240 self.put(self.samplenum, self.samplenum, self.out_ann,
5cc4b6a0 241 [ANN_ASCII, ['Parity error', 'Parity err', 'PE']])
f44d2db2
UH
242
243 # TODO: Currently only supports 1 stop bit.
97cca21f 244 def get_stop_bits(self, rxtx, signal):
f44d2db2 245 # Skip samples until we're in the middle of the stop bit(s).
a7fc4c34 246 skip_parity = 0 if self.options['parity_type'] == 'none' else 1
4a04ece4
UH
247 b = self.options['num_data_bits'] + 1 + skip_parity
248 if not self.reached_bit(rxtx, b):
1bb57ab8 249 return
f44d2db2 250
97cca21f 251 self.stopbit1[rxtx] = signal
f44d2db2 252
5cc4b6a0 253 # Stop bits must be 1. If not, we report an error.
97cca21f
UH
254 if self.stopbit1[rxtx] != 1:
255 self.put(self.frame_start[rxtx], self.samplenum, self.out_proto,
b9e44d1e 256 ['INVALID STOPBIT', rxtx, self.stopbit1[rxtx]])
5cc4b6a0 257 # TODO: Abort? Ignore the frame? Other?
f44d2db2 258
2b716038 259 self.state[rxtx] = 'WAIT FOR START BIT'
f44d2db2 260
f44d2db2 261 # TODO: Fix range.
1bb57ab8 262 self.put(self.samplenum, self.samplenum, self.out_proto,
b9e44d1e 263 ['STOPBIT', rxtx, self.stopbit1[rxtx]])
1bb57ab8 264 self.put(self.samplenum, self.samplenum, self.out_ann,
5cc4b6a0 265 [ANN_ASCII, ['Stop bit', 'Stop', 'P']])
f44d2db2 266
decde15e
UH
267 def decode(self, ss, es, data):
268 # TODO: Either RX or TX could be omitted (optional probe).
2fcd7c22
UH
269 for (self.samplenum, pins) in data:
270
b0827236
UH
271 # Note: Ignoring identical samples here for performance reasons
272 # is not possible for this PD, at least not in the current state.
273 # if self.oldpins == pins:
274 # continue
2fcd7c22 275 self.oldpins, (rx, tx) = pins, pins
f44d2db2
UH
276
277 # First sample: Save RX/TX value.
97cca21f
UH
278 if self.oldbit[RX] == None:
279 self.oldbit[RX] = rx
280 continue
281 if self.oldbit[TX] == None:
282 self.oldbit[TX] = tx
f44d2db2
UH
283 continue
284
f44d2db2 285 # State machine.
97cca21f
UH
286 for rxtx in (RX, TX):
287 signal = rx if (rxtx == RX) else tx
288
2b716038 289 if self.state[rxtx] == 'WAIT FOR START BIT':
97cca21f 290 self.wait_for_start_bit(rxtx, self.oldbit[rxtx], signal)
2b716038 291 elif self.state[rxtx] == 'GET START BIT':
97cca21f 292 self.get_start_bit(rxtx, signal)
2b716038 293 elif self.state[rxtx] == 'GET DATA BITS':
97cca21f 294 self.get_data_bits(rxtx, signal)
2b716038 295 elif self.state[rxtx] == 'GET PARITY BIT':
97cca21f 296 self.get_parity_bit(rxtx, signal)
2b716038 297 elif self.state[rxtx] == 'GET STOP BITS':
97cca21f
UH
298 self.get_stop_bits(rxtx, signal)
299 else:
decde15e 300 raise Exception('Invalid state: %d' % self.state[rxtx])
97cca21f
UH
301
302 # Save current RX/TX values for the next round.
303 self.oldbit[rxtx] = signal
f44d2db2 304