summaryrefslogtreecommitdiff
path: root/usrp/host/lib/usrp_basic.cc
blob: 4f3df5212e39619e385626aa6da7321cdd5a7049 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
/* -*- c++ -*- */
/*
 * Copyright 2003,2004,2008,2009 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio 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, or (at your option)
 * any later version.
 * 
 * GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <usrp/usrp_basic.h>
#include "usrp/usrp_prims.h"
#include "usrp_interfaces.h"
#include "fpga_regs_common.h"
#include "fpga_regs_standard.h"
#include "fusb.h"
#include "db_boards.h"
#include <usb.h>
#include <stdexcept>
#include <assert.h>
#include <math.h>
#include <ad9862.h>
#include <string.h>
#include <cstdio>

using namespace ad9862;

#define NELEM(x) (sizeof (x) / sizeof (x[0]))

// These set the buffer size used for each end point using the fast
// usb interface.  The kernel ends up locking down this much memory.

static const int FUSB_BUFFER_SIZE = fusb_sysconfig::default_buffer_size();
static const int FUSB_BLOCK_SIZE = fusb_sysconfig::max_block_size();
static const int FUSB_NBLOCKS    = FUSB_BUFFER_SIZE / FUSB_BLOCK_SIZE;


static const double POLLING_INTERVAL = 0.1;	// seconds

////////////////////////////////////////////////////////////////

static struct usb_dev_handle *
open_rx_interface (struct usb_device *dev)
{
  struct usb_dev_handle *udh = usrp_open_rx_interface (dev);
  if (udh == 0){
    fprintf (stderr, "usrp_basic_rx: can't open rx interface\n");
    usb_strerror ();
  }
  return udh;
}

static struct usb_dev_handle *
open_tx_interface (struct usb_device *dev)
{
  struct usb_dev_handle *udh = usrp_open_tx_interface (dev);
  if (udh == 0){
    fprintf (stderr, "usrp_basic_tx: can't open tx interface\n");
    usb_strerror ();
  }
  return udh;
}


//////////////////////////////////////////////////////////////////
//
//			usrp_basic
//
////////////////////////////////////////////////////////////////


// Given:
//   CLKIN = 64 MHz
//   CLKSEL pin = high 
//
// These settings give us:
//   CLKOUT1 = CLKIN = 64 MHz
//   CLKOUT2 = CLKIN = 64 MHz
//   ADC is clocked at  64 MHz
//   DAC is clocked at 128 MHz

static unsigned char common_regs[] = {
  REG_GENERAL,		0,
  REG_DLL,		(DLL_DISABLE_INTERNAL_XTAL_OSC
			 | DLL_MULT_2X
			 | DLL_FAST),
  REG_CLKOUT,		CLKOUT2_EQ_DLL_OVER_2,
  REG_AUX_ADC_CLK,	AUX_ADC_CLK_CLK_OVER_4
};


usrp_basic::usrp_basic (int which_board, 
			struct usb_dev_handle *
			open_interface (struct usb_device *dev),
			const std::string fpga_filename,
			const std::string firmware_filename)
  : d_udh (0),
    d_usb_data_rate (16000000),	// SWAG, see below
    d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)),
    d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2)
{
  /*
   * SWAG: Scientific Wild Ass Guess.
   *
   * d_usb_data_rate is used only to determine how often to poll for over- and under-runs.
   * We defualt it to 1/2  of our best case.  Classes derived from usrp_basic (e.g., 
   * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the
   * actual rate.  This doesn't change our throughput, that's determined by the signal
   * processing code in the FPGA (which we know nothing about), and the system limits
   * determined by libusb, fusb_*, and the underlying drivers.
   */
  memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows));

  usrp_one_time_init ();

  if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename))
    throw std::runtime_error ("usrp_basic/usrp_load_standard_bits");

  struct usb_device *dev = usrp_find_device (which_board);
  if (dev == 0){
    fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board);
    throw std::runtime_error ("usrp_basic/usrp_find_device");
  }

  if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){
    fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n");
    throw std::runtime_error ("usrp_basic/bad_rev");
  }

  if ((d_udh = open_interface (dev)) == 0)
    throw std::runtime_error ("usrp_basic/open_interface");

  // initialize registers that are common to rx and tx

  if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){
    fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n");
    throw std::runtime_error ("usrp_basic/init_9862");
  }

  _write_fpga_reg (FR_MODE, 0);		// ensure we're in normal mode
  _write_fpga_reg (FR_DEBUG_EN, 0);	// disable debug outputs
}

void
usrp_basic::shutdown_daughterboards()
{
  // nuke d'boards before we close down USB in ~usrp_basic
  // shutdown() will do any board shutdown while the USRP can still
  // be talked to
  for(size_t i = 0; i < d_db.size(); i++) 
    for(size_t j = 0; j < d_db[i].size(); j++) 
      d_db[i][j]->shutdown();
}

usrp_basic::~usrp_basic ()
{
  // shutdown_daughterboards();		// call from ~usrp_basic_{tx,rx}

  d_db.resize(0); // forget db shared ptrs

  if (d_udh)
    usb_close (d_udh);
}

void
usrp_basic::init_db(usrp_basic_sptr u)
{
  if (u.get() != this)
    throw std::invalid_argument("u is not this");

  d_db[0] = instantiate_dbs(d_dbid[0], u, 0);
  d_db[1] = instantiate_dbs(d_dbid[1], u, 1);
}

