2022-02-03 13:37:48 +01:00
|
|
|
#include "I2cTestClass.h"
|
2021-02-14 19:22:58 +01:00
|
|
|
|
2022-02-03 13:37:48 +01:00
|
|
|
#include <errno.h>
|
2022-02-03 16:04:24 +01:00
|
|
|
#include <fsfw_hal/linux/UnixFileGuard.h>
|
2022-02-03 13:37:48 +01:00
|
|
|
#include <linux/i2c-dev.h>
|
2022-02-03 16:04:24 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "fsfw/globalfunctions/arrayprinter.h"
|
|
|
|
#include "fsfw/serviceinterface.h"
|
2021-02-14 19:22:58 +01:00
|
|
|
|
2022-02-03 13:37:48 +01:00
|
|
|
I2cTestClass::I2cTestClass(object_id_t objectId, std::string i2cdev)
|
2022-02-03 16:04:24 +01:00
|
|
|
: TestTask(objectId), i2cdev(i2cdev) {
|
2022-02-03 13:37:48 +01:00
|
|
|
mode = TestModes::BPX_BATTERY;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cTestClass::initialize() {
|
2022-02-03 16:04:24 +01:00
|
|
|
if (mode == TestModes::BPX_BATTERY) {
|
2022-02-03 13:37:48 +01:00
|
|
|
battInit();
|
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-03 13:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cTestClass::performPeriodicAction() {
|
2022-02-03 16:04:24 +01:00
|
|
|
if (mode == TestModes::BPX_BATTERY) {
|
2022-02-03 13:37:48 +01:00
|
|
|
battPeriodic();
|
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-03 13:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2cTestClass::battInit() {
|
|
|
|
sif::info << "I2cTestClass: BPX Initialization" << std::endl;
|
2023-02-18 13:46:00 +01:00
|
|
|
UnixFileGuard fileHelper(i2cdev, bpxInfo.fd, O_RDWR, "I2cTestClass::sendMessage");
|
2022-08-24 17:27:47 +02:00
|
|
|
if (fileHelper.getOpenResult() != returnvalue::OK) {
|
2022-02-03 13:37:48 +01:00
|
|
|
sif::error << "Opening I2C device" << i2cdev << " failed" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2022-02-03 16:04:24 +01:00
|
|
|
if (ioctl(bpxInfo.fd, I2C_SLAVE, bpxInfo.addr) < 0) {
|
2022-02-03 13:37:48 +01:00
|
|
|
sif::error << "Failed to acquire bus access and/or talk to slave" << std::endl;
|
|
|
|
}
|
2023-04-06 22:35:23 +02:00
|
|
|
cmdBuf[0] = bpxBat::PORT_PING;
|
2022-02-03 13:37:48 +01:00
|
|
|
cmdBuf[1] = 0x42;
|
|
|
|
sendLen = 2;
|
|
|
|
ReturnValue_t result = i2cWrite(bpxInfo.fd, cmdBuf.data(), sendLen);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-03 13:37:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Receive back port, error byte and ping reply
|
|
|
|
recvLen = 3;
|
|
|
|
result = i2cRead(bpxInfo.fd, replyBuf.data(), recvLen);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-03 13:37:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sif::info << "Ping reply:" << std::endl;
|
|
|
|
arrayprinter::print(replyBuf.data(), recvLen);
|
2022-02-03 16:04:24 +01:00
|
|
|
if (replyBuf[2] != 0x42) {
|
2022-02-03 13:37:48 +01:00
|
|
|
sif::warning << "Received ping reply not expected value 0x42" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2cTestClass::battPeriodic() {
|
2023-02-18 13:46:00 +01:00
|
|
|
UnixFileGuard fileHelper(i2cdev, bpxInfo.fd, O_RDWR, "I2cTestClass::sendMessage");
|
2022-08-24 17:27:47 +02:00
|
|
|
if (fileHelper.getOpenResult() != returnvalue::OK) {
|
2022-02-03 13:37:48 +01:00
|
|
|
sif::error << "Opening I2C device" << i2cdev << " failed" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2022-02-03 16:04:24 +01:00
|
|
|
if (ioctl(bpxInfo.fd, I2C_SLAVE, bpxInfo.addr) < 0) {
|
2022-02-03 13:37:48 +01:00
|
|
|
sif::error << "Failed to acquire bus access and/or talk to slave" << std::endl;
|
|
|
|
}
|
2023-04-06 22:35:23 +02:00
|
|
|
cmdBuf[0] = bpxBat::PORT_GET_HK;
|
2022-02-03 13:37:48 +01:00
|
|
|
sendLen = 1;
|
|
|
|
ReturnValue_t result = i2cWrite(bpxInfo.fd, cmdBuf.data(), sendLen);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-03 13:37:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Receive back HK set
|
|
|
|
recvLen = 23;
|
|
|
|
result = i2cRead(bpxInfo.fd, replyBuf.data(), recvLen);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-03 13:37:48 +01:00
|
|
|
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;
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cTestClass::i2cRead(int fd, uint8_t* data, size_t len) {
|
2022-02-03 16:04:24 +01:00
|
|
|
if (read(fd, data, len) != static_cast<ssize_t>(len)) {
|
2022-02-03 13:37:48 +01:00
|
|
|
sif::error << "Failed to read from I2C bus" << std::endl;
|
|
|
|
sif::error << "Error " << errno << ": " << strerror(errno) << std::endl;
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
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
|
|
|
}
|