aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/dboard/twinrx/twinrx_experts.hpp
blob: 7b85137e14a5e2181c392f3b988b50a43564ce95 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//
// Copyright 2016 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef INCLUDED_DBOARD_TWINRX_EXPERTS_HPP
#define INCLUDED_DBOARD_TWINRX_EXPERTS_HPP

#include "twinrx_ctrl.hpp"
#include "expert_nodes.hpp"
#include <uhd/utils/math.hpp>

namespace uhd { namespace usrp { namespace dboard { namespace twinrx {

//---------------------------------------------------------
// Misc types and definitions
//---------------------------------------------------------

struct rf_freq_abs_t : public uhd::math::fp_compare::fp_compare_delta<double> {
    rf_freq_abs_t(double freq = 0.0, double epsilon = 1.0 /* 1Hz epsilon */) :
        uhd::math::fp_compare::fp_compare_delta<double>(freq, epsilon) {}
    inline double get() const { return _value; }
};

struct rf_freq_ppm_t : public rf_freq_abs_t {
    rf_freq_ppm_t(double freq = 0.0, double epsilon_ppm = 0.1  /* 1PPM epsilon */) :
        rf_freq_abs_t(freq, 1e-6 * freq * epsilon_ppm) {}
};

enum lo_stage_t { STAGE_LO1, STAGE_LO2 };
enum lo_inj_side_t { INJ_LOW_SIDE, INJ_HIGH_SIDE };
enum lo_synth_mapping_t { MAPPING_NONE, MAPPING_CH0, MAPPING_CH1, MAPPING_SHARED };

static const std::string prepend_ch(std::string name, const std::string& ch) {
    return ch + "/" + name;
}

static const std::string lo_stage_str(lo_stage_t stage, bool lower = false) {
    std::string prefix = lower ? "lo" : "LO";
    return prefix + ((stage == STAGE_LO1) ? "1" : "2");
}


/*!---------------------------------------------------------
 * twinrx_scheduling_expert
 *
 * This expert is responsible for scheduling time sensitive actions
 * in other experts. It responds to changes in the command time and
 * selectively causes experts to run in order to ensure a synchronized
 * system.
 *
 * ---------------------------------------------------------
 */
class twinrx_scheduling_expert : public experts::worker_node_t {
public:
    twinrx_scheduling_expert(const experts::node_retriever_t& db, std::string ch)
    : experts::worker_node_t(prepend_ch("twinrx_scheduling_expert", ch)),
      _command_time     (db, prepend_ch("time/cmd", ch)),
      _rx_frontend_time (db, prepend_ch("time/rx_frontend", ch))
    {
        bind_accessor(_command_time);
        bind_accessor(_rx_frontend_time);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<time_spec_t>    _command_time;

    //Outputs
    experts::data_writer_t<time_spec_t>    _rx_frontend_time;
};

/*!---------------------------------------------------------
 * twinrx_freq_path_expert
 *
 * This expert is responsble for translating a user-specified
 * RF and IF center frequency into TwinRX specific settings
 * like band, preselector path, LO frequency and injection
 * sides for both the LO stages.
 *
 * One instance of this expert is required for each channel
 * ---------------------------------------------------------
 */
class twinrx_freq_path_expert : public experts::worker_node_t {
public:
    twinrx_freq_path_expert(const experts::node_retriever_t& db, std::string ch)
    : experts::worker_node_t(prepend_ch("twinrx_freq_path_expert", ch)),
      _rf_freq_d        (db, prepend_ch("freq/desired", ch)),
      _if_freq_d        (db, prepend_ch("if_freq/desired", ch)),
      _signal_path      (db, prepend_ch("ch/signal_path", ch)),
      _lb_presel        (db, prepend_ch("ch/lb_presel", ch)),
      _hb_presel        (db, prepend_ch("ch/hb_presel", ch)),
      _lb_preamp_presel (db, prepend_ch("ch/lb_preamp_presel", ch)),
      _lo1_freq_d       (db, prepend_ch("los/LO1/freq/desired", ch)),
      _lo2_freq_d       (db, prepend_ch("los/LO2/freq/desired", ch)),
      _lo1_inj_side     (db, prepend_ch("ch/LO1/inj_side", ch)),
      _lo2_inj_side     (db, prepend_ch("ch/LO2/inj_side", ch))
    {
        bind_accessor(_rf_freq_d);
        bind_accessor(_if_freq_d);
        bind_accessor(_signal_path);
        bind_accessor(_lb_presel);
        bind_accessor(_hb_presel);
        bind_accessor(_lb_preamp_presel);
        bind_accessor(_lo1_freq_d);
        bind_accessor(_lo2_freq_d);
        bind_accessor(_lo1_inj_side);
        bind_accessor(_lo2_inj_side);
    }

private:
    virtual void resolve();
    static lo_inj_side_t _compute_lo2_inj_side(
        double lo1_freq, double if1_freq,  double if2_freq, double bandwidth);
    static bool _has_mixer_spurs(
        double lo1_freq, double lo2_freq, double if2_freq,
        double bandwidth, int spur_order);

