From 44c8f5f7307549b2c7399fcdcf2f5014d25b2ee4 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 9 Apr 2022 01:00:42 +0200 Subject: [PATCH] bugfix for SCEX --- bsp_linux_board/ObjectFactory.cpp | 2 +- linux/boardtest/UartTestClass.cpp | 99 ++++++++++--------- linux/boardtest/UartTestClass.h | 4 +- .../devicedefinitions/SCEXDefinitions.h | 12 ++- 4 files changed, 66 insertions(+), 51 deletions(-) diff --git a/bsp_linux_board/ObjectFactory.cpp b/bsp_linux_board/ObjectFactory.cpp index 6bfa487f..fe6a0fa3 100644 --- a/bsp_linux_board/ObjectFactory.cpp +++ b/bsp_linux_board/ObjectFactory.cpp @@ -78,7 +78,7 @@ void ObjectFactory::produce(void* args) { createRpiAcsBoard(gpioIF, spiDev); #endif -#if OBSW_ADD_SUN_SENSORS == 1 || defined(OBSW_ADD_RTD_DEVICES) +#if OBSW_ADD_SUN_SENSORS == 1 || OBSW_ADD_RTD_DEVICES == 1 #ifdef RASPBERRY_PI rpi::gpio::initSpiCsDecoder(gpioIF); #endif diff --git a/linux/boardtest/UartTestClass.cpp b/linux/boardtest/UartTestClass.cpp index 6ca8ef9a..a59dc4ea 100644 --- a/linux/boardtest/UartTestClass.cpp +++ b/linux/boardtest/UartTestClass.cpp @@ -12,7 +12,6 @@ #include "fsfw/globalfunctions/DleEncoder.h" #include "fsfw/globalfunctions/arrayprinter.h" #include "fsfw/serviceinterface.h" -#include "mission/devices/devicedefinitions/SCEXDefinitions.h" #define GPS_REPLY_WIRETAPPING 0 @@ -24,6 +23,7 @@ UartTestClass::UartTestClass(object_id_t objectId, ScexUartReader* reader) : TestTask(objectId), reader(reader) { mode = TestModes::SCEX; scexMode = ScexModes::SIMPLE; + currCmd = scex::ScexCmds::ONE_CELL; } ReturnValue_t UartTestClass::initialize() { @@ -166,14 +166,16 @@ void UartTestClass::scexPeriodic() { scexSimplePeriodic(); } else { size_t len = 0; - prepareScexPing(cmdBuf.data(), &len); + prepareScexCmd(scex::ScexCmds::PING, false, cmdBuf.data(), &len); reader->sendMessage(uartCookie, cmdBuf.data(), len); } } -int UartTestClass::prepareScexPing(uint8_t* cmdBuf, size_t* len) { +int UartTestClass::prepareScexCmd(scex::ScexCmds cmd, bool tempCheck, + uint8_t* cmdBuf, size_t* len) { + using namespace scex; // Send ping command - cmdBuf[0] = scex::CMD_PING; + cmdBuf[0] = scex::createCmdByte(cmd, false); // These two fields are the packet counter and the total packet count. Those are 1 and 1 for each // telecommand so far cmdBuf[1] = 1; @@ -188,48 +190,6 @@ int UartTestClass::prepareScexPing(uint8_t* cmdBuf, size_t* len) { return 0; } -void UartTestClass::scexSimplePeriodic() { - sif::info << "UartTestClass::scexInit: Sending ping command to SCEX" << std::endl; - uint8_t tmpCmdBuf[32] = {}; - size_t len = 0; - prepareScexPing(tmpCmdBuf, &len); - ReturnValue_t result = - dleEncoder.encode(tmpCmdBuf, len, cmdBuf.data(), cmdBuf.size(), &encodedLen, true); - if (result != HasReturnvaluesIF::RETURN_OK) { - sif::warning << "UartTestClass::scexInit: Encoding failed" << std::endl; - return; - } - if (result != 0) { - return; - }; - size_t bytesWritten = write(serialPort, cmdBuf.data(), encodedLen); - if (bytesWritten != encodedLen) { - sif::warning << "Sending ping command to solar experiment failed" << std::endl; - } - - // Read back reply immediately - int bytesRead = 0; - do { - bytesRead = read(serialPort, reinterpret_cast(recBuf.data()), - static_cast(recBuf.size())); - if (bytesRead == 0) { - sif::warning << "Reading SCEX: Timeout occured after 0.5 seconds" << std::endl; - } else if (bytesRead < 0) { - sif::warning << "UartTestClass::performPeriodicAction: read call failed with error [" << errno - << ", " << strerror(errno) << "]" << std::endl; - break; - } else if (bytesRead >= static_cast(recBuf.size())) { - sif::debug << "UartTestClass::performPeriodicAction: recv buffer might not be large enough" - << std::endl; - } else if (bytesRead > 0) { - sif::info << "Received " << bytesRead - << " bytes from the Solar Cell Experiment:" << std::endl; - arrayprinter::print(recBuf.data(), bytesRead, OutputType::HEX, false); - break; - } - } while (bytesRead > 0); -} - void UartTestClass::scexSimpleInit() { #if defined(RASPBERRY_PI) std::string devname = "/dev/serial0"; @@ -256,8 +216,8 @@ void UartTestClass::scexSimpleInit() { // Non-blocking mode, read until either line is 0.1 second idle or maximum of 255 bytes are // received in one go - tty.c_cc[VTIME] = 5; // In units of 0.1 seconds - tty.c_cc[VMIN] = 1; // Read up to 255 bytes + tty.c_cc[VTIME] = 0; // In units of 0.1 seconds + tty.c_cc[VMIN] = 0; // Read up to 255 bytes // Q7S UART Lite has fixed baud rate. For other linux systems, set baud rate here. #if !defined(XIPHOS_Q7S) @@ -273,3 +233,46 @@ void UartTestClass::scexSimpleInit() { << std::endl; } } + +void UartTestClass::scexSimplePeriodic() { + using namespace scex; + sif::info << "UartTestClass::scexInit: Sending ping command to SCEX" << std::endl; + uint8_t tmpCmdBuf[32] = {}; + size_t len = 0; + prepareScexCmd(currCmd, false, tmpCmdBuf, &len); + ReturnValue_t result = + dleEncoder.encode(tmpCmdBuf, len, cmdBuf.data(), cmdBuf.size(), &encodedLen, true); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::warning << "UartTestClass::scexInit: Encoding failed" << std::endl; + return; + } + if (result != 0) { + return; + }; + size_t bytesWritten = write(serialPort, cmdBuf.data(), encodedLen); + if (bytesWritten != encodedLen) { + sif::warning << "Sending ping command to solar experiment failed" << std::endl; + } + + // Read back reply immediately + int bytesRead = 0; + do { + bytesRead = read(serialPort, reinterpret_cast(recBuf.data()), + static_cast(recBuf.size())); + if (bytesRead == 0) { + sif::warning << "Reading SCEX: Timeout or no bytes read" << std::endl; + } else if (bytesRead < 0) { + sif::warning << "UartTestClass::performPeriodicAction: read call failed with error [" << errno + << ", " << strerror(errno) << "]" << std::endl; + break; + } else if (bytesRead >= static_cast(recBuf.size())) { + sif::debug << "UartTestClass::performPeriodicAction: recv buffer might not be large enough" + << std::endl; + } else if (bytesRead > 0) { + sif::info << "Received " << bytesRead + << " bytes from the Solar Cell Experiment:" << std::endl; + arrayprinter::print(recBuf.data(), bytesRead, OutputType::HEX, false); + break; + } + } while (bytesRead > 0); +} diff --git a/linux/boardtest/UartTestClass.h b/linux/boardtest/UartTestClass.h index 7c046931..3836506c 100644 --- a/linux/boardtest/UartTestClass.h +++ b/linux/boardtest/UartTestClass.h @@ -8,6 +8,7 @@ #include #include "lwgps/lwgps.h" +#include "mission/devices/devicedefinitions/SCEXDefinitions.h" #include "test/testtasks/TestTask.h" class ScexUartReader; @@ -34,11 +35,12 @@ class UartTestClass : public TestTask { void scexInit(); void scexPeriodic(); - int prepareScexPing(uint8_t* cmdBuf, size_t* len); + int prepareScexCmd(scex::ScexCmds cmd, bool tempCheck, uint8_t* cmdBuf, size_t* len); void scexSimplePeriodic(); void scexSimpleInit(); + scex::ScexCmds currCmd = scex::ScexCmds::PING; TestModes mode = TestModes::GPS; DleEncoder dleEncoder = DleEncoder(); UartCookie* uartCookie = nullptr; diff --git a/mission/devices/devicedefinitions/SCEXDefinitions.h b/mission/devices/devicedefinitions/SCEXDefinitions.h index 8becabc8..f9c532ff 100644 --- a/mission/devices/devicedefinitions/SCEXDefinitions.h +++ b/mission/devices/devicedefinitions/SCEXDefinitions.h @@ -6,7 +6,17 @@ // Definitions for the Solar Cell Experiment namespace scex { -static constexpr uint8_t CMD_PING = 0x4e; +enum ScexCmds: uint8_t { + PING = 0b00111, + ONE_CELL = 0b00110 +}; + +static constexpr uint8_t IDLE_BIT_0_DEF_STATE = 0; +static constexpr uint8_t IDLE_BIT_1_DEF_STATE = 1; + +uint8_t createCmdByte(ScexCmds cmd, bool tempCheck) { + return (IDLE_BIT_0_DEF_STATE << 7) | (IDLE_BIT_1_DEF_STATE << 6) | (cmd << 1) | tempCheck; +} }