2023-02-15 17:02:22 +01:00
|
|
|
#include "RwPollingTask.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fsfw/globalfunctions/CRC.h>
|
|
|
|
#include <fsfw/tasks/SemaphoreFactory.h>
|
|
|
|
#include <fsfw/tasks/TaskFactory.h>
|
2023-02-17 02:10:08 +01:00
|
|
|
#include <fsfw/timemanager/Stopwatch.h>
|
2023-02-15 17:02:22 +01:00
|
|
|
#include <fsfw_hal/common/spi/spiCommon.h>
|
2023-02-16 16:08:17 +01:00
|
|
|
#include <fsfw_hal/linux/utility.h>
|
|
|
|
#include <sys/ioctl.h>
|
2023-02-15 17:02:22 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "devConf.h"
|
|
|
|
#include "mission/devices/devicedefinitions/rwHelpers.h"
|
|
|
|
|
2023-02-16 16:08:17 +01:00
|
|
|
RwPollingTask::RwPollingTask(object_id_t objectId, const char* spiDev, GpioIF& gpioIF)
|
|
|
|
: SystemObject(objectId), spiDev(spiDev), gpioIF(gpioIF) {
|
2023-02-15 17:02:22 +01:00
|
|
|
semaphore = SemaphoreFactory::instance()->createBinarySemaphore();
|
|
|
|
semaphore->acquire();
|
|
|
|
ipcLock = MutexFactory::instance()->createMutex();
|
2023-02-16 16:08:17 +01:00
|
|
|
spiLock = MutexFactory::instance()->createMutex();
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::performOperation(uint8_t operationCode) {
|
2023-02-16 14:10:59 +01:00
|
|
|
for (unsigned i = 0; i < 4; i++) {
|
|
|
|
if (rwCookies[i] == nullptr) {
|
|
|
|
sif::error << "Invalid RW cookie at index" << i << std::endl;
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
while (true) {
|
2023-02-15 19:16:42 +01:00
|
|
|
ipcLock->lockMutex();
|
|
|
|
state = InternalState::IDLE;
|
|
|
|
ipcLock->unlockMutex();
|
2023-02-15 17:02:22 +01:00
|
|
|
semaphore->acquire();
|
2023-02-17 02:10:08 +01:00
|
|
|
// This loop takes 50 ms on a debug build.
|
|
|
|
// Stopwatch watch;
|
2023-02-16 17:57:21 +01:00
|
|
|
TaskFactory::delayTask(5);
|
2023-02-15 19:16:42 +01:00
|
|
|
int fd = 0;
|
2023-02-16 14:32:23 +01:00
|
|
|
for (auto& skip : skipCommandingForRw) {
|
|
|
|
skip = false;
|
|
|
|
}
|
2023-02-17 02:10:08 +01:00
|
|
|
setAllReadFlagsFalse();
|
2023-02-15 19:16:42 +01:00
|
|
|
ReturnValue_t result = openSpi(O_RDWR, fd);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
for (unsigned idx = 0; idx < rwCookies.size(); idx++) {
|
2023-02-16 14:32:23 +01:00
|
|
|
if (rwCookies[idx]->specialRequest == rws::SpecialRwRequest::RESET_MCU) {
|
|
|
|
prepareSimpleCommand(rws::RESET_MCU);
|
|
|
|
// No point in commanding that specific RW for the cycle.
|
|
|
|
skipCommandingForRw[idx] = true;
|
|
|
|
writeOneRwCmd(idx, fd);
|
|
|
|
} else if (rwCookies[idx]->setSpeed) {
|
2023-02-16 14:10:59 +01:00
|
|
|
prepareSetSpeedCmd(idx);
|
|
|
|
if (writeOneRwCmd(idx, fd) != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closeSpi(fd);
|
2023-02-16 17:57:21 +01:00
|
|
|
if (readAllRws(rws::SET_SPEED) != returnvalue::OK) {
|
2023-02-15 19:16:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-17 02:10:08 +01:00
|
|
|
prepareSimpleCommand(rws::GET_LAST_RESET_STATUS);
|
|
|
|
if (writeAndReadAllRws(rws::GET_LAST_RESET_STATUS) != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prepareSimpleCommand(rws::GET_RW_STATUS);
|
|
|
|
if (writeAndReadAllRws(rws::GET_RW_STATUS) != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prepareSimpleCommand(rws::GET_TEMPERATURE);
|
|
|
|
if (writeAndReadAllRws(rws::GET_TEMPERATURE) != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prepareSimpleCommand(rws::CLEAR_LAST_RESET_STATUS);
|
|
|
|
if (writeAndReadAllRws(rws::CLEAR_LAST_RESET_STATUS) != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
handleSpecialRequests();
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-15 19:16:42 +01:00
|
|
|
|
2023-02-15 17:02:22 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-16 16:08:17 +01:00
|
|
|
ReturnValue_t RwPollingTask::initialize() { return returnvalue::OK; }
|
2023-02-15 17:02:22 +01:00
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::initializeInterface(CookieIF* cookie) {
|
|
|
|
// We don't need to set the speed because a SPI core is used, but the mode has to be set once
|
|
|
|
// correctly for all RWs
|
|
|
|
if (not modeAndSpeedWasSet) {
|
2023-02-15 19:16:42 +01:00
|
|
|
int fd = open(spiDev, O_RDWR);
|
2023-02-15 17:02:22 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
sif::error << "could not open RW SPI bus" << std::endl;
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
2023-02-16 16:08:17 +01:00
|
|
|
spi::SpiModes mode = spi::RW_MODE;
|
|
|
|
int retval = ioctl(fd, SPI_IOC_WR_MODE, reinterpret_cast<uint8_t*>(&mode));
|
|
|
|
if (retval != 0) {
|
|
|
|
utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI mode failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi::RW_SPEED);
|
|
|
|
if (retval != 0) {
|
|
|
|
utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI speed failed");
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
close(fd);
|
|
|
|
modeAndSpeedWasSet = true;
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:10:59 +01:00
|
|
|
auto* rwCookie = dynamic_cast<RwCookie*>(cookie);
|
|
|
|
if (rwCookie == nullptr) {
|
2023-02-16 16:54:58 +01:00
|
|
|
sif::error << "RwPollingTask::initializeInterface: Wrong cookie" << std::endl;
|
2023-02-16 14:10:59 +01:00
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
rwCookies[rwCookie->rwIdx] = rwCookie;
|
|
|
|
|
2023-02-15 17:02:22 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::sendMessage(CookieIF* cookie, const uint8_t* sendData,
|
|
|
|
size_t sendLen) {
|
2023-02-16 17:57:21 +01:00
|
|
|
if (sendData == nullptr or sendLen < 8) {
|
2023-02-15 17:02:22 +01:00
|
|
|
return DeviceHandlerIF::INVALID_DATA;
|
|
|
|
}
|
|
|
|
int32_t speed = 0;
|
|
|
|
uint16_t rampTime = 0;
|
2023-02-16 14:10:59 +01:00
|
|
|
const uint8_t* currentBuf = sendData;
|
|
|
|
bool setSpeed = currentBuf[0];
|
|
|
|
currentBuf += 1;
|
|
|
|
sendLen -= 1;
|
|
|
|
SerializeAdapter::deSerialize(&speed, ¤tBuf, &sendLen, SerializeIF::Endianness::MACHINE);
|
|
|
|
SerializeAdapter::deSerialize(&rampTime, ¤tBuf, &sendLen, SerializeIF::Endianness::MACHINE);
|
2023-02-15 17:02:22 +01:00
|
|
|
rws::SpecialRwRequest specialRequest = rws::SpecialRwRequest::REQUEST_NONE;
|
2023-02-16 14:10:59 +01:00
|
|
|
if (sendLen == 8 and sendData[7] < static_cast<uint8_t>(rws::SpecialRwRequest::NUM_REQUESTS)) {
|
|
|
|
specialRequest = static_cast<rws::SpecialRwRequest>(sendData[7]);
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
|
|
|
RwCookie* rwCookie = dynamic_cast<RwCookie*>(cookie);
|
2023-02-15 19:16:42 +01:00
|
|
|
if (rwCookie == nullptr) {
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
{
|
|
|
|
MutexGuard mg(ipcLock);
|
2023-02-16 14:10:59 +01:00
|
|
|
rwCookie->setSpeed = setSpeed;
|
2023-02-15 17:02:22 +01:00
|
|
|
rwCookie->currentRwSpeed = speed;
|
|
|
|
rwCookie->currentRampTime = rampTime;
|
|
|
|
rwCookie->specialRequest = specialRequest;
|
2023-02-16 17:57:21 +01:00
|
|
|
if (state == InternalState::IDLE) {
|
2023-02-15 19:16:42 +01:00
|
|
|
state = InternalState::BUSY;
|
2023-02-15 17:02:22 +01:00
|
|
|
semaphore->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; }
|
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
|
|
|
|
RwCookie* rwCookie = dynamic_cast<RwCookie*>(cookie);
|
|
|
|
{
|
2023-02-19 12:25:26 +01:00
|
|
|
MutexGuard mg(rwCookie->bufLock);
|
|
|
|
memcpy(rwCookie->exchangeBuf.data(), rwCookie->replyBuf.data(), rwCookie->replyBuf.size());
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-19 12:25:26 +01:00
|
|
|
*buffer = rwCookie->exchangeBuf.data();
|
|
|
|
*size = rwCookie->exchangeBuf.size();
|
2023-02-15 17:02:22 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:42 +01:00
|
|
|
ReturnValue_t RwPollingTask::writeAndReadAllRws(DeviceCommandId_t id) {
|
2023-02-15 17:02:22 +01:00
|
|
|
// Stopwatch watch;
|
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
|
|
|
|
int fd = 0;
|
2023-02-15 19:16:42 +01:00
|
|
|
result = openSpi(O_RDWR, fd);
|
2023-02-15 17:02:22 +01:00
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
for (unsigned idx = 0; idx < rwCookies.size(); idx++) {
|
2023-02-16 14:32:23 +01:00
|
|
|
if (skipCommandingForRw[idx]) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-17 12:19:53 +01:00
|
|
|
result = sendOneMessage(fd, *rwCookies[idx]);
|
2023-02-15 17:02:22 +01:00
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
closeSpi(fd);
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closeSpi(fd);
|
2023-02-16 17:57:21 +01:00
|
|
|
return readAllRws(id);
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:42 +01:00
|
|
|
ReturnValue_t RwPollingTask::openSpi(int flags, int& fd) {
|
|
|
|
fd = open(spiDev, flags);
|
2023-02-15 17:02:22 +01:00
|
|
|
if (fd < 0) {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::error << "RwPollingTask::openSpi: Failed to open device file" << std::endl;
|
2023-02-15 17:02:22 +01:00
|
|
|
return SpiComIF::OPENING_FILE_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:42 +01:00
|
|
|
ReturnValue_t RwPollingTask::readNextReply(RwCookie& rwCookie, uint8_t* replyBuf,
|
|
|
|
size_t maxReplyLen) {
|
2023-02-15 17:02:22 +01:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
int fd = 0;
|
|
|
|
gpioId_t gpioId = rwCookie.getChipSelectPin();
|
2023-02-15 19:27:19 +01:00
|
|
|
uint8_t byteRead = 0;
|
2023-02-17 02:10:08 +01:00
|
|
|
result = openSpi(O_RDWR, fd);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
pullCsLow(gpioId, gpioIF);
|
|
|
|
bool lastByteWasFrameMarker = false;
|
|
|
|
Countdown cd(3000);
|
|
|
|
size_t readIdx = 0;
|
2023-02-15 17:02:22 +01:00
|
|
|
|
2023-02-17 02:10:08 +01:00
|
|
|
while (true) {
|
|
|
|
lastByteWasFrameMarker = false;
|
|
|
|
if (read(fd, &byteRead, 1) != 1) {
|
|
|
|
sif::error << "RwPollingTask: Read failed. " << strerror(errno) << std::endl;
|
2023-02-15 19:27:19 +01:00
|
|
|
pullCsHigh(gpioId, gpioIF);
|
2023-02-15 17:02:22 +01:00
|
|
|
closeSpi(fd);
|
2023-02-17 02:10:08 +01:00
|
|
|
return rws::SPI_READ_FAILURE;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-17 02:10:08 +01:00
|
|
|
if (byteRead == rws::FRAME_DELIMITER) {
|
|
|
|
lastByteWasFrameMarker = true;
|
|
|
|
}
|
|
|
|
// Start of frame detected.
|
|
|
|
if (byteRead != rws::FRAME_DELIMITER and not lastByteWasFrameMarker) {
|
2023-02-16 19:51:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-02-17 02:10:08 +01:00
|
|
|
|
|
|
|
if (readIdx % 100 == 0 && cd.hasTimedOut()) {
|
|
|
|
pullCsHigh(gpioId, gpioIF);
|
|
|
|
closeSpi(fd);
|
|
|
|
return rws::SPI_READ_FAILURE;
|
|
|
|
}
|
|
|
|
readIdx++;
|
2023-02-15 19:27:19 +01:00
|
|
|
}
|
2023-02-17 02:10:08 +01:00
|
|
|
|
2023-02-15 17:02:22 +01:00
|
|
|
#if FSFW_HAL_SPI_WIRETAPPING == 1
|
2023-02-15 19:27:19 +01:00
|
|
|
sif::info << "RW start marker detected" << std::endl;
|
2023-02-15 17:02:22 +01:00
|
|
|
#endif
|
|
|
|
|
2023-02-15 19:27:19 +01:00
|
|
|
size_t decodedFrameLen = 0;
|
2023-02-19 12:25:26 +01:00
|
|
|
MutexGuard mg(rwCookie.bufLock);
|
2023-02-15 17:02:22 +01:00
|
|
|
|
2023-02-15 19:27:19 +01:00
|
|
|
while (decodedFrameLen < maxReplyLen) {
|
|
|
|
// First byte already read in
|
|
|
|
if (decodedFrameLen != 0) {
|
|
|
|
byteRead = 0;
|
|
|
|
if (read(fd, &byteRead, 1) != 1) {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::error << "RwPollingTask: Read failed" << std::endl;
|
2023-02-15 19:27:19 +01:00
|
|
|
result = rws::SPI_READ_FAILURE;
|
|
|
|
break;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-15 19:27:19 +01:00
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
|
2023-02-15 19:58:32 +01:00
|
|
|
if (byteRead == rws::FRAME_DELIMITER) {
|
2023-02-15 19:27:19 +01:00
|
|
|
// Reached end of frame
|
|
|
|
break;
|
|
|
|
} else if (byteRead == 0x7D) {
|
|
|
|
if (read(fd, &byteRead, 1) != 1) {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::error << "RwPollingTask: Read failed" << std::endl;
|
2023-02-15 19:27:19 +01:00
|
|
|
result = rws::SPI_READ_FAILURE;
|
2023-02-15 17:02:22 +01:00
|
|
|
break;
|
2023-02-15 19:27:19 +01:00
|
|
|
}
|
|
|
|
if (byteRead == 0x5E) {
|
|
|
|
*(replyBuf + decodedFrameLen) = 0x7E;
|
|
|
|
decodedFrameLen++;
|
|
|
|
continue;
|
|
|
|
} else if (byteRead == 0x5D) {
|
|
|
|
*(replyBuf + decodedFrameLen) = 0x7D;
|
2023-02-15 17:02:22 +01:00
|
|
|
decodedFrameLen++;
|
|
|
|
continue;
|
2023-02-15 19:27:19 +01:00
|
|
|
} else {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::error << "RwPollingTask: Invalid substitute" << std::endl;
|
2023-02-15 19:27:19 +01:00
|
|
|
result = rws::INVALID_SUBSTITUTE;
|
|
|
|
break;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-15 19:27:19 +01:00
|
|
|
} else {
|
|
|
|
*(replyBuf + decodedFrameLen) = byteRead;
|
|
|
|
decodedFrameLen++;
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
|
2023-02-15 19:27:19 +01:00
|
|
|
// Check end marker.
|
|
|
|
/**
|
|
|
|
* There might be the unlikely case that each byte in a get-telemetry reply has been
|
|
|
|
* replaced by its substitute. Then the next byte must correspond to the end sign 0x7E.
|
|
|
|
* Otherwise there might be something wrong.
|
|
|
|
*/
|
|
|
|
if (decodedFrameLen == maxReplyLen) {
|
|
|
|
if (read(fd, &byteRead, 1) != 1) {
|
|
|
|
sif::error << "rwSpiCallback::spiCallback: Failed to read last byte" << std::endl;
|
|
|
|
result = rws::SPI_READ_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
2023-02-15 19:58:32 +01:00
|
|
|
if (byteRead != rws::FRAME_DELIMITER) {
|
|
|
|
sif::error << "rwSpiCallback::spiCallback: Missing end sign "
|
|
|
|
<< static_cast<int>(rws::FRAME_DELIMITER) << std::endl;
|
2023-02-15 19:27:19 +01:00
|
|
|
decodedFrameLen--;
|
|
|
|
result = rws::MISSING_END_SIGN;
|
|
|
|
break;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 19:27:19 +01:00
|
|
|
result = returnvalue::OK;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-15 19:27:19 +01:00
|
|
|
|
|
|
|
pullCsHigh(gpioId, gpioIF);
|
|
|
|
closeSpi(fd);
|
|
|
|
return result;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:42 +01:00
|
|
|
ReturnValue_t RwPollingTask::writeOneRwCmd(uint8_t rwIdx, int fd) {
|
|
|
|
ReturnValue_t result = sendOneMessage(fd, *rwCookies[rwIdx]);
|
2023-02-15 17:02:22 +01:00
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-16 17:57:21 +01:00
|
|
|
ReturnValue_t RwPollingTask::readAllRws(DeviceCommandId_t id) {
|
2023-02-16 14:32:23 +01:00
|
|
|
// SPI dev will be opened in readNextReply on demand.
|
2023-02-15 17:02:22 +01:00
|
|
|
for (unsigned idx = 0; idx < rwCookies.size(); idx++) {
|
2023-02-16 14:32:23 +01:00
|
|
|
if (((id == rws::SET_SPEED) and !rwCookies[idx]->setSpeed) or skipCommandingForRw[idx]) {
|
2023-02-16 14:10:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
uint8_t* replyBuf;
|
2023-02-15 19:16:42 +01:00
|
|
|
size_t maxReadLen = idAndIdxToReadBuffer(id, idx, &replyBuf);
|
2023-02-17 02:10:08 +01:00
|
|
|
ReturnValue_t result = readNextReply(*rwCookies[idx], replyBuf + 1, maxReadLen);
|
|
|
|
if (result == returnvalue::OK) {
|
|
|
|
// The first byte is always a flag which shows whether the value was read
|
|
|
|
// properly at least once.
|
|
|
|
replyBuf[0] = true;
|
|
|
|
}
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
2023-02-16 14:32:23 +01:00
|
|
|
// SPI is closed in readNextReply as well.
|
2023-02-15 17:02:22 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:42 +01:00
|
|
|
size_t RwPollingTask::idAndIdxToReadBuffer(DeviceCommandId_t id, uint8_t rwIdx, uint8_t** ptr) {
|
|
|
|
uint8_t* rawStart = rwCookies[rwIdx]->replyBuf.data();
|
|
|
|
RwReplies replies(rawStart);
|
|
|
|
switch (id) {
|
|
|
|
case (rws::GET_RW_STATUS): {
|
|
|
|
*ptr = replies.rwStatusReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
case (rws::SET_SPEED): {
|
|
|
|
*ptr = replies.setSpeedReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
case (rws::CLEAR_LAST_RESET_STATUS): {
|
|
|
|
*ptr = replies.clearLastResetStatusReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
case (rws::GET_LAST_RESET_STATUS): {
|
|
|
|
*ptr = replies.getLastResetStatusReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
case (rws::GET_TEMPERATURE): {
|
|
|
|
*ptr = replies.readTemperatureReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
case (rws::GET_TM): {
|
|
|
|
*ptr = replies.hkDataReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
case (rws::INIT_RW_CONTROLLER): {
|
|
|
|
*ptr = replies.initRwControllerReply;
|
2023-02-16 14:10:59 +01:00
|
|
|
break;
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
sif::error << "no reply buffer for rw command " << id << std::endl;
|
|
|
|
*ptr = replies.dummyPointer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 14:10:59 +01:00
|
|
|
return rws::idToPacketLen(id);
|
2023-02-15 19:16:42 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 14:50:57 +01:00
|
|
|
void RwPollingTask::fillSpecialRequestArray() {
|
|
|
|
for (unsigned idx = 0; idx < rwCookies.size(); idx++) {
|
|
|
|
if (skipCommandingForRw[idx]) {
|
|
|
|
specialRequestIds[idx] = DeviceHandlerIF::NO_COMMAND_ID;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (rwCookies[idx]->specialRequest) {
|
|
|
|
case (rws::SpecialRwRequest::GET_TM): {
|
|
|
|
specialRequestIds[idx] = rws::GET_TM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (rws::SpecialRwRequest::INIT_RW_CONTROLLER): {
|
|
|
|
specialRequestIds[idx] = rws::INIT_RW_CONTROLLER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
specialRequestIds[idx] = DeviceHandlerIF::NO_COMMAND_ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RwPollingTask::handleSpecialRequests() {
|
|
|
|
int fd = 0;
|
|
|
|
fillSpecialRequestArray();
|
|
|
|
ReturnValue_t result = openSpi(O_RDWR, fd);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (unsigned idx = 0; idx < rwCookies.size(); idx++) {
|
|
|
|
if (specialRequestIds[idx] == DeviceHandlerIF::NO_COMMAND_ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prepareSimpleCommand(specialRequestIds[idx]);
|
|
|
|
writeOneRwCmd(idx, fd);
|
|
|
|
}
|
|
|
|
closeSpi(fd);
|
|
|
|
usleep(rws::SPI_REPLY_DELAY);
|
|
|
|
for (unsigned idx = 0; idx < rwCookies.size(); idx++) {
|
|
|
|
if (specialRequestIds[idx] == DeviceHandlerIF::NO_COMMAND_ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint8_t* replyBuf;
|
|
|
|
size_t maxReadLen = idAndIdxToReadBuffer(specialRequestIds[idx], idx, &replyBuf);
|
2023-02-19 12:25:26 +01:00
|
|
|
result = readNextReply(*rwCookies[idx], replyBuf, maxReadLen);
|
|
|
|
if (result == returnvalue::OK) {
|
|
|
|
// The first byte is always a flag which shows whether the value was read
|
|
|
|
// properly at least once.
|
|
|
|
replyBuf[0] = true;
|
|
|
|
}
|
2023-02-16 14:50:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 02:10:08 +01:00
|
|
|
void RwPollingTask::setAllReadFlagsFalse() {
|
|
|
|
for (auto& rwCookie : rwCookies) {
|
|
|
|
RwReplies replies(rwCookie->replyBuf.data());
|
|
|
|
replies.getLastResetStatusReply[0] = false;
|
|
|
|
replies.clearLastResetStatusReply[0] = false;
|
|
|
|
replies.hkDataReply[0] = false;
|
|
|
|
replies.readTemperatureReply[0] = false;
|
|
|
|
replies.rwStatusReply[0] = false;
|
|
|
|
replies.setSpeedReply[0] = false;
|
|
|
|
replies.initRwControllerReply[0] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:02:22 +01:00
|
|
|
// This closes the SPI
|
|
|
|
void RwPollingTask::closeSpi(int fd) {
|
|
|
|
// This will perform the function to close the SPI
|
|
|
|
close(fd);
|
|
|
|
// The SPI is now closed.
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:42 +01:00
|
|
|
ReturnValue_t RwPollingTask::sendOneMessage(int fd, RwCookie& rwCookie) {
|
2023-02-15 17:02:22 +01:00
|
|
|
gpioId_t gpioId = rwCookie.getChipSelectPin();
|
|
|
|
if (spiLock == nullptr) {
|
|
|
|
sif::debug << "rwSpiCallback::spiCallback: Mutex or GPIO interface invalid" << std::endl;
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
2023-02-15 19:16:42 +01:00
|
|
|
// Add datalinklayer like specified in the datasheet.
|
|
|
|
size_t lenToSend = 0;
|
2023-02-15 19:58:32 +01:00
|
|
|
rws::encodeHdlc(writeBuffer.data(), writeLen, encodedBuffer.data(), lenToSend);
|
2023-02-17 02:10:08 +01:00
|
|
|
pullCsLow(gpioId, gpioIF);
|
2023-02-15 19:16:42 +01:00
|
|
|
if (write(fd, encodedBuffer.data(), lenToSend) != static_cast<ssize_t>(lenToSend)) {
|
2023-02-15 17:02:22 +01:00
|
|
|
sif::error << "rwSpiCallback::spiCallback: Write failed!" << std::endl;
|
2023-02-15 19:27:19 +01:00
|
|
|
pullCsHigh(gpioId, gpioIF);
|
2023-02-15 17:02:22 +01:00
|
|
|
return rws::SPI_WRITE_FAILURE;
|
|
|
|
}
|
2023-02-15 19:27:19 +01:00
|
|
|
pullCsHigh(gpioId, gpioIF);
|
2023-02-15 17:02:22 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:51:30 +01:00
|
|
|
ReturnValue_t RwPollingTask::pullCsLow(gpioId_t gpioId, GpioIF& gpioIF) {
|
2023-02-15 17:02:22 +01:00
|
|
|
ReturnValue_t result = spiLock->lockMutex(TIMEOUT_TYPE, TIMEOUT_MS);
|
|
|
|
if (result != returnvalue::OK) {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::debug << "RwPollingTask::pullCsLow: Failed to lock mutex" << std::endl;
|
2023-02-15 17:02:22 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// Pull SPI CS low. For now, no support for active high given
|
|
|
|
if (gpioId != gpio::NO_GPIO) {
|
2023-02-17 12:19:53 +01:00
|
|
|
result = gpioIF.pullLow(gpioId);
|
2023-02-15 17:02:22 +01:00
|
|
|
if (result != returnvalue::OK) {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::error << "RwPollingTask::pullCsLow: Failed to pull chip select low" << std::endl;
|
2023-02-15 17:02:22 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:27:19 +01:00
|
|
|
void RwPollingTask::pullCsHigh(gpioId_t gpioId, GpioIF& gpioIF) {
|
2023-02-15 17:02:22 +01:00
|
|
|
if (gpioId != gpio::NO_GPIO) {
|
|
|
|
if (gpioIF.pullHigh(gpioId) != returnvalue::OK) {
|
|
|
|
sif::error << "closeSpi: Failed to pull chip select high" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (spiLock->unlockMutex() != returnvalue::OK) {
|
2023-02-16 17:57:21 +01:00
|
|
|
sif::error << "RwPollingTask::pullCsHigh: Failed to unlock mutex" << std::endl;
|
2023-02-15 17:02:22 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RwPollingTask::prepareSimpleCommand(DeviceCommandId_t id) {
|
|
|
|
writeBuffer[0] = static_cast<uint8_t>(id);
|
|
|
|
uint16_t crc = CRC::crc16ccitt(writeBuffer.data(), 1, 0xFFFF);
|
|
|
|
writeBuffer[1] = static_cast<uint8_t>(crc & 0xFF);
|
|
|
|
writeBuffer[2] = static_cast<uint8_t>(crc >> 8 & 0xFF);
|
2023-02-15 19:16:42 +01:00
|
|
|
writeLen = 3;
|
2023-02-15 17:02:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t RwPollingTask::prepareSetSpeedCmd(uint8_t rwIdx) {
|
|
|
|
writeBuffer[0] = static_cast<uint8_t>(rws::SET_SPEED);
|
|
|
|
uint8_t* serPtr = writeBuffer.data() + 1;
|
|
|
|
int32_t speedToSet = 0;
|
|
|
|
uint16_t rampTimeToSet = 10;
|
|
|
|
{
|
|
|
|
MutexGuard mg(ipcLock);
|
|
|
|
speedToSet = rwCookies[rwIdx]->currentRwSpeed;
|
|
|
|
rampTimeToSet = rwCookies[rwIdx]->currentRampTime;
|
|
|
|
}
|
2023-02-17 02:10:08 +01:00
|
|
|
size_t serLen = 1;
|
2023-02-15 17:02:22 +01:00
|
|
|
SerializeAdapter::serialize(&speedToSet, &serPtr, &serLen, writeBuffer.size(),
|
|
|
|
SerializeIF::Endianness::LITTLE);
|
|
|
|
SerializeAdapter::serialize(&rampTimeToSet, &serPtr, &serLen, writeBuffer.size(),
|
|
|
|
SerializeIF::Endianness::LITTLE);
|
|
|
|
|
|
|
|
uint16_t crc = CRC::crc16ccitt(writeBuffer.data(), 7, 0xFFFF);
|
|
|
|
writeBuffer[7] = static_cast<uint8_t>(crc & 0xFF);
|
|
|
|
writeBuffer[8] = static_cast<uint8_t>((crc >> 8) & 0xFF);
|
2023-02-15 19:16:42 +01:00
|
|
|
writeLen = 9;
|
2023-02-15 17:02:22 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|