moved uart com if

This commit is contained in:
Robin Müller 2021-05-25 17:21:44 +02:00
parent 2be6589251
commit b03c6f686e
7 changed files with 1 additions and 619 deletions

@ -1 +1 @@
Subproject commit 9e5e6da6f89e410094a935ce399b79e55cf7208a
Subproject commit 4ba4e45789a9fc37c8ff45d09b7986ee1dbd53ca

View File

@ -1,5 +1,4 @@
add_subdirectory(csp)
add_subdirectory(uart)
add_subdirectory(utility)
add_subdirectory(boardtest)
add_subdirectory(devices)

View File

@ -1,8 +0,0 @@
target_sources(${TARGET_NAME} PUBLIC
UartComIF.cpp
UartCookie.cpp
)

View File

@ -1,371 +0,0 @@
#include "UartComIF.h"
#include <fsfw/serviceinterface/ServiceInterface.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
UartComIF::UartComIF(object_id_t objectId): SystemObject(objectId){
}
UartComIF::~UartComIF() {}
ReturnValue_t UartComIF::initializeInterface(CookieIF * cookie) {
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
if(cookie == nullptr) {
return NULLPOINTER;
}
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if (uartCookie == nullptr) {
sif::error << "UartComIF::initializeInterface: Invalid UART Cookie!" << std::endl;
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if(uartDeviceMapIter == uartDeviceMap.end()) {
int fileDescriptor = configureUartPort(uartCookie);
if (fileDescriptor < 0) {
return RETURN_FAILED;
}
size_t maxReplyLen = uartCookie->getMaxReplyLen();
UartElements_t uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
std::pair status = uartDeviceMap.emplace(deviceFile, uartElements);
if (status.second == false) {
sif::debug << "UartComIF::initializeInterface: Failed to insert device " << deviceFile
<< "to Uart device map" << std::endl;
return RETURN_FAILED;
}
}
else {
sif::debug << "UartComIF::initializeInterface: Uart device " << deviceFile << "already in "
<< "use" << std::endl;
return RETURN_FAILED;
}
return RETURN_OK;
}
int UartComIF::configureUartPort(UartCookie* uartCookie) {
struct termios options;
std::string deviceFile = uartCookie->getDeviceFile();
int fd = open(deviceFile.c_str(), O_RDWR);
if (fd < 0) {
sif::debug << "UartComIF::configureUartPort: Failed to open uart " << deviceFile << "with"
<< " error code " << errno << strerror(errno) << std::endl;
return fd;
}
/* Read in existing settings */
if(tcgetattr(fd, &options) != 0) {
sif::debug << "UartComIF::configureUartPort: Error " << errno << "from tcgetattr: "
<< strerror(errno) << std::endl;
return fd;
}
setParityOptions(&options, uartCookie);
setStopBitOptions(&options, uartCookie);
setDatasizeOptions(&options, uartCookie);
setFixedOptions(&options);
/* Sets uart to non-blocking mode. Read returns immediately when there are no data available */
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
configureBaudrate(&options, uartCookie);
/* Save option settings */
if (tcsetattr(fd, TCSANOW, &options) != 0) {
sif::debug << "UartComIF::configureUartPort: Failed to set options with error " << errno
<< ": " << strerror(errno);
return fd;
}
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;
switch (uartCookie->getStopBits()) {
case StopBits::TWO_STOP_BITS:
options->c_cflag |= CSTOPB;
break;
default:
break;
}
}
void UartComIF::setDatasizeOptions(struct termios* options, UartCookie* uartCookie) {
/* Clear size bits */
options->c_cflag &= ~CSIZE;
switch (uartCookie->getBitsPerWord()) {
case 5:
options->c_cflag |= CS5;
break;
case 6:
options->c_cflag |= CS6;
break;
case 7:
options->c_cflag |= CS7;
break;
case 8:
options->c_cflag |= CS8;
break;
default:
sif::debug << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl;
break;
}
}
void UartComIF::setFixedOptions(struct termios* options) {
/* Disable RTS/CTS hardware flow control */
options->c_cflag &= ~CRTSCTS;
/* Turn on READ & ignore ctrl lines (CLOCAL = 1) */
options->c_cflag |= CREAD | CLOCAL;
/* Disable canonical mode */
options->c_lflag &= ~ICANON;
/* Disable echo */
options->c_lflag &= ~ECHO;
/* Disable erasure */
options->c_lflag &= ~ECHOE;
/* Disable new-line echo */
options->c_lflag &= ~ECHONL;
/* Disable interpretation of INTR, QUIT and SUSP */
options->c_lflag &= ~ISIG;
/* Turn off s/w flow ctrl */
options->c_iflag &= ~(IXON | IXOFF | IXANY);
/* Disable any special handling of received bytes */
options->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);
/* Prevent special interpretation of output bytes (e.g. newline chars) */
options->c_oflag &= ~OPOST;
/* Prevent conversion of newline to carriage return/line feed */
options->c_oflag &= ~ONLCR;
}
void UartComIF::configureBaudrate(struct termios* options, UartCookie* uartCookie) {
switch (uartCookie->getBaudrate()) {
case 50:
cfsetispeed(options, B50);
cfsetospeed(options, B50);
break;
case 75:
cfsetispeed(options, B75);
cfsetospeed(options, B75);
break;
case 110:
cfsetispeed(options, B110);
cfsetospeed(options, B110);
break;
case 134:
cfsetispeed(options, B134);
cfsetospeed(options, B134);
break;
case 150:
cfsetispeed(options, B150);
cfsetospeed(options, B150);
break;
case 200:
cfsetispeed(options, B200);
cfsetospeed(options, B200);
break;
case 300:
cfsetispeed(options, B300);
cfsetospeed(options, B300);
break;
case 600:
cfsetispeed(options, B600);
cfsetospeed(options, B600);
break;
case 1200:
cfsetispeed(options, B1200);
cfsetospeed(options, B1200);
break;
case 1800:
cfsetispeed(options, B1800);
cfsetospeed(options, B1800);
break;
case 2400:
cfsetispeed(options, B2400);
cfsetospeed(options, B2400);
break;
case 4800:
cfsetispeed(options, B4800);
cfsetospeed(options, B4800);
break;
case 9600:
cfsetispeed(options, B9600);
cfsetospeed(options, B9600);
break;
case 19200:
cfsetispeed(options, B19200);
cfsetospeed(options, B19200);
break;
case 38400:
cfsetispeed(options, B38400);
cfsetospeed(options, B38400);
break;
case 57600:
cfsetispeed(options, B57600);
cfsetospeed(options, B57600);
break;
case 115200:
cfsetispeed(options, B115200);
cfsetospeed(options, B115200);
break;
case 230400:
cfsetispeed(options, B230400);
cfsetospeed(options, B230400);
break;
case 460800:
cfsetispeed(options, B460800);
cfsetospeed(options, B460800);
break;
default:
sif::debug << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl;
break;
}
}
ReturnValue_t UartComIF::sendMessage(CookieIF *cookie,
const uint8_t *sendData, size_t sendLen) {
int fd;
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
if(sendData == nullptr) {
sif::debug << "UartComIF::sendMessage: Send Data is nullptr" << std::endl;
return RETURN_FAILED;
}
if(sendLen == 0) {
return RETURN_OK;
}
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) {
sif::debug << "UartComIF::sendMessasge: Invalid Uart Cookie!" << std::endl;
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) {
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile << "not in uart map"
<< std::endl;
return RETURN_FAILED;
}
fd = uartDeviceMapIter->second.fileDescriptor;
if (write(fd, sendData, sendLen) != (int)sendLen) {
sif::error << "UartComIF::sendMessage: Failed to send data with error code " << errno
<< ": Error description: " << strerror(errno) << std::endl;
return RETURN_FAILED;
}
return RETURN_OK;
}
ReturnValue_t UartComIF::getSendSuccess(CookieIF *cookie) {
return RETURN_OK;
}
ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie,
size_t requestLen) {
int fd;
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
uint8_t* bufferPtr;
if(requestLen == 0) {
return RETURN_OK;
}
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) {
sif::debug << "UartComIF::requestReceiveMessage: Invalid Uart Cookie!" << std::endl;
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) {
sif::debug << "UartComIF::requestReceiveMessage: Device file " << deviceFile
<< " not in uart map" << std::endl;
return RETURN_FAILED;
}
fd = uartDeviceMapIter->second.fileDescriptor;
bufferPtr = uartDeviceMapIter->second.replyBuffer.data();
int bytesRead = read(fd, bufferPtr, requestLen);
if (bytesRead != static_cast<int>(requestLen)) {
sif::debug << "UartComIF::requestReceiveMessage: Only read " << bytesRead
<< " of " << requestLen << " bytes" << std::endl;
return RETURN_FAILED;
}
else {
uartDeviceMapIter->second.replyLen = bytesRead;
}
return RETURN_OK;
}
ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie,
uint8_t **buffer, size_t* size) {
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) {
sif::debug << "UartComIF::readReceivedMessage: Invalid uart cookie!" << std::endl;
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) {
sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile
<< " not in uart map" << std::endl;
return RETURN_FAILED;
}
*buffer = uartDeviceMapIter->second.replyBuffer.data();
*size = uartDeviceMapIter->second.replyLen;
/* Length is reset to 0 to prevent reading the same data twice */
uartDeviceMapIter->second.replyLen = 0;
return RETURN_OK;
}