    //Inputs
    experts::data_reader_t<double>                          _rf_freq_d;
    experts::data_reader_t<double>                          _if_freq_d;
    //Outputs
    experts::data_writer_t<twinrx_ctrl::signal_path_t>      _signal_path;
    experts::data_writer_t<twinrx_ctrl::preselector_path_t> _lb_presel;
    experts::data_writer_t<twinrx_ctrl::preselector_path_t> _hb_presel;
    experts::data_writer_t<bool>                            _lb_preamp_presel;
    experts::data_writer_t<double>                          _lo1_freq_d;
    experts::data_writer_t<double>                          _lo2_freq_d;
    experts::data_writer_t<lo_inj_side_t>                   _lo1_inj_side;
    experts::data_writer_t<lo_inj_side_t>                   _lo2_inj_side;
};

/*!---------------------------------------------------------
 * twinrx_lo_config_expert
 *
 * This expert is responsible for translating high level
 * channel-scoped  LO source and export settings to low-level
 * channel-scoped settings. The expert only deals with
 * the source and export attributes, not frequency.
 *
 * One instance of this expert is required for all channels
 * ---------------------------------------------------------
 */
class twinrx_lo_config_expert : public experts::worker_node_t {
public:
    twinrx_lo_config_expert(const experts::node_retriever_t& db)
    : experts::worker_node_t("twinrx_lo_config_expert"),
      _lo_source_ch0    (db, prepend_ch("los/all/source", "0")),
      _lo_source_ch1    (db, prepend_ch("los/all/source", "1")),
      _lo_export_ch0    (db, prepend_ch("los/all/export", "0")),
      _lo_export_ch1    (db, prepend_ch("los/all/export", "1")),
      _lo1_src_ch0      (db, prepend_ch("ch/LO1/source", "0")),
      _lo1_src_ch1      (db, prepend_ch("ch/LO1/source", "1")),
      _lo2_src_ch0      (db, prepend_ch("ch/LO2/source", "0")),
      _lo2_src_ch1      (db, prepend_ch("ch/LO2/source", "1")),
      _lo1_export_src   (db, "com/LO1/export_source"),
      _lo2_export_src   (db, "com/LO2/export_source")
    {
        bind_accessor(_lo_source_ch0);
        bind_accessor(_lo_source_ch1);
        bind_accessor(_lo_export_ch0);
        bind_accessor(_lo_export_ch1);
        bind_accessor(_lo1_src_ch0);
        bind_accessor(_lo1_src_ch1);
        bind_accessor(_lo2_src_ch0);
        bind_accessor(_lo2_src_ch1);
        bind_accessor(_lo1_export_src);
        bind_accessor(_lo2_export_src);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<std::string>                     _lo_source_ch0;
    experts::data_reader_t<std::string>                     _lo_source_ch1;
    experts::data_reader_t<bool>                            _lo_export_ch0;
    experts::data_reader_t<bool>                            _lo_export_ch1;
    //Outputs
    experts::data_writer_t<twinrx_ctrl::lo_source_t>        _lo1_src_ch0;
    experts::data_writer_t<twinrx_ctrl::lo_source_t>        _lo1_src_ch1;
    experts::data_writer_t<twinrx_ctrl::lo_source_t>        _lo2_src_ch0;
    experts::data_writer_t<twinrx_ctrl::lo_source_t>        _lo2_src_ch1;
    experts::data_writer_t<twinrx_ctrl::lo_export_source_t> _lo1_export_src;
    experts::data_writer_t<twinrx_ctrl::lo_export_source_t> _lo2_export_src;
};

/*!---------------------------------------------------------
 * twinrx_lo_mapping_expert
 *
 * This expert is responsible for translating low-level
 * channel-scoped  LO source and export settings to low-level
 * synthesizer-scoped settings. The expert deals with the
 * extremely flexible channel->synthesizer mapping and handles
 * frequency hopping modes.
 *
 * One instance of this expert is required for each LO stage
 * ---------------------------------------------------------
 */
class twinrx_lo_mapping_expert : public experts::worker_node_t {
public:
    twinrx_lo_mapping_expert(const experts::node_retriever_t& db, lo_stage_t stage)
    : experts::worker_node_t("twinrx_" + lo_stage_str(stage, true) + "_mapping_expert"),
      _lox_src_ch0          (db, prepend_ch("ch/" + lo_stage_str(stage) + "/source", "0")),
      _lox_src_ch1          (db, prepend_ch("ch/" + lo_stage_str(stage) + "/source", "1")),
      _lox_mapping_synth0   (db, prepend_ch("synth/" + lo_stage_str(stage) + "/mapping", "0")),
      _lox_mapping_synth1   (db, prepend_ch("synth/" + lo_stage_str(stage) + "/mapping", "1")),
      _lox_hopping_enabled  (db, "com/synth/" + lo_stage_str(stage) + "/hopping_enabled")
    {
        bind_accessor(_lox_src_ch0);
        bind_accessor(_lox_src_ch1);
        bind_accessor(_lox_mapping_synth0);
        bind_accessor(_lox_mapping_synth1);
        bind_accessor(_lox_hopping_enabled);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<twinrx_ctrl::lo_source_t>    _lox_src_ch0;
    experts::data_reader_t<twinrx_ctrl::lo_source_t>    _lox_src_ch1;
    //Outputs
    experts::data_writer_t<lo_synth_mapping_t>          _lox_mapping_synth0;
    experts::data_writer_t<lo_synth_mapping_t>          _lox_mapping_synth1;
    experts::data_writer_t<bool>                        _lox_hopping_enabled;
};

/*!---------------------------------------------------------
 * twinrx_freq_coercion_expert
 *
 * This expert is responsible for calculating the coerced
 * RF frequency after most settings and modes have been
 * resolved.
 *
 * One instance of this expert is required for each channel
 * ---------------------------------------------------------
 */
class twinrx_freq_coercion_expert : public experts::worker_node_t {
public:
    twinrx_freq_coercion_expert(const experts::node_retriever_t& db, std::string ch)
    : experts::worker_node_t(prepend_ch("twinrx_freq_coercion_expert", ch)),
      _lo1_freq_c       (db, prepend_ch("los/LO1/freq/coerced", ch)),
      _lo2_freq_c       (db, prepend_ch("los/LO2/freq/coerced", ch)),
      _if_freq_d        (db, prepend_ch("if_freq/desired", ch)),
      _lo1_inj_side     (db, prepend_ch("ch/LO1/inj_side", ch)),
      _lo2_inj_side     (db, prepend_ch("ch/LO2/inj_side", ch)),
      _rf_freq_c        (db, prepend_ch("freq/coerced", ch))
    {
        bind_accessor(_lo1_freq_c);
        bind_accessor(_lo2_freq_c);
        bind_accessor(_if_freq_d);
        bind_accessor(_lo1_inj_side);
        bind_accessor(_lo2_inj_side);
        bind_accessor(_rf_freq_c);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<double>          _lo1_freq_c;
    experts::data_reader_t<double>          _lo2_freq_c;
    experts::data_reader_t<double>          _if_freq_d;
    experts::data_reader_t<lo_inj_side_t>   _lo1_inj_side;
    experts::data_reader_t<lo_inj_side_t>   _lo2_inj_side;
    //Outputs
    experts::data_writer_t<double>          _rf_freq_c;
};

/*!---------------------------------------------------------
 * twinrx_nyquist_expert
 *
 * This expert is responsible for figuring out the DSP
 * front-end settings required for each channel
 *
 * One instance of this expert is required for each channel
 * ---------------------------------------------------------
 */
class twinrx_nyquist_expert : public experts::worker_node_t {
public:
    twinrx_nyquist_expert(const experts::node_retriever_t& db, std::string ch,
                          dboard_iface::sptr db_iface)
    : experts::worker_node_t(prepend_ch("twinrx_nyquist_expert", ch)),
      _channel          (ch),
      _codec_conn       (ch=="0"?"II":"QQ"),    //Ch->ADC Port mapping
      _lo1_freq_d       (db, prepend_ch("los/LO1/freq/desired", ch)),
      _lo2_freq_d       (db, prepend_ch("los/LO2/freq/desired", ch)),
      _if_freq_d        (db, prepend_ch("if_freq/desired", ch)),
      _lo1_inj_side     (db, prepend_ch("ch/LO1/inj_side", ch)),
      _lo2_inj_side     (db, prepend_ch("ch/LO2/inj_side", ch)),
      _rx_frontend_time (db, prepend_ch("time/rx_frontend", ch)),
      _if_freq_c        (db, prepend_ch("if_freq/coerced", ch)),
      _db_iface         (db_iface)
    {
        bind_accessor(_lo1_freq_d);
        bind_accessor(_lo2_freq_d);
        bind_accessor(_if_freq_d);
        bind_accessor(_lo1_inj_side);
        bind_accessor(_lo2_inj_side);
        bind_accessor(_if_freq_c);
        bind_accessor(_rx_frontend_time);
    }

private:
    virtual void resolve();

