blob: 5febbb4fc43aa501a95155916d864b4f4cbddf85 [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) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070017 AOS_LOG(FATAL, "problem opening: %s\n", tty_name);
Parker Schuh13696b32019-02-16 15:51:20 -080018 }
19
20 termios options = {};
Austin Schuhf257f3c2019-10-27 21:00:43 -070021 if (tcgetattr(itsDev, &options) == -1)
22 AOS_LOG(FATAL, "Failed to get options");
Parker Schuh13696b32019-02-16 15:51:20 -080023
24 // get raw input from the port
25 options.c_cflag |= (CLOCAL // ignore modem control lines
26 | CREAD); // enable the receiver
27
28 options.c_iflag &= ~(IGNBRK // ignore BREAK condition on input
29 | BRKINT // If IGNBRK is not set, generate SIGINT on
30 // BREAK condition, else read BREAK as \0
31 | PARMRK | ISTRIP // strip off eighth bit
32 | INLCR // donot translate NL to CR on input
33 | IGNCR // ignore CR
34 | ICRNL // translate CR to newline on input
35 | IXON // disable XON/XOFF flow control on output
36 );
37
38 // disable implementation-defined output processing
39 options.c_oflag &= ~OPOST;
40 options.c_lflag &= ~(ECHO // dont echo i/p chars
41 | ECHONL // do not echo NL under any circumstance
42 | ICANON // disable cannonical mode
43 | ISIG // do not signal for INTR, QUIT, SUSP etc
44 | IEXTEN // disable platform dependent i/p processing
45 );
46
47 cfsetispeed(&options, B115200);
48 cfsetospeed(&options, B115200);
49
50 // Set the number of bits:
51 options.c_cflag &= ~CSIZE; // mask off the 'size' bits
52 options.c_cflag |= CS8;
53
54 // Set parity option:
55 options.c_cflag &= ~(PARENB | PARODD);
56
57 // Set the stop bits option:
58 options.c_cflag &= ~CSTOPB;
59
60 // Set the flow control:
61 options.c_cflag &= ~CRTSCTS;
62 options.c_iflag &= ~(IXON | IXANY | IXOFF);
63
64 options.c_cc[VMIN] = 1;
65 options.c_cc[VTIME] = 2;
66
67 // Flow control:
68 // flow soft:
69 // options.c_iflag |= (IXON | IXANY | IXOFF);
70 // flow hard:
71 // options.c_cflag |= CRTSCTS;
72
73 if (tcsetattr(itsDev, TCSANOW, &options) == -1)
Austin Schuhf257f3c2019-10-27 21:00:43 -070074 AOS_LOG(FATAL, "Failed to set port options");
Parker Schuh13696b32019-02-16 15:51:20 -080075 return itsDev;
76}
77
78} // namespace jevois
79} // namespace y2019