]> sigrok.org Git - libsigrokdecode.git/blame - decoders/uart/__init__.py
srd: uart: Move protocol docs to __init__.py.
[libsigrokdecode.git] / decoders / uart / __init__.py
CommitLineData
64c29e28
UH
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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
8e828d2a
UH
21'''
22Universal Asynchronous Receiver Transmitter (UART) is a simple serial
23communication protocol which allows two devices to talk to each other.
24
25It uses just two data signals and a ground (GND) signal:
26 - RX/RXD: Receive signal
27 - TX/TXD: Transmit signal
28
29The protocol is asynchronous, i.e., there is no dedicated clock signal.
30Rather, both devices have to agree on a baudrate (number of bits to be
31transmitted per second) beforehand. Baudrates can be arbitrary in theory,
32but usually the choice is limited by the hardware UARTs that are used.
33Common values are 9600 or 115200.
34
35The protocol allows full-duplex transmission, i.e. both devices can send
36data at the same time. However, unlike SPI (which is always full-duplex,
37i.e., each send operation is automatically also a receive operation), UART
38allows one-way communication, too. In such a case only one signal (and GND)
39is required.
40
41The data is sent over the TX line in so-called 'frames', which consist of:
42 - Exactly one start bit (always 0/low).
43 - Between 5 and 9 data bits.
44 - An (optional) parity bit.
45 - One or more stop bit(s).
46
47The idle state of the RX/TX line is 1/high. As the start bit is 0/low, the
48receiver can continually monitor its RX line for a falling edge, in order
49to detect the start bit.
50
51Once detected, it can (due to the agreed-upon baudrate and thus the known
52width/duration of one UART bit) sample the state of the RX line "in the
53middle" of each (start/data/parity/stop) bit it wants to analyze.
54
55It is configurable whether there is a parity bit in a frame, and if yes,
56which type of parity is used:
57 - None: No parity bit is included.
58 - Odd: The number of 1 bits in the data (and parity bit itself) is odd.
59 - Even: The number of 1 bits in the data (and parity bit itself) is even.
60 - Mark/one: The parity bit is always 1/high (also called 'mark state').
61 - Space/zero: The parity bit is always 0/low (also called 'space state').
62
63It is also configurable how many stop bits are to be used:
64 - 1 stop bit (most common case)
65 - 2 stop bits
66 - 1.5 stop bits (i.e., one stop bit, but 1.5 times the UART bit width)
67 - 0.5 stop bits (i.e., one stop bit, but 0.5 times the UART bit width)
68
69The bit order of the 5-9 data bits is LSB-first.
70
71Possible special cases:
72 - One or both data lines could be inverted, which also means that the idle
73 state of the signal line(s) is low instead of high.
74 - Only the data bits on one or both data lines (and the parity bit) could
75 be inverted (but the start/stop bits remain non-inverted).
76 - The bit order could be MSB-first instead of LSB-first.
77 - The baudrate could change in the middle of the communication. This only
78 happens in very special cases, and can only work if both devices know
79 to which baudrate they are to switch, and when.
80 - Theoretically, the baudrate on RX and the one on TX could also be
81 different, but that's a very obscure case and probably doesn't happen
82 very often in practice.
83
84Error conditions:
85 - If there is a parity bit, but it doesn't match the expected parity,
86 this is called a 'parity error'.
87 - If there are no stop bit(s), that's called a 'frame error'.
88
89More information:
90TODO: URLs
91
92Protocol output format:
93
94UART packet:
95[<packet-type>, <rxtx>, <packet-data>]
96
97This is the list of <packet-types>s and their respective <packet-data>:
98 - 'STARTBIT': The data is the (integer) value of the start bit (0 or 1).
99 - 'DATA': The data is the (integer) value of the UART data. Valid values
100 range from 0 to 512 (as the data can be up to 9 bits in size).
101 - 'PARITYBIT': The data is the (integer) value of the parity bit (0 or 1).
102 - 'STOPBIT': The data is the (integer) value of the stop bit (0 or 1).
103 - 'INVALID STARTBIT': The data is the (integer) value of the start bit
104 (0 or 1).
105 - 'INVALID STOPBIT': The data is the (integer) value of the stop bit
106 (0 or 1).
107 - 'PARITY ERROR': The data is a tuple with two entries. The first one is
108 the expected parity value, the second is the actual parity value.
109 - TODO: Frame error?
110
111The <rxtx> field is 0 for RX packets, 1 for TX packets.
112'''
113
64c29e28
UH
114from .uart import *
115