std::vector<db_base_sptr> 
usrp_basic::db(int which_side)
{
  which_side &= 0x1;	// clamp it to avoid any reporting any errors
  return d_db[which_side];
}

bool
usrp_basic::is_valid(const usrp_subdev_spec &ss)
{
  if (ss.side < 0 || ss.side > 1)
    return false;

  if (ss.subdev < 0 || ss.subdev >= d_db[ss.side].size())
    return false;

  return true;
}

db_base_sptr
usrp_basic::selected_subdev(const usrp_subdev_spec &ss)
{
  if (!is_valid(ss))
    throw std::invalid_argument("invalid subdev_spec");

  return d_db[ss.side][ss.subdev];
}

bool
usrp_basic::start ()
{
  return true;		// nop
}

bool
usrp_basic::stop ()
{
  return true;		// nop
}

void
usrp_basic::set_usb_data_rate (int usb_data_rate)
{
  d_usb_data_rate = usb_data_rate;
  d_bytes_per_poll = (int) (usb_data_rate * POLLING_INTERVAL);
}

bool
usrp_basic::_write_aux_dac (int slot, int which_dac, int value)
{
  return usrp_write_aux_dac (d_udh, slot, which_dac, value);
}

bool
usrp_basic::_read_aux_adc (int slot, int which_adc, int *value)
{
  return usrp_read_aux_adc (d_udh, slot, which_adc, value);
}

int
usrp_basic::_read_aux_adc (int slot, int which_adc)
{
  int	value;
  if (!_read_aux_adc (slot, which_adc, &value))
    return READ_FAILED;

  return value;
}

bool
usrp_basic::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
{
  return usrp_eeprom_write (d_udh, i2c_addr, eeprom_offset, buf.data (), buf.size ());
}

std::string
usrp_basic::read_eeprom (int i2c_addr, int eeprom_offset, int len)
{
  if (len <= 0)
    return "";

  char buf[len];

  if (!usrp_eeprom_read (d_udh, i2c_addr, eeprom_offset, buf, len))
    return "";

  return std::string (buf, len);
}

bool
usrp_basic::write_i2c (int i2c_addr, const std::string buf)
{
  return usrp_i2c_write (d_udh, i2c_addr, buf.data (), buf.size ());
}

std::string
usrp_basic::read_i2c (int i2c_addr, int len)
{
  if (len <= 0)
    return "";

  char buf[len];

  if (!usrp_i2c_read (d_udh, i2c_addr, buf, len))
    return "";

  return std::string (buf, len);
}

std::string
usrp_basic::serial_number()
{
  return usrp_serial_number(d_udh);
}

// ----------------------------------------------------------------

bool
usrp_basic::set_adc_offset (int which_adc, int offset)
{
  if (which_adc < 0 || which_adc > 3)
    return false;

  return _write_fpga_reg (FR_ADC_OFFSET_0 + which_adc, offset);
}

bool
usrp_basic::set_dac_offset (int which_dac, int offset, int offset_pin)
{
  if (which_dac < 0 || which_dac > 3)
    return false;

  int which_codec = which_dac >> 1;
  int tx_a = (which_dac & 0x1) == 0;
  int lo = ((offset & 0x3) << 6) | (offset_pin & 0x1);
  int hi = (offset >> 2);
  bool ok;

  if (tx_a){
    ok =  _write_9862 (which_codec, REG_TX_A_OFFSET_LO, lo);
    ok &= _write_9862 (which_codec, REG_TX_A_OFFSET_HI, hi);
  }
  else {
    ok =  _write_9862 (which_codec, REG_TX_B_OFFSET_LO, lo);
    ok &= _write_9862 (which_codec, REG_TX_B_OFFSET_HI, hi);
  }
  return ok;
}

