-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclidepp.cpp
120 lines (90 loc) · 3.06 KB
/
clidepp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "Stdafx.h"
#include "clidepp.h"
#include "helpers.h"
#include "dpcutil/dpcdecl.h"
#include "dpcutil/depp.h"
using namespace helpers;
using namespace dpcutil;
String^ DEPP::Version::get() {
if (_version == nullptr) {
char* versionChars = new char[cchVersionMax];
if (DeppGetVersion(versionChars) == 0)
_version = nullptr;
else
_version = CharArrayToString(versionChars);
delete[] versionChars;
}
return _version;
}
DEPP::DEPP(Device^ dvc) {
_dvc = dvc;
_enabled = false;
}
DEPP::~DEPP() {
if (IsEnabled)
Disable();
}
bool DEPP::IsEnabled::get() {
return _enabled;
}
void DEPP::Enable() {
if (IsEnabled)
throw gcnew InvalidOperationException("DEPP is already enabled!");
if (!_dvc->IsOpened) _dvc->Open();
_dvc->Lock();
if (DeppEnable(_dvc->_hif) == 0) {
_dvc->Release();
throw gcnew ConnectionException("Failed to enable DEPP!");
}
_enabled = true;
}
void DEPP::Disable() {
if (!IsEnabled)
throw gcnew InvalidOperationException("DEPP isn't enabled!");
if (DeppDisable(_dvc->_hif) == 0)
throw gcnew ConnectionException("Failed to disable DEPP!");
_dvc->Release();
_enabled = false;
}
void DEPP::PutReg(unsigned char addr, unsigned char value) {
if (!IsEnabled)
throw gcnew InvalidOperationException("DEPP isn't enabled!");
if (DeppPutReg(_dvc->_hif, addr, value, false) == 0)
throw gcnew ConnectionException("Failed to write register data!");
}
unsigned char DEPP::GetReg(unsigned char addr) {
if (!IsEnabled)
throw gcnew InvalidOperationException("DEPP isn't enabled!");
unsigned char value;
if (DeppGetReg(_dvc->_hif, addr, &value, false) == 0)
throw gcnew ConnectionException("Failed to read register data!");
return value;
}
void DEPP::PutRegs(array<unsigned char>^ addrs, array<unsigned char>^ values) {
if (!IsEnabled)
throw gcnew InvalidOperationException("DEPP isn't enabled!");
if (addrs->Length != values->Length)
throw gcnew ArgumentException(
"Address and value arrays have different lengths!");
unsigned char* avPairs = new unsigned char[addrs->Length * 2];
for (int i = 0; i < addrs->Length; ++i) {
avPairs[2 * i] = addrs[i];
avPairs[2 * i + 1] = values[i];
}
int ret = DeppPutRegSet(_dvc->_hif, avPairs, addrs->Length, false);
delete[] avPairs;
if (ret == 0)
throw gcnew ConnectionException("Failed to write register data!");
}
array<unsigned char>^ DEPP::GetRegs(array<unsigned char>^ addrs) {
if (!IsEnabled)
throw gcnew InvalidOperationException("DEPP isn't enabled!");
array<unsigned char>^ values = gcnew array<unsigned char>(addrs->Length);
pin_ptr<unsigned char> pinnedAddrs = &addrs[0];
pin_ptr<unsigned char> pinnedValues = &values[0];
int ret = DeppGetRegSet(_dvc->_hif, (unsigned char*) pinnedAddrs,
(unsigned char*) pinnedValues, addrs->Length, false);
if (ret == 0)
throw gcnew ConnectionException("Failed to read register data!");
return values;
}