2017-11-22 18:21:50 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2017-11-30 16:36:51 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2017-11-22 18:21:50 -04:00
|
|
|
|
|
|
|
#include "UARTDriver.h"
|
|
|
|
|
2024-11-05 20:43:34 -04:00
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(__APPLE__) || defined(__OpenBSD__)
|
2018-03-02 00:28:15 -04:00
|
|
|
#define USE_TERMIOS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_TERMIOS
|
2017-11-30 16:05:56 -04:00
|
|
|
#include <termios.h>
|
2017-11-30 16:36:51 -04:00
|
|
|
#else
|
2019-07-23 08:16:49 -03:00
|
|
|
#include <asm/ioctls.h>
|
2017-11-30 16:36:51 -04:00
|
|
|
#include <asm/termbits.h>
|
|
|
|
#endif
|
|
|
|
|
2017-11-22 18:21:50 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-02-01 12:26:28 -04:00
|
|
|
bool HALSITL::UARTDriver::set_speed(int speed) const
|
2017-11-22 18:21:50 -04:00
|
|
|
{
|
|
|
|
if (_fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-02 00:28:15 -04:00
|
|
|
#ifdef USE_TERMIOS
|
2017-11-30 16:36:51 -04:00
|
|
|
struct termios t;
|
|
|
|
tcgetattr(_fd, &t);
|
|
|
|
cfsetspeed(&t, speed);
|
|
|
|
tcsetattr(_fd, TCSANOW, &t);
|
|
|
|
#else
|
|
|
|
struct termios2 tc;
|
2017-11-22 18:21:50 -04:00
|
|
|
memset(&tc, 0, sizeof(tc));
|
|
|
|
if (ioctl(_fd, TCGETS2, &tc) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-30 16:36:51 -04:00
|
|
|
|
2017-11-22 18:21:50 -04:00
|
|
|
/* speed is configured by c_[io]speed */
|
|
|
|
tc.c_cflag &= ~CBAUD;
|
|
|
|
tc.c_cflag |= BOTHER;
|
|
|
|
tc.c_ispeed = speed;
|
|
|
|
tc.c_ospeed = speed;
|
|
|
|
if (ioctl(_fd, TCSETS2, &tc) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ioctl(_fd, TCFLSH, TCIOFLUSH) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-30 16:36:51 -04:00
|
|
|
#endif
|
2017-11-22 18:21:50 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-30 16:36:51 -04:00
|
|
|
void HALSITL::UARTDriver::configure_parity(uint8_t v)
|
|
|
|
{
|
2017-11-22 18:21:50 -04:00
|
|
|
if (_fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-25 19:46:35 -03:00
|
|
|
UARTDriver::parity = v;
|
2018-03-02 00:28:15 -04:00
|
|
|
#ifdef USE_TERMIOS
|
2017-11-30 16:36:51 -04:00
|
|
|
struct termios t;
|
|
|
|
|
|
|
|
tcgetattr(_fd, &t);
|
|
|
|
#else
|
|
|
|
struct termios2 t;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
|
|
if (ioctl(_fd, TCGETS2, &t) == -1) {
|
2017-11-22 18:21:50 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-11-30 16:36:51 -04:00
|
|
|
#endif
|
2017-11-22 18:21:50 -04:00
|
|
|
if (v != 0) {
|
|
|
|
// enable parity
|
2017-11-30 16:36:51 -04:00
|
|
|
t.c_cflag |= PARENB;
|
2017-11-22 18:21:50 -04:00
|
|
|
if (v == 1) {
|
2017-11-30 16:36:51 -04:00
|
|
|
t.c_cflag |= PARODD;
|
2017-11-22 18:21:50 -04:00
|
|
|
} else {
|
2017-11-30 16:36:51 -04:00
|
|
|
t.c_cflag &= ~PARODD;
|
2017-11-22 18:21:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// disable parity
|
2017-11-30 16:36:51 -04:00
|
|
|
t.c_cflag &= ~PARENB;
|
2017-11-22 18:21:50 -04:00
|
|
|
}
|
|
|
|
|
2018-03-02 00:28:15 -04:00
|
|
|
#ifdef USE_TERMIOS
|
2017-11-30 16:36:51 -04:00
|
|
|
tcsetattr(_fd, TCSANOW, &t);
|
|
|
|
#else
|
|
|
|
ioctl(_fd, TCSETS2, &t);
|
|
|
|
#endif
|
2017-11-22 18:21:50 -04:00
|
|
|
}
|
|
|
|
|
2017-11-30 16:36:51 -04:00
|
|
|
void HALSITL::UARTDriver::set_stop_bits(int n)
|
|
|
|
{
|
2017-11-22 18:21:50 -04:00
|
|
|
if (_fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-02 00:28:15 -04:00
|
|
|
#ifdef USE_TERMIOS
|
2017-11-30 16:36:51 -04:00
|
|
|
struct termios t;
|
|
|
|
|
|
|
|
tcgetattr(_fd, &t);
|
|
|
|
#else
|
|
|
|
struct termios2 t;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
|
|
if (ioctl(_fd, TCGETS2, &t) == -1) {
|
2017-11-22 18:21:50 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-11-30 16:36:51 -04:00
|
|
|
#endif
|
2017-11-22 18:21:50 -04:00
|
|
|
|
2017-11-30 16:36:51 -04:00
|
|
|
if (n > 1) {
|
|
|
|
t.c_cflag |= CSTOPB;
|
|
|
|
} else {
|
|
|
|
t.c_cflag &= ~CSTOPB;
|
2017-11-22 18:21:50 -04:00
|
|
|
}
|
|
|
|
|
2018-03-02 00:28:15 -04:00
|
|
|
#ifdef USE_TERMIOS
|
2017-11-30 16:36:51 -04:00
|
|
|
tcsetattr(_fd, TCSANOW, &t);
|
|
|
|
#else
|
|
|
|
ioctl(_fd, TCSETS2, &t);
|
|
|
|
#endif
|
2017-11-22 18:21:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|