added spi code for linux
This commit is contained in:
parent
28004f866a
commit
8f3b0eccdb
@ -8,6 +8,8 @@ option(FSFW_HAL_ADD_RASPBERRY_PI "Add Raspberry Pi specific code to the sources"
|
|||||||
|
|
||||||
option(FSFW_HAL_ADD_STM32H7 "Add the STM32H7 HAL to the sources" OFF)
|
option(FSFW_HAL_ADD_STM32H7 "Add the STM32H7 HAL to the sources" OFF)
|
||||||
|
|
||||||
|
option(FSFW_HAL_WARNING_SHADOW_LOCAL_GCC "Enable -Wshadow=local warning in GCC" ON)
|
||||||
|
|
||||||
set(LIB_FSFW_HAL_NAME fsfw_hal)
|
set(LIB_FSFW_HAL_NAME fsfw_hal)
|
||||||
set(LINUX_HAL_PATH_NAME linux)
|
set(LINUX_HAL_PATH_NAME linux)
|
||||||
set(STM32H7_PATH_NAME stm32h7)
|
set(STM32H7_PATH_NAME stm32h7)
|
||||||
@ -32,3 +34,28 @@ endif()
|
|||||||
target_link_libraries(${LIB_FSFW_HAL_NAME} PRIVATE
|
target_link_libraries(${LIB_FSFW_HAL_NAME} PRIVATE
|
||||||
${LIB_FSFW_NAME}
|
${LIB_FSFW_NAME}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||||
|
if(NOT DEFINED FSFW_WARNING_FLAGS)
|
||||||
|
set(FSFW_WARNING_FLAGS
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wimplicit-fallthrough=1
|
||||||
|
-Wno-unused-parameter
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_compile_options(${LIB_FSFW_NAME} PRIVATE
|
||||||
|
"-ffunction-sections"
|
||||||
|
"-fdata-sections"
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_options(${LIB_FSFW_NAME} PRIVATE
|
||||||
|
"Wl,--gc-sections"
|
||||||
|
)
|
||||||
|
|
||||||
|
if(FSFW_HAL_WARNING_SHADOW_LOCAL_GCC)
|
||||||
|
list(APPEND WARNING_FLAGS "-Wshadow=local")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
endif()
|
||||||
|
@ -2,4 +2,10 @@ if(FSFW_HAL_ADD_RASPBERRY_PI)
|
|||||||
add_subdirectory(rpi)
|
add_subdirectory(rpi)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
target_sources(${LIB_FSFW_HAL_NAME} PRIVATE
|
||||||
|
UnixFileGuard.cpp
|
||||||
|
utility.cpp
|
||||||
|
)
|
||||||
|
|
||||||
add_subdirectory(gpio)
|
add_subdirectory(gpio)
|
||||||
|
add_subdirectory(spi)
|
||||||
|
33
linux/UnixFileGuard.cpp
Normal file
33
linux/UnixFileGuard.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "UnixFileGuard.h"
|
||||||
|
|
||||||
|
UnixFileGuard::UnixFileGuard(std::string device, int* fileDescriptor, int flags,
|
||||||
|
std::string diagnosticPrefix):
|
||||||
|
fileDescriptor(fileDescriptor) {
|
||||||
|
if(fileDescriptor == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*fileDescriptor = open(device.c_str(), flags);
|
||||||
|
if (*fileDescriptor < 0) {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::warning << diagnosticPrefix <<"Opening device failed with error code " << errno <<
|
||||||
|
"." << std::endl;
|
||||||
|
sif::warning << "Error description: " << strerror(errno) << std::endl;
|
||||||
|
#else
|
||||||
|
sif::printError("%sOpening device failed with error code %d.\n", diagnosticPrefix);
|
||||||
|
sif::printWarning("Error description: %s\n", strerror(errno));
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
openStatus = OPEN_FILE_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UnixFileGuard::~UnixFileGuard() {
|
||||||
|
if(fileDescriptor != nullptr) {
|
||||||
|
close(*fileDescriptor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t UnixFileGuard::getOpenResult() const {
|
||||||
|
return openStatus;
|
||||||
|
}
|
33
linux/UnixFileGuard.h
Normal file
33
linux/UnixFileGuard.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef LINUX_UTILITY_UNIXFILEGUARD_H_
|
||||||
|
#define LINUX_UTILITY_UNIXFILEGUARD_H_
|
||||||
|
|
||||||
|
#include <fsfw/returnvalues/HasReturnvaluesIF.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
class UnixFileGuard {
|
||||||
|
public:
|
||||||
|
static constexpr int READ_WRITE_FLAG = O_RDWR;
|
||||||
|
static constexpr int READ_ONLY_FLAG = O_RDONLY;
|
||||||
|
static constexpr int NON_BLOCKING_IO_FLAG = O_NONBLOCK;
|
||||||
|
|
||||||
|
static constexpr ReturnValue_t OPEN_FILE_FAILED = 1;
|
||||||
|
|
||||||
|
UnixFileGuard(std::string device, int* fileDescriptor, int flags,
|
||||||
|
std::string diagnosticPrefix = "");
|
||||||
|
|
||||||
|
virtual~ UnixFileGuard();
|
||||||
|
|
||||||
|
ReturnValue_t getOpenResult() const;
|
||||||
|
private:
|
||||||
|
int* fileDescriptor = nullptr;
|
||||||
|
ReturnValue_t openStatus = HasReturnvaluesIF::RETURN_OK;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LINUX_UTILITY_UNIXFILEGUARD_H_ */
|
8
linux/spi/CMakeLists.txt
Normal file
8
linux/spi/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
target_sources(${TARGET_NAME} PUBLIC
|
||||||
|
SpiComIF.cpp
|
||||||
|
SpiCookie.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
321
linux/spi/SpiComIF.cpp
Normal file
321
linux/spi/SpiComIF.cpp
Normal file
@ -0,0 +1,321 @@
|
|||||||
|
#include "SpiComIF.h"
|
||||||
|
#include "SpiCookie.h"
|
||||||
|
#include "../utility.h"
|
||||||
|
#include "../UnixFileGuard.h"
|
||||||
|
#include <FSFWConfig.h>
|
||||||
|
|
||||||
|
#include <fsfw/ipc/MutexFactory.h>
|
||||||
|
#include <fsfw/ipc/MutexGuard.h>
|
||||||
|
#include <fsfw/globalfunctions/arrayprinter.h>
|
||||||
|
|
||||||
|
#include <linux/spi/spidev.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
SpiComIF::SpiComIF(object_id_t objectId, GpioIF* gpioComIF): SystemObject(objectId),
|
||||||
|
gpioComIF(gpioComIF) {
|
||||||
|
if(gpioComIF == nullptr) {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::error << "SpiComIF::SpiComIF: GPIO communication interface invalid!" << std::endl;
|
||||||
|
#else
|
||||||
|
sif::printError("SpiComIF::SpiComIF: GPIO communication interface invalid!\n");
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
}
|
||||||
|
|
||||||
|
spiMutex = MutexFactory::instance()->createMutex();
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
|
||||||
|
int retval = 0;
|
||||||
|
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
|
||||||
|
if(spiCookie == nullptr) {
|
||||||
|
return NULLPOINTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
address_t spiAddress = spiCookie->getSpiAddress();
|
||||||
|
|
||||||
|
auto iter = spiDeviceMap.find(spiAddress);
|
||||||
|
if(iter == spiDeviceMap.end()) {
|
||||||
|
size_t bufferSize = spiCookie->getMaxBufferSize();
|
||||||
|
SpiInstance spiInstance = {std::vector<uint8_t>(bufferSize)};
|
||||||
|
auto statusPair = spiDeviceMap.emplace(spiAddress, spiInstance);
|
||||||
|
if (not statusPair.second) {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::error << "SpiComIF::initializeInterface: Failed to insert device with address " <<
|
||||||
|
spiAddress << "to SPI device map" << std::endl;
|
||||||
|
#else
|
||||||
|
sif::printError("SpiComIF::initializeInterface: Failed to insert device with address "
|
||||||
|
"%lu to SPI device map\n", static_cast<unsigned long>(spiAddress));
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
|
}
|
||||||
|
/* Now we emplaced the read buffer in the map, we still need to assign that location
|
||||||
|
to the SPI driver transfer struct */
|
||||||
|
spiCookie->assignReadBuffer(statusPair.first->second.replyBuffer.data());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::error << "SpiComIF::initializeInterface: SPI address already exists!" << std::endl;
|
||||||
|
#else
|
||||||
|
sif::printError("SpiComIF::initializeInterface: SPI address already exists!\n");
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Pull CS high in any case to be sure that device is inactive */
|
||||||
|
gpioId_t gpioId = spiCookie->getChipSelectPin();
|
||||||
|
if(gpioId != gpio::NO_GPIO) {
|
||||||
|
gpioComIF->pullHigh(gpioId);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t spiSpeed = 0;
|
||||||
|
spi::SpiModes spiMode = spi::SpiModes::MODE_0;
|
||||||
|
|
||||||
|
SpiCookie::UncommonParameters params;
|
||||||
|
spiCookie->getSpiParameters(spiMode, spiSpeed, ¶ms);
|
||||||
|
|
||||||
|
int fileDescriptor = 0;
|
||||||
|
UnixFileGuard fileHelper(spiCookie->getSpiDevice(), &fileDescriptor, O_RDWR,
|
||||||
|
"SpiComIF::initializeInterface: ");
|
||||||
|
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
|
||||||
|
return fileHelper.getOpenResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* These flags are rather uncommon */
|
||||||
|
if(params.threeWireSpi or params.noCs or params.csHigh) {
|
||||||
|
uint32_t currentMode = 0;
|
||||||
|
retval = ioctl(fileDescriptor, SPI_IOC_RD_MODE32, ¤tMode);
|
||||||
|
if(retval != 0) {
|
||||||
|
utility::handleIoctlError("SpiComIF::initialiezInterface: Could not read full mode!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(params.threeWireSpi) {
|
||||||
|
currentMode |= SPI_3WIRE;
|
||||||
|
}
|
||||||
|
if(params.noCs) {
|
||||||
|
/* Some drivers like the Raspberry Pi ignore this flag in any case */
|
||||||
|
currentMode |= SPI_NO_CS;
|
||||||
|
}
|
||||||
|
if(params.csHigh) {
|
||||||
|
currentMode |= SPI_CS_HIGH;
|
||||||
|
}
|
||||||
|
/* Write adapted mode */
|
||||||
|
retval = ioctl(fileDescriptor, SPI_IOC_WR_MODE32, ¤tMode);
|
||||||
|
if(retval != 0) {
|
||||||
|
utility::handleIoctlError("SpiComIF::initialiezInterface: Could not write full mode!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(params.lsbFirst) {
|
||||||
|
retval = ioctl(fileDescriptor, SPI_IOC_WR_LSB_FIRST, ¶ms.lsbFirst);
|
||||||
|
if(retval != 0) {
|
||||||
|
utility::handleIoctlError("SpiComIF::initializeInterface: Setting LSB first failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(params.bitsPerWord != 8) {
|
||||||
|
retval = ioctl(fileDescriptor, SPI_IOC_WR_BITS_PER_WORD, ¶ms.bitsPerWord);
|
||||||
|
if(retval != 0) {
|
||||||
|
utility::handleIoctlError("SpiComIF::initializeInterface: "
|
||||||
|
"Could not write bits per word!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sendLen > spiCookie->getMaxBufferSize()) {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::warning << "SpiComIF::sendMessage: Too much data sent, send length" << sendLen <<
|
||||||
|
"larger than maximum buffer length" << spiCookie->getMaxBufferSize() << std::endl;
|
||||||
|
#else
|
||||||
|
sif::printWarning("SpiComIF::sendMessage: Too much data sent, send length %lu larger "
|
||||||
|
"than maximum buffer length %lu!\n", static_cast<unsigned long>(sendLen),
|
||||||
|
static_cast<unsigned long>(spiCookie->getMaxBufferSize()));
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
return DeviceCommunicationIF::TOO_MUCH_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Prepare transfer */
|
||||||
|
int fileDescriptor = 0;
|
||||||
|
std::string device = spiCookie->getSpiDevice();
|
||||||
|
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR,
|
||||||
|
"SpiComIF::sendMessage: ");
|
||||||
|
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
|
||||||
|
return OPENING_FILE_FAILED;
|
||||||
|
}
|
||||||
|
spi::SpiModes spiMode = spi::SpiModes::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();
|
||||||
|
gpioId_t gpioId = spiCookie->getChipSelectPin();
|
||||||
|
|
||||||
|
/* GPIO access is mutex protected */
|
||||||
|
MutexGuard(spiMutex, timeoutType, timeoutMs);
|
||||||
|
|
||||||
|
/* Pull SPI CS low. For now, no support for active high given */
|
||||||
|
if(gpioId != gpio::NO_GPIO) {
|
||||||
|
gpioComIF->pullLow(gpioId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Execute transfer */
|
||||||
|
if(fullDuplex) {
|
||||||
|
/* Initiate a full duplex SPI transfer. */
|
||||||
|
retval = ioctl(fileDescriptor, SPI_IOC_MESSAGE(1), spiCookie->getTransferStructHandle());
|
||||||
|
if(retval < 0) {
|
||||||
|
utility::handleIoctlError("SpiComIF::sendMessage: ioctl error.");
|
||||||
|
result = FULL_DUPLEX_TRANSFER_FAILED;
|
||||||
|
}
|
||||||
|
#if FSFW_LINUX_SPI_WIRETAPPING == 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::info << "Sent SPI data: " << std::endl;
|
||||||
|
size_t dataLen = spiCookie->getTransferStructHandle()->len;
|
||||||
|
uint8_t* dataPtr = reinterpret_cast<uint8_t*>(spiCookie->getTransferStructHandle()->tx_buf);
|
||||||
|
arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false);
|
||||||
|
sif::info << "Received SPI data: " << std::endl;
|
||||||
|
dataPtr = reinterpret_cast<uint8_t*>(spiCookie->getTransferStructHandle()->rx_buf);
|
||||||
|
arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false);
|
||||||
|
#else
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_LINUX_SPI_WIRETAPPING == 1 */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* 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
|
||||||
|
sif::warning << "SpiComIF::sendMessage: Half-Duplex write operation failed!" <<
|
||||||
|
std::endl;
|
||||||
|
#else
|
||||||
|
sif::printWarning("SpiComIF::sendMessage: Half-Duplex write operation failed!\n");
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
result = HALF_DUPLEX_TRANSFER_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gpioId != gpio::NO_GPIO) {
|
||||||
|
gpioComIF->pullHigh(gpioId);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t SpiComIF::getSendSuccess(CookieIF *cookie) {
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
|
||||||
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||||
|
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
|
||||||
|
if(spiCookie == nullptr) {
|
||||||
|
return NULLPOINTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fullDuplex = spiCookie->isFullDuplex();
|
||||||
|
if(fullDuplex) {
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string device = spiCookie->getSpiDevice();
|
||||||
|
int fileDescriptor = 0;
|
||||||
|
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR,
|
||||||
|
"SpiComIF::requestReceiveMessage: ");
|
||||||
|
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
|
||||||
|
return OPENING_FILE_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* rxBuf = nullptr;
|
||||||
|
size_t readSize = spiCookie->getCurrentTransferSize();
|
||||||
|
result = getReadBuffer(spiCookie->getSpiAddress(), &rxBuf);
|
||||||
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
gpioId_t gpioId = spiCookie->getChipSelectPin();
|
||||||
|
MutexGuard(spiMutex, timeoutType, timeoutMs);
|
||||||
|
if(gpioId != gpio::NO_GPIO) {
|
||||||
|
gpioComIF->pullLow(gpioId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(read(fileDescriptor, rxBuf, readSize) != static_cast<ssize_t>(readSize)) {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::warning << "SpiComIF::sendMessage: Half-Duplex read operation failed!" << std::endl;
|
||||||
|
#else
|
||||||
|
sif::printWarning("SpiComIF::sendMessage: Half-Duplex read operation failed!\n");
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
result = HALF_DUPLEX_TRANSFER_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gpioId != gpio::NO_GPIO) {
|
||||||
|
gpioComIF->pullHigh(gpioId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t SpiComIF::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) {
|
||||||
|
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
|
||||||
|
if(spiCookie == nullptr) {
|
||||||
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
|
}
|
||||||
|
uint8_t* rxBuf = nullptr;
|
||||||
|
ReturnValue_t result = getReadBuffer(spiCookie->getSpiAddress(), &rxBuf);
|
||||||
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
*buffer = rxBuf;
|
||||||
|
*size = spiCookie->getCurrentTransferSize();
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t SpiComIF::getReadBuffer(address_t spiAddress, uint8_t** buffer) {
|
||||||
|
if(buffer == nullptr) {
|
||||||
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto iter = spiDeviceMap.find(spiAddress);
|
||||||
|
if(iter == spiDeviceMap.end()) {
|
||||||
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
*buffer = iter->second.replyBuffer.data();
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiComIF::setSpiSpeedAndMode(int spiFd, spi::SpiModes 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!");
|
||||||
|
}
|
||||||
|
}
|
64
linux/spi/SpiComIF.h
Normal file
64
linux/spi/SpiComIF.h
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#ifndef LINUX_SPI_SPICOMIF_H_
|
||||||
|
#define LINUX_SPI_SPICOMIF_H_
|
||||||
|
|
||||||
|
#include "spiDefinitions.h"
|
||||||
|
#include <returnvalues/classIds.h>
|
||||||
|
#include <fsfw_hal/common/gpio/GpioIF.h>
|
||||||
|
|
||||||
|
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
|
||||||
|
#include <fsfw/objectmanager/SystemObject.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Encapsulates access to linux SPI driver for FSFW objects
|
||||||
|
* @details
|
||||||
|
* Right now, only full-duplex SPI is supported.
|
||||||
|
* @author R. Mueller
|
||||||
|
*/
|
||||||
|
class SpiComIF: public DeviceCommunicationIF, public SystemObject {
|
||||||
|
public:
|
||||||
|
static constexpr uint8_t spiRetvalId = CLASS_ID::LINUX_SPI_COM_IF;
|
||||||
|
static constexpr ReturnValue_t OPENING_FILE_FAILED =
|
||||||
|
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0);
|
||||||
|
/* Full duplex (ioctl) transfer failure */
|
||||||
|
static constexpr ReturnValue_t FULL_DUPLEX_TRANSFER_FAILED =
|
||||||
|
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 1);
|
||||||
|
/* Half duplex (read/write) transfer failure */
|
||||||
|
static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED =
|
||||||
|
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2);
|
||||||
|
|
||||||
|
SpiComIF(object_id_t objectId, GpioIF* gpioComIF);
|
||||||
|
|
||||||
|
ReturnValue_t initializeInterface(CookieIF * cookie) override;
|
||||||
|
ReturnValue_t sendMessage(CookieIF *cookie,const uint8_t *sendData,
|
||||||
|
size_t sendLen) override;
|
||||||
|
ReturnValue_t getSendSuccess(CookieIF *cookie) override;
|
||||||
|
ReturnValue_t requestReceiveMessage(CookieIF *cookie,
|
||||||
|
size_t requestLen) override;
|
||||||
|
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
|
||||||
|
size_t *size) override;
|
||||||
|
private:
|
||||||
|
|
||||||
|
struct SpiInstance {
|
||||||
|
std::vector<uint8_t> replyBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
GpioIF* gpioComIF = nullptr;
|
||||||
|
|
||||||
|
MutexIF* spiMutex = nullptr;
|
||||||
|
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;
|
||||||
|
uint32_t timeoutMs = 20;
|
||||||
|
|
||||||
|
using SpiDeviceMap = std::unordered_map<address_t, SpiInstance>;
|
||||||
|
using SpiDeviceMapIter = SpiDeviceMap::iterator;
|
||||||
|
|
||||||
|
SpiDeviceMap spiDeviceMap;
|
||||||
|
|
||||||
|
|
||||||
|
ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer);
|
||||||
|
void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* LINUX_SPI_SPICOMIF_H_ */
|
107
linux/spi/SpiCookie.cpp
Normal file
107
linux/spi/SpiCookie.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#include "SpiCookie.h"
|
||||||
|
|
||||||
|
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev,
|
||||||
|
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed): spiAddress(spiAddress),
|
||||||
|
chipSelectPin(chipSelect), spiDevice(spiDev), maxSize(maxSize), spiMode(spiMode),
|
||||||
|
spiSpeed(spiSpeed) {
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiCookie::SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxSize,
|
||||||
|
spi::SpiModes spiMode, uint32_t spiSpeed):
|
||||||
|
SpiCookie(spiAddress, gpio::NO_GPIO, spiDev, maxSize, spiMode, spiSpeed) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed,
|
||||||
|
UncommonParameters* parameters) const {
|
||||||
|
spiMode = this->spiMode;
|
||||||
|
spiSpeed = this->spiSpeed;
|
||||||
|
|
||||||
|
if(parameters != nullptr) {
|
||||||
|
parameters->threeWireSpi = uncommonParameters.threeWireSpi;
|
||||||
|
parameters->lsbFirst = uncommonParameters.lsbFirst;
|
||||||
|
parameters->noCs = uncommonParameters.noCs;
|
||||||
|
parameters->bitsPerWord = uncommonParameters.bitsPerWord;
|
||||||
|
parameters->csHigh = uncommonParameters.csHigh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gpioId_t SpiCookie::getChipSelectPin() const {
|
||||||
|
return chipSelectPin;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t SpiCookie::getMaxBufferSize() const {
|
||||||
|
return maxSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
address_t SpiCookie::getSpiAddress() const {
|
||||||
|
return spiAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string SpiCookie::getSpiDevice() const {
|
||||||
|
return spiDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setThreeWireSpi(bool enable) {
|
||||||
|
uncommonParameters.threeWireSpi = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setLsbFirst(bool enable) {
|
||||||
|
uncommonParameters.lsbFirst = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setNoCs(bool enable) {
|
||||||
|
uncommonParameters.noCs = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setBitsPerWord(uint8_t bitsPerWord) {
|
||||||
|
uncommonParameters.bitsPerWord = bitsPerWord;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setCsHigh(bool enable) {
|
||||||
|
uncommonParameters.csHigh = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::activateCsDeselect(bool deselectCs, uint16_t delayUsecs) {
|
||||||
|
spiTransferStruct.cs_change = deselectCs;
|
||||||
|
spiTransferStruct.delay_usecs = delayUsecs;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::assignReadBuffer(uint8_t* rx) {
|
||||||
|
if(rx != nullptr) {
|
||||||
|
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(rx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::assignWriteBuffer(const uint8_t* tx) {
|
||||||
|
if(tx != nullptr) {
|
||||||
|
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(tx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spi_ioc_transfer* SpiCookie::getTransferStructHandle() {
|
||||||
|
return &spiTransferStruct;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setFullOrHalfDuplex(bool halfDuplex) {
|
||||||
|
this->halfDuplex = halfDuplex;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SpiCookie::isFullDuplex() const {
|
||||||
|
return not this->halfDuplex;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::assignTransferSize(size_t transferSize) {
|
||||||
|
spiTransferStruct.len = transferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t SpiCookie::getCurrentTransferSize() const {
|
||||||
|
return spiTransferStruct.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setSpiSpeed(uint32_t newSpeed) {
|
||||||
|
this->spiSpeed = newSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpiCookie::setSpiMode(spi::SpiModes newMode) {
|
||||||
|
this->spiMode = newMode;
|
||||||
|
}
|
119
linux/spi/SpiCookie.h
Normal file
119
linux/spi/SpiCookie.h
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
#ifndef LINUX_SPI_SPICOOKIE_H_
|
||||||
|
#define LINUX_SPI_SPICOOKIE_H_
|
||||||
|
|
||||||
|
#include "spiDefinitions.h"
|
||||||
|
#include <fsfw/devicehandlers/CookieIF.h>
|
||||||
|
#include <fsfw_hal/common/gpio/gpioDefinitions.h>
|
||||||
|
#include <linux/spi/spidev.h>
|
||||||
|
|
||||||
|
class SpiCookie: public CookieIF {
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Each SPI device will have a corresponding cookie. The cookie is used by the communication
|
||||||
|
* interface and contains device specific information like the largest expected size to be
|
||||||
|
* sent and received and the GPIO pin used to toggle the SPI slave select pin.
|
||||||
|
* @param spiAddress
|
||||||
|
* @param chipSelect Chip select. gpio::NO_GPIO can be used for hardware slave selects.
|
||||||
|
* @param spiDev
|
||||||
|
* @param maxSize
|
||||||
|
*/
|
||||||
|
SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev,
|
||||||
|
const size_t maxReplySize, spi::SpiModes spiMode, uint32_t spiSpeed);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Like constructor above, but without a dedicated GPIO CS. Can be used for hardware
|
||||||
|
* slave select or if CS logic is performed with decoders.
|
||||||
|
*/
|
||||||
|
SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxReplySize,
|
||||||
|
spi::SpiModes spiMode, uint32_t spiSpeed);
|
||||||
|
|
||||||
|
address_t getSpiAddress() const;
|
||||||
|
std::string getSpiDevice() const;
|
||||||
|
gpioId_t getChipSelectPin() const;
|
||||||
|
size_t getMaxBufferSize() const;
|
||||||
|
|
||||||
|
/** Enables changing SPI speed at run-time */
|
||||||
|
void setSpiSpeed(uint32_t newSpeed);
|
||||||
|
/** Enables changing the SPI mode at run-time */
|
||||||
|
void setSpiMode(spi::SpiModes newMode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True if SPI transfers should be performed in full duplex mode
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
bool isFullDuplex() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set transfer type to full duplex or half duplex. Full duplex is the default setting,
|
||||||
|
* ressembling common SPI hardware implementation with shift registers, where read and writes
|
||||||
|
* happen simultaneosly.
|
||||||
|
* @param fullDuplex
|
||||||
|
*/
|
||||||
|
void setFullOrHalfDuplex(bool halfDuplex);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This needs to be called to specify where the SPI driver writes to or reads from.
|
||||||
|
* @param readLocation
|
||||||
|
* @param writeLocation
|
||||||
|
*/
|
||||||
|
void assignReadBuffer(uint8_t* rx);
|
||||||
|
void assignWriteBuffer(const uint8_t* tx);
|
||||||
|
/**
|
||||||
|
* Assign size for the next transfer.
|
||||||
|
* @param transferSize
|
||||||
|
*/
|
||||||
|
void assignTransferSize(size_t transferSize);
|
||||||
|
size_t getCurrentTransferSize() const;
|
||||||
|
|
||||||
|
struct UncommonParameters {
|
||||||
|
uint8_t bitsPerWord = 8;
|
||||||
|
bool noCs = false;
|
||||||
|
bool csHigh = false;
|
||||||
|
bool threeWireSpi = false;
|
||||||
|
/* MSB first is more common */
|
||||||
|
bool lsbFirst = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can be used to explicitely disable hardware chip select.
|
||||||
|
* Some drivers like the Raspberry Pi Linux driver will not use hardware chip select by default
|
||||||
|
* (see https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md)
|
||||||
|
* @param enable
|
||||||
|
*/
|
||||||
|
void setNoCs(bool enable);
|
||||||
|
void setThreeWireSpi(bool enable);
|
||||||
|
void setLsbFirst(bool enable);
|
||||||
|
void setCsHigh(bool enable);
|
||||||
|
void setBitsPerWord(uint8_t bitsPerWord);
|
||||||
|
|
||||||
|
void getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed,
|
||||||
|
UncommonParameters* parameters = nullptr) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See spidev.h cs_change and delay_usecs
|
||||||
|
* @param deselectCs
|
||||||
|
* @param delayUsecs
|
||||||
|
*/
|
||||||
|
void activateCsDeselect(bool deselectCs, uint16_t delayUsecs);
|
||||||
|
|
||||||
|
spi_ioc_transfer* getTransferStructHandle();
|
||||||
|
private:
|
||||||
|
size_t currentTransferSize = 0;
|
||||||
|
|
||||||
|
address_t spiAddress;
|
||||||
|
gpioId_t chipSelectPin;
|
||||||
|
std::string spiDevice;
|
||||||
|
|
||||||
|
const size_t maxSize;
|
||||||
|
spi::SpiModes spiMode;
|
||||||
|
uint32_t spiSpeed;
|
||||||
|
bool halfDuplex = false;
|
||||||
|
|
||||||
|
struct spi_ioc_transfer spiTransferStruct = {};
|
||||||
|
UncommonParameters uncommonParameters;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LINUX_SPI_SPICOOKIE_H_ */
|
17
linux/spi/spiDefinitions.h
Normal file
17
linux/spi/spiDefinitions.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef LINUX_SPI_SPIDEFINITONS_H_
|
||||||
|
#define LINUX_SPI_SPIDEFINITONS_H_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace spi {
|
||||||
|
|
||||||
|
enum SpiModes: uint8_t {
|
||||||
|
MODE_0,
|
||||||
|
MODE_1,
|
||||||
|
MODE_2,
|
||||||
|
MODE_3
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LINUX_SPI_SPIDEFINITONS_H_ */
|
21
linux/utility.cpp
Normal file
21
linux/utility.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include <fsfw_hal/linux/utility.h>
|
||||||
|
|
||||||
|
void utility::handleIoctlError(const char* const customPrintout) {
|
||||||
|
#if FSFW_VERBOSE_LEVEL >= 1
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
if(customPrintout != nullptr) {
|
||||||
|
sif::warning << customPrintout << std::endl;
|
||||||
|
}
|
||||||
|
sif::warning << "handleIoctlError: Error code " << errno << ", "<< strerror(errno) <<
|
||||||
|
std::endl;
|
||||||
|
#else
|
||||||
|
if(customPrintout != nullptr) {
|
||||||
|
sif::printWarning("%s\n", customPrintout);
|
||||||
|
}
|
||||||
|
sif::printWarning("handleIoctlError: Error code %d, %s\n", errno, strerror(errno));
|
||||||
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||||
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
10
linux/utility.h
Normal file
10
linux/utility.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef LINUX_UTILITY_UTILITY_H_
|
||||||
|
#define LINUX_UTILITY_UTILITY_H_
|
||||||
|
|
||||||
|
namespace utility {
|
||||||
|
|
||||||
|
void handleIoctlError(const char* const customPrintout);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LINUX_UTILITY_UTILITY_H_ */
|
Loading…
Reference in New Issue
Block a user