fsfw/src/fsfw_hal/linux/spi/ManualCsLockGuard.h

46 lines
1.2 KiB
C
Raw Normal View History

2022-05-14 11:33:43 +02:00
#pragma once
2023-02-21 02:59:13 +01:00
#include <fsfw_hal/linux/spi/SpiCookie.h>
2023-02-24 15:49:05 +01:00
2022-05-14 11:33:43 +02:00
#include "fsfw/ipc/MutexIF.h"
2022-08-24 17:25:45 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2022-05-14 11:33:43 +02:00
#include "fsfw_hal/common/gpio/GpioIF.h"
2022-08-24 17:25:45 +02:00
class ManualCsLockWrapper {
2022-05-14 11:33:43 +02:00
public:
ManualCsLockWrapper(MutexIF* lock, GpioIF* gpioIF, SpiCookie* cookie,
MutexIF::TimeoutType type = MutexIF::TimeoutType::BLOCKING,
uint32_t timeoutMs = 0)
: lock(lock), gpioIF(gpioIF), cookie(cookie), type(type), timeoutMs(timeoutMs) {
if (cookie == nullptr) {
// TODO: Error? Or maybe throw exception..
return;
}
cookie->setCsLockManual(true);
lockResult = lock->lockMutex(type, timeoutMs);
2022-08-24 17:25:45 +02:00
if (lockResult != returnvalue::OK) {
2022-05-14 11:33:43 +02:00
return;
}
gpioResult = gpioIF->pullLow(cookie->getChipSelectPin());
}
~ManualCsLockWrapper() {
2022-08-24 17:25:45 +02:00
if (gpioResult == returnvalue::OK) {
2022-05-14 11:33:43 +02:00
gpioIF->pullHigh(cookie->getChipSelectPin());
}
2022-05-14 16:58:28 +02:00
cookie->setCsLockManual(false);
2022-08-24 17:25:45 +02:00
if (lockResult == returnvalue::OK) {
2022-05-14 16:58:28 +02:00
lock->unlockMutex();
}
2022-05-14 11:33:43 +02:00
}
ReturnValue_t lockResult;
ReturnValue_t gpioResult;
private:
MutexIF* lock;
GpioIF* gpioIF;
SpiCookie* cookie;
MutexIF::TimeoutType type;
uint32_t timeoutMs = 0;
};