eive-obsw/bsp_q7s/xadc/Xadc.cpp

150 lines
4.1 KiB
C++
Raw Permalink Normal View History

2022-03-13 16:32:40 +01:00
#include "Xadc.h"
#include <fcntl.h>
#include <unistd.h>
#include <sstream>
#include "fsfw/serviceinterface/ServiceInterfaceStream.h"
Xadc::Xadc() {}
Xadc::~Xadc() {}
ReturnValue_t Xadc::getTemperature(float& temperature) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-03-13 16:32:40 +01:00
int raw = 0;
int offset = 0;
float scale = 0;
result = readValFromFile<int>(xadc::file::tempRaw.c_str(), raw);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
result = readValFromFile<int>(xadc::file::tempOffset.c_str(), offset);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
result = readValFromFile<float>(xadc::file::tempScale.c_str(), scale);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
temperature = (raw + offset) * scale / 1000;
return result;
}
ReturnValue_t Xadc::getVccPint(float& vccPint) {
ReturnValue_t result =
readVoltageFromSysfs(xadc::file::vccpintRaw, xadc::file::vccpintScale, vccPint);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVccPaux(float& vccPaux) {
ReturnValue_t result =
readVoltageFromSysfs(xadc::file::vccpauxRaw, xadc::file::vccpauxScale, vccPaux);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVccInt(float& vccInt) {
ReturnValue_t result =
readVoltageFromSysfs(xadc::file::vccintRaw, xadc::file::vccintScale, vccInt);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVccAux(float& vccAux) {
ReturnValue_t result =
readVoltageFromSysfs(xadc::file::vccauxRaw, xadc::file::vccauxScale, vccAux);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVccBram(float& vccBram) {
ReturnValue_t result =
readVoltageFromSysfs(xadc::file::vccbramRaw, xadc::file::vccbramScale, vccBram);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVccOddr(float& vccOddr) {
ReturnValue_t result =
readVoltageFromSysfs(xadc::file::vccoddrRaw, xadc::file::vccoddrScale, vccOddr);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVrefp(float& vrefp) {
ReturnValue_t result = readVoltageFromSysfs(xadc::file::vrefpRaw, xadc::file::vrefpScale, vrefp);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::getVrefn(float& vrefn) {
ReturnValue_t result = readVoltageFromSysfs(xadc::file::vrefnRaw, xadc::file::vrefnScale, vrefn);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}
ReturnValue_t Xadc::readVoltageFromSysfs(std::string rawFile, std::string scaleFile,
float& voltage) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-03-13 16:32:40 +01:00
float raw = 0;
float scale = 0;
result = readValFromFile(rawFile.c_str(), raw);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
result = readValFromFile(scaleFile.c_str(), scale);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return result;
}
voltage = calculateVoltage(raw, scale);
return result;
}
float Xadc::calculateVoltage(int raw, float scale) { return static_cast<float>(raw * scale); }
template <typename T>
ReturnValue_t Xadc::readValFromFile(const char* filename, T& val) {
FILE* fp;
fp = fopen(filename, "r");
if (fp == nullptr) {
sif::warning << "Xadc::readValFromFile: Failed to open file " << filename << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-03-13 16:32:40 +01:00
}
2023-07-10 14:15:56 +02:00
char valstring[MAX_STR_LENGTH]{};
2022-03-13 16:32:40 +01:00
char* returnVal = fgets(valstring, MAX_STR_LENGTH, fp);
if (returnVal == nullptr) {
sif::warning << "Xadc::readValFromFile: Failed to read string from file " << filename
<< std::endl;
fclose(fp);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-03-13 16:32:40 +01:00
}
std::istringstream valSstream(valstring);
valSstream >> val;
2023-07-11 13:52:41 +02:00
if (valSstream.bad()) {
2023-07-10 14:15:56 +02:00
sif::warning << "Xadc: Conversion of value to target type failed" << std::endl;
fclose(fp);
return returnvalue::FAILED;
}
2022-03-13 16:32:40 +01:00
fclose(fp);
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-13 16:32:40 +01:00
}