From f30abc1f75f483ffe78d92d5109b6c1d700925a3 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Fri, 23 Sep 2022 20:20:38 +0200 Subject: added x230x programming and switching utility; fe310 flash utility; --- util/tty.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 util/tty.c (limited to 'util/tty.c') diff --git a/util/tty.c b/util/tty.c new file mode 100644 index 0000000..be56b44 --- /dev/null +++ b/util/tty.c @@ -0,0 +1,48 @@ +#include + +#include +#include +#include + +int tty_open(char *tty_fn) { + struct termios tty; + int tty_fd; + int rv; + + tty_fd = open(tty_fn, O_RDWR); + if (tty_fd < 0) return tty_fd; + + rv = tcgetattr(tty_fd, &tty); + if (rv) return -1; + + tty.c_cflag &= ~PARENB; // No parity + tty.c_cflag &= ~CSTOPB; // One stop bit + tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size + tty.c_cflag |= CS8; // 8 bits per byte + tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hw flow control + tty.c_cflag |= (CREAD | CLOCAL); // Turn on READ & ignore ctrl lines (CLOCAL = 1) + + tty.c_lflag &= ~ICANON; + tty.c_lflag &= ~(ECHO | ECHOE |ECHONL); // Disable echo + tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP signals + tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off sw flow ctrl + tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes + + tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) + tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed +#ifndef __linux__ + tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX) + tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX) +#endif + + tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received. + tty.c_cc[VMIN] = 0; + + cfsetispeed(&tty, B115200); + cfsetospeed(&tty, B115200); + + rv = tcsetattr(tty_fd, TCSANOW, &tty); + if (rv) return -1; + + return tty_fd; +}; -- cgit v1.2.3