blob: 6bc824f948f9d39f34b7df106efe4dd21cbf09c4 [file] [log] [blame]
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -03001===========================
2RS485 Serial Communications
3===========================
Claudio Scordino63295cb2010-11-11 11:22:36 +01004
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030051. Introduction
6===============
Claudio Scordino63295cb2010-11-11 11:22:36 +01007
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
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300152. Hardware-related Considerations
16==================================
Claudio Scordino63295cb2010-11-11 11:22:36 +010017
Yegor Yefremovde6f86c2010-11-22 11:06:32 +010018 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.
Claudio Scordino63295cb2010-11-11 11:22:36 +010023
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
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300293. Data Structures Already Available in the Kernel
30==================================================
Claudio Scordino63295cb2010-11-11 11:22:36 +010031
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.
35
Nicolas Ferre0331bbf2011-10-12 18:06:56 +020036 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.
39
Claudio Scordino63295cb2010-11-11 11:22:36 +010040 Any driver for devices capable of working both as RS232 and RS485 should
Baruch Siach71206b92015-06-04 10:56:59 +030041 implement the rs485_config callback in the uart_port structure. The
42 serial_core calls rs485_config to do the device specific part in response
43 to TIOCSRS485 and TIOCGRS485 ioctls (see below). The rs485_config callback
44 receives a pointer to struct serial_rs485.
Claudio Scordino63295cb2010-11-11 11:22:36 +010045
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300464. Usage from user-level
47========================
Claudio Scordino63295cb2010-11-11 11:22:36 +010048
49 From user-level, RS485 configuration can be get/set using the previous
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030050 ioctls. For instance, to set RS485 you can use the following code::
Claudio Scordino63295cb2010-11-11 11:22:36 +010051
52 #include <linux/serial.h>
53
Ricardo Ribalda Delgado5021bb92016-08-18 10:54:19 +020054 /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
55 #include <sys/ioctl.h>
Claudio Scordino63295cb2010-11-11 11:22:36 +010056
57 /* Open your specific device (e.g., /dev/mydevice): */
58 int fd = open ("/dev/mydevice", O_RDWR);
59 if (fd < 0) {
60 /* Error handling. See errno. */
61 }
62
63 struct serial_rs485 rs485conf;
64
Claudio Scordino93f33502011-11-09 15:51:49 +010065 /* Enable RS485 mode: */
Claudio Scordino63295cb2010-11-11 11:22:36 +010066 rs485conf.flags |= SER_RS485_ENABLED;
67
Claudio Scordino93f33502011-11-09 15:51:49 +010068 /* Set logical level for RTS pin equal to 1 when sending: */
69 rs485conf.flags |= SER_RS485_RTS_ON_SEND;
70 /* or, set logical level for RTS pin equal to 0 when sending: */
71 rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
72
73 /* Set logical level for RTS pin equal to 1 after sending: */
74 rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
75 /* or, set logical level for RTS pin equal to 0 after sending: */
76 rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
77
Claudio Scordino63295cb2010-11-11 11:22:36 +010078 /* Set rts delay before send, if needed: */
Claudio Scordino63295cb2010-11-11 11:22:36 +010079 rs485conf.delay_rts_before_send = ...;
80
81 /* Set rts delay after send, if needed: */
Claudio Scordino63295cb2010-11-11 11:22:36 +010082 rs485conf.delay_rts_after_send = ...;
83
Will Deacon806654a2018-11-19 11:02:45 +000084 /* Set this flag if you want to receive data even while sending data */
Bernhard Roth83cac9f2011-08-24 09:48:23 +020085 rs485conf.flags |= SER_RS485_RX_DURING_TX;
86
Claudio Scordino63295cb2010-11-11 11:22:36 +010087 if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
88 /* Error handling. See errno. */
89 }
90
91 /* Use read() and write() syscalls here... */
92
93 /* Close the device when finished: */
94 if (close (fd) < 0) {
95 /* Error handling. See errno. */
96 }
97
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300985. References
99=============
Claudio Scordino63295cb2010-11-11 11:22:36 +0100100
Yegor Yefremov83079592014-08-13 15:54:48 +0200101 [1] include/uapi/linux/serial.h
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300102
Nicolas Ferre0331bbf2011-10-12 18:06:56 +0200103 [2] Documentation/devicetree/bindings/serial/rs485.txt