save before implementing SusHandler with chip select

This commit is contained in:
2021-05-06 18:00:58 +02:00
parent 3c6ec68faa
commit 5ce0a63ad6
8 changed files with 246 additions and 140 deletions

View File

@ -1,6 +1,7 @@
#include <fsfw/datapool/PoolReadGuard.h>
#include <mission/devices/SusHandler.h>
#include <OBSWConfig.h>
#include <sys/time.h>
SusHandler::SusHandler(object_id_t objectId, object_id_t comIF,
CookieIF * comCookie) :
@ -31,26 +32,16 @@ void SusHandler::doShutDown(){
ReturnValue_t SusHandler::buildNormalDeviceCommand(
DeviceCommandId_t * id) {
switch (communicationStep) {
case CommunicationStep::START_CONVERSION: {
*id = SUS::START_CONVERSION;
communicationStep = CommunicationStep::READ_CONVERSIONS;
break;
if (communicationStep == CommunicationStep::PERFORM_CONVERSIONS) {
*id = SUS::PERFORM_CONVERSIONS;
// communicationStep = CommunicationStep::READ_TEMP;
communicationStep = CommunicationStep::PERFORM_CONVERSIONS;
}
case CommunicationStep::READ_CONVERSIONS: {
*id = SUS::READ_CONVERSIONS;
// communicationStep = CommunicationStep::START_CONVERSION;
communicationStep = CommunicationStep::READ_CONVERSIONS;
break;
else if (communicationStep == CommunicationStep::READ_TEMP) {
*id = SUS::READ_TEMP;
communicationStep = CommunicationStep::PERFORM_CONVERSIONS;
}
default: {
sif::debug << "SusHandler::buildNormalDeviceCommand: Unknwon communication "
<< "step" << std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
}
return buildCommandFromCommand(*id, nullptr, 0);
return buildCommandFromCommand(*id, nullptr, 0);
}
ReturnValue_t SusHandler::buildTransitionDeviceCommand(
@ -76,24 +67,44 @@ ReturnValue_t SusHandler::buildCommandFromCommand(
internalState = InternalState::CONFIGURED;
return RETURN_OK;
}
case(SUS::START_CONVERSION): {
cmdBuffer[0] = SUS::CONVERSION_DEFINITION;
case(SUS::PERFORM_CONVERSIONS): {
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
/**
* The sun sensor ADC is shutdown when CS is pulled high so each time requesting a
* measurement the setup has to be rewritten
*/
cmdBuffer[0] = SUS::SETUP_DEFINITION;
cmdBuffer[1] = SUS::UNIPOLAR_CONFIG;
// wirte one dummy byte here
// cmdBuffer[2] = SUS::CONVERT_TEMPERATURE;
cmdBuffer[2] = SUS::CONVERT_TEMPERATURE;
// struct timeval startOfDelay;
// gettimeofday(&startOfDelay, NULL);
// struct timeval currentTime;
// gettimeofday(&currentTime, NULL);
// while (currentTime.tv_usec - startOfDelay.tv_usec < 1000) {
// gettimeofday(&currentTime, NULL);
// }
// cmdBuffer[27] = SUS::CONVERT_DIFF_CHANNEL_0_1;
// cmdBuffer[29] = SUS::CONVERT_DIFF_CHANNEL_2_3;
// cmdBuffer[31] = SUS::CONVERT_DIFF_CHANNEL_4_5;
// cmdBuffer[0] = SUS::SETUP_DEFINITION;
// cmdBuffer[1] = SUS::UNIPOLAR_CONFIG;
// cmdBuffer[2] = SUS::CONVERT_TEMPERATURE;
// cmdBuffer[26] = SUS::CONVERT_DIFF_CHANNEL_0_1;
// cmdBuffer[28] = SUS::CONVERT_DIFF_CHANNEL_2_3;
// cmdBuffer[30] = SUS::CONVERT_DIFF_CHANNEL_4_5;
rawPacket = cmdBuffer;
rawPacketLen = 1;
// rawPacketLen = SUS::SIZE_PERFORM_CONVERSIONS;
rawPacketLen = 7;
return RETURN_OK;
}
case(SUS::READ_CONVERSIONS): {
cmdBuffer[0] = SUS::DUMMY_BYTE;
cmdBuffer[1] = SUS::DUMMY_BYTE;
cmdBuffer[2] = SUS::DUMMY_BYTE;
cmdBuffer[3] = SUS::DUMMY_BYTE;
cmdBuffer[4] = SUS::DUMMY_BYTE;
cmdBuffer[5] = SUS::DUMMY_BYTE;
cmdBuffer[6] = SUS::DUMMY_BYTE;
cmdBuffer[7] = SUS::DUMMY_BYTE;
cmdBuffer[8] = SUS::DUMMY_BYTE;
rawPacket = cmdBuffer;
rawPacketLen = SUS::READ_SIZE;
case(SUS::READ_TEMP): {
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
rawPacket = cmdBuffer;
rawPacketLen = 26;
return RETURN_OK;
}
default:
@ -104,9 +115,10 @@ ReturnValue_t SusHandler::buildCommandFromCommand(
void SusHandler::fillCommandAndReplyMap() {
this->insertInCommandMap(SUS::WRITE_SETUP);
this->insertInCommandMap(SUS::START_CONVERSION);
this->insertInCommandAndReplyMap(SUS::READ_CONVERSIONS, 1, &dataset,
SUS::READ_SIZE);
this->insertInCommandAndReplyMap(SUS::READ_TEMP, 1, &dataset, SUS::SIZE_PERFORM_CONVERSIONS);
// this->insertInCommandAndReplyMap(SUS::PERFORM_CONVERSIONS, 1, &dataset,
// SUS::SIZE_PERFORM_CONVERSIONS);
this->insertInCommandMap(SUS::PERFORM_CONVERSIONS);
}
ReturnValue_t SusHandler::scanForReply(const uint8_t *start,
@ -119,12 +131,12 @@ ReturnValue_t SusHandler::scanForReply(const uint8_t *start,
ReturnValue_t SusHandler::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) {
switch (id) {
case SUS::READ_CONVERSIONS: {
case SUS::PERFORM_CONVERSIONS: {
PoolReadGuard readSet(&dataset);
dataset.temperatureCelcius = (*(packet) << 8 | *(packet + 1)) * 0.125;
dataset.diffScanChannel0_1 = (*(packet + 2) << 8 | *(packet + 3));
dataset.diffScanChannel2_3 = (*(packet + 2) << 8 | *(packet + 3));
dataset.diffScanChannel4_5 = (*(packet + 2) << 8 | *(packet + 3));
dataset.temperatureCelcius = (*(packet + 25) << 8 | *(packet + 26)) * 0.125;
dataset.diffScanChannel0_1 = (*(packet + 29) << 8 | *(packet + 30));
dataset.diffScanChannel2_3 = (*(packet + 31) << 8 | *(packet + 32));
dataset.diffScanChannel4_5 = (*(packet + 33) << 8 | *(packet + 34));
#if OBSW_VERBOSE_LEVEL >= 1 && DEBUG_SUS
sif::info << "SUS with object id " << std::hex << this->getObjectId() << ", temperature: "
<< dataset.temperatureCelcius << " °C" << std::endl;

View File

@ -40,8 +40,8 @@ protected:
private:
enum class CommunicationStep {
START_CONVERSION,
READ_CONVERSIONS
PERFORM_CONVERSIONS,
READ_TEMP
};
enum class InternalState {
@ -51,11 +51,9 @@ private:
SUS::SusDataset dataset;
static const uint8_t MAX_CMD_LEN = SUS::READ_SIZE;
uint8_t cmdBuffer[MAX_CMD_LEN];
uint8_t cmdBuffer[SUS::MAX_CMD_SIZE];
InternalState internalState = InternalState::SETUP;
CommunicationStep communicationStep = CommunicationStep::START_CONVERSION;
CommunicationStep communicationStep = CommunicationStep::PERFORM_CONVERSIONS;
};
#endif /* MISSION_DEVICES_SUSHANDLER_H_ */

View File

@ -10,8 +10,8 @@ namespace SUS {
* temperature sensor.
*/
static const DeviceCommandId_t WRITE_SETUP = 0x1;
static const DeviceCommandId_t START_CONVERSION = 0x2;
static const DeviceCommandId_t READ_CONVERSIONS = 0x3;
static const DeviceCommandId_t PERFORM_CONVERSIONS = 0x2;
static const DeviceCommandId_t READ_TEMP = 0x3;
/**
* @brief This is the configuration byte which will be written to the setup register after
@ -19,12 +19,15 @@ namespace SUS {
*
* @note Bit1 (DIFFSEL1) - Bit0 (DIFFSEL0): 0b10, following byte will be written to the
* unipolar register to perform differential conversion
* Bit3 (REFSEL1) - Bit2 (REFSEL0): 0b11, external reference differential (AIN6 is REF-)
* Bit5 (CLKSEL1) - Bit4 (CLKSEL0): 0b10, MAX1227 uses internal oscillator for timing
* Bit3 (REFSEL1) - Bit2 (REFSEL0): 0b10, internal reference differential (AIN6 is REF-)
* Bit5 (CLKSEL1) - Bit4 (CLKSEL0): 0b11, MAX1227 clocked through SPI SCLK
* Bit7 - Bit6: 0b01, tells MAX1227 that this is the setup register
*
*/
static const uint8_t SETUP_DEFINITION = 0b01101110;
// static const uint8_t SETUP_DEFINITION = 0b0111110;
// Internal reference 0b10
static const uint8_t SETUP_DEFINITION = 0b0111010;
/**
* @brief This byte will be written to the unipolar register
@ -38,21 +41,24 @@ namespace SUS {
* @brief This value will always be written to the ADC conversion register to specify the
* conversions to perform.
* @details Bit0: 1 - Enables temperature conversion
* Bit2 (SCAN1) and Bit1 (SCAN0): 0b00 (channel conversion from 0 to N)
* Bit2 (SCAN1) and Bit1 (SCAN0): 0b11, No scan, converts channel N once only.
* Scanning is not supported in spi clock mode.
* Bit6 - Bit3 defines N: 0b0001 (N = 4)
* Bit7: Always 1. Tells the ADC that this is the conversion register.
*/
static const uint8_t CONVERSION_DEFINITION = 0b10100001;
// static const uint8_t CONVERT_TEMPERATURE = 0b10000111;
static const uint8_t CONVERT_TEMPERATURE = 0b10000110;
static const uint8_t CONVERT_DIFF_CHANNEL_0_1 = 0b10000110;
static const uint8_t CONVERT_DIFF_CHANNEL_2_3 = 0b10010110;
static const uint8_t CONVERT_DIFF_CHANNEL_4_5 = 0b10100110;
static const uint8_t DUMMY_BYTE = 0xFF;
static const uint8_t DUMMY_BYTE = 0x0;
static const uint8_t SUS_DATA_SET_ID = READ_CONVERSIONS;
static const uint8_t SUS_DATA_SET_ID = PERFORM_CONVERSIONS;
/**
* One temperature value, one differential conversion for channels 0/1, one for channels 2/3 and
* one for channels 3/4
*/
static const uint8_t READ_SIZE = 8;
static const uint8_t SIZE_PERFORM_CONVERSIONS = 34;
static const uint8_t MAX_CMD_SIZE = SIZE_PERFORM_CONVERSIONS;
enum Max1227PoolIds: lp_id_t {
TEMPERATURE_C,