//! This gets called whenever a parameter is reconfigured
void BladeRfTxComponent::parameterHasChanged(std::string name)
{
  try
  {
    if(name == "frequency") {
        // Set center frequency
        uint32_t actualValue;
        int ret = bladerf_set_frequency(device_, BLADERF_MODULE_TX, frequency_x);
        if (ret != 0) {
            throw IrisException("Failed to set center frequency!");
        }
        bladerf_get_frequency(device_, BLADERF_MODULE_TX, &actualValue);
        LOG(LINFO) << "Actual Tx center frequency is: " << actualValue << " Hz";
    } else if(name == "rate") {
        uint32_t actualValue;
        int ret = bladerf_set_sample_rate(device_, BLADERF_MODULE_TX, (uint32_t)rate_x, &actualValue);
        if (ret != 0) {
            throw IrisException("Failed to set sample rate!");
        }
        LOG(LINFO) << "Actual Tx sample rate is: " << actualValue << " Hz";
    } else if(name == "vga1gain") {
        if (bladerf_set_txvga1(device_, vga1Gain_x) != 0) {
            throw IrisException("Failed to set VGA1 gain!");
        }
        int actualGain;
        bladerf_get_txvga1(device_, &actualGain);
        LOG(LINFO) << "Actual VGA1 gain is " << actualGain << " dB";
    } else if(name == "vga2gain") {
        if (bladerf_set_txvga2(device_, vga2Gain_x) != 0) {
            throw IrisException("Failed to set VGA2 gain!");
        }
        int actualGain;
        bladerf_get_txvga2(device_, &actualGain);
        LOG(LINFO) << "Actual VGA2 gain is " << actualGain << " dB";
    }
  }
  catch(std::exception &e)
  {
    throw IrisException(e.what());
  }
}
//! Do any initialization required
void BladeRfTxComponent::initialize()
{
    // Set up the input DataBuffer
    inBuf_ = castToType< complex<float> >(inputBuffers.at(0));

    // Initialize raw sample vector to some multiple of block size
    rawSampleBuffer_.data.resize(128 * BLADERF_SAMPLE_BLOCK_SIZE);

    // Set up the BladeRF
    try
    {
        // Create the device
        LOG(LINFO) << "Trying to open device " << deviceName_x;
        int ret = bladerf_open(&device_, deviceName_x.c_str());
        if (ret != 0) {
            throw IrisException("Failed to open bladeRF device!");
        }

        // Check whether FPGA is configured yet
        if (bladerf_is_fpga_configured(device_) != 1 ) {
            // try to load FPGA image
            if (not fpgaImage_x.empty()) {
                ret = bladerf_load_fpga(device_, fpgaImage_x.c_str());
                if (ret != 0) {
                    throw IrisException("Failed to load FPGA to bladeRF!");
                } else {
                    LOG(LINFO) << "FPGA image successfully loaded.";
                }
            } else {
                throw IrisException("BladeRF FPGA is not configured and no FPGA image given!");
            }
        }

        // Print some information about device
        struct bladerf_version version;
        if (bladerf_fw_version(device_, &version) == 0) {
            LOG(LINFO) << "Using FW " << version.describe;
        }
        if (bladerf_fpga_version(device_, &version) == 0) {
            LOG(LINFO) << "Using FPGA " << version.describe;
        }
        if (bladerf_is_fpga_configured(device_) != 1 ) {
            throw IrisException("BladeRF FPGA is not configured!");
        }

        // setting up sync config
        ret = bladerf_sync_config(device_,
                                  BLADERF_MODULE_TX,
                                  BLADERF_FORMAT_SC16_Q11,
                                  BLADERF_DEFAULT_STREAM_BUFFERS,
                                  BLADERF_DEFAULT_STREAM_SAMPLES,
                                  BLADERF_DEFAULT_STREAM_XFERS,
                                  BLADERF_SYNC_TIMEOUT_MS);
        if (ret != 0) {
            throw IrisException("Couldn't enable BladeRF Tx sync handle!");
            LOG(LERROR) << bladerf_strerror(ret);
        }

        // Turn on transmitter
        ret = bladerf_enable_module(device_, BLADERF_MODULE_TX, true);
        if ( ret != 0 ) {
            throw IrisException("Couldn't enable BladeRF Tx module!");
        }

        // Set sample rate
        uint32_t actualValue;
        ret = bladerf_set_sample_rate(device_, BLADERF_MODULE_TX, (uint32_t)rate_x, &actualValue);
        if (ret != 0) {
            throw IrisException("Failed to set sample rate!");
        }
        LOG(LINFO) << "Actual Tx sample rate is: " << actualValue << " Hz";

        // Set center frequency
        ret = bladerf_set_frequency(device_, BLADERF_MODULE_TX, frequency_x);
        if (ret != 0) {
            throw IrisException("Failed to set center frequency!");
        }
        bladerf_get_frequency(device_, BLADERF_MODULE_TX, &actualValue);
        LOG(LINFO) << "Actual Tx center frequency is: " << actualValue << " Hz";

        // Set bandwidth
        ret = bladerf_set_bandwidth(device_, BLADERF_MODULE_TX, bw_x, &actualValue);
        if (ret != 0) {
            throw IrisException("Failed to set receive bandwidth!");
        }
        LOG(LINFO) << "Actual Tx bandwidth is " << actualValue << " Hz";

        // Set VGA1 gain
        int actualGain;
        ret = bladerf_set_txvga1(device_, vga1Gain_x);
        if (ret != 0) {
            throw IrisException("Failed to set VGA1 gain!");
        }
        bladerf_get_txvga1(device_, &actualGain);
        LOG(LINFO) << "Actual VGA1 gain is " << actualGain << " dB";

        // Set VGA2 gain
        ret = bladerf_set_txvga2(device_, vga2Gain_x);
        if (ret != 0) {
            throw IrisException("Failed to set VGA2 gain!");
        }
        bladerf_get_txvga2(device_, &actualGain);
        LOG(LINFO) << "Actual VGA2 gain is " << actualGain << " dB";
    }
    catch(const boost::exception &e)
    {
        throw IrisException(boost::diagnostic_information(e));
    }
    catch(std::exception& e)
    {
        throw IrisException(e.what());
    }
}
Esempio n. 3
0
static int init_device(struct repeater *repeater, struct repeater_config *config)
{
    int status = 0;
    unsigned int actual_value;

    status = bladerf_open(&repeater->device, config->device_str);
    if (!repeater->device) {
        fprintf(stderr, "Failed to open %s: %s\r\n", config->device_str,
                bladerf_strerror(status));
        return -1;
    }

    status = bladerf_is_fpga_configured(repeater->device);
    if (status < 0) {
        fprintf(stderr, "Failed to determine if FPGA is loaded: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else if (status == 0) {
        fprintf(stderr, "FPGA is not loaded. Aborting.\r\n");
        status = BLADERF_ERR_NODEV;
        goto init_device_error;
    }

    status = bladerf_set_bandwidth(repeater->device, BLADERF_MODULE_TX,
                                    config->bandwidth, &actual_value);

    if (status < 0) {
        fprintf(stderr, "Failed to set TX bandwidth: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual TX bandwidth: %d Hz\r\n", actual_value);
    }

    status = bladerf_set_bandwidth(repeater->device, BLADERF_MODULE_RX,
                                    config->bandwidth, &actual_value);

    if (status < 0) {
        fprintf(stderr, "Failed to set RX bandwidth: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual RX bandwidth: %d Hz\r\n", actual_value);
    }

    status = bladerf_set_sample_rate(repeater->device, BLADERF_MODULE_TX,
                                     config->sample_rate, &actual_value);

    if (status < 0) {
        fprintf(stderr, "Failed to set TX sample rate: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual TX sample rate is %d Hz\r\n", actual_value);
    }

    status = bladerf_set_sample_rate(repeater->device, BLADERF_MODULE_RX,
                                     config->sample_rate, &actual_value);

    if (status < 0) {
        fprintf(stderr, "Failed to set RX sample rate: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual RX sample rate is %d Hz\r\n", actual_value);
    }

    status = bladerf_set_frequency(repeater->device,
                                   BLADERF_MODULE_TX, config->tx_freq);
    if (status < 0) {
        fprintf(stderr, "Failed to set TX frequency: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Set TX frequency to %d Hz\r\n", config->tx_freq);
    }

    status = bladerf_set_frequency(repeater->device,
                                   BLADERF_MODULE_RX, config->rx_freq);
    if (status < 0) {
        fprintf(stderr, "Failed to set RX frequency: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Set RX frequency to %d Hz\r\n", config->rx_freq);
    }

    status = bladerf_enable_module(repeater->device, BLADERF_MODULE_RX, true);
    if (status < 0) {
        fprintf(stderr, "Failed to enable RX module: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Enabled RX module\r\n");
    }

    status = bladerf_enable_module(repeater->device, BLADERF_MODULE_TX, true);
    if (status < 0) {
        bladerf_enable_module(repeater->device, BLADERF_MODULE_RX, false);
        fprintf(stderr, "Failed to enable TX module: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Enabled TX module\r\n");
    }

    status = bladerf_get_txvga1(repeater->device, &repeater->gain_txvga1);
    if (status < 0) {
        fprintf(stderr, "Failed to get TXVGA1 gain: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("TXVGA1 gain is: %d\r\n", repeater->gain_txvga1);
    }

    status = bladerf_get_txvga2(repeater->device, &repeater->gain_txvga2);
    if (status < 0) {
        fprintf(stderr, "Failed to get TXVGA2 gain: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("TXVGA2 gain is: %d\r\n", repeater->gain_txvga2);
    }

    status = bladerf_get_rxvga1(repeater->device, &repeater->gain_rxvga1);
    if (status < 0) {
        fprintf(stderr, "Failed to get RXVGA1 gain: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("RXVGA1 gain is: %d\r\n", repeater->gain_rxvga1);
    }

    status = bladerf_get_rxvga2(repeater->device, &repeater->gain_rxvga2);
    if (status < 0) {
        fprintf(stderr, "Failed to get RXVGA2 gain: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("RXVGA2 gain is: %d\r\n", repeater->gain_rxvga2);
    }

    status = bladerf_get_lna_gain(repeater->device, &repeater->gain_lna);
    if (status < 0) {
        fprintf(stderr, "Failed to get LNA gain: %s\r\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("LNA gain is: %d\r\n", repeater->gain_lna);
    }

    return status;

init_device_error:
    bladerf_close(repeater->device);
    repeater->device = NULL;

    return status;
}