SPI refactoring #80

Merged
meierj merged 35 commits from mueller/spi-cache-spi-dev-in-com-if into develop 2022-05-11 16:28:09 +02:00
16 changed files with 49 additions and 54 deletions
Showing only changes of commit e06c457743 - Show all commits

View File

@ -6,7 +6,7 @@
#include "fsfw/FSFW.h" #include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h" #include "fsfw/serviceinterface.h"
UnixFileGuard::UnixFileGuard(std::string device, int* fileDescriptor, int flags, UnixFileGuard::UnixFileGuard(const std::string& device, int* fileDescriptor, int flags,
std::string diagnosticPrefix) std::string diagnosticPrefix)
: fileDescriptor(fileDescriptor) { : fileDescriptor(fileDescriptor) {
if (fileDescriptor == nullptr) { if (fileDescriptor == nullptr) {

View File

@ -15,7 +15,7 @@ class UnixFileGuard {
static constexpr ReturnValue_t OPEN_FILE_FAILED = 1; static constexpr ReturnValue_t OPEN_FILE_FAILED = 1;
UnixFileGuard(std::string device, int* fileDescriptor, int flags, UnixFileGuard(const std::string& device, int* fileDescriptor, int flags,
std::string diagnosticPrefix = ""); std::string diagnosticPrefix = "");
virtual ~UnixFileGuard(); virtual ~UnixFileGuard();

View File

@ -15,8 +15,8 @@
#include "fsfw_hal/linux/spi/SpiCookie.h" #include "fsfw_hal/linux/spi/SpiCookie.h"
#include "fsfw_hal/linux/utility.h" #include "fsfw_hal/linux/utility.h"
SpiComIF::SpiComIF(object_id_t objectId, GpioIF* gpioComIF) SpiComIF::SpiComIF(object_id_t objectId, std::string devname, GpioIF* gpioComIF)
: SystemObject(objectId), gpioComIF(gpioComIF) { : SystemObject(objectId), gpioComIF(gpioComIF), dev(std::move(devname)) {
if (gpioComIF == nullptr) { if (gpioComIF == nullptr) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
@ -85,8 +85,7 @@ ReturnValue_t SpiComIF::initializeInterface(CookieIF* cookie) {
spiCookie->getSpiParameters(spiMode, spiSpeed, &params); spiCookie->getSpiParameters(spiMode, spiSpeed, &params);
int fileDescriptor = 0; int fileDescriptor = 0;
UnixFileGuard fileHelper(spiCookie->getSpiDevice(), &fileDescriptor, O_RDWR, UnixFileGuard fileHelper(dev, &fileDescriptor, O_RDWR, "SpiComIF::initializeInterface");
"SpiComIF::initializeInterface");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) { if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult(); return fileHelper.getOpenResult();
} }
@ -182,8 +181,7 @@ ReturnValue_t SpiComIF::performRegularSendOperation(SpiCookie* spiCookie, const
int retval = 0; int retval = 0;
/* Prepare transfer */ /* Prepare transfer */
int fileDescriptor = 0; int fileDescriptor = 0;
std::string device = spiCookie->getSpiDevice(); UnixFileGuard fileHelper(dev, &fileDescriptor, O_RDWR, "SpiComIF::sendMessage");
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, "SpiComIF::sendMessage");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) { if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return OPENING_FILE_FAILED; return OPENING_FILE_FAILED;
} }
@ -278,9 +276,8 @@ ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLe
ReturnValue_t SpiComIF::performHalfDuplexReception(SpiCookie* spiCookie) { ReturnValue_t SpiComIF::performHalfDuplexReception(SpiCookie* spiCookie) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
std::string device = spiCookie->getSpiDevice();
int fileDescriptor = 0; int fileDescriptor = 0;
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, "SpiComIF::requestReceiveMessage"); UnixFileGuard fileHelper(dev, &fileDescriptor, O_RDWR, "SpiComIF::requestReceiveMessage");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) { if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return OPENING_FILE_FAILED; return OPENING_FILE_FAILED;
} }
@ -424,3 +421,5 @@ void SpiComIF::getSpiSpeedAndMode(int spiFd, spi::SpiModes &mode, uint32_t &spee
utility::handleIoctlError("SpiComIF::getSpiSpeedAndMode: Getting SPI speed failed"); utility::handleIoctlError("SpiComIF::getSpiSpeedAndMode: Getting SPI speed failed");
} }
} }
const std::string& SpiComIF::getSpiDev() const { return dev; }

View File