View File

@ -1,94 +0,0 @@
#ifndef BSP_Q7S_COMIF_UARTCOMIF_H_
#define BSP_Q7S_COMIF_UARTCOMIF_H_
#include <fsfw/objectmanager/SystemObject.h>
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <unordered_map>
#include <vector>
#include "UartCookie.h"
/**
* @brief This is the communication interface to access serial ports on linux based operating
* systems.
*
* @details The implementation follows the instructions from https://blog.mbedded.ninja/programming/
* operating-systems/linux/linux-serial-ports-using-c-cpp/#disabling-canonical-mode
*
* @author J. Meier
*/
class UartComIF: public DeviceCommunicationIF, public SystemObject {
public:
UartComIF(object_id_t objectId);
virtual ~UartComIF();
ReturnValue_t initializeInterface(CookieIF * cookie) override;
ReturnValue_t sendMessage(CookieIF *cookie,const uint8_t *sendData,
size_t sendLen) override;
ReturnValue_t getSendSuccess(CookieIF *cookie) override;
ReturnValue_t requestReceiveMessage(CookieIF *cookie,
size_t requestLen) override;
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
size_t *size) override;
private:
using UartDeviceFile_t = std::string;
typedef struct UartElements {
int fileDescriptor;
std::vector<uint8_t> replyBuffer;
/** Number of bytes read will be written to this variable */
size_t replyLen;
} UartElements_t;
using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements_t>;
using UartDeviceMapIter = UartDeviceMap::iterator;
/**
* The uart devie map stores informations of initialized uart ports.
*/
UartDeviceMap uartDeviceMap;
/**
* @brief This function opens and configures a uart device by using the information stored
* in the uart cookie.
* @param uartCookie Pointer to uart cookie with information about the uart. Contains the
* uart device file, baudrate, parity, stopbits etc.
* @return The file descriptor of the configured uart.
*/
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);
/**
* @brief This function sets options which are not configurable by the uartCookie.
*/
void setFixedOptions(struct termios* options);
/**
* @brief With this function the datasize settings are added to the termios options struct.
*/
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);
};
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */

