Skip to content

Commit

Permalink
Port CRSF RC driver to new Serial UART API (PX4#22917)
Browse files Browse the repository at this point in the history
* Added implementations of Rx Tx swap and single wire for new UART API needed by CRSF driver
* Added inverted mode to Serial interface API
  • Loading branch information
katzfey authored Apr 2, 2024
1 parent a9ba0ac commit 8e61026
Show file tree
Hide file tree
Showing 14 changed files with 361 additions and 73 deletions.
1 change: 1 addition & 0 deletions boards/modalai/voxl2-slpi/default.px4board
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CONFIG_DRIVERS_MAGNETOMETER_ISENTEK_IST8308=y
CONFIG_DRIVERS_MAGNETOMETER_ISENTEK_IST8310=y
CONFIG_DRIVERS_MAGNETOMETER_QMC5883L=y
CONFIG_DRIVERS_POWER_MONITOR_VOXLPM=y
CONFIG_DRIVERS_RC_CRSF_RC=y
CONFIG_DRIVERS_QSHELL_QURT=y
CONFIG_MODULES_COMMANDER=y
CONFIG_MODULES_CONTROL_ALLOCATOR=y
Expand Down
33 changes: 33 additions & 0 deletions platforms/common/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ ssize_t Serial::write(const void *buffer, size_t buffer_size)
return _impl.write(buffer, buffer_size);
}

void Serial::flush()
{
return _impl.flush();
}

uint32_t Serial::getBaudrate() const
{
return _impl.getBaudrate();
Expand Down Expand Up @@ -134,6 +139,34 @@ bool Serial::setFlowcontrol(FlowControl flowcontrol)
return _impl.setFlowcontrol(flowcontrol);
}

bool Serial::getSingleWireMode() const
{
return _impl.getSingleWireMode();
}
bool Serial::setSingleWireMode()
{
return _impl.setSingleWireMode();
}

bool Serial::getSwapRxTxMode() const
{
return _impl.getSwapRxTxMode();
}
bool Serial::setSwapRxTxMode()
{
return _impl.setSwapRxTxMode();
}

bool Serial::getInvertedMode() const
{
return _impl.getInvertedMode();
}

bool Serial::setInvertedMode(bool enable)
{
return _impl.setInvertedMode(enable);
}

const char *Serial::getPort() const
{
return _impl.getPort();
Expand Down
11 changes: 11 additions & 0 deletions platforms/common/include/px4_platform_common/Serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Serial

ssize_t write(const void *buffer, size_t buffer_size);

void flush();

// If port is already open then the following configuration functions
// 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.
Expand All @@ -84,6 +86,15 @@ class Serial
FlowControl getFlowcontrol() const;
bool setFlowcontrol(FlowControl flowcontrol);

bool getSingleWireMode() const;
bool setSingleWireMode();

bool getSwapRxTxMode() const;
bool setSwapRxTxMode();

bool getInvertedMode() const;
bool setInvertedMode(bool enable);

static bool validatePort(const char *port);
bool setPort(const char *port);
const char *getPort() const;
Expand Down
83 changes: 83 additions & 0 deletions platforms/nuttx/src/px4/common/SerialImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ bool SerialImpl::open()

_open = true;

// Do pin operations after port has been opened
if (_single_wire_mode) {
setSingleWireMode();
}

if (_swap_rx_tx_mode) {
setSwapRxTxMode();
}

setInvertedMode(_inverted_mode);

return _open;
}

Expand Down Expand Up @@ -323,6 +334,13 @@ ssize_t SerialImpl::write(const void *buffer, size_t buffer_size)
return written;
}

void SerialImpl::flush()
{
if (_open) {
tcflush(_serial_fd, TCIOFLUSH);
}
}

const char *SerialImpl::getPort() const
{
return _port;
Expand Down Expand Up @@ -416,4 +434,69 @@ bool SerialImpl::setFlowcontrol(FlowControl flowcontrol)
return flowcontrol == FlowControl::Disabled;
}

bool SerialImpl::getSingleWireMode() const
{
return _single_wire_mode;
}

bool SerialImpl::setSingleWireMode()
{
#if defined(TIOCSSINGLEWIRE)

if (_open) {
ioctl(_serial_fd, TIOCSSINGLEWIRE, SER_SINGLEWIRE_ENABLED);
}

_single_wire_mode = true;
return true;
#else
return false;
#endif // TIOCSSINGLEWIRE
}

bool SerialImpl::getSwapRxTxMode() const
{
return _swap_rx_tx_mode;
}

bool SerialImpl::setSwapRxTxMode()
{
#if defined(TIOCSSWAP)

if (_open) {
ioctl(_serial_fd, TIOCSSWAP, SER_SWAP_ENABLED);
}

_swap_rx_tx_mode = true;
return true;
#else
return false;
#endif // TIOCSSWAP
}

bool SerialImpl::getInvertedMode() const
{
return _inverted_mode;
}

bool SerialImpl::setInvertedMode(bool enable)
{
#if defined(TIOCSINVERT)

if (_open) {
if (enable) {
ioctl(_serial_fd, TIOCSINVERT, SER_INVERT_ENABLED_RX | SER_INVERT_ENABLED_TX);

} else {
ioctl(_serial_fd, TIOCSINVERT, 0);
}
}

_inverted_mode = enable;
return true;
#else
return _inverted_mode == enable;
#endif // TIOCSINVERT
}

} // namespace device
14 changes: 14 additions & 0 deletions platforms/nuttx/src/px4/common/include/SerialImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class SerialImpl

