2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "peripheral_interface_uart.h"
21 #define PATH_BUF_MAX 64
22 #define UART_BUF_MAX 16
24 #define UART_BAUDRATE_SIZE 19
27 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
29 #define MAX_ERR_LEN 128
31 char *sysfs_uart_path[] = {
37 static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = {
38 B0, B50, B75, B110, B134,
39 B150, B200, B300, B600, B1200,
40 B1800, B2400, B4800, B9600, B19200,
41 B38400, B57600, B115200, B230400
44 static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
46 int peripheral_interface_uart_close(peripheral_uart_h uart)
50 peripheral_interface_uart_flush(uart);
52 ret = close(uart->fd);
53 CHECK_ERROR(ret != 0);
58 int peripheral_interface_uart_flush(peripheral_uart_h uart)
60 int ret = tcflush(uart->fd, TCIOFLUSH);
61 CHECK_ERROR(ret != 0);
66 int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
71 ret = tcgetattr(uart->fd, &tio);
72 CHECK_ERROR(ret != 0);
74 tio.c_cflag = peripheral_uart_br[baud];
81 peripheral_interface_uart_flush(uart);
82 ret = tcsetattr(uart->fd, TCSANOW, &tio);
83 CHECK_ERROR(ret != 0);
88 int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
93 ret = tcgetattr(uart->fd, &tio);
94 CHECK_ERROR(ret != 0);
97 tio.c_cflag &= ~CSIZE;
98 tio.c_cflag |= byteinfo[byte_size];
99 tio.c_cflag |= (CLOCAL | CREAD);
101 peripheral_interface_uart_flush(uart);
102 ret = tcsetattr(uart->fd, TCSANOW, &tio);
103 CHECK_ERROR(ret != 0);
108 int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
113 ret = tcgetattr(uart->fd, &tio);
114 CHECK_ERROR(ret != 0);
116 /* set parity info */
118 case PERIPHERAL_UART_PARITY_EVEN:
119 tio.c_cflag |= PARENB;
120 tio.c_cflag &= ~PARODD;
122 case PERIPHERAL_UART_PARITY_ODD:
123 tio.c_cflag |= PARENB;
124 tio.c_cflag |= PARODD;
126 case PERIPHERAL_UART_PARITY_NONE:
128 tio.c_cflag &= ~PARENB;
129 tio.c_cflag &= ~PARODD;
133 peripheral_interface_uart_flush(uart);
134 ret = tcsetattr(uart->fd, TCSANOW, &tio);
135 CHECK_ERROR(ret != 0);
140 int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
145 ret = tcgetattr(uart->fd, &tio);
146 CHECK_ERROR(ret != 0);
150 case PERIPHERAL_UART_STOP_BITS_1BIT:
151 tio.c_cflag &= ~CSTOPB;
153 case PERIPHERAL_UART_STOP_BITS_2BIT:
154 tio.c_cflag |= CSTOPB;
157 _E("Invalid parameter stop_bits");
161 peripheral_interface_uart_flush(uart);
162 ret = tcsetattr(uart->fd, TCSANOW, &tio);
163 CHECK_ERROR(ret != 0);
168 int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_software_flow_control_e xonxoff, peripheral_uart_hardware_flow_control_e rtscts)
173 ret = tcgetattr(uart->fd, &tio);
174 CHECK_ERROR(ret != 0);
176 if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS)
177 tio.c_cflag |= CRTSCTS;
178 else if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE)
179 tio.c_cflag &= ~CRTSCTS;
183 if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF)
184 tio.c_iflag |= (IXON | IXOFF | IXANY);
185 else if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE)
186 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
190 ret = tcsetattr(uart->fd, TCSANOW, &tio);
191 CHECK_ERROR(ret != 0);
196 int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
198 int ret = read(uart->fd, (void *)buf, length);
199 CHECK_ERROR(ret != length);
204 int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
206 int ret = write(uart->fd, buf, length);
207 CHECK_ERROR(ret != length);