eive-obsw/linux/acs/RwPollingTask.h

91 lines
3.3 KiB
C
Raw Normal View History

2023-02-15 17:02:22 +01:00
#ifndef LINUX_DEVICES_RWPOLLINGTASK_H_
#define LINUX_DEVICES_RWPOLLINGTASK_H_
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <fsfw/objectmanager/SystemObject.h>
#include <fsfw/tasks/ExecutableObjectIF.h>
#include <fsfw/tasks/SemaphoreIF.h>
2023-02-16 16:08:17 +01:00
#include <fsfw_hal/common/gpio/GpioIF.h>
2023-02-15 17:02:22 +01:00
#include <fsfw_hal/linux/spi/SpiComIF.h>
#include <fsfw_hal/linux/spi/SpiCookie.h>
2023-04-07 17:02:37 +02:00
#include <mission/acs/defs.h>
2023-02-15 17:02:22 +01:00
2023-03-24 20:50:33 +01:00
#include "mission/acs/rwHelpers.h"
2023-02-15 17:02:22 +01:00
class RwCookie : public SpiCookie {
friend class RwPollingTask;
public:
static constexpr size_t REPLY_BUF_LEN = 524;
2023-02-15 17:02:22 +01:00
RwCookie(uint8_t rwIdx, address_t spiAddress, gpioId_t chipSelect, const size_t maxSize,
spi::SpiModes spiMode, uint32_t spiSpeed)
2023-02-19 12:25:26 +01:00
: SpiCookie(spiAddress, chipSelect, maxSize, spiMode, spiSpeed), rwIdx(rwIdx) {
bufLock = MutexFactory::instance()->createMutex();
}
2023-02-15 17:02:22 +01:00
private:
std::array<uint8_t, REPLY_BUF_LEN> replyBuf{};
2023-02-19 12:25:26 +01:00
std::array<uint8_t, REPLY_BUF_LEN> exchangeBuf{};
MutexIF* bufLock;
2023-02-15 17:02:22 +01:00
uint8_t rwIdx;
};
class RwPollingTask : public SystemObject, public ExecutableObjectIF, public DeviceCommunicationIF {
public:
2023-02-16 16:08:17 +01:00
RwPollingTask(object_id_t objectId, const char* spiDev, GpioIF& gpioIF);
2023-02-15 17:02:22 +01:00
ReturnValue_t performOperation(uint8_t operationCode) override;
ReturnValue_t initialize() override;
private:
2023-03-24 01:01:29 +01:00
enum class InternalState { IDLE, IS_BUSY } state = InternalState::IDLE;
2023-02-15 17:02:22 +01:00
SemaphoreIF* semaphore;
bool debugMode = false;
bool modeAndSpeedWasSet = false;
MutexIF* ipcLock;
MutexIF* spiLock;
const char* spiDev;
2023-02-16 16:08:17 +01:00
GpioIF& gpioIF;
2023-02-16 14:32:23 +01:00
std::array<bool, 4> skipCommandingForRw;
2023-04-07 17:15:40 +02:00
std::array<bool, 4> skipSetSpeedReply;
std::array<DeviceCommandId_t, 4> specialRequestIds;
2023-02-15 17:02:22 +01:00
std::array<RwCookie*, 4> rwCookies;
2023-04-07 17:02:37 +02:00
std::array<rws::RwRequest, 4> rwRequests{};
2023-02-15 17:02:22 +01:00
std::array<uint8_t, rws::MAX_CMD_SIZE> writeBuffer;
std::array<uint8_t, rws::MAX_CMD_SIZE * 2> encodedBuffer;
2023-02-15 17:02:22 +01:00
size_t writeLen = 0;
static constexpr MutexIF::TimeoutType TIMEOUT_TYPE = MutexIF::TimeoutType::WAITING;
static constexpr uint32_t TIMEOUT_MS = 20;
static constexpr uint8_t MAX_RETRIES_REPLY = 5;
2023-02-19 12:25:26 +01:00
// DeviceCommunicationIF overrides
ReturnValue_t initializeInterface(CookieIF* cookie) override;
ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
ReturnValue_t getSendSuccess(CookieIF* cookie) override;
ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) override;
ReturnValue_t writeAndReadAllRws(DeviceCommandId_t id);
ReturnValue_t writeOneRwCmd(uint8_t rwIdx, int fd);
2023-02-16 17:57:21 +01:00
ReturnValue_t readAllRws(DeviceCommandId_t id);
ReturnValue_t sendOneMessage(int fd, RwCookie& rwCookie);
ReturnValue_t readNextReply(RwCookie& rwCookie, uint8_t* replyBuf, size_t maxReplyLen);
void handleSpecialRequests();
ReturnValue_t openSpi(int flags, int& fd);
2023-02-16 19:51:30 +01:00
ReturnValue_t pullCsLow(gpioId_t gpioId, GpioIF& gpioIF);
2023-02-15 17:02:22 +01:00
void prepareSimpleCommand(DeviceCommandId_t id);
ReturnValue_t prepareSetSpeedCmd(uint8_t rwIdx);
size_t idAndIdxToReadBuffer(DeviceCommandId_t id, uint8_t rwIdx, uint8_t** readPtr);
void fillSpecialRequestArray();
2023-02-17 02:10:08 +01:00
void setAllReadFlagsFalse();
2023-02-15 19:27:19 +01:00
void pullCsHigh(gpioId_t gpioId, GpioIF& gpioIF);
2023-02-15 17:02:22 +01:00
void closeSpi(int);
};
#endif /* LINUX_DEVICES_RWPOLLINGTASK_H_ */