-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSwt.cxx
More file actions
338 lines (288 loc) · 10.9 KB
/
Copy pathSwt.cxx
File metadata and controls
338 lines (288 loc) · 10.9 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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.
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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 Swt.cxx
/// \brief Definition of SWT operations
///
/// \author Kostas Alexopoulos (kostas.alexopoulos@cern.ch)
#include <boost/format.hpp>
#include <chrono>
#include <thread>
#include "Alf/Exception.h"
#include "Logger.h"
#include "ReadoutCard/CardDescriptor.h"
#include "ReadoutCard/CardFinder.h"
#include "ReadoutCard/ChannelFactory.h"
#include "ReadoutCard/Cru.h"
#include "Alf/Swt.h"
namespace o2
{
namespace alf
{
namespace sc_regs = AliceO2::roc::Cru::ScRegisters;
Swt::Swt(AlfLink link, std::shared_ptr<lla::Session> llaSession, SwtWord::Size swtWordSize)
: ScBase(link, llaSession), mSwtWordSize(swtWordSize)
{
if (kDebugLogging) {
Logger::setFacility("ALF/SWT");
}
}
Swt::Swt(const roc::Parameters::CardIdType& cardId, int linkId)
: ScBase(cardId, linkId)
{
if (kDebugLogging) {
Logger::setFacility("ALF/SWT");
}
}
Swt::Swt(std::string cardId, int linkId)
: ScBase(cardId, linkId)
{
if (kDebugLogging) {
Logger::setFacility("ALF/SWT");
}
}
std::vector<SwtWord> Swt::read(SwtWord::Size wordSize, TimeOut msTimeOut)
{
checkChannelSet();
std::vector<SwtWord> words;
uint32_t numWords = 0x0;
auto timeOut = std::chrono::steady_clock::now() + std::chrono::milliseconds(msTimeOut);
while ((std::chrono::steady_clock::now() < timeOut) && numWords < 1) {
numWords = (barRead(sc_regs::SWT_MON.index) >> 16);
}
if (numWords < 1) { // #WORDS in READ FIFO
BOOST_THROW_EXCEPTION(SwtException() << ErrorInfo::Message("Not enough words in SWT READ FIFO"));
}
for (int i = 0; i < (int)numWords; i++) {
SwtWord tempWord;
tempWord.setLow(barRead(sc_regs::SWT_RD_WORD_L.index));
if (wordSize == SwtWord::Size::Medium || wordSize == SwtWord::Size::High) {
tempWord.setMed(barRead(sc_regs::SWT_RD_WORD_M.index));
}
if (wordSize == SwtWord::Size::High) {
tempWord.setHigh(barRead(sc_regs::SWT_RD_WORD_H.index));
}
//wordMonPairs.push_back(std::make_pair(tempWord, barRead(sc_regs::SWT_MON.index)));
words.push_back(tempWord);
}
return words;
}
std::vector<SwtWord> Swt::readMultiple(SwtWord::Size wordSize, unsigned int wordsToRead, TimeOut msTimeOut)
{
checkChannelSet();
std::vector<SwtWord> words;
unsigned int readWords = 0x0;
while (readWords < wordsToRead) {
uint32_t numWords = 0x0;
auto timeOut = std::chrono::steady_clock::now() + std::chrono::milliseconds(msTimeOut);
bool isTimeout = false;
while (!isTimeout && (numWords < 1)) {
numWords = (barRead(sc_regs::SWT_MON.index) >> 16);
isTimeout = std::chrono::steady_clock::now() > timeOut;
}
if(isTimeout){
break;
}
for (int i = 0; i < (int)numWords; i++) {
SwtWord tempWord;
tempWord.setLow(barRead(sc_regs::SWT_RD_WORD_L.index));
if (wordSize == SwtWord::Size::Medium || wordSize == SwtWord::Size::High) {
tempWord.setMed(barRead(sc_regs::SWT_RD_WORD_M.index));
}
if (wordSize == SwtWord::Size::High) {
tempWord.setHigh(barRead(sc_regs::SWT_RD_WORD_H.index));
}
words.push_back(tempWord);
}
readWords += numWords;
}
if (readWords != wordsToRead) {
BOOST_THROW_EXCEPTION(SwtException() <<
ErrorInfo::Message("ReadMultiple different number of words than expected: " + std::to_string(readWords) + " (expected " + std::to_string(wordsToRead) + ")"));
}
return words;
}
void Swt::write(const SwtWord& swtWord)
{
checkChannelSet();
// prep the swt word
if (swtWord.getSize() == SwtWord::Size::High) {
barWrite(sc_regs::SWT_WR_WORD_H.index, swtWord.getHigh());
}
if (swtWord.getSize() == SwtWord::Size::High || swtWord.getSize() == SwtWord::Size::Medium) {
barWrite(sc_regs::SWT_WR_WORD_M.index, swtWord.getMed());
}
barWrite(sc_regs::SWT_WR_WORD_L.index, swtWord.getLow()); // The LOW bar write, triggers the write operation
//return barRead(sc_regs::SWT_MON.index);
}
std::vector<std::pair<Swt::Operation, Swt::Data>> Swt::executeSequence(std::vector<std::pair<Operation, Data>> sequence, bool lock, int lockTimeout)
{
if (lock) {
mLlaSession->start(lockTimeout);
}
try {
checkChannelSet();
} catch (const SwtException& e) {
return { { Operation::Error, e.what() } };
}
std::vector<std::pair<Operation, Data>> ret;
for (const auto& it : sequence) {
Operation operation = it.first;
Data data = it.second;
try {
if (operation == Operation::Read) {
int timeOut;
try {
timeOut = boost::get<TimeOut>(data);
} catch (...) { // no timeout was provided
data = readTimeout;
timeOut = boost::get<TimeOut>(data);
}
auto results = read(mSwtWordSize, timeOut);
for (const auto& result : results) {
ret.push_back({ operation, result });
}
} else if (operation == Operation::ReadMultiple) {
int count;
count = boost::get<int>(data);
auto results = readMultiple(mSwtWordSize, count, readTimeout);
for (const auto& result : results) {
ret.push_back({ operation, result });
}
} else if (operation == Operation::SetReadTimeout) {
readTimeout = boost::get<TimeOut>(data);
ret.push_back({ operation, readTimeout });
} else if (operation == Operation::Write) {
SwtWord word = boost::get<SwtWord>(data);
write(word);
ret.push_back({ operation, word });
//ret.push_back({ Operation::Write, {} }); // TODO: Is it better to return {} ?
} else if (operation == Operation::SCReset) {
scReset();
ret.push_back({ operation, {} });
} else if (operation == Operation::Wait) {
int waitTime;
try {
waitTime = boost::get<WaitTime>(data);
} catch (...) { // no timeout was provided
data = DEFAULT_SWT_WAIT_TIME_MS;
waitTime = boost::get<WaitTime>(data);
}
std::this_thread::sleep_for(std::chrono::milliseconds(waitTime));
ret.push_back({ operation, waitTime });
} else {
BOOST_THROW_EXCEPTION(SwtException() << ErrorInfo::Message("SWT operation type unknown"));
}
} catch (const SwtException& e) {
std::string meaningfulMessage;
if (operation == Operation::Read) {
meaningfulMessage = (boost::format("SWT_SEQUENCE READ timeout=%d serialId=%s link=%d, error='%s'") % boost::get<TimeOut>(data) % mLink.serialId % mLink.linkId % e.what()).str();
} else if (operation == Operation::Write) {
meaningfulMessage = (boost::format("SWT_SEQUENCE WRITE data=%s serialId=%s link=%d, error='%s'") % boost::get<SwtWord>(data) % mLink.serialId % mLink.linkId % e.what()).str();
} else if (operation == Operation::SCReset) {
meaningfulMessage = (boost::format("SWT_SEQUENCE SC RESET serialId=%d link=%s, error='%s'") % mLink.serialId % mLink.linkId % e.what()).str();
} else if (operation == Operation::Wait) {
meaningfulMessage = (boost::format("SWT_SEQUENCE WAIT waitTime=%d serialId=%s link=%d error='%s'") % boost::get<WaitTime>(data) % mLink.serialId % mLink.linkId % e.what()).str();
} else {
meaningfulMessage = (boost::format("SWT_SEQUENCE UNKNOWN serialId=%d link=%s, error='%s'") % mLink.serialId % mLink.linkId % e.what()).str();
}
//Logger::get().err() << meaningfulMessage << endm;
ret.push_back({ Operation::Error, meaningfulMessage });
break;
}
}
if (lock) {
mLlaSession->stop();
}
return ret;
}
std::string Swt::writeSequence(std::vector<std::pair<Operation, Data>> sequence, bool lock, int lockTimeout)
{
std::stringstream resultBuffer;
auto out = executeSequence(sequence, lock, lockTimeout);
for (const auto& it : out) {
Operation operation = it.first;
Data data = it.second;
if (operation == Operation::Read) {
resultBuffer << data << "\n";
} else if (operation == Operation::ReadMultiple) {
resultBuffer << data << "\n";
} else if (operation == Operation::SetReadTimeout) {
resultBuffer << std::dec << data << "\n";
} else if (operation == Operation::Write) {
resultBuffer << "0\n";
} else if (operation == Operation::SCReset) {
/* DO NOTHING */
} else if (operation == Operation::Wait) {
resultBuffer << std::dec << data << "\n";
} else if (operation == Operation::Error) {
resultBuffer << data;
if (kDebugLogging) {
Logger::get() << data << LogErrorDevel_(5200) << endm;
}
BOOST_THROW_EXCEPTION(SwtException() << ErrorInfo::Message(resultBuffer.str()));
break;
}
}
return resultBuffer.str();
}
std::string Swt::SwtOperationToString(Swt::Operation op)
{
if (op == Swt::Operation::Read) {
return "read";
} else if (op == Swt::Operation::ReadMultiple) {
return "read_multiple";
} else if (op == Swt::Operation::SetReadTimeout) {
return "set_read_timeout";
} else if (op == Swt::Operation::Write) {
return "write";
} else if (op == Swt::Operation::SCReset) {
return "sc_reset";
} else if (op == Swt::Operation::Wait) {
return "wait";
} else if (op == Swt::Operation::Lock) {
return "lock";
} else if (op == Swt::Operation::Error) {
return "error";
}
BOOST_THROW_EXCEPTION(SwtException() << ErrorInfo::Message("Cannot convert SWT operation to string"));
}
Swt::Operation Swt::StringToSwtOperation(std::string op)
{
if (op == "read") {
return Swt::Operation::Read;
} else if (op == "read_multiple") {
return Swt::Operation::ReadMultiple;
} else if (op == "set_read_timeout") {
return Swt::Operation::SetReadTimeout;
} else if (op == "write") {
return Swt::Operation::Write;
} else if (op == "sc_reset") {
return Swt::Operation::SCReset;
} else if (op == "wait") {
return Swt::Operation::Wait;
} else if (op == "lock") {
return Swt::Operation::Lock;
} else if (op == "error") {
return Swt::Operation::Error;
}
BOOST_THROW_EXCEPTION(SwtException() << ErrorInfo::Message("Cannot convert operation to SWT string " + op));
}
} // namespace alf
} // namespace o2