    //Inputs
    const std::string                                       _channel;
    const std::string                                       _codec_conn;
    experts::data_reader_t<double>                          _lo1_freq_d;
    experts::data_reader_t<double>                          _lo2_freq_d;
    experts::data_reader_t<double>                          _if_freq_d;
    experts::data_reader_t<lo_inj_side_t>                   _lo1_inj_side;
    experts::data_reader_t<lo_inj_side_t>                   _lo2_inj_side;
    experts::data_reader_t<time_spec_t>                     _rx_frontend_time;

    //Outputs
    experts::data_writer_t<double>                          _if_freq_c;
    dboard_iface::sptr                                      _db_iface;

    //Misc
    time_spec_t  _cached_cmd_time;
};

/*!---------------------------------------------------------
 * twinrx_antenna_expert
 *
 * This expert is responsible for translating high-level
 * antenna selection settings and channel enables to low-level
 * switch configurations.
 *
 * One instance of this expert is required for all channels
 * ---------------------------------------------------------
 */
class twinrx_antenna_expert : public experts::worker_node_t {
public:
    twinrx_antenna_expert(const experts::node_retriever_t& db)
    : experts::worker_node_t("twinrx_antenna_expert"),
      _antenna_ch0      (db, prepend_ch("antenna", "0")),
      _antenna_ch1      (db, prepend_ch("antenna", "1")),
      _enabled_ch0      (db, prepend_ch("enabled", "0")),
      _enabled_ch1      (db, prepend_ch("enabled", "1")),
      _lo_export_ch0    (db, prepend_ch("los/all/export", "0")),
      _lo_export_ch1    (db, prepend_ch("los/all/export", "1")),
      _ant_mapping      (db, "com/ant_mapping"),
      _cal_mode         (db, "com/cal_mode")
    {
        bind_accessor(_antenna_ch0);
        bind_accessor(_antenna_ch1);
        bind_accessor(_enabled_ch0);
        bind_accessor(_enabled_ch1);
        bind_accessor(_lo_export_ch0);
        bind_accessor(_lo_export_ch1);
        bind_accessor(_ant_mapping);
        bind_accessor(_cal_mode);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<std::string>                     _antenna_ch0;
    experts::data_reader_t<std::string>                     _antenna_ch1;
    experts::data_reader_t<bool>                            _enabled_ch0;
    experts::data_reader_t<bool>                            _enabled_ch1;
    experts::data_reader_t<bool>                            _lo_export_ch0;
    experts::data_reader_t<bool>                            _lo_export_ch1;
    //Outputs
    experts::data_writer_t<twinrx_ctrl::antenna_mapping_t>  _ant_mapping;
    experts::data_writer_t<twinrx_ctrl::cal_mode_t>         _cal_mode;
};

/*!---------------------------------------------------------
 * twinrx_chan_gain_expert
 *
 * This expert is responsible for mapping high-level channel
 * gain settings to individual attenuator and amp configurations
 * that are also channel-scoped. This expert will implement
 * the gain distribution strategy.
 *
 * One instance of this expert is required for each channel
 * ---------------------------------------------------------
 */
class twinrx_chan_gain_expert : public experts::worker_node_t {
public:
    twinrx_chan_gain_expert(const experts::node_retriever_t& db, std::string ch)
    : experts::worker_node_t(prepend_ch("twinrx_chan_gain_expert", ch)),
      _gain         (db, prepend_ch("gain", ch)),
      _gain_profile (db, prepend_ch("gain_profile", ch)),
      _signal_path  (db, prepend_ch("ch/signal_path", ch)),
      _lb_presel    (db, prepend_ch("ch/lb_presel", ch)),
      _hb_presel    (db, prepend_ch("ch/hb_presel", ch)),
      _ant_mapping  (db, "com/ant_mapping"),
      _input_atten  (db, prepend_ch("ch/input_atten", ch)),
      _lb_atten     (db, prepend_ch("ch/lb_atten", ch)),
      _hb_atten     (db, prepend_ch("ch/hb_atten", ch)),
      _preamp1      (db, prepend_ch("ch/preamp1", ch)),
      _preamp2      (db, prepend_ch("ch/preamp2", ch))
    {
        bind_accessor(_gain);
        bind_accessor(_gain_profile);
        bind_accessor(_signal_path);
        bind_accessor(_lb_presel);
        bind_accessor(_hb_presel);
        bind_accessor(_ant_mapping);
        bind_accessor(_input_atten);
        bind_accessor(_lb_atten);
        bind_accessor(_hb_atten);
        bind_accessor(_preamp1);
        bind_accessor(_preamp2);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<double>                          _gain;
    experts::data_reader_t<std::string>                     _gain_profile;
    experts::data_reader_t<twinrx_ctrl::signal_path_t>      _signal_path;
    experts::data_reader_t<twinrx_ctrl::preselector_path_t> _lb_presel;
    experts::data_reader_t<twinrx_ctrl::preselector_path_t> _hb_presel;
    experts::data_reader_t<twinrx_ctrl::antenna_mapping_t>  _ant_mapping;
    //Outputs
    experts::data_writer_t<uint8_t>                  _input_atten;
    experts::data_writer_t<uint8_t>                  _lb_atten;
    experts::data_writer_t<uint8_t>                  _hb_atten;
    experts::data_writer_t<twinrx_ctrl::preamp_state_t>     _preamp1;
    experts::data_writer_t<bool>                            _preamp2;
};

/*!---------------------------------------------------------
 * twinrx_ant_gain_expert
 *
 * This expert is responsible for translating between the
 * channel-scoped low-level gain settings to antenna-scoped
 * gain settings.
 *
 * One instance of this expert is required for all channels
 * ---------------------------------------------------------
 */
class twinrx_ant_gain_expert : public experts::worker_node_t {
public:
    twinrx_ant_gain_expert(const experts::node_retriever_t& db)
    : experts::worker_node_t("twinrx_ant_gain_expert"),
      _ant_mapping          (db, "com/ant_mapping"),
      _ch0_input_atten      (db, prepend_ch("ch/input_atten", "0")),
      _ch0_preamp1          (db, prepend_ch("ch/preamp1", "0")),
      _ch0_preamp2          (db, prepend_ch("ch/preamp2", "0")),
      _ch0_lb_preamp_presel (db, prepend_ch("ch/lb_preamp_presel", "0")),
      _ch1_input_atten      (db, prepend_ch("ch/input_atten", "1")),
      _ch1_preamp1          (db, prepend_ch("ch/preamp1", "1")),
      _ch1_preamp2          (db, prepend_ch("ch/preamp2", "1")),
      _ch1_lb_preamp_presel (db, prepend_ch("ch/lb_preamp_presel", "1")),
      _ant0_input_atten     (db, prepend_ch("ant/input_atten", "0")),
      _ant0_preamp1         (db, prepend_ch("ant/preamp1", "0")),
      _ant0_preamp2         (db, prepend_ch("ant/preamp2", "0")),
      _ant0_lb_preamp_presel(db, prepend_ch("ant/lb_preamp_presel", "0")),
      _ant1_input_atten     (db, prepend_ch("ant/input_atten", "1")),
      _ant1_preamp1         (db, prepend_ch("ant/preamp1", "1")),
      _ant1_preamp2         (db, prepend_ch("ant/preamp2", "1")),
      _ant1_lb_preamp_presel(db, prepend_ch("ant/lb_preamp_presel", "1"))
    {
        bind_accessor(_ant_mapping);
        bind_accessor(_ch0_input_atten);
        bind_accessor(_ch0_preamp1);
        bind_accessor(_ch0_preamp2);
        bind_accessor(_ch0_lb_preamp_presel);
        bind_accessor(_ch1_input_atten);
        bind_accessor(_ch1_preamp1);
        bind_accessor(_ch1_preamp2);
        bind_accessor(_ch1_lb_preamp_presel);
        bind_accessor(_ant0_input_atten);
        bind_accessor(_ant0_preamp1);
        bind_accessor(_ant0_preamp2);
        bind_accessor(_ant0_lb_preamp_presel);
        bind_accessor(_ant1_input_atten);
        bind_accessor(_ant1_preamp1);
        bind_accessor(_ant1_preamp2);
        bind_accessor(_ant1_lb_preamp_presel);
    }

private:
    virtual void resolve();