View File

@ -1,63 +0,0 @@
#include "UartCookie.h"
#include <fsfw/serviceinterface/ServiceInterface.h>
UartCookie::UartCookie(std::string deviceFile, uint32_t baudrate, size_t maxReplyLen) :
deviceFile(deviceFile), baudrate(baudrate), maxReplyLen(maxReplyLen) {
}
UartCookie::~UartCookie() {}
uint32_t UartCookie::getBaudrate() const {
return baudrate;
}
size_t UartCookie::getMaxReplyLen() const {
return maxReplyLen;
}
std::string UartCookie::getDeviceFile() const {
return deviceFile;
}
void UartCookie::setParityOdd() {
parity = Parity::ODD;
}
void UartCookie::setParityEven() {
parity = Parity::EVEN;
}
Parity UartCookie::getParity() const {
return parity;
}
void UartCookie::setBitsPerWord(uint8_t bitsPerWord_) {
switch(bitsPerWord_) {
case 5:
case 6:
case 7:
case 8:
break;
default:
sif::debug << "UartCookie::setBitsPerWord: Invalid bits per word specified" << std::endl;
return;
}
bitsPerWord = bitsPerWord_;
}
uint8_t UartCookie::getBitsPerWord() const {
return bitsPerWord;
}
StopBits UartCookie::getStopBits() const {
return stopBits;
}
void UartCookie::setTwoStopBits() {
stopBits = StopBits::TWO_STOP_BITS;
}
void UartCookie::setOneStopBit() {
stopBits = StopBits::ONE_STOP_BIT;
}

View File

@ -1,81 +0,0 @@
#ifndef SAM9G20_COMIF_COOKIES_UART_COOKIE_H_
#define SAM9G20_COMIF_COOKIES_UART_COOKIE_H_
#include <fsfw/devicehandlers/CookieIF.h>
#include <string>
enum class Parity {
NONE,
EVEN,
ODD
};
enum class StopBits {
ONE_STOP_BIT,
TWO_STOP_BITS
};
/**
* @brief Cookie for the UartComIF. There are many options available to configure the uart driver.
* The constructor only requests for common options like the baudrate. Other options can
* be set by member functions.
*
* @author J. Meier
*/
class UartCookie: public CookieIF {
public:
/**
* @brief Constructor for the uart cookie.
* @param deviceFile The device file specifying the uart to use. E.g. "/dev/ttyPS1".
* @param baudrate The baudrate to use for input and output. Possible Baudrates are: 50,
* 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, B19200,
* 38400, 57600, 115200, 230400, 460800
* @param maxReplyLen The maximum size an object using this cookie expects.
*
* @details Default configuration: No parity
* 8 databits (number of bits transfered with one uart frame)
* One stop bit
*
*
*/
UartCookie(std::string deviceFile, uint32_t baudrate, size_t maxReplyLen);
virtual ~UartCookie();
uint32_t getBaudrate() const;
size_t getMaxReplyLen() const;
std::string getDeviceFile() const;
Parity getParity() const;
uint8_t getBitsPerWord() const;
StopBits getStopBits() const;
/**
* Functions two enable parity checking.
*/
void setParityOdd();
void setParityEven();
/**
* Function two set number of bits per UART frame.
*/
void setBitsPerWord(uint8_t bitsPerWord_);
/**
* Function to specify the number of stopbits.
*/
void setTwoStopBits();
void setOneStopBit();
private:
std::string deviceFile;
uint32_t baudrate;
size_t maxReplyLen = 0;
Parity parity = Parity::NONE;
uint8_t bitsPerWord = 8;
StopBits stopBits = StopBits::ONE_STOP_BIT;
};
#endif