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.
25 #include <sys/ioctl.h>
28 #include "peripheral_common.h"
30 #define PATH_BUF_MAX 64
31 #define UART_BUF_MAX 16
33 #define UART_BAUDRATE_SIZE 19
36 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
39 char *sysfs_uart_path[] = {
45 static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = {
46 B0, B50, B75, B110, B134,
47 B150, B200, B300, B600, B1200,
48 B1800, B2400, B4800, B9600, B19200,
49 B38400, B57600, B115200, B230400
52 static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
54 int uart_open(int port, int *file_hndl)
57 char uart_dev[PATH_BUF_MAX] = {0};
59 _D("port : %d", port);
61 for (i = 0; i < ARRAY_SIZE(sysfs_uart_path); i++) {
62 snprintf(uart_dev, PATH_BUF_MAX, "%s%d", sysfs_uart_path[i], port);
63 if (access(uart_dev, F_OK) == 0)
67 if ((fd = open(uart_dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
68 _E("Error[%d]: can't open %s, %s--[%d]\n", errno, uart_dev, __FUNCTION__, __LINE__);
75 int uart_close(int file_hndl)
77 _D("file_hndl : %d", file_hndl);
80 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
87 int uart_flush(int file_hndl)
92 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
96 ret = tcflush(file_hndl, TCIOFLUSH);
98 _E("FAILED[%d]: tcflush, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
105 int uart_set_baudrate(int file_hndl, uart_baudrate_e baud)
110 _D("file_hndl : %d, baud : %d", file_hndl, baud);
112 memset(&tio, 0, sizeof(tio));
114 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
118 if (baud > UART_BAUDRATE_230400) {
119 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
123 ret = tcgetattr(file_hndl, &tio);
125 _E("Error[%d]: tcgetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
128 tio.c_cflag = peripheral_uart_br[baud];
129 tio.c_iflag = IGNPAR;
135 uart_flush(file_hndl);
136 ret = tcsetattr(file_hndl, TCSANOW, &tio);
138 _E("Error[%d]: tcsetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
145 int uart_set_mode(int file_hndl, uart_bytesize_e bytesize, uart_parity_e parity, uart_stopbits_e stopbits)
150 _D("file_hndl : %d, bytesize : %d, parity : %d, stopbits : %d", file_hndl, bytesize, parity, stopbits);
153 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
157 ret = tcgetattr(file_hndl, &tio);
159 _E("Error[%d]: tcgetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
164 if (bytesize < UART_BYTESIZE_5BIT || bytesize > UART_BYTESIZE_8BIT) {
165 _E("Error[%d]: Invalid parameter bytesize, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
168 tio.c_cflag &= ~CSIZE;
169 tio.c_cflag |= byteinfo[bytesize];
170 tio.c_cflag |= (CLOCAL | CREAD);
172 /* set parity info */
174 case UART_PARITY_EVEN:
175 tio.c_cflag |= PARENB;
176 tio.c_cflag &= ~PARODD;
178 case UART_PARITY_ODD:
179 tio.c_cflag |= PARENB;
180 tio.c_cflag |= PARODD;
182 case UART_PARITY_NONE:
184 tio.c_cflag &= ~PARENB;
185 tio.c_cflag &= ~PARODD;
191 case UART_STOPBITS_1BIT:
192 tio.c_cflag &= ~CSTOPB;
194 case UART_STOPBITS_2BIT:
195 tio.c_cflag |= CSTOPB;
198 _E("Error[%d]: Invalid parameter stopbits, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
202 uart_flush(file_hndl);
203 ret = tcsetattr(file_hndl, TCSANOW, &tio);
205 _E("Error[%d]: tcsetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
212 int uart_set_flowcontrol(int file_hndl, bool xonxoff, bool rtscts)
217 _D("file_hndl : %d, xonxoff : %d, rtscts : %d", file_hndl, xonxoff, rtscts);
220 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
224 ret = tcgetattr(file_hndl, &tio);
226 _E("Error[%d]: tcgetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
230 /* rtscts => 1: rts/cts on, 0: off */
232 tio.c_cflag |= CRTSCTS;
234 tio.c_cflag &= ~CRTSCTS;
236 /* xonxoff => 1: xon/xoff on, 0: off */
238 tio.c_iflag |= (IXON | IXOFF | IXANY);
240 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
242 ret = tcsetattr(file_hndl, TCSANOW, &tio);
244 _E("Error[%d]: tcsetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
251 int uart_read(int file_hndl, uint8_t *buf, unsigned int length)
255 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
259 ret = read(file_hndl, (void *)buf, length);
260 if ((errno != EAGAIN && errno != EINTR) && ret < 0) {
261 _E("Error[%d]: read, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
268 int uart_write(int file_hndl, uint8_t *buf, unsigned int length)
272 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
276 ret = write(file_hndl, buf, length);
278 _E("Error[%d]: write, %s--[%d]\n", errno, __FUNCTION__, __LINE__);