    //Inputs
    experts::data_reader_t<twinrx_ctrl::antenna_mapping_t>  _ant_mapping;
    experts::data_reader_t<uint8_t>                  _ch0_input_atten;
    experts::data_reader_t<twinrx_ctrl::preamp_state_t>     _ch0_preamp1;
    experts::data_reader_t<bool>                            _ch0_preamp2;
    experts::data_reader_t<bool>                            _ch0_lb_preamp_presel;
    experts::data_reader_t<uint8_t>                  _ch1_input_atten;
    experts::data_reader_t<twinrx_ctrl::preamp_state_t>     _ch1_preamp1;
    experts::data_reader_t<bool>                            _ch1_preamp2;
    experts::data_reader_t<bool>                            _ch1_lb_preamp_presel;

    //Outputs
    experts::data_writer_t<uint8_t>                  _ant0_input_atten;
    experts::data_writer_t<twinrx_ctrl::preamp_state_t>     _ant0_preamp1;
    experts::data_writer_t<bool>                            _ant0_preamp2;
    experts::data_writer_t<bool>                            _ant0_lb_preamp_presel;
    experts::data_writer_t<uint8_t>                  _ant1_input_atten;
    experts::data_writer_t<twinrx_ctrl::preamp_state_t>     _ant1_preamp1;
    experts::data_writer_t<bool>                            _ant1_preamp2;
    experts::data_writer_t<bool>                            _ant1_lb_preamp_presel;
};

/*!---------------------------------------------------------
 * twinrx_settings_expert
 *
 * This expert is responsible for gathering all low-level
 * settings and writing them to hardware. All LO frequency
 * settings are cached with a hysteresis. All other settings
 * are always written to twinrx_ctrl and rely on register
 * level caching.
 *
 * One instance of this expert is required for all channels
 * ---------------------------------------------------------
 */
class twinrx_settings_expert : public experts::worker_node_t {
public:
    twinrx_settings_expert(const experts::node_retriever_t& db, twinrx_ctrl::sptr ctrl)
    : experts::worker_node_t("twinrx_settings_expert"), _ctrl(ctrl),
      _ch0              (db, "0"),
      _ch1              (db, "1"),
      _lo1_synth0_mapping(db, "0/synth/LO1/mapping"),
      _lo1_synth1_mapping(db, "1/synth/LO1/mapping"),
      _lo2_synth0_mapping(db, "0/synth/LO2/mapping"),
      _lo2_synth1_mapping(db, "1/synth/LO2/mapping"),
      _lo1_hopping_enabled(db, "com/synth/LO1/hopping_enabled"),
      _lo2_hopping_enabled(db, "com/synth/LO2/hopping_enabled"),
      _lo1_export_src   (db, "com/LO1/export_source"),
      _lo2_export_src   (db, "com/LO2/export_source"),
      _ant_mapping      (db, "com/ant_mapping"),
      _cal_mode         (db, "com/cal_mode")
    {
        for (size_t i = 0; i < 2; i++) {
            ch_settings& ch = (i==1) ? _ch1 : _ch0;
            bind_accessor(ch.chan_enabled);
            bind_accessor(ch.preamp1);
            bind_accessor(ch.preamp2);
            bind_accessor(ch.lb_preamp_presel);
            bind_accessor(ch.signal_path);
            bind_accessor(ch.lb_presel);
            bind_accessor(ch.hb_presel);
            bind_accessor(ch.input_atten);
            bind_accessor(ch.lb_atten);
            bind_accessor(ch.hb_atten);
            bind_accessor(ch.lo1_source);
            bind_accessor(ch.lo2_source);
            bind_accessor(ch.lo1_freq_d);
            bind_accessor(ch.lo2_freq_d);
            bind_accessor(ch.lo1_freq_c);
            bind_accessor(ch.lo2_freq_c);
        }
        bind_accessor(_lo1_synth0_mapping);
        bind_accessor(_lo1_synth1_mapping);
        bind_accessor(_lo2_synth0_mapping);
        bind_accessor(_lo2_synth1_mapping);
        bind_accessor(_lo1_hopping_enabled);
        bind_accessor(_lo2_hopping_enabled);
        bind_accessor(_lo1_export_src);
        bind_accessor(_lo2_export_src);
        bind_accessor(_ant_mapping);
        bind_accessor(_cal_mode);
    }

private:
    virtual void resolve();
    void _resolve_lox_freq(
        lo_stage_t                      lo_stage,
        experts::data_reader_t<double>& ch0_freq_d,
        experts::data_reader_t<double>& ch1_freq_d,
        experts::data_writer_t<double>& ch0_freq_c,
        experts::data_writer_t<double>& ch1_freq_c,
        twinrx_ctrl::lo_source_t        ch0_lo_source,
        twinrx_ctrl::lo_source_t        ch1_lo_source,
        lo_synth_mapping_t              synth0_mapping,
        lo_synth_mapping_t              synth1_mapping,
        bool                            hopping_enabled);
    double _set_lox_synth_freq(lo_stage_t stage, twinrx_ctrl::channel_t ch, double freq);

