diff --git a/linux/boardtest/SpiTestClass.cpp b/linux/boardtest/SpiTestClass.cpp index 537baffb..3804bff9 100644 --- a/linux/boardtest/SpiTestClass.cpp +++ b/linux/boardtest/SpiTestClass.cpp @@ -26,7 +26,7 @@ SpiTestClass::SpiTestClass(object_id_t objectId, GpioIF *gpioIF) } testMode = TestModes::SUS_0; spiTransferStruct[0].rx_buf = reinterpret_cast<__u64>(recvBuffer.data()); - spiTransferStruct[0].tx_buf = reinterpret_cast<__u64>(sendBuffer.data()); + setSendBuffer(); } ReturnValue_t SpiTestClass::performOneShotAction() { @@ -281,11 +281,14 @@ void SpiTestClass::performL3gTest(uint8_t l3gId) { void SpiTestClass::performMax1227Test() { using namespace max1227; - bool testRadSensorExtConv = false; - bool testRadSensorIntConv = false; - bool susExtConversion = false; - bool susIntConversion = false; - bool plPcduAdcExtConv = false; + testRadSensorExtConvWithDelay = false; + testRadSensorIntConv = false; + + susExtConversion = false; + susIntConversion = false; + + plPcduAdcExtConv = false; + plPcduAdcIntConv = true; #ifdef XIPHOS_Q7S std::string deviceName = q7s::SPI_DEFAULT_DEV; #elif defined(RASPBERRY_PI) @@ -302,36 +305,60 @@ void SpiTestClass::performMax1227Test() { spi::SpiModes spiMode = spi::SpiModes::MODE_3; setSpiSpeedAndMode(fd, spiMode, spiSpeed); - if (testRadSensorExtConv) { - sendBuffer[0] = max1227::buildResetByte(true); - sendBuffer[1] = max1227::buildSetupByte(ClkSel::EXT_CONV_EXT_TIMED, RefSel::INT_REF_NO_WAKEUP, - DiffSel::NONE_0); - spiTransferStruct[0].len = 2; - transfer(fd, gpioIds::CS_RAD_SENSOR); + max1227RadSensorTest(fd); + max1227SusTest(fd); + max1227PlPcduTest(fd); +} +void SpiTestClass::max1227RadSensorTest(int fd) { + using namespace max1227; + if (testRadSensorExtConvWithDelay) { + sendBuffer[0] = max1227::buildResetByte(true); + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::CS_RAD_SENSOR); + usleep(200); + sendBuffer[0] = max1227::buildSetupByte(ClkSel::EXT_CONV_EXT_TIMED, RefSel::INT_REF_WITH_WAKEUP, + DiffSel::NONE_0); + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::CS_RAD_SENSOR); max1227::prepareExternallyClockedRead0ToN(sendBuffer.data(), 7, spiTransferStruct[0].len); + size_t tmpLen = spiTransferStruct[0].len; + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::CS_RAD_SENSOR); + std::memcpy(sendBuffer.data(), sendBuffer.data() + 1, tmpLen - 1); + spiTransferStruct[0].len = tmpLen - 1; + usleep(65); transfer(fd, gpioIds::CS_RAD_SENSOR); arrayprinter::print(recvBuffer.data(), 13, OutputType::HEX); uint16_t adcRaw[8] = {}; - adcRaw[0] = (recvBuffer[1] << 8) | recvBuffer[2]; - adcRaw[1] = (recvBuffer[3] << 8) | recvBuffer[4]; - adcRaw[2] = (recvBuffer[5] << 8) | recvBuffer[6]; - adcRaw[3] = (recvBuffer[7] << 8) | recvBuffer[8]; - adcRaw[4] = (recvBuffer[9] << 8) | recvBuffer[10]; - adcRaw[5] = (recvBuffer[11] << 8) | recvBuffer[12]; - adcRaw[6] = (recvBuffer[13] << 8) | recvBuffer[14]; - adcRaw[7] = (recvBuffer[15] << 8) | recvBuffer[16]; + adcRaw[0] = (recvBuffer[0] << 8) | recvBuffer[1]; + adcRaw[1] = (recvBuffer[2] << 8) | recvBuffer[3]; + adcRaw[2] = (recvBuffer[4] << 8) | recvBuffer[5]; + adcRaw[3] = (recvBuffer[6] << 8) | recvBuffer[7]; + adcRaw[4] = (recvBuffer[8] << 8) | recvBuffer[9]; + adcRaw[5] = (recvBuffer[10] << 8) | recvBuffer[11]; + adcRaw[6] = (recvBuffer[12] << 8) | recvBuffer[13]; + adcRaw[7] = (recvBuffer[14] << 8) | recvBuffer[15]; arrayprinter::print(recvBuffer.data(), 17, OutputType::HEX); for (int idx = 0; idx < 8; idx++) { sif::info << "ADC raw " << idx << ": " << adcRaw[idx] << std::endl; } + max1227::prepareExternallyClockedTemperatureRead(sendBuffer.data(), spiTransferStruct[0].len); + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::CS_RAD_SENSOR); + usleep(65); + spiTransferStruct[0].len = 24; + std::memcpy(sendBuffer.data(), sendBuffer.data() + 1, 24); + transfer(fd, gpioIds::CS_RAD_SENSOR); + int16_t tempRaw = ((recvBuffer[22] & 0x0f) << 8) | recvBuffer[23]; + float temp = max1227::getTemperature(tempRaw); + sif::info << "Temperature: " << temp << std::endl; } if (testRadSensorIntConv) { sendBuffer[0] = max1227::buildResetByte(false); spiTransferStruct[0].len = 1; transfer(fd, gpioIds::CS_RAD_SENSOR); - usleep(65); - + usleep(5); // Now use internal conversion sendBuffer[0] = max1227::buildSetupByte(ClkSel::INT_CONV_INT_TIMED_CNVST_AS_AIN, RefSel::INT_REF_NO_WAKEUP, DiffSel::NONE_0); @@ -345,13 +372,13 @@ void SpiTestClass::performMax1227Test() { usleep(65); spiTransferStruct[0].len = 18; // Shift out zeros - spiTransferStruct[0].tx_buf = 0; + shiftOutZeros(); transfer(fd, gpioIds::CS_RAD_SENSOR); + setSendBuffer(); arrayprinter::print(recvBuffer.data(), 14); uint16_t adcRaw[8] = {}; int16_t tempRaw = ((recvBuffer[0] & 0x0f) << 8) | recvBuffer[1]; - sif::info << "Temperature: " << tempRaw * 0.125 << " C" << std::endl; adcRaw[0] = (recvBuffer[2] << 8) | recvBuffer[3]; adcRaw[1] = (recvBuffer[4] << 8) | recvBuffer[5]; @@ -365,6 +392,10 @@ void SpiTestClass::performMax1227Test() { sif::info << "ADC raw " << idx << ": " << adcRaw[idx] << std::endl; } } +} + +void SpiTestClass::max1227SusTest(int fd) { + using namespace max1227; if (susExtConversion) { sendBuffer[0] = max1227::buildResetByte(false); spiTransferStruct[0].len = 1; @@ -408,8 +439,9 @@ void SpiTestClass::performMax1227Test() { usleep(65); spiTransferStruct[0].len = 14; // Shift out zeros - spiTransferStruct[0].tx_buf = 0; + shiftOutZeros(); transfer(fd, gpio::NO_GPIO); + setSendBuffer(); arrayprinter::print(recvBuffer.data(), 14); float temp = static_cast(((recvBuffer[0] & 0x0f) << 8) | recvBuffer[1]) * 0.125; sif::info << "Temperature: " << temp << " C" << std::endl; @@ -424,11 +456,22 @@ void SpiTestClass::performMax1227Test() { sif::info << "ADC raw " << idx << ": " << adcRaw[idx] << std::endl; } } +} + +void SpiTestClass::max1227PlPcduTest(int fd) { + using namespace max1227; if (plPcduAdcExtConv) { // This enables the ADC - gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT0); - gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT1); - sendBuffer[0] = max1227::buildResetByte(true); + ReturnValue_t result = gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT0); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + result = gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT1); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + usleep(1000); + sendBuffer[0] = max1227::buildResetByte(false); spiTransferStruct[0].len = 1; transfer(fd, gpioIds::PLPCDU_ADC_CS); sendBuffer[0] = max1227::buildSetupByte(ClkSel::EXT_CONV_EXT_TIMED, RefSel::INT_REF_NO_WAKEUP, @@ -439,11 +482,56 @@ void SpiTestClass::performMax1227Test() { max1227::prepareExternallyClockedRead0ToN(sendBuffer.data(), n, spiTransferStruct[0].len); transfer(fd, gpioIds::PLPCDU_ADC_CS); uint16_t adcRaw[n + 1] = {}; - for(uint8_t idx = 0; idx < n + 1; idx++) { + for (uint8_t idx = 0; idx < n + 1; idx++) { adcRaw[idx] = (recvBuffer[idx * 2 + 1] << 8) | recvBuffer[idx * 2 + 2]; } arrayprinter::print(recvBuffer.data(), spiTransferStruct[0].len, OutputType::HEX); + sif::info << "PL PCDU ADC values:" << std::endl; for (int idx = 0; idx < n + 1; idx++) { + sif::info << "Raw Value " << idx << ": " << adcRaw[idx] << std::endl; + } + max1227::prepareExternallyClockedTemperatureRead(sendBuffer.data(), spiTransferStruct[0].len); + transfer(fd, gpioIds::PLPCDU_ADC_CS); + int16_t tempRaw = ((recvBuffer[23] & 0x0f) << 8) | recvBuffer[24]; + float temp = max1227::getTemperature(tempRaw); + sif::info << "Temperature: " << temp << std::endl; + } + if (plPcduAdcIntConv) { + // This enables the ADC + ReturnValue_t result = gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT0); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + result = gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT1); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + usleep(1000); + sendBuffer[0] = max1227::buildResetByte(false); + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::PLPCDU_ADC_CS); + // Now use internal conversion + sendBuffer[0] = max1227::buildSetupByte(ClkSel::INT_CONV_INT_TIMED_CNVST_AS_AIN, + RefSel::INT_REF_NO_WAKEUP, DiffSel::NONE_0); + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::PLPCDU_ADC_CS); + usleep(10); + uint8_t n = 11; + sendBuffer[0] = buildConvByte(ScanModes::CHANNELS_0_TO_N, n, true); + spiTransferStruct[0].len = 1; + transfer(fd, gpioIds::PLPCDU_ADC_CS); + + usleep(65); + spiTransferStruct[0].len = 26; + // Shift out zeros + shiftOutZeros(); + transfer(fd, gpioIds::PLPCDU_ADC_CS); + setSendBuffer(); + uint16_t adcRaw[n + 1] = {}; + int16_t tempRaw = ((recvBuffer[0] & 0x0f) << 8) | recvBuffer[1]; + sif::info << "Temperature: " << tempRaw * 0.125 << " C" << std::endl; + for (int idx = 0; idx < n + 1; idx++) { + adcRaw[idx] = (recvBuffer[idx * 2 + 2] << 8) | recvBuffer[idx * 2 + 3]; sif::info << "ADC raw " << idx << ": " << adcRaw[idx] << std::endl; } } @@ -624,6 +712,12 @@ void SpiTestClass::readMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t readMultipleRegisters(fd, chipSelect, reg, reply, len); } +void SpiTestClass::shiftOutZeros() { spiTransferStruct[0].tx_buf = 0; } + +void SpiTestClass::setSendBuffer() { + spiTransferStruct[0].tx_buf = reinterpret_cast<__u64>(sendBuffer.data()); +} + void SpiTestClass::readMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t *reply, size_t len) { if (reply == nullptr) { @@ -680,8 +774,8 @@ uint8_t SpiTestClass::readRegister(int fd, gpioId_t chipSelect, uint8_t reg) { ReturnValue_t SpiTestClass::transfer(int fd, gpioId_t chipSelect = gpio::NO_GPIO) { int retval = 0; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; - if(chipSelect != gpio::NO_GPIO) { - result = gpioIF->pullLow(gpioIds::CS_RAD_SENSOR); + if (chipSelect != gpio::NO_GPIO) { + result = gpioIF->pullLow(chipSelect); if (result != HasReturnvaluesIF::RETURN_OK) { return result; } @@ -693,8 +787,8 @@ ReturnValue_t SpiTestClass::transfer(int fd, gpioId_t chipSelect = gpio::NO_GPIO return HasReturnvaluesIF::RETURN_FAILED; } - if(chipSelect != gpio::NO_GPIO) { - result = gpioIF->pullHigh(gpioIds::CS_RAD_SENSOR); + if (chipSelect != gpio::NO_GPIO) { + result = gpioIF->pullHigh(chipSelect); if (result != HasReturnvaluesIF::RETURN_OK) { return result; } diff --git a/linux/boardtest/SpiTestClass.h b/linux/boardtest/SpiTestClass.h index 3c9c159e..adf94739 100644 --- a/linux/boardtest/SpiTestClass.h +++ b/linux/boardtest/SpiTestClass.h @@ -51,6 +51,15 @@ class SpiTestClass : public TestTask { uint8_t gyro2AdisChipSelect = gpio::GYRO_2_BCM_PIN; uint8_t gyro3L3gd20ChipSelect = gpio::GYRO_3_BCM_PIN; #else + bool testRadSensorExtConvWithDelay = false; + bool testRadSensorIntConv = false; + + bool susExtConversion = false; + bool susIntConversion = false; + + bool plPcduAdcExtConv = false; + bool plPcduAdcIntConv = false; + uint8_t mgm0Lis3mdlChipSelect = 0; uint8_t mgm1Rm3100ChipSelect = 0; uint8_t gyro0AdisResetLine = 0; @@ -65,6 +74,13 @@ class SpiTestClass : public TestTask { static constexpr uint8_t RM3100_READ_MASK = STM_READ_MASK; static constexpr uint8_t STM_AUTO_INCR_MASK = 0b0100'0000; + void shiftOutZeros(); + void setSendBuffer(); + + void max1227RadSensorTest(int fd); + void max1227SusTest(int fd); + void max1227PlPcduTest(int fd); + void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed); void writeStmRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value,