UART ComIF update #11

Merged
meierj merged 10 commits from mueller/master into master 2021-06-21 16:58:32 +02:00
5 changed files with 361 additions and 199 deletions

View File

@ -1,6 +1,7 @@
#include "UartComIF.h" #include "UartComIF.h"
#include "OBSWConfig.h"
#include <fsfw/serviceinterface/ServiceInterface.h> #include "fsfw/serviceinterface/ServiceInterface.h"
#include <cstring> #include <cstring>
#include <fcntl.h> #include <fcntl.h>
@ -13,7 +14,7 @@ UartComIF::UartComIF(object_id_t objectId): SystemObject(objectId){
UartComIF::~UartComIF() {} UartComIF::~UartComIF() {}
ReturnValue_t UartComIF::initializeInterface(CookieIF * cookie) { ReturnValue_t UartComIF::initializeInterface(CookieIF* cookie) {
std::string deviceFile; std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter; UartDeviceMapIter uartDeviceMapIter;
@ -37,17 +38,17 @@ ReturnValue_t UartComIF::initializeInterface(CookieIF * cookie) {
return RETURN_FAILED; return RETURN_FAILED;
} }
size_t maxReplyLen = uartCookie->getMaxReplyLen(); size_t maxReplyLen = uartCookie->getMaxReplyLen();
UartElements_t uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0}; UartElements uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
auto status = uartDeviceMap.emplace(deviceFile, uartElements); auto status = uartDeviceMap.emplace(deviceFile, uartElements);
if (status.second == false) { if (status.second == false) {
sif::debug << "UartComIF::initializeInterface: Failed to insert device " << deviceFile sif::warning << "UartComIF::initializeInterface: Failed to insert device " <<
<< "to UART device map" << std::endl; deviceFile << "to UART device map" << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
} }
else { else {
sif::debug << "UartComIF::initializeInterface: UART device " << deviceFile << " already in " sif::warning << "UartComIF::initializeInterface: UART device " << deviceFile <<
<< "use" << std::endl; " already in use" << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
@ -56,20 +57,20 @@ ReturnValue_t UartComIF::initializeInterface(CookieIF * cookie) {
int UartComIF::configureUartPort(UartCookie* uartCookie) { int UartComIF::configureUartPort(UartCookie* uartCookie) {
struct termios options; struct termios options = {};
std::string deviceFile = uartCookie->getDeviceFile(); std::string deviceFile = uartCookie->getDeviceFile();
int fd = open(deviceFile.c_str(), O_RDWR); int fd = open(deviceFile.c_str(), O_RDWR);
if (fd < 0) { if (fd < 0) {
sif::debug << "UartComIF::configureUartPort: Failed to open uart " << deviceFile << "with" sif::warning << "UartComIF::configureUartPort: Failed to open uart " << deviceFile <<
<< " error code " << errno << strerror(errno) << std::endl; "with error code " << errno << strerror(errno) << std::endl;
return fd; return fd;
} }
/* Read in existing settings */ /* Read in existing settings */
if(tcgetattr(fd, &options) != 0) { if(tcgetattr(fd, &options) != 0) {
sif::debug << "UartComIF::configureUartPort: Error " << errno << "from tcgetattr: " sif::warning << "UartComIF::configureUartPort: Error " << errno << "from tcgetattr: "
<< strerror(errno) << std::endl; << strerror(errno) << std::endl;
return fd; return fd;
} }
@ -78,6 +79,9 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) {
setStopBitOptions(&options, uartCookie); setStopBitOptions(&options, uartCookie);
setDatasizeOptions(&options, uartCookie); setDatasizeOptions(&options, uartCookie);
setFixedOptions(&options); setFixedOptions(&options);
if(uartCookie->getInputShouldBeFlushed()) {
tcflush(fd, TCIFLUSH);
}
/* Sets uart to non-blocking mode. Read returns immediately when there are no data available */ /* Sets uart to non-blocking mode. Read returns immediately when there are no data available */
options.c_cc[VTIME] = 0; options.c_cc[VTIME] = 0;
@ -87,8 +91,8 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) {
/* Save option settings */ /* Save option settings */
if (tcsetattr(fd, TCSANOW, &options) != 0) { if (tcsetattr(fd, TCSANOW, &options) != 0) {
sif::debug << "UartComIF::configureUartPort: Failed to set options with error " << errno sif::warning << "UartComIF::configureUartPort: Failed to set options with error " <<
<< ": " << strerror(errno); errno << ": " << strerror(errno);
return fd; return fd;
} }
return fd; return fd;
@ -140,7 +144,7 @@ void UartComIF::setDatasizeOptions(struct termios* options, UartCookie* uartCook
options->c_cflag |= CS8; options->c_cflag |= CS8;
break; break;
default: default:
sif::debug << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl; sif::warning << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl;
break; break;
} }
} }
@ -150,8 +154,6 @@ void UartComIF::setFixedOptions(struct termios* options) {
options->c_cflag &= ~CRTSCTS; options->c_cflag &= ~CRTSCTS;
/* Turn on READ & ignore ctrl lines (CLOCAL = 1) */ /* Turn on READ & ignore ctrl lines (CLOCAL = 1) */
options->c_cflag |= CREAD | CLOCAL; options->c_cflag |= CREAD | CLOCAL;
/* Disable canonical mode */
options->c_lflag &= ~ICANON;
/* Disable echo */ /* Disable echo */
options->c_lflag &= ~ECHO; options->c_lflag &= ~ECHO;
/* Disable erasure */ /* Disable erasure */
@ -272,23 +274,23 @@ ReturnValue_t UartComIF::sendMessage(CookieIF *cookie,
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie); UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) { if(uartCookie == nullptr) {
sif::debug << "UartComIF::sendMessasge: Invalid Uart Cookie!" << std::endl; sif::debug << "UartComIF::sendMessasge: Invalid UART Cookie!" << std::endl;
return NULLPOINTER; return NULLPOINTER;
} }
deviceFile = uartCookie->getDeviceFile(); deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile); uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) { if (uartDeviceMapIter == uartDeviceMap.end()) {
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile << "not in uart map" sif::debug << "UartComIF::sendMessage: Device file " << deviceFile <<
<< std::endl; "not in UART map" << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
fd = uartDeviceMapIter->second.fileDescriptor; fd = uartDeviceMapIter->second.fileDescriptor;
if (write(fd, sendData, sendLen) != (int)sendLen) { if (write(fd, sendData, sendLen) != (int)sendLen) {
sif::error << "UartComIF::sendMessage: Failed to send data with error code " << errno sif::error << "UartComIF::sendMessage: Failed to send data with error code " <<
<< ": Error description: " << strerror(errno) << std::endl; errno << ": Error description: " << strerror(errno) << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
@ -299,13 +301,9 @@ ReturnValue_t UartComIF::getSendSuccess(CookieIF *cookie) {
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie, ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
size_t requestLen) {
int fd = 0;
std::string deviceFile; std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter; UartDeviceMapIter uartDeviceMapIter;
uint8_t* bufferPtr;
if(requestLen == 0) { if(requestLen == 0) {
return RETURN_OK; return RETURN_OK;
@ -317,6 +315,7 @@ ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie,
return NULLPOINTER; return NULLPOINTER;
} }
UartModes uartMode = uartCookie->getUartMode();
deviceFile = uartCookie->getDeviceFile(); deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile); uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) { if (uartDeviceMapIter == uartDeviceMap.end()) {
@ -325,19 +324,97 @@ ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie,
return RETURN_FAILED; return RETURN_FAILED;
} }
fd = uartDeviceMapIter->second.fileDescriptor; if (uartMode == UartModes::CANONICAL) {
bufferPtr = uartDeviceMapIter->second.replyBuffer.data(); return handleCanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
}
else if (uartMode == UartModes::NON_CANONICAL) {
return handleNoncanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
}
else {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
ReturnValue_t UartComIF::handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
uint8_t maxReadCycles = uartCookie.getReadCycles();
uint8_t currentReadCycles = 0;
int bytesRead = 0;
size_t currentBytesRead = 0;
size_t maxReplySize = uartCookie.getMaxReplyLen();
int fd = iter->second.fileDescriptor;
auto bufferPtr = iter->second.replyBuffer.data();
do {
size_t allowedReadSize = 0;
if(currentBytesRead >= maxReplySize) {
// Overflow risk. Emit warning, trigger event and break. If this happens,
// the reception buffer is not large enough or data is not polled often enough.
#if OBSW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
<< std::endl;
#else
sif::printWarning("UartComIF::requestReceiveMessage: "
"Next read would cause overflow!");
#endif
#endif
result = UART_RX_BUFFER_TOO_SMALL;
break;
}
else {
allowedReadSize = maxReplySize - currentBytesRead;
}
bytesRead = read(fd, bufferPtr, allowedReadSize);
if (bytesRead < 0) {
return RETURN_FAILED;
}
else if (bytesRead != static_cast<int>(requestLen)) {
sif::debug << "UartComIF::requestReceiveMessage: Only read " << bytesRead <<
" of " << requestLen << " bytes" << std::endl;
return RETURN_FAILED;
}
else if(bytesRead > 0) {
iter->second.replyLen += bytesRead;
bufferPtr += bytesRead;
currentBytesRead += bytesRead;
}
currentReadCycles++;
} while(bytesRead > 0 and currentReadCycles < maxReadCycles);
return result;
}
ReturnValue_t UartComIF::handleNoncanonicalRead(UartCookie &uartCookie, UartDeviceMapIter &iter,
size_t requestLen) {
int fd = iter->second.fileDescriptor;
auto bufferPtr = iter->second.replyBuffer.data();
// Size check to prevent buffer overflow
if(requestLen > uartCookie.getMaxReplyLen()) {
#if OBSW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
<< std::endl;
#else
sif::printWarning("UartComIF::requestReceiveMessage: "
"Next read would cause overflow!");
#endif
#endif
return UART_RX_BUFFER_TOO_SMALL;
}
int bytesRead = read(fd, bufferPtr, requestLen); int bytesRead = read(fd, bufferPtr, requestLen);
if (bytesRead != static_cast<int>(requestLen)) { if (bytesRead < 0) {
sif::debug << "UartComIF::requestReceiveMessage: Only read " << bytesRead return RETURN_FAILED;
<< " of " << requestLen << " bytes" << std::endl; }
else if (bytesRead != static_cast<int>(requestLen)) {
sif::debug << "UartComIF::requestReceiveMessage: Only read " << bytesRead <<
" of " << requestLen << " bytes" << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
else { else {
uartDeviceMapIter->second.replyLen = bytesRead; iter->second.replyLen = bytesRead;
} }
return HasReturnvaluesIF::RETURN_OK;
return RETURN_OK;
} }
ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie, ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie,
@ -355,8 +432,8 @@ ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie,
deviceFile = uartCookie->getDeviceFile(); deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile); uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) { if (uartDeviceMapIter == uartDeviceMap.end()) {
sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile <<
<< " not in uart map" << std::endl; " not in uart map" << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
@ -368,3 +445,14 @@ ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie,
return RETURN_OK; return RETURN_OK;
} }
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,14 +1,13 @@
#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 <fsfw/objectmanager/SystemObject.h> #include <fsfw/objectmanager/SystemObject.h>
#include <fsfw/devicehandlers/DeviceCommunicationIF.h> #include <fsfw/devicehandlers/DeviceCommunicationIF.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
* systems. * systems.
@ -20,6 +19,15 @@
*/ */
class UartComIF: public DeviceCommunicationIF, public SystemObject { class UartComIF: public DeviceCommunicationIF, public SystemObject {
public: public:
static constexpr uint8_t uartRetvalId = CLASS_ID::HAL_UART;
static constexpr ReturnValue_t UART_READ_FAILURE =
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 1);
static constexpr ReturnValue_t UART_READ_SIZE_MISSMATCH =
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 2);
static constexpr ReturnValue_t UART_RX_BUFFER_TOO_SMALL =
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 3);
UartComIF(object_id_t objectId); UartComIF(object_id_t objectId);
virtual ~UartComIF(); virtual ~UartComIF();
@ -37,14 +45,14 @@ private:
using UartDeviceFile_t = std::string; using UartDeviceFile_t = std::string;
typedef struct UartElements { struct UartElements {
int fileDescriptor; int fileDescriptor;
std::vector<uint8_t> replyBuffer; std::vector<uint8_t> replyBuffer;
/** Number of bytes read will be written to this variable */ /** Number of bytes read will be written to this variable */
size_t replyLen; size_t replyLen;
} UartElements_t; };
using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements_t>; using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements>;
using UartDeviceMapIter = UartDeviceMap::iterator; using UartDeviceMapIter = UartDeviceMap::iterator;
/** /**
@ -89,6 +97,14 @@ private:
* struct. * struct.
*/ */
void configureBaudrate(struct termios* options, UartCookie* uartCookie); 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,
size_t requestLen);
}; };
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */ #endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */

