From 398277ca088b4652d5c30e91bb8eca1e90b705b9 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 22 Feb 2022 20:13:16 +0100 Subject: [PATCH] this fixes SCEX ping for RPi --- linux/boardtest/UartTestClass.cpp | 88 +++++++++++++++---------------- linux/boardtest/UartTestClass.h | 4 ++ 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/linux/boardtest/UartTestClass.cpp b/linux/boardtest/UartTestClass.cpp index b8e7da10..1f8c2563 100644 --- a/linux/boardtest/UartTestClass.cpp +++ b/linux/boardtest/UartTestClass.cpp @@ -1,16 +1,11 @@ #include "UartTestClass.h" +#include // Error integer and strerror() function +#include // Contains file controls like O_RDWR #include -#if defined(RASPBERRY_PI) -#include "rpiConfig.h" -#elif defined(XIPHOS_Q7S) -#include "q7sConfig.h" -#endif - -#include // Error integer and strerror() function -#include // Contains file controls like O_RDWR #include // write(), read(), close() +#include "OBSWConfig.h" #include "fsfw/globalfunctions/CRC.h" #include "fsfw/globalfunctions/DleEncoder.h" #include "fsfw/globalfunctions/arrayprinter.h" @@ -42,7 +37,7 @@ ReturnValue_t UartTestClass::performPeriodicAction() { } void UartTestClass::gpsInit() { -#if RPI_TEST_GPS_DEVICE == 1 +#if RPI_TEST_GPS_HANDLER == 1 int result = lwgps_init(&gpsData); if (result == 0) { sif::warning << "lwgps_init error: " << result << std::endl; @@ -90,7 +85,7 @@ void UartTestClass::gpsInit() { } void UartTestClass::gpsPeriodic() { -#if RPI_TEST_GPS_DEVICE == 1 +#if RPI_TEST_GPS_HANDLER == 1 int bytesRead = 0; do { bytesRead = read(serialPort, reinterpret_cast(recBuf.data()), @@ -129,7 +124,7 @@ void UartTestClass::gpsPeriodic() { void UartTestClass::scexInit() { #if defined(RASPBERRY_PI) - std::string devname = "/dev/ttyUSB1"; + std::string devname = "/dev/serial0"; #else std::string devname = "/dev/ul-scex"; #endif @@ -156,6 +151,13 @@ void UartTestClass::scexInit() { tty.c_cc[VTIME] = 1; // In units of 0.1 seconds tty.c_cc[VMIN] = 255; // Read up to 255 bytes + // 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 + if (tcsetattr(serialPort, TCSANOW, &tty) != 0) { sif::warning << "tcsetattr call failed with error [" << errno << ", " << strerror(errno) << std::endl; @@ -165,37 +167,12 @@ void UartTestClass::scexInit() { } void UartTestClass::scexPeriodic() { - auto dleEncoder = DleEncoder(); - std::array 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; - - size_t encodedLen = 0; - ReturnValue_t result = - dleEncoder.encode(tmpCmdBuf.data(), 7, cmdBuf.data(), cmdBuf.size(), &encodedLen, true); - if (result != HasReturnvaluesIF::RETURN_OK) { - sif::warning << "UartTestClass::scexInit: Encoding failed" << std::endl; + sif::info << "UartTestClass::scexInit: Sending ping command to SCEX" << std::endl; + int result = prepareScexPing(); + if (result != 0) { return; - } - arrayprinter::print(cmdBuf.data(), 9); + }; size_t bytesWritten = write(serialPort, cmdBuf.data(), encodedLen); - - if (bytesWritten != encodedLen) { - sif::warning << "Sending ping command to solar experiment failed" << std::endl; - } - - TaskFactory::delayTask(20); - bytesWritten = write(serialPort, cmdBuf.data(), encodedLen); if (bytesWritten != encodedLen) { sif::warning << "Sending ping command to solar experiment failed" << std::endl; } @@ -210,12 +187,35 @@ void UartTestClass::scexPeriodic() { << ", " << strerror(errno) << "]" << std::endl; break; } else if (bytesRead >= static_cast(recBuf.size())) { - sif::debug << "UartTestClass::performPeriodicAction: " - "recv buffer might not be large enough" + sif::debug << "UartTestClass::performPeriodicAction: recv buffer might not be large enough" << std::endl; } else if (bytesRead > 0) { - sif::info << "Received " << bytesRead << " from the Solar Cell Experiment:" << std::endl; - arrayprinter::print(recBuf.data(), bytesRead); + sif::info << "Received " << bytesRead + << " bytes from the Solar Cell Experiment:" << std::endl; + arrayprinter::print(recBuf.data(), bytesRead, OutputType::HEX, false); } } while (bytesRead > 0); } + +int UartTestClass::prepareScexPing() { + std::array 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); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::warning << "UartTestClass::scexInit: Encoding failed" << std::endl; + return -1; + } + return 0; +} diff --git a/linux/boardtest/UartTestClass.h b/linux/boardtest/UartTestClass.h index 33194598..786d01ce 100644 --- a/linux/boardtest/UartTestClass.h +++ b/linux/boardtest/UartTestClass.h @@ -1,6 +1,7 @@ #ifndef LINUX_BOARDTEST_UARTTESTCLASS_H_ #define LINUX_BOARDTEST_UARTTESTCLASS_H_ +#include #include // Contains POSIX terminal control definitions #include @@ -28,7 +29,10 @@ class UartTestClass : public TestTask { void scexInit(); void scexPeriodic(); + int prepareScexPing(); TestModes mode = TestModes::GPS; + DleEncoder dleEncoder = DleEncoder(); + size_t encodedLen = 0; lwgps_t gpsData = {}; struct termios tty = {}; int serialPort = 0;