serial-rs485.txt 3.4 KB

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