UART_utils.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <AP_HAL/AP_HAL.h>
  18. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  19. #include "UARTDriver.h"
  20. #if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(__APPLE__)
  21. #define USE_TERMIOS
  22. #endif
  23. #ifdef USE_TERMIOS
  24. #include <termios.h>
  25. #else
  26. #include <asm/ioctls.h>
  27. #include <asm/termbits.h>
  28. #endif
  29. #include <string.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. bool HALSITL::UARTDriver::set_speed(int speed)
  34. {
  35. if (_fd < 0) {
  36. return false;
  37. }
  38. #ifdef USE_TERMIOS
  39. struct termios t;
  40. tcgetattr(_fd, &t);
  41. cfsetspeed(&t, speed);
  42. tcsetattr(_fd, TCSANOW, &t);
  43. #else
  44. struct termios2 tc;
  45. memset(&tc, 0, sizeof(tc));
  46. if (ioctl(_fd, TCGETS2, &tc) == -1) {
  47. return false;
  48. }
  49. /* speed is configured by c_[io]speed */
  50. tc.c_cflag &= ~CBAUD;
  51. tc.c_cflag |= BOTHER;
  52. tc.c_ispeed = speed;
  53. tc.c_ospeed = speed;
  54. if (ioctl(_fd, TCSETS2, &tc) == -1) {
  55. return false;
  56. }
  57. if (ioctl(_fd, TCFLSH, TCIOFLUSH) == -1) {
  58. return false;
  59. }
  60. #endif
  61. return true;
  62. }
  63. void HALSITL::UARTDriver::configure_parity(uint8_t v)
  64. {
  65. if (_fd < 0) {
  66. return;
  67. }
  68. #ifdef USE_TERMIOS
  69. struct termios t;
  70. tcgetattr(_fd, &t);
  71. #else
  72. struct termios2 t;
  73. memset(&t, 0, sizeof(t));
  74. if (ioctl(_fd, TCGETS2, &t) == -1) {
  75. return;
  76. }
  77. #endif
  78. if (v != 0) {
  79. // enable parity
  80. t.c_cflag |= PARENB;
  81. if (v == 1) {
  82. t.c_cflag |= PARODD;
  83. } else {
  84. t.c_cflag &= ~PARODD;
  85. }
  86. }
  87. else {
  88. // disable parity
  89. t.c_cflag &= ~PARENB;
  90. }
  91. #ifdef USE_TERMIOS
  92. tcsetattr(_fd, TCSANOW, &t);
  93. #else
  94. ioctl(_fd, TCSETS2, &t);
  95. #endif
  96. }
  97. void HALSITL::UARTDriver::set_stop_bits(int n)
  98. {
  99. if (_fd < 0) {
  100. return;
  101. }
  102. #ifdef USE_TERMIOS
  103. struct termios t;
  104. tcgetattr(_fd, &t);
  105. #else
  106. struct termios2 t;
  107. memset(&t, 0, sizeof(t));
  108. if (ioctl(_fd, TCGETS2, &t) == -1) {
  109. return;
  110. }
  111. #endif
  112. if (n > 1) {
  113. t.c_cflag |= CSTOPB;
  114. } else {
  115. t.c_cflag &= ~CSTOPB;
  116. }
  117. #ifdef USE_TERMIOS
  118. tcsetattr(_fd, TCSANOW, &t);
  119. #else
  120. ioctl(_fd, TCSETS2, &t);
  121. #endif
  122. }
  123. #endif