spi com if basic verification complete

This commit is contained in:
Robin Müller 2021-02-24 11:16:33 +01:00 committed by Robin Mueller
parent 961dd7f769
commit 873412f3fb
3 changed files with 35 additions and 90 deletions

View File

@ -1,5 +1,4 @@
#include "SpiComIF.h"
#include "spiDefinitions.h"
#include <linux/utility/Utility.h>
#include <linux/spi/SpiCookie.h>
@ -32,6 +31,7 @@ SpiComIF::SpiComIF(object_id_t objectId, GpioIF* gpioComIF): SystemObject(object
}
ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
int retval = 0;
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
if(spiCookie == nullptr) {
return NULLPOINTER;
@ -90,16 +90,6 @@ ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
return fileHelper.getOpenResult();
}
int retval = ioctl(fileDescriptor, SPI_IOC_WR_MODE, reinterpret_cast<uint8_t*>(&spiMode));
if(retval != 0) {
utility::handleIoctlError("SpiComIF::initializeInterface: Setting SPI mode failed!");
}
retval = ioctl(fileDescriptor, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed);
if(retval != 0) {
utility::handleIoctlError("SpiComIF::initializeInterface: Setting SPI speed failed!");
}
/* These flags are rather uncommon */
if(params.threeWireSpi or params.noCs or params.csHigh) {
uint32_t currentMode = 0;
@ -143,6 +133,8 @@ ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
int retval = 0;
if(spiCookie == nullptr) {
return NULLPOINTER;
}
@ -161,8 +153,8 @@ ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, s
return DeviceCommunicationIF::TOO_MUCH_DATA;
}
spiCookie->assignWriteBuffer(sendData);
spiCookie->assignTransferSize(sendLen);
/* Prepare transfer */
int fileDescriptor = 0;
std::string device = spiCookie->getSpiDevice();
utility::UnixFileHelper fileHelper(device, &fileDescriptor, O_RDWR,
@ -170,17 +162,25 @@ ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, s
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return OPENING_FILE_FAILED;
}
spi::SpiMode spiMode = spi::SpiMode::MODE_0;
uint32_t spiSpeed = 0;
spiCookie->getSpiParameters(spiMode, spiSpeed, nullptr);
setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
spiCookie->assignWriteBuffer(sendData);
spiCookie->assignTransferSize(sendLen);
bool fullDuplex = spiCookie->isFullDuplex();
int retval = 0;
gpioId_t gpioId = spiCookie->getChipSelectPin();
/* GPIO access is mutex protected */
MutexHelper(spiMutex, timeoutType, timeoutMs);
/* Pull SPI CS low. For now, no support for active high given */
if(gpioId != gpio::NO_GPIO) {
/* For now, no support for active high given */
gpioComIF->pullLow(gpioId);
}
/* Execute transfer */
if(fullDuplex) {
/* Initiate a full duplex SPI transfer. */
retval = ioctl(fileDescriptor, SPI_IOC_MESSAGE(1), spiCookie->getTransferStructHandle());
@ -190,7 +190,7 @@ ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, s
}
}
else {
/* We write with a blocking transfer here */
/* We write with a blocking half-duplex transfer here */
if (write(fileDescriptor, sendData, sendLen) != static_cast<ssize_t>(sendLen)) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
@ -294,3 +294,15 @@ ReturnValue_t SpiComIF::getReadBuffer(address_t spiAddress, uint8_t** buffer) {
*buffer = iter->second.replyBuffer.data();
return HasReturnvaluesIF::RETURN_OK;
}
void SpiComIF::setSpiSpeedAndMode(int spiFd, spi::SpiMode mode, uint32_t speed) {
int retval = ioctl(spiFd, SPI_IOC_WR_MODE, 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!");
}
}

View File

@ -4,6 +4,7 @@
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <fsfw/objectmanager/SystemObject.h>
#include <linux/gpio/GpioIF.h>
#include <linux/spi/spiDefinitions.h>
#include <returnvalues/classIds.h>
#include <vector>
@ -54,7 +55,9 @@ private:
SpiDeviceMap spiDeviceMap;
ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer);
void setSpiSpeedAndMode(int spiFd, spi::SpiMode mode, uint32_t speed);
};
#endif /* LINUX_SPI_SPICOMIF_H_ */

