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 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
|
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2015-2018 Josh Blum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bladeRF_SoapySDR.hpp"
#include <SoapySDR/Logger.hpp>
#include <algorithm> //find
#include <stdexcept>
#include <cstdio>
#include <cmath>
//! convert bladerf range to a soapysdr range
static SoapySDR::Range toRange(const bladerf_range* range)
{
return SoapySDR::Range(range->min*range->scale, range->max*range->scale, range->step*range->scale);
}
/*******************************************************************
* Device init/shutdown
******************************************************************/
bladeRF_SoapySDR::bladeRF_SoapySDR(const bladerf_devinfo &devinfo):
_isBladeRF1(false),
_rxSampRate(1.0),
_txSampRate(1.0),
_inTxBurst(false),
_rxFloats(false),
_txFloats(false),
_rxOverflow(false),
_rxNextTicks(0),
_txNextTicks(0),
_timeNsOffset(0),
_rxBuffSize(0),
_txBuffSize(0),
_rxMinTimeoutMs(0),
_xb200Mode("disabled"),
_samplingMode("internal"),
_loopbackMode("disabled"),
_dev(NULL)
{
bladerf_devinfo info = devinfo;
SoapySDR::logf(SOAPY_SDR_INFO, "bladerf_open_with_devinfo()");
int ret = bladerf_open_with_devinfo(&_dev, &info);
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_open_with_devinfo() returned %s", _err2str(ret).c_str());
throw std::runtime_error("bladerf_open_with_devinfo() failed " + _err2str(ret));
}
_isBladeRF1 = std::string(bladerf_get_board_name(_dev)) == "bladerf1";
_isBladeRF2 = std::string(bladerf_get_board_name(_dev)) == "bladerf2";
bladerf_serial serial;
ret = bladerf_get_serial_struct(_dev, &serial);
if (ret == 0) SoapySDR::logf(SOAPY_SDR_INFO, "bladerf_get_serial() = %s", serial.serial);
//initialize the sample rates to something
this->setSampleRate(SOAPY_SDR_RX, 0, 4e6);
this->setSampleRate(SOAPY_SDR_TX, 0, 4e6);
}
bladeRF_SoapySDR::~bladeRF_SoapySDR(void)
{
SoapySDR::logf(SOAPY_SDR_INFO, "bladerf_close()");
if (_dev != NULL) bladerf_close(_dev);
}
/*******************************************************************
* Identification API
******************************************************************/
std::string bladeRF_SoapySDR::getHardwareKey(void) const
{
return bladerf_get_board_name(_dev);
}
SoapySDR::Kwargs bladeRF_SoapySDR::getHardwareInfo(void) const
{
SoapySDR::Kwargs info;
{
bladerf_serial serial;
int ret = bladerf_get_serial_struct(_dev, &serial);
if (ret == 0) info["serial"] = serial.serial;
}
{
bladerf_fpga_size fpgaSize = BLADERF_FPGA_UNKNOWN;
int ret = bladerf_get_fpga_size(_dev, &fpgaSize);
char fpgaStr[100];
sprintf(fpgaStr, "%u", int(fpgaSize));
if (ret == 0) info["fpga_size"] = fpgaStr;
}
{
struct bladerf_version verInfo;
int ret = bladerf_fw_version(_dev, &verInfo);
if (ret == 0) info["fw_version"] = verInfo.describe;
}
{
struct bladerf_version verInfo;
int ret = bladerf_fpga_version(_dev, &verInfo);
if (ret == 0) info["fpga_version"] = verInfo.describe;
}
return info;
}
/*******************************************************************
* Channels API
******************************************************************/
size_t bladeRF_SoapySDR::getNumChannels(const int direction) const
{
return bladerf_get_channel_count(_dev, (direction == SOAPY_SDR_RX)?BLADERF_RX:BLADERF_TX);
}
bool bladeRF_SoapySDR::getFullDuplex(const int, const size_t) const
{
return true;
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listAntennas(const int direction, const size_t channel) const
{
return {BLADERF_CHANNEL_IS_TX(_toch(direction, channel))?"TX":"RX"};
}
void bladeRF_SoapySDR::setAntenna(const int, const size_t, const std::string &)
{
return; //nothing to set, ignore it
}
std::string bladeRF_SoapySDR::getAntenna(const int direction, const size_t channel) const
{
return this->listAntennas(direction, channel).front();
}
/*******************************************************************
* Calibration API
******************************************************************/
bool bladeRF_SoapySDR::hasDCOffset(const int, const size_t) const
{
return true;
}
void bladeRF_SoapySDR::setDCOffset(const int direction, const size_t channel, const std::complex<double> &offset)
{
int ret = 0;
int16_t i = 0;
int16_t q = 0;
if (offset.real() > 1.0)
i = int16_t(1.0 * 2048);
else
i = int16_t(offset.real() * 2048);
if (offset.imag() > 1.0)
q = int16_t(1.0 * 2048);
else
q = int16_t(offset.imag() * 2048);
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_I, i);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", i, _err2str(ret).c_str());
throw std::runtime_error("setDCOffset() " + _err2str(ret));
}
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_Q, q);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", q, _err2str(ret).c_str());
throw std::runtime_error("setDCOffset() " + _err2str(ret));
}
}
std::complex<double> bladeRF_SoapySDR::getDCOffset(const int direction, const size_t channel) const
{
int ret = 0;
int16_t i = 0;
int16_t q = 0;
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_I, &i);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getDCOffset() " + _err2str(ret));
}
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_Q, &q);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getDCOffset() " + _err2str(ret));
}
std::complex<double> z(i / 2048.0f, q / 2048.0f);
return z;
}
bool bladeRF_SoapySDR::hasIQBalance(const int, const size_t channel) const
{
return true;
}
void bladeRF_SoapySDR::setIQBalance(const int direction, const size_t channel, const std::complex<double> &balance)
{
int ret = 0;
int16_t gain = 0;
int16_t phase = 0;
if (balance.real() > 1.0)
gain = int16_t(1.0 * 4096);
else
gain = int16_t(balance.real() * 4096);
if (balance.imag() > 1.0)
phase = int16_t(1.0 * 4096);
else
phase = int16_t(balance.imag() * 4096);
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_GAIN, gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", gain, _err2str(ret).c_str());
throw std::runtime_error("setIQBalance() " + _err2str(ret));
}
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_PHASE, phase);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", phase, _err2str(ret).c_str());
throw std::runtime_error("setIQBalance() " + _err2str(ret));
}
}
std::complex<double> bladeRF_SoapySDR::getIQBalance(const int direction, const size_t channel) const
{
int ret = 0;
int16_t gain = 0;
int16_t phase = 0;
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_GAIN, &gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getIQBalance() " + _err2str(ret));
}
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_PHASE, &phase);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getIQBalance() " + _err2str(ret));
}
std::complex<double> z(gain / 4096.0f, phase / 4096.0f);
return z;
}
/*******************************************************************
* Gain API
******************************************************************/
bool bladeRF_SoapySDR::hasGainMode(const int direction, const size_t channel) const
{
if (_toch(direction, channel) != BLADERF_CHANNEL_RX(channel)) {
return false;
} else {
/* This actually depends on a lot of things, including presence of a LUT
* table, so best to determine dynamically.
*/
bladerf_gain_mode mode;
int ret;
ret = bladerf_get_gain_mode(_dev, _toch(direction, channel), &mode);
if (ret != 0) {
return false;
}
/* Test if it will take automatic mode */
ret = bladerf_set_gain_mode(_dev, _toch(direction, channel), BLADERF_GAIN_AUTOMATIC);
if (ret != 0) {
return false;
}
/* We're good - restore it to the original mode */
ret = bladerf_set_gain_mode(_dev, _toch(direction, channel), mode);
if (ret != 0) {
return false;
}
return true;
}
}
void bladeRF_SoapySDR::setGainMode(const int direction, const size_t channel, const bool automatic)
{
if (direction == SOAPY_SDR_TX) return; //not supported on tx
bladerf_gain_mode gain_mode = automatic ? BLADERF_GAIN_AUTOMATIC : BLADERF_GAIN_MANUAL;
const int ret = bladerf_set_gain_mode(_dev, _toch(direction, channel), gain_mode);
if (ret != 0 and automatic) //only throw when mode is automatic, manual is default even when call bombs
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_gain_mode(%s) returned %s", automatic?"automatic":"manual", _err2str(ret).c_str());
throw std::runtime_error("setGainMode() " + _err2str(ret));
}
bladerf_gain_mode return_mode;
bladerf_get_gain_mode(_dev, _toch(direction, channel), &return_mode);
std::string gain_mode_string;
if (return_mode == BLADERF_GAIN_DEFAULT) {
gain_mode_string = "default";
} else if (return_mode == BLADERF_GAIN_MGC) {
gain_mode_string = "manual";
} else if (return_mode == BLADERF_GAIN_FASTATTACK_AGC) {
gain_mode_string = "fastattack";
} else if (return_mode == BLADERF_GAIN_SLOWATTACK_AGC) {
gain_mode_string = "slowattack";
} else if (return_mode == BLADERF_GAIN_HYBRID_AGC) {
gain_mode_string = "hybrid";
} else {
gain_mode_string = "<unknown>";
}
SoapySDR::logf(SOAPY_SDR_INFO, "setGainMode(%s, %d, %d), actual = %s", direction==SOAPY_SDR_RX?"Rx":"Tx", int(channel), automatic, gain_mode_string.c_str());
}
bool bladeRF_SoapySDR::getGainMode(const int direction, const size_t channel) const
{
if (direction == SOAPY_SDR_TX) return false; //not supported on tx
bladerf_gain_mode gain_mode;
int ret = bladerf_get_gain_mode(_dev, _toch(direction, channel), &gain_mode);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_mode() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getGainMode() " + _err2str(ret));
}
return gain_mode == BLADERF_GAIN_AUTOMATIC;
}
std::vector<std::string> bladeRF_SoapySDR::listGains(const int direction, const size_t channel) const
{
#define MAX_STAGES 8
const char *stages[MAX_STAGES];
int ret = bladerf_get_gain_stages(_dev, _toch(direction, channel), (const char **)&stages, MAX_STAGES);
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_stages() returned %s", _err2str(ret).c_str());
throw std::runtime_error("listGains() " + _err2str(ret));
}
std::vector<std::string> options;
for (int i = 0; i < ret; i++) options.push_back(stages[i]);
return options;
}
void bladeRF_SoapySDR::setGain(const int direction, const size_t channel, const double value)
{
const int ret = bladerf_set_gain(_dev, _toch(direction, channel), bladerf_gain(std::round(value)));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_gain(%f) returned %s", value, _err2str(ret).c_str());
throw std::runtime_error("setGain() " + _err2str(ret));
}
}
void bladeRF_SoapySDR::setGain(const int direction, const size_t channel, const std::string &name, const double value)
{
int ret = bladerf_set_gain_stage(_dev, _toch(direction, channel), name.c_str(), bladerf_gain(std::round(value)));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_gain_stage(%s, %f) returned %s", name.c_str(), value, _err2str(ret).c_str());
throw std::runtime_error("setGain("+name+") " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getGain(const int direction, const size_t channel) const
{
bladerf_gain gain(0);
const int ret = bladerf_get_gain(_dev, _toch(direction, channel), &gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getGain() " + _err2str(ret));
}
return double(gain);
}
double bladeRF_SoapySDR::getGain(const int direction, const size_t channel, const std::string &name) const
{
bladerf_gain gain(0);
int ret = bladerf_get_gain_stage(_dev, _toch(direction, channel), name.c_str(), &gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_stage(%s) returned %s", name.c_str(), _err2str(ret).c_str());
throw std::runtime_error("getGain("+name+") " + _err2str(ret));
}
return double(gain);
}
SoapySDR::Range bladeRF_SoapySDR::getGainRange(const int direction, const size_t channel) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_gain_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getGainRange()" + _err2str(ret));
}
return toRange(range);
}
SoapySDR::Range bladeRF_SoapySDR::getGainRange(const int direction, const size_t channel, const std::string &name) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_gain_stage_range(_dev, _toch(direction, channel), name.c_str(), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_stage_range(%s) returned %s", name.c_str(), _err2str(ret).c_str());
throw std::runtime_error("getGainRange("+name+")" + _err2str(ret));
}
return toRange(range);
}
/*******************************************************************
* Frequency API
******************************************************************/
void bladeRF_SoapySDR::setFrequency(const int direction, const size_t channel, const std::string &name, const double frequency, const SoapySDR::Kwargs &args)
{
if (name == "BB") return; //for compatibility
if (name != "RF") throw std::runtime_error("setFrequency("+name+") unknown name");
//NOTE on quick tunes:
// - this is available on BladeRF2, not BladeRF1
// - there can be up to NUM_BBP_FASTLOCK_PROFILES (256) quick tunes in both directions.
// bladerf2_get_quick_tune returns BLADERF_ERR_UNEXPECTED if you try to get more.
// - to clear previous quick tunes, the RFIC should have its state reset which is currently done
// when the FPGA is loaded or reloaded. To have the ability to live reset the index to 0,
// the ADI AXI core might have to be modified.
//if "saveQuickTune" == "1", set the frequency and store the quick tune parameter.
//NOTE that it's possible to overwrite previous quick tune parameter.
auto saveQuickTuneIter = args.find("saveQuickTune");
if (saveQuickTuneIter != args.end() && saveQuickTuneIter->second == "1")
{
if (!_isBladeRF2)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "saveQuickTune is only available for BladeRF2.");
throw std::runtime_error("saveQuickTune is only available for BladeRF2.");
}
setRfFrequency(direction, channel, frequency);
auto quickTune = getQuickTune(direction, channel);
if (quickTune == nullptr)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "Cannot set frequency for retune.");
throw std::runtime_error("Cannot set frequency for retune.");
}
auto quickTuneIter = _quickTunesByDirChanAndFreq.find({ direction, channel, frequency });
if (quickTuneIter != _quickTunesByDirChanAndFreq.end()) delete quickTuneIter->second;
_quickTunesByDirChanAndFreq[{direction, channel, frequency}] = quickTune;
return;
}
//Else, if "reuseQuickTune" == "1", we'll see if a corresponding quick tune parameter exists.
//If it does, we'll use it. Else, we throw an exception.
//If "timestamp" is specified, the quick tune will be scheduled at that time, else it'll be done at once.
auto reuseQuickTuneIter = args.find("reuseQuickTune");
if (reuseQuickTuneIter != args.end() && reuseQuickTuneIter->second == "1") {
if (!_isBladeRF2)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "reuseQuickTune is only available for BladeRF2.");
throw std::runtime_error("reuseQuickTune is only available for BladeRF2.");
}
auto quickTuneIter = _quickTunesByDirChanAndFreq.find({ direction, channel, frequency });
if (quickTuneIter == _quickTunesByDirChanAndFreq.end())
{
SoapySDR::logf(SOAPY_SDR_ERROR, "Unkown quick tune for frequency %f and channel %d", frequency, channel);
throw std::runtime_error("Unkown quick tune");
}
auto value = args.find("timestamp");
long long timestamp = value == args.end() ? 0 : std::stoll(value->second);
retune(direction, channel, timestamp, quickTuneIter->second);
return;
}
//Else, we simply set the RF frequency.
setRfFrequency(direction, channel, frequency);
}
void bladeRF_SoapySDR::setRfFrequency(const int direction, const size_t channel, const double frequency)
{
int ret = bladerf_set_frequency(_dev, _toch(direction, channel), bladerf_frequency(std::round(frequency)));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_frequency(%f) returned %s", frequency, _err2str(ret).c_str());
throw std::runtime_error("setFrequency(RF) " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getFrequency(const int direction, const size_t channel, const std::string &name) const
{
if (name == "BB") return 0.0; //for compatibility
if (name != "RF") throw std::runtime_error("getFrequency("+name+") unknown name");
bladerf_frequency freq(0);
int ret = bladerf_get_frequency(_dev, _toch(direction, channel), &freq);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_frequency() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getFrequency("+name+") " + _err2str(ret));
}
return double(freq);
}
std::vector<std::string> bladeRF_SoapySDR::listFrequencies(const int, const size_t channel) const
{
return {"RF"};
}
SoapySDR::RangeList bladeRF_SoapySDR::getFrequencyRange(const int direction, const size_t channel, const std::string &name) const
{
if (name == "BB") return SoapySDR::RangeList(1, SoapySDR::Range(0.0, 0.0)); //for compatibility
if (name != "RF") throw std::runtime_error("getFrequencyRange("+name+") unknown name");
const bladerf_range* range(nullptr);
int ret = bladerf_get_frequency_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_frequency_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getFrequencyRange() " + _err2str(ret));
}
return {toRange(range)};
}
bladerf_quick_tune* bladeRF_SoapySDR::getQuickTune(const int direction, const size_t channel) const
{
bladerf_quick_tune* quick_tune = new bladerf_quick_tune();
bladerf_channel ch = _toch(direction, channel);
int ret = bladerf_get_quick_tune(_dev, ch, quick_tune);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_quick_tune() returned %s", _err2str(ret).c_str());
delete quick_tune;
return nullptr;
}
return quick_tune;
}
void bladeRF_SoapySDR::retune(const int direction, const size_t channel, long long timestamp, bladerf_quick_tune* quickTune)
{
bladerf_channel ch = _toch(direction, channel);
int ret = bladerf_schedule_retune(_dev, ch, timestamp, 0 /* frequency not needed for retune */, quickTune);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_schedule_retune() returned %s", _err2str(ret).c_str());
throw std::runtime_error("retune() " + _err2str(ret));
}
}
/*******************************************************************
* Sample Rate API
******************************************************************/
void bladeRF_SoapySDR::setSampleRate(const int direction, const size_t channel, const double rate)
{
bladerf_rational_rate ratRate;
ratRate.integer = uint64_t(rate);
ratRate.den = uint64_t(1 << 14); //arbitrary denominator -- should be big enough
ratRate.num = uint64_t(rate - ratRate.integer) * ratRate.den;
//stash the approximate hardware time so it can be restored
const long long timeNow = this->getHardwareTime();
int ret = bladerf_set_rational_sample_rate(_dev, _toch(direction, channel), &ratRate, NULL);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_rational_sample_rate(%f) returned %s", rate, _err2str(ret).c_str());
throw std::runtime_error("setSampleRate() " + _err2str(ret));
}
//stash the actual rate
const double actual = this->getSampleRate(direction, channel);
if (direction == SOAPY_SDR_RX)
{
_rxSampRate = actual;
this->updateRxMinTimeoutMs();
}
if (direction == SOAPY_SDR_TX)
{
_txSampRate = actual;
}
//restore the previous hardware time setting (after rate stash)
this->setHardwareTime(timeNow);
SoapySDR::logf(SOAPY_SDR_INFO, "setSampleRate(%s, %d, %f MHz), actual = %f MHz", direction==SOAPY_SDR_RX?"Rx":"Tx", int(channel), rate/1e6, actual/1e6);
}
double bladeRF_SoapySDR::getSampleRate(const int direction, const size_t channel) const
{
bladerf_rational_rate ratRate;
int ret = bladerf_get_rational_sample_rate(_dev, _toch(direction, channel), &ratRate);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rational_sample_rate() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getSampleRate() " + _err2str(ret));
}
return double(ratRate.integer) + (double(ratRate.num)/double(ratRate.den));
}
SoapySDR::RangeList bladeRF_SoapySDR::getSampleRateRange(const int direction, const size_t channel) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_sample_rate_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_sample_rate_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getSampleRateRange() " + _err2str(ret));
}
//create useful ranges based on the overall range
//these values were suggested by the authors in the gr-osmosdr plugin for bladerf
const auto overallRange = toRange(range);
SoapySDR::RangeList ranges;
ranges.emplace_back(overallRange.minimum()/1.0, overallRange.maximum()/4.0, overallRange.maximum()/16.0);
ranges.emplace_back(overallRange.maximum()/4.0, overallRange.maximum()/2.0, overallRange.maximum()/8.0);
ranges.emplace_back(overallRange.maximum()/2.0, overallRange.maximum()/1.0, overallRange.maximum()/4.0);
return ranges;
}
std::vector<double> bladeRF_SoapySDR::listSampleRates(const int direction, const size_t channel) const
{
//deprecated list of sample rates, just iterate though the ranges and build a list
std::vector<double> rates;
for (const auto &range : this->getSampleRateRange(direction, channel))
{
for (double rate = range.minimum(); rate <= range.maximum(); rate += range.step())
{
rates.push_back(rate);
}
}
return rates;
}
/*******************************************************************
* Bandwidth API
******************************************************************/
void bladeRF_SoapySDR::setBandwidth(const int direction, const size_t channel, const double bw)
{
//bypass the filter when sufficiently large BW is selected
if (bw > this->getBandwidthRange(direction, channel).back().maximum())
{
bladerf_set_lpf_mode(_dev, _toch(direction, channel), BLADERF_LPF_BYPASSED);
return;
}
//otherwise set to normal and configure the filter bandwidth
bladerf_set_lpf_mode(_dev, _toch(direction, channel), BLADERF_LPF_NORMAL);
int ret = bladerf_set_bandwidth(_dev, _toch(direction, channel), bladerf_bandwidth(std::round(bw)), NULL);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_bandwidth(%f) returned %s", bw, _err2str(ret).c_str());
throw std::runtime_error("setBandwidth() " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getBandwidth(const int direction, const size_t channel) const
{
bladerf_bandwidth bw(0);
int ret = bladerf_get_bandwidth(_dev, _toch(direction, channel), &bw);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_bandwidth() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getBandwidth() " + _err2str(ret));
}
return double(bw);
}
SoapySDR::RangeList bladeRF_SoapySDR::getBandwidthRange(const int direction, const size_t channel) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_bandwidth_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_bandwidth_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getBandwidthRange() " + _err2str(ret));
}
return {toRange(range)};
}
std::vector<double> bladeRF_SoapySDR::listBandwidths(const int direction, const size_t channel) const
{
//this is a deprecated call, it should be removed in the future
//for bladerfv2, return a simple 2 element list based on the available range
if (this->getNumChannels(direction) == 2)
{
const auto ranges = this->getBandwidthRange(direction, channel);
return {ranges.front().minimum(), ranges.back().maximum()};
}
//for bladerfv1 these were the chosen bw options
//but the authors removed it in gr-osmosdr
//so thats why its not present in the ranges API
std::vector<double> options = {0.75, 0.875, 1.25, 1.375, 1.5, 1.92, 2.5, 2.75, 3, 3.5, 4.375, 5, 6, 7, 10, 14};
for (auto &option : options) option *= 2e6;
return options;
}
/*******************************************************************
* Clocking API
******************************************************************/
void bladeRF_SoapySDR::setMasterClockRate(const double rate)
{
if (! _isBladeRF2) return;
int ret = bladerf_set_pll_refclk(_dev, uint64_t(rate));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_pll_refclk() returned %s", _err2str(ret).c_str());
throw std::runtime_error("setMasterClockRate() " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getMasterClockRate(void) const
{
if (! _isBladeRF2) return 0;
uint64_t rate(0);
int ret = bladerf_get_pll_refclk(_dev, &rate);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_pll_refclk() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getMasterClockRate() " + _err2str(ret));
}
return double(rate);
}
SoapySDR::RangeList bladeRF_SoapySDR::getMasterClockRates(void) const
{
if (! _isBladeRF2) return SoapySDR::RangeList();
const bladerf_range* range(nullptr);
int ret = bladerf_get_pll_refclk_range(_dev, &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_pll_refclk_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getMasterClockRates() " + _err2str(ret));
}
return {toRange(range)};
}
std::vector<std::string> bladeRF_SoapySDR::listClockSources(void) const
{
std::vector<std::string> clocks;
clocks.push_back("internal");
if (_isBladeRF2) clocks.push_back("ref_in");
return clocks;
}
void bladeRF_SoapySDR::setClockSource(const std::string &source)
{
if (! _isBladeRF2) return;
bool enable = (source == "ref_in");
int ret = bladerf_set_pll_enable(_dev, enable);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_pll_enable() returned %s", _err2str(ret).c_str());
throw std::runtime_error("setClockSource() " + _err2str(ret));
}
}
std::string bladeRF_SoapySDR::getClockSource(void) const
{
if (! _isBladeRF2) return "internal";
bool enabled(false);
int ret = bladerf_get_pll_enable(_dev, &enabled);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_pll_enable() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getClockSource() " + _err2str(ret));
}
if (enabled) return "ref_in";
else return "internal";
}
/*******************************************************************
* Time API
******************************************************************/
bool bladeRF_SoapySDR::hasHardwareTime(const std::string &what) const
{
if (not what.empty()) return SoapySDR::Device::hasHardwareTime(what);
return true;
}
long long bladeRF_SoapySDR::getHardwareTime(const std::string &what) const
{
if (not what.empty()) return SoapySDR::Device::getHardwareTime(what);
uint64_t ticksNow = 0;
const int ret = bladerf_get_timestamp(_dev, BLADERF_RX, &ticksNow);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_timestamp() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getHardwareTime() " + _err2str(ret));
}
return _rxTicksToTimeNs(ticksNow);
}
void bladeRF_SoapySDR::setHardwareTime(const long long timeNs, const std::string &what)
{
if (not what.empty()) return SoapySDR::Device::setHardwareTime(timeNs, what);
//reset the counters with GPIO and stash the offset
//this is the same as setting the time because
//we maintain the offset math within the driver
int ret = 0;
uint32_t original = 0;
ret |= bladerf_config_gpio_read(_dev, &original);
ret |= bladerf_config_gpio_write(_dev, original & ~(BLADERF_GPIO_TIMESTAMP));
ret |= bladerf_config_gpio_write(_dev, original | BLADERF_GPIO_TIMESTAMP);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_config_gpio_read/write() returned %s", _err2str(ret).c_str());
throw std::runtime_error("setHardwareTime() " + _err2str(ret));
}
_timeNsOffset = timeNs;
}
/*******************************************************************
* Sensor API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listSensors(void) const
{
std::vector<std::string> sensors;
if (_isBladeRF2) sensors.push_back("RFIC_TEMP");
return sensors;
}
SoapySDR::ArgInfo bladeRF_SoapySDR::getSensorInfo(const std::string &key) const
{
if (key == "RFIC_TEMP")
{
SoapySDR::ArgInfo info;
info.key = key;
info.value = "0";
info.name = "RFIC Temperature";
info.description = "Temperature in degrees C";
info.units = "C";
info.type = SoapySDR::ArgInfo::FLOAT;
return info;
}
else throw std::runtime_error("getSensorInfo(" + key + ") unknown sensor");
}
std::string bladeRF_SoapySDR::readSensor(const std::string &key) const
{
if (key == "RFIC_TEMP")
{
float val(0);
int ret = bladerf_get_rfic_temperature(_dev, &val);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rfic_temperature() returned %s", _err2str(ret).c_str());
throw std::runtime_error("readSensor("+key+") " + _err2str(ret));
}
return std::to_string(val);
}
else throw std::runtime_error("readSensor(" + key + ") unknown sensor");
}
std::vector<std::string> bladeRF_SoapySDR::listSensors(const int direction, const size_t channel) const
{
std::vector<std::string> sensors;
if (_isBladeRF2 and direction == SOAPY_SDR_RX) sensors.push_back("PRE_RSSI");
if (_isBladeRF2 and direction == SOAPY_SDR_RX) sensors.push_back("SYM_RSSI");
return sensors;
}
SoapySDR::ArgInfo bladeRF_SoapySDR::getSensorInfo(const int direction, const size_t, const std::string &key) const
{
if (key == "PRE_RSSI" and direction == SOAPY_SDR_RX)
{
SoapySDR::ArgInfo info;
info.key = key;
info.value = "0";
info.name = "Preamble RSSI";
info.description = "Preamble RSSI in dB (first calculated RSSI result)";
info.units = "dB";
info.type = SoapySDR::ArgInfo::FLOAT;
return info;
}
else if (key == "SYM_RSSI" and direction == SOAPY_SDR_RX)
{
SoapySDR::ArgInfo info;
info.key = key;
info.value = "0";
info.name = "Symbol RSSI";
info.description = "Symbol RSSI in dB (most recent RSSI result)";
info.units = "dB";
info.type = SoapySDR::ArgInfo::FLOAT;
return info;
}
else throw std::runtime_error("getSensorInfo(" + key + ") unknown sensor");
}
std::string bladeRF_SoapySDR::readSensor(const int direction, const size_t channel, const std::string &key) const
{
if (key == "PRE_RSSI" or key == "SYM_RSSI")
{
int32_t pre_rssi(0), sym_rssi(0);
int ret = bladerf_get_rfic_rssi(_dev, _toch(direction, channel), &pre_rssi, &sym_rssi);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rfic_rssi() returned %s", _err2str(ret).c_str());
throw std::runtime_error("readSensor("+key+") " + _err2str(ret));
}
return std::to_string((key[0] == 'P')?pre_rssi:sym_rssi);
}
else throw std::runtime_error("readSensor(" + key + ") unknown sensor");
}
/*******************************************************************
* Register API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listRegisterInterfaces(void) const
{
std::vector<std::string> ifaces;
if (_isBladeRF1) ifaces.push_back("LMS");
if (_isBladeRF2) ifaces.push_back("RFIC");
return ifaces;
}
void bladeRF_SoapySDR::writeRegister(const std::string &name, const unsigned addr, const unsigned value)
{
if (name == "LMS")
{
const int ret = bladerf_lms_write(_dev, uint8_t(addr), uint8_t(value));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_lms_write(0x%x) returned %s", addr, _err2str(ret).c_str());
throw std::runtime_error("writeRegister() " + _err2str(ret));
}
}
else if (name == "RFIC")
{
const int ret = bladerf_set_rfic_register(_dev, uint16_t(addr), uint8_t(value));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_rfic_register(0x%x) returned %s", addr, _err2str(ret).c_str());
throw std::runtime_error("writeRegister() " + _err2str(ret));
}
}
else throw std::runtime_error("writeRegister(" + name + ") unknown register interface");
}
unsigned bladeRF_SoapySDR::readRegister(const std::string &name, const unsigned addr) const
{
if (name == "LMS")
{
uint8_t value = 0;
const int ret = bladerf_lms_read(_dev, uint8_t(addr), &value);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_lms_read(0x%x) returned %s", addr, _err2str(ret).c_str());
throw std::runtime_error("readRegister() " + _err2str(ret));
}
return value;
}
if (name == "RFIC")
{
uint8_t value = 0;
const int ret = bladerf_get_rfic_register(_dev, uint16_t(addr), &value);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rfic_register(0x%x) returned %s", addr, _err2str(ret).c_str());
throw std::runtime_error("readRegister() " + _err2str(ret));
}
return value;
}
throw std::runtime_error("readRegister(" + name + ") unknown register interface");
}
/*******************************************************************
* Settings API
******************************************************************/
SoapySDR::ArgInfoList bladeRF_SoapySDR::getSettingInfo(void) const
{
SoapySDR::ArgInfoList setArgs;
// XB200 setting
SoapySDR::ArgInfo xb200SettingArg;
xb200SettingArg.key = "xb200";
xb200SettingArg.value = "disabled";
xb200SettingArg.name = "XB200 Transverter";
xb200SettingArg.description = "bladeRF XB200 Transverter Board";
xb200SettingArg.type = SoapySDR::ArgInfo::STRING;
xb200SettingArg.options.push_back("disabled");
xb200SettingArg.optionNames.push_back("Disabled");
xb200SettingArg.options.push_back("50M");
xb200SettingArg.optionNames.push_back("Filterbank: 50M");
xb200SettingArg.options.push_back("144M");
xb200SettingArg.optionNames.push_back("Filterbank: 144M");
xb200SettingArg.options.push_back("222M");
xb200SettingArg.optionNames.push_back("Filterbank: 222M");
xb200SettingArg.options.push_back("auto1db");
xb200SettingArg.optionNames.push_back("Filterbank: Auto (1dB)");
xb200SettingArg.options.push_back("auto3db");
xb200SettingArg.optionNames.push_back("Filterbank: Auto (3dB)");
xb200SettingArg.options.push_back("auto");
xb200SettingArg.optionNames.push_back("Filterbank: Auto");
xb200SettingArg.options.push_back("custom");
xb200SettingArg.optionNames.push_back("Filterbank: Custom");
if (_isBladeRF1) setArgs.push_back(xb200SettingArg);
// Sampling mode
SoapySDR::ArgInfo samplingModeArg;
samplingModeArg.key = "sampling_mode";
samplingModeArg.value = "internal";
samplingModeArg.name = "Sampling Mode";
samplingModeArg.description = "Internal = Via RX/TX connectors, External = Direct sampling from J60/J61 connectors";
samplingModeArg.type = SoapySDR::ArgInfo::STRING;
samplingModeArg.options.push_back("internal");
samplingModeArg.optionNames.push_back("Internal (Default)");
samplingModeArg.options.push_back("external");
samplingModeArg.optionNames.push_back("Direct Sampling");
if (_isBladeRF1) setArgs.push_back(samplingModeArg);
// Loopback
SoapySDR::ArgInfo lookbackArg;
lookbackArg.key = "loopback";
lookbackArg.name = "Loopback Mode";
lookbackArg.description = "Enable/disable internal loopback";
lookbackArg.type = SoapySDR::ArgInfo::STRING;
const bladerf_loopback_modes *modes(nullptr);
const int numModes = bladerf_get_loopback_modes(_dev, &modes);
if (modes and numModes > 0) for (int i = 0; i < numModes; i++)
{
if (modes[i].mode == BLADERF_LB_NONE) lookbackArg.value = modes[i].name;
lookbackArg.options.push_back(modes[i].name);
}
setArgs.push_back(lookbackArg);
// Device reset
SoapySDR::ArgInfo resetArg;
resetArg.key = "reset";
resetArg.value = "false";
resetArg.name = "Reset Device";
resetArg.description = "Reset the device, causing it to reload its firmware from flash.";
resetArg.type = SoapySDR::ArgInfo::BOOL;
resetArg.options.push_back("true");
resetArg.optionNames.push_back("True");
resetArg.options.push_back("false");
resetArg.optionNames.push_back("False");
setArgs.push_back(resetArg);
// Erase stored FPGA
SoapySDR::ArgInfo eraseArg;
eraseArg.key = "erase_stored_fpga";
eraseArg.value = "false";
eraseArg.name = "Erase the FPGA region of flash";
eraseArg.description = "Erase the FPGA region of SPI flash, effectively disabling FPGA autoloading.";
eraseArg.type = SoapySDR::ArgInfo::BOOL;
eraseArg.options.push_back("true");
eraseArg.optionNames.push_back("True");
eraseArg.options.push_back("false");
eraseArg.optionNames.push_back("False");
setArgs.push_back(eraseArg);
// Flash firmware
SoapySDR::ArgInfo firmwareArg;
firmwareArg.key = "flash_firmware";
firmwareArg.value = "";
firmwareArg.name = "Write FX3 firmware to flash";
firmwareArg.description = "Write FX3 firmware to the bladeRF's SPI flash from the provided file path. This will require a power cycle to take effect.";
firmwareArg.type = SoapySDR::ArgInfo::STRING;
setArgs.push_back(firmwareArg);
// Flash FPGA
SoapySDR::ArgInfo flashArg;
flashArg.key = "flash_fpga";
flashArg.value = "";
flashArg.name = "Write to the FPGA region of flash";
flashArg.description = "Write FPGA image to the bladeRF's SPI flash from the provided file path and enable FPGA loading from SPI flash at power on.";
flashArg.type = SoapySDR::ArgInfo::STRING;
setArgs.push_back(flashArg);
// Jump to bootloader
SoapySDR::ArgInfo bootloaderArg;
bootloaderArg.key = "jump_to_bootloader";
bootloaderArg.value = "false";
bootloaderArg.name = "Clear out a firmware signature word in flash and jump to FX3 bootloader";
bootloaderArg.description = "The device will continue to boot into the FX3 bootloader across power cycles until new firmware is written to the device.";
bootloaderArg.type = SoapySDR::ArgInfo::BOOL;
bootloaderArg.options.push_back("true");
bootloaderArg.optionNames.push_back("True");
bootloaderArg.options.push_back("false");
bootloaderArg.optionNames.push_back("False");
setArgs.push_back(bootloaderArg);
// Load FPGA
SoapySDR::ArgInfo loadArg;
loadArg.key = "load_fpga";
loadArg.value = "";
loadArg.name = "Load device's FPGA";
loadArg.description = "Load device's FPGA from the provided file path. Note that this FPGA configuration will be reset at the next power cycle.";
loadArg.type = SoapySDR::ArgInfo::STRING;
setArgs.push_back(loadArg);
// BiasTee TX
SoapySDR::ArgInfo biasTeeTx;
biasTeeTx.key = "biastee_tx";
biasTeeTx.value = "false";
biasTeeTx.name = "Enable bias tee for tx";
biasTeeTx.description = "Enables the bias tee on the tx channel 0";
biasTeeTx.type = SoapySDR::ArgInfo::BOOL;
biasTeeTx.options.push_back("true");
biasTeeTx.optionNames.push_back("True");
biasTeeTx.options.push_back("false");
biasTeeTx.optionNames.push_back("False");
setArgs.push_back(biasTeeTx);
// BiasTee RX
SoapySDR::ArgInfo biasTeeRx;
biasTeeRx.key = "biastee_rx";
biasTeeRx.value = "false";
biasTeeRx.name = "Enable bias tee for rx";
biasTeeRx.description = "Enables the bias tee on the rx channel 0";
biasTeeRx.type = SoapySDR::ArgInfo::BOOL;
biasTeeRx.options.push_back("true");
biasTeeRx.optionNames.push_back("True");
biasTeeRx.options.push_back("false");
biasTeeRx.optionNames.push_back("False");
setArgs.push_back(biasTeeRx);
return setArgs;
}
std::string bladeRF_SoapySDR::readSetting(const std::string &key) const
{
if (key == "xb200") {
return _xb200Mode;
} else if (key == "sampling_mode") {
return _samplingMode;
} else if (key == "loopback") {
bladerf_loopback lb;
bladerf_get_loopback(_dev, &lb);
const bladerf_loopback_modes *modes(nullptr);
const int numModes = bladerf_get_loopback_modes(_dev, &modes);
if (modes and numModes > 0) for (int i = 0; i < numModes; i++)
{
if (modes[i].mode == lb) return modes[i].name;
}
return "unknown";
} else if (key == "reset") {
return "false";
} else if (key == "erase_stored_fpga") {
return "false";
} else if (key == "flash_firmware") {
return "";
} else if (key == "flash_fpga") {
return "";
} else if (key == "jump_to_bootloader") {
return "false";
} else if (key == "load_fpga") {
return "";
} else if (key == "biastee_tx") {
return "false";
} else if (key == "biastee_rx") {
return "false";
}
SoapySDR_logf(SOAPY_SDR_WARNING, "Unknown setting '%s'", key.c_str());
return "";
}
void bladeRF_SoapySDR::writeSetting(const std::string &key, const std::string &value)
{
if (key == "xb200")
{
// Verify that a valid setting has arrived
std::vector<std::string> xb200_validSettings{ "disabled", "50M", "144M", "222M", "auto1db", "auto3db", "auto", "custom" };
if (std::find(std::begin(xb200_validSettings), std::end(xb200_validSettings), value) != std::end(xb200_validSettings))
{
// --> Valid setting has arrived
_xb200Mode = value;
// Get attached expansion device
bladerf_xb _bladerf_xb_attached = bladerf_xb::BLADERF_XB_NONE;
bladerf_expansion_get_attached(_dev, &_bladerf_xb_attached);
// If "disabled," ensure board is bypassed, if present, and return
if (value == "disabled")
{
if (_bladerf_xb_attached == bladerf_xb::BLADERF_XB_200)
{
// Apply bypass around connected XB200
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: Disabling connected XB200 by bypassing signal path");
bladerf_xb200_set_path(_dev, BLADERF_CHANNEL_RX(0), bladerf_xb200_path::BLADERF_XB200_BYPASS);
}
return;
}
// Attach the XB200, if it isn't already attached
if (_bladerf_xb_attached == bladerf_xb::BLADERF_XB_NONE)
{
if (bladerf_expansion_attach(_dev, bladerf_xb::BLADERF_XB_200))
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Could not attach to XB200");
return;
}
}
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: XB200 is attached");
// Which filterbank was selected?
bladerf_xb200_filter filter = bladerf_xb200_filter::BLADERF_XB200_AUTO_1DB;
if (value == "50M")
{
// 50-54 MHz (6 meter band) filterbank
filter = bladerf_xb200_filter::BLADERF_XB200_50M;
}
else if (value == "144M")
{
// 144-148 MHz (2 meter band) filterbank
filter = bladerf_xb200_filter::BLADERF_XB200_144M;
}
else if (value == "222M")
{
// 222-225 MHz (1.25 meter band) filterbank
// Note that this filter option is technically wider, covering 206-235 MHz
filter = bladerf_xb200_filter::BLADERF_XB200_222M;
}
else if (value == "auto1db")
{
// The other filter options are automatically selected depending on the RX or TX
// module's current frequency, based upon the 1dB points of the on-board filters
// For frequencies outside the range of the on-board filters, the custom path is used
filter = bladerf_xb200_filter::BLADERF_XB200_AUTO_1DB;
}
else if (value == "auto3db")
{
// The other filter options are automatically selected depending on the RX or TX
// module's current frequency, based upon the 3dB points of the on-board filters
// For frequencies outside the range of the on-board filters, the custom path is used
filter = bladerf_xb200_filter::BLADERF_XB200_AUTO_3DB;
}
else if (value == "custom")
{
// The custom filter bank path across the FILT and FILT-ANT SMA connectors
filter = bladerf_xb200_filter::BLADERF_XB200_CUSTOM;
}
else
{
// Default: Auto, 1dB points
// The other filter options are automatically selected depending on the RX or TX
// module's current frequency, based upon the 1dB points of the on-board filters
// For frequencies outside the range of the on-board filters, the custom path is used
filter = bladerf_xb200_filter::BLADERF_XB200_AUTO_1DB;
}
// Set the filterbank
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: Set XB200 filterbank '%s'", value.c_str());
int ret = bladerf_xb200_set_filterbank(_dev, BLADERF_CHANNEL_RX(0), filter);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_xb200_set_filterbank(%s) returned %s", value.c_str(), _err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
// Check signal path
bladerf_xb200_path _bladerf_xb200_path = bladerf_xb200_path::BLADERF_XB200_MIX;
bladerf_xb200_get_path(_dev, BLADERF_CHANNEL_RX(0), &_bladerf_xb200_path);
if (_bladerf_xb200_path != bladerf_xb200_path::BLADERF_XB200_MIX)
{
// Apply mix path through XB200
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: Adjusting mix path through XB200");
bladerf_xb200_set_path(_dev, BLADERF_CHANNEL_RX(0), bladerf_xb200_path::BLADERF_XB200_MIX);
}
}
else
{
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Invalid XB200 setting '%s'", value.c_str());
//throw std::runtime_error("writeSetting(" + key + "," + value + ") unknown value");
}
}
else if (key == "sampling_mode")
{
/* Configure the sampling of the LMS6002D to be either internal or external.
** Internal sampling will read from the RXVGA2 driver internal to the chip.
** External sampling will connect the ADC inputs to the external inputs for direct sampling.
*/
// Verify that a valid setting has arrived
std::vector<std::string> sampling_mode_validSettings{ "internal", "external" };
if (std::find(std::begin(sampling_mode_validSettings), std::end(sampling_mode_validSettings), value) != std::end(sampling_mode_validSettings))
{
// --> Valid setting has arrived
_samplingMode = value;
// Set the sampling mode
int ret = 0;
if (value == "external")
{
// External/direct sampling
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: Set sampling mode to direct/external sampling", value.c_str());
ret = bladerf_set_sampling(_dev, bladerf_sampling::BLADERF_SAMPLING_EXTERNAL);
}
else
{
// Default: Internal
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: Set sampling mode to internal sampling", value.c_str());
ret = bladerf_set_sampling(_dev, bladerf_sampling::BLADERF_SAMPLING_INTERNAL);
}
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_sampling(%s) returned %s", value.c_str(), _err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
else
{
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Invalid sampling mode '%s'", value.c_str());
//throw std::runtime_error("writeSetting(" + key + "," + value + ") unknown value");
}
}
else if (key == "loopback")
{
bladerf_loopback loopback(BLADERF_LB_NONE);
const bladerf_loopback_modes *modes(nullptr);
const int numModes = bladerf_get_loopback_modes(_dev, &modes);
if (modes and numModes > 0) for (int i = 0; i < numModes; i++)
{
if (modes[i].name == value) loopback = modes[i].mode;
}
if (bladerf_is_loopback_mode_supported(_dev, loopback))
{
// If the loopback isn't already set, set the loopback
bladerf_loopback _bladerf_loopback = bladerf_loopback::BLADERF_LB_NONE;
bladerf_get_loopback(_dev, &_bladerf_loopback);
if (_bladerf_loopback != loopback)
{
SoapySDR::logf(SOAPY_SDR_INFO, "bladeRF: Loopback set '%s'", value.c_str());
int ret = bladerf_set_loopback(_dev, loopback);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_loopback(%s) returned %s", value.c_str(), _err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
}
else
{
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Invalid loopback setting '%s'", value.c_str());
//throw std::runtime_error("writeSetting(" + key + "," + value + ") unknown value");
}
}
else if (key == "reset")
{
// Verify that a valid setting has arrived
if (value == "true") {
// --> Valid setting has arrived
int ret = bladerf_device_reset(_dev);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_device_reset(%s) returned %s", value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
/*else {
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Invalid reset setting '%s'", value.c_str());
}*/
}
else if (key == "erase_stored_fpga")
{
// Verify that a valid setting has arrived
if (value == "true") {
// --> Valid setting has arrived
int ret = bladerf_erase_stored_fpga(_dev);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_erase_stored_fpga(%s) returned %s", value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
/*else {
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Invalid erase setting '%s'", value.c_str());
}*/
}
else if (key == "flash_firmware")
{
if (!value.empty()) {
int ret = bladerf_flash_firmware(_dev, value.c_str());
if (ret != 0) {
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_flash_firmware(%s) returned %s", value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
/*else {
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: The provided firmware file path is empty");
}*/
}
else if (key == "flash_fpga")
{
if (!value.empty()) {
int ret = bladerf_flash_fpga(_dev, value.c_str());
if (ret != 0) {
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_flash_fpga(%s) returned %s", value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
/*else {
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: The provided FPGA image path is empty");
}*/
}
else if (key == "jump_to_bootloader")
{
// Verify that a valid setting has arrived
if (value == "true") {
// --> Valid setting has arrived
int ret = bladerf_jump_to_bootloader(_dev);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_jump_to_bootloader(%s) returned %s", value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
/*else {
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: Invalid jump to bootloader setting '%s'", value.c_str());
}*/
}
else if (key == "load_fpga")
{
if (!value.empty()) {
int ret = bladerf_load_fpga(_dev, value.c_str());
if (ret != 0) {
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_load_fpga(%s) returned %s", value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
/*else {
// --> Invalid setting has arrived
SoapySDR::logf(SOAPY_SDR_ERROR, "bladeRF: The provided FPGA image path is empty");
}*/
}
else if (key == "biastee_tx")
{
if (value == "true" || value == "false") {
// --> Valid setting has arrived
int ret = bladerf_set_bias_tee(_dev, BLADERF_CHANNEL_TX(0), value == "true");
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_bias_tee(BLADERF_CHANNEL_TX(0), %s) returned %s",
value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
}
else if (key == "biastee_rx")
{
if (value == "true" || value == "false") {
// --> Valid setting has arrived
int ret = bladerf_set_bias_tee(_dev, BLADERF_CHANNEL_RX(0), value == "true");
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_bias_tee(BLADERF_CHANNEL_RX(0), %s) returned %s",
value.c_str(),
_err2str(ret).c_str());
throw std::runtime_error("writeSetting() " + _err2str(ret));
}
}
}
else
{
throw std::runtime_error("writeSetting(" + key + ") unknown setting");
}
}
/*******************************************************************
* GPIO API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listGPIOBanks(void) const
{
std::vector<std::string> banks;
banks.push_back("CONFIG");
banks.push_back("EXPANSION");
return banks;
}
void bladeRF_SoapySDR::writeGPIO(const std::string &bank, const unsigned value)
{
int ret = 0;
if (bank == "CONFIG")
{
ret = bladerf_config_gpio_write(_dev, value);
}
else if (bank == "EXPANSION")
{
ret = bladerf_expansion_gpio_write(_dev, value);
}
else throw std::runtime_error("writeGPIO("+bank+") unknown bank name");
if (ret != 0) throw std::runtime_error("writeGPIO("+bank+") " + _err2str(ret));
}
void bladeRF_SoapySDR::writeGPIO(const std::string &bank, const unsigned value, const unsigned mask)
{
if (bank == "EXPANSION")
{
int ret = bladerf_expansion_gpio_masked_write(_dev, mask, value);
if (ret != 0) throw std::runtime_error("writeGPIODir("+bank+") " + _err2str(ret));
return;
}
return SoapySDR::Device::writeGPIO(bank, value, mask);
}
unsigned bladeRF_SoapySDR::readGPIO(const std::string &bank) const
{
uint32_t value = 0;
int ret = 0;
if (bank == "CONFIG")
{
ret = bladerf_config_gpio_read(_dev, &value);
}
else if (bank == "EXPANSION")
{
ret = bladerf_expansion_gpio_read(_dev, &value);
}
else throw std::runtime_error("readGPIO("+bank+") unknown bank name");
if (ret != 0) throw std::runtime_error("readGPIO("+bank+") " + _err2str(ret));
return value;
}
void bladeRF_SoapySDR::writeGPIODir(const std::string &bank, const unsigned dir)
{
int ret = 0;
if (bank == "CONFIG")
{
throw std::runtime_error("data direction not configurable for CONFIG bank");
}
else if (bank == "EXPANSION")
{
ret = bladerf_expansion_gpio_dir_write(_dev, dir);
}
else throw std::runtime_error("writeGPIODir("+bank+") unknown bank name");
if (ret != 0) throw std::runtime_error("writeGPIODir("+bank+") " + _err2str(ret));
}
void bladeRF_SoapySDR::writeGPIODir(const std::string &bank, const unsigned dir, const unsigned mask)
{
if (bank == "EXPANSION")
{
int ret = bladerf_expansion_gpio_dir_masked_write(_dev, mask, dir);
if (ret != 0) throw std::runtime_error("writeGPIODir("+bank+") " + _err2str(ret));
return;
}
return SoapySDR::Device::writeGPIODir(bank, dir, mask);
}
unsigned bladeRF_SoapySDR::readGPIODir(const std::string &bank) const
{
uint32_t value = 0;
int ret = 0;
if (bank == "CONFIG")
{
throw std::runtime_error("data direction not configurable for CONFIG bank");
}
else if (bank == "EXPANSION")
{
ret = bladerf_expansion_gpio_dir_read(_dev, &value);
}
else throw std::runtime_error("readGPIODir("+bank+") unknown bank name");
if (ret != 0) throw std::runtime_error("readGPIODir("+bank+") " + _err2str(ret));
return value;
}
|