@ -32,7 +32,7 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject {
static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED = static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2); HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2);
SpiComIF(object_id_t objectId, GpioIF* gpioComIF); SpiComIF(object_id_t objectId, std::string devname, GpioIF* gpioComIF);
ReturnValue_t initializeInterface(CookieIF* cookie) override; ReturnValue_t initializeInterface(CookieIF* cookie) override;
ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override; ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
@ -60,6 +60,8 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject {
GpioIF* getGpioInterface(); GpioIF* getGpioInterface();
void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed); void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed);
void getSpiSpeedAndMode(int spiFd, spi::SpiModes& mode, uint32_t& speed) const; void getSpiSpeedAndMode(int spiFd, spi::SpiModes& mode, uint32_t& speed) const;
const std::string& getSpiDev() const;
void performSpiWiretapping(SpiCookie* spiCookie); void performSpiWiretapping(SpiCookie* spiCookie);
ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer); ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer);
@ -71,7 +73,7 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject {
}; };
GpioIF* gpioComIF = nullptr; GpioIF* gpioComIF = nullptr;
std::string dev = "";
MutexIF* spiMutex = nullptr; MutexIF* spiMutex = nullptr;
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;
uint32_t timeoutMs = 20; uint32_t timeoutMs = 20;

View File

@ -1,26 +1,25 @@
#include "SpiCookie.h" #include "SpiCookie.h"
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, const size_t maxSize,
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed)
: SpiCookie(spi::SpiComIfModes::REGULAR, spiAddress, chipSelect, spiDev, maxSize, spiMode,
spiSpeed, nullptr, nullptr) {}
SpiCookie::SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxSize,
spi::SpiModes spiMode, uint32_t spiSpeed) spi::SpiModes spiMode, uint32_t spiSpeed)
: SpiCookie(spiAddress, gpio::NO_GPIO, spiDev, maxSize, spiMode, spiSpeed) {} : SpiCookie(spi::SpiComIfModes::REGULAR, spiAddress, chipSelect, maxSize, spiMode, spiSpeed,
nullptr, nullptr) {}
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, SpiCookie::SpiCookie(address_t spiAddress, const size_t maxSize, spi::SpiModes spiMode,
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed, uint32_t spiSpeed)
: SpiCookie(spiAddress, gpio::NO_GPIO, maxSize, spiMode, spiSpeed) {}
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, const size_t maxSize,
spi::SpiModes spiMode, uint32_t spiSpeed,
spi::send_callback_function_t callback, void* args) spi::send_callback_function_t callback, void* args)
: SpiCookie(spi::SpiComIfModes::CALLBACK, spiAddress, chipSelect, spiDev, maxSize, spiMode, : SpiCookie(spi::SpiComIfModes::CALLBACK, spiAddress, chipSelect, maxSize, spiMode, spiSpeed,
spiSpeed, callback, args) {} callback, args) {}
SpiCookie::SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect, SpiCookie::SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect,
std::string spiDev, const size_t maxSize, spi::SpiModes spiMode, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed,
uint32_t spiSpeed, spi::send_callback_function_t callback, void* args) spi::send_callback_function_t callback, void* args)
: spiAddress(spiAddress), : spiAddress(spiAddress),
chipSelectPin(chipSelect), chipSelectPin(chipSelect),
spiDevice(spiDev),
comIfMode(comIfMode), comIfMode(comIfMode),
maxSize(maxSize), maxSize(maxSize),
spiMode(spiMode), spiMode(spiMode),
@ -50,8 +49,6 @@ size_t SpiCookie::getMaxBufferSize() const { return maxSize; }
address_t SpiCookie::getSpiAddress() const { return spiAddress; } address_t SpiCookie::getSpiAddress() const { return spiAddress; }
std::string SpiCookie::getSpiDevice() const { return spiDevice; }
void SpiCookie::setThreeWireSpi(bool enable) { uncommonParameters.threeWireSpi = enable; } void SpiCookie::setThreeWireSpi(bool enable) { uncommonParameters.threeWireSpi = enable; }
void SpiCookie::setLsbFirst(bool enable) { uncommonParameters.lsbFirst = enable; } void SpiCookie::setLsbFirst(bool enable) { uncommonParameters.lsbFirst = enable; }

View File

