Files
ardupilot/libraries/AP_HAL_Linux/UARTDevice.cpp
T
heeplr b892968cfa
colcon build/test / build-test (push) Has been cancelled
Cygwin Build / build (push) Has been cancelled
ESP32 Build / build (esp32buzz, 10) (push) Has been cancelled
ESP32 Build / build (esp32s3empty, 10) (push) Has been cancelled
Macos Build / build (CubeOrange) (push) Has been cancelled
Macos Build / build (sitl) (push) Has been cancelled
QURT Build / build (push) Has been cancelled
test chibios / build (CubeOrange-EKF2, 10, chibios) (push) Has been cancelled
test chibios / build (CubeOrange-ODID, 10, chibios) (push) Has been cancelled
test chibios / build (CubeOrange-PPP, 10, chibios) (push) Has been cancelled
test chibios / build (CubeOrange-bootloader, 10, chibios) (push) Has been cancelled
test chibios / build (CubeRedPrimary-bootloader, 10, chibios) (push) Has been cancelled
test chibios / build (MatekF405-Wing, 10, chibios) (push) Has been cancelled
test chibios / build (Pixhawk6X-PPPGW, 10, chibios) (push) Has been cancelled
test chibios / build (SOHW, 10, chibios) (push) Has been cancelled
test chibios / build (build-options-defaults-test, 10, chibios) (push) Has been cancelled
test chibios / build (configure-all, 10, chibios) (push) Has been cancelled
test chibios / build (fmuv2-plane, 10, chibios) (push) Has been cancelled
test chibios / build (fmuv3, 10, chibios) (push) Has been cancelled
test chibios / build (fmuv3-bootloader, 10, chibios) (push) Has been cancelled
test chibios / build (iofirmware, 10, chibios) (push) Has been cancelled
test chibios / build (new-check, 10, chibios) (push) Has been cancelled
test chibios / build (periph-build, 10, chibios) (push) Has been cancelled
test chibios / build (revo-bootloader, 10, chibios) (push) Has been cancelled
test chibios / build (revo-mini, 10, chibios) (push) Has been cancelled
test chibios / build (signing, 10, chibios) (push) Has been cancelled
test chibios / build (stm32f7, 10, chibios) (push) Has been cancelled
test chibios / build (stm32h7, 10, chibios) (push) Has been cancelled
test chibios / build (stm32h7-debug, 10, chibios) (push) Has been cancelled
test dds / build (sitl) (push) Has been cancelled
test dds / build (stm32h7) (push) Has been cancelled
test Linux SBC / build (bbbmini, armhf) (push) Has been cancelled
test Linux SBC / build (bebop, armhf) (push) Has been cancelled
test Linux SBC / build (bhat, armhf) (push) Has been cancelled
test Linux SBC / build (canzero, armhf) (push) Has been cancelled
test Linux SBC / build (erlebrain2, armhf) (push) Has been cancelled
test Linux SBC / build (linux, base) (push) Has been cancelled
test Linux SBC / build (navigator, armhf-musl) (push) Has been cancelled
test Linux SBC / build (navigator64, aarch64) (push) Has been cancelled
test Linux SBC / build (navio, armhf) (push) Has been cancelled
test Linux SBC / build (navio2, armhf) (push) Has been cancelled
test Linux SBC / build (pxf, armhf) (push) Has been cancelled
test Linux SBC / build (pxfmini, armhf) (push) Has been cancelled
test scripts / build (astyle-cleanliness) (push) Has been cancelled
test scripts / build (check_autotest_options) (push) Has been cancelled
test scripts / build (logger_metadata) (push) Has been cancelled
test scripts / build (param_parse) (push) Has been cancelled
test scripts / build (python-cleanliness) (push) Has been cancelled
test scripts / build (validate_board_list) (push) Has been cancelled
test plane / build (base) (push) Has been cancelled
test plane / build (clang) (push) Has been cancelled
test plane / autotest (sitltest-plane-tests1a) (push) Has been cancelled
test plane / autotest (sitltest-plane-tests1b) (push) Has been cancelled
test plane / autotest (sitltest-quadplane) (push) Has been cancelled
test unit tests and sitl building / build (examples, base) (push) Has been cancelled
test unit tests and sitl building / build (examples, clang) (push) Has been cancelled
test unit tests and sitl building / build (sitl, base) (push) Has been cancelled
test unit tests and sitl building / build (sitl, clang) (push) Has been cancelled
test unit tests and sitl building / build (unit-tests, base) (push) Has been cancelled
test unit tests and sitl building / build (unit-tests, clang) (push) Has been cancelled
test ccache / build (10, chibios) (push) Has been cancelled
test replay / build (replay, base) (push) Has been cancelled
test replay / build (replay, clang) (push) Has been cancelled
test blimp / build (base) (push) Has been cancelled
test blimp / build (clang) (push) Has been cancelled
test blimp / autotest (sitltest-blimp) (push) Has been cancelled
test copter / build (base) (push) Has been cancelled
test copter / build (clang) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests1a) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests1b) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests1c) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests1d) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests1e) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests2a) (push) Has been cancelled
test copter / autotest (sitltest-copter-tests2b) (push) Has been cancelled
test copter / build-gcc-heli (push) Has been cancelled
test copter / autotest-heli (sitltest-heli) (push) Has been cancelled
test ap_periph / build-gcc-ap_periph (push) Has been cancelled
test ap_periph / autotest-can (sitltest-can) (push) Has been cancelled
test rover / build (base) (push) Has been cancelled
test rover / build (clang) (push) Has been cancelled
test rover / autotest (sitltest-balancebot) (push) Has been cancelled
test rover / autotest (sitltest-rover) (push) Has been cancelled
test rover / autotest (sitltest-sailboat) (push) Has been cancelled
test sub / build (base) (push) Has been cancelled
test sub / build (clang) (push) Has been cancelled
test sub / autotest (sitltest-sub) (push) Has been cancelled
test tracker / build (base) (push) Has been cancelled
test tracker / build (clang) (push) Has been cancelled
test tracker / autotest (sitltest-tracker) (push) Has been cancelled
AP_HAL_Linux: set BOTHER c_cflag to choose other baudrates
References:
* https://man7.org/linux/man-pages/man2/TCSETS.2const.html
* https://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/
2025-06-07 15:28:25 +10:00

