X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fuart.py;h=9243bbc3e33741e38ec09e033dd88258aa5bb3a3;hp=65e274f99dd5f0325a1e363daf54b28f91c495fa;hb=9a12a6e7af3d7091d8e35dd1c731402cb80a01b0;hpb=1bb57ab8b3f108aff2cf83cd7dba3744050ec195 diff --git a/decoders/uart.py b/decoders/uart.py index 65e274f..9243bbc 100644 --- a/decoders/uart.py +++ b/decoders/uart.py @@ -94,7 +94,38 @@ # TODO: URLs # -import sigrokdecode +# +# Protocol output format: +# put(, , self.out_proto, ) +# +# The is a list with two entries: +# [, ] +# +# Valid packet-type values: T_START, T_DATA, T_PARITY, T_STOP, T_INVALID_START, +# T_INVALID_STOP, T_PARITY_ERROR +# +# The packet-data field has the following format and meaning: +# - T_START: The data is the (integer) value of the start bit (0 or 1). +# - T_DATA: The data is the (integer) value of the UART data. Valid values +# range from 0 to 512 (as the data can be up to 9 bits in size). +# - T_PARITY: The data is the (integer) value of the parity bit (0 or 1). +# - T_STOP: The data is the (integer) value of the stop bit (0 or 1). +# - T_INVALID_START: The data is the (integer) value of the start bit (0 or 1). +# - T_INVALID_STOP: The data is the (integer) value of the stop bit (0 or 1). +# - T_PARITY_ERROR: The data is a tuple with two entries. The first one is +# the expected parity value, the second is the actual parity value. +# +# Examples: +# [T_START, 0] +# [T_DATA, 65] +# [T_PARITY, 0] +# [T_STOP, 1] +# [T_INVALID_START, 1] +# [T_INVALID_STOP, 0] +# [T_PARITY_ERROR, (0, 1)] +# + +import sigrokdecode as srd # States WAIT_FOR_START_BIT = 0 @@ -127,6 +158,15 @@ ANN_HEX = 2 ANN_OCT = 3 ANN_BITS = 4 +# Protocol output packet types +T_START = 0 +T_DATA = 1 +T_PARITY = 2 +T_STOP = 3 +T_INVALID_START = 4 +T_INVALID_STOP = 5 +T_PARITY_ERROR = 6 + # Given a parity type to check (odd, even, zero, one), the value of the # parity bit, the value of the data, and the length of the data (5-9 bits, # usually 8 bits) return True if the parity is correct, False otherwise. @@ -150,7 +190,7 @@ def parity_ok(parity_type, parity_bit, data, num_data_bits): else: raise Exception('Invalid parity type: %d' % parity_type) -class Decoder(sigrokdecode.Decoder): +class Decoder(srd.Decoder): id = 'uart' name = 'UART' longname = 'Universal Asynchronous Receiver/Transmitter (UART)' @@ -177,23 +217,20 @@ class Decoder(sigrokdecode.Decoder): # TODO: Options to invert the signal(s). # ... } - annotation = [ + annotations = [ # ANN_ASCII - ["ASCII", "TODO: description"], + ['ASCII', 'TODO: description'], # ANN_DEC - ["Decimal", "TODO: description"], + ['Decimal', 'TODO: description'], # ANN_HEX - ["Hex", "TODO: description"], + ['Hex', 'TODO: description'], # ANN_OCT - ["Octal", "TODO: description"], + ['Octal', 'TODO: description'], # ANN_BITS - ["Bits", "TODO: description"], + ['Bits', 'TODO: description'], ] def __init__(self, **kwargs): - self.out_proto = None - self.out_ann = None - # Set defaults, can be overridden in 'start'. self.baudrate = 115200 self.num_data_bits = 8 @@ -218,8 +255,8 @@ class Decoder(sigrokdecode.Decoder): def start(self, metadata): self.samplerate = metadata['samplerate'] - self.out_proto = self.output_new(sigrokdecode.SRD_OUTPUT_PROTOCOL, 'uart') - self.out_ann = self.output_new(sigrokdecode.SRD_OUTPUT_ANNOTATION, 'uart') + self.out_proto = self.add(srd.OUTPUT_PROTO, 'uart') + self.out_ann = self.add(srd.OUTPUT_ANN, 'uart') # TODO ### self.baudrate = metadata['baudrate'] @@ -270,9 +307,11 @@ class Decoder(sigrokdecode.Decoder): self.startbit = signal + # The startbit must be 0. If not, we report an error. if self.startbit != 0: - # TODO: Startbit must be 0. If not, we report an error. - pass + self.put(self.frame_start, self.samplenum, self.out_proto, + [T_INVALID_START, self.startbit]) + # TODO: Abort? Ignore rest of the frame? self.cur_data_bit = 0 self.databyte = 0 @@ -281,9 +320,9 @@ class Decoder(sigrokdecode.Decoder): self.staterx = GET_DATA_BITS self.put(self.frame_start, self.samplenum, self.out_proto, - ['START_BIT']) + [T_START, self.startbit]) self.put(self.frame_start, self.samplenum, self.out_ann, - [ANN_ASCII, ['Start bit', 'S']]) + [ANN_ASCII, ['Start bit', 'Start', 'S']]) def get_data_bits(self, signal): # Skip samples until we're in the middle of the desired data bit. @@ -312,7 +351,7 @@ class Decoder(sigrokdecode.Decoder): self.staterx = GET_PARITY_BIT self.put(self.startsample, self.samplenum - 1, self.out_proto, - [self.databyte]) + [T_DATA, self.databyte]) self.put(self.startsample, self.samplenum - 1, self.out_ann, [ANN_ASCII, [chr(self.databyte)]]) @@ -343,15 +382,16 @@ class Decoder(sigrokdecode.Decoder): self.num_data_bits): # TODO: Fix range. self.put(self.samplenum, self.samplenum, self.out_proto, - ['PARITY_BIT']) + [T_PARITY_BIT, self.paritybit]) self.put(self.samplenum, self.samplenum, self.out_ann, - [ANN_ASCII, ['Parity bit', 'P']]) + [ANN_ASCII, ['Parity bit', 'Parity', 'P']]) else: # TODO: Fix range. + # TODO: Return expected/actual parity values. self.put(self.samplenum, self.samplenum, self.out_proto, - ['PARITY_ERROR']) # TODO: Pass parity bit value. + [T_PARITY_ERROR, (0, 1)]) # FIXME: Dummy tuple... self.put(self.samplenum, self.samplenum, self.out_ann, - [ANN_ASCII, ['Parity error', 'PE']]) + [ANN_ASCII, ['Parity error', 'Parity err', 'PE']]) # TODO: Currently only supports 1 stop bit. def get_stop_bits(self, signal): @@ -362,17 +402,19 @@ class Decoder(sigrokdecode.Decoder): self.stopbit1 = signal + # Stop bits must be 1. If not, we report an error. if self.stopbit1 != 1: - # TODO: Stop bits must be 1. If not, we report an error. - pass + self.put(self.frame_start, self.samplenum, self.out_proto, + [T_INVALID_STOP, self.stopbit1]) + # TODO: Abort? Ignore the frame? Other? self.staterx = WAIT_FOR_START_BIT # TODO: Fix range. self.put(self.samplenum, self.samplenum, self.out_proto, - ['STOP_BIT']) + [T_STOP, self.stopbit1]) self.put(self.samplenum, self.samplenum, self.out_ann, - [ANN_ASCII, ['Stop bit', 'P']]) + [ANN_ASCII, ['Stop bit', 'Stop', 'P']]) def decode(self, timeoffset, duration, data): # TODO # for (samplenum, (rx, tx)) in data: