eive-obsw/linux/ipcore/PapbVcInterface.cpp

148 lines
4.2 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-06-26 17:08:39 +02:00
PapbVcInterface::PapbVcInterface(LinuxLibgpioIF* gpioComIF, gpioId_t papbEmptyId,
2023-10-13 15:10:52 +02:00
std::string uioFile, int mapNum, size_t maxPacketSize)
: gpioComIF(gpioComIF),
papbEmptyId(papbEmptyId),
packetBuf(maxPacketSize),
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-06-26 17:08:39 +02:00
UioMapper::Permissions::READ_WRITE);
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
}
2023-10-13 09:20:51 +02:00
ReturnValue_t PapbVcInterface::write(const uint8_t* data, size_t size, size_t& writtenSize) {
// There are no packets smaller than 4, this is considered a configuration error.
if (size < 4) {
2023-11-23 17:20:55 +01:00
sif::warning << "PapbVcInterface::write: Passed packet smaller than 4 bytes" << std::endl;
return returnvalue::FAILED;
}
2023-10-13 15:24:06 +02:00
// The user must call advance until completion before starting a new packet transfer.
if (writeActiveStatus) {
2023-10-13 15:10:52 +02:00
return IS_BUSY;
2023-10-13 09:39:50 +02:00
}
2023-10-13 15:10:52 +02:00
if (size > packetBuf.capacity()) {
sif::error << "PapbVcInterface: Packet with size " << size << " larger than maximum configured"
<< " byte size " << packetBuf.capacity() << std::endl;
return returnvalue::FAILED;
}
std::memcpy(packetBuf.data(), data, size);
currentPacketSize = size;
currentPacketIndex = 0;
2023-06-26 17:08:39 +02:00
if (pollReadyForPacket()) {
startPacketTransfer(ByteWidthCfg::ONE);
2023-03-23 18:31:47 +01:00
} else {
return DirectTmSinkIF::IS_BUSY;
}
2023-10-13 15:10:52 +02:00
return advanceWrite(writtenSize);
2021-09-19 12:27:48 +02:00
}
void PapbVcInterface::startPacketTransfer(ByteWidthCfg initWidth) {
*vcBaseReg = CONFIG_DATA_INPUT | initWidth;
2023-10-13 16:25:40 +02:00
writeActiveStatus = true;
}
2021-09-19 12:27:48 +02:00
2023-06-26 17:08:39 +02:00
bool PapbVcInterface::pollReadyForPacket() const {
// 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.
uint32_t reg = *vcBaseReg;
return (reg >> 6) & 0b1;
2021-09-19 12:27:48 +02:00
}
2023-10-13 15:10:52 +02:00
ReturnValue_t PapbVcInterface::advanceWrite(size_t& writtenSize) {
2023-10-13 15:24:06 +02:00
if (!writeActiveStatus) {
return NO_WRITE_ACTIVE;
}
2023-10-13 09:27:24 +02:00
if (not pollReadyForPacket()) {
2023-10-13 15:10:52 +02:00
return IS_BUSY;
2023-10-13 09:27:24 +02:00
}
2023-10-18 12:24:22 +02:00
while (currentPacketIndex < currentPacketSize) {
2023-10-13 09:20:51 +02:00
if (not pollReadyForOctet(MAX_BUSY_POLLS)) {
if (not pollReadyForPacket()) {
return PARTIALLY_WRITTEN;
}
abortPacketTransfer();
return returnvalue::FAILED;
}
2023-10-18 12:24:22 +02:00
*(vcBaseReg + DATA_REG_OFFSET) = static_cast<uint32_t>(packetBuf[currentPacketIndex++]);
2023-10-13 15:10:52 +02:00
writtenSize++;
2023-10-13 09:20:51 +02:00
}
if (not pollReadyForOctet(MAX_BUSY_POLLS)) {
2023-11-23 17:22:36 +01:00
if (not pollReadyForPacket()) {
return PARTIALLY_WRITTEN;
}
2023-10-13 09:20:51 +02:00
abortPacketTransfer();
return returnvalue::FAILED;
}
completePacketTransfer();
return returnvalue::OK;
}
2023-10-13 15:24:06 +02:00
bool PapbVcInterface::writeActive() const { return writeActiveStatus; }
2023-06-26 17:08:39 +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-06-26 17:08:39 +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-06-26 17:08:39 +02:00
return true;
}
2023-06-26 17:08:39 +02:00
return false;
2021-09-19 12:27:48 +02:00
}
2023-06-26 17:08:39 +02:00
bool PapbVcInterface::isBusy() const { return not pollReadyForPacket(); }
2023-03-23 18:31:47 +01:00
void PapbVcInterface::cancelTransfer() { abortPacketTransfer(); }
2023-03-10 18:04:04 +01:00
2023-06-29 00:33:58 +02:00
inline bool PapbVcInterface::pollReadyForOctet(uint32_t maxCycles) const {
uint32_t reg;
uint32_t idx = 0;
while (idx < maxCycles) {
reg = *vcBaseReg;
// Busy bit.
if (not((reg >> 5) & 0b1)) {
return true;
}
idx++;
}
return false;
}
2023-10-13 15:24:06 +02:00
void PapbVcInterface::abortPacketTransfer() {
*vcBaseReg = CONFIG_ABORT;
writeActiveStatus = false;
currentPacketIndex = 0;
currentPacketSize = 0;
}
void PapbVcInterface::completePacketTransfer() {
*vcBaseReg = CONFIG_END;
writeActiveStatus = false;
currentPacketIndex = 0;
currentPacketSize = 0;
}