2021-06-16 18:38:06 +02:00
|
|
|
#include "UartTestClass.h"
|
2022-02-09 12:07:29 +01:00
|
|
|
|
2022-02-22 20:13:16 +01:00
|
|
|
#include <errno.h> // Error integer and strerror() function
|
|
|
|
#include <fcntl.h> // Contains file controls like O_RDWR
|
2022-02-09 12:07:29 +01:00
|
|
|
#include <fsfw/tasks/TaskFactory.h>
|
2022-01-17 13:48:55 +01:00
|
|
|
#include <unistd.h> // write(), read(), close()
|
2021-06-16 18:38:06 +02:00
|
|
|
|
2022-02-22 20:13:16 +01:00
|
|
|
#include "OBSWConfig.h"
|
2022-02-04 17:48:05 +01:00
|
|
|
#include "fsfw/globalfunctions/CRC.h"
|
2022-02-09 12:07:29 +01:00
|
|
|
#include "fsfw/globalfunctions/DleEncoder.h"
|
2022-02-04 17:48:05 +01:00
|
|
|
#include "fsfw/globalfunctions/arrayprinter.h"
|
|
|
|
#include "fsfw/serviceinterface.h"
|
|
|
|
#include "mission/devices/devicedefinitions/SCEXDefinitions.h"
|
2021-06-16 18:38:06 +02:00
|
|
|
|
2022-01-17 13:48:55 +01:00
|
|
|
#define GPS_REPLY_WIRETAPPING 0
|
2021-06-16 18:38:06 +02:00
|
|
|
|
2022-03-08 09:37:23 +01:00
|
|
|
#ifndef RPI_TEST_GPS_HANDLER
|
|
|
|
#define RPI_TEST_GPS_HANDLER 0
|
|
|
|
#endif
|
|
|
|
|
2022-02-04 17:48:05 +01:00
|
|
|
UartTestClass::UartTestClass(object_id_t objectId) : TestTask(objectId) { mode = TestModes::SCEX; }
|
2021-03-04 18:29:28 +01:00
|
|
|
|
2021-06-16 18:38:06 +02:00
|
|
|
ReturnValue_t UartTestClass::initialize() {
|
2022-02-03 13:37:48 +01:00
|
|
|
if (mode == TestModes::GPS) {
|
|
|
|
gpsInit();
|
2022-02-04 17:48:05 +01:00
|
|
|
} else if (mode == TestModes::SCEX) {
|
|
|
|
scexInit();
|
2022-02-03 13:37:48 +01:00
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-03 13:37:48 +01:00
|
|
|
}
|
|
|
|
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t UartTestClass::performOneShotAction() { return returnvalue::OK; }
|
2022-02-03 13:37:48 +01:00
|
|
|
|
|
|
|
ReturnValue_t UartTestClass::performPeriodicAction() {
|
2022-02-03 16:04:24 +01:00
|
|
|
if (mode == TestModes::GPS) {
|
2022-02-03 13:37:48 +01:00
|
|
|
gpsPeriodic();
|
2022-02-04 17:48:05 +01:00
|
|
|
} else if (mode == TestModes::SCEX) {
|
|
|
|
scexPeriodic();
|
2022-02-03 13:37:48 +01:00
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-03 13:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UartTestClass::gpsInit() {
|
2022-02-22 20:13:16 +01:00
|
|
|
#if RPI_TEST_GPS_HANDLER == 1
|
2022-01-17 13:48:55 +01:00
|
|
|
int result = lwgps_init(&gpsData);
|
|
|
|
if (result == 0) {
|
|
|
|
sif::warning << "lwgps_init error: " << result << std::endl;
|
|
|
|
}
|
2021-06-16 18:38:06 +02:00
|
|
|
|
2022-01-17 13:48:55 +01:00
|
|
|
/* Get file descriptor */
|
|
|
|
serialPort = open("/dev/serial0", O_RDWR);
|
|
|
|
if (serialPort < 0) {
|
|
|
|
sif::warning << "open call failed with error [" << errno << ", " << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
/* Setting up UART parameters */
|
|
|
|
tty.c_cflag &= ~PARENB; // Clear parity bit
|
|
|
|
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
|
|
|
|
tty.c_cflag &= ~CSIZE; // Clear all the size bits
|
|
|
|
tty.c_cflag |= CS8; // 8 bits per byte
|
|
|
|
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
|
|
|
|
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
|
|
|
|
// Use canonical mode for GPS device
|
|
|
|
tty.c_lflag |= ICANON;
|
|
|
|
tty.c_lflag &= ~ECHO; // Disable echo
|
|
|
|
tty.c_lflag &= ~ECHOE; // Disable erasure
|
|
|
|
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
|
|
|
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
|
|
|
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
|
|
|
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
|
|
|
|
ICRNL); // Disable any special handling of received bytes
|
|
|
|
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
|
|
|
|
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
2021-06-16 18:38:06 +02:00
|
|
|
|
2022-01-17 13:48:55 +01:00
|
|
|
// Non-blocking mode
|
|
|
|
tty.c_cc[VTIME] = 0;
|
|
|
|
tty.c_cc[VMIN] = 0;
|
2021-06-16 18:38:06 +02:00
|
|
|
|
2022-01-17 13:48:55 +01:00
|
|
|
cfsetispeed(&tty, B9600);
|
|
|
|
cfsetospeed(&tty, B9600);
|
|
|
|
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
|
|
|
|
sif::warning << "tcsetattr call failed with error [" << errno << ", " << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
;
|
|
|
|
}
|
|
|
|
// Flush received and unread data. Those are old NMEA strings which are not relevant anymore
|
|
|
|
tcflush(serialPort, TCIFLUSH);
|
2021-06-16 18:38:06 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:37:48 +01:00
|
|
|
void UartTestClass::gpsPeriodic() {
|
2022-02-22 20:13:16 +01:00
|
|
|
#if RPI_TEST_GPS_HANDLER == 1
|
2022-01-17 13:48:55 +01:00
|
|
|
int bytesRead = 0;
|
|
|
|
do {
|
|
|
|
bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
|
|
|
|
static_cast<unsigned int>(recBuf.size()));
|
|
|
|
if (bytesRead < 0) {
|
|
|
|
sif::warning << "UartTestClass::performPeriodicAction: read call failed with error [" << errno
|
|
|
|
<< ", " << strerror(errno) << "]" << std::endl;
|
|
|
|
break;
|
|
|
|
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
|
|
|
|
sif::debug << "UartTestClass::performPeriodicAction: "
|
|
|
|
"recv buffer might not be large enough"
|
|
|
|
<< std::endl;
|
|
|
|
} else if (bytesRead > 0) {
|
|
|
|
// pass data to lwgps for processing
|
2021-06-16 18:38:06 +02:00
|
|
|
#if GPS_REPLY_WIRETAPPING == 1
|
2022-01-17 13:48:55 +01:00
|
|
|
sif::info << recBuf.data() << std::endl;
|
2021-06-16 18:38:06 +02:00
|
|
|
#endif
|
2022-01-17 13:48:55 +01:00
|
|
|
int result = lwgps_process(&gpsData, recBuf.data(), bytesRead);
|
|
|
|
if (result == 0) {
|
|
|
|
sif::warning << "UartTestClass::performPeriodicAction: lwgps_process error" << std::endl;
|
|
|
|
}
|
|
|
|
recvCnt++;
|
|
|
|
if (recvCnt == 6) {
|
|
|
|
recvCnt = 0;
|
|
|
|
sif::info << "GPS Data" << std::endl;
|
|
|
|
// Print messages
|
|
|
|
printf("Valid status: %d\n", gpsData.is_valid);
|
|
|
|
printf("Latitude: %f degrees\n", gpsData.latitude);
|
|
|
|
printf("Longitude: %f degrees\n", gpsData.longitude);
|
|
|
|
printf("Altitude: %f meters\n", gpsData.altitude);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (bytesRead > 0);
|
2021-06-16 18:38:06 +02:00
|
|
|
#endif
|
2021-03-04 18:29:28 +01:00
|
|
|
}
|
2022-02-04 17:48:05 +01:00
|
|
|
|
|
|
|
void UartTestClass::scexInit() {
|
|
|
|
#if defined(RASPBERRY_PI)
|
2022-02-22 20:13:16 +01:00
|
|
|
std::string devname = "/dev/serial0";
|
2022-02-04 17:48:05 +01:00
|
|
|
#else
|
|
|
|
std::string devname = "/dev/ul-scex";
|
|
|
|
#endif
|
|
|
|
/* Get file descriptor */
|
|
|
|
serialPort = open(devname.c_str(), O_RDWR);
|
|
|
|
if (serialPort < 0) {
|
|
|
|
sif::warning << "open call failed with error [" << errno << ", " << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Setting up UART parameters
|
|
|
|
tty.c_cflag &= ~PARENB; // Clear parity bit
|
|
|
|
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
|
|
|
|
tty.c_cflag &= ~CSIZE; // Clear all the size bits
|
|
|
|
tty.c_cflag |= CS8; // 8 bits per byte
|
|
|
|
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
|
|
|
|
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
|
|
|
|
|
|
|
|
// Use non-canonical mode and clear echo flag
|
|
|
|
tty.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
|
2022-02-08 17:23:02 +01:00
|
|
|
// Non-blocking mode, read until either line is 0.1 second idle or maximum of 255 bytes are
|
|
|
|
// received in one go
|
2022-02-09 12:07:29 +01:00
|
|
|
tty.c_cc[VTIME] = 1; // In units of 0.1 seconds
|
|
|
|
tty.c_cc[VMIN] = 255; // Read up to 255 bytes
|
2022-02-04 17:48:05 +01:00
|
|
|
|
2022-02-22 20:13:16 +01:00
|
|
|
// Q7S UART Lite has fixed baud rate. For other linux systems, set baud rate here.
|
|
|
|
#if !defined(XIPHOS_Q7S)
|
|
|
|
if (cfsetispeed(&tty, B57600) != 0) {
|
|
|
|
sif::warning << "UartTestClass::scexInit: Setting baud rate failed" << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-02-04 17:48:05 +01:00
|
|
|
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
|
|
|
|
sif::warning << "tcsetattr call failed with error [" << errno << ", " << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
// Flush received and unread data
|
|
|
|
tcflush(serialPort, TCIFLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UartTestClass::scexPeriodic() {
|
2022-02-22 20:13:16 +01:00
|
|
|
sif::info << "UartTestClass::scexInit: Sending ping command to SCEX" << std::endl;
|
|
|
|
int result = prepareScexPing();
|
|
|
|
if (result != 0) {
|
2022-02-09 12:07:29 +01:00
|
|
|
return;
|
2022-02-22 20:13:16 +01:00
|
|
|
};
|
2022-02-08 15:58:17 +01:00
|
|
|
size_t bytesWritten = write(serialPort, cmdBuf.data(), encodedLen);
|
|
|
|
if (bytesWritten != encodedLen) {
|
2022-02-04 17:48:05 +01:00
|
|
|
sif::warning << "Sending ping command to solar experiment failed" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read back reply immediately
|
|
|
|
int bytesRead = 0;
|
|
|
|
do {
|
|
|
|
bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
|
|
|
|
static_cast<unsigned int>(recBuf.size()));
|
|
|
|
if (bytesRead < 0) {
|
|
|
|
sif::warning << "UartTestClass::performPeriodicAction: read call failed with error [" << errno
|
|
|
|
<< ", " << strerror(errno) << "]" << std::endl;
|
|
|
|
break;
|
|
|
|
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
|
2022-02-22 20:13:16 +01:00
|
|
|
sif::debug << "UartTestClass::performPeriodicAction: recv buffer might not be large enough"
|
2022-02-04 17:48:05 +01:00
|
|
|
<< std::endl;
|
|
|
|
} else if (bytesRead > 0) {
|
2022-02-22 20:13:16 +01:00
|
|
|
sif::info << "Received " << bytesRead
|
|
|
|
<< " bytes from the Solar Cell Experiment:" << std::endl;
|
|
|
|
arrayprinter::print(recBuf.data(), bytesRead, OutputType::HEX, false);
|
2022-02-04 17:48:05 +01:00
|
|
|
}
|
|
|
|
} while (bytesRead > 0);
|
|
|
|
}
|
2022-02-22 20:13:16 +01:00
|
|
|
|
|
|
|
int UartTestClass::prepareScexPing() {
|
|
|
|
std::array<uint8_t, 128> tmpCmdBuf = {};
|
|
|
|
// Send ping command
|
|
|
|
tmpCmdBuf[0] = scex::CMD_PING;
|
|
|
|
// These two fields are the packet counter and the total packet count. Those are 1 and 1 for each
|
|
|
|
// telecommand so far
|
|
|
|
tmpCmdBuf[1] = 1;
|
|
|
|
tmpCmdBuf[2] = 1;
|
|
|
|
uint16_t userDataLen = 0;
|
|
|
|
tmpCmdBuf[3] = (userDataLen >> 8) & 0xff;
|
|
|
|
tmpCmdBuf[4] = userDataLen & 0xff;
|
|
|
|
uint16_t crc = CRC::crc16ccitt(tmpCmdBuf.data(), 5);
|
|
|
|
tmpCmdBuf[5] = (crc >> 8) & 0xff;
|
|
|
|
tmpCmdBuf[6] = crc & 0xff;
|
|
|
|
ReturnValue_t result =
|
|
|
|
dleEncoder.encode(tmpCmdBuf.data(), 7, cmdBuf.data(), cmdBuf.size(), &encodedLen, true);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-22 20:13:16 +01:00
|
|
|
sif::warning << "UartTestClass::scexInit: Encoding failed" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|