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