bool
usrp_basic::set_adc_buffer_bypass (int which_adc, bool bypass)
{
  if (which_adc < 0 || which_adc > 3)
    return false;

  int codec = which_adc >> 1;
  int reg = (which_adc & 1) == 0 ? REG_RX_A : REG_RX_B;

  unsigned char cur_rx;
  unsigned char cur_pwr_dn;

  // If the input buffer is bypassed, we need to power it down too.

  bool ok = _read_9862 (codec, reg, &cur_rx);
  ok &= _read_9862 (codec, REG_RX_PWR_DN, &cur_pwr_dn);
  if (!ok)
    return false;

  if (bypass){
    cur_rx |= RX_X_BYPASS_INPUT_BUFFER;
    cur_pwr_dn |= ((which_adc & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B;
  }
  else {
    cur_rx &= ~RX_X_BYPASS_INPUT_BUFFER;
    cur_pwr_dn &= ~(((which_adc & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B);
  }

  ok &= _write_9862 (codec, reg, cur_rx);
  ok &= _write_9862 (codec, REG_RX_PWR_DN, cur_pwr_dn);
  return ok;
}

bool
usrp_basic::set_dc_offset_cl_enable(int bits, int mask)
{
  return _write_fpga_reg(FR_DC_OFFSET_CL_EN, 
			 (d_fpga_shadows[FR_DC_OFFSET_CL_EN] & ~mask) | (bits & mask));
}

// ----------------------------------------------------------------

bool
usrp_basic::_write_fpga_reg (int regno, int value)
{
  if (d_verbose){
    fprintf (stdout, "_write_fpga_reg(%3d, 0x%08x)\n", regno, value);
    fflush (stdout);
  }

  if (regno >= 0 && regno < MAX_REGS)
    d_fpga_shadows[regno] = value;

  return usrp_write_fpga_reg (d_udh, regno, value);
}

bool
usrp_basic::_write_fpga_reg_masked (int regno, int value, int mask)
{
  //Only use this for registers who actually use a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
  //value is a 16 bits value and mask is a 16 bits mask
  if (d_verbose){
    fprintf (stdout, "_write_fpga_reg_masked(%3d, 0x%04x,0x%04x)\n", regno, value, mask);
    fflush (stdout);
  }

  if (regno >= 0 && regno < MAX_REGS)
    d_fpga_shadows[regno] = value;

  return usrp_write_fpga_reg (d_udh, regno, (value & 0xffff) | ((mask & 0xffff)<<16));
}


bool
usrp_basic::_read_fpga_reg (int regno, int *value)
{
  return usrp_read_fpga_reg (d_udh, regno, value);
}

int
usrp_basic::_read_fpga_reg (int regno)
{
  int value;
  if (!_read_fpga_reg (regno, &value))
    return READ_FAILED;
  return value;
}

bool
usrp_basic::_write_9862 (int which_codec, int regno, unsigned char value)
{
  if (0 && d_verbose){
    // FIXME really want to enable logging in usrp_prims:usrp_9862_write
    fprintf(stdout, "_write_9862(codec = %d, regno = %2d, val = 0x%02x)\n", which_codec, regno, value);
    fflush(stdout);
  }

  return usrp_9862_write (d_udh, which_codec, regno, value);
}


bool
usrp_basic::_read_9862 (int which_codec, int regno, unsigned char *value) const
{
  return usrp_9862_read (d_udh, which_codec, regno, value);
}

int
usrp_basic::_read_9862 (int which_codec, int regno) const
{
  unsigned char value;
  if (!_read_9862 (which_codec, regno, &value))
    return READ_FAILED;
  return value;
}

bool
usrp_basic::_write_spi (int optional_header, int enables, int format, std::string buf)
{
  return usrp_spi_write (d_udh, optional_header, enables, format,
			 buf.data(), buf.size());
}

std::string
usrp_basic::_read_spi (int optional_header, int enables, int format, int len)
{
  if (len <= 0)
    return "";
  
  char buf[len];

  if (!usrp_spi_read (d_udh, optional_header, enables, format, buf, len))
    return "";

  return std::string (buf, len);
}


bool
usrp_basic::_set_led (int which_led, bool on)
{
  return usrp_set_led (d_udh, which_led, on);
}

bool
usrp_basic::write_atr_tx_delay(int value)
{
  return _write_fpga_reg(FR_ATR_TX_DELAY, value);
}

bool
usrp_basic::write_atr_rx_delay(int value)
{
  return _write_fpga_reg(FR_ATR_RX_DELAY, value);
}

/*
 * ----------------------------------------------------------------
 * Routines to access and control daughterboard specific i/o
 * ----------------------------------------------------------------
 */
static int
slot_id_to_oe_reg (int slot_id)
{
  static int reg[4]  = { FR_OE_0, FR_OE_1, FR_OE_2, FR_OE_3 };
  assert (0 <= slot_id && slot_id < 4);
  return reg[slot_id];
}

static int
slot_id_to_io_reg (int slot_id)
{
  static int reg[4]  = { FR_IO_0, FR_IO_1, FR_IO_2, FR_IO_3 };
  assert (0 <= slot_id && slot_id < 4);
  return reg[slot_id];
}

static int
slot_id_to_refclk_reg(int slot_id)
{
  static int reg[4]  = { FR_TX_A_REFCLK, FR_RX_A_REFCLK, FR_TX_B_REFCLK, FR_RX_B_REFCLK };
  assert (0 <= slot_id && slot_id < 4);
  return reg[slot_id];
}

static int
slot_id_to_atr_mask_reg(int slot_id)
{
  static int reg[4]  = { FR_ATR_MASK_0, FR_ATR_MASK_1, FR_ATR_MASK_2, FR_ATR_MASK_3 };
  assert (0 <= slot_id && slot_id < 4);
  return reg[slot_id];
}

static int
slot_id_to_atr_txval_reg(int slot_id)
{
  static int reg[4]  = { FR_ATR_TXVAL_0, FR_ATR_TXVAL_1, FR_ATR_TXVAL_2, FR_ATR_TXVAL_3 };
  assert (0 <= slot_id && slot_id < 4);
  return reg[slot_id];
}

static int
slot_id_to_atr_rxval_reg(int slot_id)
{
  static int reg[4]  = { FR_ATR_RXVAL_0, FR_ATR_RXVAL_1, FR_ATR_RXVAL_2, FR_ATR_RXVAL_3 };
  assert (0 <= slot_id && slot_id < 4);
  return reg[slot_id];
}

static int
to_slot(txrx_t txrx, int which_side)
{
  // TX_A = 0
  // RX_A = 1
  // TX_B = 2
  // RX_B = 3
  return ((which_side & 0x1) << 1) | ((txrx & 0x1) == C_RX);
}

bool
usrp_basic::common_set_pga(txrx_t txrx, int which_amp, double gain)
{
  if (which_amp < 0 || which_amp > 3)
    return false;

  gain = std::min(common_pga_max(txrx),
		  std::max(common_pga_min(txrx), gain));

  int codec = which_amp >> 1;	
  int int_gain = (int) rint((gain - common_pga_min(txrx)) / common_pga_db_per_step(txrx));

  if (txrx == C_TX){		// 0 and 1 are same, as are 2 and 3
    return _write_9862(codec, REG_TX_PGA, int_gain);
  }
  else {
    int reg = (which_amp & 1) == 0 ? REG_RX_A : REG_RX_B;

    // read current value to get input buffer bypass flag.
    unsigned char cur_rx;
    if (!_read_9862(codec, reg, &cur_rx))
      return false;

    cur_rx = (cur_rx & RX_X_BYPASS_INPUT_BUFFER) | (int_gain & 0x7f);
    return _write_9862(codec, reg, cur_rx);
  }
}

double
usrp_basic::common_pga(txrx_t txrx, int which_amp) const
{
  if (which_amp < 0 || which_amp > 3)
    return READ_FAILED;

  if (txrx == C_TX){
    int codec = which_amp >> 1;
    unsigned char v;
    bool ok = _read_9862 (codec, REG_TX_PGA, &v);
    if (!ok)
      return READ_FAILED;

    return (pga_db_per_step() * v) + pga_min();
  }
  else {
    int codec = which_amp >> 1;
    int reg = (which_amp & 1) == 0 ? REG_RX_A : REG_RX_B;
    unsigned char v;
    bool ok = _read_9862 (codec, reg, &v);
    if (!ok)
      return READ_FAILED;

    return (pga_db_per_step() * (v & 0x1f)) + pga_min();
  }
}

double
usrp_basic::common_pga_min(txrx_t txrx) const
{
  if (txrx == C_TX)
    return -20.0;
  else
    return   0.0;
}

double
usrp_basic::common_pga_max(txrx_t txrx) const
{
  if (txrx == C_TX)
    return   0.0;
  else
    return  20.0;
}

double
usrp_basic::common_pga_db_per_step(txrx_t txrx) const
{
  if (txrx == C_TX)
    return  20.0 / 255;
  else
    return  20.0 / 20;
}

bool
usrp_basic::_common_write_oe(txrx_t txrx, int which_side, int value, int mask)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  return _write_fpga_reg(slot_id_to_oe_reg(to_slot(txrx, which_side)),
			 (mask << 16) | (value & 0xffff));
}

bool
usrp_basic::common_write_io(txrx_t txrx, int which_side, int value, int mask)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  return _write_fpga_reg(slot_id_to_io_reg(to_slot(txrx, which_side)),
			 (mask << 16) | (value & 0xffff));
}

bool
usrp_basic::common_read_io(txrx_t txrx, int which_side, int *value)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  int t;
  int reg = which_side + 1;	// FIXME, *very* magic number (fix in serial_io.v)
  bool ok = _read_fpga_reg(reg, &t);
  if (!ok)
    return false;

  if (txrx == C_TX){
    *value = t & 0xffff;		// FIXME, more magic
    return true;
  }
  else {
    *value = (t >> 16) & 0xffff;	// FIXME, more magic
    return true;
  }
}

int
usrp_basic::common_read_io(txrx_t txrx, int which_side)
{
  int	value;
  if (!common_read_io(txrx, which_side, &value))
    return READ_FAILED;
  return value;
}

bool
usrp_basic::common_write_refclk(txrx_t txrx, int which_side, int value)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  return _write_fpga_reg(slot_id_to_refclk_reg(to_slot(txrx, which_side)),
			 value);
}

