UARTDevice.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "UARTDevice.h"
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <poll.h>
  5. #include <stdio.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. #include <AP_HAL/AP_HAL.h>
  9. UARTDevice::UARTDevice(const char *device_path):
  10. _device_path(device_path)
  11. {
  12. }
  13. UARTDevice::~UARTDevice()
  14. {
  15. }
  16. bool UARTDevice::close()
  17. {
  18. if (_fd != -1) {
  19. if (::close(_fd) < 0) {
  20. return false;
  21. }
  22. }
  23. _fd = -1;
  24. return true;
  25. }
  26. bool UARTDevice::open()
  27. {
  28. _fd = ::open(_device_path, O_RDWR | O_CLOEXEC | O_NOCTTY);
  29. if (_fd < 0) {
  30. ::fprintf(stderr, "Failed to open UART device %s - %s\n",
  31. _device_path, strerror(errno));
  32. return false;
  33. }
  34. _disable_crlf();
  35. return true;
  36. }
  37. ssize_t UARTDevice::read(uint8_t *buf, uint16_t n)
  38. {
  39. return ::read(_fd, buf, n);
  40. }
  41. ssize_t UARTDevice::write(const uint8_t *buf, uint16_t n)
  42. {
  43. struct pollfd fds;
  44. fds.fd = _fd;
  45. fds.events = POLLOUT;
  46. fds.revents = 0;
  47. int ret = 0;
  48. if (poll(&fds, 1, 0) == 1) {
  49. ret = ::write(_fd, buf, n);
  50. }
  51. return ret;
  52. }
  53. void UARTDevice::set_blocking(bool blocking)
  54. {
  55. int flags = fcntl(_fd, F_GETFL, 0);
  56. if (blocking) {
  57. flags = flags & ~O_NONBLOCK;
  58. } else {
  59. flags = flags | O_NONBLOCK;
  60. }
  61. if (fcntl(_fd, F_SETFL, flags) < 0) {
  62. ::fprintf(stderr, "Failed to make UART nonblocking %s - %s\n",
  63. _device_path, strerror(errno));
  64. }
  65. }
  66. void UARTDevice::_disable_crlf()
  67. {
  68. struct termios t;
  69. memset(&t, 0, sizeof(t));
  70. tcgetattr(_fd, &t);
  71. // disable LF -> CR/LF
  72. t.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL | IXON | IXOFF);
  73. t.c_oflag &= ~(OPOST | ONLCR);
  74. t.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
  75. t.c_cc[VMIN] = 0;
  76. tcsetattr(_fd, TCSANOW, &t);
  77. }
  78. void UARTDevice::set_speed(uint32_t baudrate)
  79. {
  80. struct termios t;
  81. memset(&t, 0, sizeof(t));
  82. tcgetattr(_fd, &t);
  83. cfsetspeed(&t, baudrate);
  84. tcsetattr(_fd, TCSANOW, &t);
  85. }
  86. void UARTDevice::set_flow_control(AP_HAL::UARTDriver::flow_control flow_control_setting)
  87. {
  88. struct termios t;
  89. if (_flow_control == flow_control_setting) {
  90. return;
  91. }
  92. tcgetattr(_fd, &t);
  93. if (flow_control_setting != AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE) {
  94. t.c_cflag |= CRTSCTS;
  95. } else {
  96. t.c_cflag &= ~CRTSCTS;
  97. }
  98. tcsetattr(_fd, TCSANOW, &t);
  99. _flow_control = flow_control_setting;
  100. }
  101. void UARTDevice::set_parity(int v)
  102. {
  103. struct termios t;
  104. tcgetattr(_fd, &t);
  105. if (v != 0) {
  106. // enable parity
  107. t.c_cflag |= PARENB;
  108. if (v == 1) {
  109. t.c_cflag |= PARODD;
  110. } else {
  111. t.c_cflag &= ~PARODD;
  112. }
  113. }
  114. else {
  115. // disable parity
  116. t.c_cflag &= ~PARENB;
  117. }
  118. tcsetattr(_fd, TCSANOW, &t);
  119. }