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 UART_BAUDRATE_SIZE 19
23 static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = {
24 B0, B50, B75, B110, B134,
25 B150, B200, B300, B600, B1200,
26 B1800, B2400, B4800, B9600, B19200,
27 B38400, B57600, B115200, B230400
30 static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
32 void peripheral_interface_uart_close(peripheral_uart_h uart)
34 peripheral_interface_uart_flush(uart);
38 int peripheral_interface_uart_flush(peripheral_uart_h uart)
40 int ret = tcflush(uart->fd, TCIOFLUSH);
41 CHECK_ERROR(ret != 0);
43 return PERIPHERAL_ERROR_NONE;
46 int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
51 ret = tcgetattr(uart->fd, &tio);
52 CHECK_ERROR(ret != 0);
54 tio.c_cflag = peripheral_uart_br[baud];
61 peripheral_interface_uart_flush(uart);
62 ret = tcsetattr(uart->fd, TCSANOW, &tio);
63 CHECK_ERROR(ret != 0);
65 return PERIPHERAL_ERROR_NONE;
68 int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
73 ret = tcgetattr(uart->fd, &tio);
74 CHECK_ERROR(ret != 0);
77 tio.c_cflag &= ~CSIZE;
78 tio.c_cflag |= byteinfo[byte_size];
79 tio.c_cflag |= (CLOCAL | CREAD);
81 peripheral_interface_uart_flush(uart);
82 ret = tcsetattr(uart->fd, TCSANOW, &tio);
83 CHECK_ERROR(ret != 0);
85 return PERIPHERAL_ERROR_NONE;
88 int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
93 ret = tcgetattr(uart->fd, &tio);
94 CHECK_ERROR(ret != 0);
98 case PERIPHERAL_UART_PARITY_EVEN:
99 tio.c_cflag |= PARENB;
100 tio.c_cflag &= ~PARODD;
102 case PERIPHERAL_UART_PARITY_ODD:
103 tio.c_cflag |= PARENB;
104 tio.c_cflag |= PARODD;
106 case PERIPHERAL_UART_PARITY_NONE:
108 tio.c_cflag &= ~PARENB;
109 tio.c_cflag &= ~PARODD;
113 peripheral_interface_uart_flush(uart);
114 ret = tcsetattr(uart->fd, TCSANOW, &tio);
115 CHECK_ERROR(ret != 0);
117 return PERIPHERAL_ERROR_NONE;
120 int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
125 ret = tcgetattr(uart->fd, &tio);
126 CHECK_ERROR(ret != 0);
130 case PERIPHERAL_UART_STOP_BITS_1BIT:
131 tio.c_cflag &= ~CSTOPB;
133 case PERIPHERAL_UART_STOP_BITS_2BIT:
134 tio.c_cflag |= CSTOPB;
137 _E("Invalid parameter stop_bits");
138 return PERIPHERAL_ERROR_INVALID_PARAMETER;
141 peripheral_interface_uart_flush(uart);
142 ret = tcsetattr(uart->fd, TCSANOW, &tio);
143 CHECK_ERROR(ret != 0);
145 return PERIPHERAL_ERROR_NONE;
148 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)
153 ret = tcgetattr(uart->fd, &tio);
154 CHECK_ERROR(ret != 0);
156 if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS)
157 tio.c_cflag |= CRTSCTS;
158 else if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE)
159 tio.c_cflag &= ~CRTSCTS;
161 return PERIPHERAL_ERROR_INVALID_PARAMETER;
163 if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF)
164 tio.c_iflag |= (IXON | IXOFF | IXANY);
165 else if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE)
166 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
168 return PERIPHERAL_ERROR_INVALID_PARAMETER;
170 ret = tcsetattr(uart->fd, TCSANOW, &tio);
171 CHECK_ERROR(ret != 0);
173 return PERIPHERAL_ERROR_NONE;
176 int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
178 int ret = read(uart->fd, (void *)buf, length);
179 CHECK_ERROR(ret != length);
181 return PERIPHERAL_ERROR_NONE;
184 int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
186 int ret = write(uart->fd, buf, length);
187 CHECK_ERROR(ret != length);
189 return PERIPHERAL_ERROR_NONE;