From 8a38c7958bea1a4374bc74126414dde4b190c882 Mon Sep 17 00:00:00 2001 From: "Jakob.Meier" Date: Wed, 12 May 2021 10:22:23 +0200 Subject: [PATCH] spi mutex only blocked when gpio != NO_GPIO --- linux/gpio/LinuxLibgpioIF.cpp | 11 ++++++----- linux/spi/SpiComIF.cpp | 11 +++++++---- linux/spi/SpiComIF.h | 5 +++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/linux/gpio/LinuxLibgpioIF.cpp b/linux/gpio/LinuxLibgpioIF.cpp index b8151c6..3001b8a 100644 --- a/linux/gpio/LinuxLibgpioIF.cpp +++ b/linux/gpio/LinuxLibgpioIF.cpp @@ -82,7 +82,7 @@ ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, GpiodRegular chipname = regularGpio->chipname; chip = gpiod_chip_open_by_name(chipname.c_str()); if (!chip) { - sif::warning << "LinuxLibgpioIF::configureGpios: Failed to open chip " + sif::warning << "LinuxLibgpioIF::configureRegularGpio: Failed to open chip " << chipname << ". Gpio ID: " << gpioId << std::endl; return RETURN_FAILED; } @@ -90,9 +90,10 @@ ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, GpiodRegular lineNum = regularGpio->lineNum; lineHandle = gpiod_chip_get_line(chip, lineNum); if (!lineHandle) { - sif::warning << "LinuxLibgpioIF::configureGpios: Failed to open line for GPIO" << std::endl; - sif::warning << "GPIO ID " << gpioId << "with line number " << lineNum << - " and chipname " << chipname << std::endl; + sif::debug << "LinuxLibgpioIF::configureRegularGpio: Failed to open line " << std::endl; + sif::debug << "GPIO ID: " << gpioId << ", line number: " << lineNum << + ", chipname: " << chipname << std::endl; + sif::debug << "Check if linux GPIO configuration has changed. " << std::endl; gpiod_chip_close(chip); return RETURN_FAILED; } @@ -105,7 +106,7 @@ ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, GpiodRegular result = gpiod_line_request_output(lineHandle, consumer.c_str(), regularGpio->initValue); if (result < 0) { - sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line " << lineNum << + sif::error << "LinuxLibgpioIF::configureRegularGpio: Failed to request line " << lineNum << " from GPIO instance with ID: " << gpioId << std::endl; gpiod_line_release(lineHandle); return RETURN_FAILED; diff --git a/linux/spi/SpiComIF.cpp b/linux/spi/SpiComIF.cpp index d70f91a..2724ac4 100644 --- a/linux/spi/SpiComIF.cpp +++ b/linux/spi/SpiComIF.cpp @@ -178,11 +178,10 @@ ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, s 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) { + /* GPIO access is mutex protected */ + MutexGuard(spiMutex, timeoutType, timeoutMs); gpioComIF->pullLow(gpioId); } @@ -263,8 +262,8 @@ ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLe } gpioId_t gpioId = spiCookie->getChipSelectPin(); - MutexGuard(spiMutex, timeoutType, timeoutMs); if(gpioId != gpio::NO_GPIO) { + MutexGuard(spiMutex, timeoutType, timeoutMs); gpioComIF->pullLow(gpioId); } @@ -302,6 +301,10 @@ ReturnValue_t SpiComIF::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, return HasReturnvaluesIF::RETURN_OK; } +MutexIF* SpiComIF::getMutex() { + return spiMutex; +} + ReturnValue_t SpiComIF::getReadBuffer(address_t spiAddress, uint8_t** buffer) { if(buffer == nullptr) { return HasReturnvaluesIF::RETURN_FAILED; diff --git a/linux/spi/SpiComIF.h b/linux/spi/SpiComIF.h index 67f7923..9228994 100644 --- a/linux/spi/SpiComIF.h +++ b/linux/spi/SpiComIF.h @@ -39,6 +39,11 @@ public: size_t requestLen) override; ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) override; + /** + * @brief This function returns the mutex which can be used to protect the spi bus when + * the chip select must be driven from outside of the com if. + */ + MutexIF* getMutex(); private: struct SpiInstance { -- 2.34.1