bool
usrp_basic::common_write_atr_mask(txrx_t txrx, int which_side, int value)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  return _write_fpga_reg(slot_id_to_atr_mask_reg(to_slot(txrx, which_side)),
			 value);
}

bool
usrp_basic::common_write_atr_txval(txrx_t txrx, int which_side, int value)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  return _write_fpga_reg(slot_id_to_atr_txval_reg(to_slot(txrx, which_side)),
			 value);
}

bool
usrp_basic::common_write_atr_rxval(txrx_t txrx, int which_side, int value)
{
  if (! (0 <= which_side && which_side <= 1))
    return false;

  return _write_fpga_reg(slot_id_to_atr_rxval_reg(to_slot(txrx, which_side)),
			 value);
}

bool
usrp_basic::common_write_aux_dac(txrx_t txrx, int which_side, int which_dac, int value)
{
  return _write_aux_dac(to_slot(txrx, which_side), which_dac, value);
}

bool
usrp_basic::common_read_aux_adc(txrx_t txrx, int which_side, int which_adc, int *value)
{
  return _read_aux_adc(to_slot(txrx, which_side), which_adc, value);
}

int
usrp_basic::common_read_aux_adc(txrx_t txrx, int which_side, int which_adc)
{
  return _read_aux_adc(to_slot(txrx, which_side), which_adc);
}


////////////////////////////////////////////////////////////////
//
//			   usrp_basic_rx
//
////////////////////////////////////////////////////////////////

static unsigned char rx_init_regs[] = {
  REG_RX_PWR_DN,	0,
  REG_RX_A,		0,	// minimum gain = 0x00 (max gain = 0x14)
  REG_RX_B,		0,	// minimum gain = 0x00 (max gain = 0x14)
  REG_RX_MISC,		(RX_MISC_HS_DUTY_CYCLE | RX_MISC_CLK_DUTY),
  REG_RX_IF,		(RX_IF_USE_CLKOUT1
			 | RX_IF_2S_COMP),
  REG_RX_DIGITAL,	(RX_DIGITAL_2_CHAN)
};


