43 lines
1.1 KiB
C
43 lines
1.1 KiB
C
|
#pragma once
|
||
|
|
||
|
#include "fsfw/ipc/MutexIF.h"
|
||
|
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
|
||
|
#include "fsfw_hal/common/gpio/GpioIF.h"
|
||
|
|
||
|
class ManualCsLockWrapper : public HasReturnvaluesIF {
|
||
|
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);
|
||
|
if (lockResult != RETURN_OK) {
|
||
|
return;
|
||
|
}
|
||
|
gpioResult = gpioIF->pullLow(cookie->getChipSelectPin());
|
||
|
}
|
||
|
|
||
|
~ManualCsLockWrapper() {
|
||
|
if (lockResult == RETURN_OK) {
|
||
|
lock->unlockMutex();
|
||
|
}
|
||
|
if (gpioResult == RETURN_OK) {
|
||
|
gpioIF->pullHigh(cookie->getChipSelectPin());
|
||
|
}
|
||
|
}
|
||
|
ReturnValue_t lockResult;
|
||
|
ReturnValue_t gpioResult;
|
||
|
|
||
|
private:
|
||
|
MutexIF* lock;
|
||
|
GpioIF* gpioIF;
|
||
|
SpiCookie* cookie;
|
||
|
MutexIF::TimeoutType type;
|
||
|
uint32_t timeoutMs = 0;
|
||
|
};
|