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 SYSFS_UART_PATH "/dev/ttySAC"
32 #define PATH_BUF_MAX 64
33 #define UART_BUF_MAX 16
35 #define UART_BAUDRATE_SIZE 19
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 uart_open(int port, int *file_hndl)
49 char fName[PATH_BUF_MAX] = {0};
51 _D("port : %d", port);
53 snprintf(fName, PATH_BUF_MAX, SYSFS_UART_PATH "%d", port);
54 if ((fd = open(fName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
55 _E("Error[%d]: can't open %s, %s--[%d]\n", errno, fName, __FUNCTION__, __LINE__);
62 int uart_close(int file_hndl)
64 _D("file_hndl : %d", file_hndl);
67 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
74 int uart_flush(int file_hndl)
79 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
83 ret = tcflush(file_hndl, TCIOFLUSH);
85 _E("FAILED[%d]: tcflush, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
92 int uart_set_baudrate(int file_hndl, uart_baudrate_e baud)
97 _D("file_hndl : %d, baud : %d", file_hndl, baud);
99 memset(&tio, 0, sizeof(tio));
101 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
105 if (baud > UART_BAUDRATE_230400) {
106 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
110 ret = tcgetattr(file_hndl, &tio);
112 _E("Error[%d]: tcgetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
115 tio.c_cflag = peripheral_uart_br[baud];
116 tio.c_iflag = IGNPAR;
122 uart_flush(file_hndl);
123 ret = tcsetattr(file_hndl, TCSANOW, &tio);
125 _E("Error[%d]: tcsetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
132 int uart_set_mode(int file_hndl, uart_bytesize_e bytesize, uart_parity_e parity, uart_stopbits_e stopbits)
137 _D("file_hndl : %d, bytesize : %d, parity : %d, stopbits : %d", file_hndl, bytesize, parity, stopbits);
140 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
144 ret = tcgetattr(file_hndl, &tio);
146 _E("Error[%d]: tcgetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
151 if (bytesize < UART_BYTESIZE_5BIT || bytesize > UART_BYTESIZE_8BIT) {
152 _E("Error[%d]: Invalid parameter bytesize, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
155 tio.c_cflag &= ~CSIZE;
156 tio.c_cflag |= byteinfo[bytesize];
157 tio.c_cflag |= (CLOCAL | CREAD);
159 /* set parity info */
161 case UART_PARITY_EVEN:
162 tio.c_cflag |= PARENB;
163 tio.c_cflag &= ~PARODD;
165 case UART_PARITY_ODD:
166 tio.c_cflag |= PARENB;
167 tio.c_cflag |= PARODD;
169 case UART_PARITY_NONE:
171 tio.c_cflag &= ~PARENB;
172 tio.c_cflag &= ~PARODD;
178 case UART_STOPBITS_1BIT:
179 tio.c_cflag &= ~CSTOPB;
181 case UART_STOPBITS_2BIT:
182 tio.c_cflag |= CSTOPB;
185 _E("Error[%d]: Invalid parameter stopbits, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
189 uart_flush(file_hndl);
190 ret = tcsetattr(file_hndl, TCSANOW, &tio);
192 _E("Error[%d]: tcsetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
199 int uart_set_flowcontrol(int file_hndl, bool xonxoff, bool rtscts)
204 _D("file_hndl : %d, xonxoff : %d, rtscts : %d", file_hndl, xonxoff, rtscts);
207 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
211 ret = tcgetattr(file_hndl, &tio);
213 _E("Error[%d]: tcgetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
217 /* rtscts => 1: rts/cts on, 0: off */
219 tio.c_cflag |= CRTSCTS;
221 tio.c_cflag &= ~CRTSCTS;
223 /* xonxoff => 1: xon/xoff on, 0: off */
225 tio.c_iflag |= (IXON | IXOFF | IXANY);
227 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
229 ret = tcsetattr(file_hndl, TCSANOW, &tio);
231 _E("Error[%d]: tcsetattr, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
238 int uart_read(int file_hndl, uint8_t *buf, unsigned int length)
242 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
246 ret = read(file_hndl, (void *)buf, length);
247 if ((errno != EAGAIN && errno != EINTR) && ret < 0) {
248 _E("Error[%d]: read, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
255 int uart_write(int file_hndl, uint8_t *buf, unsigned int length)
259 _E("Error[%d]: Invalid parameter, %s--[%d]\n", errno, __FUNCTION__, __LINE__);
263 ret = write(file_hndl, buf, length);
265 _E("Error[%d]: write, %s--[%d]\n", errno, __FUNCTION__, __LINE__);