|
|
/*
* uart.c * * Created on: Aug 5, 2019 * Author: Cristian Fatu * Implements basic UART functionality, over Uart Lite linux driver, using termios. * After booting linux, a device like "/dev/ttyUL1" must be present. * These functions work in both canonic and not canonic modes. * In the canonic communication mode, the received chars can be retrieved by read only after \n is detected. * In the non canonic communication mode, the received chars can be retrieved by read as they are received. */ #include "uart.hpp"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace iflytop; using namespace chrono;
static int64_t getnowms() { return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); }
static map<string, uint32_t> s_baundmap = { {"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
{"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
{"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
{"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
{"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
{"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
{"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
{"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017}, };
Uart::Uart() {} Uart::~Uart() {}
int Uart::open(string path, string ratestr) { int rc;
m_name = path; uint32_t rate = 0;
m_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY); if (m_fd < 0) { // m_error = fmt::format("open {} failed,{}", path, strerror(errno));
return -1; }
if (s_baundmap.find(ratestr) == s_baundmap.end()) { // m_error = fmt::format("baund {} not support", ratestr);
return -1; } rate = s_baundmap[ratestr]; m_rate = ratestr; memset(&m_tty, 0, sizeof(m_tty)); // memset(tty, 0, sizeof(struct termios));
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. CRTSCTS : output hardware flow control (only used if the cable has all necessary lines. See sect. 7 of Serial-HOWTO) CS8 : 8n1 (8bit,no parity,1 stopbit) CLOCAL : local connection, no modem contol CREAD : enable receiving characters */ // tty->c_cflag = dev->rate | CRTSCTS | CS8 | CLOCAL | CREAD;
m_tty.c_cflag = rate | CS8 | CLOCAL | CREAD;
// not canonic
/*
IGNPAR : ignore bytes with parity errorsc_cc[VTIME] */ m_tty.c_iflag = IGNPAR; /* set input mode (non-canonical, no echo,...) */ m_tty.c_lflag = 0; /* Do not wait for data */ m_tty.c_cc[VTIME] = 10; /* inter-character timer unused */ m_tty.c_cc[VMIN] = 0; /* blocking read until 5 chars received */
/*
Raw output. */ m_tty.c_oflag = 0;
/* Flush port */ tcflush(m_fd, TCIFLUSH);
/* Apply attributes */ rc = tcsetattr(m_fd, TCSANOW, &m_tty); if (rc) { // m_error = fmt::format("tcsetattr {} failed,{}", path, strerror(errno));
return -1; } return 0; } int Uart::send(char *data, int size) { // if (logger->level() <= level::debug) {
// logger->debug("{} send: {}", m_name, StringUtils().bytesToString((const uint8_t *)data, size));
// }
int sent = 0; sent = write(m_fd, data, size); if (sent < 0) { // m_error = fmt::format("write {} failed,{}", m_name, strerror(errno));
} return sent; } int Uart::receive(char *data, int size_max) { int received = 0; received = read(m_fd, data, size_max); if (received < 0) { // m_error = fmt::format("read {} failed,{}", m_name, strerror(errno));
} // if (logger->level() <= level::debug) {
// logger->debug("{} receive: {}", m_name, StringUtils().bytesToString((const uint8_t *)data, received));
// }
return received; } int Uart::receive(char *data, int size, int overtimems) { if (m_fd < 0) return -1;
int64_t now = getnowms(); int64_t end = now + overtimems; int rc = 0; int total = 0; while (getnowms() < end) { rc = receive(data + total, size - total); if (rc > 0) { total += rc; if (total >= size) break; } usleep(333); } return total; } int Uart::close() { ::close(m_fd); m_fd = -1; return 0; } bool Uart::flush_rx() { if (m_fd < 0) return false; int rc = tcflush(m_fd, TCIFLUSH); return rc == 0; } bool Uart::flush_tx() { if (m_fd < 0) return false; int rc = tcflush(m_fd, TCOFLUSH); return rc == 0; } bool Uart::set_rx_overtime(int n100ms) { if (m_fd < 0) return false; m_tty.c_cc[VTIME] = n100ms; int rc = tcsetattr(this->m_fd, TCSANOW, &m_tty); return rc == 0; }
|