new uart helper module

This commit is contained in:
Robin Müller 2022-10-06 11:14:00 +02:00
parent b9d0ff8fb7
commit 40e7b2dc31
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
6 changed files with 221 additions and 193 deletions

View File

@ -1 +1 @@
target_sources(${LIB_FSFW_NAME} PUBLIC UartComIF.cpp UartCookie.cpp) target_sources(${LIB_FSFW_NAME} PUBLIC UartComIF.cpp UartCookie.cpp helper.cpp)

View File

@ -1,5 +1,4 @@
#include "UartComIF.h" #include "UartComIF.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
@ -93,7 +92,7 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) {
setStopBitOptions(&options, uartCookie); setStopBitOptions(&options, uartCookie);
setDatasizeOptions(&options, uartCookie); setDatasizeOptions(&options, uartCookie);
setFixedOptions(&options); setFixedOptions(&options);
setUartMode(&options, *uartCookie); uart::setMode(options, uartCookie->getUartMode());
if (uartCookie->getInputShouldBeFlushed()) { if (uartCookie->getInputShouldBeFlushed()) {
tcflush(fd, TCIFLUSH); tcflush(fd, TCIFLUSH);
} }
@ -102,7 +101,7 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) {
options.c_cc[VTIME] = 0; options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0; options.c_cc[VMIN] = 0;
configureBaudrate(&options, uartCookie); uart::setBaudrate(options, uartCookie->getBaudrate());
/* Save option settings */ /* Save option settings */
if (tcsetattr(fd, TCSANOW, &options) != 0) { if (tcsetattr(fd, TCSANOW, &options) != 0) {
@ -191,138 +190,6 @@ void UartComIF::setFixedOptions(struct termios* options) {
options->c_oflag &= ~ONLCR; 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) { ReturnValue_t UartComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
int fd = 0; int fd = 0;
std::string deviceFile; std::string deviceFile;
@ -592,12 +459,4 @@ ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF* cookie) {
return returnvalue::FAILED; 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;
}
}

View File

@ -1,13 +1,15 @@
#ifndef BSP_Q7S_COMIF_UARTCOMIF_H_ #ifndef BSP_Q7S_COMIF_UARTCOMIF_H_
#define BSP_Q7S_COMIF_UARTCOMIF_H_ #define BSP_Q7S_COMIF_UARTCOMIF_H_
#include "UartCookie.h"
#include "helper.h"
#include <fsfw/devicehandlers/DeviceCommunicationIF.h> #include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <fsfw/objectmanager/SystemObject.h> #include <fsfw/objectmanager/SystemObject.h>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "UartCookie.h"
/** /**
* @brief This is the communication interface to access serial ports on linux based operating * @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); 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, ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen); size_t requestLen);
ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter, ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,

View File

@ -1,51 +1,14 @@
#ifndef SAM9G20_COMIF_COOKIES_UART_COOKIE_H_ #ifndef SAM9G20_COMIF_COOKIES_UART_COOKIE_H_
#define SAM9G20_COMIF_COOKIES_UART_COOKIE_H_ #define SAM9G20_COMIF_COOKIES_UART_COOKIE_H_
#include "helper.h"
#include <fsfw/devicehandlers/CookieIF.h> #include <fsfw/devicehandlers/CookieIF.h>
#include <fsfw/objectmanager/SystemObjectIF.h> #include <fsfw/objectmanager/SystemObjectIF.h>
#include <string> #include <string>
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. * @brief Cookie for the UartComIF. There are many options available to configure the UART driver.

View File

@ -0,0 +1,150 @@
#include "helper.h"
#include "fsfw/serviceinterface.h"
#include <sys/ioctl.h>
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);
}

View File

@ -0,0 +1,62 @@
#ifndef FSFW_HAL_LINUX_UART_HELPER_H_
#define FSFW_HAL_LINUX_UART_HELPER_H_
#include <termios.h>
#include <linux/serial.h>
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_ */