Line data Source code
1 : /**
2 : * @file DaphneV3ControllerModule.cpp
3 : *
4 : * Implementation of the DAQModule to control the Daphne mezzanine board
5 : *
6 : * This is part of the DUNE DAQ Software Suite, copyright 2020.
7 : * Licensing/copyright details are in the COPYING file that you should have
8 : * received with this code.
9 : */
10 :
11 : #include "DaphneV3ControllerModule.hpp"
12 : #include "logging/Logging.hpp"
13 : #include "daphnemodules/daphne_control_high.pb.h"
14 :
15 : #include "appmodel/DaphneConf.hpp"
16 : #include "appmodel/DaphneV2BoardConf.hpp"
17 : #include "appmodel/DaphneV2Channel.hpp"
18 : #include "appmodel/DaphneV2AFE.hpp"
19 : #include "appmodel/DaphneV2ADC.hpp"
20 : #include "appmodel/DaphneV2PGA.hpp"
21 : #include "appmodel/DaphneV2LNA.hpp"
22 :
23 : #include "daphnemodules/opmon/DaphneControllerModule.pb.h"
24 :
25 : #include <fmt/format.h>
26 : #include <zmq.hpp>
27 :
28 : #include <regex>
29 : #include <string>
30 : #include <memory>
31 : #include <utility>
32 :
33 : namespace dunedaq::daphnemodules {
34 :
35 0 : DaphneV3ControllerModule::DaphneV3ControllerModule(const std::string& name)
36 0 : : appfwk::DAQModule(name)
37 : {
38 0 : register_command("conf", &DaphneV3ControllerModule::do_conf);
39 0 : register_command("start", &DaphneV3ControllerModule::do_start);
40 0 : register_command("scrap", &DaphneV3ControllerModule::do_scrap);
41 0 : }
42 :
43 0 : void DaphneV3ControllerModule::init(std::shared_ptr<appfwk::ConfigurationManager> cfg)
44 : {
45 0 : auto mdal = cfg->get_dal<conf_t>(get_name());
46 0 : if (!mdal) {
47 0 : throw ConfigurationFailed(ERS_HERE, get_name());
48 : }
49 0 : m_module_config = mdal;
50 0 : }
51 :
52 :
53 0 : void DaphneV3ControllerModule::do_conf(const CommandData_t&)
54 : {
55 :
56 0 : const std::lock_guard<std::mutex> lock(m_mutex);
57 :
58 0 : TLOG() << get_name() << " starting configuring";
59 0 : auto start_time = std::chrono::high_resolution_clock::now();
60 :
61 :
62 0 : using namespace daphne;
63 :
64 0 : auto board_conf = m_module_config->get_board_conf();
65 0 : auto general_conf = m_module_config->get_daphne_conf();
66 :
67 0 : create_interface( board_conf->get_address(),
68 : general_conf->get_timeout() );
69 :
70 : // validation to be taken from the previous version
71 : // this should include the slot check
72 0 : validate_configuration(*board_conf);
73 :
74 0 : configure_analog_chain(true);
75 :
76 0 : auto end_time = std::chrono::high_resolution_clock::now();
77 :
78 0 : auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
79 0 : TLOG() << get_name() << ": board configured in " << duration.count() << " microseconds";
80 :
81 0 : }
82 :
83 0 : void DaphneV3ControllerModule::do_start(const CommandData_t& ) { ; }
84 0 : void DaphneV3ControllerModule::do_scrap(const CommandData_t&) {
85 :
86 0 : m_scrap_called.store(true);
87 0 : const std::lock_guard<std::mutex> lock(m_mutex);
88 :
89 0 : TLOG() << get_name() << " starting scrap";
90 0 : auto start_time = std::chrono::high_resolution_clock::now();
91 :
92 0 : using namespace daphne;
93 :
94 0 : configure_analog_chain(false);
95 :
96 0 : m_iface = nullptr;
97 :
98 0 : auto end_time = std::chrono::high_resolution_clock::now();
99 0 : auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
100 0 : TLOG() << get_name() << ": scrapped in " << duration.count() << " microseconds";
101 0 : }
102 :
103 0 : void DaphneV3ControllerModule::configure_analog_chain(bool initial_config) {
104 :
105 0 : auto general_conf = m_module_config->get_daphne_conf();
106 0 : auto board_conf = initial_config ? m_module_config->get_board_conf() :
107 0 : general_conf->get_default_v3_settings();
108 :
109 : // Step 1: Build the ConfigureRequest
110 0 : daphne::ConfigureRequest req;
111 0 : req.set_daphne_address(board_conf->get_address());
112 0 : req.set_slot(board_conf->get_slot_id());
113 0 : req.set_timeout_ms(general_conf->get_timeout_ms());
114 0 : req.set_biasctrl(board_conf->get_bias_ctrl());
115 0 : req.set_self_trigger_threshold(board_conf->get_self_trigger_threshold());
116 0 : req.set_self_trigger_xcorr(board_conf->get_self_trigger_xcorr());
117 0 : req.set_tp_conf(board_conf->get_tp_conf());
118 0 : req.set_compensator(board_conf->get_compensator());
119 0 : req.set_inverters(board_conf->get_inverter());
120 :
121 0 : for ( ChannelId ch = 0; ch < s_max_channels; ++ch ) {
122 :
123 0 : const auto & channel_conf = board_conf->get_channel(ch);
124 : // get_channel returns the running value if the bool argument is true,
125 : // and the default values when the bool argument is false
126 : // hence, this loop does both the job of enabling and disebling
127 :
128 0 : auto* channel = req.add_channels();
129 0 : channel->set_id(ch);
130 0 : channel->set_trim(channel_conf.get_trim());
131 0 : channel->set_offset(channel_conf.get_offset());
132 0 : channel->set_gain(channel_conf.get_gain());
133 :
134 : } // loop over channels
135 :
136 :
137 0 : for ( AFEId id = 0; id < s_max_afes; ++id ) {
138 :
139 0 : const auto & afe_conf = board_conf->get_afe(id);
140 :
141 0 : auto* afe = req.add_afes();
142 0 : afe->set_id(id);
143 0 : afe->set_attenuators(afe_conf.get_attenuator());
144 0 : afe->set_v_bias(afe_conf.get_v_bias());
145 :
146 0 : auto* adc = afe_conf.get_adc();
147 0 : afe->mutable_adc()->set_resolution(adc->get_low_resolution());
148 0 : afe->mutable_adc()->set_output_format(adc->get_output_offset_binary());
149 0 : afe->mutable_adc()->set_sb_first(adc->get_MSB_first());
150 :
151 0 : auto* pga = afe_conf.get_pga();
152 0 : afe->mutable_pga()->set_lpf_cut_frequency(pga->get_lpf_cut_frequency());
153 0 : afe->mutable_pga()->set_integrator_disable(pga->get_integrator_disable());
154 0 : afe->mutable_pga()->set_gain(pga->get_gain());
155 :
156 0 : auto* lna = afe_conf.get_lna();
157 0 : afe->mutable_lna()->set_clamp(lna->get_clamp());
158 0 : afe->mutable_lna()->set_gain(lna->get_gain());
159 0 : afe->mutable_lna()->set_integrator_disable(lna->get_integrator_disable());
160 :
161 : } // loop over AFE
162 :
163 0 : TLOG() << get_name() << ": Configuration message ready to send";
164 :
165 0 : auto response = m_iface.load()->send<daphne::ConfigureResponse>( req.SerializeAsString(),
166 : daphne::MT2_CONFIGURE_FE_REQ,
167 0 : daphne::MT2_CONFIGURE_FE_RESP );
168 :
169 0 : if ( ! response.success() ) {
170 0 : throw UnsuccessfulConfiguration(ERS_HERE, get_name(), response.message());
171 : }
172 :
173 0 : TLOG() << "Success message: " << response.message();
174 :
175 0 : }
176 :
177 0 : void DaphneV3ControllerModule::create_interface( const std::string & address,
178 : std::chrono::milliseconds timeout ) {
179 :
180 0 : m_iface = make_unique<DaphneV3Interface>( address, get_name(), timeout);
181 :
182 0 : }
183 :
184 0 : void DaphneV3ControllerModule::validate_configuration(const appmodel::DaphneV2BoardConf & c) const {
185 :
186 0 : const auto & channel_confs = c.get_active_channels();
187 :
188 0 : for ( const auto & ch : channel_confs ) {
189 0 : auto id = ch->get_channel_id();
190 :
191 : //CH OFFSET maximum is 2700 if GAIN is 1, 1500 if GAIN is 2
192 0 : auto gain = ch->get_gain();
193 0 : if ( gain != 1 && gain != 2 ) {
194 0 : throw InvalidChannelConfiguration(ERS_HERE,
195 0 : id, ch->get_trim(), ch->get_offset(), gain);
196 : }
197 0 : auto offset = ch -> get_offset();
198 0 : if ( gain == 1 ) {
199 0 : if ( offset > 2700 ) {
200 0 : throw InvalidChannelConfiguration(ERS_HERE, id, ch->get_trim(), offset, gain);
201 : }
202 0 : } else if ( gain == 2 ) {
203 0 : if ( offset > 1500 ) {
204 0 : throw InvalidChannelConfiguration(ERS_HERE, id, ch->get_trim(), offset, gain);
205 : }
206 : }
207 : } // loop over channels
208 :
209 0 : auto size = c.get_full_stream_channels().size();
210 0 : if (size>16) {
211 : // we can only stream 16 channels at most
212 0 : throw TooManyChannels( ERS_HERE, size );
213 : }
214 :
215 0 : }
216 :
217 : void
218 0 : DaphneV3ControllerModule::generate_opmon_data() {
219 :
220 0 : if ( ! m_iface.load() ) return ;
221 :
222 0 : if ( m_scrap_called.load() ) return;
223 :
224 0 : std::unique_lock<std::mutex> lock(m_mutex);
225 :
226 0 : daphne::ReadTriggerCountersRequest req;
227 0 : auto response = m_iface.load()->send<daphne::ReadTriggerCountersResponse>( req.SerializeAsString(),
228 : daphne::MT2_READ_TRIGGER_COUNTERS_REQ,
229 0 : daphne::MT2_READ_TRIGGER_COUNTERS_RESP );
230 :
231 0 : if ( ! response.success() ) {
232 0 : ers::warning( TriggerMonitoringFailed(ERS_HERE, get_name(), response.message() ) );
233 0 : return;
234 : }
235 :
236 0 : lock.unlock();
237 :
238 0 : const auto & snapshots = response.snapshots();
239 :
240 0 : static uint32_t def_threshold = 0x3ff; // NOLINT
241 :
242 0 : for ( const auto & c : snapshots ) {
243 :
244 : // we only publish channels info when threshold is not default or the counters are not zero
245 0 : if ( c.threshold() == def_threshold
246 0 : && c.record_count() == 0
247 0 : && c.busy_count() == 0
248 0 : && c.full_count() == 0 ) continue;
249 :
250 0 : opmon::TempTriggerSnapshotInfo info;
251 0 : info.set_threshold( c.threshold() );
252 0 : info.set_record_count( c.record_count() );
253 0 : info.set_busy_count( c.busy_count() );
254 0 : info.set_full_count( c.full_count() );
255 :
256 0 : publish( std::move(info), {{"channel", fmt::format("{}", c.channel() ) }} );
257 0 : }
258 :
259 0 : }
260 :
261 :
262 : } // namespace dunedaq::daphnemodules
263 :
264 0 : DEFINE_DUNE_DAQ_MODULE(dunedaq::daphnemodules::DaphneV3ControllerModule)
|