usrp_basic_rx::usrp_basic_rx (int which_board, int fusb_block_size, int fusb_nblocks,
			      const std::string fpga_filename,
			      const std::string firmware_filename
			      )
  : usrp_basic (which_board, open_rx_interface, fpga_filename, firmware_filename),
    d_devhandle (0), d_ephandle (0),
    d_bytes_seen (0), d_first_read (true),
    d_rx_enable (false)
{
  // initialize rx specific registers

  if (!usrp_9862_write_many_all (d_udh, rx_init_regs, sizeof (rx_init_regs))){
    fprintf (stderr, "usrp_basic_rx: failed to init AD9862 RX regs\n");
    throw std::runtime_error ("usrp_basic_rx/init_9862");
  }

  if (0){
    // FIXME power down 2nd codec rx path
    usrp_9862_write (d_udh, 1, REG_RX_PWR_DN, 0x1);	// power down everything
  }

  // Reset the rx path and leave it disabled.
  set_rx_enable (false);
  usrp_set_fpga_rx_reset (d_udh, true);
  usrp_set_fpga_rx_reset (d_udh, false);

  set_fpga_rx_sample_rate_divisor (2);	// usually correct

  set_dc_offset_cl_enable(0xf, 0xf);	// enable DC offset removal control loops

  probe_rx_slots (false);

  //d_db[0] = instantiate_dbs(d_dbid[0], this, 0);
  //d_db[1] = instantiate_dbs(d_dbid[1], this, 1);

  // check fusb buffering parameters

  if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");

  if (fusb_nblocks < 0)
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
  
  if (fusb_block_size == 0)
    fusb_block_size = fusb_sysconfig::default_block_size();

  if (fusb_nblocks == 0)
    fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);

  d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
  d_ephandle = d_devhandle->make_ephandle (USRP_RX_ENDPOINT, true,
					   fusb_block_size, fusb_nblocks);

  write_atr_mask(0, 0);		// zero Rx A Auto Transmit/Receive regs
  write_atr_txval(0, 0);
  write_atr_rxval(0, 0);
  write_atr_mask(1, 0);		// zero Rx B Auto Transmit/Receive regs
  write_atr_txval(1, 0);
  write_atr_rxval(1, 0);
}

static unsigned char rx_fini_regs[] = {
  REG_RX_PWR_DN,	0x1				// power down everything
};

usrp_basic_rx::~usrp_basic_rx ()
{
  if (!set_rx_enable (false)){
    fprintf (stderr, "usrp_basic_rx: set_fpga_rx_enable failed\n");
    usb_strerror ();
  }

  d_ephandle->stop ();
  delete d_ephandle;
  delete d_devhandle;

  if (!usrp_9862_write_many_all (d_udh, rx_fini_regs, sizeof (rx_fini_regs))){
    fprintf (stderr, "usrp_basic_rx: failed to fini AD9862 RX regs\n");
  }

  shutdown_daughterboards();
}


bool
usrp_basic_rx::start ()
{
  if (!usrp_basic::start ())	// invoke parent's method
    return false;

  // fire off reads before asserting rx_enable

  if (!d_ephandle->start ()){
    fprintf (stderr, "usrp_basic_rx: failed to start end point streaming");
    usb_strerror ();
    return false;
  }

  if (!set_rx_enable (true)){
    fprintf (stderr, "usrp_basic_rx: set_rx_enable failed\n");
    usb_strerror ();
    return false;
  }
  
  return true;
}

bool
usrp_basic_rx::stop ()
{
  bool ok = usrp_basic::stop();

  if (!set_rx_enable(false)){
    fprintf (stderr, "usrp_basic_rx: set_rx_enable(false) failed\n");
    usb_strerror ();
    ok = false;
  }

  if (!d_ephandle->stop()){
    fprintf (stderr, "usrp_basic_rx: failed to stop end point streaming");
    usb_strerror ();
    ok = false;
  }

  return ok;
}

usrp_basic_rx *
usrp_basic_rx::make (int which_board, int fusb_block_size, int fusb_nblocks,
		     const std::string fpga_filename,
		     const std::string firmware_filename)
{
  usrp_basic_rx *u = 0;
  
  try {
    u = new usrp_basic_rx (which_board, fusb_block_size, fusb_nblocks,
			   fpga_filename, firmware_filename);
    return u;
  }
  catch (...){
    delete u;
    return 0;
  }

  return u;
}

bool
usrp_basic_rx::set_fpga_rx_sample_rate_divisor (unsigned int div)
{
  return _write_fpga_reg (FR_RX_SAMPLE_RATE_DIV, div - 1);
}


/*
 * \brief read data from the D/A's via the FPGA.
 * \p len must be a multiple of 512 bytes.
 *
 * \returns the number of bytes read, or -1 on error.
 *
 * If overrun is non-NULL it will be set true iff an RX overrun is detected.
 */
int
usrp_basic_rx::read (void *buf, int len, bool *overrun)
{
  int	r;
  
  if (overrun)
    *overrun = false;
  
  if (len < 0 || (len % 512) != 0){
    fprintf (stderr, "usrp_basic_rx::read: invalid length = %d\n", len);
    return -1;
  }

  r = d_ephandle->read (buf, len);
  if (r > 0)
    d_bytes_seen += r;

  /*
   * In many cases, the FPGA reports an rx overrun right after we
   * enable the Rx path.  If this is our first read, check for the
   * overrun to clear the condition, then ignore the result.
   */
  if (0 && d_first_read){	// FIXME
    d_first_read = false;
    bool bogus_overrun;
    usrp_check_rx_overrun (d_udh, &bogus_overrun);
  }

  if (overrun != 0 && d_bytes_seen >= d_bytes_per_poll){
    d_bytes_seen = 0;
    if (!usrp_check_rx_overrun (d_udh, overrun)){
      fprintf (stderr, "usrp_basic_rx: usrp_check_rx_overrun failed\n");
      usb_strerror ();
    }
  }
    
  return r;
}