    class ch_settings {
    public:
        ch_settings(const experts::node_retriever_t& db, const std::string& ch) :
            chan_enabled    (db, prepend_ch("enabled", ch)),
            preamp1         (db, prepend_ch("ant/preamp1", ch)),
            preamp2         (db, prepend_ch("ant/preamp2", ch)),
            lb_preamp_presel(db, prepend_ch("ant/lb_preamp_presel", ch)),
            signal_path     (db, prepend_ch("ch/signal_path", ch)),
            lb_presel       (db, prepend_ch("ch/lb_presel", ch)),
            hb_presel       (db, prepend_ch("ch/hb_presel", ch)),
            input_atten     (db, prepend_ch("ant/input_atten", ch)),
            lb_atten        (db, prepend_ch("ch/lb_atten", ch)),
            hb_atten        (db, prepend_ch("ch/hb_atten", ch)),
            lo1_source      (db, prepend_ch("ch/LO1/source", ch)),
            lo2_source      (db, prepend_ch("ch/LO2/source", ch)),
            lo1_freq_d      (db, prepend_ch("los/LO1/freq/desired", ch)),
            lo2_freq_d      (db, prepend_ch("los/LO2/freq/desired", ch)),
            lo1_freq_c      (db, prepend_ch("los/LO1/freq/coerced", ch)),
            lo2_freq_c      (db, prepend_ch("los/LO2/freq/coerced", ch))
        {}

