Implemented flush for Serial UART API

This commit is contained in:
Eric Katzfey 2024-03-07 10:27:56 -08:00
parent 65a73496e7
commit cc48676eaa
9 changed files with 58 additions and 20 deletions

View File

@ -80,6 +80,11 @@ ssize_t Serial::write(const void *buffer, size_t buffer_size)
return _impl.write(buffer, buffer_size); return _impl.write(buffer, buffer_size);
} }
void Serial::flush()
{
return _impl.flush();
}
uint32_t Serial::getBaudrate() const uint32_t Serial::getBaudrate() const
{ {
return _impl.getBaudrate(); return _impl.getBaudrate();

View File

@ -64,6 +64,8 @@ public:
ssize_t write(const void *buffer, size_t buffer_size); ssize_t write(const void *buffer, size_t buffer_size);
void flush();
// If port is already open then the following configuration functions // If port is already open then the following configuration functions
// will reconfigure the port. If the port is not yet open then they will // will reconfigure the port. If the port is not yet open then they will
// simply store the configuration in preparation for the port to be opened. // simply store the configuration in preparation for the port to be opened.

View File

@ -309,6 +309,13 @@ ssize_t SerialImpl::write(const void *buffer, size_t buffer_size)
return written; return written;
} }
void SerialImpl::flush()
{
if (_open) {
tcflush(_serial_fd, TCIOFLUSH);
}
}
const char *SerialImpl::getPort() const const char *SerialImpl::getPort() const
{ {
return _port; return _port;

View File

@ -64,6 +64,8 @@ public:
ssize_t write(const void *buffer, size_t buffer_size); ssize_t write(const void *buffer, size_t buffer_size);
void flush();
const char *getPort() const; const char *getPort() const;
uint32_t getBaudrate() const; uint32_t getBaudrate() const;

View File

@ -64,6 +64,8 @@ public:
ssize_t write(const void *buffer, size_t buffer_size); ssize_t write(const void *buffer, size_t buffer_size);
void flush();
const char *getPort() const; const char *getPort() const;
uint32_t getBaudrate() const; uint32_t getBaudrate() const;

View File

@ -309,6 +309,13 @@ ssize_t SerialImpl::write(const void *buffer, size_t buffer_size)
return written; return written;
} }
void SerialImpl::flush()
{
if (_open) {
tcflush(_serial_fd, TCIOFLUSH);
}
}
const char *SerialImpl::getPort() const const char *SerialImpl::getPort() const
{ {
return _port; return _port;

View File

@ -63,6 +63,8 @@ public:
ssize_t write(const void *buffer, size_t buffer_size); ssize_t write(const void *buffer, size_t buffer_size);
void flush();
const char *getPort() const; const char *getPort() const;
bool setPort(const char *port); bool setPort(const char *port);

View File

@ -251,6 +251,15 @@ ssize_t SerialImpl::write(const void *buffer, size_t buffer_size)
return ret_write; return ret_write;
} }
void SerialImpl::flush()
{
if (_open) {
uint8_t buffer[4];
// A read clears out all current data
read(buffer, 4);
}
}
const char *SerialImpl::getPort() const const char *SerialImpl::getPort() const
{ {
return _port; return _port;

View File

@ -116,11 +116,13 @@ void CrsfRc::Run()
{ {
if (should_exit()) { if (should_exit()) {
ScheduleClear(); ScheduleClear();
if (_uart) { if (_uart) {
(void) _uart->close(); (void) _uart->close();
delete _uart; delete _uart;
_uart = nullptr; _uart = nullptr;
} }
exit_and_cleanup(); exit_and_cleanup();
return; return;
} }
@ -152,26 +154,26 @@ void CrsfRc::Run()
return; return;
} }
// if (board_rc_swap_rxtx(_device)) { if (board_rc_swap_rxtx(_device)) {
// #if defined(TIOCSSWAP) #if defined(TIOCSSWAP)
// ioctl(_rc_fd, TIOCSSWAP, SER_SWAP_ENABLED); ioctl(_rc_fd, TIOCSSWAP, SER_SWAP_ENABLED);
// #endif // TIOCSSWAP #endif // TIOCSSWAP
// } }
//
// if (board_rc_singlewire(_device)) { if (board_rc_singlewire(_device)) {
// _is_singlewire = true; _is_singlewire = true;
// #if defined(TIOCSSINGLEWIRE) #if defined(TIOCSSINGLEWIRE)
// ioctl(_rc_fd, TIOCSSINGLEWIRE, SER_SINGLEWIRE_ENABLED); ioctl(_rc_fd, TIOCSSINGLEWIRE, SER_SINGLEWIRE_ENABLED);
// #endif // TIOCSSINGLEWIRE #endif // TIOCSSINGLEWIRE
// } }
//
// PX4_INFO("Crsf serial opened sucessfully"); PX4_INFO("Crsf serial opened sucessfully");
//
// if (_is_singlewire) { if (_is_singlewire) {
// PX4_INFO("Crsf serial is single wire. Telemetry disabled"); PX4_INFO("Crsf serial is single wire. Telemetry disabled");
// } }
//
// tcflush(_rc_fd, TCIOFLUSH); _uart->flush();
Crc8Init(0xd5); Crc8Init(0xd5);