bool
usrp_basic_rx::set_rx_enable (bool on)
{
  d_rx_enable = on;
  return usrp_set_fpga_rx_enable (d_udh, on);
}

// conditional disable, return prev state
bool
usrp_basic_rx::disable_rx ()
{
  bool enabled = rx_enable ();
  if (enabled)
    set_rx_enable (false);
  return enabled;
}

// conditional set
void
usrp_basic_rx::restore_rx (bool on)
{
  if (on != rx_enable ())
    set_rx_enable (on);
}

void
usrp_basic_rx::probe_rx_slots (bool verbose)
{
  struct usrp_dboard_eeprom	eeprom;
  static int slot_id_map[2] = { SLOT_RX_A, SLOT_RX_B };
  static const char *slot_name[2] = { "RX d'board A", "RX d'board B" };

  for (int i = 0; i < 2; i++){
    int slot_id = slot_id_map [i];
    const char *msg = 0;
    usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);

    switch (s){
    case UDBE_OK:
      d_dbid[i] = eeprom.id;
      msg = usrp_dbid_to_string (eeprom.id).c_str ();
      set_adc_offset (2*i+0, eeprom.offset[0]);
      set_adc_offset (2*i+1, eeprom.offset[1]);
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
      break;
      
    case UDBE_NO_EEPROM:
      d_dbid[i] = -1;
      msg = "<none>";
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
      break;
      
    case UDBE_INVALID_EEPROM:
      d_dbid[i] = -2;
      msg = "Invalid EEPROM contents";
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
      break;
      
    case UDBE_BAD_SLOT:
    default:
      assert (0);
    }

    if (verbose){
      fflush (stdout);
      fprintf (stderr, "%s: %s\n", slot_name[i], msg);
    }
  }
}

bool
usrp_basic_rx::set_pga (int which_amp, double gain)
{
  return common_set_pga(C_RX, which_amp, gain);
}

double
usrp_basic_rx::pga(int which_amp) const
{
  return common_pga(C_RX, which_amp);
}

double
usrp_basic_rx::pga_min() const
{
  return common_pga_min(C_RX);
}

double
usrp_basic_rx::pga_max() const
{
  return common_pga_max(C_RX);
}

double
usrp_basic_rx::pga_db_per_step() const
{
  return common_pga_db_per_step(C_RX);
}

bool
usrp_basic_rx::_write_oe (int which_side, int value, int mask)
{
  return _common_write_oe(C_RX, which_side, value, mask);
}

bool
usrp_basic_rx::write_io (int which_side, int value, int mask)
{
  return common_write_io(C_RX, which_side, value, mask);
}

bool
usrp_basic_rx::read_io (int which_side, int *value)
{
  return common_read_io(C_RX, which_side, value);
}

int
usrp_basic_rx::read_io (int which_side)
{
  return common_read_io(C_RX, which_side);
}

bool
usrp_basic_rx::write_refclk(int which_side, int value)
{
  return common_write_refclk(C_RX, which_side, value);
}

bool
usrp_basic_rx::write_atr_mask(int which_side, int value)
{
  return common_write_atr_mask(C_RX, which_side, value);
}

bool
usrp_basic_rx::write_atr_txval(int which_side, int value)
{
  return common_write_atr_txval(C_RX, which_side, value);
}

bool
usrp_basic_rx::write_atr_rxval(int which_side, int value)
{
  return common_write_atr_rxval(C_RX, which_side, value);
}

bool
usrp_basic_rx::write_aux_dac (int which_side, int which_dac, int value)
{
  return common_write_aux_dac(C_RX, which_side, which_dac, value);
}

bool
usrp_basic_rx::read_aux_adc (int which_side, int which_adc, int *value)
{
  return common_read_aux_adc(C_RX, which_side, which_adc, value);
}

int
usrp_basic_rx::read_aux_adc (int which_side, int which_adc)
{
  return common_read_aux_adc(C_RX, which_side, which_adc);
}

int
usrp_basic_rx::block_size () const { return d_ephandle->block_size(); }

////////////////////////////////////////////////////////////////
//
//			   usrp_basic_tx
//
////////////////////////////////////////////////////////////////


//
// DAC input rate 64 MHz interleaved for a total input rate of 128 MHz
// DAC input is latched on rising edge of CLKOUT2
// NCO is disabled
// interpolate 2x
// coarse modulator disabled
//

static unsigned char tx_init_regs[] = {
  REG_TX_PWR_DN,	0,
  REG_TX_A_OFFSET_LO,	0,
  REG_TX_A_OFFSET_HI,	0,
  REG_TX_B_OFFSET_LO,	0,
  REG_TX_B_OFFSET_HI,	0,
  REG_TX_A_GAIN,	(TX_X_GAIN_COARSE_FULL | 0),
  REG_TX_B_GAIN,	(TX_X_GAIN_COARSE_FULL | 0),
  REG_TX_PGA,		0xff,			// maximum gain (0 dB)
  REG_TX_MISC,		0,
  REG_TX_IF,		(TX_IF_USE_CLKOUT1
			 | TX_IF_I_FIRST
			 | TX_IF_INV_TX_SYNC
			 | TX_IF_2S_COMP
			 | TX_IF_INTERLEAVED),
  REG_TX_DIGITAL,	(TX_DIGITAL_2_DATA_PATHS
			 | TX_DIGITAL_INTERPOLATE_4X),
  REG_TX_MODULATOR,	(TX_MODULATOR_DISABLE_NCO
			 | TX_MODULATOR_COARSE_MODULATION_NONE),
  REG_TX_NCO_FTW_7_0,	0,
  REG_TX_NCO_FTW_15_8,	0,
  REG_TX_NCO_FTW_23_16,	0
};

