1 ===========================
2 RS485 Serial Communications
3 ===========================
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
15 2. Hardware-related Considerations
16 ==================================
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.
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
29 3. Data Structures Already Available in the Kernel
30 ==================================================
32 The Linux kernel provides the serial_rs485 structure (see [1]) to handle
33 RS485 communications. This data structure is used to set and configure RS485
34 parameters in the platform data and in ioctls.
36 The device tree can also provide RS485 boot time parameters (see [2]
37 for bindings). The driver is in charge of filling this data structure from
38 the values given by the device tree.
40 Any driver for devices capable of working both as RS232 and RS485 should
41 implement the rs485_config callback and provide rs485_supported in the
42 uart_port structure. The serial core calls rs485_config to do the device
43 specific part in response to TIOCSRS485 ioctl (see below). The rs485_config
44 callback receives a pointer to a sanitizated serial_rs485 structure. The
45 serial_rs485 userspace provides is sanitized before calling rs485_config
46 using rs485_supported that indicates what RS485 features the driver supports
47 for the uart_port. TIOCGRS485 ioctl can be used to read back the
48 serial_rs485 structure matching to the current configuration.
50 4. Usage from user-level
51 ========================
53 From user-level, RS485 configuration can be get/set using the previous
54 ioctls. For instance, to set RS485 you can use the following code::
56 #include <linux/serial.h>
58 /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
59 #include <sys/ioctl.h>
61 /* Open your specific device (e.g., /dev/mydevice): */
62 int fd = open ("/dev/mydevice", O_RDWR);
64 /* Error handling. See errno. */
67 struct serial_rs485 rs485conf;
69 /* Enable RS485 mode: */
70 rs485conf.flags |= SER_RS485_ENABLED;
72 /* Set logical level for RTS pin equal to 1 when sending: */
73 rs485conf.flags |= SER_RS485_RTS_ON_SEND;
74 /* or, set logical level for RTS pin equal to 0 when sending: */
75 rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
77 /* Set logical level for RTS pin equal to 1 after sending: */
78 rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
79 /* or, set logical level for RTS pin equal to 0 after sending: */
80 rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
82 /* Set rts delay before send, if needed: */
83 rs485conf.delay_rts_before_send = ...;
85 /* Set rts delay after send, if needed: */
86 rs485conf.delay_rts_after_send = ...;
88 /* Set this flag if you want to receive data even while sending data */
89 rs485conf.flags |= SER_RS485_RX_DURING_TX;
91 if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
92 /* Error handling. See errno. */
95 /* Use read() and write() syscalls here... */
97 /* Close the device when finished: */
99 /* Error handling. See errno. */
102 5. Multipoint Addressing
103 ========================
105 The Linux kernel provides addressing mode for multipoint RS-485 serial
106 communications line. The addressing mode is enabled with SER_RS485_ADDRB
107 flag in serial_rs485. Struct serial_rs485 has two additional flags and
108 fields for enabling receive and destination addresses.
111 - SER_RS485_ADDRB: Enabled addressing mode (sets also ADDRB in termios).
112 - SER_RS485_ADDR_RECV: Receive (filter) address enabled.
113 - SER_RS485_ADDR_DEST: Set destination address.
115 Address fields (enabled with corresponding SER_RS485_ADDR_* flag):
116 - addr_recv: Receive address.
117 - addr_dest: Destination address.
119 Once a receive address is set, the communication can occur only with the
120 particular device and other peers are filtered out. It is left up to the
121 receiver side to enforce the filtering. Receive address will be cleared
122 if SER_RS485_ADDR_RECV is not set.
124 Note: not all devices supporting RS485 support multipoint addressing.
129 [1] include/uapi/linux/serial.h
131 [2] Documentation/devicetree/bindings/serial/rs485.txt