-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathadrv9002.c
3381 lines (2865 loc) · 108 KB
/
adrv9002.c
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ADRV9002 (Navassa) Plugin
*
* Copyright (C) 2020 Analog Devices, Inc.
*
* Licensed under the GPL-2.
*
**/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <math.h>
#include <unistd.h>
#include "../osc.h"
#include "../osc_plugin.h"
#include "../iio_widget.h"
#include "../iio_utils.h"
#include "../config.h"
#include "dac_data_manager.h"
#include "../cJSON/cJSON.h"
/*---------------------------------------------------------------------------
* libadrv9002-iio structures
*/
/**
* @struct rx_radio_channel_config
* @brief RX channel configuration
*
* A structure containing the configuration for a single RX channel
*/
typedef struct rx_radio_channel_config
{
/** Enable channel */
bool enabled;
/** Enable high performance ADC, otherwise use low-power ADC */
bool adc_high_performance_mode;
/** Enable ADC frequency offset correction */
bool frequency_offset_correction_enable;
/** Power mode of front-end analog filter Options are:
0 - Low power
1 - Medium power
2 - High power
*/
uint8_t analog_filter_power_mode;
/** Use second order (Biquad) analog filter, otherwise first order TIA is used */
bool analog_filter_biquad;
/** Front-end analog filter 1dB (Biquad) or 3 dB (TIA) bandwidth in Hz*/
uint32_t analog_filter_bandwidth_hz;
/** Channel bandwidth of interest at ADC in Hz*/
uint32_t channel_bandwidth_hz;
/** RX channel sample rate at digital interface */
uint32_t sample_rate_hz;
/** Enable NCO to perform frequency translation */
bool nco_enable;
/** NCO frequency in Hz */
int32_t nco_frequency_hz;
/** RF port source used for channel Options are:
0 - RX_A
1 - RX_B
*/
uint8_t rf_port;
} rx_radio_channel_config;
/**
* @struct tx_radio_channel_config
* @brief TX channel configuration
*
* A structure containing the configuration for a single TX channel
*/
typedef struct tx_radio_channel_config
{
/** Enable channel */
bool enabled;
/** Data rate at digital interface in Hz */
uint32_t sample_rate_hz;
/** Enable DAC frequency offset correction */
bool frequency_offset_correction_enable;
/** Power mode of front-end analog filter Options are:
0 - Low power
1 - Medium power
2 - High power
*/
uint8_t analog_filter_power_mode;
/** Channel bandwidth of interest at DAC in Hz*/
uint32_t channel_bandwidth_hz;
/** Enable observation path */
bool orx_enabled;
/** Set external loopback mode. Options are:
0 - Disabled
1 - Before PA
2 - After PA
*/
uint8_t elb_type;
} tx_radio_channel_config;
/**
* @struct radio_config
* @brief Device configuration
*
* A structure containing the configuration for the top-level device
*/
typedef struct radio_config
{
/** SSI lanes to use Valid cases:
1 (CMOS/LVDS)
2 (LVDS)
4 (CMOS)
*/
uint8_t ssi_lanes;
/** Use DDR mode at digital interface, false will use SDR */
bool ddr;
/** Use short strobe mode at digital interface, false will use long strobe */
bool short_strobe;
/** Use LVDS mode at digital interface, false will use CMOS*/
bool lvds;
/** ADC clock rate mode select. Options are:
1 = LOW
2 = MEDIUM
3 = HIGH
*/
uint8_t adc_rate_mode;
/** Use FDD duplex mode, false will use TDD */
bool fdd;
/** Channel configurations for RX1 and RX2 */
rx_radio_channel_config rx_config[2];
/** Channel configurations for TX1 and TX2 */
tx_radio_channel_config tx_config[2];
} radio_config;
/**
* @struct clock_config
* @brief Clock configuration
*
* A structure containing the configuration for the device clock
*/
typedef struct clock_config
{
/** Device clock frequency in kHz */
uint32_t device_clock_frequency_khz;
bool device_clock_output_enable;
uint8_t device_clock_output_divider;
/** Enable high performance PLL mode, otherwise low-power mode is used*/
bool clock_pll_high_performance_enable;
/** PLL power mode. Options:
0 = low power
1 = medium performance
2 = high performance
*/
uint8_t clock_pll_power_mode;
/** Processor clock divider. Valid values are 1, 2, 4, 8, 16, 32, 64, 128, 256
*/
uint8_t processor_clock_divider;
} clock_config;
/**
* @struct adrv9002_config
* @brief Top-level configuration
*
* A structure containing the configuration for the top-level device
*/
typedef struct adrv9002_config
{
radio_config radio_cfg;
clock_config clk_cfg;
} adrv9002_config;
/*---------------------------------------------------------------------------*/
#ifndef ENOTSUPP
#define ENOTSUPP 524
#endif
#define THIS_DRIVER "ADRV9002"
#define PHY_DEVICE "adrv9002-phy"
#define DDS_DEVICE "axi-adrv9002-tx"
#define CAP_DEVICE "axi-adrv9002-rx"
#define ADRV9002_NUM_CHANNELS 2
/* Max nr of widgets per channel */
#define NUM_MAX_WIDGETS 10
#define NUM_MAX_ORX_WIDGETS 3
#define NUM_MAX_DDS 2
#define NUM_MAX_ADC 2
#define NUM_DEVICE_MAX_WIDGETS 1
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
const gdouble mhz_scale = 1000000.0;
#define BBDC_LOOP_GAIN_RES 2147483648U
static const gdouble bbdc_adjust_min = 1.0 / BBDC_LOOP_GAIN_RES;
static const gdouble bbdc_adjust_max = 1.0 / BBDC_LOOP_GAIN_RES * UINT32_MAX;
struct adrv9002_gtklabel {
GtkLabel *labels;
struct iio_channel *chann;
const char *iio_attr;
const char *label_str;
int scale;
};
struct adrv9002_common {
struct plugin_private *priv;
struct iio_widget gain_ctrl;
struct iio_widget gain;
struct iio_widget nco_freq;
struct iio_widget carrier;
struct iio_widget ensm;
struct iio_widget port_en;
struct adrv9002_gtklabel rf_bandwidth;
struct adrv9002_gtklabel sampling_rate;
/* these are generic widgets that don't need any special attention */
struct iio_widget w[NUM_MAX_WIDGETS];
uint16_t num_widgets;
bool enabled;
bool enable_gpio;
uint8_t idx;
};
struct adrv9002_rx {
struct adrv9002_common rx;
struct iio_widget digital_gain_ctl;
struct iio_widget intf_gain;
struct adrv9002_gtklabel rssi;
struct adrv9002_gtklabel decimated_power;
};
struct adrv9002_orx {
struct iio_widget w[NUM_MAX_ORX_WIDGETS];
struct iio_widget orx_en;
struct plugin_private *priv;
bool enabled;
uint16_t num_widgets;
uint8_t idx;
};
struct adrv9002_dac_mgmt {
struct dac_data_manager *dac_tx_manager;
const char *dac_name;
struct iio_channel *ch0;
};
struct plugin_private {
/* Associated GTK builder */
GtkBuilder *builder;
/* notebook */
GtkNotebook *nbook;
/* plugin context */
struct osc_plugin_context plugin_ctx;
/* iio */
struct iio_context *ctx;
struct iio_device *adrv9002;
/* misc */
gboolean plugin_detached;
gint this_page;
gint refresh_timeout;
char last_profile[PATH_MAX];
char last_stream[PATH_MAX];
struct adrv9002_gtklabel temperature;
struct iio_widget device_w[NUM_DEVICE_MAX_WIDGETS];
int num_widgets;
/* rx */
struct adrv9002_rx rx_widgets[ADRV9002_NUM_CHANNELS];
/* tx */
struct adrv9002_common tx_widgets[ADRV9002_NUM_CHANNELS];
int n_txs;
/* orx */
struct adrv9002_orx orx_widgets[ADRV9002_NUM_CHANNELS];
/* dac */
struct adrv9002_dac_mgmt dac_manager[NUM_MAX_DDS];
int n_dacs;
/* adc */
const char *adc_name[NUM_MAX_ADC];
int n_adcs;
/* profile generator */
int current_preset;
};
#define dialog_box_message(widget, title, level, msg) { \
GtkWidget *toplevel = gtk_widget_get_toplevel(widget); \
\
if (gtk_widget_is_toplevel(toplevel)) { \
GtkWidget *dialog; \
const char *icon; \
\
dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(toplevel), \
GTK_DIALOG_DESTROY_WITH_PARENT, \
level, GTK_BUTTONS_CLOSE, \
msg); \
\
gtk_window_set_title(GTK_WINDOW(dialog), title); \
if (level == GTK_MESSAGE_INFO) \
icon = "dialog-information-symbolic"; \
else \
icon = "dialog-error-symbolic"; \
gtk_window_set_icon_name(GTK_WINDOW(dialog), icon); \
gtk_dialog_run(GTK_DIALOG(dialog)); \
gtk_widget_destroy (dialog); \
} else { \
printf("Cannot display dialog: Toplevel wigdet not found\n"); \
} \
}
#define dialog_box_message_error(widget, title, msg, ...) \
dialog_box_message(widget, title, GTK_MESSAGE_ERROR, msg)
#define dialog_box_message_info(widget, title, msg, ...) \
dialog_box_message(widget, title, GTK_MESSAGE_INFO, msg)
static void save_gain_ctl(GtkWidget *widget, struct adrv9002_common *chann)
{
char *gain_ctl;
iio_widget_save_block_signals_by_data(&chann->gain_ctrl);
gain_ctl = gtk_combo_box_text_get_active_text(
GTK_COMBO_BOX_TEXT(widget));
if (gain_ctl && strcmp(gain_ctl, "spi")) {
gtk_widget_set_sensitive(chann->gain.widget, false);
}
else {
gtk_widget_set_sensitive(chann->gain.widget, true);
/*
* When changing modes the device might automatically change
* some values
*/
iio_widget_update_block_signals_by_data(&chann->gain);
}
g_free(gain_ctl);
}
static void save_intf_gain(GtkWidget *widget, struct adrv9002_rx *rx)
{
char *ensm = gtk_combo_box_text_get_active_text(
GTK_COMBO_BOX_TEXT(rx->rx.ensm.widget));
if (ensm && strcmp(ensm, "rf_enabled")) {
dialog_box_message_error(widget, "Interface Gain Set Failed",
"ENSM must be rf_enabled to change the interface gain");
iio_widget_update_block_signals_by_data(&rx->intf_gain);
} else {
iio_widget_save_block_signals_by_data(&rx->intf_gain);
}
g_free(ensm);
}
static void save_digital_gain_ctl(GtkWidget *widget, struct adrv9002_rx *rx)
{
char *digital_gain;
iio_widget_save_block_signals_by_data(&rx->digital_gain_ctl);
digital_gain = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
if (digital_gain && strcmp(digital_gain, "spi")) {
gtk_widget_set_sensitive(rx->intf_gain.widget, false);
} else {
gtk_widget_set_sensitive(rx->intf_gain.widget, true);
iio_widget_update_block_signals_by_data(&rx->intf_gain);
}
g_free(digital_gain);
}
static void orx_control_track_cals(const struct adrv9002_common *chan, bool en)
{
int i = 0;
if (!chan->enabled)
return;
for (i = 0; i < chan->num_widgets; i++) {
if (!strstr(chan->w[i].attr_name, "_tracking_en"))
continue;
gtk_widget_set_sensitive(chan->w[i].widget, en);
}
}
static void orx_control_tx_widgets_visibility(const struct adrv9002_common *tx, bool en)
{
int i = 0;
/* Disable all tx controls that might affect tx ensm state if @en=true */
gtk_widget_set_sensitive(tx->carrier.widget, en);
gtk_widget_set_sensitive(tx->ensm.widget, en);
gtk_widget_set_sensitive(tx->port_en.widget, en);
gtk_widget_set_sensitive(tx->gain_ctrl.widget, en);
for (i = 0; i < tx->num_widgets; i++) {
if (strcmp(tx->w[i].attr_name, "en"))
continue;
gtk_widget_set_sensitive(tx->w[i].widget, en);
}
}
static void orx_control_track_cal_visibility(const struct adrv9002_orx *orx, bool en)
{
int i;
int other = ~orx->idx & 0x1;
const struct adrv9002_orx *orx_other = &orx->priv->orx_widgets[other];
gboolean wired;
/*
* The thing with tracking cals is that, due to the way the device driver API/Firmware
* is designed, we need to move all 4 ports to calibrated state to change a cal
* (even if we are only changing cals in one specific port). Hence, we need to make
* sure that if one of the ORxs is enabled, we block all the tracking calibrations
* controls in all enabled ports. The driver would not allow it anyways, so this way,
* we make it explicit to the user that this is not permitted.
*/
if (!en) {
wired = false;
} else {
/*
* this check is needed for adrv9003 where ORX2 is never enabled and the
* widgets are never initialized for it.
*/
if (orx_other->enabled)
wired = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(orx_other->orx_en.widget));
else
wired = true;
}
for (i = 0; i < orx->num_widgets; i++) {
if (strcmp(orx->w[i].attr_name, "orx_quadrature_w_poly_tracking_en"))
continue;
gtk_widget_set_sensitive(orx->w[i].widget, wired);
if (orx_other->enabled)
gtk_widget_set_sensitive(orx_other->w[i].widget, wired);
break;
}
/* control track calls on both RXs */
orx_control_track_cals(&orx->priv->rx_widgets[other].rx, wired);
orx_control_track_cals(&orx->priv->rx_widgets[orx->idx].rx, wired);
/* control track calls on both TXs */
orx_control_track_cals(&orx->priv->tx_widgets[other], wired);
orx_control_track_cals(&orx->priv->tx_widgets[orx->idx], wired);
}
static void orx_control_rx_widgets_visibility(const struct adrv9002_common *rx, bool en)
{
char rx_str[32];
GtkWidget *rx_frame;
/* Just disable all the RX frame as it does not make sense to control if @en=true*/
sprintf(rx_str, "frame_rx%d", rx->idx + 1);
rx_frame = GTK_WIDGET(gtk_builder_get_object(rx->priv->builder, rx_str));
if (rx->enabled && rx_frame)
gtk_widget_set_sensitive(rx_frame, en);
}
static void save_orx_powerdown(GtkWidget *widget, struct adrv9002_orx *orx)
{
struct adrv9002_rx *rx = &orx->priv->rx_widgets[orx->idx];
struct adrv9002_common *tx = &orx->priv->tx_widgets[orx->idx];
GtkWidget *rx_ensm = rx->rx.ensm.widget;
GtkWidget *tx_ensm = tx->ensm.widget;
char *r_ensm = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(rx_ensm));
char *t_ensm = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(tx_ensm));
bool en = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
/*
* The way ORx is supposed to work is to enable it only if RX is not in rf_enabled
* and TX __is__ in rf_enabled state. After that point we should not really touch any
* of the RX controls as it might trigger some state change that could break the Orx
* capture. For TX, we are also not supposed to do any state change on the port as some
* state transitions break ORx and we can't recover from it without toggling the ORx button.
*/
if (rx->rx.enabled && r_ensm && !strcmp(r_ensm, "rf_enabled") && !en) {
dialog_box_message_error(widget, "ORX Enable failed",
"RX ENSM cannot be in rf_enabled in order to enable ORX");
/* restore widget value */
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), true);
} else if (t_ensm && strcmp(t_ensm, "rf_enabled") && !en) {
dialog_box_message_error(widget, "ORX Enable failed",
"TX ENSM must be in rf_enabled in order to enable ORX");
/* restore widget value */
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), true);
} else {
iio_widget_save_block_signals_by_data(&orx->orx_en);
/* let's get the value again to make sure it is the most up to date */
en = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
orx_control_rx_widgets_visibility(&rx->rx, en);
orx_control_tx_widgets_visibility(tx, en);
orx_control_track_cal_visibility(orx, en);
}
g_free(r_ensm);
g_free(t_ensm);
}
static void save_ensm(GtkWidget *w, struct adrv9002_common *chann)
{
gchar *ensm = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(chann->ensm.widget));
gchar *port_en = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(chann->port_en.widget));
if (!strcmp(port_en, "pin") && !strcmp(ensm, "calibrated")) {
dialog_box_message_error(chann->ensm.widget, "Enable State Mode",
"Only primed or rf_enabled possible when pin mode is enabled");
goto out_free;
}
chann->ensm.save(&chann->ensm);
/*
* If it is a transition to rf_enabled, it can take some time and so, we
* can still get the old value if we do not wait a bit...
*/
usleep(2000);
out_free:
iio_widget_update_block_signals_by_data(&chann->ensm);
g_free(ensm);
g_free(port_en);
}
static void save_port_en(GtkWidget *widget, struct adrv9002_common *chann)
{
char *port_en;
iio_widget_save_block_signals_by_data(&chann->port_en);
if (chann->enable_gpio) {
/*
* If we can control RF state when in pin mode, there's no need to
* control the sensitivity. Hence, just update the RF state widget (as
* it can change when moving from pin to spi and vice versa) and bail out.
*/
iio_widget_update_block_signals_by_data(&chann->ensm);
return;
}
port_en = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
if (port_en && strcmp(port_en, "spi")) {
gtk_widget_set_sensitive(chann->ensm.widget, false);
} else {
gtk_widget_set_sensitive(chann->ensm.widget, true);
iio_widget_update_block_signals_by_data(&chann->ensm);
}
g_free(port_en);
}
static void adrv9002_save_carrier_freq(GtkWidget *widget, struct adrv9002_common *chan)
{
struct plugin_private *priv = chan->priv;
int other = ~chan->idx & 0x1;
/* we can use whatever attr as the iio channel is the same (naturally not for the LOs) */
bool tx = iio_channel_is_output(chan->ensm.chn);
chan->carrier.save(&chan->carrier);
/*
* Carriers are only moved together for ports of the same type so just update the
* value of the @other channel. In cases like TDD, that won't be the case so we could
* actually skip updating the widget but doing it unconditionally it's just much
* simpler and allow us to drop all the code to keep the LO mappings.
*/
if (tx && other < priv->n_txs)
iio_widget_update_block_signals_by_data(&priv->tx_widgets[other].carrier);
else
iio_widget_update_block_signals_by_data(&priv->rx_widgets[other].rx.carrier);
iio_widget_update_block_signals_by_data(&chan->carrier);
}
static void adrv9002_show_help(GtkWidget *widget, void *unused)
{
dialog_box_message_info(widget, "Initial Calibrations Help",
"<b>off:</b> Initial calibrations won't run automatically.\n"
"<b>auto:</b> Initial calibrations will run automatically for Carrier changes bigger or equal to 100MHz.\n\n"
"<b>To manually run the calibrations, press the \"Calibrate now\" button!</b>");
}
static void adrv9002_run_cals(GtkWidget *widget, struct plugin_private *priv)
{
ssize_t ret;
ret = iio_device_attr_write(priv->adrv9002, "initial_calibrations", "run");
if (ret < 0)
dialog_box_message_error(widget, "Initial Calibrations",
"Failed to re-run Initial Calibrations");
}
static double adrv9002_bbdc_loop_gain_convert(double val, bool updating)
{
double loop_gain;
if (updating)
loop_gain = val / BBDC_LOOP_GAIN_RES;
else
loop_gain = round(val * BBDC_LOOP_GAIN_RES);
return loop_gain;
}
static void handle_section_cb(GtkToggleToolButton *btn, GtkWidget *section)
{
GtkWidget *toplevel;
if (gtk_toggle_tool_button_get_active(btn)) {
g_object_set(G_OBJECT(btn), "stock-id", "gtk-go-down", NULL);
gtk_widget_show(section);
} else {
g_object_set(G_OBJECT(btn), "stock-id", "gtk-go-up", NULL);
gtk_widget_hide(section);
toplevel = gtk_widget_get_toplevel(GTK_WIDGET(btn));
if (gtk_widget_is_toplevel(toplevel))
gtk_window_resize(GTK_WINDOW(toplevel), 1, 1);
}
}
static void adrv9002_gtk_label_init(struct plugin_private *priv,
struct adrv9002_gtklabel *adrv9002_label,
struct iio_channel *chann,
const char *iio_str, const char *label,
const int scale)
{
GtkLabel *glabel = GTK_LABEL(gtk_builder_get_object(priv->builder,
label));
adrv9002_label->chann = chann;
adrv9002_label->iio_attr = iio_str;
adrv9002_label->scale = scale ? scale : 1;
adrv9002_label->labels = glabel;
}
static void update_dac_manager(struct plugin_private *priv)
{
int i;
for(i = 0; i < priv->n_dacs; i++) {
double dac_tx_sampling_freq = 0;
struct iio_channel *ch0 = priv->dac_manager[i].ch0;
long long dac_freq = 0;
if (iio_channel_attr_read_longlong(ch0, "sampling_frequency", &dac_freq) == 0)
dac_tx_sampling_freq = (double)dac_freq / 1000000ul;
dac_data_manager_freq_widgets_range_update(priv->dac_manager[i].dac_tx_manager,
dac_tx_sampling_freq / 2);
dac_data_manager_update_iio_widgets(priv->dac_manager[i].dac_tx_manager);
}
}
static void update_label(const struct adrv9002_gtklabel *label)
{
double val;
char attr_val[64];
if (iio_channel_attr_read_double(label->chann, label->iio_attr, &val) == 0)
snprintf(attr_val, sizeof(attr_val), "%.4f", val / label->scale);
else
snprintf(attr_val, sizeof(attr_val), "%s", "error");
gtk_label_set_text(label->labels, attr_val);
}
static void update_special_widgets(struct adrv9002_common *chann, const char *ensm, size_t len)
{
char *gain_ctl = gtk_combo_box_text_get_active_text(
GTK_COMBO_BOX_TEXT(chann->gain_ctrl.widget));
char *port_en = gtk_combo_box_text_get_active_text(
GTK_COMBO_BOX_TEXT(chann->port_en.widget));
if (gain_ctl && strcmp(gain_ctl, "spi"))
iio_widget_update_block_signals_by_data(&chann->gain);
if (port_en && strcmp(port_en, "spi") && !chann->enable_gpio) {
if (ensm)
iio_widget_update_value(&chann->ensm, ensm, len);
else
iio_widget_update_block_signals_by_data(&chann->ensm);
}
g_free(gain_ctl);
g_free(port_en);
}
static void update_special_rx_widgets(struct adrv9002_rx *rx, const int n_widgets)
{
int i;
for (i = 0; i < n_widgets; i++) {
char *digital_gain = gtk_combo_box_text_get_active_text(
GTK_COMBO_BOX_TEXT(rx[i].digital_gain_ctl.widget));
char ensm[32] = {0};
if (!rx[i].rx.enabled)
goto nex_widget;
/*
* There was a change in the driver API where an error is returned if we try to read
* the RSSI level if the channel is not enabled. Hence, make sure we only update it
* if the channel is enabled.
*/
if (iio_channel_attr_read(rx[i].rx.ensm.chn, rx[i].rx.ensm.attr_name, ensm,
sizeof(ensm)) > 0 && !strcmp(ensm, "rf_enabled"))
update_label(&rx[i].rssi);
update_label(&rx[i].decimated_power);
/*
* Pass in ensm as we already got it and so no need for another possible
* remote call into the device.
*/
update_special_widgets(&rx[i].rx, ensm, sizeof(ensm));
if (digital_gain && strstr(digital_gain, "automatic"))
iio_widget_update_block_signals_by_data(&rx[i].intf_gain);
nex_widget:
g_free(digital_gain);
}
}
static void update_special_tx_widgets(struct adrv9002_common *tx, const int n_widgets)
{
int i;
for (i = 0; i < n_widgets; i++) {
if (!tx[i].enabled)
continue;
update_special_widgets(&tx[i], NULL, 0);
}
}
static gboolean update_display(gpointer arg)
{
struct plugin_private *priv = arg;
if (priv->this_page == gtk_notebook_get_current_page(priv->nbook) ||
priv->plugin_detached) {
update_special_rx_widgets(priv->rx_widgets,
ARRAY_SIZE(priv->rx_widgets));
update_special_tx_widgets(priv->tx_widgets,
ARRAY_SIZE(priv->tx_widgets));
update_label(&priv->temperature);
}
return true;
}
static void adrv9002_update_orx_widgets(struct plugin_private *priv, const int chann)
{
struct adrv9002_orx *orx = &priv->orx_widgets[chann];
int ret;
long long dummy;
char label[32];
if (!orx->enabled) {
/* make sure the widget is initialized */
if (chann >= priv->n_txs)
return;
/*
* This will make sure that we restore TX/RX widgets sensitivity if we had ORx
* enabled before updating the profile
*/
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(orx->orx_en.widget), true);
return;
}
/* can we actually enable/disable orx?! */
sprintf(label, "powerdown_en_label_orx%d", orx->idx + 1);
ret = iio_channel_attr_read_longlong(orx->orx_en.chn, "orx_en", &dummy);
/*
* Lets' hide as this is not really supposed to change at runtime. However, we need
* to do the check here as at the plugin initialization, the device might not have a
* profile supporting ORx and in that case, we cannot really know if orx_en is supported
* or not as the return val would be ENODEV...
*/
if (ret == -ENOTSUPP) {
gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(priv->builder, label)));
gtk_widget_hide(orx->orx_en.widget);
} else {
gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(priv->builder, label)));
gtk_widget_show(orx->orx_en.widget);
iio_widget_update(&orx->orx_en);
}
/* generic widgets */
iio_update_widgets_block_signals_by_data(orx->w, orx->num_widgets);
}
static void adrv9002_update_rx_widgets(struct plugin_private *priv, const int chann)
{
struct adrv9002_rx *rx = &priv->rx_widgets[chann];
gchar *ensm;
/* rx */
if (!rx->rx.enabled)
return;
iio_widget_update_block_signals_by_data(&rx->rx.gain_ctrl);
iio_widget_update_block_signals_by_data(&rx->rx.gain);
iio_widget_update_block_signals_by_data(&rx->rx.nco_freq);
iio_widget_update_block_signals_by_data(&rx->rx.ensm);
iio_widget_update_block_signals_by_data(&rx->rx.port_en);
iio_widget_update_block_signals_by_data(&rx->digital_gain_ctl);
iio_widget_update_block_signals_by_data(&rx->intf_gain);
iio_widget_update_block_signals_by_data(&rx->rx.carrier);
/* generic widgets */
iio_update_widgets_block_signals_by_data(rx->rx.w, rx->rx.num_widgets);
/* labels */
ensm = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(&rx->rx.ensm.widget));
if (ensm && !strcmp(ensm, "rf_enabled"))
update_label(&rx->rssi);
update_label(&rx->decimated_power);
update_label(&rx->rx.rf_bandwidth);
update_label(&rx->rx.sampling_rate);
}
static void adrv9002_update_tx_widgets(struct plugin_private *priv, const int chann)
{
struct adrv9002_common *tx = &priv->tx_widgets[chann];
if (!tx->enabled)
return;
iio_widget_update_block_signals_by_data(&tx->gain_ctrl);
iio_widget_update_block_signals_by_data(&tx->gain);
iio_widget_update_block_signals_by_data(&tx->nco_freq);
iio_widget_update_block_signals_by_data(&tx->carrier);
iio_widget_update_block_signals_by_data(&tx->ensm);
iio_widget_update_block_signals_by_data(&tx->port_en);
/* generic widgets */
iio_update_widgets_block_signals_by_data(tx->w, tx->num_widgets);
/* labels */
update_label(&tx->rf_bandwidth);
update_label(&tx->sampling_rate);
}
static void rx_sample_rate_update(struct plugin_private *priv)
{
int i;
for (i = 0; i < priv->n_adcs; i++) {
rx_update_device_sampling_freq(priv->adc_name[i],
USE_INTERN_SAMPLING_FREQ);
}
}
static void adrv9002_profile_read(struct plugin_private *priv)
{
char profile[512];
ssize_t ret;
GtkLabel *label = GTK_LABEL(gtk_builder_get_object(priv->builder, "profile_config_read"));
ret = iio_device_attr_read(priv->adrv9002, "profile_config", profile, sizeof(profile));
if (ret < 0)
strcpy(profile, "error\n");
gtk_label_set_text(label, profile);
}
static void adrv9002_check_orx_status(struct plugin_private *priv, struct adrv9002_orx *orx)
{
int ret;
double dummy;
char gtk_str[32];
sprintf(gtk_str, "frame_orx%d", orx->idx + 1);
ret = iio_channel_attr_read_double(orx->w[0].chn, "orx_hardwaregain", &dummy);
if (ret == -ENODEV) {
orx->enabled = false;
gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(priv->builder, gtk_str)));
} else {
orx->enabled = true;
gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(priv->builder, gtk_str)));
}
}
static bool adrv9002_channel_is_enabled(struct adrv9002_common *chan)
{
int ret;
double dummy;
/*
* We use this attr to check if the channel is enabled or not since it should only
* return error if the channel is disabled (assuming there's nothing seriously
* wrong with the device). We can also just use the iio channel from the first
* widget as the channel is the same for all widgets on this port...
*/
ret = iio_channel_attr_read_double(chan->w[0].chn, "rf_bandwidth", &dummy);
if(ret < 0 && ret != -ENODEV) {
printf("Warning: iio channel returned an error when reading it: %d. We assume the channel is enabled\n",
ret);
}
return ret == 0;
}
static void adrv9002_check_channel_status(struct plugin_private *priv,
struct adrv9002_common *chan,
const char *gtk_str)
{
if (!adrv9002_channel_is_enabled(chan)) {
chan->enabled = false;
gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(priv->builder,
gtk_str)));
} else {
chan->enabled = true;
gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(priv->builder,
gtk_str)));
}
}
static void adrv9002_check_nco_freq_support(struct plugin_private *priv, const int channel,
const bool tx)
{
int ret;
long long dummy;
char gtk_str[32], label_str[32];
struct adrv9002_common *c;
if (tx) {
c = &priv->tx_widgets[channel];
sprintf(gtk_str, "nco_freq_tx%d", channel + 1);
sprintf(label_str, "nco_label_tx%d", channel + 1);
} else {
c = &priv->rx_widgets[channel].rx;
sprintf(gtk_str, "nco_freq_rx%d", channel + 1);
sprintf(label_str, "nco_label_rx%d", channel + 1);
}
/* nothing to do if the port is already disabled */
if (!c->enabled)
return;
ret = iio_channel_attr_read_longlong(c->nco_freq.chn, "nco_frequency", &dummy);
if (ret == -ENOTSUPP) {
gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(priv->builder,
label_str)));
gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(priv->builder,
gtk_str)));
} else {
gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(priv->builder,
label_str)));
gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(priv->builder,
gtk_str)));
}
}
static void adrv9002_update_rx_intf_gain_attr_available(struct adrv9002_rx *rx)
{
struct iio_widget *w = &rx->intf_gain;
gchar **saved_list, **available;
char text[512];
int ret;
ret = iio_channel_attr_read(w->chn, w->attr_name_avail, text,
sizeof(text));
if (ret < 0)
return;
/*
* We need to block the signals to avoid multiple calls to save_intf_gain() triggered
* by removing the elements (number of calls depends on the active value/index). Note
* there are other widgets sharing @rx as signal data but we are safe in still blocking
* all the signals for it at this stage. We should only get here when the 'reload_settings'
* button is pressed or on profile load.
*/
g_signal_handlers_block_matched(G_OBJECT(w->widget), G_SIGNAL_MATCH_DATA, 0, 0,
NULL, NULL, rx);
gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(w->widget));
g_signal_handlers_unblock_matched(G_OBJECT(w->widget), G_SIGNAL_MATCH_DATA, 0, 0,
NULL, NULL, rx);
available = saved_list = g_strsplit(text, " ", 0);
/* our available is static so we can just set it here once */
for (; *available; available++) {
if (*available[0] == '\0')
continue;
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(w->widget),
*available);
}
g_strfreev(saved_list);
}
static void update_all(struct plugin_private *priv)
{
int i;
char gtk_str[32];
for(i = 0; i < ADRV9002_NUM_CHANNELS; i++) {
sprintf(gtk_str, "frame_rx%d", i + 1);
adrv9002_check_channel_status(priv, &priv->rx_widgets[i].rx, gtk_str);