usrp_basic_tx::usrp_basic_tx (int which_board, int fusb_block_size, int fusb_nblocks,
			      const std::string fpga_filename,
			      const std::string firmware_filename)
  : usrp_basic (which_board, open_tx_interface, fpga_filename, firmware_filename),
    d_devhandle (0), d_ephandle (0),
    d_bytes_seen (0), d_first_write (true),
    d_tx_enable (false)
{
  if (!usrp_9862_write_many_all (d_udh, tx_init_regs, sizeof (tx_init_regs))){
    fprintf (stderr, "usrp_basic_tx: failed to init AD9862 TX regs\n");
    throw std::runtime_error ("usrp_basic_tx/init_9862");
  }

  if (0){
    // FIXME power down 2nd codec tx path
    usrp_9862_write (d_udh, 1, REG_TX_PWR_DN,
		     (TX_PWR_DN_TX_DIGITAL
		      | TX_PWR_DN_TX_ANALOG_BOTH));
  }

  // Reset the tx path and leave it disabled.
  set_tx_enable (false);
  usrp_set_fpga_tx_reset (d_udh, true);
  usrp_set_fpga_tx_reset (d_udh, false);

  set_fpga_tx_sample_rate_divisor (4);	// we're using interp x4

  probe_tx_slots (false);

  //d_db[0] = instantiate_dbs(d_dbid[0], this, 0);
  //d_db[1] = instantiate_dbs(d_dbid[1], this, 1);

  // check fusb buffering parameters

  if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");

  if (fusb_nblocks < 0)
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
  
  if (fusb_block_size == 0)
    fusb_block_size = FUSB_BLOCK_SIZE;

  if (fusb_nblocks == 0)
    fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);

  d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
  d_ephandle = d_devhandle->make_ephandle (USRP_TX_ENDPOINT, false,
					   fusb_block_size, fusb_nblocks);

  write_atr_mask(0, 0);		// zero Tx A Auto Transmit/Receive regs
  write_atr_txval(0, 0);
  write_atr_rxval(0, 0);
  write_atr_mask(1, 0);		// zero Tx B Auto Transmit/Receive regs
  write_atr_txval(1, 0);
  write_atr_rxval(1, 0);
}


static unsigned char tx_fini_regs[] = {
  REG_TX_PWR_DN,	(TX_PWR_DN_TX_DIGITAL
			 | TX_PWR_DN_TX_ANALOG_BOTH),
  REG_TX_MODULATOR,	(TX_MODULATOR_DISABLE_NCO
			 | TX_MODULATOR_COARSE_MODULATION_NONE)
};

usrp_basic_tx::~usrp_basic_tx ()
{
  d_ephandle->stop ();
  delete d_ephandle;
  delete d_devhandle;

  if (!usrp_9862_write_many_all (d_udh, tx_fini_regs, sizeof (tx_fini_regs))){
    fprintf (stderr, "usrp_basic_tx: failed to fini AD9862 TX regs\n");
  }

  shutdown_daughterboards();
}

bool
usrp_basic_tx::start ()
{
  if (!usrp_basic::start ())
    return false;

  if (!set_tx_enable (true)){
    fprintf (stderr, "usrp_basic_tx: set_tx_enable failed\n");
    usb_strerror ();
    return false;
  }
  
  if (!d_ephandle->start ()){
    fprintf (stderr, "usrp_basic_tx: failed to start end point streaming");
    usb_strerror ();
    return false;
  }

  return true;
}

bool
usrp_basic_tx::stop ()
{
  bool ok = usrp_basic::stop ();

  if (!d_ephandle->stop ()){
    fprintf (stderr, "usrp_basic_tx: failed to stop end point streaming");
    usb_strerror ();
    ok = false;
  }

  if (!set_tx_enable (false)){
    fprintf (stderr, "usrp_basic_tx: set_tx_enable(false) failed\n");
    usb_strerror ();
    ok = false;
  }

  return ok;
}

usrp_basic_tx *
usrp_basic_tx::make (int which_board, int fusb_block_size, int fusb_nblocks,
		     const std::string fpga_filename,
		     const std::string firmware_filename)
{
  usrp_basic_tx *u = 0;
  
  try {
    u = new usrp_basic_tx (which_board, fusb_block_size, fusb_nblocks,
			   fpga_filename, firmware_filename);
    return u;
  }
  catch (...){
    delete u;
    return 0;
  }

  return u;
}

bool
usrp_basic_tx::set_fpga_tx_sample_rate_divisor (unsigned int div)
{
  return _write_fpga_reg (FR_TX_SAMPLE_RATE_DIV, div - 1);
}

/*!
 * \brief Write data to the A/D's via the FPGA.
 *
 * \p len must be a multiple of 512 bytes.
 * \returns number of bytes written or -1 on error.
 *
 * if \p underrun is non-NULL, it will be set to true iff
 * a transmit underrun condition is detected.
 */
