1 RS485 SERIAL COMMUNICATIONS 2 31. INTRODUCTION 4 5 EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the 6 electrical characteristics of drivers and receivers for use in balanced 7 digital multipoint systems. 8 This standard is widely used for communications in industrial automation 9 because it can be used effectively over long distances and in electrically 10 noisy environments. 11 122. HARDWARE-RELATED CONSIDERATIONS 13 14 Some CPUs/UARTs (e.g., Atmel AT91 or 16C950 UART) contain a built-in 15 half-duplex mode capable of automatically controlling line direction by 16 toggling RTS or DTR signals. That can be used to control external 17 half-duplex hardware like an RS485 transceiver or any RS232-connected 18 half-duplex devices like some modems. 19 20 For these microcontrollers, the Linux driver should be made capable of 21 working in both modes, and proper ioctls (see later) should be made 22 available at user-level to allow switching from one mode to the other, and 23 vice versa. 24 253. DATA STRUCTURES ALREADY AVAILABLE IN THE KERNEL 26 27 The Linux kernel provides the serial_rs485 structure (see [1]) to handle 28 RS485 communications. This data structure is used to set and configure RS485 29 parameters in the platform data and in ioctls. 30 31 The device tree can also provide RS485 boot time parameters (see [2] 32 for bindings). The driver is in charge of filling this data structure from 33 the values given by the device tree. 34 35 Any driver for devices capable of working both as RS232 and RS485 should 36 provide at least the following ioctls: 37 38 - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used 39 to enable/disable RS485 mode from user-space 40 41 - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used 42 to get RS485 mode from kernel-space (i.e., driver) to user-space. 43 44 In other words, the serial driver should contain a code similar to the next 45 one: 46 47 static struct uart_ops atmel_pops = { 48 /* ... */ 49 .ioctl = handle_ioctl, 50 }; 51 52 static int handle_ioctl(struct uart_port *port, 53 unsigned int cmd, 54 unsigned long arg) 55 { 56 struct serial_rs485 rs485conf; 57 58 switch (cmd) { 59 case TIOCSRS485: 60 if (copy_from_user(&rs485conf, 61 (struct serial_rs485 *) arg, 62 sizeof(rs485conf))) 63 return -EFAULT; 64 65 /* ... */ 66 break; 67 68 case TIOCGRS485: 69 if (copy_to_user((struct serial_rs485 *) arg, 70 ..., 71 sizeof(rs485conf))) 72 return -EFAULT; 73 /* ... */ 74 break; 75 76 /* ... */ 77 } 78 } 79 80 814. USAGE FROM USER-LEVEL 82 83 From user-level, RS485 configuration can be get/set using the previous 84 ioctls. For instance, to set RS485 you can use the following code: 85 86 #include <linux/serial.h> 87 88 /* Driver-specific ioctls: */ 89 #define TIOCGRS485 0x542E 90 #define TIOCSRS485 0x542F 91 92 /* Open your specific device (e.g., /dev/mydevice): */ 93 int fd = open ("/dev/mydevice", O_RDWR); 94 if (fd < 0) { 95 /* Error handling. See errno. */ 96 } 97 98 struct serial_rs485 rs485conf; 99 100 /* Enable RS485 mode: */ 101 rs485conf.flags |= SER_RS485_ENABLED; 102 103 /* Set logical level for RTS pin equal to 1 when sending: */ 104 rs485conf.flags |= SER_RS485_RTS_ON_SEND; 105 /* or, set logical level for RTS pin equal to 0 when sending: */ 106 rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); 107 108 /* Set logical level for RTS pin equal to 1 after sending: */ 109 rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; 110 /* or, set logical level for RTS pin equal to 0 after sending: */ 111 rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND); 112 113 /* Set rts delay before send, if needed: */ 114 rs485conf.delay_rts_before_send = ...; 115 116 /* Set rts delay after send, if needed: */ 117 rs485conf.delay_rts_after_send = ...; 118 119 /* Set this flag if you want to receive data even whilst sending data */ 120 rs485conf.flags |= SER_RS485_RX_DURING_TX; 121 122 if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) { 123 /* Error handling. See errno. */ 124 } 125 126 /* Use read() and write() syscalls here... */ 127 128 /* Close the device when finished: */ 129 if (close (fd) < 0) { 130 /* Error handling. See errno. */ 131 } 132 1335. REFERENCES 134 135 [1] include/uapi/linux/serial.h 136 [2] Documentation/devicetree/bindings/serial/rs485.txt 137