From 672fca5169b017387e58e2ff864913d932c59aa1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 4 Nov 2022 11:08:23 +0100 Subject: [PATCH] extend uart helper a bit --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 9 +--- src/fsfw/tmtcservices/PusServiceBase.cpp | 4 +- src/fsfw_hal/linux/uart/UartComIF.cpp | 19 +------- src/fsfw_hal/linux/uart/UartComIF.h | 11 ----- src/fsfw_hal/linux/uart/helper.cpp | 44 +++++++++++++++++++ src/fsfw_hal/linux/uart/helper.h | 10 +++++ 6 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index 7bded0de..559e4cfd 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -566,7 +566,6 @@ void DeviceHandlerBase::setTransition(Mode_t modeTo, Submode_t submodeTo) { } void DeviceHandlerBase::setMode(Mode_t newMode, uint8_t newSubmode) { - /** * handle transition from OFF to NORMAL by continuing towards normal when ON is reached */ @@ -1584,13 +1583,9 @@ void DeviceHandlerBase::setPowerSwitcher(PowerSwitchIF* switcher) { this->powerSwitcher = switcher; } -Mode_t DeviceHandlerBase::getMode() { - return mode; -} +Mode_t DeviceHandlerBase::getMode() { return mode; } -Submode_t DeviceHandlerBase::getSubmode() { - return submode; -} +Submode_t DeviceHandlerBase::getSubmode() { return submode; } void DeviceHandlerBase::disableCommandsAndReplies() { for (auto& command : deviceCommandMap) { diff --git a/src/fsfw/tmtcservices/PusServiceBase.cpp b/src/fsfw/tmtcservices/PusServiceBase.cpp index 8e0c5c4f..fbabbd70 100644 --- a/src/fsfw/tmtcservices/PusServiceBase.cpp +++ b/src/fsfw/tmtcservices/PusServiceBase.cpp @@ -27,8 +27,8 @@ ReturnValue_t PusServiceBase::performOperation(uint8_t opCode) { ReturnValue_t result = performService(); if (result != returnvalue::OK) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::error << "PusService " << static_cast(psbParams.serviceId) << - ": performService returned with " << static_cast(result) << std::endl; + sif::error << "PusService " << static_cast(psbParams.serviceId) + << ": performService returned with " << static_cast(result) << std::endl; #endif return returnvalue::FAILED; } diff --git a/src/fsfw_hal/linux/uart/UartComIF.cpp b/src/fsfw_hal/linux/uart/UartComIF.cpp index 0b44e4a5..a65adb33 100644 --- a/src/fsfw_hal/linux/uart/UartComIF.cpp +++ b/src/fsfw_hal/linux/uart/UartComIF.cpp @@ -88,7 +88,7 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) { return fd; } - setParityOptions(&options, uartCookie); + uart::setParity(options, uartCookie->getParity()); setStopBitOptions(&options, uartCookie); setDatasizeOptions(&options, uartCookie); setFixedOptions(&options); @@ -114,23 +114,6 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) { return fd; } -void UartComIF::setParityOptions(struct termios* options, UartCookie* uartCookie) { - /* Clear parity bit */ - options->c_cflag &= ~PARENB; - switch (uartCookie->getParity()) { - case Parity::EVEN: - options->c_cflag |= PARENB; - options->c_cflag &= ~PARODD; - break; - case Parity::ODD: - options->c_cflag |= PARENB; - options->c_cflag |= PARODD; - break; - default: - break; - } -} - void UartComIF::setStopBitOptions(struct termios* options, UartCookie* uartCookie) { /* Clear stop field. Sets stop bit to one bit */ options->c_cflag &= ~CSTOPB; diff --git a/src/fsfw_hal/linux/uart/UartComIF.h b/src/fsfw_hal/linux/uart/UartComIF.h index 4bdbd954..fc92921f 100644 --- a/src/fsfw_hal/linux/uart/UartComIF.h +++ b/src/fsfw_hal/linux/uart/UartComIF.h @@ -78,17 +78,6 @@ class UartComIF : public DeviceCommunicationIF, public SystemObject { */ int configureUartPort(UartCookie* uartCookie); - /** - * @brief This function adds the parity settings to the termios options struct. - * - * @param options Pointer to termios options struct which will be modified to enable or disable - * parity checking. - * @param uartCookie Pointer to uart cookie containing the information about the desired - * parity settings. - * - */ - void setParityOptions(struct termios* options, UartCookie* uartCookie); - void setStopBitOptions(struct termios* options, UartCookie* uartCookie); /** diff --git a/src/fsfw_hal/linux/uart/helper.cpp b/src/fsfw_hal/linux/uart/helper.cpp index c32fed31..f38dd3d3 100644 --- a/src/fsfw_hal/linux/uart/helper.cpp +++ b/src/fsfw_hal/linux/uart/helper.cpp @@ -115,6 +115,50 @@ void uart::setBaudrate(struct termios& options, UartBaudRate baud) { } } +void uart::setBitsPerWord(struct termios& options, BitsPerWord bits) { + options.c_cflag &= ~CSIZE; // Clear all the size bits + if (bits == BitsPerWord::BITS_5) { + options.c_cflag |= CS5; + } else if (bits == BitsPerWord::BITS_6) { + options.c_cflag |= CS6; + } else if (bits == BitsPerWord::BITS_7) { + options.c_cflag |= CS7; + } else if (bits == BitsPerWord::BITS_8) { + options.c_cflag |= CS8; + } +} + +void uart::enableRead(struct termios& options) { options.c_cflag |= CREAD; } + +void uart::ignoreCtrlLines(struct termios& options) { options.c_cflag |= CLOCAL; } + +void uart::setParity(struct termios& options, Parity parity) { + /* Clear parity bit */ + options.c_cflag &= ~PARENB; + switch (parity) { + case Parity::EVEN: + options.c_cflag |= PARENB; + options.c_cflag &= ~PARODD; + break; + case Parity::ODD: + options.c_cflag |= PARENB; + options.c_cflag |= PARODD; + break; + default: + break; + } +} + int uart::readCountersAndErrors(int serialPort, serial_icounter_struct& icounter) { return ioctl(serialPort, TIOCGICOUNT, &icounter); } + +void uart::setStopbits(struct termios& options, StopBits bits) { + if (bits == StopBits::TWO_STOP_BITS) { + // Use two stop bits + options.c_cflag |= CSTOPB; + } else { + // Clear stop field, only one stop bit used in communication + options.c_cflag &= ~CSTOPB; + } +} diff --git a/src/fsfw_hal/linux/uart/helper.h b/src/fsfw_hal/linux/uart/helper.h index 515f815b..7f067d00 100644 --- a/src/fsfw_hal/linux/uart/helper.h +++ b/src/fsfw_hal/linux/uart/helper.h @@ -54,6 +54,16 @@ void setMode(struct termios& options, UartModes mode); */ void setBaudrate(struct termios& options, UartBaudRate baud); +void setStopbits(struct termios& options, StopBits bits); + +void setBitsPerWord(struct termios& options, BitsPerWord bits); + +void enableRead(struct termios& options); + +void setParity(struct termios& options, Parity parity); + +void ignoreCtrlLines(struct termios& options); + int readCountersAndErrors(int serialPort, serial_icounter_struct& icounter); } // namespace uart