-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIcPythonInterface.h
More file actions
264 lines (210 loc) · 6.78 KB
/
IcPythonInterface.h
File metadata and controls
264 lines (210 loc) · 6.78 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file IcPythonInterface.cxx
/// \brief Python IC interface for the Slow Control library
///
/// \author Kostas Alexopoulos ([email protected])
#ifndef O2_ALF_SRC_ICPYTHONINTERFACE_H
#define O2_ALF_SRC_ICPYTHONINTERFACE_H
#include <boost/python.hpp>
#include "Alf/Ic.h"
#include "PythonInterface.h"
namespace o2
{
namespace alf
{
namespace python
{
namespace bp = boost::python;
typedef boost::variant<std::string, std::pair<std::string, uint32_t>, std::pair<std::string, std::pair<uint32_t, uint32_t>>> IcArgsVariant;
/// Exposed python function docs
auto sIcInitDoc =
R"(Initializes an IC object
Args:
card id: String containing PCI address (e.g. 42:0.0), serial-endpoint pair (e.g. 10241:1), sequence number (e.g. #2)
channel: Channel number to initially set)";
auto sIcSetChannelDoc =
R"(Sets the Slow Control channel
Args:
channel: Channel number to set)";
auto sIcScResetDoc =
R"(Resets Slow Control)";
auto sIcWriteDoc =
R"(Perform an IC write
Args:
address: IC address (unsigned 32-bit) to write to
data: IC data (unsigned 32-bit))";
auto sIcReadDoc =
R"(Perform an IC write
Args:
address: IC address (unsigned 32-bit) to read from)
Returns:
data: IC data (unsigned 32-bit) read)";
auto sIcWriteGbtI2cDoc =
R"(Perform an IC GBT I2C write
Args:
data: Data (unsigned 32-bit) to write)";
auto sIcSequenceDoc =
R"(Execute an IC sequence:
Args:
sequence: A list of tuples made up of:
operation: An operation(string) to perform
input: Input((unsigned 32-bit int, unsigned 32-bit int), unsigned 32-bit int) to said operation
Operations and inputs:
operation: "sc_reset"
input: no-input
operation: "read"
input: address (unsigned 32-bit int)
operation: "write"
input: address, data tuple (unsigned 32-bit int, unsigned 32-bit int)
lock: boolean to execute the sequence within an LLA session
Returns:
sequence: A list of tuples made up of:
operation: The operation carried out (string, same as input)
output: Output (string, (unsigned 32-bit int, unsigned 32-bit int), unsigned 32-bit int) of said operation)
Output per operation:
operation: "sc_reset"
output: "" (string)
operation: "read"
output: data (unsigned 32-bit int)
operation: "write"
output: echo of the address and data tuple (unsigned 32-bit int, unsigned 32-bit int)
)";
struct IcArgsVariantConverter {
IcArgsVariantConverter&
fromPython()
{
bp::converter::registry::push_back(
&IcArgsVariantConverter::convertible,
&IcArgsVariantConverter::construct,
bp::type_id<IcArgsVariant>());
return *this;
}
static void* convertible(PyObject* obj)
{
return obj;
}
static void construct(PyObject* obj, bp::converter::rvalue_from_python_stage1_data* data)
{
bp::handle<> handle(bp::borrowed(obj));
void* storage = ((bp::converter::rvalue_from_python_storage<IcArgsVariant>*)data)->storage.bytes;
IcArgsVariant ret;
bp::extract<std::string> s(obj);
if (s.check()) {
ret = s();
} else {
bp::tuple tuple(bp::borrowed(obj));
bp::extract<uint32_t> i(tuple[1]);
if (!i.check()) {
ret = std::pair<std::string, std::pair<uint32_t, uint32_t>>(bp::extract<std::string>(tuple[0]),
bp::extract<std::pair<uint32_t, uint32_t>>(tuple[1]));
} else {
ret = bp::extract<std::pair<std::string, uint32_t>>(tuple);
}
}
new (storage) IcArgsVariant(ret);
data->convertible = storage;
return;
}
};
class IcInterface
{
public:
IcInterface(std::string cardIdString, int linkId = -1)
{
mIc = std::make_shared<Ic>(cardIdString, linkId);
}
void setChannel(int gbtChannel)
{
mIc->setChannel(gbtChannel);
}
void scReset()
{
mIc->scReset();
}
uint32_t read(uint32_t address)
{
return mIc->read(address);
}
void write(uint32_t address, uint32_t data)
{
mIc->write(address, data);
}
void writeGbtI2c(uint32_t data)
{
mIc->writeGbtI2c(data);
}
std::vector<std::pair<Ic::Operation, Ic::Data>> sequence(std::vector<IcArgsVariant> sequence, bool lock = false)
{
ScopedGILRelease s; // enable boost::python multi-threading
std::vector<std::pair<Ic::Operation, Ic::Data>> icSequence;
for (const auto& v : sequence) {
icSequence.push_back(boost::apply_visitor(IcArgsVariantVisitor(), v));
}
return mIc->executeSequence(icSequence, lock);
}
std::vector<std::pair<Ic::Operation, Ic::Data>> sequenceDefault(std::vector<IcArgsVariant> icSeq)
{
return sequence(icSeq);
}
private:
struct IcArgsVariantVisitor
: public boost::static_visitor<std::pair<Ic::Operation, Ic::Data>> {
auto operator()(std::string v) const
{
return std::make_pair(Ic::StringToIcOperation(v), 0);
}
auto operator()(std::pair<std::string, uint32_t> v) const
{
return std::make_pair(Ic::StringToIcOperation(v.first), v.second);
}
auto operator()(std::pair<std::string, std::pair<uint32_t, uint32_t>> v) const
{
return std::make_pair(Ic::StringToIcOperation(v.first), Ic::IcData{ v.second.first, v.second.second });
}
};
std::shared_ptr<Ic> mIc;
};
struct IcOperationDataVectorToPythonListOfTuples {
static PyObject* convert(const std::vector<std::pair<Ic::Operation, Ic::Data>>& seqOut)
{
bp::list ret;
for (const auto& el : seqOut) {
ret.append(bp::make_tuple(Ic::IcOperationToString(el.first), el.second));
}
return bp::incref(bp::object(ret).ptr());
}
};
struct IcDataToPython {
struct IcDataConvertVisitor : public boost::static_visitor<PyObject*> {
auto operator()(Ic::IcData data) const
{
bp::tuple ret = bp::make_tuple(data.address, data.data);
return bp::incref(bp::object(ret).ptr());
}
auto operator()(int data) const
{
return bp::incref(bp::object(data).ptr());
}
auto operator()(std::string data) const
{
return bp::incref(bp::object(data).ptr());
}
};
static PyObject* convert(Ic::Data data)
{
return boost::apply_visitor(IcDataConvertVisitor(), data);
}
};
} //namespace python
} //namespace alf
} //namespace o2
#endif // O2_ALF_SRC_ICPYTHONINTERFACE_H