serial-rs485.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ===========================
  2. RS485 Serial Communications
  3. ===========================
  4. 1. Introduction
  5. ===============
  6. EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
  7. electrical characteristics of drivers and receivers for use in balanced
  8. digital multipoint systems.
  9. This standard is widely used for communications in industrial automation
  10. because it can be used effectively over long distances and in electrically
  11. noisy environments.
  12. 2. 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. For these microcontrollers, the Linux driver should be made capable of
  20. working in both modes, and proper ioctls (see later) should be made
  21. available at user-level to allow switching from one mode to the other, and
  22. vice versa.
  23. 3. Data Structures Already Available in the Kernel
  24. ==================================================
  25. The Linux kernel provides the struct serial_rs485 to handle RS485
  26. communications. This data structure is used to set and configure RS485
  27. parameters in the platform data and in ioctls.
  28. The device tree can also provide RS485 boot time parameters
  29. [#DT-bindings]_. The serial core fills the struct serial_rs485 from the
  30. values given by the device tree when the driver calls
  31. uart_get_rs485_mode().
  32. Any driver for devices capable of working both as RS232 and RS485 should
  33. implement the ``rs485_config`` callback and provide ``rs485_supported``
  34. in the ``struct uart_port``. The serial core calls ``rs485_config`` to do
  35. the device specific part in response to TIOCSRS485 ioctl (see below). The
  36. ``rs485_config`` callback receives a pointer to a sanitizated struct
  37. serial_rs485. The struct serial_rs485 userspace provides is sanitized
  38. before calling ``rs485_config`` using ``rs485_supported`` that indicates
  39. what RS485 features the driver supports for the ``struct uart_port``.
  40. TIOCGRS485 ioctl can be used to read back the struct serial_rs485
  41. matching to the current configuration.
  42. .. kernel-doc:: include/uapi/linux/serial.h
  43. :identifiers: serial_rs485 uart_get_rs485_mode
  44. 4. Usage from user-level
  45. ========================
  46. From user-level, RS485 configuration can be get/set using the previous
  47. ioctls. For instance, to set RS485 you can use the following code::
  48. #include <linux/serial.h>
  49. /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
  50. #include <sys/ioctl.h>
  51. /* Open your specific device (e.g., /dev/mydevice): */
  52. int fd = open ("/dev/mydevice", O_RDWR);
  53. if (fd < 0) {
  54. /* Error handling. See errno. */
  55. }
  56. struct serial_rs485 rs485conf;
  57. /* Enable RS485 mode: */
  58. rs485conf.flags |= SER_RS485_ENABLED;
  59. /* Set logical level for RTS pin equal to 1 when sending: */
  60. rs485conf.flags |= SER_RS485_RTS_ON_SEND;
  61. /* or, set logical level for RTS pin equal to 0 when sending: */
  62. rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
  63. /* Set logical level for RTS pin equal to 1 after sending: */
  64. rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
  65. /* or, set logical level for RTS pin equal to 0 after sending: */
  66. rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
  67. /* Set rts delay before send, if needed: */
  68. rs485conf.delay_rts_before_send = ...;
  69. /* Set rts delay after send, if needed: */
  70. rs485conf.delay_rts_after_send = ...;
  71. /* Set this flag if you want to receive data even while sending data */
  72. rs485conf.flags |= SER_RS485_RX_DURING_TX;
  73. if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
  74. /* Error handling. See errno. */
  75. }
  76. /* Use read() and write() syscalls here... */
  77. /* Close the device when finished: */
  78. if (close (fd) < 0) {
  79. /* Error handling. See errno. */
  80. }
  81. 5. Multipoint Addressing
  82. ========================
  83. The Linux kernel provides addressing mode for multipoint RS-485 serial
  84. communications line. The addressing mode is enabled with
  85. ``SER_RS485_ADDRB`` flag in struct serial_rs485. The struct serial_rs485
  86. has two additional flags and fields for enabling receive and destination
  87. addresses.
  88. Address mode flags:
  89. - ``SER_RS485_ADDRB``: Enabled addressing mode (sets also ADDRB in termios).
  90. - ``SER_RS485_ADDR_RECV``: Receive (filter) address enabled.
  91. - ``SER_RS485_ADDR_DEST``: Set destination address.
  92. Address fields (enabled with corresponding ``SER_RS485_ADDR_*`` flag):
  93. - ``addr_recv``: Receive address.
  94. - ``addr_dest``: Destination address.
  95. Once a receive address is set, the communication can occur only with the
  96. particular device and other peers are filtered out. It is left up to the
  97. receiver side to enforce the filtering. Receive address will be cleared
  98. if ``SER_RS485_ADDR_RECV`` is not set.
  99. Note: not all devices supporting RS485 support multipoint addressing.
  100. 6. References
  101. =============
  102. .. [#DT-bindings] Documentation/devicetree/bindings/serial/rs485.txt