Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error handling #52

Merged
merged 12 commits into from
Nov 18, 2023
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ lib/libcRIOcpp.a: FORCE

# Other Targets
clean:
@$(foreach file,doc,echo '[RM ] ${file}'; $(RM) -r $(file);)
@$(foreach file,,echo '[RM ] ${file}'; $(RM) -r $(file);)
@$(foreach dir,src tests,$(MAKE) -C ${dir} $@;)

tests: tests/Makefile tests/*.cpp
Expand Down
2 changes: 1 addition & 1 deletion Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ifndef VERBOSE
silence := --silence-errors
endif

c_opts := -fdata-sections -ffunction-sections -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE -DSPDLOG_FMT_EXTERNAL
c_opts := -fdata-sections -ffunction-sections -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE # -DSPDLOG_FMT_EXTERNAL

ifdef DEBUG
c_opts += -g
Expand Down
6 changes: 6 additions & 0 deletions doc/version-history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Version History
###############

v1.10.0
=======

* handleMissingReply, MissingReply exception
* IRQTimeout handling

v1.9.0
======

Expand Down
12 changes: 2 additions & 10 deletions include/cRIO/CliApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
#ifndef __CliApp_h
#define __CliApp_h

#include <iomanip>
#include <iostream>
#include <ostream>

#include <cRIO/Application.h>
#include <cRIO/OStreamRestore.h>
#include <cRIO/ModbusBuffer.h>

namespace LSST {
namespace cRIO {
Expand Down Expand Up @@ -226,13 +224,7 @@ get_the_answer command.
*/
template <typename dt>
static const void printHexBuffer(dt* buf, size_t len, std::ostream& os = std::cout) {
OStreamRestore res(os);

os << std::hex << std::setfill('0');
for (size_t i = 0; i < len; i++) {
os << " " << std::setw(sizeof(dt) * 2) << +(buf[i]);
}
os << std::dec;
os << ModbusBuffer::hexDump<dt>(buf, len);
}

static const void printDecodedBuffer(uint16_t* buf, size_t len, std::ostream& os = std::cout);
Expand Down
6 changes: 3 additions & 3 deletions include/cRIO/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef COMMAND_H_
#define COMMAND_H_
#ifndef __CRIO_COMMAND_H_
#define __CRIO_COMMAND_H_

#include <string>

Expand Down Expand Up @@ -74,4 +74,4 @@ class Command {
} // namespace cRIO
} // namespace LSST

#endif /* COMMAND_H_ */
#endif /* __CRIO_COMMAND_H_ */
40 changes: 26 additions & 14 deletions include/cRIO/DataTypes.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/*
* 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 file is part of LSST M1M3 support system package.
*
* 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.
* Developed for the Vera C. Rubin Telescope and Site System.
* This product includes software developed by the LSST 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 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.
* 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.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
* 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 DATATYPES_H_
Expand All @@ -41,6 +44,15 @@
/// Number of actuators in Z axis - shall equal to total number of actuators.
#define FA_Z_COUNT 156

/// Number of secondary (X or Y) actuators.
#define FA_S_COUNT (FA_X_COUNT + FA_Y_COUNT)

/// Maximal number of Force Actuator near neighbors
#define FA_MAX_NEAR_COUNT 7

/// Number of Force Actuator neighbors
#define FA_FAR_COUNT 12

/// Number of hardpoints.
#define HP_COUNT 6

Expand Down
4 changes: 3 additions & 1 deletion include/cRIO/FPGA.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ class FPGA : public SimpleFPGA {
* the data.
*
* @param mpu Modbus Processing Unit to read the data
*
* @return data read from the port
*/
virtual void readMPUFIFO(MPU& mpu) = 0;
virtual std::vector<uint8_t> readMPUFIFO(MPU& mpu) = 0;

/**
* Writes buffer to command FIFO. Command FIFO is processed in
Expand Down
34 changes: 32 additions & 2 deletions include/cRIO/MPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ namespace MPUCommands {
const static uint8_t WRITE = 1;
const static uint8_t READ_US = 2;
const static uint8_t READ_MS = 3;
const static uint8_t WAIT_US = 100;
const static uint8_t WAIT_MS = 101;
const static uint8_t IRQ = 240;
const static uint8_t TELEMETRY = 254;
const static uint8_t CLEAR = 255;
const static uint8_t RESET = 255;
} // namespace MPUCommands

typedef enum { WRITE, READ, IDLE } loop_state_t;
Expand Down Expand Up @@ -76,6 +78,25 @@ class MPU : public ModbusBuffer {

void clearCommanded();

/**
* Reset bus. Clear all FIFOs.
*/
void resetBus();

/**
* Wait for given number of microseonds.
*
* @param us number of microseconds to wait
*/
void waitUs(uint16_t us);

/**
* Wait for given number of milliseconds.
*
* @param ms number of milliseconds to wait
*/
void waitMs(uint16_t ms);

