Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 1 | RS485 SERIAL COMMUNICATIONS |
| 2 | |
| 3 | 1. 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 | |
| 12 | 2. HARDWARE-RELATED CONSIDERATIONS |
| 13 | |
Yegor Yefremov | de6f86c | 2010-11-22 11:06:32 +0100 | [diff] [blame] | 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. |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 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 | |
| 25 | 3. 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 | |
Nicolas Ferre | 0331bbf | 2011-10-12 18:06:56 +0200 | [diff] [blame] | 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 | |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 35 | Any driver for devices capable of working both as RS232 and RS485 should |
Baruch Siach | 71206b9 | 2015-06-04 10:56:59 +0300 | [diff] [blame] | 36 | implement the rs485_config callback in the uart_port structure. The |
| 37 | serial_core calls rs485_config to do the device specific part in response |
| 38 | to TIOCSRS485 and TIOCGRS485 ioctls (see below). The rs485_config callback |
| 39 | receives a pointer to struct serial_rs485. |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 40 | |
| 41 | 4. USAGE FROM USER-LEVEL |
| 42 | |
| 43 | From user-level, RS485 configuration can be get/set using the previous |
| 44 | ioctls. For instance, to set RS485 you can use the following code: |
| 45 | |
| 46 | #include <linux/serial.h> |
| 47 | |
Baruch Siach | 71206b9 | 2015-06-04 10:56:59 +0300 | [diff] [blame] | 48 | /* RS485 ioctls: */ |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 49 | #define TIOCGRS485 0x542E |
| 50 | #define TIOCSRS485 0x542F |
| 51 | |
| 52 | /* Open your specific device (e.g., /dev/mydevice): */ |
| 53 | int fd = open ("/dev/mydevice", O_RDWR); |
| 54 | if (fd < 0) { |
| 55 | /* Error handling. See errno. */ |
| 56 | } |
| 57 | |
| 58 | struct serial_rs485 rs485conf; |
| 59 | |
Claudio Scordino | 93f3350 | 2011-11-09 15:51:49 +0100 | [diff] [blame] | 60 | /* Enable RS485 mode: */ |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 61 | rs485conf.flags |= SER_RS485_ENABLED; |
| 62 | |
Claudio Scordino | 93f3350 | 2011-11-09 15:51:49 +0100 | [diff] [blame] | 63 | /* Set logical level for RTS pin equal to 1 when sending: */ |
| 64 | rs485conf.flags |= SER_RS485_RTS_ON_SEND; |
| 65 | /* or, set logical level for RTS pin equal to 0 when sending: */ |
| 66 | rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); |
| 67 | |
| 68 | /* Set logical level for RTS pin equal to 1 after sending: */ |
| 69 | rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; |
| 70 | /* or, set logical level for RTS pin equal to 0 after sending: */ |
| 71 | rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND); |
| 72 | |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 73 | /* Set rts delay before send, if needed: */ |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 74 | rs485conf.delay_rts_before_send = ...; |
| 75 | |
| 76 | /* Set rts delay after send, if needed: */ |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 77 | rs485conf.delay_rts_after_send = ...; |
| 78 | |
Bernhard Roth | 83cac9f | 2011-08-24 09:48:23 +0200 | [diff] [blame] | 79 | /* Set this flag if you want to receive data even whilst sending data */ |
| 80 | rs485conf.flags |= SER_RS485_RX_DURING_TX; |
| 81 | |
Claudio Scordino | 63295cb | 2010-11-11 11:22:36 +0100 | [diff] [blame] | 82 | if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) { |
| 83 | /* Error handling. See errno. */ |
| 84 | } |
| 85 | |
| 86 | /* Use read() and write() syscalls here... */ |
| 87 | |
| 88 | /* Close the device when finished: */ |
| 89 | if (close (fd) < 0) { |
| 90 | /* Error handling. See errno. */ |
| 91 | } |
| 92 | |
| 93 | 5. REFERENCES |
| 94 | |
Yegor Yefremov | 8307959 | 2014-08-13 15:54:48 +0200 | [diff] [blame] | 95 | [1] include/uapi/linux/serial.h |
Nicolas Ferre | 0331bbf | 2011-10-12 18:06:56 +0200 | [diff] [blame] | 96 | [2] Documentation/devicetree/bindings/serial/rs485.txt |