Skip to content

Commit

Permalink
Transport::commands method
Browse files Browse the repository at this point in the history
  • Loading branch information
pkubanek committed Oct 3, 2024
1 parent ab8a629 commit e239274
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 62 deletions.
7 changes: 5 additions & 2 deletions include/Transports/FPGASerialDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ class FPGASerialDevice : public Transport {
*/
FPGASerialDevice(uint32_t fpga_session, int write_fifo, int read_fifo);

void write(const char* buf, size_t len) override;
void write(const unsigned char* buf, size_t len) override;

std::vector<uint8_t> read(size_t len, const std::chrono::duration<long int>& timeout,
LSST::cRIO::Thread* calling_thread = NULL) override;

virtual void telemetry(uint64_t& write_bytes, uint64_t& read_bytes) override;
void commands(Modbus::BusList& bus_list, const std::chrono::duration<long int>& timeout,
LSST::cRIO::Thread* calling_thread = NULL) override;

void telemetry(uint64_t& write_bytes, uint64_t& read_bytes) override;

private:
uint32_t _fpga_session;
Expand Down
31 changes: 18 additions & 13 deletions include/Transports/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <vector>

#include <cRIO/Thread.h>
#include <Modbus/BusList.h>

namespace Transports {

Expand Down Expand Up @@ -57,32 +58,36 @@ class Transport {
*
* @throw std::runtime_error on failure
*/
virtual void write(const char* buf, size_t len) = 0;

/**
* Send data to the other end of the transport.
*
* @param data data to send
*
* @throw std::runtime_error on failure
*/
virtual void write(std::vector<uint8_t> buf) {
write(reinterpret_cast<const char*>(buf.data()), buf.size());
}
virtual void write(const unsigned char* buf, size_t len) = 0;

/**
* Reads data from transport. Returns data available until timeout expires. Can be interrupted by notifing
* interrupt_condition.
*
* @param len expected len to read. If timeout expires and data aren't available, returns what was read
* @param timeout maximal time to wait for data
* @param interrupt_condition if not NULL, shall be used for waiting
* @param calling_thread thread calling the read. Used for waits.
*
* @throw std::runtime_error on failure
*/
virtual std::vector<uint8_t> read(size_t len, const std::chrono::duration<long int>& timeout,
LSST::cRIO::Thread* calling_thread = NULL) = 0;

/**
* Sends MPU commands to command FIFO. MPU command buffer must be filled
* before calling this method. Read returns for commands, ask MPU object to process those.
*
* @note This function shall live in transport, as this seems to be the
* only way to combine traditional serial port approach with cRIO DIO based
* mass processing.
*
* @param bus_list Modbus bus list containing the commands and processing
* @param timeout timeout to sleep before reading
* @param calling_thread thread calling the read. Used for waits.
*/
virtual void commands(Modbus::BusList& bus_list, const std::chrono::duration<long int>& timeout,
LSST::cRIO::Thread* calling_thread = NULL) = 0;

/**
* Retrieve transport telemetry.
*
Expand Down
30 changes: 0 additions & 30 deletions include/cRIO/FPGA.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,36 +112,6 @@ class FPGA : public SimpleFPGA {
*/
virtual void ilcCommands(ILC::ILCBusList& ilc, int32_t timeout);

/**
* Sends MPU commands to command FIFO. MPU command buffer must be filled
* before calling this method. Read outs data if data output was specified in MPU commands. If you would
* like to split commanding and reading code, please use writeMPUFIFO and readMPUFIFO.
*
* @param mpu Modbus Processing Unit containing the commands.
* @param timeout timeout to sleep before reading. Default to 500ms.
*/
void mpuCommands(MPU& mpu, const std::chrono::duration<double>& timeout = 500ms);

/**
* Commands FPGA to write to MPU commands buffer.
*
* @param mpu Modbus Processing Unit that shall be written
* @param data data to write to the MPU command buffer
* @param timeout timeout in milliseconds
*/
virtual void writeMPUFIFO(MPU& mpu, const std::vector<uint8_t>& data, uint32_t timeout) = 0;

/**
* Commands FPGA to copy MPU output FIFO to FPGA-C/C++ output FIFO. This
* method will dump data from MPU to FIFO which C/C++ can read, and reads
* the data.
*
* @param mpu Modbus Processing Unit to read the data
*
* @return data read from the port
*/
virtual std::vector<uint8_t> readMPUFIFO(MPU& mpu) = 0;

/**
* Writes buffer to command FIFO. Command FIFO is processed in
* CommandMultiplexer Vi.
Expand Down
16 changes: 0 additions & 16 deletions src/LSST/cRIO/FPGA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,5 @@ void FPGA::ilcCommands(ILC::ILCBusList &ilc, int32_t timeout) {
reportTime(beginTs, endTs);
}

void FPGA::mpuCommands(MPU &mpu, const std::chrono::duration<double> &timeout) {
auto end = std::chrono::steady_clock::now() + timeout;

for (auto cmd : mpu) {
writeMPUFIFO(mpu, cmd.buffer, 0);

// read reply
auto answer = readMPUFIFO(mpu);
if (answer.empty()) {
throw std::runtime_error(fmt::format("Empty answer to {}", Modbus::hexDump(cmd.buffer)));
}
mpu.parse(answer);
mpu.reset();
}
}

} // namespace cRIO
} // namespace LSST
17 changes: 16 additions & 1 deletion src/Transports/FPGASerialDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using namespace Transports;
FPGASerialDevice::FPGASerialDevice(uint32_t fpga_session, int write_fifo, int read_fifo)
: _fpga_session(fpga_session), _write_fifo(write_fifo), _read_fifo(read_fifo) {}

void FPGASerialDevice::write(const char* buf, size_t len) {
void FPGASerialDevice::write(const unsigned char* buf, size_t len) {
assert(len < 255);

uint8_t header[2] = {1, static_cast<uint8_t>(len)};
Expand Down Expand Up @@ -96,6 +96,21 @@ std::vector<uint8_t> FPGASerialDevice::read(size_t len, const std::chrono::durat
return ret;
}

void FPGASerialDevice::commands(Modbus::BusList& bus_list, const std::chrono::duration<long int>& timeout,
Thread* calling_thread) {
for (auto cmd : bus_list) {
write(cmd.buffer.data(), cmd.buffer.size());

// read reply
auto answer = read(4, timeout, calling_thread);
if (answer.empty()) {
throw std::runtime_error(fmt::format("Empty answer to {}", Modbus::hexDump(cmd.buffer)));
}
bus_list.parse(answer);
bus_list.reset();
}
}

void FPGASerialDevice::telemetry(uint64_t& write_bytes, uint64_t& read_bytes) {
uint8_t req = 0;
NiThrowError("Reading FIFO requesting telemetry",
Expand Down

0 comments on commit e239274

Please sign in to comment.