int
usrp_basic_tx::write (const void *buf, int len, bool *underrun)
{
  int	r;
  
  if (underrun)
    *underrun = false;
  
  if (len < 0 || (len % 512) != 0){
    fprintf (stderr, "usrp_basic_tx::write: invalid length = %d\n", len);
    return -1;
  }

  r = d_ephandle->write (buf, len);
  if (r > 0)
    d_bytes_seen += r;
    
  /*
   * In many cases, the FPGA reports an tx underrun right after we
   * enable the Tx path.  If this is our first write, check for the
   * underrun to clear the condition, then ignore the result.
   */
  if (d_first_write && d_bytes_seen >= 4 * FUSB_BLOCK_SIZE){
    d_first_write = false;
    bool bogus_underrun;
    usrp_check_tx_underrun (d_udh, &bogus_underrun);
  }

  if (underrun != 0 && d_bytes_seen >= d_bytes_per_poll){
    d_bytes_seen = 0;
    if (!usrp_check_tx_underrun (d_udh, underrun)){
      fprintf (stderr, "usrp_basic_tx: usrp_check_tx_underrun failed\n");
      usb_strerror ();
    }
  }

  return r;
}

void
usrp_basic_tx::wait_for_completion ()
{
  d_ephandle->wait_for_completion ();
}

bool
usrp_basic_tx::set_tx_enable (bool on)
{
  d_tx_enable = on;
  // fprintf (stderr, "set_tx_enable %d\n", on);
  return usrp_set_fpga_tx_enable (d_udh, on);
}

// conditional disable, return prev state
bool
usrp_basic_tx::disable_tx ()
{
  bool enabled = tx_enable ();
  if (enabled)
    set_tx_enable (false);
  return enabled;
}

// conditional set
void
usrp_basic_tx::restore_tx (bool on)
{
  if (on != tx_enable ())
    set_tx_enable (on);
}

void
usrp_basic_tx::probe_tx_slots (bool verbose)
{
  struct usrp_dboard_eeprom	eeprom;
  static int slot_id_map[2] = { SLOT_TX_A, SLOT_TX_B };
  static const char *slot_name[2] = { "TX d'board A", "TX d'board B" };

  for (int i = 0; i < 2; i++){
    int slot_id = slot_id_map [i];
    const char *msg = 0;
    usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);

    switch (s){
    case UDBE_OK:
      d_dbid[i] = eeprom.id;
      msg = usrp_dbid_to_string (eeprom.id).c_str ();
      // FIXME, figure out interpretation of dc offset for TX d'boards
      // offset = (eeprom.offset[1] << 16) | (eeprom.offset[0] & 0xffff);
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
      break;
      
    case UDBE_NO_EEPROM:
      d_dbid[i] = -1;
      msg = "<none>";
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
      break;
      
    case UDBE_INVALID_EEPROM:
      d_dbid[i] = -2;
      msg = "Invalid EEPROM contents";
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
      break;
      
    case UDBE_BAD_SLOT:
    default:
      assert (0);
    }

    if (verbose){
      fflush (stdout);
      fprintf (stderr, "%s: %s\n", slot_name[i], msg);
    }
  }
}

bool
usrp_basic_tx::set_pga (int which_amp, double gain)
{
  return common_set_pga(C_TX, which_amp, gain);
}

double
usrp_basic_tx::pga (int which_amp) const
{
  return common_pga(C_TX, which_amp);
}

double
usrp_basic_tx::pga_min() const
{
  return common_pga_min(C_TX);
}

double
usrp_basic_tx::pga_max() const
{
  return common_pga_max(C_TX);
}

double
usrp_basic_tx::pga_db_per_step() const
{
  return common_pga_db_per_step(C_TX);
}

bool
usrp_basic_tx::_write_oe (int which_side, int value, int mask)
{
  return _common_write_oe(C_TX, which_side, value, mask);
}

bool
usrp_basic_tx::write_io (int which_side, int value, int mask)
{
  return common_write_io(C_TX, which_side, value, mask);
}

bool
usrp_basic_tx::read_io (int which_side, int *value)
{
  return common_read_io(C_TX, which_side, value);
}

int
usrp_basic_tx::read_io (int which_side)
{
  return common_read_io(C_TX, which_side);
}

bool
usrp_basic_tx::write_refclk(int which_side, int value)
{
  return common_write_refclk(C_TX, which_side, value);
}

bool
usrp_basic_tx::write_atr_mask(int which_side, int value)
{
  return common_write_atr_mask(C_TX, which_side, value);
}

bool
usrp_basic_tx::write_atr_txval(int which_side, int value)
{
  return common_write_atr_txval(C_TX, which_side, value);
}

bool
usrp_basic_tx::write_atr_rxval(int which_side, int value)
{
  return common_write_atr_rxval(C_TX, which_side, value);
}

bool
usrp_basic_tx::write_aux_dac (int which_side, int which_dac, int value)
{
  return common_write_aux_dac(C_TX, which_side, which_dac, value);
}

bool
usrp_basic_tx::read_aux_adc (int which_side, int which_adc, int *value)
{
  return common_read_aux_adc(C_TX, which_side, which_adc, value);
}

int
usrp_basic_tx::read_aux_adc (int which_side, int which_adc)
{
  return common_read_aux_adc(C_TX, which_side, which_adc);
}

int
usrp_basic_tx::block_size () const { return d_ephandle->block_size(); }