ca_uart.c (3227B)
1 /* 2 This file is part of TALER cash2ecash 3 Copyright (C) 2026 GNUnet e.V. 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU Affero General Public License as 7 published by the Free Software Foundation, either version 3 of the 8 License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 /** 21 * @file uart_wrapper.c 22 * @brief read UART signals 23 * @author Reto Tellenbach 24 */ 25 26 #include <termios.h> 27 #include <fcntl.h> 28 #include <stdlib.h> 29 #include <unistd.h> 30 #include "ca_uart.h" 31 #include <stdio.h> 32 33 int 34 ca_uart_init(const char *dev) 35 { 36 int uart_filestream; 37 uart_filestream = 0; 38 // Config UART0 39 uart_filestream = open(dev, O_RDONLY | O_NOCTTY | O_NDELAY); 40 // O_RDONLY: read only 41 // O_NOCTTY: The port never becomes the controlling terminal of the process 42 // O_NDELAY: Use non-blocking I/O 43 if (-1 == uart_filestream) 44 { 45 perror ("open uart"); 46 return -1; 47 } 48 struct termios options; 49 if(0 != tcgetattr(uart_filestream, 50 &options)) 51 { 52 close(uart_filestream); 53 return -1; 54 } 55 cfmakeraw(&options); 56 if(0 != cfsetspeed(&options, B9600)) 57 { 58 close(uart_filestream); 59 return -1; 60 } 61 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // RAW input 62 options.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit 63 //options.c_cflag |= PARENB; // Parity even bit enabled 64 options.c_cflag &= ~PARENB; /* disable parity */ 65 options.c_cflag &= ~PARODD; // clear Parity odd bit 66 //options.c_iflag |= (INPCK); // check and paryty bit 67 options.c_iflag &= ~(INPCK); 68 options.c_iflag |= IGNPAR; /* ignore parity errors */ 69 options.c_iflag &= ~ISTRIP; // parity bit is 9th bit therfor cleared this option 70 options.c_iflag |= (IGNBRK); // ignore BREAK condition input 71 options.c_cflag |= CS8; // Size: 8 Bits 72 options.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common) 73 options.c_cflag |= (CREAD | CLOCAL); // These will ensure that your program does not become the 'owner' of the port subject to sporatic job control and hangup signals, and also that the serial interface driver will read incoming data bytes. 74 options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow controll 75 options.c_cc[VTIME] = 0; 76 options.c_cc[VMIN] = 0; 77 78 //Flush buffers and apply setti 79 if(0 != tcsetattr(uart_filestream, TCSANOW, &options)) 80 { 81 close(uart_filestream); 82 return -1; 83 } 84 if(0 != tcflush (uart_filestream, TCIFLUSH)) 85 { 86 close(uart_filestream); 87 return -1; 88 } 89 90 return uart_filestream; 91 } 92 93