diff options
Diffstat (limited to 'Documentation/driver-api/serial/serial-rs485.rst')
| -rw-r--r-- | Documentation/driver-api/serial/serial-rs485.rst | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/Documentation/driver-api/serial/serial-rs485.rst b/Documentation/driver-api/serial/serial-rs485.rst new file mode 100644 index 000000000000..6bc824f948f9 --- /dev/null +++ b/Documentation/driver-api/serial/serial-rs485.rst | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | =========================== | ||
| 2 | RS485 Serial Communications | ||
| 3 | =========================== | ||
| 4 | |||
| 5 | 1. Introduction | ||
| 6 | =============== | ||
| 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 | |||
| 15 | 2. Hardware-related Considerations | ||
| 16 | ================================== | ||
| 17 | |||
| 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. | ||
| 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 | |||
| 29 | 3. Data Structures Already Available in the Kernel | ||
| 30 | ================================================== | ||
| 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 | |||
| 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 | |||
| 40 | Any driver for devices capable of working both as RS232 and RS485 should | ||
| 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. | ||
| 45 | |||
| 46 | 4. Usage from user-level | ||
| 47 | ======================== | ||
| 48 | |||
| 49 | From user-level, RS485 configuration can be get/set using the previous | ||
| 50 | ioctls. For instance, to set RS485 you can use the following code:: | ||
| 51 | |||
| 52 | #include <linux/serial.h> | ||
| 53 | |||
| 54 | /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */ | ||
| 55 | #include <sys/ioctl.h> | ||
| 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 | |||
| 65 | /* Enable RS485 mode: */ | ||
| 66 | rs485conf.flags |= SER_RS485_ENABLED; | ||
| 67 | |||
| 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 | |||
| 78 | /* Set rts delay before send, if needed: */ | ||
| 79 | rs485conf.delay_rts_before_send = ...; | ||
| 80 | |||
| 81 | /* Set rts delay after send, if needed: */ | ||
| 82 | rs485conf.delay_rts_after_send = ...; | ||
| 83 | |||
| 84 | /* Set this flag if you want to receive data even while sending data */ | ||
| 85 | rs485conf.flags |= SER_RS485_RX_DURING_TX; | ||
| 86 | |||
| 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 | |||
| 98 | 5. References | ||
| 99 | ============= | ||
| 100 | |||
| 101 | [1] include/uapi/linux/serial.h | ||
| 102 | |||
| 103 | [2] Documentation/devicetree/bindings/serial/rs485.txt | ||
