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]))
38 #define MAX_ERR_LEN 128
40 char *sysfs_uart_path[] = {
46 static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = {
47 B0, B50, B75, B110, B134,
48 B150, B200, B300, B600, B1200,
49 B1800, B2400, B4800, B9600, B19200,
50 B38400, B57600, B115200, B230400
53 static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
55 int uart_open(int port, int *file_hndl)
58 char uart_dev[PATH_BUF_MAX] = {0};
60 _D("port : %d", port);
62 for (i = 0; i < ARRAY_SIZE(sysfs_uart_path); i++) {
63 snprintf(uart_dev, PATH_BUF_MAX, "%s%d", sysfs_uart_path[i], port);
64 if (access(uart_dev, F_OK) == 0)
67 _D("uart_dev : %s", uart_dev);
68 if ((fd = open(uart_dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
69 char errmsg[MAX_ERR_LEN];
70 strerror_r(errno, errmsg, MAX_ERR_LEN);
71 _E("can't open %s, errmsg : %s", uart_dev, errmsg);
78 int uart_close(int file_hndl)
80 _D("file_hndl : %d", file_hndl);
83 _E("Invalid NULL parameter");
90 int uart_flush(int file_hndl)
95 _E("Invalid NULL parameter");
99 ret = tcflush(file_hndl, TCIOFLUSH);
101 char errmsg[MAX_ERR_LEN];
102 strerror_r(errno, errmsg, MAX_ERR_LEN);
103 _E("tcflush failed, errmsg : %s", errmsg);
110 int uart_set_baudrate(int file_hndl, uart_baudrate_e baud)
115 _D("file_hndl : %d, baud : %d", file_hndl, baud);
117 memset(&tio, 0, sizeof(tio));
119 _E("Invalid NULL parameter");
123 if (baud > UART_BAUDRATE_230400) {
124 _E("Invalid parameter");
128 ret = tcgetattr(file_hndl, &tio);
130 char errmsg[MAX_ERR_LEN];
131 strerror_r(errno, errmsg, MAX_ERR_LEN);
132 _E("tcgetattr failed, errmsg : %s", errmsg);
135 tio.c_cflag = peripheral_uart_br[baud];
136 tio.c_iflag = IGNPAR;
142 uart_flush(file_hndl);
143 ret = tcsetattr(file_hndl, TCSANOW, &tio);
145 char errmsg[MAX_ERR_LEN];
146 strerror_r(errno, errmsg, MAX_ERR_LEN);
147 _E("tcsetattr failed, errmsg: %s", errmsg);
154 int uart_set_mode(int file_hndl, uart_bytesize_e bytesize, uart_parity_e parity, uart_stopbits_e stopbits)
159 _D("file_hndl : %d, bytesize : %d, parity : %d, stopbits : %d", file_hndl, bytesize, parity, stopbits);
162 _E("Invalid NULL parameter");
166 if (bytesize > UART_BYTESIZE_8BIT) {
167 _E("Invalid bytesize parameter");
171 ret = tcgetattr(file_hndl, &tio);
173 char errmsg[MAX_ERR_LEN];
174 strerror_r(errno, errmsg, MAX_ERR_LEN);
175 _E("tcgetattr failed, errmsg: %s", errmsg);
180 tio.c_cflag &= ~CSIZE;
181 tio.c_cflag |= byteinfo[bytesize];
182 tio.c_cflag |= (CLOCAL | CREAD);
184 /* set parity info */
186 case UART_PARITY_EVEN:
187 tio.c_cflag |= PARENB;
188 tio.c_cflag &= ~PARODD;
190 case UART_PARITY_ODD:
191 tio.c_cflag |= PARENB;
192 tio.c_cflag |= PARODD;
194 case UART_PARITY_NONE:
196 tio.c_cflag &= ~PARENB;
197 tio.c_cflag &= ~PARODD;
203 case UART_STOPBITS_1BIT:
204 tio.c_cflag &= ~CSTOPB;
206 case UART_STOPBITS_2BIT:
207 tio.c_cflag |= CSTOPB;
210 _E("Invalid parameter stopbits");
214 uart_flush(file_hndl);
215 ret = tcsetattr(file_hndl, TCSANOW, &tio);
217 char errmsg[MAX_ERR_LEN];
218 strerror_r(errno, errmsg, MAX_ERR_LEN);
219 _E("tcsetattr failed, errmsg : %s", errmsg);
226 int uart_set_flowcontrol(int file_hndl, bool xonxoff, bool rtscts)
231 _D("file_hndl : %d, xonxoff : %d, rtscts : %d", file_hndl, xonxoff, rtscts);
234 _E("Invalid NULL parameter");
238 ret = tcgetattr(file_hndl, &tio);
240 char errmsg[MAX_ERR_LEN];
241 strerror_r(errno, errmsg, MAX_ERR_LEN);
242 _E("tcgetattr failed, errmsg : %s", errmsg);
246 /* rtscts => 1: rts/cts on, 0: off */
248 tio.c_cflag |= CRTSCTS;
250 tio.c_cflag &= ~CRTSCTS;
252 /* xonxoff => 1: xon/xoff on, 0: off */
254 tio.c_iflag |= (IXON | IXOFF | IXANY);
256 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
258 ret = tcsetattr(file_hndl, TCSANOW, &tio);
260 char errmsg[MAX_ERR_LEN];
261 strerror_r(errno, errmsg, MAX_ERR_LEN);
262 _E("tcsetattr failed, errmsg : %s", errmsg);
269 int uart_read(int file_hndl, uint8_t *buf, unsigned int length)
274 _E("Invalid NULL parameter");
278 ret = read(file_hndl, (void *)buf, length);
279 if ((errno != EAGAIN && errno != EINTR) && ret < 0) {
280 char errmsg[MAX_ERR_LEN];
281 strerror_r(errno, errmsg, MAX_ERR_LEN);
282 _E("read failed, errmsg : %s", errmsg);
289 int uart_write(int file_hndl, uint8_t *buf, unsigned int length)
294 _E("Invalid NULL parameter");
298 ret = write(file_hndl, buf, length);
300 char errmsg[MAX_ERR_LEN];
301 strerror_r(errno, errmsg, MAX_ERR_LEN);
302 _E("write failed, errmsg : %s", errmsg);