spi setup more stable now
All checks were successful
EIVE/eive-obsw/pipeline/pr-develop This commit looks good

This commit is contained in:
2022-05-14 11:34:25 +02:00
parent 1c9f411e75
commit 478975db26
14 changed files with 97 additions and 45 deletions

View File

@ -1,9 +1,11 @@
#include "Max31865RtdLowlevelHandler.h"
#include <fsfw/tasks/TaskFactory.h>
#include <fsfw/timemanager/Stopwatch.h>
#include <fsfw_hal/linux/spi/ManualCsLockGuard.h>
Max31865RtdReader::Max31865RtdReader(object_id_t objectId, SpiComIF* lowLevelComIF)
: SystemObject(objectId), rtds(EiveMax31855::NUM_RTDS), comIF(lowLevelComIF) {
Max31865RtdReader::Max31865RtdReader(object_id_t objectId, SpiComIF* lowLevelComIF, GpioIF* gpioIF)
: SystemObject(objectId), rtds(EiveMax31855::NUM_RTDS), comIF(lowLevelComIF), gpioIF(gpioIF) {
readerMutex = MutexFactory::instance()->createMutex();
}
@ -18,6 +20,7 @@ ReturnValue_t Max31865RtdReader::performOperation(uint8_t operationCode) {
void Max31865RtdReader::rtdMainLoop() {
using namespace MAX31865;
// Stopwatch watch;
if (periodicInitHandling()) {
// 10 ms delay for VBIAS startup
TaskFactory::delayTask(10);
@ -55,9 +58,15 @@ bool Max31865RtdReader::periodicInitHandling() {
}
if ((rtd->on or rtd->active) and not rtd->configured) {
if (rtd->cd.hasTimedOut()) {
uint8_t cfg = (Bias::OFF << CfgBitPos::BIAS_SEL) |
(Wires::FOUR_WIRE << CfgBitPos::WIRE_SEL) |
(ConvMode::NORM_OFF << CfgBitPos::CONV_MODE);
ManualCsLockWrapper mg(csLock, gpioIF, rtd->spiCookie, csTimeoutType, csTimeoutMs);
if (mg.lockResult != RETURN_OK or mg.gpioResult != RETURN_OK) {
sif::error << "Max31865RtdReader::periodicInitHandling: Manual CS lock failed"
<< std::endl;
break;
}
uint8_t cfg =
(Bias::OFF << CfgBitPos::BIAS_SEL) | (Wires::FOUR_WIRE << CfgBitPos::WIRE_SEL) |
(ConvMode::NORM_OFF << CfgBitPos::CONV_MODE) | (1 << CfgBitPos::FAULT_STATUS_CLEAR);
result = writeCfgReg(rtd->spiCookie, cfg);
if (result != HasReturnvaluesIF::RETURN_OK) {
handleSpiError(rtd, result, "writeCfgReg");
@ -97,6 +106,11 @@ bool Max31865RtdReader::periodicInitHandling() {
continue;
}
if (rtdIsActive(rtd->idx)) {
ManualCsLockWrapper mg(csLock, gpioIF, rtd->spiCookie, csTimeoutType, csTimeoutMs);
if (mg.lockResult != RETURN_OK or mg.gpioResult != RETURN_OK) {
sif::error << "Max31865RtdReader::periodicInitHandling: Manual CS lock failed" << std::endl;
break;
}
someRtdUsable = true;
result = writeBiasSel(Bias::ON, rtd->spiCookie);
}
@ -117,17 +131,24 @@ void Max31865RtdReader::periodicReadReqHandling() {
continue;
}
if (rtdIsActive(rtd->idx)) {
ManualCsLockWrapper mg(csLock, gpioIF, rtd->spiCookie, csTimeoutType, csTimeoutMs);
if (mg.lockResult != RETURN_OK or mg.gpioResult != RETURN_OK) {
sif::error << "Max31865RtdReader::periodicInitHandling: Manual CS lock failed" << std::endl;
break;
}
uint8_t currentCfg = 0;
auto result = readCfgReg(rtd->spiCookie, currentCfg);
if (result != RETURN_OK) {
handleSpiError(rtd, result, "readCfgReg");
continue;
// Release mutex ASAP
break;
}
currentCfg |= (1 << CfgBitPos::ONE_SHOT);
result = writeCfgReg(rtd->spiCookie, currentCfg);
if (result != RETURN_OK) {
handleSpiError(rtd, result, "writeCfgReg");
continue;
// Release mutex ASAP
break;
}
}
}
@ -147,6 +168,11 @@ void Max31865RtdReader::periodicReadHandling() {
continue;
}
if (rtdIsActive(rtd->idx)) {
ManualCsLockWrapper mg(csLock, gpioIF, rtd->spiCookie, csTimeoutType, csTimeoutMs);
if (mg.lockResult != RETURN_OK or mg.gpioResult != RETURN_OK) {
sif::error << "Max31865RtdReader::periodicInitHandling: Manual CS lock failed" << std::endl;
break;
}
uint16_t rtdVal = 0;
bool faultBitSet = false;
result = readRtdVal(rtd->spiCookie, rtdVal, faultBitSet);
@ -167,6 +193,11 @@ void Max31865RtdReader::periodicReadHandling() {
// Even if a device was made inactive, turn off the bias here. If it was turned off, not
// necessary anymore..
if (rtd->on) {
ManualCsLockWrapper mg(csLock, gpioIF, rtd->spiCookie, csTimeoutType, csTimeoutMs);
if (mg.lockResult != RETURN_OK or mg.gpioResult != RETURN_OK) {
sif::error << "Max31865RtdReader::periodicInitHandling: Manual CS lock failed" << std::endl;
break;
}
result = writeBiasSel(Bias::OFF, rtd->spiCookie);
}
}
@ -338,7 +369,7 @@ ReturnValue_t Max31865RtdReader::clearFaultStatus(SpiCookie* cookie) {
if (result != RETURN_OK) {
return result;
}
currentCfg |= (1 << CfgBitPos::FAULTY_STATUS_CLEAR);
currentCfg |= (1 << CfgBitPos::FAULT_STATUS_CLEAR);
return writeCfgReg(cookie, currentCfg);
}
@ -446,3 +477,8 @@ ReturnValue_t Max31865RtdReader::handleSpiError(Max31865ReaderCookie* cookie, Re
<< std::endl;
return result;
}
ReturnValue_t Max31865RtdReader::initialize() {
csLock = comIF->getCsMutex();
return SystemObject::initialize();
}

View File

@ -38,9 +38,10 @@ class Max31865RtdReader : public SystemObject,
public ExecutableObjectIF,
public DeviceCommunicationIF {
public:
Max31865RtdReader(object_id_t objectId, SpiComIF* lowLevelComIF);
Max31865RtdReader(object_id_t objectId, SpiComIF* lowLevelComIF, GpioIF* gpioIF);
[[noreturn]] ReturnValue_t performOperation(uint8_t operationCode) override;
ReturnValue_t initialize() override;
private:
std::vector<Max31865ReaderCookie*> rtds;
@ -48,6 +49,12 @@ class Max31865RtdReader : public SystemObject,
size_t dbLen = 0;
MutexIF* readerMutex;
SpiComIF* comIF;
GpioIF* gpioIF;
MutexIF::TimeoutType csTimeoutType = MutexIF::TimeoutType::BLOCKING;
uint32_t csTimeoutMs = 0;
MutexIF* csLock = nullptr;
void rtdMainLoop();
bool periodicInitHandling();
void periodicReadReqHandling();
@ -75,8 +82,6 @@ class Max31865RtdReader : public SystemObject,
ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) override;
ReturnValue_t handleSpiError(Max31865ReaderCookie* cookie, ReturnValue_t result, const char* ctx);
SpiComIF* comIF;
};
#endif /* LINUX_DEVICES_MAX31865RTDREADER_H_ */