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