Line data Source code
1 : /*
2 : * Copyright (C) 2017 Intel Corporation. All rights reserved.
3 : *
4 : * This file is free software: you can redistribute it and/or modify it
5 : * under the terms of the GNU General Public License as published by the
6 : * Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This file is distributed in the hope that it will be useful, but
10 : * WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 : * See the GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License along
15 : * with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 : #include <AP_HAL/AP_HAL.h>
18 : #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
19 :
20 : #include "UARTDriver.h"
21 :
22 : #if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
23 : #define USE_TERMIOS
24 : #endif
25 :
26 : #ifdef USE_TERMIOS
27 : #include <termios.h>
28 : #if defined(__APPLE__)
29 : #include <IOKit/serial/ioss.h>
30 : #endif
31 : #else
32 : #include <asm/ioctls.h>
33 : #include <asm/termbits.h>
34 : #endif
35 :
36 : #include <string.h>
37 : #include <sys/ioctl.h>
38 : #include <sys/stat.h>
39 : #include <sys/types.h>
40 :
41 0 : bool HALSITL::UARTDriver::set_speed(int speed) const
42 : {
43 0 : if (_fd < 0) {
44 0 : return false;
45 : }
46 : #ifdef USE_TERMIOS
47 : #if defined(__APPLE__)
48 : if (ioctl(_fd, IOSSIOSPEED, &speed) == -1) {
49 : return false;
50 : }
51 : #else
52 : struct termios t;
53 : tcgetattr(_fd, &t);
54 : cfsetspeed(&t, speed);
55 : tcsetattr(_fd, TCSANOW, &t);
56 : #endif
57 : #else
58 : struct termios2 tc;
59 0 : memset(&tc, 0, sizeof(tc));
60 0 : if (ioctl(_fd, TCGETS2, &tc) == -1) {
61 0 : return false;
62 : }
63 :
64 : /* speed is configured by c_[io]speed */
65 0 : tc.c_cflag &= ~CBAUD;
66 0 : tc.c_cflag |= BOTHER;
67 0 : tc.c_ispeed = speed;
68 0 : tc.c_ospeed = speed;
69 0 : if (ioctl(_fd, TCSETS2, &tc) == -1) {
70 0 : return false;
71 : }
72 0 : if (ioctl(_fd, TCFLSH, TCIOFLUSH) == -1) {
73 0 : return false;
74 : }
75 : #endif
76 :
77 0 : return true;
78 : }
79 :
80 1 : void HALSITL::UARTDriver::configure_parity(uint8_t v)
81 : {
82 1 : if (_fd < 0) {
83 1 : return;
84 : }
85 0 : UARTDriver::parity = v;
86 : #ifdef USE_TERMIOS
87 : struct termios t;
88 :
89 : tcgetattr(_fd, &t);
90 : #else
91 : struct termios2 t;
92 0 : memset(&t, 0, sizeof(t));
93 0 : if (ioctl(_fd, TCGETS2, &t) == -1) {
94 0 : return;
95 : }
96 : #endif
97 0 : if (v != 0) {
98 : // enable parity
99 0 : t.c_cflag |= PARENB;
100 0 : if (v == 1) {
101 0 : t.c_cflag |= PARODD;
102 : } else {
103 0 : t.c_cflag &= ~PARODD;
104 : }
105 : }
106 : else {
107 : // disable parity
108 0 : t.c_cflag &= ~PARENB;
109 : }
110 :
111 : #ifdef USE_TERMIOS
112 : tcsetattr(_fd, TCSANOW, &t);
113 : #else
114 0 : ioctl(_fd, TCSETS2, &t);
115 : #endif
116 : }
117 :
118 1 : void HALSITL::UARTDriver::set_stop_bits(int n)
119 : {
120 1 : if (_fd < 0) {
121 1 : return;
122 : }
123 : #ifdef USE_TERMIOS
124 : struct termios t;
125 :
126 : tcgetattr(_fd, &t);
127 : #else
128 : struct termios2 t;
129 0 : memset(&t, 0, sizeof(t));
130 0 : if (ioctl(_fd, TCGETS2, &t) == -1) {
131 0 : return;
132 : }
133 : #endif
134 :
135 0 : if (n > 1) {
136 0 : t.c_cflag |= CSTOPB;
137 : } else {
138 0 : t.c_cflag &= ~CSTOPB;
139 : }
140 :
141 : #ifdef USE_TERMIOS
142 : tcsetattr(_fd, TCSANOW, &t);
143 : #else
144 0 : ioctl(_fd, TCSETS2, &t);
145 : #endif
146 : }
147 :
148 : #endif
|