Merge branch 'develop' into mohr/cmake
This commit is contained in:
@ -1,5 +1,101 @@
|
||||
#include <linux/boardtest/I2cTestClass.h>
|
||||
#include "I2cTestClass.h"
|
||||
|
||||
I2cTestClass::I2cTestClass(object_id_t objectId) : TestTask(objectId) {}
|
||||
#include <errno.h>
|
||||
#include <fsfw_hal/linux/UnixFileGuard.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
ReturnValue_t I2cTestClass::performPeriodicAction() { return HasReturnvaluesIF::RETURN_OK; }
|
||||
#include "fsfw/globalfunctions/arrayprinter.h"
|
||||
#include "fsfw/serviceinterface.h"
|
||||
|
||||
I2cTestClass::I2cTestClass(object_id_t objectId, std::string i2cdev)
|
||||
: TestTask(objectId), i2cdev(i2cdev) {
|
||||
mode = TestModes::BPX_BATTERY;
|
||||
}
|
||||
|
||||
ReturnValue_t I2cTestClass::initialize() {
|
||||
if (mode == TestModes::BPX_BATTERY) {
|
||||
battInit();
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t I2cTestClass::performPeriodicAction() {
|
||||
if (mode == TestModes::BPX_BATTERY) {
|
||||
battPeriodic();
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void I2cTestClass::battInit() {
|
||||
sif::info << "I2cTestClass: BPX Initialization" << std::endl;
|
||||
UnixFileGuard fileHelper(i2cdev, &bpxInfo.fd, O_RDWR, "I2cTestClass::sendMessage");
|
||||
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
|
||||
sif::error << "Opening I2C device" << i2cdev << " failed" << std::endl;
|
||||
return;
|
||||
}
|
||||
if (ioctl(bpxInfo.fd, I2C_SLAVE, bpxInfo.addr) < 0) {
|
||||
sif::error << "Failed to acquire bus access and/or talk to slave" << std::endl;
|
||||
}
|
||||
cmdBuf[0] = BpxBattery::PORT_PING;
|
||||
cmdBuf[1] = 0x42;
|
||||
sendLen = 2;
|
||||
ReturnValue_t result = i2cWrite(bpxInfo.fd, cmdBuf.data(), sendLen);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return;
|
||||
}
|
||||
// Receive back port, error byte and ping reply
|
||||
recvLen = 3;
|
||||
result = i2cRead(bpxInfo.fd, replyBuf.data(), recvLen);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return;
|
||||
}
|
||||
sif::info << "Ping reply:" << std::endl;
|
||||
arrayprinter::print(replyBuf.data(), recvLen);
|
||||
if (replyBuf[2] != 0x42) {
|
||||
sif::warning << "Received ping reply not expected value 0x42" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void I2cTestClass::battPeriodic() {
|
||||
UnixFileGuard fileHelper(i2cdev, &bpxInfo.fd, O_RDWR, "I2cTestClass::sendMessage");
|
||||
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
|
||||
sif::error << "Opening I2C device" << i2cdev << " failed" << std::endl;
|
||||
return;
|
||||
}
|
||||
if (ioctl(bpxInfo.fd, I2C_SLAVE, bpxInfo.addr) < 0) {
|
||||
sif::error << "Failed to acquire bus access and/or talk to slave" << std::endl;
|
||||
}
|
||||
cmdBuf[0] = BpxBattery::PORT_GET_HK;
|
||||
sendLen = 1;
|
||||
ReturnValue_t result = i2cWrite(bpxInfo.fd, cmdBuf.data(), sendLen);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return;
|
||||
}
|
||||
// Receive back HK set
|
||||
recvLen = 23;
|
||||
result = i2cRead(bpxInfo.fd, replyBuf.data(), recvLen);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return;
|
||||
}
|
||||
sif::info << "HK reply:" << std::endl;
|
||||
arrayprinter::print(replyBuf.data(), recvLen);
|
||||
}
|
||||
|
||||
ReturnValue_t I2cTestClass::i2cWrite(int fd, uint8_t* data, size_t len) {
|
||||
if (write(fd, data, len) != static_cast<ssize_t>(len)) {
|
||||
sif::error << "Failed to write to I2C bus" << std::endl;
|
||||
sif::error << "Error " << errno << ": " << strerror(errno) << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t I2cTestClass::i2cRead(int fd, uint8_t* data, size_t len) {
|
||||
if (read(fd, data, len) != static_cast<ssize_t>(len)) {
|
||||
sif::error << "Failed to read from I2C bus" << std::endl;
|
||||
sif::error << "Error " << errno << ": " << strerror(errno) << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
@ -3,13 +3,38 @@
|
||||
|
||||
#include <test/testtasks/TestTask.h>
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
#include "mission/devices/devicedefinitions/BpxBatteryDefinitions.h"
|
||||
|
||||
class I2cTestClass : public TestTask {
|
||||
public:
|
||||
I2cTestClass(object_id_t objectId);
|
||||
I2cTestClass(object_id_t objectId, std::string i2cdev);
|
||||
|
||||
ReturnValue_t initialize() override;
|
||||
ReturnValue_t performPeriodicAction() override;
|
||||
|
||||
private:
|
||||
enum TestModes { NONE, BPX_BATTERY };
|
||||
struct I2cInfo {
|
||||
int addr = 0;
|
||||
int fd = 0;
|
||||
};
|
||||
|
||||
TestModes mode = TestModes::NONE;
|
||||
void battInit();
|
||||
void battPeriodic();
|
||||
|
||||
I2cInfo bpxInfo = {.addr = 0x07, .fd = 0};
|
||||
std::string i2cdev;
|
||||
size_t sendLen = 0;
|
||||
size_t recvLen = 0;
|
||||
std::array<uint8_t, 64> cmdBuf = {};
|
||||
std::array<uint8_t, 64> replyBuf = {};
|
||||
|
||||
ReturnValue_t i2cWrite(int fd, uint8_t* data, size_t len);
|
||||
ReturnValue_t i2cRead(int fd, uint8_t* data, size_t len);
|
||||
};
|
||||
|
||||
#endif /* LINUX_BOARDTEST_I2CTESTCLASS_H_ */
|
||||
|
@ -1,4 +1,6 @@
|
||||
#include "UartTestClass.h"
|
||||
|
||||
#include <fsfw/tasks/TaskFactory.h>
|
||||
#if defined(RASPBERRY_PI)
|
||||
#include "rpiConfig.h"
|
||||
#elif defined(XIPHOS_Q7S)
|
||||
@ -9,14 +11,37 @@
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <unistd.h> // write(), read(), close()
|
||||
|
||||
#include "fsfw/serviceinterface/ServiceInterface.h"
|
||||
#include "lwgps/lwgps.h"
|
||||
#include "fsfw/globalfunctions/CRC.h"
|
||||
#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
|
||||
|
||||
UartTestClass::UartTestClass(object_id_t objectId) : TestTask(objectId) {}
|
||||
UartTestClass::UartTestClass(object_id_t objectId) : TestTask(objectId) { mode = TestModes::SCEX; }
|
||||
|
||||
ReturnValue_t UartTestClass::initialize() {
|
||||
if (mode == TestModes::GPS) {
|
||||
gpsInit();
|
||||
} else if (mode == TestModes::SCEX) {
|
||||
scexInit();
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t UartTestClass::performOneShotAction() { return HasReturnvaluesIF::RETURN_OK; }
|
||||
|
||||
ReturnValue_t UartTestClass::performPeriodicAction() {
|
||||
if (mode == TestModes::GPS) {
|
||||
gpsPeriodic();
|
||||
} else if (mode == TestModes::SCEX) {
|
||||
scexPeriodic();
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void UartTestClass::gpsInit() {
|
||||
#if RPI_TEST_GPS_DEVICE == 1
|
||||
int result = lwgps_init(&gpsData);
|
||||
if (result == 0) {
|
||||
@ -62,16 +87,9 @@ ReturnValue_t UartTestClass::initialize() {
|
||||
// Flush received and unread data. Those are old NMEA strings which are not relevant anymore
|
||||
tcflush(serialPort, TCIFLUSH);
|
||||
#endif
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t UartTestClass::performOneShotAction() {
|
||||
#if RPI_TEST_GPS_DEVICE == 1
|
||||
#endif
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t UartTestClass::performPeriodicAction() {
|
||||
void UartTestClass::gpsPeriodic() {
|
||||
#if RPI_TEST_GPS_DEVICE == 1
|
||||
int bytesRead = 0;
|
||||
do {
|
||||
@ -107,5 +125,97 @@ ReturnValue_t UartTestClass::performPeriodicAction() {
|
||||
}
|
||||
} while (bytesRead > 0);
|
||||
#endif
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void UartTestClass::scexInit() {
|
||||
#if defined(RASPBERRY_PI)
|
||||
std::string devname = "/dev/ttyUSB1";
|
||||
#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);
|
||||
|
||||
// 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] = 1; // In units of 0.1 seconds
|
||||
tty.c_cc[VMIN] = 255; // Read up to 255 bytes
|
||||
|
||||
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() {
|
||||
auto dleEncoder = DleEncoder();
|
||||
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;
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
// 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())) {
|
||||
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);
|
||||
}
|
||||
} while (bytesRead > 0);
|
||||
}
|
||||
|
@ -17,10 +17,23 @@ class UartTestClass : public TestTask {
|
||||
ReturnValue_t performPeriodicAction() override;
|
||||
|
||||
private:
|
||||
enum TestModes {
|
||||
GPS,
|
||||
// Solar Cell Experiment
|
||||
SCEX
|
||||
};
|
||||
|
||||
void gpsInit();
|
||||
void gpsPeriodic();
|
||||
|
||||
void scexInit();
|
||||
void scexPeriodic();
|
||||
TestModes mode = TestModes::GPS;
|
||||
lwgps_t gpsData = {};
|
||||
struct termios tty = {};
|
||||
int serialPort = 0;
|
||||
std::array<uint8_t, 512> recBuf;
|
||||
std::array<uint8_t, 64> cmdBuf = {};
|
||||
std::array<uint8_t, 4096> recBuf = {};
|
||||
uint8_t recvCnt = 0;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user