xref: /linux/Documentation/driver-api/serial/serial-rs485.rst (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
1===========================
2RS485 Serial Communications
3===========================
4
51. Introduction
6===============
7
8   EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
9   electrical characteristics of drivers and receivers for use in balanced
10   digital multipoint systems.
11   This standard is widely used for communications in industrial automation
12   because it can be used effectively over long distances and in electrically
13   noisy environments.
14
152. Hardware-related Considerations
16==================================
17
18   Some CPUs/UARTs (e.g., Atmel AT91 or 16C950 UART) contain a built-in
19   half-duplex mode capable of automatically controlling line direction by
20   toggling RTS or DTR signals. That can be used to control external
21   half-duplex hardware like an RS485 transceiver or any RS232-connected
22   half-duplex devices like some modems.
23
24   For these microcontrollers, the Linux driver should be made capable of
25   working in both modes, and proper ioctls (see later) should be made
26   available at user-level to allow switching from one mode to the other, and
27   vice versa.
28
293. Data Structures Already Available in the Kernel
30==================================================
31
32   The Linux kernel provides the struct serial_rs485 to handle RS485
33   communications. This data structure is used to set and configure RS485
34   parameters in the platform data and in ioctls.
35
36   The device tree can also provide RS485 boot time parameters
37   [#DT-bindings]_. The serial core fills the struct serial_rs485 from the
38   values given by the device tree when the driver calls
39   uart_get_rs485_mode().
40
41   Any driver for devices capable of working both as RS232 and RS485 should
42   implement the ``rs485_config`` callback and provide ``rs485_supported``
43   in the ``struct uart_port``. The serial core calls ``rs485_config`` to do
44   the device specific part in response to TIOCSRS485 ioctl (see below). The
45   ``rs485_config`` callback receives a pointer to a sanitizated struct
46   serial_rs485. The struct serial_rs485 userspace provides is sanitized
47   before calling ``rs485_config`` using ``rs485_supported`` that indicates
48   what RS485 features the driver supports for the ``struct uart_port``.
49   TIOCGRS485 ioctl can be used to read back the struct serial_rs485
50   matching to the current configuration.
51
52.. kernel-doc:: include/uapi/linux/serial.h
53   :identifiers: serial_rs485
54
55.. kernel-doc:: drivers/tty/serial/serial_core.c
56   :identifiers: uart_get_rs485_mode
57
584. Usage from user-level
59========================
60
61   From user-level, RS485 configuration can be get/set using the previous
62   ioctls. For instance, to set RS485 you can use the following code::
63
64	#include <linux/serial.h>
65
66	/* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
67	#include <sys/ioctl.h>
68
69	/* Open your specific device (e.g., /dev/mydevice): */
70	int fd = open ("/dev/mydevice", O_RDWR);
71	if (fd < 0) {
72		/* Error handling. See errno. */
73	}
74
75	struct serial_rs485 rs485conf;
76
77	/* Enable RS485 mode: */
78	rs485conf.flags |= SER_RS485_ENABLED;
79
80	/* Set logical level for RTS pin equal to 1 when sending: */
81	rs485conf.flags |= SER_RS485_RTS_ON_SEND;
82	/* or, set logical level for RTS pin equal to 0 when sending: */
83	rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
84
85	/* Set logical level for RTS pin equal to 1 after sending: */
86	rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
87	/* or, set logical level for RTS pin equal to 0 after sending: */
88	rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
89
90	/* Set rts delay before send, if needed: */
91	rs485conf.delay_rts_before_send = ...;
92
93	/* Set rts delay after send, if needed: */
94	rs485conf.delay_rts_after_send = ...;
95
96	/* Set this flag if you want to receive data even while sending data */
97	rs485conf.flags |= SER_RS485_RX_DURING_TX;
98
99	if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
100		/* Error handling. See errno. */
101	}
102
103	/* Use read() and write() syscalls here... */
104
105	/* Close the device when finished: */
106	if (close (fd) < 0) {
107		/* Error handling. See errno. */
108	}
109
1105. Multipoint Addressing
111========================
112
113   The Linux kernel provides addressing mode for multipoint RS-485 serial
114   communications line. The addressing mode is enabled with
115   ``SER_RS485_ADDRB`` flag in struct serial_rs485. The struct serial_rs485
116   has two additional flags and fields for enabling receive and destination
117   addresses.
118
119   Address mode flags:
120	- ``SER_RS485_ADDRB``: Enabled addressing mode (sets also ADDRB in termios).
121	- ``SER_RS485_ADDR_RECV``: Receive (filter) address enabled.
122	- ``SER_RS485_ADDR_DEST``: Set destination address.
123
124   Address fields (enabled with corresponding ``SER_RS485_ADDR_*`` flag):
125	- ``addr_recv``: Receive address.
126	- ``addr_dest``: Destination address.
127
128   Once a receive address is set, the communication can occur only with the
129   particular device and other peers are filtered out. It is left up to the
130   receiver side to enforce the filtering. Receive address will be cleared
131   if ``SER_RS485_ADDR_RECV`` is not set.
132
133   Note: not all devices supporting RS485 support multipoint addressing.
134
1356. References
136=============
137
138.. [#DT-bindings]	Documentation/devicetree/bindings/serial/rs485.yaml
139