-
Notifications
You must be signed in to change notification settings - Fork 1
/
v1290.cpp
260 lines (220 loc) · 5.67 KB
/
v1290.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
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
#include <algorithm>
#include <thread>
#include <chrono>
#include <cmath>
#include "v1290.hpp"
namespace caen {
const float V1290::single_resolution[4] = {
800e-12,
200e-12,
100e-12,
25e-12
};
const float V1290::pair_resolution[16] = {
100e-12,
200e-12,
400e-12,
800e-12,
1.6e-9,
3.2e-9,
6.25e-9,
12.5e-9,
25e-9,
50e-9,
100e-9,
200e-9,
400e-9,
800e-9,
0,
0
};
const float V1290::dead_times[4] = {
5e-9,
10e-9,
30e-9,
100e-9
};
V1290::V1290(const Connection& connection): Device(connection) {
if (oui() != OUI || id() != 1290) throw WrongDevice(connection, "V1290");
version_ = static_cast<Version>(read16(0x4030));
};
V1290::V1290(V1290&& device): Device(std::move(device)) {
version_ = device.version_;
};
V1290::Resolution V1290::resolution() const {
auto mode = edge_detection();
micro_write(0x2600);
uint16_t res = micro_read();
Resolution result;
if (mode & 3 == 3) {
result.edge = pair_resolution[res & 7];
result.pulse = pair_resolution[res >> 3 & 0xF];
} else {
result.edge = single_resolution[res & 3];
result.pulse = 0;
};
return result;
};
static uint8_t find_nearest(
const float* array, uint8_t size, float value, bool ascending = true
) {
int i = (
ascending
? std::upper_bound(array, array + size, value)
: std::upper_bound(array, array + size, value, std::greater<float>())
) - array;
if (i == 0) return 0;
if (i == size) return i - 1;
if (std::fabs(value - array[i-1]) < std::fabs(value - array[i])) return i - 1;
return i;
};
void V1290::set_resolution(float edge, float pulse) {
auto mode = edge_detection();
if (mode & 3 == 3) {
uint8_t iedge = find_nearest(pair_resolution, 14, edge);
uint8_t ipulse = find_nearest(pair_resolution, 14, pulse);
micro_write(0x2500);
micro_write(ipulse << 8 | iedge);
} else {
uint8_t iedge = find_nearest(single_resolution, 4, edge, false);
micro_write(0x2400);
micro_write(iedge);
};
};
void V1290::set_dead_time(float time) {
uint8_t t = find_nearest(dead_times, 4, time);
micro_write(0x2800);
micro_write(t);
};
V1290::TriggerConfiguration V1290::trigger_configuration() const {
TriggerConfiguration result;
micro_write(0x1600);
result.window_width = cycles_to_seconds(micro_read());
result.window_offset = cycles_to_seconds(micro_read());
result.search_margin = cycles_to_seconds(micro_read());
result.reject_margin = cycles_to_seconds(micro_read());
result.time_subtraction_enabled = micro_read() & 1;
return result;
};
void V1290::set_edge_detection(bool leading, bool trailing) {
uint16_t value = 0;
if (trailing) value |= 1;
if (leading) value |= 3;
micro_write(0x2200);
micro_write(value);
};
int V1290::event_size() const {
micro_write(0x3400);
uint16_t code = micro_read();
if (code == 9) return -1;
if (code == 0) return 0;
return 1 << code - 1;
};
static uint8_t log2_ceil(uint8_t x) {
#ifdef __cpp_lib_int_pow2
uint8_t n = std::bit_width(x);
if (x == 1 << n - 1) return n - 1;
return n;
#else
uint8_t n = 0;
while (x > 1 << n) ++n;
return n;
#endif
};
void V1290::set_event_size(int size) {
uint16_t code;
if (size < 0 || size > 128)
code = 9;
else if (size == 0)
code = 0;
else
code = log2_ceil(size) + 1;
micro_write(0x3300);
micro_write(code);
};
void V1290::set_fifo_size(unsigned nwords) {
uint8_t code;
if (nwords <= 2)
code = 0;
else if (nwords >= 256)
code = 7;
else
code = log2_ceil(nwords) - 1;
micro_write(0x3B00);
micro_write(code);
};
void V1290::enable_channels(uint32_t mask) {
micro_write(0x4400);
micro_write(mask & 0xFFFF);
if (version_ == V1290A) micro_write(mask >> 16);
};
uint32_t V1290::enabled_channels() const {
micro_write(0x4500);
uint32_t result = micro_read();
if (version_ == V1290A) result = micro_read() << 16 | result;
return result;
};
uint32_t V1290::enabled_tdc_channels(uint8_t tdc) const {
micro_write(0x4700 | tdc);
uint32_t result = micro_read();
result = micro_read() << 16 | result;
return result;
};
void V1290::enable_tdc_channels(uint8_t tdc, uint32_t mask) {
micro_write(0x4600 | tdc);
micro_write(mask & 0xFFFF);
micro_write(mask >> 16);
};
uint64_t V1290::tdc_status(uint8_t tdc) const {
micro_write(0x7600 | tdc);
uint64_t result = 0;
for (int i = 0; i < 4; ++i) result = result << 16 | micro_read();
return result;
};
void V1290::eeprom_write(uint16_t address, uint8_t byte) {
micro_write(0xC000);
micro_write(address);
micro_write(byte);
};
uint8_t V1290::eeprom_read(uint16_t address) const {
micro_write(0xC100);
micro_write(address);
return micro_read();
};
V1290::MicroRevision V1290::micro_revision_date() const {
MicroRevision result;
micro_write(0xC200);
result.version = micro_read();
result.day = micro_read();
result.month = micro_read();
result.year = micro_read();
return result;
};
void V1290::enable_test_mode(uint32_t test_word) {
micro_write(0xC500);
micro_write(test_word & 0xFFFF);
micro_write(test_word >> 16);
};
void V1290::scan_path_read(uint8_t tdc, uint16_t* path) const {
micro_write(0xC900 | tdc);
for (int i = 0; i < scan_path_length; ++i) path[i] = micro_read();
};
void V1290::micro_wait(uint8_t bit) const {
while (!(read16(0x1030) & bit))
std::this_thread::sleep_for(std::chrono::milliseconds(100));
};
uint16_t V1290::micro_read() const {
micro_wait(2);
return read16(0x102E);
};
void V1290::micro_write(uint16_t value) {
micro_wait(1);
write16(0x102E, value);
};
void V1290::micro_write(uint16_t value) const {
const_cast<V1290*>(this)->micro_write(value);
};
int16_t V1290::seconds_to_cycles(float seconds) const {
return std::round(seconds / 25e-9);
};
};