You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.9 KiB

2 years ago
  1. /*
  2. * uart.c
  3. *
  4. * Created on: Aug 5, 2019
  5. * Author: Cristian Fatu
  6. * Implements basic UART functionality, over Uart Lite linux driver, using termios.
  7. * After booting linux, a device like "/dev/ttyUL1" must be present.
  8. * These functions work in both canonic and not canonic modes.
  9. * In the canonic communication mode, the received chars can be retrieved by read only after \n is detected.
  10. * In the non canonic communication mode, the received chars can be retrieved by read as they are received.
  11. */
  12. #include "uart.hpp"
  13. #include <fcntl.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <termios.h>
  18. #include <chrono>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <list>
  22. #include <map>
  23. #include <memory>
  24. #include <set>
  25. #include <sstream>
  26. #include <string>
  27. #include <vector>
  28. using namespace iflytop;
  29. using namespace chrono;
  30. static int64_t getnowms() { return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); }
  31. static map<string, uint32_t> s_baundmap = {
  32. {"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
  33. {"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
  34. {"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
  35. {"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
  36. {"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
  37. {"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
  38. {"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
  39. {"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017},
  40. };
  41. Uart::Uart() {}
  42. Uart::~Uart() {}
  43. int Uart::open(string path, string ratestr) {
  44. int rc;
  45. m_name = path;
  46. uint32_t rate = 0;
  47. m_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY);
  48. if (m_fd < 0) {
  49. // m_error = fmt::format("open {} failed,{}", path, strerror(errno));
  50. return -1;
  51. }
  52. if (s_baundmap.find(ratestr) == s_baundmap.end()) {
  53. // m_error = fmt::format("baund {} not support", ratestr);
  54. return -1;
  55. }
  56. rate = s_baundmap[ratestr];
  57. m_rate = ratestr;
  58. memset(&m_tty, 0, sizeof(m_tty));
  59. // memset(tty, 0, sizeof(struct termios));
  60. /*
  61. BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
  62. CRTSCTS : output hardware flow control (only used if the cable has
  63. all necessary lines. See sect. 7 of Serial-HOWTO)
  64. CS8 : 8n1 (8bit,no parity,1 stopbit)
  65. CLOCAL : local connection, no modem contol
  66. CREAD : enable receiving characters
  67. */
  68. // tty->c_cflag = dev->rate | CRTSCTS | CS8 | CLOCAL | CREAD;
  69. m_tty.c_cflag = rate | CS8 | CLOCAL | CREAD;
  70. // not canonic
  71. /*
  72. IGNPAR : ignore bytes with parity errorsc_cc[VTIME]
  73. */
  74. m_tty.c_iflag = IGNPAR;
  75. /* set input mode (non-canonical, no echo,...) */
  76. m_tty.c_lflag = 0;
  77. /* Do not wait for data */
  78. m_tty.c_cc[VTIME] = 10; /* inter-character timer unused */
  79. m_tty.c_cc[VMIN] = 0; /* blocking read until 5 chars received */
  80. /*
  81. Raw output.
  82. */
  83. m_tty.c_oflag = 0;
  84. /* Flush port */
  85. tcflush(m_fd, TCIFLUSH);
  86. /* Apply attributes */
  87. rc = tcsetattr(m_fd, TCSANOW, &m_tty);
  88. if (rc) {
  89. // m_error = fmt::format("tcsetattr {} failed,{}", path, strerror(errno));
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. int Uart::send(char *data, int size) {
  95. // if (logger->level() <= level::debug) {
  96. // logger->debug("{} send: {}", m_name, StringUtils().bytesToString((const uint8_t *)data, size));
  97. // }
  98. int sent = 0;
  99. sent = write(m_fd, data, size);
  100. if (sent < 0) {
  101. // m_error = fmt::format("write {} failed,{}", m_name, strerror(errno));
  102. }
  103. return sent;
  104. }
  105. int Uart::receive(char *data, int size_max) {
  106. int received = 0;
  107. received = read(m_fd, data, size_max);
  108. if (received < 0) {
  109. // m_error = fmt::format("read {} failed,{}", m_name, strerror(errno));
  110. }
  111. // if (logger->level() <= level::debug) {
  112. // logger->debug("{} receive: {}", m_name, StringUtils().bytesToString((const uint8_t *)data, received));
  113. // }
  114. return received;
  115. }
  116. int Uart::receive(char *data, int size, int overtimems) {
  117. if (m_fd < 0) return -1;
  118. int64_t now = getnowms();
  119. int64_t end = now + overtimems;
  120. int rc = 0;
  121. int total = 0;
  122. while (getnowms() < end) {
  123. rc = receive(data + total, size - total);
  124. if (rc > 0) {
  125. total += rc;
  126. if (total >= size) break;
  127. }
  128. usleep(333);
  129. }
  130. return total;
  131. }
  132. int Uart::close() {
  133. ::close(m_fd);
  134. m_fd = -1;
  135. return 0;
  136. }
  137. bool Uart::flush_rx() {
  138. if (m_fd < 0) return false;
  139. int rc = tcflush(m_fd, TCIFLUSH);
  140. return rc == 0;
  141. }
  142. bool Uart::flush_tx() {
  143. if (m_fd < 0) return false;
  144. int rc = tcflush(m_fd, TCOFLUSH);
  145. return rc == 0;
  146. }
  147. bool Uart::set_rx_overtime(int n100ms) {
  148. if (m_fd < 0) return false;
  149. m_tty.c_cc[VTIME] = n100ms;
  150. int rc = tcsetattr(this->m_fd, TCSANOW, &m_tty);
  151. return rc == 0;
  152. }