View File

@ -2,8 +2,10 @@
#include <fsfw/serviceinterface/ServiceInterface.h> #include <fsfw/serviceinterface/ServiceInterface.h>
UartCookie::UartCookie(std::string deviceFile, uint32_t baudrate, size_t maxReplyLen) : UartCookie::UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode,
deviceFile(deviceFile), baudrate(baudrate), maxReplyLen(maxReplyLen) { uint32_t baudrate, size_t maxReplyLen):
handlerId(handlerId), deviceFile(deviceFile), uartMode(uartMode), baudrate(baudrate),
maxReplyLen(maxReplyLen) {
} }
UartCookie::~UartCookie() {} UartCookie::~UartCookie() {}
@ -61,3 +63,27 @@ void UartCookie::setTwoStopBits() {
void UartCookie::setOneStopBit() { void UartCookie::setOneStopBit() {
stopBits = StopBits::ONE_STOP_BIT; stopBits = StopBits::ONE_STOP_BIT;
} }
UartModes UartCookie::getUartMode() const {
return uartMode;
}
void UartCookie::setReadCycles(uint8_t readCycles) {
this->readCycles = readCycles;
}
void UartCookie::setToFlushInput(bool enable) {
this->flushInput = enable;
}
uint8_t UartCookie::getReadCycles() const {
return readCycles;
}
bool UartCookie::getInputShouldBeFlushed() {
return this->flushInput;
}
object_id_t UartCookie::getHandlerId() const {
return this->handlerId;
}

View File

@ -2,6 +2,8 @@
#define SAM9G20_COMIF_COOKIES_UART_COOKIE_H_ #define SAM9G20_COMIF_COOKIES_UART_COOKIE_H_
#include <fsfw/devicehandlers/CookieIF.h> #include <fsfw/devicehandlers/CookieIF.h>
#include <fsfw/objectmanager/SystemObjectIF.h>
#include <string> #include <string>
enum class Parity { enum class Parity {
@ -15,8 +17,13 @@ enum class StopBits {
TWO_STOP_BITS TWO_STOP_BITS
}; };
enum class UartModes {
CANONICAL,
NON_CANONICAL
};
/** /**
* @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.
* The constructor only requests for common options like the baudrate. Other options can * The constructor only requests for common options like the baudrate. Other options can
* be set by member functions. * be set by member functions.
* *
@ -27,19 +34,21 @@ public:
/** /**
* @brief Constructor for the uart cookie. * @brief Constructor for the uart cookie.
* @param deviceFile The device file specifying the uart to use. E.g. "/dev/ttyPS1". * @param deviceFile The device file specifying the uart to use, e.g. "/dev/ttyPS1"
* @param uartMode Specify the UART mode. The canonical mode should be used if the
* messages are separated by a delimited character like '\n'. See the
* termios documentation for more information
* @param baudrate The baudrate to use for input and output. Possible Baudrates are: 50, * @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, * 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, B19200,
* 38400, 57600, 115200, 230400, 460800 * 38400, 57600, 115200, 230400, 460800
* @param maxReplyLen The maximum size an object using this cookie expects. * @param maxReplyLen The maximum size an object using this cookie expects
* * @details
* @details Default configuration: No parity * Default configuration: No parity
* 8 databits (number of bits transfered with one uart frame) * 8 databits (number of bits transfered with one uart frame)
* One stop bit * One stop bit
*
*
*/ */
UartCookie(std::string deviceFile, uint32_t baudrate, size_t maxReplyLen); UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode,
uint32_t baudrate, size_t maxReplyLen);
virtual ~UartCookie(); virtual ~UartCookie();
@ -49,6 +58,25 @@ public:
Parity getParity() const; Parity getParity() const;
uint8_t getBitsPerWord() const; uint8_t getBitsPerWord() const;
StopBits getStopBits() const; StopBits getStopBits() const;
UartModes getUartMode() const;
object_id_t getHandlerId() const;
/**
* The UART ComIF will only perform a specified number of read cycles for the canonical mode.
* The user can specify how many of those read cycles are performed for one device handler
* communication cycle. An example use-case would be to read all available GPS NMEA strings
* at once.
* @param readCycles
*/
void setReadCycles(uint8_t readCycles);
uint8_t getReadCycles() const;
/**
* Allows to flush the data which was received but has not been read yet. This is useful
* to discard obsolete data at software startup.
*/
void setToFlushInput(bool enable);
bool getInputShouldBeFlushed();
/** /**
* Functions two enable parity checking. * Functions two enable parity checking.
@ -67,14 +95,17 @@ public:
void setTwoStopBits(); void setTwoStopBits();
void setOneStopBit(); void setOneStopBit();
private: private:
const object_id_t handlerId;
std::string deviceFile; std::string deviceFile;
const UartModes uartMode;
bool flushInput = false;
uint32_t baudrate; uint32_t baudrate;
size_t maxReplyLen = 0; size_t maxReplyLen = 0;
Parity parity = Parity::NONE; Parity parity = Parity::NONE;
uint8_t bitsPerWord = 8; uint8_t bitsPerWord = 8;
uint8_t readCycles = 1;
StopBits stopBits = StopBits::ONE_STOP_BIT; StopBits stopBits = StopBits::ONE_STOP_BIT;
}; };

View File

@ -28,6 +28,7 @@ GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transf
txDmaHandle = new DMA_HandleTypeDef(); txDmaHandle = new DMA_HandleTypeDef();
rxDmaHandle = new DMA_HandleTypeDef(); rxDmaHandle = new DMA_HandleTypeDef();
spi::setSpiHandle(spiHandle); spi::setSpiHandle(spiHandle);
spi::assignSpiUserArgs(spi::SpiBus::SPI_1, spiHandle);
transferMode = transferMode_; transferMode = transferMode_;
if(transferMode == spi::TransferModes::DMA) { if(transferMode == spi::TransferModes::DMA) {
mspCfg = new spi::MspDmaConfigStruct(); mspCfg = new spi::MspDmaConfigStruct();