serial-rs485.txt 4.0 KB

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