]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - Documentation/serial/serial-rs485.rst
Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[mirror_ubuntu-jammy-kernel.git] / Documentation / serial / serial-rs485.rst
CommitLineData
f1374017
MCC
1===========================
2RS485 Serial Communications
3===========================
63295cb2 4
f1374017
MCC
51. Introduction
6===============
63295cb2
CS
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
f1374017
MCC
152. Hardware-related Considerations
16==================================
63295cb2 17
de6f86ce
YY
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.
63295cb2
CS
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
f1374017
MCC
293. Data Structures Already Available in the Kernel
30==================================================
63295cb2
CS
31
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
0331bbf3
NF
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.
39
63295cb2 40 Any driver for devices capable of working both as RS232 and RS485 should
71206b9f
BS
41 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.
63295cb2 45
f1374017
MCC
464. Usage from user-level
47========================
63295cb2
CS
48
49 From user-level, RS485 configuration can be get/set using the previous
f1374017 50 ioctls. For instance, to set RS485 you can use the following code::
63295cb2
CS
51
52 #include <linux/serial.h>
53
5021bb93
RR
54 /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
55 #include <sys/ioctl.h>
63295cb2
CS
56
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
93f3350c 65 /* Enable RS485 mode: */
63295cb2
CS
66 rs485conf.flags |= SER_RS485_ENABLED;
67
93f3350c
CS
68 /* 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
63295cb2 78 /* Set rts delay before send, if needed: */
63295cb2
CS
79 rs485conf.delay_rts_before_send = ...;
80
81 /* Set rts delay after send, if needed: */
63295cb2
CS
82 rs485conf.delay_rts_after_send = ...;
83
806654a9 84 /* Set this flag if you want to receive data even while sending data */
83cac9f3
BR
85 rs485conf.flags |= SER_RS485_RX_DURING_TX;
86
63295cb2
CS
87 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
f1374017
MCC
985. References
99=============
63295cb2 100
8307959d 101 [1] include/uapi/linux/serial.h
f1374017 102
0331bbf3 103 [2] Documentation/devicetree/bindings/serial/rs485.txt