extended uart com if with canonical mode
This commit is contained in:
parent
cd0dfc49d5
commit
cd9c9647ec
@ -17,7 +17,7 @@ class GpioCookie;
|
|||||||
class LinuxLibgpioIF : public GpioIF, public SystemObject {
|
class LinuxLibgpioIF : public GpioIF, public SystemObject {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
static const uint8_t gpioRetvalId = CLASS_ID::LINUX_LIBGPIO_IF;
|
static const uint8_t gpioRetvalId = CLASS_ID::HAL_GPIO;
|
||||||
|
|
||||||
static constexpr ReturnValue_t UNKNOWN_GPIO_ID =
|
static constexpr ReturnValue_t UNKNOWN_GPIO_ID =
|
||||||
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 1);
|
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 1);
|
||||||
|
@ -22,7 +22,7 @@ class SpiCookie;
|
|||||||
*/
|
*/
|
||||||
class SpiComIF: public DeviceCommunicationIF, public SystemObject {
|
class SpiComIF: public DeviceCommunicationIF, public SystemObject {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t spiRetvalId = CLASS_ID::LINUX_SPI_COM_IF;
|
static constexpr uint8_t spiRetvalId = CLASS_ID::HAL_SPI;
|
||||||
static constexpr ReturnValue_t OPENING_FILE_FAILED =
|
static constexpr ReturnValue_t OPENING_FILE_FAILED =
|
||||||
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0);
|
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0);
|
||||||
/* Full duplex (ioctl) transfer failure */
|
/* Full duplex (ioctl) transfer failure */
|
||||||
|
@ -37,17 +37,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,14 +62,14 @@ int UartComIF::configureUartPort(UartCookie* uartCookie) {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@ -87,8 +87,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 +140,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 +150,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 +270,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,9 +297,7 @@ 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;
|
int fd = 0;
|
||||||
std::string deviceFile;
|
std::string deviceFile;
|
||||||
UartDeviceMapIter uartDeviceMapIter;
|
UartDeviceMapIter uartDeviceMapIter;
|
||||||
@ -317,6 +313,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()) {
|
||||||
@ -327,16 +324,24 @@ ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie,
|
|||||||
|
|
||||||
fd = uartDeviceMapIter->second.fileDescriptor;
|
fd = uartDeviceMapIter->second.fileDescriptor;
|
||||||
bufferPtr = uartDeviceMapIter->second.replyBuffer.data();
|
bufferPtr = uartDeviceMapIter->second.replyBuffer.data();
|
||||||
int bytesRead = read(fd, bufferPtr, requestLen);
|
if (uartMode == UartModes::CANONICAL) {
|
||||||
if (bytesRead != static_cast<int>(requestLen)) {
|
int bytesRead = read(fd, bufferPtr, uartCookie->getMaxReplyLen());
|
||||||
sif::debug << "UartComIF::requestReceiveMessage: Only read " << bytesRead
|
|
||||||
<< " of " << requestLen << " bytes" << std::endl;
|
|
||||||
return RETURN_FAILED;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
uartDeviceMapIter->second.replyLen = bytesRead;
|
uartDeviceMapIter->second.replyLen = bytesRead;
|
||||||
}
|
}
|
||||||
|
else if (uartMode == UartModes::NON_CANONICAL) {
|
||||||
|
int bytesRead = read(fd, bufferPtr, requestLen);
|
||||||
|
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 {
|
||||||
|
uartDeviceMapIter->second.replyLen = bytesRead;
|
||||||
|
}
|
||||||
|
}
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,8 +360,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 +373,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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -20,6 +20,13 @@
|
|||||||
*/
|
*/
|
||||||
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);
|
||||||
|
|
||||||
UartComIF(object_id_t objectId);
|
UartComIF(object_id_t objectId);
|
||||||
|
|
||||||
virtual ~UartComIF();
|
virtual ~UartComIF();
|
||||||
@ -37,14 +44,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 +96,8 @@ private:
|
|||||||
* struct.
|
* struct.
|
||||||
*/
|
*/
|
||||||
void configureBaudrate(struct termios* options, UartCookie* uartCookie);
|
void configureBaudrate(struct termios* options, UartCookie* uartCookie);
|
||||||
|
|
||||||
|
void setUartMode(struct termios* options, UartCookie& uartCookie);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */
|
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
#include <fsfw/serviceinterface/ServiceInterface.h>
|
#include <fsfw/serviceinterface/ServiceInterface.h>
|
||||||
|
|
||||||
UartCookie::UartCookie(std::string deviceFile, uint32_t baudrate, size_t maxReplyLen) :
|
UartCookie::UartCookie(std::string deviceFile, UartModes uartMode, uint32_t baudrate,
|
||||||
deviceFile(deviceFile), baudrate(baudrate), maxReplyLen(maxReplyLen) {
|
size_t maxReplyLen) :
|
||||||
|
deviceFile(deviceFile), uartMode(uartMode), baudrate(baudrate), maxReplyLen(maxReplyLen) {
|
||||||
}
|
}
|
||||||
|
|
||||||
UartCookie::~UartCookie() {}
|
UartCookie::~UartCookie() {}
|
||||||
@ -61,3 +62,7 @@ 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;
|
||||||
|
}
|
||||||
|
@ -15,8 +15,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 +32,20 @@ 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(std::string deviceFile, UartModes uartMode, uint32_t baudrate, size_t maxReplyLen);
|
||||||
|
|
||||||
virtual ~UartCookie();
|
virtual ~UartCookie();
|
||||||
|
|
||||||
@ -49,6 +55,7 @@ public:
|
|||||||
Parity getParity() const;
|
Parity getParity() const;
|
||||||
uint8_t getBitsPerWord() const;
|
uint8_t getBitsPerWord() const;
|
||||||
StopBits getStopBits() const;
|
StopBits getStopBits() const;
|
||||||
|
UartModes getUartMode() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functions two enable parity checking.
|
* Functions two enable parity checking.
|
||||||
@ -67,10 +74,10 @@ public:
|
|||||||
void setTwoStopBits();
|
void setTwoStopBits();
|
||||||
void setOneStopBit();
|
void setOneStopBit();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::string deviceFile;
|
std::string deviceFile;
|
||||||
|
const UartModes uartMode;
|
||||||
uint32_t baudrate;
|
uint32_t baudrate;
|
||||||
size_t maxReplyLen = 0;
|
size_t maxReplyLen = 0;
|
||||||
Parity parity = Parity::NONE;
|
Parity parity = Parity::NONE;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user