@ -29,23 +29,22 @@ class SpiCookie : public CookieIF {
* @param spiDev * @param spiDev
* @param maxSize * @param maxSize
*/ */
SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, const size_t maxSize, SpiCookie(address_t spiAddress, gpioId_t chipSelect, const size_t maxSize, spi::SpiModes spiMode,
spi::SpiModes spiMode, uint32_t spiSpeed); uint32_t spiSpeed);
/** /**
* Like constructor above, but without a dedicated GPIO CS. Can be used for hardware * Like constructor above, but without a dedicated GPIO CS. Can be used for hardware
* slave select or if CS logic is performed with decoders. * slave select or if CS logic is performed with decoders.
*/ */
SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxReplySize, SpiCookie(address_t spiAddress, const size_t maxReplySize, spi::SpiModes spiMode,
spi::SpiModes spiMode, uint32_t spiSpeed); uint32_t spiSpeed);
/** /**
* Use the callback mode of the SPI communication interface. The user can pass the callback * Use the callback mode of the SPI communication interface. The user can pass the callback
* function here or by using the setter function #setCallbackMode * function here or by using the setter function #setCallbackMode
*/ */
SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, const size_t maxSize, SpiCookie(address_t spiAddress, gpioId_t chipSelect, const size_t maxSize, spi::SpiModes spiMode,
spi::SpiModes spiMode, uint32_t spiSpeed, spi::send_callback_function_t callback, uint32_t spiSpeed, spi::send_callback_function_t callback, void* args);
void* args);
/** /**
* Get the callback function * Get the callback function
@ -55,7 +54,6 @@ class SpiCookie : public CookieIF {
void getCallback(spi::send_callback_function_t* callback, void** args); void getCallback(spi::send_callback_function_t* callback, void** args);
address_t getSpiAddress() const; address_t getSpiAddress() const;
std::string getSpiDevice() const;
gpioId_t getChipSelectPin() const; gpioId_t getChipSelectPin() const;
size_t getMaxBufferSize() const; size_t getMaxBufferSize() const;
@ -154,12 +152,11 @@ class SpiCookie : public CookieIF {
* @param args * @param args
*/ */
SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect, SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect,
std::string spiDev, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed,
spi::send_callback_function_t callback, void* args); spi::send_callback_function_t callback, void* args);
address_t spiAddress; address_t spiAddress;
gpioId_t chipSelectPin; gpioId_t chipSelectPin;
std::string spiDevice;
spi::SpiComIfModes comIfMode; spi::SpiComIfModes comIfMode;

View File

@ -12,6 +12,7 @@ template <typename T, size_t MAX_SIZE, typename count_t = uint8_t>
class FixedArrayList : public ArrayList<T, count_t> { class FixedArrayList : public ArrayList<T, count_t> {
static_assert(MAX_SIZE <= std::numeric_limits<count_t>::max(), static_assert(MAX_SIZE <= std::numeric_limits<count_t>::max(),
"count_t is not large enough to hold MAX_SIZE"); "count_t is not large enough to hold MAX_SIZE");
private: private:
T data[MAX_SIZE]; T data[MAX_SIZE];

View File

@ -1401,8 +1401,7 @@ uint8_t DeviceHandlerBase::getReplyDelayCycles(DeviceCommandId_t deviceCommand)
DeviceReplyMap::iterator iter = deviceReplyMap.find(deviceCommand); DeviceReplyMap::iterator iter = deviceReplyMap.find(deviceCommand);
if (iter == deviceReplyMap.end()) { if (iter == deviceReplyMap.end()) {
return 0; return 0;
} } else if (iter->second.countdown != nullptr) {
else if (iter->second.countdown != nullptr) {
return 0; return 0;
} }
return iter->second.delayCycles; return iter->second.delayCycles;

View File

@ -97,7 +97,8 @@ ReturnValue_t CService201HealthCommanding::handleReply(const CommandMessage *rep
} }
// Not used for now, health state already reported by event // Not used for now, health state already reported by event
[[maybe_unused]] ReturnValue_t CService201HealthCommanding::prepareHealthSetReply(const CommandMessage *reply) { [[maybe_unused]] ReturnValue_t CService201HealthCommanding::prepareHealthSetReply(
const CommandMessage *reply) {
auto health = static_cast<uint8_t>(HealthMessage::getHealth(reply)); auto health = static_cast<uint8_t>(HealthMessage::getHealth(reply));
auto oldHealth = static_cast<uint8_t>(HealthMessage::getOldHealth(reply)); auto oldHealth = static_cast<uint8_t>(HealthMessage::getOldHealth(reply));
HealthSetReply healthSetReply(health, oldHealth); HealthSetReply healthSetReply(health, oldHealth);

View File

@ -3,8 +3,7 @@
Countdown::Countdown(uint32_t initialTimeout, bool startImmediately) : timeout(initialTimeout) { Countdown::Countdown(uint32_t initialTimeout, bool startImmediately) : timeout(initialTimeout) {
if (startImmediately) { if (startImmediately) {
setTimeout(initialTimeout); setTimeout(initialTimeout);
} } else {
else {
timeout = initialTimeout; timeout = initialTimeout;
} }
} }