Scope
FPGAException.h
1 #pragma once
2 
3 #include "devices/fpga/NiFpga.h"
4 #include "helpers.h"
5 
6 namespace scope {
7 
10  : public std::exception {
11 
12 public:
14  FPGAException(const int32_t& _status)
15  : std::exception()
16  , fpga_status(_status) {
17  }
18 
21  FPGAException(const int32_t& _status, const char* const _msg)
22  : std::exception(_msg)
23  , fpga_status(_status) {
24  }
25 
27  const int32_t fpga_status;
28 };
29 
33  static bool Check(const NiFpga_Status& _st) {
34  if(NiFpga_IsError(_st))
35  return false;
36  return true;
37  }
38 };
39 
41 template<bool ThrowException>
42 class FPGAStatusT {
43 
44 protected:
46  NiFpga_Status status;
47 
49  CString fpga_message;
50 
52  void Assign(const NiFpga_Status& _st) {
53  status = _st;
54  switch (status ) {
55  case NiFpga_Status_FifoTimeout:
56  fpga_message = L"FIFO timeout";
57  break;
58  case NiFpga_Status_InvalidParameter:
59  fpga_message = L"Invalid parameter";
60  break;
61  case NiFpga_Status_ResourceNotFound:
62  fpga_message = L"Resource not found";
63  break;
64  case NiFpga_Status_SignatureMismatch:
65  fpga_message = L"Bitfile signature mismatch";
66  break;
67  case NiFpga_Status_IncompatibleBitfile:
68  fpga_message = L"Bitfile incompatible";
69  break;
70  case NiFpga_Status_InvalidResourceName:
71  fpga_message = L"Invalid resource name";
72  break;
73  case NiFpga_Status_BitfileReadError:
74  fpga_message = L"Bitfile read error";
75  break;
76  case NiFpga_Status_CommunicationTimeout:
77  fpga_message = L"Communication between FPGA and host timed out";
78  break;
79  case NiFpga_Status_BadReadWriteCount:
80  fpga_message = L"The number of FIFO elements is invalid. Either the number is greater than the depth of the host memory DMA FIFO, or more elements were requested for";
81  break;
82  default:
83  fpga_message = L"Unknown";
84  }
85 
86  if( !FPGAStatusChecker::Check(status) ) {
87  DBOUT(L"FPGAException " << status << L" " << fpga_message.GetString());
88  }
89 
90  if( !FPGAStatusChecker::Check(status) && ThrowException )
91  throw FPGAException(status);
92  }
93 
94 public:
96  FPGAStatusT(const NiFpga_Status& _st = NiFpga_Status_Success)
97  : fpga_message(L"") {
98  Assign(_st);
99  }
100 
102  FPGAStatusT& operator= (const NiFpga_Status& _st) {
103  Assign(_st);
104  return *this;
105  }
106 
108  operator NiFpga_Status () const {
109  return status;
110  }
111 
113  bool Success() const {
114  return FPGAStatusChecker::Check(status);
115  }
116 };
117 
119 typedef FPGAStatusT<true> FPGAStatus;
120 
122 typedef FPGAStatusT<false> FPGAStatusSafe;
123 
124 }
FPGAStatusT(const NiFpga_Status &_st=NiFpga_Status_Success)
Standard constructor.
Definition: FPGAException.h:96
FPGAException(const int32_t &_status)
Definition: FPGAException.h:14
An exception for FPGA stuff.
Definition: FPGAException.h:9
static bool Check(const NiFpga_Status &_st)
Definition: FPGAException.h:33
STL namespace.
const int32_t fpga_status
the FPGA status
Definition: FPGAException.h:27
Wrapper class for the FPGA status, templatized for throwing or non-throwing.
Definition: FPGAException.h:42
CString fpga_message
status message
Definition: FPGAException.h:49
NiFpga_Status status
the FPGA status
Definition: FPGAException.h:46
void Assign(const NiFpga_Status &_st)
Set new status and check/throw if ThrowException.
Definition: FPGAException.h:52
FPGAException(const int32_t &_status, const char *const _msg)
Definition: FPGAException.h:21
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
bool Success() const
FPGAStatusT & operator=(const NiFpga_Status &_st)
Assignment operator.
Helper class to check the FPGA status value returned from NI FPGA C API.
Definition: FPGAException.h:31
Various helper functions and classes for Scope.