2022-05-14 11:33:43 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#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;
|
|
|
|
};
|