eive-obsw/mission/devices/SusHandler.cpp

234 lines
7.6 KiB
C++
Raw Normal View History

2021-05-17 16:53:06 +02:00
#include "SusHandler.h"
2021-05-17 16:37:29 +02:00
#include <fsfw/datapool/PoolReadGuard.h>
2022-02-14 19:41:03 +01:00
#include <fsfw/globalfunctions/arrayprinter.h>
2021-05-07 18:48:42 +02:00
2022-02-15 17:08:57 +01:00
#include "OBSWConfig.h"
2022-02-18 13:10:39 +01:00
SusHandler::SusHandler(object_id_t objectId, uint8_t susIdx, object_id_t comIF, CookieIF *comCookie,
2022-01-17 15:58:27 +01:00
LinuxLibgpioIF *gpioComIF, gpioId_t chipSelectId)
2022-02-18 13:10:39 +01:00
: DeviceHandlerBase(objectId, comIF, comCookie), divider(5), dataset(this), susIdx(susIdx) {}
2021-05-03 11:59:33 +02:00
2022-01-17 15:58:27 +01:00
SusHandler::~SusHandler() {}
2021-05-12 16:38:20 +02:00
2021-05-12 13:06:56 +02:00
ReturnValue_t SusHandler::initialize() {
2022-01-17 15:58:27 +01:00
ReturnValue_t result = RETURN_OK;
result = DeviceHandlerBase::initialize();
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
2021-05-12 13:06:56 +02:00
}
2021-05-03 11:59:33 +02:00
2022-01-17 15:58:27 +01:00
void SusHandler::doStartUp() {
2022-02-14 17:13:06 +01:00
if (comState == ComStates::IDLE) {
comState = ComStates::WRITE_SETUP;
2022-02-14 19:41:03 +01:00
commandExecuted = false;
2022-02-14 17:13:06 +01:00
}
2022-02-14 19:41:03 +01:00
if (comState == ComStates::WRITE_SETUP) {
2022-02-14 17:13:06 +01:00
if (commandExecuted) {
2022-02-22 19:28:04 +01:00
if (goToNormalModeImmediately) {
2022-02-22 19:42:55 +01:00
setMode(MODE_NORMAL);
2022-02-22 19:28:04 +01:00
} else {
setMode(_MODE_TO_ON);
}
2022-02-14 17:13:06 +01:00
commandExecuted = false;
2022-02-15 16:40:48 +01:00
if (clkMode == ClkModes::INT_CLOCKED) {
2022-02-14 19:41:03 +01:00
comState = ComStates::START_INT_CLOCKED_CONVERSIONS;
} else {
comState = ComStates::EXT_CLOCKED_CONVERSIONS;
}
2022-02-14 17:13:06 +01:00
}
}
2021-05-03 11:59:33 +02:00
}
2022-01-17 15:58:27 +01:00
void SusHandler::doShutDown() { setMode(_MODE_POWER_DOWN); }
ReturnValue_t SusHandler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
2022-02-14 17:13:06 +01:00
switch (comState) {
case (ComStates::IDLE): {
2022-02-22 19:36:58 +01:00
break;
2022-02-14 17:13:06 +01:00
}
case (ComStates::WRITE_SETUP): {
*id = SUS::WRITE_SETUP;
return buildCommandFromCommand(*id, nullptr, 0);
}
2022-02-14 19:41:03 +01:00
case (ComStates::EXT_CLOCKED_CONVERSIONS): {
*id = SUS::READ_EXT_TIMED_CONVERSIONS;
2022-02-14 17:13:06 +01:00
return buildCommandFromCommand(*id, nullptr, 0);
}
2022-02-14 19:41:03 +01:00
case (ComStates::START_INT_CLOCKED_CONVERSIONS): {
*id = SUS::START_INT_TIMED_CONVERSIONS;
comState = ComStates::READ_INT_CLOCKED_CONVERSIONS;
return buildCommandFromCommand(*id, nullptr, 0);
}
case (ComStates::READ_INT_CLOCKED_CONVERSIONS): {
*id = SUS::READ_INT_TIMED_CONVERSIONS;
comState = ComStates::START_INT_CLOCKED_CONVERSIONS;
2022-02-14 17:13:06 +01:00
return buildCommandFromCommand(*id, nullptr, 0);
}
2022-02-15 16:40:48 +01:00
case (ComStates::EXT_CLOCKED_TEMP): {
*id = SUS::READ_EXT_TIMED_TEMPS;
2022-02-15 17:08:57 +01:00
return buildCommandFromCommand(*id, nullptr, 0);
2022-02-15 16:40:48 +01:00
}
2022-01-17 15:58:27 +01:00
}
2022-02-14 17:13:06 +01:00
return NOTHING_TO_SEND;
2021-05-03 11:59:33 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t SusHandler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
2022-02-14 17:13:06 +01:00
if (comState == ComStates::WRITE_SETUP) {
*id = SUS::WRITE_SETUP;
return buildCommandFromCommand(*id, nullptr, 0);
}
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-05-12 16:38:20 +02:00
2022-01-17 15:58:27 +01:00
ReturnValue_t SusHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData,
size_t commandDataLen) {
using namespace max1227;
2022-01-17 15:58:27 +01:00
switch (deviceCommand) {
case (SUS::WRITE_SETUP): {
2022-02-15 16:40:48 +01:00
if (clkMode == ClkModes::INT_CLOCKED) {
2022-02-14 19:41:03 +01:00
cmdBuffer[0] = SUS::SETUP_INT_CLOKED;
} else {
cmdBuffer[0] = SUS::SETUP_EXT_CLOCKED;
}
2022-01-17 15:58:27 +01:00
rawPacket = cmdBuffer;
rawPacketLen = 1;
2022-02-15 16:50:16 +01:00
break;
2021-05-03 11:59:33 +02:00
}
2022-02-14 19:41:03 +01:00
case (SUS::START_INT_TIMED_CONVERSIONS): {
2022-01-17 15:58:27 +01:00
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
2022-02-18 12:46:58 +01:00
cmdBuffer[0] = max1227::buildResetByte(true);
cmdBuffer[1] = SUS::CONVERSION;
2022-01-17 15:58:27 +01:00
rawPacket = cmdBuffer;
2022-02-18 12:46:58 +01:00
rawPacketLen = 2;
2022-02-15 16:50:16 +01:00
break;
2021-05-03 11:59:33 +02:00
}
2022-02-14 19:41:03 +01:00
case (SUS::READ_INT_TIMED_CONVERSIONS): {
2022-01-17 15:58:27 +01:00
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
rawPacket = cmdBuffer;
2022-02-14 19:41:03 +01:00
rawPacketLen = SUS::SIZE_READ_INT_CONVERSIONS;
2022-02-15 16:50:16 +01:00
break;
2022-02-14 19:41:03 +01:00
}
case (SUS::READ_EXT_TIMED_CONVERSIONS): {
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
rawPacket = cmdBuffer;
2022-02-16 21:29:37 +01:00
for (uint8_t idx = 0; idx < 6; idx++) {
2022-02-15 20:40:44 +01:00
cmdBuffer[idx * 2] = buildConvByte(ScanModes::N_ONCE, idx, false);
2022-02-14 19:41:03 +01:00
cmdBuffer[idx * 2 + 1] = 0;
}
2022-02-16 21:29:37 +01:00
cmdBuffer[12] = 0x00;
rawPacketLen = SUS::SIZE_READ_EXT_CONVERSIONS;
2022-02-15 16:50:16 +01:00
break;
2021-05-03 11:59:33 +02:00
}
2022-02-15 16:40:48 +01:00
case (SUS::READ_EXT_TIMED_TEMPS): {
cmdBuffer[0] = buildConvByte(ScanModes::N_ONCE, 0, true);
std::memset(cmdBuffer + 1, 0, 24);
rawPacket = cmdBuffer;
rawPacketLen = 25;
break;
}
2022-01-17 15:58:27 +01:00
default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2022-02-15 16:50:16 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-05-03 11:59:33 +02:00
}
void SusHandler::fillCommandAndReplyMap() {
2022-02-15 16:40:48 +01:00
insertInCommandAndReplyMap(SUS::WRITE_SETUP, 1);
insertInCommandAndReplyMap(SUS::START_INT_TIMED_CONVERSIONS, 1);
insertInCommandAndReplyMap(SUS::READ_INT_TIMED_CONVERSIONS, 1, &dataset,
2022-02-15 17:08:57 +01:00
SUS::SIZE_READ_INT_CONVERSIONS);
2022-02-15 16:40:48 +01:00
insertInCommandAndReplyMap(SUS::READ_EXT_TIMED_CONVERSIONS, 1, &dataset,
2022-02-15 17:08:57 +01:00
SUS::SIZE_READ_EXT_CONVERSIONS);
2022-02-15 16:40:48 +01:00
insertInCommandAndReplyMap(SUS::READ_EXT_TIMED_TEMPS, 1);
2021-05-03 11:59:33 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t SusHandler::scanForReply(const uint8_t *start, size_t remainingSize,
DeviceCommandId_t *foundId, size_t *foundLen) {
*foundId = this->getPendingCommand();
*foundLen = remainingSize;
return HasReturnvaluesIF::RETURN_OK;
2021-05-03 11:59:33 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t SusHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) {
switch (id) {
2022-02-14 19:41:03 +01:00
case SUS::WRITE_SETUP: {
if (mode == _MODE_START_UP) {
commandExecuted = true;
}
return HasReturnvaluesIF::RETURN_OK;
}
case SUS::START_INT_TIMED_CONVERSIONS: {
return HasReturnvaluesIF::RETURN_OK;
}
case SUS::READ_INT_TIMED_CONVERSIONS: {
2022-01-17 15:58:27 +01:00
PoolReadGuard readSet(&dataset);
2022-02-16 21:29:37 +01:00
2022-02-22 19:36:58 +01:00
dataset.temperatureCelcius = max1227::getTemperature(((packet[0] & 0x0f) << 8) | packet[1]);
2022-02-22 19:28:04 +01:00
for (uint8_t idx = 0; idx < 6; idx++) {
dataset.channels[idx] = packet[idx * 2 + 2] << 8 | packet[idx * 2 + 3];
}
2022-02-14 19:41:03 +01:00
printDataset();
break;
}
case (SUS::READ_EXT_TIMED_CONVERSIONS): {
PoolReadGuard readSet(&dataset);
2022-02-22 19:28:04 +01:00
for (uint8_t idx = 0; idx < 6; idx++) {
dataset.channels[idx] = packet[idx * 2 + 1] << 8 | packet[idx * 2 + 2];
}
2022-02-15 16:40:48 +01:00
// Read temperature in next read cycle
2022-02-15 17:08:57 +01:00
if (clkMode == ClkModes::EXT_CLOCKED_WITH_TEMP) {
2022-02-15 16:40:48 +01:00
comState = ComStates::EXT_CLOCKED_TEMP;
}
2022-02-14 19:41:03 +01:00
printDataset();
2022-01-17 15:58:27 +01:00
break;
}
2022-02-15 16:40:48 +01:00
case (SUS::READ_EXT_TIMED_TEMPS): {
PoolReadGuard readSet(&dataset);
2022-02-22 19:36:58 +01:00
dataset.temperatureCelcius = max1227::getTemperature(((packet[23] & 0x0f) << 8) | packet[24]);
2022-02-15 16:40:48 +01:00
comState = ComStates::EXT_CLOCKED_CONVERSIONS;
break;
}
2022-01-17 15:58:27 +01:00
default: {
sif::debug << "SusHandler::interpretDeviceReply: Unknown reply id" << std::endl;
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
}
}
return HasReturnvaluesIF::RETURN_OK;
2021-05-03 11:59:33 +02:00
}
2022-02-14 19:41:03 +01:00
uint32_t SusHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 2000; }
2021-05-03 11:59:33 +02:00
2022-01-17 15:58:27 +01:00
ReturnValue_t SusHandler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
LocalDataPoolManager &poolManager) {
2022-02-22 19:28:04 +01:00
localDataPoolMap.emplace(SUS::TEMPERATURE_C, &tempC);
localDataPoolMap.emplace(SUS::CHANNEL_VEC, &channelVec);
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-05-03 11:59:33 +02:00
}
2022-02-11 17:38:02 +01:00
void SusHandler::setToGoToNormalMode(bool enable) { this->goToNormalModeImmediately = enable; }
2022-02-14 19:41:03 +01:00
void SusHandler::printDataset() {
2022-02-21 16:57:16 +01:00
if (periodicPrintout) {
2022-02-19 17:15:38 +01:00
if (divider.checkAndIncrement()) {
2022-02-22 19:43:59 +01:00
sif::info << "SUS ADC " << static_cast<int>(susIdx) << " hex [" << std::setfill('0') << std::hex;
2022-02-22 19:28:04 +01:00
for (uint8_t idx = 0; idx < 6; idx++) {
sif::info << std::setw(3) << dataset.channels[idx];
if (idx < 6 - 1) {
sif::info << ",";
}
}
sif::info << "] | T[C] " << std::dec << dataset.temperatureCelcius.value << std::endl;
2022-02-19 17:15:38 +01:00
}
2022-02-14 19:41:03 +01:00
}
2022-02-19 17:15:38 +01:00
}
void SusHandler::enablePeriodicPrintout(bool enable, uint8_t divider) {
this->periodicPrintout = enable;
this->divider.setDivider(divider);
2022-02-14 19:41:03 +01:00
}