ssize_t write(const void *buffer, size_t buffer_size);

void flush();

const char *getPort() const;
static bool validatePort(const char *port);
bool setPort(const char *port);
Expand All @@ -83,6 +85,15 @@ class SerialImpl
FlowControl getFlowcontrol() const;
bool setFlowcontrol(FlowControl flowcontrol);

bool getSingleWireMode() const;
bool setSingleWireMode();

bool getSwapRxTxMode() const;
bool setSwapRxTxMode();

bool getInvertedMode() const;
bool setInvertedMode(bool enable);

private:

int _serial_fd{-1};
Expand All @@ -101,6 +112,9 @@ class SerialImpl
bool validateBaudrate(uint32_t baudrate);
bool configure();

bool _single_wire_mode{false};
bool _swap_rx_tx_mode{false};
bool _inverted_mode{false};
};

} // namespace device
1 change: 1 addition & 0 deletions platforms/nuttx/src/px4/common/px4_protected_layers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ target_link_libraries(px4_layer

add_library(px4_kernel_layer
${KERNEL_SRCS}
SerialImpl.cpp
)

target_link_libraries(px4_kernel_layer
Expand Down
15 changes: 15 additions & 0 deletions platforms/posix/include/SerialImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class SerialImpl

ssize_t write(const void *buffer, size_t buffer_size);

void flush();

const char *getPort() const;
static bool validatePort(const char *port);
bool setPort(const char *port);
Expand All @@ -83,6 +85,15 @@ class SerialImpl
FlowControl getFlowcontrol() const;
bool setFlowcontrol(FlowControl flowcontrol);

bool getSingleWireMode() const;
bool setSingleWireMode();

bool getSwapRxTxMode() const;
bool setSwapRxTxMode();

bool getInvertedMode() const;
bool setInvertedMode(bool enable);

private:

int _serial_fd{-1};
Expand All @@ -100,6 +111,10 @@ class SerialImpl

bool validateBaudrate(uint32_t baudrate);
bool configure();

bool _single_wire_mode{false};
bool _swap_rx_tx_mode{false};
bool _inverted_mode{false};
};

} // namespace device
82 changes: 82 additions & 0 deletions platforms/posix/src/px4/common/SerialImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ bool SerialImpl::open()

_open = true;

if (_single_wire_mode) {
setSingleWireMode();
}

if (_swap_rx_tx_mode) {
setSwapRxTxMode();
}

setInvertedMode(_inverted_mode);

return _open;
}

Expand Down Expand Up @@ -316,6 +326,13 @@ ssize_t SerialImpl::write(const void *buffer, size_t buffer_size)
return written;
}

void SerialImpl::flush()
{
if (_open) {
tcflush(_serial_fd, TCIOFLUSH);
}
}

const char *SerialImpl::getPort() const
{
return _port;
Expand Down Expand Up @@ -409,4 +426,69 @@ bool SerialImpl::setFlowcontrol(FlowControl flowcontrol)
return flowcontrol == FlowControl::Disabled;
}

bool SerialImpl::getSingleWireMode() const
{
return _single_wire_mode;
}

bool SerialImpl::setSingleWireMode()
{
#if defined(TIOCSSINGLEWIRE)

if (_open) {
ioctl(_serial_fd, TIOCSSINGLEWIRE, SER_SINGLEWIRE_ENABLED);
}

_single_wire_mode = true;
return true;
#else
return false;
#endif // TIOCSSINGLEWIRE
}

bool SerialImpl::getSwapRxTxMode() const
{
return _swap_rx_tx_mode;
}

bool SerialImpl::setSwapRxTxMode()
{
#if defined(TIOCSSWAP)

if (_open) {
ioctl(_serial_fd, TIOCSSWAP, SER_SWAP_ENABLED);
}

_swap_rx_tx_mode = true;
return true;
#else
return false;
#endif // TIOCSSWAP
}

bool SerialImpl::getInvertedMode() const
{
return _inverted_mode;
}

bool SerialImpl::setInvertedMode(bool enable)
{
#if defined(TIOCSINVERT)

if (_open) {
if (enable) {
ioctl(_serial_fd, TIOCSINVERT, SER_INVERT_ENABLED_RX | SER_INVERT_ENABLED_TX);

} else {
ioctl(_serial_fd, TIOCSINVERT, 0);
}
}

_inverted_mode = enable;
return true;
#else
return _inverted_mode == enable;
#endif // TIOCSINVERT
}

} // namespace device
11 changes: 11 additions & 0 deletions platforms/qurt/include/SerialImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class SerialImpl

ssize_t write(const void *buffer, size_t buffer_size);

void flush();

const char *getPort() const;
bool setPort(const char *port);
static bool validatePort(const char *port);
Expand All @@ -82,6 +84,15 @@ class SerialImpl
FlowControl getFlowcontrol() const;
bool setFlowcontrol(FlowControl flowcontrol);

bool getSingleWireMode() const;
bool setSingleWireMode();

bool getSwapRxTxMode() const;
bool setSwapRxTxMode();

bool getInvertedMode() const;
bool setInvertedMode(bool enable);

private:

int _serial_fd{-1};
Expand Down
Loading

0 comments on commit 8e61026

Please sign in to comment.