eive-obsw/linux/ipcore/PapbVcInterface.cpp

173 lines
5.8 KiB
C++
Raw Normal View History

2022-01-24 16:33:22 +01:00
#include <fsfw_hal/linux/uio/UioMapper.h>
#include <linux/ipcore/PapbVcInterface.h>
2023-03-23 15:43:14 +01:00
#include <unistd.h>
2022-01-26 17:59:31 +01:00
#include <cstring>
2023-03-27 17:42:54 +02:00
#include <ctime>
2021-09-19 12:27:48 +02:00
2023-03-27 21:39:37 +02:00
#include "fsfw/serviceinterface/ServiceInterface.h"
2023-04-26 11:02:24 +02:00
PapbVcInterface::PapbVcInterface(LinuxLibgpioIF* gpioComIF, gpioId_t papbEmptyId,
std::string uioFile, int mapNum)
: gpioComIF(gpioComIF), papbEmptyId(papbEmptyId), uioFile(std::move(uioFile)), mapNum(mapNum) {}
2021-09-19 12:27:48 +02:00
PapbVcInterface::~PapbVcInterface() {}
2021-09-19 12:27:48 +02:00
2022-01-24 16:33:22 +01:00
ReturnValue_t PapbVcInterface::initialize() {
UioMapper uioMapper(uioFile, mapNum);
2023-03-30 23:52:37 +02:00
ReturnValue_t result = uioMapper.getMappedAdress(const_cast<uint32_t**>(&vcBaseReg),
2023-03-31 01:14:59 +02:00
UioMapper::Permissions::WRITE_ONLY);
2023-03-23 15:43:14 +01:00
if (result != returnvalue::OK) {
return result;
}
return returnvalue::OK;
2021-09-19 12:27:48 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t PapbVcInterface::write(const uint8_t* data, size_t size) {
// There are no packets smaller than 4, this is considered a configuration error.
if (size < 4) {
return returnvalue::FAILED;
}
2023-03-31 15:12:05 +02:00
if (pollInterfaceReadiness(0, true) == returnvalue::OK) {
startPacketTransfer(ByteWidthCfg::ONE);
2023-03-23 18:31:47 +01:00
} else {
return DirectTmSinkIF::IS_BUSY;
}
2023-03-31 17:35:53 +02:00
// TODO: This should work but does not.. :(
// size_t idx = 0;
// while (idx < size) {
//
// nanosleep(&BETWEEN_POLL_DELAY, &remDelay);
// if ((size - idx) < 4) {
// *vcBaseReg = CONFIG_DATA_INPUT | (size - idx - 1);
// usleep(1);
// }
// if (pollPapbBusySignal(2) == returnvalue::OK) {
// // vcBaseReg + DATA_REG_OFFSET + 3 = static_cast<uint8_t>(data + idx);
// // vcBaseReg + DATA_REG_OFFSET + 2 = static_cast<uint8_t>(data + idx + 1);
// // vcBaseReg + DATA_REG_OFFSET + 1 = static_cast<uint8_t>(data + idx + 2);
// // vcBaseReg + DATA_REG_OFFSET = static_cast<uint8_t>(data + idx + 3);
//
// // std::memcpy((vcBaseReg + DATA_REG_OFFSET), data + idx , nextWriteSize);
// *(vcBaseReg + DATA_REG_OFFSET) = *reinterpret_cast<const uint32_t*>(data + idx);
// //uint8_t* byteReg = reinterpret_cast<uint8_t*>(vcBaseReg + DATA_REG_OFFSET);
//
// //byteReg[0] = data[idx];
// //byteReg[1] = data[idx];
// } else {
// abortPacketTransfer();
// return returnvalue::FAILED;
// }
// // TODO: Change this after the bugfix. Right now, the PAPB ignores the content of the byte
// // width configuration.5
// // It's okay to increment by a larger amount for the last segment here, loop will be over
// // in any case.
// idx += 4;
// }
for (size_t idx = 0; idx < size; idx++) {
2023-03-28 21:45:04 +02:00
// This delay is super-important, DO NOT REMOVE!
2023-03-31 15:12:05 +02:00
// Polling the GPIO or the config register too often messes up the scheduler.
2023-03-28 22:04:40 +02:00
// TODO: Maybe this should not be done like this. It would be better if there was a custom
// FPGA module which can accept packets and then takes care of dumping that packet into
// the PTME. DMA would be an ideal solution for this.
2023-03-28 21:45:04 +02:00
nanosleep(&BETWEEN_POLL_DELAY, &remDelay);
2023-03-31 15:12:05 +02:00
if (pollInterfaceReadiness(2, false) == returnvalue::OK) {
2023-03-23 15:43:14 +01:00
*(vcBaseReg + DATA_REG_OFFSET) = static_cast<uint32_t>(data[idx]);
} else {
2023-03-23 15:43:14 +01:00
abortPacketTransfer();
2023-03-23 18:31:47 +01:00
return returnvalue::FAILED;
2022-01-24 16:33:22 +01:00
}
}
2023-03-28 21:45:04 +02:00
nanosleep(&BETWEEN_POLL_DELAY, &remDelay);
2023-03-31 15:12:05 +02:00
if (pollInterfaceReadiness(2, false) == returnvalue::OK) {
2023-03-23 15:43:14 +01:00
completePacketTransfer();
2023-03-23 18:31:47 +01:00
} else {
abortPacketTransfer();
return returnvalue::FAILED;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-09-19 12:27:48 +02:00
}
void PapbVcInterface::startPacketTransfer(ByteWidthCfg initWidth) {
*vcBaseReg = CONFIG_DATA_INPUT | initWidth;
}
2021-09-19 12:27:48 +02:00
2023-03-23 15:43:14 +01:00
void PapbVcInterface::completePacketTransfer() { *vcBaseReg = CONFIG_END; }
2021-09-19 12:27:48 +02:00
2023-03-31 15:12:05 +02:00
ReturnValue_t PapbVcInterface::pollInterfaceReadiness(uint32_t maxPollRetries,
2023-04-26 11:02:24 +02:00
bool checkReadyForPacketState) const {
2023-03-20 14:31:10 +01:00
uint32_t busyIdx = 0;
2023-03-31 17:41:01 +02:00
nextDelay.tv_nsec = FIRST_DELAY_PAPB_POLLING_NS;
2023-03-20 14:31:10 +01:00
while (true) {
2023-03-30 23:52:37 +02:00
// Check if PAPB interface is ready to receive data. Use the configuration register for this.
// Bit 5, see PTME ptme_001_01-0-7-r2 Table 31.
2023-03-31 15:12:05 +02:00
uint32_t reg = *vcBaseReg;
bool busy = (reg >> 5) & 0b1;
2023-04-26 11:02:24 +02:00
bool readyForPacket = (reg >> 6) & 0b1;
if (checkReadyForPacketState) {
if (not busy and readyForPacket) {
return returnvalue::OK;
} else if (not busy and not readyForPacket) {
return PAPB_BUSY;
}
} else if (not busy) {
2023-03-20 14:31:10 +01:00
return returnvalue::OK;
}
2023-03-23 18:31:47 +01:00
busyIdx++;
if (busyIdx >= maxPollRetries) {
2023-03-20 14:31:10 +01:00
return PAPB_BUSY;
}
2023-03-23 18:31:47 +01:00
2023-03-27 17:43:52 +02:00
// Ignore signal handling here for now.
nanosleep(&nextDelay, &remDelay);
2023-03-27 18:13:00 +02:00
// Adaptive delay.
2023-03-29 17:38:38 +02:00
if (nextDelay.tv_nsec * 2 <= MAX_DELAY_PAPB_POLLING_NS) {
2023-03-27 17:42:54 +02:00
nextDelay.tv_nsec *= 2;
2023-03-27 17:02:02 +02:00
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-09-19 12:27:48 +02:00
}
2023-04-26 11:02:24 +02:00
bool PapbVcInterface::isVcInterfaceBufferEmpty() {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-05-04 14:08:27 +02:00
gpio::Levels papbEmptyState = gpio::Levels::HIGH;
2022-01-24 16:33:22 +01:00
2022-05-04 14:08:27 +02:00
result = gpioComIF->readGpio(papbEmptyId, papbEmptyState);
2021-09-19 12:27:48 +02:00
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2023-04-26 11:02:24 +02:00
sif::error << "PapbVcInterface::isVcInterfaceBufferEmpty: Failed to read papb empty signal"
<< std::endl;
return true;
}
2022-05-04 14:08:27 +02:00
if (papbEmptyState == gpio::Levels::HIGH) {
2023-04-26 11:02:24 +02:00
return true;
}
2023-04-26 11:02:24 +02:00
return false;
2021-09-19 12:27:48 +02:00
}
2023-03-31 15:12:05 +02:00
bool PapbVcInterface::isBusy() const { return pollInterfaceReadiness(0, true) == PAPB_BUSY; }
2023-03-23 18:31:47 +01:00
void PapbVcInterface::cancelTransfer() { abortPacketTransfer(); }
2023-03-10 18:04:04 +01:00
2021-09-19 12:27:48 +02:00
ReturnValue_t PapbVcInterface::sendTestFrame() {
/** Size of one complete transfer frame data field amounts to 1105 bytes */
uint8_t testPacket[1105];
2021-09-19 12:27:48 +02:00
/** Fill one test packet */
for (int idx = 0; idx < 1105; idx++) {
testPacket[idx] = static_cast<uint8_t>(idx & 0xFF);
}
2021-09-19 12:27:48 +02:00
ReturnValue_t result = write(testPacket, 1105);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
return result;
}
2021-09-19 12:27:48 +02:00
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-09-19 12:27:48 +02:00
}
2023-03-23 15:43:14 +01:00
void PapbVcInterface::abortPacketTransfer() { *vcBaseReg = CONFIG_ABORT; }