        //Inputs (channel specific)
        experts::data_reader_t<bool>                            chan_enabled;
        experts::data_reader_t<twinrx_ctrl::preamp_state_t>     preamp1;
        experts::data_reader_t<bool>                            preamp2;
        experts::data_reader_t<bool>                            lb_preamp_presel;
        experts::data_reader_t<twinrx_ctrl::signal_path_t>      signal_path;
        experts::data_reader_t<twinrx_ctrl::preselector_path_t> lb_presel;
        experts::data_reader_t<twinrx_ctrl::preselector_path_t> hb_presel;
        experts::data_reader_t<uint8_t>                  input_atten;
        experts::data_reader_t<uint8_t>                  lb_atten;
        experts::data_reader_t<uint8_t>                  hb_atten;
        experts::data_reader_t<twinrx_ctrl::lo_source_t>        lo1_source;
        experts::data_reader_t<twinrx_ctrl::lo_source_t>        lo2_source;
        experts::data_reader_t<double>                          lo1_freq_d;
        experts::data_reader_t<double>                          lo2_freq_d;

        //Output (channel specific)
        experts::data_writer_t<double>                          lo1_freq_c;
        experts::data_writer_t<double>                          lo2_freq_c;
    };

    //External interface
    twinrx_ctrl::sptr                                       _ctrl;

