-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transports classes to separate data transport from FPGA.
- Loading branch information
Showing
13 changed files
with
4,084 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Serial communication through FPGA FIFOs. | ||
* | ||
* Developed for the Vera C. Rubin Observatory Telescope & Site Software Systems. | ||
* This product includes software developed by the Vera C.Rubin Observatory Project | ||
* (https://www.lsst.org). See the COPYRIGHT file at the top-level directory of | ||
* this distribution for details of code ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef __Transports_FPGASerialDevice__ | ||
#define __Transports_FPGASerialDevice__ | ||
|
||
#include <chrono> | ||
|
||
#include "Transport.h" | ||
|
||
namespace Transports { | ||
|
||
/** | ||
* Communicate with various serial devices hooked on cRIO serial ports. | ||
*/ | ||
class FPGASerialDevice : public Transport { | ||
public: | ||
/** | ||
* Construct object to communicate with serial device connected to cRIO. | ||
* The FPGA in cRIO shall use SerialDevice.vi to communicate with the device. | ||
* | ||
* @param fpga_session | ||
* @param write_fifo | ||
* @param read_fifo | ||
*/ | ||
FPGASerialDevice(uint32_t fpga_session, int write_fifo, int read_fifo); | ||
|
||
void write(const 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; | ||
|
||
private: | ||
uint32_t _fpga_session; | ||
int _write_fifo; | ||
int _read_fifo; | ||
}; | ||
|
||
} // namespace Transports | ||
|
||
#endif // !__Transports_FPGASerialDevice__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Transport base class for communication. | ||
* | ||
* Developed for the Vera C. Rubin Observatory Telescope & Site Software Systems. | ||
* This product includes software developed by the Vera C.Rubin Observatory Project | ||
* (https://www.lsst.org). See the COPYRIGHT file at the top-level directory of | ||
* this distribution for details of code ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef __Transports_Transport__ | ||
#define __Transports_Transport__ | ||
|
||
#include <vector> | ||
|
||
#include <cRIO/Thread.h> | ||
|
||
namespace Transports { | ||
|
||
/** | ||
* Base transport class. Provides abstract interface to write and read bytes to | ||
* serial devices. The methods can throw errors on failures. | ||
*/ | ||
class Transport { | ||
public: | ||
/** | ||
* Opens transport connection. | ||
* | ||
* @throw std::runtime_error on failure | ||
*/ | ||
virtual void open() {} | ||
|
||
/** | ||
* Closes transport connection. | ||
* | ||
* @throw std::runtime_error on failure | ||
*/ | ||
virtual void close() {} | ||
|
||
/** | ||
* Send data to the other end of the transport. | ||
* | ||
* @param buf buffer to send | ||
* @param size its size | ||
* | ||
* @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()); | ||
} | ||
|
||
/** | ||
* 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 | ||
* | ||
* @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; | ||
}; | ||
|
||
} // namespace Transports | ||
|
||
#endif // !__Transports_Transport__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Serial communication through FPGA FIFOs. | ||
* | ||
* Developed for the Vera C. Rubin Observatory Telescope & Site Software Systems. | ||
* This product includes software developed by the Vera C.Rubin Observatory Project | ||
* (https://www.lsst.org). See the COPYRIGHT file at the top-level directory of | ||
* this distribution for details of code ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <cassert> | ||
|
||
#include "cRIO/NiError.h" | ||
#include "NiFpga/NiFpga.h" | ||
#include "Transports/FPGASerialDevice.h" | ||
|
||
using namespace LSST::cRIO; | ||
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) { | ||
assert(len < 255); | ||
|
||
uint8_t header[2] = {1, static_cast<uint8_t>(len)}; | ||
NiThrowError("Writing FIFO write header", | ||
NiFpga_WriteFifoU8(_fpga_session, _write_fifo, header, 2, 0, NULL)); | ||
NiThrowError("Writing FIFO write data", | ||
NiFpga_WriteFifoU8(_fpga_session, _write_fifo, reinterpret_cast<const uint8_t*>(buf), len, 0, | ||
NULL)); | ||
} | ||
|
||
std::vector<uint8_t> FPGASerialDevice::read(size_t len, const std::chrono::duration<long int>& timeout, | ||
LSST::cRIO::Thread* thread) { | ||
auto end = std::chrono::steady_clock::now() + timeout; | ||
|
||
std::vector<uint8_t> ret; | ||
|
||
bool not_run = true; | ||
|
||
do { | ||
if (not_run == false) { | ||
auto end_wait = std::chrono::steady_clock::now() + std::chrono::milliseconds(10); | ||
|
||
thread->wait_until(end_wait < end ? end_wait : end); | ||
} else { | ||
not_run = true; | ||
} | ||
|
||
uint8_t data[255]; | ||
|
||
uint8_t req = 2; | ||
NiThrowError("Reading FIFO requesting response", | ||
NiFpga_WriteFifoU8(_fpga_session, _write_fifo, &req, 1, 0, NULL)); | ||
|
||
uint8_t response[2]; | ||
NiThrowError("Reading FIFO reading response and its length", | ||
NiFpga_WriteFifoU8(_fpga_session, _read_fifo, response, 2, 1, NULL)); | ||
|
||
if (response[0] != 2) { | ||
throw std::runtime_error(fmt::format("Invalid reply from bus - {}, expected 2", response[0])); | ||
} | ||
if (response[1] == 0) { | ||
continue; | ||
} | ||
|
||
NiThrowError("ThermalFPGA::readMPUFIFO: reading response", | ||
NiFpga_ReadFifoU8(_fpga_session, _read_fifo, data, response[1], 0, NULL)); | ||
|
||
ret.insert(ret.end(), data, data + response[1]); | ||
if (ret.size() >= len) { | ||
break; | ||
} | ||
|
||
} while (end <= std::chrono::steady_clock::now()); | ||
|
||
return ret; | ||
} |
Oops, something went wrong.