From 40e7b2dc31686840ea001628b7f8ebb803b995b2 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 6 Oct 2022 11:14:00 +0200 Subject: [PATCH] new uart helper module --- src/fsfw_hal/linux/uart/CMakeLists.txt | 2 +- src/fsfw_hal/linux/uart/UartComIF.cpp | 147 +----------------------- src/fsfw_hal/linux/uart/UartComIF.h | 12 +- src/fsfw_hal/linux/uart/UartCookie.h | 41 +------ src/fsfw_hal/linux/uart/helper.cpp | 150 +++++++++++++++++++++++++ src/fsfw_hal/linux/uart/helper.h | 62 ++++++++++ 6 files changed, 221 insertions(+), 193 deletions(-) create mode 100644 src/fsfw_hal/linux/uart/helper.cpp create mode 100644 src/fsfw_hal/linux/uart/helper.h diff --git a/src/fsfw_hal/linux/uart/CMakeLists.txt b/src/fsfw_hal/linux/uart/CMakeLists.txt index 9cad62a4..d8daa9ff 100644 --- a/src/fsfw_hal/linux/uart/CMakeLists.txt +++ b/src/fsfw_hal/linux/uart/CMakeLists.txt @@ -1 +1 @@ -target_sources(${LIB_FSFW_NAME} PUBLIC UartComIF.cpp UartCookie.cpp) +target_sources(${LIB_FSFW_NAME} PUBLIC UartComIF.cpp UartCookie.cpp helper.cpp) diff --git a/src/fsfw_hal/linux/uart/UartComIF.cpp b/src/fsfw_hal/linux/uart/UartComIF.cpp index 8947c562..df21da64 100644 --- a/src/fsfw_hal/linux/uart/UartComIF.cpp +++ b/src/fsfw_hal/linux/uart/UartComIF.cpp @@ -1,5 +1,4 @@ #include "UartComIF.h" - #include #include #include @@ -93,7 +92,7 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) { setStopBitOptions(&options, uartCookie); setDatasizeOptions(&options, uartCookie); setFixedOptions(&options); - setUartMode(&options, *uartCookie); + uart::setMode(options, uartCookie->getUartMode()); if (uartCookie->getInputShouldBeFlushed()) { tcflush(fd, TCIFLUSH); } @@ -102,7 +101,7 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) { options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 0; - configureBaudrate(&options, uartCookie); + uart::setBaudrate(options, uartCookie->getBaudrate()); /* Save option settings */ if (tcsetattr(fd, TCSANOW, &options) != 0) { @@ -191,138 +190,6 @@ void UartComIF::setFixedOptions(struct termios* options) { options->c_oflag &= ~ONLCR; } -void UartComIF::configureBaudrate(struct termios* options, UartCookie* uartCookie) { - switch (uartCookie->getBaudrate()) { - case UartBaudRate::RATE_50: - cfsetispeed(options, B50); - cfsetospeed(options, B50); - break; - case UartBaudRate::RATE_75: - cfsetispeed(options, B75); - cfsetospeed(options, B75); - break; - case UartBaudRate::RATE_110: - cfsetispeed(options, B110); - cfsetospeed(options, B110); - break; - case UartBaudRate::RATE_134: - cfsetispeed(options, B134); - cfsetospeed(options, B134); - break; - case UartBaudRate::RATE_150: - cfsetispeed(options, B150); - cfsetospeed(options, B150); - break; - case UartBaudRate::RATE_200: - cfsetispeed(options, B200); - cfsetospeed(options, B200); - break; - case UartBaudRate::RATE_300: - cfsetispeed(options, B300); - cfsetospeed(options, B300); - break; - case UartBaudRate::RATE_600: - cfsetispeed(options, B600); - cfsetospeed(options, B600); - break; - case UartBaudRate::RATE_1200: - cfsetispeed(options, B1200); - cfsetospeed(options, B1200); - break; - case UartBaudRate::RATE_1800: - cfsetispeed(options, B1800); - cfsetospeed(options, B1800); - break; - case UartBaudRate::RATE_2400: - cfsetispeed(options, B2400); - cfsetospeed(options, B2400); - break; - case UartBaudRate::RATE_4800: - cfsetispeed(options, B4800); - cfsetospeed(options, B4800); - break; - case UartBaudRate::RATE_9600: - cfsetispeed(options, B9600); - cfsetospeed(options, B9600); - break; - case UartBaudRate::RATE_19200: - cfsetispeed(options, B19200); - cfsetospeed(options, B19200); - break; - case UartBaudRate::RATE_38400: - cfsetispeed(options, B38400); - cfsetospeed(options, B38400); - break; - case UartBaudRate::RATE_57600: - cfsetispeed(options, B57600); - cfsetospeed(options, B57600); - break; - case UartBaudRate::RATE_115200: - cfsetispeed(options, B115200); - cfsetospeed(options, B115200); - break; - case UartBaudRate::RATE_230400: - cfsetispeed(options, B230400); - cfsetospeed(options, B230400); - break; -#ifndef __APPLE__ - case UartBaudRate::RATE_460800: - cfsetispeed(options, B460800); - cfsetospeed(options, B460800); - break; - case UartBaudRate::RATE_500000: - cfsetispeed(options, B500000); - cfsetospeed(options, B500000); - break; - case UartBaudRate::RATE_576000: - cfsetispeed(options, B576000); - cfsetospeed(options, B576000); - break; - case UartBaudRate::RATE_921600: - cfsetispeed(options, B921600); - cfsetospeed(options, B921600); - break; - case UartBaudRate::RATE_1000000: - cfsetispeed(options, B1000000); - cfsetospeed(options, B1000000); - break; - case UartBaudRate::RATE_1152000: - cfsetispeed(options, B1152000); - cfsetospeed(options, B1152000); - break; - case UartBaudRate::RATE_1500000: - cfsetispeed(options, B1500000); - cfsetospeed(options, B1500000); - break; - case UartBaudRate::RATE_2000000: - cfsetispeed(options, B2000000); - cfsetospeed(options, B2000000); - break; - case UartBaudRate::RATE_2500000: - cfsetispeed(options, B2500000); - cfsetospeed(options, B2500000); - break; - case UartBaudRate::RATE_3000000: - cfsetispeed(options, B3000000); - cfsetospeed(options, B3000000); - break; - case UartBaudRate::RATE_3500000: - cfsetispeed(options, B3500000); - cfsetospeed(options, B3500000); - break; - case UartBaudRate::RATE_4000000: - cfsetispeed(options, B4000000); - cfsetospeed(options, B4000000); - break; -#endif // ! __APPLE__ - default: -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl; -#endif - break; - } -} - ReturnValue_t UartComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) { int fd = 0; std::string deviceFile; @@ -592,12 +459,4 @@ ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF* cookie) { return returnvalue::FAILED; } -void UartComIF::setUartMode(struct termios* options, UartCookie& uartCookie) { - UartModes uartMode = uartCookie.getUartMode(); - if (uartMode == UartModes::NON_CANONICAL) { - /* Disable canonical mode */ - options->c_lflag &= ~ICANON; - } else if (uartMode == UartModes::CANONICAL) { - options->c_lflag |= ICANON; - } -} + diff --git a/src/fsfw_hal/linux/uart/UartComIF.h b/src/fsfw_hal/linux/uart/UartComIF.h index 77318166..940938d9 100644 --- a/src/fsfw_hal/linux/uart/UartComIF.h +++ b/src/fsfw_hal/linux/uart/UartComIF.h @@ -1,13 +1,15 @@ #ifndef BSP_Q7S_COMIF_UARTCOMIF_H_ #define BSP_Q7S_COMIF_UARTCOMIF_H_ +#include "UartCookie.h" +#include "helper.h" + #include #include #include #include -#include "UartCookie.h" /** * @brief This is the communication interface to access serial ports on linux based operating @@ -101,14 +103,6 @@ class UartComIF : public DeviceCommunicationIF, public SystemObject { */ void setDatasizeOptions(struct termios* options, UartCookie* uartCookie); - /** - * @brief This functions adds the baudrate specified in the uartCookie to the termios options - * struct. - */ - void configureBaudrate(struct termios* options, UartCookie* uartCookie); - - void setUartMode(struct termios* options, UartCookie& uartCookie); - ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter, size_t requestLen); ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter, diff --git a/src/fsfw_hal/linux/uart/UartCookie.h b/src/fsfw_hal/linux/uart/UartCookie.h index cae33d58..6fa2bd1b 100644 --- a/src/fsfw_hal/linux/uart/UartCookie.h +++ b/src/fsfw_hal/linux/uart/UartCookie.h @@ -1,51 +1,14 @@ #ifndef SAM9G20_COMIF_COOKIES_UART_COOKIE_H_ #define SAM9G20_COMIF_COOKIES_UART_COOKIE_H_ +#include "helper.h" + #include #include #include -enum class Parity { NONE, EVEN, ODD }; -enum class StopBits { ONE_STOP_BIT, TWO_STOP_BITS }; - -enum class UartModes { CANONICAL, NON_CANONICAL }; - -enum class BitsPerWord { BITS_5, BITS_6, BITS_7, BITS_8 }; - -enum class UartBaudRate { - RATE_50, - RATE_75, - RATE_110, - RATE_134, - RATE_150, - RATE_200, - RATE_300, - RATE_600, - RATE_1200, - RATE_1800, - RATE_2400, - RATE_4800, - RATE_9600, - RATE_19200, - RATE_38400, - RATE_57600, - RATE_115200, - RATE_230400, - RATE_460800, - RATE_500000, - RATE_576000, - RATE_921600, - RATE_1000000, - RATE_1152000, - RATE_1500000, - RATE_2000000, - RATE_2500000, - RATE_3000000, - RATE_3500000, - RATE_4000000 -}; /** * @brief Cookie for the UartComIF. There are many options available to configure the UART driver. diff --git a/src/fsfw_hal/linux/uart/helper.cpp b/src/fsfw_hal/linux/uart/helper.cpp new file mode 100644 index 00000000..b451f457 --- /dev/null +++ b/src/fsfw_hal/linux/uart/helper.cpp @@ -0,0 +1,150 @@ +#include "helper.h" +#include "fsfw/serviceinterface.h" + +#include + +void uart::setMode(struct termios& options, UartModes mode) { + if (mode == UartModes::NON_CANONICAL) { + /* Disable canonical mode */ + options.c_lflag &= ~ICANON; + } else if (mode == UartModes::CANONICAL) { + options.c_lflag |= ICANON; + } +} + +void uart::setBaudrate(struct termios& options, UartBaudRate baud) { + switch (baud) { + case UartBaudRate::RATE_50: + cfsetispeed(&options, B50); + cfsetospeed(&options, B50); + break; + case UartBaudRate::RATE_75: + cfsetispeed(&options, B75); + cfsetospeed(&options, B75); + break; + case UartBaudRate::RATE_110: + cfsetispeed(&options, B110); + cfsetospeed(&options, B110); + break; + case UartBaudRate::RATE_134: + cfsetispeed(&options, B134); + cfsetospeed(&options, B134); + break; + case UartBaudRate::RATE_150: + cfsetispeed(&options, B150); + cfsetospeed(&options, B150); + break; + case UartBaudRate::RATE_200: + cfsetispeed(&options, B200); + cfsetospeed(&options, B200); + break; + case UartBaudRate::RATE_300: + cfsetispeed(&options, B300); + cfsetospeed(&options, B300); + break; + case UartBaudRate::RATE_600: + cfsetispeed(&options, B600); + cfsetospeed(&options, B600); + break; + case UartBaudRate::RATE_1200: + cfsetispeed(&options, B1200); + cfsetospeed(&options, B1200); + break; + case UartBaudRate::RATE_1800: + cfsetispeed(&options, B1800); + cfsetospeed(&options, B1800); + break; + case UartBaudRate::RATE_2400: + cfsetispeed(&options, B2400); + cfsetospeed(&options, B2400); + break; + case UartBaudRate::RATE_4800: + cfsetispeed(&options, B4800); + cfsetospeed(&options, B4800); + break; + case UartBaudRate::RATE_9600: + cfsetispeed(&options, B9600); + cfsetospeed(&options, B9600); + break; + case UartBaudRate::RATE_19200: + cfsetispeed(&options, B19200); + cfsetospeed(&options, B19200); + break; + case UartBaudRate::RATE_38400: + cfsetispeed(&options, B38400); + cfsetospeed(&options, B38400); + break; + case UartBaudRate::RATE_57600: + cfsetispeed(&options, B57600); + cfsetospeed(&options, B57600); + break; + case UartBaudRate::RATE_115200: + cfsetispeed(&options, B115200); + cfsetospeed(&options, B115200); + break; + case UartBaudRate::RATE_230400: + cfsetispeed(&options, B230400); + cfsetospeed(&options, B230400); + break; +#ifndef __APPLE__ + case UartBaudRate::RATE_460800: + cfsetispeed(&options, B460800); + cfsetospeed(&options, B460800); + break; + case UartBaudRate::RATE_500000: + cfsetispeed(&options, B500000); + cfsetospeed(&options, B500000); + break; + case UartBaudRate::RATE_576000: + cfsetispeed(&options, B576000); + cfsetospeed(&options, B576000); + break; + case UartBaudRate::RATE_921600: + cfsetispeed(&options, B921600); + cfsetospeed(&options, B921600); + break; + case UartBaudRate::RATE_1000000: + cfsetispeed(&options, B1000000); + cfsetospeed(&options, B1000000); + break; + case UartBaudRate::RATE_1152000: + cfsetispeed(&options, B1152000); + cfsetospeed(&options, B1152000); + break; + case UartBaudRate::RATE_1500000: + cfsetispeed(&options, B1500000); + cfsetospeed(&options, B1500000); + break; + case UartBaudRate::RATE_2000000: + cfsetispeed(&options, B2000000); + cfsetospeed(&options, B2000000); + break; + case UartBaudRate::RATE_2500000: + cfsetispeed(&options, B2500000); + cfsetospeed(&options, B2500000); + break; + case UartBaudRate::RATE_3000000: + cfsetispeed(&options, B3000000); + cfsetospeed(&options, B3000000); + break; + case UartBaudRate::RATE_3500000: + cfsetispeed(&options, B3500000); + cfsetospeed(&options, B3500000); + break; + case UartBaudRate::RATE_4000000: + cfsetispeed(&options, B4000000); + cfsetospeed(&options, B4000000); + break; +#endif // ! __APPLE__ + default: +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl; +#endif + break; + } +} + +int uart::readCountersAndErrors(int serialPort, serial_icounter_struct& icounter) { + return ioctl(serialPort, TIOCGICOUNT, &icounter); +} + diff --git a/src/fsfw_hal/linux/uart/helper.h b/src/fsfw_hal/linux/uart/helper.h new file mode 100644 index 00000000..b6a524d6 --- /dev/null +++ b/src/fsfw_hal/linux/uart/helper.h @@ -0,0 +1,62 @@ +#ifndef FSFW_HAL_LINUX_UART_HELPER_H_ +#define FSFW_HAL_LINUX_UART_HELPER_H_ + +#include +#include + +enum class Parity { NONE, EVEN, ODD }; + +enum class StopBits { ONE_STOP_BIT, TWO_STOP_BITS }; + +enum class UartModes { CANONICAL, NON_CANONICAL }; + +enum class BitsPerWord { BITS_5, BITS_6, BITS_7, BITS_8 }; + +enum class UartBaudRate { + RATE_50, + RATE_75, + RATE_110, + RATE_134, + RATE_150, + RATE_200, + RATE_300, + RATE_600, + RATE_1200, + RATE_1800, + RATE_2400, + RATE_4800, + RATE_9600, + RATE_19200, + RATE_38400, + RATE_57600, + RATE_115200, + RATE_230400, + RATE_460800, + RATE_500000, + RATE_576000, + RATE_921600, + RATE_1000000, + RATE_1152000, + RATE_1500000, + RATE_2000000, + RATE_2500000, + RATE_3000000, + RATE_3500000, + RATE_4000000 +}; + +namespace uart { + +void setMode(struct termios& options, UartModes mode); +/** + * @brief This functions adds the baudrate specified in the uartCookie to the termios options + * struct. + */ +void setBaudrate(struct termios& options, UartBaudRate baud); + +int readCountersAndErrors(int serialPort, serial_icounter_struct& icounter); + +} + + +#endif /* FSFW_HAL_LINUX_UART_HELPER_H_ */