    //Inputs (channel agnostic)
    ch_settings                                             _ch0;
    ch_settings                                             _ch1;
    experts::data_reader_t<lo_synth_mapping_t>              _lo1_synth0_mapping;
    experts::data_reader_t<lo_synth_mapping_t>              _lo1_synth1_mapping;
    experts::data_reader_t<lo_synth_mapping_t>              _lo2_synth0_mapping;
    experts::data_reader_t<lo_synth_mapping_t>              _lo2_synth1_mapping;
    experts::data_reader_t<bool>                            _lo1_hopping_enabled;
    experts::data_reader_t<bool>                            _lo2_hopping_enabled;
    experts::data_reader_t<twinrx_ctrl::lo_export_source_t> _lo1_export_src;
    experts::data_reader_t<twinrx_ctrl::lo_export_source_t> _lo2_export_src;
    experts::data_reader_t<twinrx_ctrl::antenna_mapping_t>  _ant_mapping;
    experts::data_reader_t<twinrx_ctrl::cal_mode_t>         _cal_mode;

    //Outputs (channel agnostic)
    //None

    //Misc
    struct lo_freq_cache_t {
        rf_freq_ppm_t desired;
        double        coerced;
    };
    lo_freq_cache_t  _cached_lo1_synth0_freq;
    lo_freq_cache_t  _cached_lo2_synth0_freq;
    lo_freq_cache_t  _cached_lo1_synth1_freq;
    lo_freq_cache_t  _cached_lo2_synth1_freq;

    static const bool FORCE_COMMIT;
};


}}}} //namespaces

#endif /* INCLUDED_DBOARD_TWINRX_EXPERTS_HPP */