View File

@ -40,6 +40,7 @@ void MGMHandlerLIS3MDL::doStartUp() {
/* Set up cached registers which will be used to configure the MGM. */
if(commandExecuted) {
commandExecuted = false;
/* Replace _MODE_TO_ON with MODE_NORMAL to jump to normal mode quickly */
setMode(_MODE_TO_ON);
}
break;
@ -283,7 +284,8 @@ ReturnValue_t MGMHandlerLIS3MDL::interpretDeviceReply(DeviceCommandId_t id,
#if OBSW_VERBOSE_LEVEL >= 1
if(debugDivider->check()) {
/* Set terminal to utf-8 if there is an issue with micro printout. */
sif::info << "MGMHandlerLIS3: Temperature: " << tempValue<< " °C" << std::endl;
sif::info << "MGMHandlerLIS3: Temperature: " << tempValue << " \xC2\xB0" << "C" <<
std::endl;
}
#endif
ReturnValue_t result = dataset.read();
@ -442,77 +444,5 @@ ReturnValue_t MGMHandlerLIS3MDL::initializeLocalDataPool(
return HasReturnvaluesIF::RETURN_OK;
}
#include <devices/gpioIds.h>
#include <linux/spi/spiDefinitions.h>
#include <linux/utility/Utility.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <bitset>
void MGMHandlerLIS3MDL::performOperationHook() {
/* Adapt accordingly */
// if(lis3Id != mgm0Lis3ChipSelect and lis3Id != mgm2Lis3mdlChipSelect) {
// sif::warning << "SpiTestClass::performLis3MdlTest: Invalid MGM ID!" << std::endl;
// }
gpioId_t currentGpioId = gpioIds::MGM_0_LIS3_CS;
uint8_t chipSelectPin = 0;
uint8_t whoAmIReg = 0b0000'1111;
uint8_t reg = whoAmIReg;
uint32_t spiSpeed = 3'900'000;
spi::SpiMode spiMode = spi::SpiMode::MODE_3;
#ifdef RASPBERRY_PI
std::string deviceName = "/dev/spidev0.0";
#else
std::string deviceName = "placeholder";
#endif
int fileDescriptor = 0;
uint8_t recvBuffer [16];
uint8_t sendBuffer [16];
utility::UnixFileHelper fileHelper(deviceName, &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface: ");
if(fileHelper.getOpenResult()) {
sif::error << "SpiTestClass::performLis3Mdl3100Test: File descriptor could not be opened!"
<< std::endl;
return;
}
int retval = ioctl(fileDescriptor, SPI_IOC_WR_MODE, reinterpret_cast<uint8_t*>(&spiMode));
if(retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI mode failed!");
}
retval = ioctl(fileDescriptor, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed);
if(retval != 0) {
utility::handleIoctlError("SpiTestClass::performRm3100Test: Setting SPI speed failed!");
}
GpioIF* gpioIF = objectManager->get<GpioIF>(objects::GPIO_IF);
uint8_t STM_READ_MASK = 0b1000'0000;
struct spi_ioc_transfer spiTransferStruct = {};
reg |= STM_READ_MASK;
spiTransferStruct.len = 2;
sendBuffer[0] = reg;
sendBuffer[1] = 0;
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(recvBuffer);
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(sendBuffer);
//spiTransferStruct.bits_per_word = 8;
//spiTransferStruct.speed_hz = 3'900'000;
if(gpioIF != nullptr and currentGpioId != gpio::NO_GPIO) {
gpioIF->pullLow(currentGpioId);
}
retval = ioctl(fileDescriptor, SPI_IOC_MESSAGE(1), &spiTransferStruct);
if(retval < 0) {
utility::handleIoctlError("SpiTestClass::readStmRegiste: Read failed");
}
if(gpioIF != nullptr and currentGpioId != gpio::NO_GPIO) {
gpioIF->pullHigh(currentGpioId);
}
sif::info << "SpiTestClass::performLis3MdlTest: WHO AM I Regiter 0b" <<
std::bitset<8>(recvBuffer[1]) << std::endl;
}