applied clang formatting
Some checks failed
EIVE/eive-obsw/pipeline/head There was a failure building this commit

This commit is contained in:
2022-01-18 11:41:19 +01:00
parent 975b3cd294
commit 5cc7331e90
207 changed files with 28884 additions and 30264 deletions

View File

@ -1,8 +1,5 @@
#include <linux/boardtest/I2cTestClass.h>
I2cTestClass::I2cTestClass(object_id_t objectId): TestTask(objectId) {
}
I2cTestClass::I2cTestClass(object_id_t objectId) : TestTask(objectId) {}
ReturnValue_t I2cTestClass::performPeriodicAction() {
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t I2cTestClass::performPeriodicAction() { return HasReturnvaluesIF::RETURN_OK; }

View File

@ -3,15 +3,13 @@
#include <test/testtasks/TestTask.h>
class I2cTestClass: public TestTask {
public:
I2cTestClass(object_id_t objectId);
class I2cTestClass : public TestTask {
public:
I2cTestClass(object_id_t objectId);
ReturnValue_t performPeriodicAction() override;
private:
ReturnValue_t performPeriodicAction() override;
private:
};
#endif /* LINUX_BOARDTEST_I2CTESTCLASS_H_ */

View File

@ -1,135 +1,127 @@
#include "LibgpiodTest.h"
#include <fsfw/objectmanager/ObjectManager.h>
#include <fsfw/serviceinterface/ServiceInterfaceStream.h>
#include <fsfw/tasks/TaskFactory.h>
#include "devices/gpioIds.h"
#include <fsfw/serviceinterface/ServiceInterfaceStream.h>
#include <fsfw/objectmanager/ObjectManager.h>
#include <fsfw/tasks/TaskFactory.h>
LibgpiodTest::LibgpiodTest(object_id_t objectId, object_id_t gpioIfobjectId,
GpioCookie* gpioCookie):
TestTask(objectId) {
gpioInterface = ObjectManager::instance()->get<GpioIF>(gpioIfobjectId);
if (gpioInterface == nullptr) {
sif::error << "LibgpiodTest::LibgpiodTest: Invalid Gpio interface." << std::endl;
}
gpioInterface->addGpios(gpioCookie);
testCase = TestCases::BLINK;
LibgpiodTest::LibgpiodTest(object_id_t objectId, object_id_t gpioIfobjectId, GpioCookie* gpioCookie)
: TestTask(objectId) {
gpioInterface = ObjectManager::instance()->get<GpioIF>(gpioIfobjectId);
if (gpioInterface == nullptr) {
sif::error << "LibgpiodTest::LibgpiodTest: Invalid Gpio interface." << std::endl;
}
gpioInterface->addGpios(gpioCookie);
testCase = TestCases::BLINK;
}
LibgpiodTest::~LibgpiodTest() {
}
LibgpiodTest::~LibgpiodTest() {}
ReturnValue_t LibgpiodTest::performPeriodicAction() {
int gpioState;
ReturnValue_t result;
int gpioState;
ReturnValue_t result;
switch(testCase) {
case(TestCases::READ): {
result = gpioInterface->readGpio(gpioIds::TEST_ID_0, &gpioState);
if (result != RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Failed to read gpio "
<< std::endl;
return RETURN_FAILED;
}
else {
sif::debug << "LibgpiodTest::performPeriodicAction: MIO 0 state = " << gpioState
<< std::endl;
}
break;
switch (testCase) {
case (TestCases::READ): {
result = gpioInterface->readGpio(gpioIds::TEST_ID_0, &gpioState);
if (result != RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Failed to read gpio " << std::endl;
return RETURN_FAILED;
} else {
sif::debug << "LibgpiodTest::performPeriodicAction: MIO 0 state = " << gpioState
<< std::endl;
}
break;
}
case(TestCases::LOOPBACK): {
break;
case (TestCases::LOOPBACK): {
break;
}
case(TestCases::BLINK): {
result = gpioInterface->readGpio(gpioIds::TEST_ID_0, &gpioState);
case (TestCases::BLINK): {
result = gpioInterface->readGpio(gpioIds::TEST_ID_0, &gpioState);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Failed to read gpio " << std::endl;
return RETURN_FAILED;
}
if (gpioState == 1) {
result = gpioInterface->pullLow(gpioIds::TEST_ID_0);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Failed to read gpio "
<< std::endl;
return RETURN_FAILED;
sif::warning << "LibgpiodTest::performPeriodicAction: Could not pull GPIO low!"
<< std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
if (gpioState == 1) {
result = gpioInterface->pullLow(gpioIds::TEST_ID_0);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Could not pull GPIO low!"
<< std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
}
else if (gpioState == 0) {
result = gpioInterface->pullHigh(gpioIds::TEST_ID_0);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Could not pull GPIO high!"
<< std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
}
else {
sif::warning << "LibgpiodTest::performPeriodicAction: Invalid GPIO state" << std::endl;
} else if (gpioState == 0) {
result = gpioInterface->pullHigh(gpioIds::TEST_ID_0);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "LibgpiodTest::performPeriodicAction: Could not pull GPIO high!"
<< std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
} else {
sif::warning << "LibgpiodTest::performPeriodicAction: Invalid GPIO state" << std::endl;
}
break;
break;
}
default:
sif::debug << "LibgpiodTest::performPeriodicAction: Invalid test case" << std::endl;
break;
}
sif::debug << "LibgpiodTest::performPeriodicAction: Invalid test case" << std::endl;
break;
}
return RETURN_OK;
return RETURN_OK;
}
ReturnValue_t LibgpiodTest::performOneShotAction() {
int gpioState;
ReturnValue_t result;
int gpioState;
ReturnValue_t result;
switch(testCase) {
case(TestCases::READ): {
break;
switch (testCase) {
case (TestCases::READ): {
break;
}
case(TestCases::BLINK): {
break;
case (TestCases::BLINK): {
break;
}
case(TestCases::LOOPBACK): {
result = gpioInterface->pullHigh(gpioIds::TEST_ID_0);
if(result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO pulled high successfully for loopback test" << std::endl;
}
else {
sif::warning << "LibgpiodTest::performOneShotAction: Could not pull GPIO high!"
<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
result = gpioInterface->readGpio(gpioIds::TEST_ID_1, &gpioState);
if(result == HasReturnvaluesIF::RETURN_OK and gpioState == 1) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO state read successfully and is high" << std::endl;
}
else {
sif::warning << "LibgpiodTest::performOneShotAction: GPIO read and is not high!"
<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
case (TestCases::LOOPBACK): {
result = gpioInterface->pullHigh(gpioIds::TEST_ID_0);
if (result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO pulled high successfully for loopback test"
<< std::endl;
} else {
sif::warning << "LibgpiodTest::performOneShotAction: Could not pull GPIO high!"
<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
result = gpioInterface->readGpio(gpioIds::TEST_ID_1, &gpioState);
if (result == HasReturnvaluesIF::RETURN_OK and gpioState == 1) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO state read successfully and is high"
<< std::endl;
} else {
sif::warning << "LibgpiodTest::performOneShotAction: GPIO read and is not high!"
<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
result = gpioInterface->pullLow(gpioIds::TEST_ID_0);
if(result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO pulled low successfully for loopback test" << std::endl;
}
result = gpioInterface->readGpio(gpioIds::TEST_ID_1, &gpioState);
if(result == HasReturnvaluesIF::RETURN_OK and gpioState == 0) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO state read successfully and is low" << std::endl;
}
else {
sif::warning << "LibgpiodTest::performOneShotAction: GPIO read and is not low!"
<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
break;
result = gpioInterface->pullLow(gpioIds::TEST_ID_0);
if (result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO pulled low successfully for loopback test"
<< std::endl;
}
result = gpioInterface->readGpio(gpioIds::TEST_ID_1, &gpioState);
if (result == HasReturnvaluesIF::RETURN_OK and gpioState == 0) {
sif::info << "LibgpiodTest::performOneShotAction: "
"GPIO state read successfully and is low"
<< std::endl;
} else {
sif::warning << "LibgpiodTest::performOneShotAction: GPIO read and is not low!"
<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
break;
}
}
return HasReturnvaluesIF::RETURN_OK;
}
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -1,34 +1,31 @@
#ifndef TEST_TESTTASKS_LIBGPIODTEST_H_
#define TEST_TESTTASKS_LIBGPIODTEST_H_
#include "TestTask.h"
#include <fsfw_hal/common/gpio/GpioIF.h>
#include <fsfw_hal/common/gpio/GpioCookie.h>
#include <fsfw/objectmanager/SystemObject.h>
#include <fsfw_hal/common/gpio/GpioCookie.h>
#include <fsfw_hal/common/gpio/GpioIF.h>
#include "TestTask.h"
/**
* @brief Test for the GPIO read implementation of the LinuxLibgpioIF.
* @author J. Meier
*/
class LibgpiodTest: public TestTask {
public:
enum TestCases {
READ = 0,
LOOPBACK = 1,
BLINK
};
class LibgpiodTest : public TestTask {
public:
enum TestCases { READ = 0, LOOPBACK = 1, BLINK };
TestCases testCase;
TestCases testCase;
LibgpiodTest(object_id_t objectId, object_id_t gpioIfobjectId, GpioCookie* gpioCookie);
virtual ~LibgpiodTest();
LibgpiodTest(object_id_t objectId, object_id_t gpioIfobjectId, GpioCookie* gpioCookie);
virtual ~LibgpiodTest();
protected:
ReturnValue_t performOneShotAction() override;
ReturnValue_t performPeriodicAction() override;
protected:
ReturnValue_t performOneShotAction() override;
ReturnValue_t performPeriodicAction() override;
private:
GpioIF* gpioInterface;
private:
GpioIF* gpioInterface;
};
#endif /* TEST_TESTTASKS_LIBGPIODTEST_H_ */

View File

@ -1,503 +1,483 @@
#include "SpiTestClass.h"
#include <fcntl.h>
#include <fsfw/globalfunctions/arrayprinter.h>
#include <fsfw/serviceinterface/ServiceInterface.h>
#include <fsfw/tasks/TaskFactory.h>
#include <fsfw/timemanager/Stopwatch.h>
#include <fsfw_hal/common/gpio/GpioCookie.h>
#include <fsfw_hal/common/gpio/gpioDefinitions.h>
#include <fsfw_hal/linux/UnixFileGuard.h>
#include <fsfw_hal/linux/utility.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <bitset>
#include "devices/gpioIds.h"
#include <fsfw/serviceinterface/ServiceInterface.h>
#include <fsfw/globalfunctions/arrayprinter.h>
#include <fsfw/tasks/TaskFactory.h>
#include <fsfw/timemanager/Stopwatch.h>
#include <fsfw_hal/linux/utility.h>
#include <fsfw_hal/linux/UnixFileGuard.h>
#include <fsfw_hal/common/gpio/gpioDefinitions.h>
#include <fsfw_hal/common/gpio/GpioCookie.h>
#include <linux/spi/spidev.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <bitset>
SpiTestClass::SpiTestClass(object_id_t objectId, GpioIF* gpioIF): TestTask(objectId),
gpioIF(gpioIF) {
if(gpioIF == nullptr) {
sif::error << "SpiTestClass::SpiTestClass: Invalid GPIO ComIF!" << std::endl;
}
testMode = TestModes::MGM_LIS3MDL;
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(recvBuffer.data());
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(sendBuffer.data());
SpiTestClass::SpiTestClass(object_id_t objectId, GpioIF *gpioIF)
: TestTask(objectId), gpioIF(gpioIF) {
if (gpioIF == nullptr) {
sif::error << "SpiTestClass::SpiTestClass: Invalid GPIO ComIF!" << std::endl;
}
testMode = TestModes::MGM_LIS3MDL;
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(recvBuffer.data());
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(sendBuffer.data());
}
ReturnValue_t SpiTestClass::performOneShotAction() {
switch(testMode) {
case(TestModes::NONE): {
break;
switch (testMode) {
case (TestModes::NONE): {
break;
}
case(TestModes::MGM_LIS3MDL): {
performLis3MdlTest(mgm0Lis3mdlChipSelect);
break;
case (TestModes::MGM_LIS3MDL): {
performLis3MdlTest(mgm0Lis3mdlChipSelect);
break;
}
case(TestModes::MGM_RM3100): {
performRm3100Test(mgm1Rm3100ChipSelect);
break;
case (TestModes::MGM_RM3100): {
performRm3100Test(mgm1Rm3100ChipSelect);
break;
}
case(TestModes::GYRO_L3GD20H): {
performL3gTest(gyro1L3gd20ChipSelect);
break;
case (TestModes::GYRO_L3GD20H): {
performL3gTest(gyro1L3gd20ChipSelect);
break;
}
}
return HasReturnvaluesIF::RETURN_OK;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t SpiTestClass::performPeriodicAction() {
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t SpiTestClass::performPeriodicAction() { return HasReturnvaluesIF::RETURN_OK; }
void SpiTestClass::performRm3100Test(uint8_t mgmId) {
/* Configure all SPI chip selects and pull them high */
acsInit();
/* Configure all SPI chip selects and pull them high */
acsInit();
/* Adapt accordingly */
if(mgmId != mgm1Rm3100ChipSelect and mgmId != mgm3Rm3100ChipSelect) {
sif::warning << "SpiTestClass::performRm3100Test: Invalid MGM ID!" << std::endl;
}
gpioId_t currentGpioId = 0;
uint8_t chipSelectPin = mgmId;
if(chipSelectPin == mgm1Rm3100ChipSelect) {
currentGpioId = gpioIds::MGM_1_RM3100_CS;
}
else {
currentGpioId = gpioIds::MGM_3_RM3100_CS;
}
uint32_t rm3100speed = 976'000;
uint8_t rm3100revidReg = 0x36;
spi::SpiModes rm3100mode = spi::SpiModes::MODE_3;
/* Adapt accordingly */
if (mgmId != mgm1Rm3100ChipSelect and mgmId != mgm3Rm3100ChipSelect) {
sif::warning << "SpiTestClass::performRm3100Test: Invalid MGM ID!" << std::endl;
}
gpioId_t currentGpioId = 0;
uint8_t chipSelectPin = mgmId;
if (chipSelectPin == mgm1Rm3100ChipSelect) {
currentGpioId = gpioIds::MGM_1_RM3100_CS;
} else {
currentGpioId = gpioIds::MGM_3_RM3100_CS;
}
uint32_t rm3100speed = 976'000;
uint8_t rm3100revidReg = 0x36;
spi::SpiModes rm3100mode = spi::SpiModes::MODE_3;
#ifdef RASPBERRY_PI
std::string deviceName = "/dev/spidev0.0";
std::string deviceName = "/dev/spidev0.0";
#else
std::string deviceName = "/dev/spidev2.0";
std::string deviceName = "/dev/spidev2.0";
#endif
int fileDescriptor = 0;
int fileDescriptor = 0;
UnixFileGuard fileHelper(deviceName, &fileDescriptor, O_RDWR, "SpiComIF::initializeInterface");
if (fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performRm3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
setSpiSpeedAndMode(fileDescriptor, rm3100mode, rm3100speed);
UnixFileGuard fileHelper(deviceName, &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface");
if(fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performRm3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
setSpiSpeedAndMode(fileDescriptor, rm3100mode, rm3100speed);
uint8_t revId = readRegister(fileDescriptor, currentGpioId, rm3100revidReg);
sif::info << "SpiTestClass::performRm3100Test: Revision ID 0b" << std::bitset<8>(revId)
<< std::endl;
uint8_t revId = readRegister(fileDescriptor, currentGpioId, rm3100revidReg);
sif::info << "SpiTestClass::performRm3100Test: Revision ID 0b" << std::bitset<8>(revId) <<
std::endl;
/* Write configuration to CMM register */
writeRegister(fileDescriptor, currentGpioId, 0x01, 0x75);
uint8_t cmmRegister = readRm3100Register(fileDescriptor, currentGpioId, 0x01);
sif::info << "SpiTestClass::performRm3100Test: CMM register value: " << std::hex << "0x"
<< static_cast<int>(cmmRegister) << std::dec << std::endl;
/* Write configuration to CMM register */
writeRegister(fileDescriptor, currentGpioId, 0x01, 0x75);
uint8_t cmmRegister = readRm3100Register(fileDescriptor , currentGpioId, 0x01);
sif::info << "SpiTestClass::performRm3100Test: CMM register value: " <<
std::hex << "0x" << static_cast<int>(cmmRegister) << std::dec << std::endl;
/* Read the cycle count registers */
uint8_t cycleCountsRaw[6];
readMultipleRegisters(fileDescriptor, currentGpioId, 0x04, cycleCountsRaw, 6);
/* Read the cycle count registers */
uint8_t cycleCountsRaw[6];
readMultipleRegisters(fileDescriptor, currentGpioId, 0x04, cycleCountsRaw, 6);
uint16_t cycleCountX = cycleCountsRaw[0] << 8 | cycleCountsRaw[1];
uint16_t cycleCountY = cycleCountsRaw[2] << 8 | cycleCountsRaw[3];
uint16_t cycleCountZ = cycleCountsRaw[4] << 8 | cycleCountsRaw[5];
uint16_t cycleCountX = cycleCountsRaw[0] << 8 | cycleCountsRaw[1];
uint16_t cycleCountY = cycleCountsRaw[2] << 8 | cycleCountsRaw[3];
uint16_t cycleCountZ = cycleCountsRaw[4] << 8 | cycleCountsRaw[5];
sif::info << "Cycle count X: " << cycleCountX << std::endl;
sif::info << "Cycle count Y: " << cycleCountY << std::endl;
sif::info << "Cycle count z: " << cycleCountZ << std::endl;
sif::info << "Cycle count X: " << cycleCountX << std::endl;
sif::info << "Cycle count Y: " << cycleCountY << std::endl;
sif::info << "Cycle count z: " << cycleCountZ << std::endl;
writeRegister(fileDescriptor, currentGpioId, 0x0B, 0x96);
uint8_t tmrcReg = readRm3100Register(fileDescriptor, currentGpioId, 0x0B);
sif::info << "SpiTestClass::performRm3100Test: TMRC register value: " <<
std::hex << "0x" << static_cast<int>(tmrcReg) << std::dec << std::endl;
writeRegister(fileDescriptor, currentGpioId, 0x0B, 0x96);
uint8_t tmrcReg = readRm3100Register(fileDescriptor, currentGpioId, 0x0B);
sif::info << "SpiTestClass::performRm3100Test: TMRC register value: " << std::hex << "0x"
<< static_cast<int>(tmrcReg) << std::dec << std::endl;
TaskFactory::delayTask(10);
uint8_t statusReg = readRm3100Register(fileDescriptor, currentGpioId, 0x34);
sif::info << "SpiTestClass::performRm3100Test: Status Register 0b" << std::bitset<8>(statusReg)
<< std::endl;
/* This means that data is not ready */
if ((statusReg & 0b1000'0000) == 0) {
sif::warning << "SpiTestClass::performRm3100Test: Data not ready!" << std::endl;
TaskFactory::delayTask(10);
uint8_t statusReg = readRm3100Register(fileDescriptor, currentGpioId, 0x34);
sif::info << "SpiTestClass::performRm3100Test: Status Register 0b" <<
std::bitset<8>(statusReg) << std::endl;
/* This means that data is not ready */
if((statusReg & 0b1000'0000) == 0) {
sif::warning << "SpiTestClass::performRm3100Test: Data not ready!" << std::endl;
TaskFactory::delayTask(10);
uint8_t statusReg = readRm3100Register(fileDescriptor, currentGpioId, 0x34);
if((statusReg & 0b1000'0000) == 0) {
return;
}
if ((statusReg & 0b1000'0000) == 0) {
return;
}
}
uint32_t rm3100DefaultCycleCout = 0xC8;
/* Gain scales lineary with cycle count and is 38 for cycle count 100 */
float rm3100Gain = rm3100DefaultCycleCout / 100.0 * 38.0;
float scaleFactor = 1 / rm3100Gain;
uint8_t rawValues[9];
readMultipleRegisters(fileDescriptor, currentGpioId, 0x24, rawValues, 9);
uint32_t rm3100DefaultCycleCout = 0xC8;
/* Gain scales lineary with cycle count and is 38 for cycle count 100 */
float rm3100Gain = rm3100DefaultCycleCout / 100.0 * 38.0;
float scaleFactor = 1 / rm3100Gain;
uint8_t rawValues[9];
readMultipleRegisters(fileDescriptor, currentGpioId, 0x24, rawValues, 9);
/* The sensor generates 24 bit signed values */
int32_t rawX = ((rawValues[0] << 24) | (rawValues[1] << 16) | (rawValues[2] << 8)) >> 8;
int32_t rawY = ((rawValues[3] << 24) | (rawValues[4] << 16) | (rawValues[5] << 8)) >> 8;
int32_t rawZ = ((rawValues[6] << 24) | (rawValues[7] << 16) | (rawValues[8] << 8)) >> 8;
/* The sensor generates 24 bit signed values */
int32_t rawX = ((rawValues[0] << 24) | (rawValues[1] << 16) | (rawValues[2] << 8)) >> 8;
int32_t rawY = ((rawValues[3] << 24) | (rawValues[4] << 16) | (rawValues[5] << 8)) >> 8;
int32_t rawZ = ((rawValues[6] << 24) | (rawValues[7] << 16) | (rawValues[8] << 8)) >> 8;
float fieldStrengthX = rawX * scaleFactor;
float fieldStrengthY = rawY * scaleFactor;
float fieldStrengthZ = rawZ * scaleFactor;
float fieldStrengthX = rawX * scaleFactor;
float fieldStrengthY = rawY * scaleFactor;
float fieldStrengthZ = rawZ * scaleFactor;
sif::info << "RM3100 measured field strengths in microtesla:" << std::endl;
sif::info << "Field Strength X: " << fieldStrengthX << " uT" << std::endl;
sif::info << "Field Strength Y: " << fieldStrengthY << " uT" << std::endl;
sif::info << "Field Strength Z: " << fieldStrengthZ << " uT" << std::endl;
sif::info << "RM3100 measured field strengths in microtesla:" << std::endl;
sif::info << "Field Strength X: " << fieldStrengthX << " uT" << std::endl;
sif::info << "Field Strength Y: " << fieldStrengthY << " uT" << std::endl;
sif::info << "Field Strength Z: " << fieldStrengthZ << " uT" << std::endl;
}
void SpiTestClass::performLis3MdlTest(uint8_t lis3Id) {
/* Configure all SPI chip selects and pull them high */
acsInit();
/* Configure all SPI chip selects and pull them high */
acsInit();
/* Adapt accordingly */
if(lis3Id != mgm0Lis3mdlChipSelect and lis3Id != mgm2Lis3mdlChipSelect) {
sif::warning << "SpiTestClass::performLis3MdlTest: Invalid MGM ID!" << std::endl;
}
gpioId_t currentGpioId = 0;
uint8_t chipSelectPin = lis3Id;
uint8_t whoAmIReg = 0b0000'1111;
uint8_t whoAmIRegExpectedVal = 0b0011'1101;
if(chipSelectPin == mgm0Lis3mdlChipSelect) {
currentGpioId = gpioIds::MGM_0_LIS3_CS;
}
else {
currentGpioId = gpioIds::MGM_2_LIS3_CS;
}
uint32_t spiSpeed = 10'000'000;
spi::SpiModes spiMode = spi::SpiModes::MODE_0;
/* Adapt accordingly */
if (lis3Id != mgm0Lis3mdlChipSelect and lis3Id != mgm2Lis3mdlChipSelect) {
sif::warning << "SpiTestClass::performLis3MdlTest: Invalid MGM ID!" << std::endl;
}
gpioId_t currentGpioId = 0;
uint8_t chipSelectPin = lis3Id;
uint8_t whoAmIReg = 0b0000'1111;
uint8_t whoAmIRegExpectedVal = 0b0011'1101;
if (chipSelectPin == mgm0Lis3mdlChipSelect) {
currentGpioId = gpioIds::MGM_0_LIS3_CS;
} else {
currentGpioId = gpioIds::MGM_2_LIS3_CS;
}
uint32_t spiSpeed = 10'000'000;
spi::SpiModes spiMode = spi::SpiModes::MODE_0;
#ifdef RASPBERRY_PI
std::string deviceName = "/dev/spidev0.0";
std::string deviceName = "/dev/spidev0.0";
#else
std::string deviceName = "/dev/spidev2.0";
std::string deviceName = "/dev/spidev2.0";
#endif
int fileDescriptor = 0;
int fileDescriptor = 0;
UnixFileGuard fileHelper(deviceName, &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface");
if(fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performLis3Mdl3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
spiTransferStruct.delay_usecs = 0;
uint8_t whoAmIRegVal = readStmRegister(fileDescriptor, currentGpioId, whoAmIReg, false);
sif::info << "SpiTestClass::performLis3MdlTest: WHO AM I register 0b" <<
std::bitset<8>(whoAmIRegVal) << std::endl;
if(whoAmIRegVal != whoAmIRegExpectedVal) {
sif::warning << "SpiTestClass::performLis3MdlTest: WHO AM I register invalid!"
<< std::endl;
}
UnixFileGuard fileHelper(deviceName, &fileDescriptor, O_RDWR, "SpiComIF::initializeInterface");
if (fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performLis3Mdl3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
spiTransferStruct.delay_usecs = 0;
uint8_t whoAmIRegVal = readStmRegister(fileDescriptor, currentGpioId, whoAmIReg, false);
sif::info << "SpiTestClass::performLis3MdlTest: WHO AM I register 0b"
<< std::bitset<8>(whoAmIRegVal) << std::endl;
if (whoAmIRegVal != whoAmIRegExpectedVal) {
sif::warning << "SpiTestClass::performLis3MdlTest: WHO AM I register invalid!" << std::endl;
}
}
void SpiTestClass::performL3gTest(uint8_t l3gId) {
/* Configure all SPI chip selects and pull them high */
acsInit();
/* Configure all SPI chip selects and pull them high */
acsInit();
gpioId_t currentGpioId = 0;
uint8_t chipSelectPin = l3gId;
uint8_t whoAmIReg = 0b0000'1111;
uint8_t whoAmIRegExpectedVal = 0b1101'0111;
gpioId_t currentGpioId = 0;
uint8_t chipSelectPin = l3gId;
uint8_t whoAmIReg = 0b0000'1111;
uint8_t whoAmIRegExpectedVal = 0b1101'0111;
if(chipSelectPin == gyro1L3gd20ChipSelect) {
currentGpioId = gpioIds::GYRO_1_L3G_CS;
}
else {
currentGpioId = gpioIds::GYRO_3_L3G_CS;
}
uint32_t spiSpeed = 3'900'000;
spi::SpiModes spiMode = spi::SpiModes::MODE_3;
if (chipSelectPin == gyro1L3gd20ChipSelect) {
currentGpioId = gpioIds::GYRO_1_L3G_CS;
} else {
currentGpioId = gpioIds::GYRO_3_L3G_CS;
}
uint32_t spiSpeed = 3'900'000;
spi::SpiModes spiMode = spi::SpiModes::MODE_3;
#ifdef RASPBERRY_PI
std::string deviceName = "/dev/spidev0.0";
std::string deviceName = "/dev/spidev0.0";
#else
std::string deviceName = "/dev/spidev2.0";
std::string deviceName = "/dev/spidev2.0";
#endif
int fileDescriptor = 0;
int fileDescriptor = 0;
UnixFileGuard fileHelper(deviceName, &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface");
if(fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performLis3Mdl3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
uint8_t whoAmIRegVal = readStmRegister(fileDescriptor, currentGpioId, whoAmIReg, false);
sif::info << "SpiTestClass::performLis3MdlTest: WHO AM I register 0b" <<
std::bitset<8>(whoAmIRegVal) << std::endl;
if(whoAmIRegVal != whoAmIRegExpectedVal) {
sif::warning << "SpiTestClass::performL3gTest: Read WHO AM I register invalid!" <<
std::endl;
UnixFileGuard fileHelper(deviceName, &fileDescriptor, O_RDWR, "SpiComIF::initializeInterface");
if (fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performLis3Mdl3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
uint8_t whoAmIRegVal = readStmRegister(fileDescriptor, currentGpioId, whoAmIReg, false);
sif::info << "SpiTestClass::performLis3MdlTest: WHO AM I register 0b"
<< std::bitset<8>(whoAmIRegVal) << std::endl;
if (whoAmIRegVal != whoAmIRegExpectedVal) {
sif::warning << "SpiTestClass::performL3gTest: Read WHO AM I register invalid!" << std::endl;
}
uint8_t ctrlReg1Addr = 0b0010'0000;
{
uint8_t commandRegs[5];
commandRegs[0] = 0b0000'1111;
commandRegs[1] = 0x0;
commandRegs[2] = 0x0;
/* Configure big endian data format */
commandRegs[3] = 0b0100'0000;
commandRegs[4] = 0x0;
writeMultipleStmRegisters(fileDescriptor, currentGpioId, ctrlReg1Addr, commandRegs,
sizeof(commandRegs));
uint8_t readRegs[5];
readMultipleRegisters(fileDescriptor, currentGpioId, ctrlReg1Addr, readRegs, sizeof(readRegs));
for (uint8_t idx = 0; idx < sizeof(readRegs); idx++) {
if (readRegs[idx] != commandRegs[0]) {
sif::warning << "SpiTestClass::performL3gTest: Read control register "
<< static_cast<int>(idx + 1) << " not equal to configured value" << std::endl;
}
}
}
uint8_t ctrlReg1Addr = 0b0010'0000;
{
uint8_t commandRegs[5];
commandRegs[0] = 0b0000'1111;
commandRegs[1] = 0x0;
commandRegs[2] = 0x0;
/* Configure big endian data format */
commandRegs[3] = 0b0100'0000;
commandRegs[4] = 0x0;
writeMultipleStmRegisters(fileDescriptor, currentGpioId, ctrlReg1Addr, commandRegs,
sizeof(commandRegs));
uint8_t readRegs[5];
readMultipleRegisters(fileDescriptor, currentGpioId, ctrlReg1Addr, readRegs,
sizeof(readRegs));
for(uint8_t idx = 0; idx < sizeof(readRegs); idx++) {
if(readRegs[idx] != commandRegs[0]) {
sif::warning << "SpiTestClass::performL3gTest: Read control register " <<
static_cast<int>(idx + 1) << " not equal to configured value" << std::endl;
}
}
}
uint8_t readOutBuffer[14];
readMultipleStmRegisters(fileDescriptor, currentGpioId, ctrlReg1Addr, readOutBuffer,
sizeof(readOutBuffer));
uint8_t readOutBuffer[14];
readMultipleStmRegisters(fileDescriptor, currentGpioId, ctrlReg1Addr, readOutBuffer,
sizeof(readOutBuffer));
uint8_t statusReg = readOutBuffer[7];
sif::info << "SpiTestClass::performL3gTest: Status Register 0b" << std::bitset<8>(statusReg)
<< std::endl;
uint8_t statusReg = readOutBuffer[7];
sif::info << "SpiTestClass::performL3gTest: Status Register 0b" <<
std::bitset<8>(statusReg) << std::endl;
uint16_t l3gRange = 245;
float scaleFactor = static_cast<float>(l3gRange) / INT16_MAX;
/* The sensor spits out little endian */
int16_t angVelocRawX = (readOutBuffer[8] << 8) | readOutBuffer[9];
int16_t angVelocRawY = (readOutBuffer[10] << 8) | readOutBuffer[11];
int16_t angVelocRawZ = (readOutBuffer[12] << 8) | readOutBuffer[13];
uint16_t l3gRange = 245;
float scaleFactor = static_cast<float>(l3gRange) / INT16_MAX;
/* The sensor spits out little endian */
int16_t angVelocRawX = (readOutBuffer[8] << 8) | readOutBuffer[9];
int16_t angVelocRawY = (readOutBuffer[10] << 8) | readOutBuffer[11];
int16_t angVelocRawZ = (readOutBuffer[12] << 8) | readOutBuffer[13];
float angVelocX = scaleFactor * angVelocRawX;
float angVelocY = scaleFactor * angVelocRawY;
float angVelocZ = scaleFactor * angVelocRawZ;
sif::info << "Angular velocities for the L3GD20H in degrees per second:" << std::endl;
sif::info << "X: " << angVelocX << std::endl;
sif::info << "Y: " << angVelocY << std::endl;
sif::info << "Z: " << angVelocZ << std::endl;
float angVelocX = scaleFactor * angVelocRawX;
float angVelocY = scaleFactor * angVelocRawY;
float angVelocZ = scaleFactor * angVelocRawZ;
sif::info << "Angular velocities for the L3GD20H in degrees per second:" << std::endl;
sif::info << "X: " << angVelocX << std::endl;
sif::info << "Y: " << angVelocY << std::endl;
sif::info << "Z: " << angVelocZ << std::endl;
}
void SpiTestClass::acsInit() {
GpioCookie* gpioCookie = new GpioCookie();
GpioCookie *gpioCookie = new GpioCookie();
#ifdef RASPBERRY_PI
GpiodRegularByChip* gpio = nullptr;
std::string rpiGpioName = "gpiochip0";
gpio = new GpiodRegularByChip(rpiGpioName, mgm0Lis3mdlChipSelect, "MGM_0_LIS3",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio);
GpiodRegularByChip *gpio = nullptr;
std::string rpiGpioName = "gpiochip0";
gpio = new GpiodRegularByChip(rpiGpioName, mgm0Lis3mdlChipSelect, "MGM_0_LIS3", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, mgm1Rm3100ChipSelect, "MGM_1_RM3100",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, mgm1Rm3100ChipSelect, "MGM_1_RM3100", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, gyro0AdisChipSelect, "GYRO_0_ADIS",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, gyro0AdisChipSelect, "GYRO_0_ADIS", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, gyro1L3gd20ChipSelect, "GYRO_1_L3G",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, gyro1L3gd20ChipSelect, "GYRO_1_L3G", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, gyro3L3gd20ChipSelect, "GYRO_2_L3G",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_3_L3G_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, gyro3L3gd20ChipSelect, "GYRO_2_L3G", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_3_L3G_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, mgm2Lis3mdlChipSelect, "MGM_2_LIS3",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, mgm2Lis3mdlChipSelect, "MGM_2_LIS3", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, mgm3Rm3100ChipSelect, "MGM_3_RM3100",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio);
gpio = new GpiodRegularByChip(rpiGpioName, mgm3Rm3100ChipSelect, "MGM_3_RM3100", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio);
#elif defined(XIPHOS_Q7S)
GpiodRegularByLineName* gpio = nullptr;
gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_0_CS, "MGM_0_LIS3", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_1_CS, "MGM_1_RM3100", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_2_CS, "MGM_2_LIS3", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_1_CS, "MGM_3_RM3100", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio);
GpiodRegularByLineName *gpio = nullptr;
gpio =
new GpiodRegularByLineName(q7s::gpioNames::MGM_0_CS, "MGM_0_LIS3", gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_1_CS, "MGM_1_RM3100", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio);
gpio =
new GpiodRegularByLineName(q7s::gpioNames::MGM_2_CS, "MGM_2_LIS3", gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_1_CS, "MGM_3_RM3100", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ADIS_CS, "GYRO_0_ADIS",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_1_L3G_CS, "GYRO_1_L3G", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_2_ADIS_CS, "GYRO_2_ADIS",
gpio::DIR_OUT, gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_2_ADIS_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_3_L3G_CS, "GYRO_3_L3G", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_3_L3G_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ADIS_CS, "GYRO_0_ADIS", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_1_L3G_CS, "GYRO_1_L3G", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_2_ADIS_CS, "GYRO_2_ADIS", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_2_ADIS_CS, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_3_L3G_CS, "GYRO_3_L3G", gpio::DIR_OUT,
gpio::HIGH);
gpioCookie->addGpio(gpioIds::GYRO_3_L3G_CS, gpio);
// Enable pins must be pulled low for regular operations
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ENABLE, "GYRO_0_ENABLE", gpio::DIR_OUT,
gpio::LOW);
gpioCookie->addGpio(gpioIds::GYRO_0_ENABLE, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ENABLE, "GYRO_2_ENABLE", gpio::DIR_OUT,
gpio::LOW);
gpioCookie->addGpio(gpioIds::GYRO_2_ENABLE, gpio);
// Enable pins must be pulled low for regular operations
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ENABLE, "GYRO_0_ENABLE", gpio::DIR_OUT,
gpio::LOW);
gpioCookie->addGpio(gpioIds::GYRO_0_ENABLE, gpio);
gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ENABLE, "GYRO_2_ENABLE", gpio::DIR_OUT,
gpio::LOW);
gpioCookie->addGpio(gpioIds::GYRO_2_ENABLE, gpio);
#endif
if (gpioIF != nullptr) {
gpioIF->addGpios(gpioCookie);
}
if (gpioIF != nullptr) {
gpioIF->addGpios(gpioCookie);
}
}
void SpiTestClass::setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed) {
int mode_test = SPI_MODE_3;
int retval = ioctl(spiFd, SPI_IOC_WR_MODE, &mode_test);//reinterpret_cast<uint8_t*>(&mode));
if(retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI mode failed!");
}
int mode_test = SPI_MODE_3;
int retval = ioctl(spiFd, SPI_IOC_WR_MODE, &mode_test); // reinterpret_cast<uint8_t*>(&mode));
if (retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI mode failed!");
}
retval = ioctl(spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if(retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI speed failed!");
}
retval = ioctl(spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI speed failed!");
}
}
void SpiTestClass::writeRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value) {
spiTransferStruct.len = 2;
sendBuffer[0] = reg;
sendBuffer[1] = value;
spiTransferStruct.len = 2;
sendBuffer[0] = reg;
sendBuffer[1] = value;
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if(retval < 0) {
utility::handleIoctlError("SpiTestClass::writeRegister: Write failed");
}
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if (retval < 0) {
utility::handleIoctlError("SpiTestClass::writeRegister: Write failed");
}
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
}
void SpiTestClass::writeStmRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value,
bool autoIncrement) {
if(autoIncrement) {
reg |= STM_AUTO_INCR_MASK;
}
writeRegister(fd, chipSelect, reg, value);
bool autoIncrement) {
if (autoIncrement) {
reg |= STM_AUTO_INCR_MASK;
}
writeRegister(fd, chipSelect, reg, value);
}
void SpiTestClass::writeMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg,
uint8_t *values, size_t len) {
if(values == nullptr) {
return;
}
reg |= STM_AUTO_INCR_MASK;
/* Clear read mask */
reg &= ~STM_READ_MASK;
writeMultipleRegisters(fd, chipSelect, reg, values, len);
uint8_t *values, size_t len) {
if (values == nullptr) {
return;
}
reg |= STM_AUTO_INCR_MASK;
/* Clear read mask */
reg &= ~STM_READ_MASK;
writeMultipleRegisters(fd, chipSelect, reg, values, len);
}
void SpiTestClass::writeMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg,
uint8_t *values, size_t len) {
if(values == nullptr) {
return;
}
void SpiTestClass::writeMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t *values,
size_t len) {
if (values == nullptr) {
return;
}
sendBuffer[0] = reg;
std::memcpy(sendBuffer.data() + 1, values, len);
spiTransferStruct.len = len + 1;
sendBuffer[0] = reg;
std::memcpy(sendBuffer.data() + 1, values, len);
spiTransferStruct.len = len + 1;
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if(retval < 0) {
utility::handleIoctlError("SpiTestClass::readRegister: Read failed");
}
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if (retval < 0) {
utility::handleIoctlError("SpiTestClass::readRegister: Read failed");
}
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
}
uint8_t SpiTestClass::readRm3100Register(int fd, gpioId_t chipSelect, uint8_t reg) {
return readStmRegister(fd, chipSelect, reg, false);
return readStmRegister(fd, chipSelect, reg, false);
}
void SpiTestClass::readMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t *reply,
size_t len) {
reg |= STM_AUTO_INCR_MASK;
readMultipleRegisters(fd, chipSelect, reg, reply, len);
void SpiTestClass::readMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg,
uint8_t *reply, size_t len) {
reg |= STM_AUTO_INCR_MASK;
readMultipleRegisters(fd, chipSelect, reg, reply, len);
}
void SpiTestClass::readMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t *reply,
size_t len) {
if(reply == nullptr) {
return;
}
size_t len) {
if (reply == nullptr) {
return;
}
spiTransferStruct.len = len + 1;
sendBuffer[0] = reg | STM_READ_MASK;
spiTransferStruct.len = len + 1;
sendBuffer[0] = reg | STM_READ_MASK;
for(uint8_t idx = 0; idx < len ; idx ++) {
sendBuffer[idx + 1] = 0;
}
for (uint8_t idx = 0; idx < len; idx++) {
sendBuffer[idx + 1] = 0;
}
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if(retval < 0) {
utility::handleIoctlError("SpiTestClass::readRegister: Read failed");
}
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
std::memcpy(reply, recvBuffer.data() + 1, len);
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if (retval < 0) {
utility::handleIoctlError("SpiTestClass::readRegister: Read failed");
}
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
std::memcpy(reply, recvBuffer.data() + 1, len);
}
uint8_t SpiTestClass::readStmRegister(int fd, gpioId_t chipSelect, uint8_t reg,
bool autoIncrement) {
reg |= STM_READ_MASK;
if(autoIncrement) {
reg |= STM_AUTO_INCR_MASK;
}
return readRegister(fd, chipSelect, reg);
bool autoIncrement) {
reg |= STM_READ_MASK;
if (autoIncrement) {
reg |= STM_AUTO_INCR_MASK;
}
return readRegister(fd, chipSelect, reg);
}
uint8_t SpiTestClass::readRegister(int fd, gpioId_t chipSelect, uint8_t reg) {
spiTransferStruct.len = 2;
sendBuffer[0] = reg;
sendBuffer[1] = 0;
spiTransferStruct.len = 2;
sendBuffer[0] = reg;
sendBuffer[1] = 0;
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if(retval < 0) {
utility::handleIoctlError("SpiTestClass::readRegister: Read failed");
}
if(gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
return recvBuffer[1];
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullLow(chipSelect);
}
int retval = ioctl(fd, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if (retval < 0) {
utility::handleIoctlError("SpiTestClass::readRegister: Read failed");
}
if (gpioIF != nullptr and chipSelect != gpio::NO_GPIO) {
gpioIF->pullHigh(chipSelect);
}
return recvBuffer[1];
}

View File

@ -13,83 +13,78 @@
#include <vector>
class SpiTestClass: public TestTask {
public:
enum TestModes {
NONE,
MGM_LIS3MDL,
MGM_RM3100,
GYRO_L3GD20H,
};
class SpiTestClass : public TestTask {
public:
enum TestModes {
NONE,
MGM_LIS3MDL,
MGM_RM3100,
GYRO_L3GD20H,
};
TestModes testMode;
TestModes testMode;
SpiTestClass(object_id_t objectId, GpioIF* gpioIF);
SpiTestClass(object_id_t objectId, GpioIF* gpioIF);
ReturnValue_t performOneShotAction() override;
ReturnValue_t performPeriodicAction() override;
private:
ReturnValue_t performOneShotAction() override;
ReturnValue_t performPeriodicAction() override;
GpioIF* gpioIF;
private:
GpioIF* gpioIF;
std::array<uint8_t, 128> recvBuffer;
std::array<uint8_t, 128> sendBuffer;
struct spi_ioc_transfer spiTransferStruct = {};
std::array<uint8_t, 128> recvBuffer;
std::array<uint8_t, 128> sendBuffer;
struct spi_ioc_transfer spiTransferStruct = {};
void performRm3100Test(uint8_t mgmId);
void performLis3MdlTest(uint8_t lis3Id);
void performL3gTest(uint8_t l3gId);
void performRm3100Test(uint8_t mgmId);
void performLis3MdlTest(uint8_t lis3Id);
void performL3gTest(uint8_t l3gId);
/* ACS board specific code which pulls all GPIOs high */
void acsInit();
/* ACS board specific code which pulls all GPIOs high */
void acsInit();
/* ACS board specific variables */
/* ACS board specific variables */
#ifdef RASPBERRY_PI
uint8_t mgm0Lis3mdlChipSelect = gpio::MGM_0_BCM_PIN;
uint8_t mgm1Rm3100ChipSelect = gpio::MGM_1_BCM_PIN;
uint8_t mgm2Lis3mdlChipSelect = gpio::MGM_2_BCM_PIN;
uint8_t mgm3Rm3100ChipSelect = gpio::MGM_3_BCM_PIN;
uint8_t mgm0Lis3mdlChipSelect = gpio::MGM_0_BCM_PIN;
uint8_t mgm1Rm3100ChipSelect = gpio::MGM_1_BCM_PIN;
uint8_t mgm2Lis3mdlChipSelect = gpio::MGM_2_BCM_PIN;
uint8_t mgm3Rm3100ChipSelect = gpio::MGM_3_BCM_PIN;
uint8_t gyro0AdisChipSelect = gpio::GYRO_0_BCM_PIN;
uint8_t gyro1L3gd20ChipSelect = gpio::GYRO_1_BCM_PIN;
uint8_t gyro2AdisChipSelect = gpio::GYRO_2_BCM_PIN;
uint8_t gyro3L3gd20ChipSelect = gpio::GYRO_3_BCM_PIN;
uint8_t gyro0AdisChipSelect = gpio::GYRO_0_BCM_PIN;
uint8_t gyro1L3gd20ChipSelect = gpio::GYRO_1_BCM_PIN;
uint8_t gyro2AdisChipSelect = gpio::GYRO_2_BCM_PIN;
uint8_t gyro3L3gd20ChipSelect = gpio::GYRO_3_BCM_PIN;
#else
uint8_t mgm0Lis3mdlChipSelect = 0;
uint8_t mgm1Rm3100ChipSelect = 0;
uint8_t gyro0AdisResetLine = 0;
uint8_t gyro0AdisChipSelect = 0;
uint8_t gyro1L3gd20ChipSelect = 0;
uint8_t gyro2L3gd20ChipSelect = 0;
uint8_t mgm2Lis3mdlChipSelect = 0;
uint8_t mgm3Rm3100ChipSelect = 0;
uint8_t mgm0Lis3mdlChipSelect = 0;
uint8_t mgm1Rm3100ChipSelect = 0;
uint8_t gyro0AdisResetLine = 0;
uint8_t gyro0AdisChipSelect = 0;
uint8_t gyro1L3gd20ChipSelect = 0;
uint8_t gyro2L3gd20ChipSelect = 0;
uint8_t mgm2Lis3mdlChipSelect = 0;
uint8_t mgm3Rm3100ChipSelect = 0;
#endif
static constexpr uint8_t STM_READ_MASK = 0b1000'0000;
static constexpr uint8_t RM3100_READ_MASK = STM_READ_MASK;
static constexpr uint8_t STM_AUTO_INCR_MASK = 0b0100'0000;
static constexpr uint8_t STM_READ_MASK = 0b1000'0000;
static constexpr uint8_t RM3100_READ_MASK = STM_READ_MASK;
static constexpr uint8_t STM_AUTO_INCR_MASK = 0b0100'0000;
void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed);
void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed);
void writeStmRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value,
bool autoIncrement);
void writeMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t* values,
size_t len);
void writeMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t *values,
size_t len);
void writeRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value);
uint8_t readRm3100Register(int fd, gpioId_t chipSelect, uint8_t reg);
uint8_t readStmRegister(int fd, gpioId_t chipSelect, uint8_t reg, bool autoIncrement);
uint8_t readRegister(int fd, gpioId_t chipSelect, uint8_t reg);
void readMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t *reply,
size_t len);
void readMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg,
uint8_t* reply, size_t len);
void writeStmRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value,
bool autoIncrement);
void writeMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t* values,
size_t len);
void writeMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t* values,
size_t len);
void writeRegister(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t value);
uint8_t readRm3100Register(int fd, gpioId_t chipSelect, uint8_t reg);
uint8_t readStmRegister(int fd, gpioId_t chipSelect, uint8_t reg, bool autoIncrement);
uint8_t readRegister(int fd, gpioId_t chipSelect, uint8_t reg);
void readMultipleStmRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t* reply,
size_t len);
void readMultipleRegisters(int fd, gpioId_t chipSelect, uint8_t reg, uint8_t* reply, size_t len);
};
#endif /* LINUX_BOARDTEST_SPITESTCLASS_H_ */

View File

@ -5,110 +5,107 @@
#include "q7sConfig.h"
#endif
#include "fsfw/serviceinterface/ServiceInterface.h"
#include <errno.h> // Error integer and strerror() function
#include <fcntl.h> // Contains file controls like O_RDWR
#include <unistd.h> // write(), read(), close()
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "lwgps/lwgps.h"
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <unistd.h> // write(), read(), close()
#define GPS_REPLY_WIRETAPPING 0
#define GPS_REPLY_WIRETAPPING 0
UartTestClass::UartTestClass(object_id_t objectId): TestTask(objectId) {
}
UartTestClass::UartTestClass(object_id_t objectId) : TestTask(objectId) {}
ReturnValue_t UartTestClass::initialize() {
#if RPI_TEST_GPS_DEVICE == 1
int result = lwgps_init(&gpsData);
if(result == 0) {
sif::warning << "lwgps_init error: " << result << std::endl;
}
int result = lwgps_init(&gpsData);
if (result == 0) {
sif::warning << "lwgps_init error: " << result << std::endl;
}
/* Get file descriptor */
serialPort = open("/dev/serial0", O_RDWR);
if(serialPort < 0) {
sif::warning << "open call failed with error [" << errno << ", " << strerror(errno)
<< std::endl;
}
/* Setting up UART parameters */
tty.c_cflag &= ~PARENB; // Clear parity bit
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
tty.c_cflag &= ~CSIZE; // Clear all the size bits
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
// Use canonical mode for GPS device
tty.c_lflag |= ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
/* Get file descriptor */
serialPort = open("/dev/serial0", O_RDWR);
if (serialPort < 0) {
sif::warning << "open call failed with error [" << errno << ", " << strerror(errno)
<< std::endl;
}
/* Setting up UART parameters */
tty.c_cflag &= ~PARENB; // Clear parity bit
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
tty.c_cflag &= ~CSIZE; // Clear all the size bits
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
// Use canonical mode for GPS device
tty.c_lflag |= ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// Non-blocking mode
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
// Non-blocking mode
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
cfsetispeed(&tty, B9600);
cfsetospeed(&tty, B9600);
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
sif::warning << "tcsetattr call failed with error [" << errno << ", " <<
strerror(errno) << std::endl;;
}
// Flush received and unread data. Those are old NMEA strings which are not relevant anymore
tcflush(serialPort, TCIFLUSH);
cfsetispeed(&tty, B9600);
cfsetospeed(&tty, B9600);
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
sif::warning << "tcsetattr call failed with error [" << errno << ", " << strerror(errno)
<< std::endl;
;
}
// Flush received and unread data. Those are old NMEA strings which are not relevant anymore
tcflush(serialPort, TCIFLUSH);
#endif
return HasReturnvaluesIF::RETURN_OK;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t UartTestClass::performOneShotAction() {
#if RPI_TEST_GPS_DEVICE == 1
#endif
return HasReturnvaluesIF::RETURN_OK;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t UartTestClass::performPeriodicAction() {
#if RPI_TEST_GPS_DEVICE == 1
int bytesRead = 0;
do {
bytesRead = read(serialPort,
reinterpret_cast<void*>(recBuf.data()),
static_cast<unsigned int>(recBuf.size()));
if(bytesRead < 0) {
sif::warning << "UartTestClass::performPeriodicAction: read call failed with error [" <<
errno << ", " << strerror(errno) << "]" << std::endl;
break;
}
else if(bytesRead >= static_cast<int>(recBuf.size())) {
sif::debug << "UartTestClass::performPeriodicAction: "
"recv buffer might not be large enough" << std::endl;
}
else if(bytesRead > 0) {
// pass data to lwgps for processing
int bytesRead = 0;
do {
bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
static_cast<unsigned int>(recBuf.size()));
if (bytesRead < 0) {
sif::warning << "UartTestClass::performPeriodicAction: read call failed with error [" << errno
<< ", " << strerror(errno) << "]" << std::endl;
break;
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
sif::debug << "UartTestClass::performPeriodicAction: "
"recv buffer might not be large enough"
<< std::endl;
} else if (bytesRead > 0) {
// pass data to lwgps for processing
#if GPS_REPLY_WIRETAPPING == 1
sif::info << recBuf.data() << std::endl;
sif::info << recBuf.data() << std::endl;
#endif
int result = lwgps_process(&gpsData, recBuf.data(), bytesRead);
if(result == 0) {
sif::warning << "UartTestClass::performPeriodicAction: lwgps_process error"
<< std::endl;
}
recvCnt++;
if(recvCnt == 6) {
recvCnt = 0;
sif::info << "GPS Data" << std::endl;
// Print messages
printf("Valid status: %d\n", gpsData.is_valid);
printf("Latitude: %f degrees\n", gpsData.latitude);
printf("Longitude: %f degrees\n", gpsData.longitude);
printf("Altitude: %f meters\n", gpsData.altitude);
}
}
} while(bytesRead > 0);
int result = lwgps_process(&gpsData, recBuf.data(), bytesRead);
if (result == 0) {
sif::warning << "UartTestClass::performPeriodicAction: lwgps_process error" << std::endl;
}
recvCnt++;
if (recvCnt == 6) {
recvCnt = 0;
sif::info << "GPS Data" << std::endl;
// Print messages
printf("Valid status: %d\n", gpsData.is_valid);
printf("Latitude: %f degrees\n", gpsData.latitude);
printf("Longitude: %f degrees\n", gpsData.longitude);
printf("Altitude: %f meters\n", gpsData.altitude);
}
}
} while (bytesRead > 0);
#endif
return HasReturnvaluesIF::RETURN_OK;
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -1,27 +1,27 @@
#ifndef LINUX_BOARDTEST_UARTTESTCLASS_H_
#define LINUX_BOARDTEST_UARTTESTCLASS_H_
#include "test/testtasks/TestTask.h"
#include "lwgps/lwgps.h"
#include <termios.h> // Contains POSIX terminal control definitions
#include <array>
#include <termios.h> // Contains POSIX terminal control definitions
class UartTestClass: public TestTask {
public:
UartTestClass(object_id_t objectId);
#include "lwgps/lwgps.h"
#include "test/testtasks/TestTask.h"
ReturnValue_t initialize() override;
ReturnValue_t performOneShotAction() override;
ReturnValue_t performPeriodicAction() override;
private:
class UartTestClass : public TestTask {
public:
UartTestClass(object_id_t objectId);
lwgps_t gpsData = {};
struct termios tty = {};
int serialPort = 0;
std::array<uint8_t, 512> recBuf;
uint8_t recvCnt = 0;
ReturnValue_t initialize() override;
ReturnValue_t performOneShotAction() override;
ReturnValue_t performPeriodicAction() override;
private:
lwgps_t gpsData = {};
struct termios tty = {};
int serialPort = 0;
std::array<uint8_t, 512> recBuf;
uint8_t recvCnt = 0;
};
#endif /* LINUX_BOARDTEST_UARTTESTCLASS_H_ */