/**
* Returns bus number (internal FPGA identifier).
*
Expand Down Expand Up @@ -146,6 +167,15 @@ class MPU : public ModbusBuffer {
*/
virtual void loopWrite() = 0;

/**
* Exception raised when read timeouts.
*/
class IRQTimeout : public std::runtime_error {
public:
IRQTimeout(std::vector<uint8_t> _data);
std::vector<uint8_t> data;
};

/***
* Called to process data read in the loop.
*
Expand All @@ -165,7 +195,7 @@ class MPU : public ModbusBuffer {
*
* @param FPGA fpga used to process MPU commands.
*/
void runLoop(FPGA &fpga);
bool runLoop(FPGA &fpga);

loop_state_t getLoopState() { return _loop_state; }

Expand Down
35 changes: 35 additions & 0 deletions include/cRIO/ModbusBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#define CRIO_MODBUSBUFFER_H_

#include <functional>
#include <iomanip>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stdexcept>
#include <vector>
Expand Down Expand Up @@ -178,6 +180,26 @@ class ModbusBuffer {
*/
std::vector<uint8_t> getReadData(int32_t length);

/**
* Dumps hex data to ostring stream.
*
*
* @param
*/
template <typename dt>
static const std::string hexDump(dt* buf, size_t len) {
std::ostringstream os;
os << std::setfill('0') << std::hex;
for (size_t i = 0; i < len; i++) {
if (i > 0) {
os << " ";
}
os << std::setw(sizeof(dt) * 2) << +(buf[i]);
}
os << std::dec;
return os.str();
}

/**
* Reads data from buffer. Updates CRC as it reads the data. Size of
* receiving buffer is assumed to be equal or greater than len.
Expand Down Expand Up @@ -505,6 +527,11 @@ class ModbusBuffer {
EmptyCommanded(uint8_t address, uint8_t function);
};

class MissingReply : public std::runtime_error {
public:
MissingReply(uint8_t address, uint8_t func);
};

class UnmatchedFunction : public std::runtime_error {
public:
UnmatchedFunction(uint8_t address, uint8_t function);
Expand All @@ -521,6 +548,14 @@ class ModbusBuffer {
void incIndex() { _index++; }
void resetCRC() { _crc.reset(); }

/**
* Called when a reply to command is missing.
*
* @parameter address ILC address
* @parameter func called function
*/
virtual void handleMissingReply(uint8_t address, uint8_t func);

/**
* Return data item to write to buffer. Updates CRC counter.
*
Expand Down
15 changes: 9 additions & 6 deletions include/cRIO/NiError.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,40 @@ class NiError : public std::runtime_error {
};

/**
* Throws NI exception if an error occurred.
* Throws NI exception if an error occurred. Ignores positive status values, as
* those signal a warning - see NiFpga_IsError.
*
* @param msg message associated with the error
* @param status NI status
*
* @throw NiError if status != 0
* @throw NiError if status < 0
*
* @see NiError
*/
void NiThrowError(const char *msg, int32_t status);

/**
* Throws NI exception if an error occurred.
* Throws NI exception if an error occurred. Ignores positive status values, as
* those signal a warning - see NiFpga_IsError.
*
* @param msg message associated with the error
* @param status NI status
*
* @throw NiError if status != 0
* @throw NiError if status < 0
*
* @see NiError
*/
inline void NiThrowError(const std::string &msg, int32_t status) { NiThrowError(msg.c_str(), status); }

/**
* Throws NI exception if an error occurred.
* Throws NI exception if an error occurred. Ignores positive status values, as
* those signal a warning - see NiFpga_IsError.
*
* @param func reporting function
* @param ni_func Ni function name signaling the exception
* @param status NI status
*
* @throw NiError if status != 0
* @throw NiError if status < 0
*
* @see NiError
*/
Expand Down
2 changes: 1 addition & 1 deletion include/cRIO/PrintILC.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ class PrintILC : public virtual ILC {
} // namespace cRIO
} // namespace LSST

#endif // !_cRIO_ILC_
#endif // !_cRIO_PrintILC_
8 changes: 5 additions & 3 deletions include/cRIO/SAL/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
namespace LSST {
namespace cRIO {
namespace SAL {
constexpr int ACK_INPROGRESS = 301;
constexpr int ACK_COMPLETE = 303;
constexpr int ACK_FAILED = -302;

constexpr int32_t ACK_INPROGRESS = 301; /// Acknowledges command reception, command is being executed.
constexpr int32_t ACK_COMPLETE = 303; /// Command is completed.
constexpr int32_t ACK_FAILED = -302; /// Command execution failed.

} // namespace SAL
} // namespace cRIO
} // namespace LSST
Expand Down
1 change: 1 addition & 0 deletions src/LSST/cRIO/CliApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "cRIO/CliApp.h"
#include "cRIO/ModbusBuffer.h"
#include "cRIO/OStreamRestore.h"
#include "cRIO/Timestamp.h"

namespace LSST {
Expand Down
Loading
Loading