eive-obsw/linux/boardtest/SpiTestClass.cpp

122 lines
3.8 KiB
C++
Raw Normal View History

2021-02-23 16:47:34 +01:00
#include "SpiTestClass.h"
2021-02-14 19:22:58 +01:00
2021-02-23 16:47:34 +01:00
#include <fsfwconfig/devices/gpioIds.h>
#include <fsfw/serviceinterface/ServiceInterface.h>
#include <linux/spi/spidev.h>
#include <fcntl.h>
#include <linux/gpio/gpioDefinitions.h>
#include <linux/gpio/GpioCookie.h>
2021-02-23 16:58:22 +01:00
#include <linux/utility/Utility.h>
2021-02-23 16:47:34 +01:00
#include <unistd.h>
#include <sys/ioctl.h>
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_RM3100;
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(recvBuffer.data());
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(sendBuffer.data());
}
ReturnValue_t SpiTestClass::performOneShotAction() {
switch(testMode) {
case(TestModes::MGM_LIS3MDL): {
break;
}
case(TestModes::MGM_RM3100): {
performRm3100Test();
break;
}
case(TestModes::GYRO_L3GD20H): {
break;
}
}
return HasReturnvaluesIF::RETURN_OK;
2021-02-14 19:22:58 +01:00
}
ReturnValue_t SpiTestClass::performPeriodicAction() {
return HasReturnvaluesIF::RETURN_OK;
}
2021-02-23 16:47:34 +01:00
void SpiTestClass::performRm3100Test() {
2021-02-23 17:01:25 +01:00
/* Configure all SPI chip selects and pull them high */
acsInit();
2021-02-23 16:47:34 +01:00
/* Adapt accordingly */
uint8_t chipSelectPin = mgm1Rm3100ChipSelect;
2021-02-23 16:58:22 +01:00
uint32_t rm3100Speed = 3'900'000;
2021-02-23 17:01:25 +01:00
uint8_t rm3100whoAmIReg = 0b0000'1111;
2021-02-23 16:58:22 +01:00
int rm3100mode = SPI_MODE_3;
#ifdef RASPBERRY_PI
std::string deviceName = "spidev0.0";
#else
#endif
int fileDescriptor = 0;
utility::UnixFileHelper fileHelper(deviceName, &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface: ");
if(fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performRm3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
2021-02-23 17:01:25 +01:00
int retval = ioctl(fileDescriptor, SPI_IOC_WR_MODE, rm3100mode);
2021-02-23 16:58:22 +01:00
if(retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI mode failed!");
}
2021-02-23 17:01:25 +01:00
retval = ioctl(fileDescriptor, SPI_IOC_WR_MAX_SPEED_HZ, rm3100Speed);
if(retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI speed failed!");
}
2021-02-23 16:47:34 +01:00
}
void SpiTestClass::acsInit() {
GpioCookie* gpioCookie = new GpioCookie();
std::string rpiGpioName = "gpiochip0";
{
GpiodRegular gpio(rpiGpioName.c_str(), mgm0Lis3ChipSelect, "MGM_0_LIS3",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio);
}
{
GpiodRegular gpio(rpiGpioName.c_str(), mgm1Rm3100ChipSelect, "MGM_1_RM3100",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio);
}
{
GpiodRegular gpio(rpiGpioName.c_str(), gyro0AdisChipSelect, "GYRO_0_ADIS",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);
}
{
GpiodRegular gpio(rpiGpioName.c_str(), gyro1L3gd20ChipSelect, "GYRO_1_L3G",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio);
}
{
GpiodRegular gpio(rpiGpioName.c_str(), gyro2L3gd20ChipSelect, "GYRO_2_L3G",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::GYRO_2_L3G_CS, gpio);
}
{
GpiodRegular gpio(rpiGpioName.c_str(), mgm2Lis3mdlChipSelect, "MGM_2_LIS3",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio);
}
{
GpiodRegular gpio(rpiGpioName.c_str(), mgm3Rm3100ChipSelect, "MGM_3_RM3100",
gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio);
}
if(gpioIF != nullptr) {
gpioIF->addGpios(gpioCookie);
}
}