blob: 7c49b4f275b9d450bbdc91a0610794eadea00641 [file] [log] [blame]
Parker Schuh13696b32019-02-16 15:51:20 -08001#include "y2019/jevois/serial.h"
2
3#include "aos/logging/logging.h"
4
5#include <fcntl.h>
6#include <sys/stat.h>
7#include <sys/types.h>
8#include <termios.h>
9#include <unistd.h>
10
11namespace y2019 {
12namespace jevois {
13
14int open_via_terminos(const char *tty_name) {
15 int itsDev = ::open(tty_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
16 if (itsDev == -1) {
17 LOG(FATAL, "problem opening: %s\n", tty_name);
18 }
19
20 termios options = {};
21 if (tcgetattr(itsDev, &options) == -1) LOG(FATAL, "Failed to get options");
22
23 // get raw input from the port
24 options.c_cflag |= (CLOCAL // ignore modem control lines
25 | CREAD); // enable the receiver
26
27 options.c_iflag &= ~(IGNBRK // ignore BREAK condition on input
28 | BRKINT // If IGNBRK is not set, generate SIGINT on
29 // BREAK condition, else read BREAK as \0
30 | PARMRK | ISTRIP // strip off eighth bit
31 | INLCR // donot translate NL to CR on input
32 | IGNCR // ignore CR
33 | ICRNL // translate CR to newline on input
34 | IXON // disable XON/XOFF flow control on output
35 );
36
37 // disable implementation-defined output processing
38 options.c_oflag &= ~OPOST;
39 options.c_lflag &= ~(ECHO // dont echo i/p chars
40 | ECHONL // do not echo NL under any circumstance
41 | ICANON // disable cannonical mode
42 | ISIG // do not signal for INTR, QUIT, SUSP etc
43 | IEXTEN // disable platform dependent i/p processing
44 );
45
46 cfsetispeed(&options, B115200);
47 cfsetospeed(&options, B115200);
48
49 // Set the number of bits:
50 options.c_cflag &= ~CSIZE; // mask off the 'size' bits
51 options.c_cflag |= CS8;
52
53 // Set parity option:
54 options.c_cflag &= ~(PARENB | PARODD);
55
56 // Set the stop bits option:
57 options.c_cflag &= ~CSTOPB;
58
59 // Set the flow control:
60 options.c_cflag &= ~CRTSCTS;
61 options.c_iflag &= ~(IXON | IXANY | IXOFF);
62
63 options.c_cc[VMIN] = 1;
64 options.c_cc[VTIME] = 2;
65
66 // Flow control:
67 // flow soft:
68 // options.c_iflag |= (IXON | IXANY | IXOFF);
69 // flow hard:
70 // options.c_cflag |= CRTSCTS;
71
72 if (tcsetattr(itsDev, TCSANOW, &options) == -1)
73 LOG(FATAL, "Failed to set port options");
74 return itsDev;
75}
76
77} // namespace jevois
78} // namespace y2019