M1M3 Support System
Loading...
Searching...
No Matches
ModbusBuffer.h
1/*
2 * This file is part of LSST M1M3 support system package.
3 *
4 * Developed for the Vera C. Rubin Telescope and Site System.
5 * This product includes software developed by the LSST Project
6 * (https://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef MODBUSBUFFER_H_
25#define MODBUSBUFFER_H_
26
27#include <cRIO/DataTypes.h>
28#include <string>
29#include <vector>
30
31namespace LSST {
32namespace M1M3 {
33namespace SS {
34
43public:
45 virtual ~ModbusBuffer();
46
47 int32_t getIndex();
48 void setIndex(int32_t index);
49 void incIndex(int32_t inc);
50 void skipToNextFrame();
51 uint16_t *getBuffer();
52 void set(int32_t index, uint16_t data);
53 void setLength(int32_t length);
54 int32_t getLength();
55
59 void reset();
60 bool endOfBuffer();
61 bool endOfFrame();
62
70 std::vector<uint8_t> getReadData(int32_t length);
71
79 static uint16_t calculateCRC(std::vector<uint8_t> data);
80
88 uint16_t calculateCRC(int32_t length);
89
90 uint16_t readLength();
91 int32_t readI32();
92 uint8_t readU8();
93 uint16_t readU16();
94 uint32_t readU32();
95 uint64_t readU48();
96 float readSGL();
97 std::string readString(int32_t length);
98 uint16_t readCRC();
99 double readTimestamp();
100
108 static inline uint8_t readInstructionByte(uint16_t instruction) {
109 return (uint8_t)((instruction >> 1) & 0xFF);
110 }
111
112 void readEndOfFrame();
113
114 void writeSubnet(uint8_t data);
115 void writeLength(uint16_t data);
116 void writeI8(int8_t data);
117 void writeI16(int16_t data);
118 void writeI24(int32_t data);
119 void writeI32(int32_t data);
120 void writeU8(uint8_t data);
121 void writeU16(uint16_t data);
122 void writeU32(uint32_t data);
123 void writeSGL(float data);
124 void writeCRC(int32_t length);
125
133 static inline uint16_t writeByteInstruction(uint8_t data) { return (((uint16_t)data) << 1) | 0x1200; };
134
135 void writeDelay(uint32_t delayMicros);
136 void writeEndOfFrame();
137 void writeSoftwareTrigger();
138 void writeTimestamp();
139 void writeTriggerIRQ();
140 void writeWaitForRx(uint32_t timeoutMicros);
141
148 void pullModbusResponse(uint16_t request, uint64_t &beginTs, uint64_t &endTs, std::vector<uint8_t> &data);
149
150private:
151 uint16_t _buffer[5120];
152 uint8_t _floatPointBuffer[8];
153 uint8_t _stringBuffer[256];
154 int32_t _index;
155 int32_t _length;
156};
157
158} /* namespace SS */
159} /* namespace M1M3 */
160} /* namespace LSST */
161
162#endif /* MODBUSBUFFER_H_ */
Utility class for Modbus buffer management.
Definition ModbusBuffer.h:42
std::vector< uint8_t > getReadData(int32_t length)
Returns read data from buffer.
Definition ModbusBuffer.cpp:97
void reset()
Set empty buffer.
Definition ModbusBuffer.cpp:91
static uint8_t readInstructionByte(uint16_t instruction)
Reads instruction byte from FPGA FIFO.
Definition ModbusBuffer.h:108
void pullModbusResponse(uint16_t request, uint64_t &beginTs, uint64_t &endTs, std::vector< uint8_t > &data)
Fills buffer with data from response, returns start and end timestamps.
Definition ModbusBuffer.cpp:307
static uint16_t writeByteInstruction(uint8_t data)
Return data item to write to buffer.
Definition ModbusBuffer.h:133
static uint16_t calculateCRC(std::vector< uint8_t > data)
Calculate Modbus CRC16.
Definition ModbusBuffer.cpp:105