serial-rs485.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 serial_rs485 structure (see [1]) to handle
  26. RS485 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 (see [2]
  29. for bindings). The driver is in charge of filling this data structure from
  30. the values given by the device tree.
  31. Any driver for devices capable of working both as RS232 and RS485 should
  32. implement the rs485_config callback in the uart_port structure. The
  33. serial_core calls rs485_config to do the device specific part in response
  34. to TIOCSRS485 and TIOCGRS485 ioctls (see below). The rs485_config callback
  35. receives a pointer to struct serial_rs485.
  36. 4. Usage from user-level
  37. ========================
  38. From user-level, RS485 configuration can be get/set using the previous
  39. ioctls. For instance, to set RS485 you can use the following code::
  40. #include <linux/serial.h>
  41. /* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
  42. #include <sys/ioctl.h>
  43. /* Open your specific device (e.g., /dev/mydevice): */
  44. int fd = open ("/dev/mydevice", O_RDWR);
  45. if (fd < 0) {
  46. /* Error handling. See errno. */
  47. }
  48. struct serial_rs485 rs485conf;
  49. /* Enable RS485 mode: */
  50. rs485conf.flags |= SER_RS485_ENABLED;
  51. /* Set logical level for RTS pin equal to 1 when sending: */
  52. rs485conf.flags |= SER_RS485_RTS_ON_SEND;
  53. /* or, set logical level for RTS pin equal to 0 when sending: */
  54. rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
  55. /* Set logical level for RTS pin equal to 1 after sending: */
  56. rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
  57. /* or, set logical level for RTS pin equal to 0 after sending: */
  58. rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
  59. /* Set rts delay before send, if needed: */
  60. rs485conf.delay_rts_before_send = ...;
  61. /* Set rts delay after send, if needed: */
  62. rs485conf.delay_rts_after_send = ...;
  63. /* Set this flag if you want to receive data even while sending data */
  64. rs485conf.flags |= SER_RS485_RX_DURING_TX;
  65. if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
  66. /* Error handling. See errno. */
  67. }
  68. /* Use read() and write() syscalls here... */
  69. /* Close the device when finished: */
  70. if (close (fd) < 0) {
  71. /* Error handling. See errno. */
  72. }
  73. 5. References
  74. =============
  75. [1] include/uapi/linux/serial.h
  76. [2] Documentation/devicetree/bindings/serial/rs485.txt