serial-rs485.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 (e.g., Atmel AT91) contain a built-in half-duplex mode capable of
  11. automatically controlling line direction by toggling RTS. That can used to
  12. control external half-duplex hardware like an RS485 transceiver or any
  13. RS232-connected half-duplex device like some modems.
  14. For these microcontrollers, the Linux driver should be made capable of
  15. working in both modes, and proper ioctls (see later) should be made
  16. available at user-level to allow switching from one mode to the other, and
  17. vice versa.
  18. 3. DATA STRUCTURES ALREADY AVAILABLE IN THE KERNEL
  19. The Linux kernel provides the serial_rs485 structure (see [1]) to handle
  20. RS485 communications. This data structure is used to set and configure RS485
  21. parameters in the platform data and in ioctls.
  22. Any driver for devices capable of working both as RS232 and RS485 should
  23. provide at least the following ioctls:
  24. - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used
  25. to enable/disable RS485 mode from user-space
  26. - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used
  27. to get RS485 mode from kernel-space (i.e., driver) to user-space.
  28. In other words, the serial driver should contain a code similar to the next
  29. one:
  30. static struct uart_ops atmel_pops = {
  31. /* ... */
  32. .ioctl = handle_ioctl,
  33. };
  34. static int handle_ioctl(struct uart_port *port,
  35. unsigned int cmd,
  36. unsigned long arg)
  37. {
  38. struct serial_rs485 rs485conf;
  39. switch (cmd) {
  40. case TIOCSRS485:
  41. if (copy_from_user(&rs485conf,
  42. (struct serial_rs485 *) arg,
  43. sizeof(rs485conf)))
  44. return -EFAULT;
  45. /* ... */
  46. break;
  47. case TIOCGRS485:
  48. if (copy_to_user((struct serial_rs485 *) arg,
  49. ...,
  50. sizeof(rs485conf)))
  51. return -EFAULT;
  52. /* ... */
  53. break;
  54. /* ... */
  55. }
  56. }
  57. 4. USAGE FROM USER-LEVEL
  58. From user-level, RS485 configuration can be get/set using the previous
  59. ioctls. For instance, to set RS485 you can use the following code:
  60. #include <linux/serial.h>
  61. /* Driver-specific ioctls: */
  62. #define TIOCGRS485 0x542E
  63. #define TIOCSRS485 0x542F
  64. /* Open your specific device (e.g., /dev/mydevice): */
  65. int fd = open ("/dev/mydevice", O_RDWR);
  66. if (fd < 0) {
  67. /* Error handling. See errno. */
  68. }
  69. struct serial_rs485 rs485conf;
  70. /* Set RS485 mode: */
  71. rs485conf.flags |= SER_RS485_ENABLED;
  72. /* Set rts delay before send, if needed: */
  73. rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND;
  74. rs485conf.delay_rts_before_send = ...;
  75. /* Set rts delay after send, if needed: */
  76. rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
  77. rs485conf.delay_rts_after_send = ...;
  78. if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
  79. /* Error handling. See errno. */
  80. }
  81. /* Use read() and write() syscalls here... */
  82. /* Close the device when finished: */
  83. if (close (fd) < 0) {
  84. /* Error handling. See errno. */
  85. }
  86. 5. REFERENCES
  87. [1] include/linux/serial.h