serial-rs485.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. Any driver for devices capable of working both as RS232 and RS485 should
  24. provide at least the following ioctls:
  25. - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used
  26. to enable/disable RS485 mode from user-space
  27. - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used
  28. to get RS485 mode from kernel-space (i.e., driver) to user-space.
  29. In other words, the serial driver should contain a code similar to the next
  30. one:
  31. static struct uart_ops atmel_pops = {
  32. /* ... */
  33. .ioctl = handle_ioctl,
  34. };
  35. static int handle_ioctl(struct uart_port *port,
  36. unsigned int cmd,
  37. unsigned long arg)
  38. {
  39. struct serial_rs485 rs485conf;
  40. switch (cmd) {
  41. case TIOCSRS485:
  42. if (copy_from_user(&rs485conf,
  43. (struct serial_rs485 *) arg,
  44. sizeof(rs485conf)))
  45. return -EFAULT;
  46. /* ... */
  47. break;
  48. case TIOCGRS485:
  49. if (copy_to_user((struct serial_rs485 *) arg,
  50. ...,
  51. sizeof(rs485conf)))
  52. return -EFAULT;
  53. /* ... */
  54. break;
  55. /* ... */
  56. }
  57. }
  58. 4. USAGE FROM USER-LEVEL
  59. From user-level, RS485 configuration can be get/set using the previous
  60. ioctls. For instance, to set RS485 you can use the following code:
  61. #include <linux/serial.h>
  62. /* Driver-specific ioctls: */
  63. #define TIOCGRS485 0x542E
  64. #define TIOCSRS485 0x542F
  65. /* Open your specific device (e.g., /dev/mydevice): */
  66. int fd = open ("/dev/mydevice", O_RDWR);
  67. if (fd < 0) {
  68. /* Error handling. See errno. */
  69. }
  70. struct serial_rs485 rs485conf;
  71. /* Set RS485 mode: */
  72. rs485conf.flags |= SER_RS485_ENABLED;
  73. /* Set rts delay before send, if needed: */
  74. rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND;
  75. rs485conf.delay_rts_before_send = ...;
  76. /* Set rts delay after send, if needed: */
  77. rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
  78. rs485conf.delay_rts_after_send = ...;
  79. if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
  80. /* Error handling. See errno. */
  81. }
  82. /* Use read() and write() syscalls here... */
  83. /* Close the device when finished: */
  84. if (close (fd) < 0) {
  85. /* Error handling. See errno. */
  86. }
  87. 5. REFERENCES
  88. [1] include/linux/serial.h