1*4882a593Smuzhiyun=========================== 2*4882a593SmuzhiyunRS485 Serial Communications 3*4882a593Smuzhiyun=========================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun1. Introduction 6*4882a593Smuzhiyun=============== 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the 9*4882a593Smuzhiyun electrical characteristics of drivers and receivers for use in balanced 10*4882a593Smuzhiyun digital multipoint systems. 11*4882a593Smuzhiyun This standard is widely used for communications in industrial automation 12*4882a593Smuzhiyun because it can be used effectively over long distances and in electrically 13*4882a593Smuzhiyun noisy environments. 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun2. Hardware-related Considerations 16*4882a593Smuzhiyun================================== 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun Some CPUs/UARTs (e.g., Atmel AT91 or 16C950 UART) contain a built-in 19*4882a593Smuzhiyun half-duplex mode capable of automatically controlling line direction by 20*4882a593Smuzhiyun toggling RTS or DTR signals. That can be used to control external 21*4882a593Smuzhiyun half-duplex hardware like an RS485 transceiver or any RS232-connected 22*4882a593Smuzhiyun half-duplex devices like some modems. 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun For these microcontrollers, the Linux driver should be made capable of 25*4882a593Smuzhiyun working in both modes, and proper ioctls (see later) should be made 26*4882a593Smuzhiyun available at user-level to allow switching from one mode to the other, and 27*4882a593Smuzhiyun vice versa. 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun3. Data Structures Already Available in the Kernel 30*4882a593Smuzhiyun================================================== 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun The Linux kernel provides the serial_rs485 structure (see [1]) to handle 33*4882a593Smuzhiyun RS485 communications. This data structure is used to set and configure RS485 34*4882a593Smuzhiyun parameters in the platform data and in ioctls. 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun The device tree can also provide RS485 boot time parameters (see [2] 37*4882a593Smuzhiyun for bindings). The driver is in charge of filling this data structure from 38*4882a593Smuzhiyun the values given by the device tree. 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun Any driver for devices capable of working both as RS232 and RS485 should 41*4882a593Smuzhiyun implement the rs485_config callback in the uart_port structure. The 42*4882a593Smuzhiyun serial_core calls rs485_config to do the device specific part in response 43*4882a593Smuzhiyun to TIOCSRS485 and TIOCGRS485 ioctls (see below). The rs485_config callback 44*4882a593Smuzhiyun receives a pointer to struct serial_rs485. 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun4. Usage from user-level 47*4882a593Smuzhiyun======================== 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun From user-level, RS485 configuration can be get/set using the previous 50*4882a593Smuzhiyun ioctls. For instance, to set RS485 you can use the following code:: 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun #include <linux/serial.h> 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */ 55*4882a593Smuzhiyun #include <sys/ioctl.h> 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun /* Open your specific device (e.g., /dev/mydevice): */ 58*4882a593Smuzhiyun int fd = open ("/dev/mydevice", O_RDWR); 59*4882a593Smuzhiyun if (fd < 0) { 60*4882a593Smuzhiyun /* Error handling. See errno. */ 61*4882a593Smuzhiyun } 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun struct serial_rs485 rs485conf; 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun /* Enable RS485 mode: */ 66*4882a593Smuzhiyun rs485conf.flags |= SER_RS485_ENABLED; 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun /* Set logical level for RTS pin equal to 1 when sending: */ 69*4882a593Smuzhiyun rs485conf.flags |= SER_RS485_RTS_ON_SEND; 70*4882a593Smuzhiyun /* or, set logical level for RTS pin equal to 0 when sending: */ 71*4882a593Smuzhiyun rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun /* Set logical level for RTS pin equal to 1 after sending: */ 74*4882a593Smuzhiyun rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; 75*4882a593Smuzhiyun /* or, set logical level for RTS pin equal to 0 after sending: */ 76*4882a593Smuzhiyun rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND); 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun /* Set rts delay before send, if needed: */ 79*4882a593Smuzhiyun rs485conf.delay_rts_before_send = ...; 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun /* Set rts delay after send, if needed: */ 82*4882a593Smuzhiyun rs485conf.delay_rts_after_send = ...; 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun /* Set this flag if you want to receive data even while sending data */ 85*4882a593Smuzhiyun rs485conf.flags |= SER_RS485_RX_DURING_TX; 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) { 88*4882a593Smuzhiyun /* Error handling. See errno. */ 89*4882a593Smuzhiyun } 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun /* Use read() and write() syscalls here... */ 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun /* Close the device when finished: */ 94*4882a593Smuzhiyun if (close (fd) < 0) { 95*4882a593Smuzhiyun /* Error handling. See errno. */ 96*4882a593Smuzhiyun } 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun5. References 99*4882a593Smuzhiyun============= 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun [1] include/uapi/linux/serial.h 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun [2] Documentation/devicetree/bindings/serial/rs485.txt 104