added some testcode, zero init spi struct

This commit is contained in:
Robin Müller 2021-02-24 10:36:23 +01:00
parent 6b982ce5cd
commit 2cc22b2233
6 changed files with 85 additions and 29 deletions

View File

@ -3,8 +3,8 @@
#include <cstdint> #include <cstdint>
#define RPI_ADD_GPIO_TEST 1 #define RPI_ADD_GPIO_TEST 0
#define RPI_LOOPBACK_TEST_GPIO 1 #define RPI_LOOPBACK_TEST_GPIO 0
/* Only one of those 2 should be enabled! */ /* Only one of those 2 should be enabled! */
#define RPI_ADD_SPI_TEST 0 #define RPI_ADD_SPI_TEST 0

View File

@ -6,7 +6,7 @@
#include <iostream> #include <iostream>
/** /**
* @brief This is the main program for the target hardware. * @brief This is the main program and entry point for the Raspberry Pi.
* @return * @return
*/ */
int main(void) int main(void)
@ -20,7 +20,7 @@ int main(void)
initmission::initMission(); initmission::initMission();
for(;;) { for(;;) {
/* suspend main thread by sleeping it. */ /* Suspend main thread by sleeping it. */
TaskFactory::delayTask(5000); TaskFactory::delayTask(5000);
} }
} }

View File

@ -135,37 +135,37 @@ void SpiTestClass::acsInit() {
GpioCookie* gpioCookie = new GpioCookie(); GpioCookie* gpioCookie = new GpioCookie();
std::string rpiGpioName = "gpiochip0"; std::string rpiGpioName = "gpiochip0";
{ {
GpiodRegular gpio(rpiGpioName.c_str(), mgm0Lis3ChipSelect, "MGM_0_LIS3", GpiodRegular gpio(rpiGpioName, mgm0Lis3ChipSelect, "MGM_0_LIS3",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio); gpioCookie->addGpio(gpioIds::MGM_0_LIS3_CS, gpio);
} }
{ {
GpiodRegular gpio(rpiGpioName.c_str(), mgm1Rm3100ChipSelect, "MGM_1_RM3100", GpiodRegular gpio(rpiGpioName, mgm1Rm3100ChipSelect, "MGM_1_RM3100",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio); gpioCookie->addGpio(gpioIds::MGM_1_RM3100_CS, gpio);
} }
{ {
GpiodRegular gpio(rpiGpioName.c_str(), gyro0AdisChipSelect, "GYRO_0_ADIS", GpiodRegular gpio(rpiGpioName, gyro0AdisChipSelect, "GYRO_0_ADIS",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio); gpioCookie->addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);
} }
{ {
GpiodRegular gpio(rpiGpioName.c_str(), gyro1L3gd20ChipSelect, "GYRO_1_L3G", GpiodRegular gpio(rpiGpioName, gyro1L3gd20ChipSelect, "GYRO_1_L3G",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio); gpioCookie->addGpio(gpioIds::GYRO_1_L3G_CS, gpio);
} }
{ {
GpiodRegular gpio(rpiGpioName.c_str(), gyro2L3gd20ChipSelect, "GYRO_2_L3G", GpiodRegular gpio(rpiGpioName, gyro2L3gd20ChipSelect, "GYRO_2_L3G",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::GYRO_2_L3G_CS, gpio); gpioCookie->addGpio(gpioIds::GYRO_2_L3G_CS, gpio);
} }
{ {
GpiodRegular gpio(rpiGpioName.c_str(), mgm2Lis3mdlChipSelect, "MGM_2_LIS3", GpiodRegular gpio(rpiGpioName, mgm2Lis3mdlChipSelect, "MGM_2_LIS3",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio); gpioCookie->addGpio(gpioIds::MGM_2_LIS3_CS, gpio);
} }
{ {
GpiodRegular gpio(rpiGpioName.c_str(), mgm3Rm3100ChipSelect, "MGM_3_RM3100", GpiodRegular gpio(rpiGpioName, mgm3Rm3100ChipSelect, "MGM_3_RM3100",
gpio::Direction::OUT, 1); gpio::Direction::OUT, 1);
gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio); gpioCookie->addGpio(gpioIds::MGM_3_RM3100_CS, gpio);
} }

View File

@ -71,6 +71,7 @@ ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
/* Pull CS high in any case to be sure that device is inactive */
gpioId_t gpioId = spiCookie->getChipSelectPin(); gpioId_t gpioId = spiCookie->getChipSelectPin();
if(gpioId != gpio::NO_GPIO) { if(gpioId != gpio::NO_GPIO) {
gpioComIF->pullHigh(gpioId); gpioComIF->pullHigh(gpioId);
@ -171,23 +172,6 @@ ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, s
} }
bool fullDuplex = spiCookie->isFullDuplex(); bool fullDuplex = spiCookie->isFullDuplex();
if(fullDuplex) {
uint8_t* readBuf = nullptr;
ReturnValue_t result = getReadBuffer(spiCookie->getSpiAddress(), &readBuf);
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Could not get read buffer!" <<
std::endl;
#else
sif::printWarning("SpiComIF::sendMessage: Could not get read buffer!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
return result;
}
spiCookie->assignReadBuffer(readBuf);
}
int retval = 0; int retval = 0;
gpioId_t gpioId = spiCookie->getChipSelectPin(); gpioId_t gpioId = spiCookie->getChipSelectPin();

View File

@ -104,7 +104,7 @@ private:
uint32_t spiSpeed; uint32_t spiSpeed;
bool halfDuplex = false; bool halfDuplex = false;
struct spi_ioc_transfer spiTransferStruct; struct spi_ioc_transfer spiTransferStruct = {};
UncommonParameters uncommonParameters; UncommonParameters uncommonParameters;
}; };

View File

@ -442,5 +442,77 @@ ReturnValue_t MGMHandlerLIS3MDL::initializeLocalDataPool(
return HasReturnvaluesIF::RETURN_OK; 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() { 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;
} }