197 lines
4.3 KiB
C++

#include "UARTDevice.h"
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <asm/ioctls.h>
#include <asm/termbits.h>
#include <unistd.h>
#include <AP_HAL/AP_HAL.h>
UARTDevice::UARTDevice(const char *device_path):
_device_path(device_path)
{
}
UARTDevice::~UARTDevice()
{
}
bool UARTDevice::close()
{
if (_fd != -1) {
if (::close(_fd) < 0) {
return false;
}
}
_fd = -1;
return true;
}
bool UARTDevice::open()
{
_fd = ::open(_device_path, O_RDWR | O_CLOEXEC | O_NOCTTY);
if (_fd < 0) {
::fprintf(stderr, "Failed to open UART device %s - %s\n",
_device_path, strerror(errno));
return false;
}
_disable_crlf();
return true;
}
ssize_t UARTDevice::read(uint8_t *buf, uint16_t n)
{
return ::read(_fd, buf, n);
}
ssize_t UARTDevice::write(const uint8_t *buf, uint16_t n)
{
struct pollfd fds;
fds.fd = _fd;
fds.events = POLLOUT;
fds.revents = 0;
int ret = 0;
if (poll(&fds, 1, 0) == 1) {
ret = ::write(_fd, buf, n);
}
return ret;
}
void UARTDevice::set_blocking(bool blocking)
{
int flags = fcntl(_fd, F_GETFL, 0);
if (blocking) {
flags = flags & ~O_NONBLOCK;
} else {
flags = flags | O_NONBLOCK;
}
if (fcntl(_fd, F_SETFL, flags) < 0) {
::fprintf(stderr, "Failed to make UART nonblocking %s - %s\n",
_device_path, strerror(errno));
}
}
void UARTDevice::_disable_crlf()
{
struct termios2 t = { 0 };
if (ioctl(_fd, TCGETS2, &t) != 0) {
::fprintf(stderr, "Failed to read serial options for %s - %s\n",
_device_path, strerror(errno));
return;
}
// disable LF -> CR/LF
t.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL | IXON | IXOFF);
t.c_oflag &= ~(OPOST | ONLCR);
t.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
t.c_cc[VMIN] = 0;
if (ioctl(_fd, TCSETS2, &t) != 0) {
::fprintf(stderr, "Failed to disable crlf on %s - %s\n",
_device_path, strerror(errno));
return;
}
}
void UARTDevice::set_speed(uint32_t baudrate)
{
struct termios2 tio = { 0 };
if (ioctl(_fd, TCGETS2, &tio) != 0) {
::fprintf(stderr, "Failed to read serial options for %s - %s\n",
_device_path, strerror(errno));
return;
}
// use CBAUDEX and B(aud)OTHER to gain access to "non-standard" rates that are common for eg. RC receivers
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= CBAUDEX;
#if defined BOTHER
tio.c_cflag |= BOTHER;
#endif
tio.c_ispeed = baudrate;
tio.c_ospeed = baudrate;
if (ioctl(_fd, TCSETS2, &tio) != 0) {
::fprintf(stderr, "Failed to set serial baud to %d for %s - %s\n",
baudrate, _device_path, strerror(errno));
return;
}
}
void UARTDevice::set_flow_control(AP_HAL::UARTDriver::flow_control flow_control_setting)
{
if (_flow_control == flow_control_setting) {
return;
}
struct termios2 t = { 0 };
if (ioctl(_fd, TCGETS2, &t) != 0) {
::fprintf(stderr, "Failed to read serial options for %s - %s\n",
_device_path, strerror(errno));
return;
}
if (flow_control_setting != AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE) {
t.c_cflag |= CRTSCTS;
} else {
t.c_cflag &= ~CRTSCTS;
}
if (ioctl(_fd, TCSETS2, &t) != 0) {
::fprintf(stderr, "Failed to set flow control for %s - %s\n",
_device_path, strerror(errno));
return;
}
_flow_control = flow_control_setting;
}
void UARTDevice::set_parity(int v)
{
struct termios2 t = { 0 };
if (ioctl(_fd, TCGETS2, &t) != 0) {
::fprintf(stderr, "Failed to read serial options for %s - %s\n",
_device_path, strerror(errno));
return;
}
if (v != 0) {
// enable parity
t.c_cflag |= PARENB;
if (v == 1) {
t.c_cflag |= PARODD;
} else {
t.c_cflag &= ~PARODD;
}
}
else {
// disable parity
t.c_cflag &= ~PARENB;
}
if (ioctl(_fd, TCSETS2, &t) != 0) {
::fprintf(stderr, "Failed to set parity for %s - %s\n",
_device_path, strerror(errno));
return;
}
}