示例#1
0
文件: info.c 项目: 2ephyr/bladeRF
int cmd_info(struct cli_state *state, int argc, char **argv)
{
    int status;
    bladerf_fpga_size fpga_size;
    uint16_t dac_trim;
    bool fpga_loaded;
    struct bladerf_devinfo info;
    bladerf_dev_speed usb_speed;
    const char *backend_str;

    status = bladerf_get_devinfo(state->dev, &info);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }

    backend_str = backend_description(info.backend);

    status = bladerf_is_fpga_configured(state->dev);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }
    fpga_loaded = status != 0;

    status = bladerf_get_fpga_size(state->dev, &fpga_size);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }

    status = bladerf_get_vctcxo_trim(state->dev, &dac_trim);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }

    usb_speed = bladerf_device_speed(state->dev);

    printf("\n");
    printf("  Serial #:                 %s\n", info.serial);
    printf("  VCTCXO DAC calibration:   0x%.4x\n", dac_trim);
    if (fpga_size != 0) {
        printf("  FPGA size:                %d KLE\n", fpga_size);
    } else {
        printf("  FPGA size:                Unknown\n");
    }
    printf("  FPGA loaded:              %s\n", fpga_loaded ? "yes" : "no");
    printf("  USB bus:                  %d\n", info.usb_bus);
    printf("  USB address:              %d\n", info.usb_addr);
    printf("  USB speed:                %s\n", devspeed2str(usb_speed));
    printf("  Backend:                  %s\n", backend_str);
    printf("  Instance:                 %d\n", info.instance);

    printf("\n");
    return 0;
}
示例#2
0
文件: version.c 项目: 2ephyr/bladeRF
int cmd_version(struct cli_state *state, int argc, char **argv)
{
    int status;

    struct bladerf_version fw_version, fpga_version, lib_version;
    bool fpga_loaded = false;

    bladerf_version(&lib_version);

    printf("\n");
    printf("  bladeRF-cli version:        " BLADERF_CLI_VERSION "\n");
    printf("  libbladeRF version:         %s\n", lib_version.describe);
    printf("\n");

    /* Exit cleanly if no device is attached */
    if (state->dev == NULL) {
        printf("  Device version information unavailable: No device attached.\n");
        return 0;
    }

    status = bladerf_is_fpga_configured(state->dev);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    } else if (status != 0) {
        fpga_loaded = true;
        status = bladerf_fpga_version(state->dev, &fpga_version);
        if (status < 0) {
            state->last_lib_error = status;
            return CLI_RET_LIBBLADERF;
        }
    }

    status = bladerf_fw_version(state->dev, &fw_version);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }


    printf("  Firmware version:           %s\n", fw_version.describe);

    if (fpga_loaded) {
        printf("  FPGA version:               %s\n", fpga_version.describe);
    } else {
        printf("  FPGA version:               Unknown (FPGA not loaded)\n");
    }

    printf("\n");
    return CLI_RET_OK;
}
示例#3
0
static int is_fpga_configured(struct cli_state *state, const char *cmd)
{
    int status;

    status = bladerf_is_fpga_configured(state->dev);
    if (status < 0) {
        cli_err(state, cmd, "Failed to determine if the FPGA is "
                            "configured. Is the FX3 programmed?");

        state->last_lib_error = status;
        status = CLI_RET_LIBBLADERF;
    }

    return status;
}
示例#4
0
文件: rxtx.c 项目: DanielG/bladeRF
int rxtx_cmd_start_check(struct cli_state *s, struct rxtx_data *rxtx,
                         const char *argv0)
{
    int status = CMD_RET_UNKNOWN;
    int fpga_status;
    bool have_file;

    if (!cli_device_is_opened(s)) {
        return CMD_RET_NODEV;
    } else if (rxtx_get_state(rxtx) != RXTX_STATE_IDLE) {
        return CMD_RET_STATE;
    } else {
        fpga_status = bladerf_is_fpga_configured(s->dev);
        if (fpga_status < 0) {
            s->last_lib_error = fpga_status;
            status = CMD_RET_LIBBLADERF;
        } else if (fpga_status != 1) {
            status = CMD_RET_NOFPGA;
        } else {
            pthread_mutex_lock(&rxtx->file_mgmt.file_meta_lock);
            have_file = (rxtx->file_mgmt.path != NULL);
            pthread_mutex_unlock(&rxtx->file_mgmt.file_meta_lock);

            if (!have_file) {
                cli_err(s, argv0, "File not configured");
                status = CMD_RET_INVPARAM;
            } else {
                status = validate_stream_params(s, rxtx, argv0);
            }

            if (status == 0) {
                check_samplerate(s, rxtx);
            }
        }
    }

    return status;
}
示例#5
0
文件: bladerf.c 项目: kaysin/bladeRF
int bladerf_open_with_devinfo(struct bladerf **opened_device,
                                struct bladerf_devinfo *devinfo)
{
    struct bladerf *dev;
    int status;

    *opened_device = NULL;

    dev = (struct bladerf *)calloc(1, sizeof(struct bladerf));
    if (dev == NULL) {
        return BLADERF_ERR_MEM;
    }

    dev->fpga_version.describe = calloc(1, BLADERF_VERSION_STR_MAX + 1);
    if (dev->fpga_version.describe == NULL) {
        free(dev);
        return BLADERF_ERR_MEM;
    }

    dev->fw_version.describe = calloc(1, BLADERF_VERSION_STR_MAX + 1);
    if (dev->fw_version.describe == NULL) {
        free((void*)dev->fpga_version.describe);
        free(dev);
        return BLADERF_ERR_MEM;
    }

    status = backend_open(dev, devinfo);
    if (status != 0) {
        free((void*)dev->fw_version.describe);
        free((void*)dev->fpga_version.describe);
        free(dev);
        return status;
    }

    status = dev->fn->get_device_speed(dev, &dev->usb_speed);
    if (status < 0) {
        log_debug("Failed to get device speed: %s\n",
                  bladerf_strerror(status));
        goto error;
    }

    if (dev->usb_speed != BLADERF_DEVICE_SPEED_HIGH &&
        dev->usb_speed != BLADERF_DEVICE_SPEED_SUPER) {
        log_debug("Unsupported device speed: %d\n", dev->usb_speed);
        goto error;
    }

    /* VCTCXO trim and FPGA size are non-fatal indicators that we've
     * trashed the calibration region of flash. If these were made fatal,
     * we wouldn't be able to open the device to restore them. */
    status = bladerf_get_and_cache_vctcxo_trim(dev);
    if (status < 0) {
        log_warning("Failed to get VCTCXO trim value: %s\n",
                    bladerf_strerror(status));
    }

    status = bladerf_get_and_cache_fpga_size(dev);
    if (status < 0) {
        log_warning("Failed to get FPGA size %s\n",
                    bladerf_strerror(status));
    }

    status = bladerf_is_fpga_configured(dev);
    if (status > 0) {
        status = bladerf_init_device(dev);
    }

error:
    if (status < 0) {
        bladerf_close(dev);
    } else {
        *opened_device = dev;
    }

    return status;
}
示例#6
0
文件: main.c 项目: adamgreig/bladeRF
int main(int argc, char *argv[])
{
    int status;
    unsigned int actual;
    struct bladerf *dev;
    struct bladerf_stream *stream;
    struct test_data test_data;
    bool conv_ok;

    if (argc != 4 && argc != 5) {
        fprintf(stderr,
                "Usage: %s [tx|rx] <samples per buffer> <# buffers> [# samples]\n", argv[0]);
        return EXIT_FAILURE;
    }

    if (strcasecmp(argv[1], "rx") == 0 ) {
        test_data.module = BLADERF_MODULE_RX ;
    } else if (strcasecmp(argv[1], "tx") == 0 ) {
        test_data.module = BLADERF_MODULE_TX;
    } else {
        fprintf(stderr, "Invalid module: %s\n", argv[1]);
        return EXIT_FAILURE;
    }

    test_data.idx = 0;
    test_data.fout = NULL;

    test_data.samples_per_buffer = str2int(argv[2], 1, INT_MAX, &conv_ok);
    if (!conv_ok) {
        fprintf(stderr, "Invalid samples per buffer value: %s\n", argv[2]);
        return EXIT_FAILURE;
    }

    test_data.num_buffers = str2int(argv[3], 1, INT_MAX, &conv_ok);
    if (!conv_ok) {
        fprintf(stderr, "Invalid # buffers: %s\n", argv[3]);
        return EXIT_FAILURE;
    }

    if(test_data.module == BLADERF_MODULE_RX && argc == 5) {
        test_data.samples_left = str2int(argv[4], 1, INT_MAX, &conv_ok);
        if(!conv_ok) {
            fprintf(stderr, "Invalid number of samples: %s\n", argv[4]);
            return EXIT_FAILURE;
        }
    }

    if (signal(SIGINT, handler) == SIG_ERR ||
        signal(SIGTERM, handler) == SIG_ERR) {
        fprintf(stderr, "Failed to set up signal handler\n");
        return EXIT_FAILURE;
    }

    status = bladerf_open(&dev, NULL);
    if (status < 0) {
        fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(status));
        return EXIT_FAILURE;
    }

    status = bladerf_is_fpga_configured(dev);
    if (status < 0) {
        fprintf(stderr, "Failed to determine FPGA state: %s\n",
                bladerf_strerror(status));
        return EXIT_FAILURE;
    } else if (status == 0) {
        fprintf(stderr, "Error: FPGA is not loaded.\n");
        bladerf_close(dev);
        return EXIT_FAILURE;
    }

    if (!status) {
        status = bladerf_set_frequency(dev, test_data.module, 1000000000);
        if (status < 0) {
            fprintf(stderr, "Failed to set frequency: %s\n",
                    bladerf_strerror(status));
            bladerf_close(dev);
            return EXIT_FAILURE;
        }
    }

    if (!status) {
        status = bladerf_set_sample_rate(dev, test_data.module, 40000000, &actual);
        if (status < 0) {
            fprintf(stderr, "Failed to set sample rate: %s\n",
                    bladerf_strerror(status));
            bladerf_close(dev);
            return EXIT_FAILURE;
        }
    }

    /* Initialize the stream */
    status = bladerf_init_stream(
                &stream,
                dev,
                stream_callback,
                &test_data.buffers,
                test_data.num_buffers,
                BLADERF_FORMAT_SC16_Q12,
                test_data.samples_per_buffer,
                test_data.num_buffers,
                &test_data
             ) ;

    /* Populate buffers with test data */
    if( test_data.module == BLADERF_MODULE_TX ) {
        if (populate_test_data(&test_data) ) {
            fprintf(stderr, "Failed to populated test data\n");
            bladerf_deinit_stream(stream);
            bladerf_close(dev);
            return EXIT_FAILURE;
        }
    } else {
        /* Open up file we'll read test data to */
        test_data.fout = fopen( "samples.txt", "w" );
        if (!test_data.fout) {
            fprintf(stderr, "Failed to open samples.txt: %s\n", strerror(errno));
            bladerf_deinit_stream(stream);
            bladerf_close(dev);
            return EXIT_FAILURE;
        }
    }

    status = bladerf_enable_module(dev, test_data.module, true);
    if (status < 0) {
        fprintf(stderr, "Failed to enable module: %s\n",
                bladerf_strerror(status));
    }

    if (!status) {
        /* Start stream and stay there until we kill the stream */
        status = bladerf_stream(stream, test_data.module);

        if (status < 0) {
            fprintf(stderr, "Stream error: %s\n", bladerf_strerror(status));
        }
    }

    status = bladerf_enable_module(dev, test_data.module, false);
    if (status < 0) {
        fprintf(stderr, "Failed to enable module: %s\n",
                bladerf_strerror(status));
    }

    bladerf_deinit_stream(stream);
    bladerf_close(dev);

    if (test_data.fout) {
        fclose(test_data.fout);
    }

    return 0;
}
示例#7
0
文件: rxtx.c 项目: lunixbochs/bladeRF
int cmd_rxtx(struct cli_state *s, int argc, char **argv)
{
    int ret = CMD_RET_OK;
    int fpga_loaded;
    enum rxtx_cmd cmd;
    struct common_cfg *common;
    bool is_tx;
    int (*start_init)(struct cli_state *s);
    int (*stop_cleanup)(struct cli_state *s);

        if (!strcasecmp("tx", argv[0])) {
            is_tx = true;
            common = &s->rxtx_data->tx.common;
            start_init = tx_start_init;
            stop_cleanup = tx_stop_cleanup;
        } else if (!strcasecmp("rx", argv[0])) {
            is_tx = false;
            common = &s->rxtx_data->rx.common;
            start_init = rx_start_init;
            stop_cleanup = rx_stop_cleanup;
        } else {
            /* Bug */
            assert(0);
            return CMD_RET_UNKNOWN;
        }

    /* Just <rx|tx> is supported shorthand for <rx|tx> config */
    if (argc == 1) {
        cmd = RXTX_CMD_CFG;
    } else {
        cmd = get_cmd(argv[1]);
    }

    switch (cmd) {
        case RXTX_CMD_START:
            if (!cli_device_is_opened(s)) {
                ret = CMD_RET_NODEV;
            } else if (get_state(common) == RXTX_STATE_RUNNING) {
                ret = CMD_RET_STATE;
            } else {
                fpga_loaded = bladerf_is_fpga_configured(s->dev);
                if (fpga_loaded < 0) {
                    s->last_lib_error = fpga_loaded;
                    ret = CMD_RET_LIBBLADERF;
                } else if (!fpga_loaded) {
                    cli_err(s, argv[0], "FPGA is not configured");
                    ret = CMD_RET_INVPARAM;
                } else {
                    ret = validate_config(s, argv[0], s->rxtx_data, is_tx);
                    if (!ret) {
                        ret = open_samples_file(common, NULL, is_tx ? "r" : "w");
                        if (!ret) {
                            ret = start_init(s);
                        }
                    }
                }
            }
            break;

        case RXTX_CMD_STOP:
            if (!cli_device_is_opened(s)) {
                ret = CMD_RET_NODEV;
            } else if (get_state(common) != RXTX_STATE_RUNNING) {
                ret = CMD_RET_STATE;
            } else {
                ret = stop_cleanup(s);
            }
            break;

        case RXTX_CMD_CFG:
            if (argc > 2) {
                if (get_state(common) != RXTX_STATE_RUNNING) {
                    ret = handle_params(s, argc, argv, is_tx);
                } else {
                    ret = CMD_RET_STATE;
                }
            } else {
                print_config(s->rxtx_data, is_tx);
            }
            break;

        default:
            cli_err(s, argv[0], "Invalid command (%s)", argv[1]);
            ret = CMD_RET_INVPARAM;
    }


    return ret;
}
示例#8
0
文件: main.c 项目: litghost/bladeRF
int main(int argc, char *argv[])
{
    int status = 0;
    struct rc_config rc;
    struct cli_state *state;
    bool exit_immediately = false;

    /* If no actions are specified, just show the usage text and exit */
    if (argc == 1) {
        usage(argv[0]);
        return 0;
    }

    init_rc_config(&rc);

    if (get_rc_config(argc, argv, &rc)) {
        return 1;
    }

    state = cli_state_create();

    if (!state) {
        fprintf(stderr, "Failed to create state object\n");
        return 1;
    }

    bladerf_log_set_verbosity(rc.verbosity);

    if (rc.show_help) {
        usage(argv[0]);
        exit_immediately = true;
    } else if (rc.show_version) {
        printf(BLADERF_CLI_VERSION "\n");
        exit_immediately = true;
    } else if (rc.show_lib_version) {
        struct bladerf_version version;
        bladerf_version(&version);
        printf("%s\n", version.describe);
        exit_immediately = true;
    } else if (rc.probe) {
        status = cmd_handle(state, "probe");
        exit_immediately = true;
    }

    if (!exit_immediately) {
        /* Conditionally performed items, depending on runtime config */
        status = open_device(&rc, state, status);
        if (status) {
            fprintf(stderr, "Could not open device\n");
            goto main__issues ;
        }

        status = flash_fw(&rc, state, status);
        if (status) {
            fprintf(stderr, "Could not flash firmware\n");
            goto main__issues ;
        }

        status = load_fpga(&rc, state, status);
        if (status) {
            fprintf(stderr, "Could not load fpga\n");
            goto main__issues ;
        }

        status = open_script(&rc, state, status);
        if (status) {
            fprintf(stderr, "Could not load scripts\n");
            goto main__issues ;
        }

main__issues:
        /* These items are no longer needed */
        free(rc.device);
        rc.device = NULL;

        free(rc.fw_file);
        rc.fw_file = NULL;

        free(rc.fpga_file);
        rc.fpga_file = NULL;

        free(rc.script_file);
        rc.script_file = NULL;

        /* Drop into interactive mode or begin executing commands
         * from a script. If we're not requested to do either, exit cleanly */
        if (rc.interactive_mode || state->script != NULL) {
            status = start_threads(state);

            if (status < 0) {
                fprintf(stderr, "Failed to kick off threads\n");
            } else {
                status = interactive(state, !rc.interactive_mode);
                stop_threads(state);
            }

        }

        /* Ensure we exit with RX & TX disabled.
         * Can't do much about an error at this point anyway... */
        if (state->dev && bladerf_is_fpga_configured(state->dev)) {
            bladerf_enable_module(state->dev, BLADERF_MODULE_TX, false);
            bladerf_enable_module(state->dev, BLADERF_MODULE_RX, false);
        }
    }

    cli_state_destroy(state);
    return status;
}
示例#9
0
文件: main.c 项目: adamgreig/radar
int configure_bladerf(struct bladerf** dev, struct bladerf_config* config)
{
    unsigned int abw, asr;
    int status;

    setlocale(LC_NUMERIC, "");

    printf("%-50s", "Connecting to device... ");
    fflush(stdout);
    status = bladerf_open(dev, NULL);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-50s", "Checking FPGA status... ");
    fflush(stdout);
    status = bladerf_is_fpga_configured(*dev);
    if(status < 0) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    } else if(status == 0) {
        printf(KRED "Failed: FPGA not loaded" KNRM "\n");
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'13uHz... ", "Tuning TX to:", config->tx_freq);
    fflush(stdout);
    status = bladerf_set_frequency(*dev, BLADERF_MODULE_TX, config->tx_freq);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'13uHz... ", "Tuning RX to:", config->rx_freq);
    fflush(stdout);
    status = bladerf_set_frequency(*dev, BLADERF_MODULE_RX, config->rx_freq);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'13uHz... ", "Setting TX bandwidth to:", config->tx_bw);
    fflush(stdout);
    status = bladerf_set_bandwidth(*dev, BLADERF_MODULE_TX,
                                   config->tx_bw, &abw);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'13uHz\n", "Actual bandwidth:", abw);
    if(abw != config->tx_bw) {
        printf("Actual bandwidth not equal to desired bandwidth, quitting.\n");
        return 1;
    }

    printf("%-30s %'13uHz... ", "Setting RX bandwidth to:", config->rx_bw);
    fflush(stdout);
    status = bladerf_set_bandwidth(*dev, BLADERF_MODULE_RX,
                                   config->rx_bw, &abw);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'13uHz\n", "Actual bandwidth:", abw);
    if(abw != config->rx_bw) {
        printf("Actual bandwidth not equal to desired bandwidth, quitting.\n");
        return 1;
    }

    printf("%-30s %'12usps... ", "Setting TX sampling rate to:",
           config->tx_sr);
    fflush(stdout);
    status = bladerf_set_sample_rate(*dev, BLADERF_MODULE_TX,
                                     config->tx_sr, &asr);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'12usps\n", "Actual sampling rate:", asr);
    if(asr != config->tx_sr) {
        printf("Actual sampling rate not equal to desired sampling rate, "
               "quitting.\n");
        return 1;
    }

    printf("%-30s %'12usps... ", "Setting RX sampling rate to:",
           config->rx_sr);
    fflush(stdout);
    status = bladerf_set_sample_rate(*dev, BLADERF_MODULE_RX,
                                     config->rx_sr, &asr);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %'12usps\n", "Actual sampling rate:", asr);
    if(asr != config->rx_sr) {
        printf("Actual sampling rate not equal to desired sampling rate, "
               "quitting.\n");
        return 1;
    }

    printf("%-30s %+13ddB... ", "Setting TXVGA1 gain to:", config->txvga1);
    fflush(stdout);
    status = bladerf_set_txvga1(*dev, config->txvga1);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %+13ddB... ", "Setting TXVGA2 gain to:", config->txvga2);
    fflush(stdout);
    status = bladerf_set_txvga2(*dev, config->txvga2);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %+13ddB... ", "Setting RXVGA1 gain to:", config->rxvga1);
    fflush(stdout);
    status = bladerf_set_rxvga1(*dev, config->rxvga1);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %+13ddB... ", "Setting RXVGA2 gain to:", config->rxvga2);
    fflush(stdout);
    status = bladerf_set_rxvga2(*dev, config->rxvga2);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("%-30s %15d... ", "Setting LNA gain to:", config->lna);
    fflush(stdout);
    status = bladerf_set_lna_gain(*dev, config->lna);
    if(status) {
        printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status));
        bladerf_close(*dev);
        return 1;
    }
    printf(KGRN "OK" KNRM "\n");

    printf("All set up.\n");

    return 0;
}
示例#10
0
文件: info.c 项目: DINKIN/bladeRF
int cmd_info(struct cli_state *state, int argc, char **argv)
{
    int status;
    bladerf_fpga_size fpga_size;
    uint16_t dac_trim;
    bool fpga_loaded;
    struct bladerf_devinfo info;
    bladerf_dev_speed usb_speed;

    if (!cli_device_is_opened(state)) {
        return CLI_RET_NODEV;
    }

    status = bladerf_get_devinfo(state->dev, &info);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }

    status = bladerf_is_fpga_configured(state->dev);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }
    fpga_loaded = status != 0;

    status = bladerf_get_fpga_size(state->dev, &fpga_size);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }

    status = bladerf_get_vctcxo_trim(state->dev, &dac_trim);
    if (status < 0) {
        state->last_lib_error = status;
        return CLI_RET_LIBBLADERF;
    }

    usb_speed = bladerf_device_speed(state->dev);

    printf("\n");
    printf("  Serial #:                 %s\n", info.serial);
    printf("  VCTCXO DAC calibration:   0x%.4x\n", dac_trim);
    if (fpga_size != 0) {
        printf("  FPGA size:                %d KLE\n", fpga_size);
    } else {
        printf("  FPGA size:                Unknown\n");
    }
    printf("  FPGA loaded:              %s\n", fpga_loaded ? "yes" : "no");
    printf("  USB bus:                  %d\n", info.usb_bus);
    printf("  USB address:              %d\n", info.usb_addr);
    printf("  USB speed:                %s\n", devspeed2str(usb_speed));

    switch(info.backend) {
        case BLADERF_BACKEND_LIBUSB:
            printf("  Backend:                  libusb\n");
            break;

        case BLADERF_BACKEND_LINUX:
            printf("  Backend:                  Linux driver\n");
            break;

        default:
            printf("  Backend:                  Unknown\n");
    }
    printf("  Instance:                 %d\n", info.instance);

    printf("\n");
    return 0;
}
示例#11
0
static int init_device(struct repeater *repeater, struct repeater_config *config)
{
    int status;
    unsigned int actual_value;

    status = bladerf_open(&repeater->device, config->device_str);
    if (!repeater->device) {
        fprintf(stderr, "Failed to open %s: %s\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else if (status == 0) {
        fprintf(stderr, "FPGA is not loaded. Aborting.\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual TX bandwidth: %d Hz\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual RX bandwidth: %d Hz\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual TX sample rate is %d Hz\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Actual RX sample rate is %d Hz\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Set TX frequency to %d Hz\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Set RX frequency to %d Hz\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Enabled RX module\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\n",
                bladerf_strerror(status));
        goto init_device_error;
    } else {
        printf("Enabled TX module\n");
    }

    return status;

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

    return status;
}
示例#12
0
// Open BladeRF device.
BladeRFSource::BladeRFSource(const char *serial) :
    m_dev(0),
    m_sampleRate(1000000),
    m_actualSampleRate(1000000),
    m_frequency(300000000),
    m_minFrequency(300000000),
    m_bandwidth(1500000),
    m_actualBandwidth(1500000),
    m_lnaGain(3),
    m_vga1Gain(6),
    m_vga2Gain(5),
    m_thread(0)
{
    int status;
    struct bladerf_devinfo info;

    bladerf_init_devinfo(&info);

    if (serial != 0)
    {
        strncpy(info.serial, serial, BLADERF_SERIAL_LENGTH - 1);
        info.serial[BLADERF_SERIAL_LENGTH - 1] = '\0';
    }

    status = bladerf_open_with_devinfo(&m_dev, &info);

    if (status == BLADERF_ERR_NODEV)
    {
        std::ostringstream err_ostr;
        err_ostr << "No devices available with serial=" << serial;
        m_error = err_ostr.str();
        m_dev = 0;
    }
    else if (status != 0)
    {
        std::ostringstream err_ostr;
        err_ostr << "Failed to open device with serial=" << serial;
        m_error = err_ostr.str();
        m_dev = 0;
    }
    else
    {
        int fpga_loaded = bladerf_is_fpga_configured(m_dev);

        if (fpga_loaded < 0)
        {
            std::ostringstream err_ostr;
            err_ostr << "Failed to check FPGA state: " << bladerf_strerror(fpga_loaded);
            m_error = err_ostr.str();
            m_dev = 0;
        }
        else if (fpga_loaded == 0)
        {
            m_error = "The device's FPGA is not loaded.";
            m_dev = 0;
        }
        else
        {
            if ((status = bladerf_sync_config(m_dev, BLADERF_MODULE_RX, BLADERF_FORMAT_SC16_Q11, 64, 8192, 32, 10000)) < 0)
            {
                std::ostringstream err_ostr;
                err_ostr << "bladerf_sync_config failed with return code " << status;
                m_error = err_ostr.str();
                m_dev = 0;
            }
            else
            {
                if ((status = bladerf_enable_module(m_dev, BLADERF_MODULE_RX, true)) < 0)
                {
                    std::ostringstream err_ostr;
                    err_ostr << "bladerf_enable_module failed with return code " << status;
                    m_error = err_ostr.str();
                    m_dev = 0;
                }
                else
                {
                    if (bladerf_expansion_attach(m_dev, BLADERF_XB_200) == 0)
                    {
                        std::cerr << "BladeRFSource::BladeRFSource: Attached XB200 extension" << std::endl;

                        if ((status = bladerf_xb200_set_path(m_dev, BLADERF_MODULE_RX, BLADERF_XB200_MIX)) != 0)
                        {
                            std::cerr << "BladeRFSource::BladeRFSource: bladerf_xb200_set_path failed with return code " << status << std::endl;
                        }
                        else
                        {
                            if ((status = bladerf_xb200_set_filterbank(m_dev, BLADERF_MODULE_RX, BLADERF_XB200_AUTO_1DB)) != 0)
                            {
                                std::cerr << "BladeRFSource::BladeRFSource: bladerf_xb200_set_filterbank failed with return code " << status << std::endl;
                            }
                            else
                            {
                                std::cerr << "BladeRFSource::BladeRFSource: XB200 configured. Min freq set to 100kHz" << std::endl;
                                m_minFrequency = 100000;
                            }
                        }
                    }
                }
            }
        }
    }

    std::ostringstream lgains_ostr;

    for (int g: m_lnaGains) {
        lgains_ostr << g << " ";
    }

    m_lnaGainsStr = lgains_ostr.str();

    std::ostringstream v1gains_ostr;

    for (int g: m_vga1Gains) {
        v1gains_ostr << g << " ";
    }

    m_vga1GainsStr = v1gains_ostr.str();

    std::ostringstream v2gains_ostr;

    for (int g: m_vga2Gains) {
        v2gains_ostr << g << " ";
    }

    m_vga2GainsStr = v2gains_ostr.str();

    std::ostringstream bw_ostr;

    for (int b: m_halfbw) {
        bw_ostr << 2*b << " ";
    }

    m_bwfiltStr = bw_ostr.str();

    m_this = this;
}
//! 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());
    }
}
示例#14
0
文件: bladerf.c 项目: n-west/bladeRF
int bladerf_open_with_devinfo(struct bladerf **device,
                                struct bladerf_devinfo *devinfo)
{
    struct bladerf *opened_device;
    int status;

    *device = NULL;
    status = backend_open(device, devinfo);

    if (!status) {

        /* Catch bugs from backends returning status = 0, but a NULL device */
        assert(*device);
        opened_device = *device;

        /* We got a device */
        bladerf_set_error(&opened_device->error, ETYPE_LIBBLADERF, 0);

        status = opened_device->fn->get_device_speed(opened_device,
                                                     &opened_device->usb_speed);

        if (status < 0 ||
                (opened_device->usb_speed != BLADERF_DEVICE_SPEED_HIGH &&
                 opened_device->usb_speed != BLADERF_DEVICE_SPEED_SUPER)) {
            opened_device->fn->close((*device));
            *device = NULL;
        } else {
            if (opened_device->legacy) {
                /* Currently two modes of legacy:
                 *  - ALT_SETTING
                 *  - CONFIG_IF
                 *
                 * If either of these are set, we should tell the user to update
                 */
                printf("********************************************************************************\n");
                printf("* ENTERING LEGACY MODE, PLEASE UPGRADE TO THE LATEST FIRMWARE BY RUNNING:\n");
                printf("* wget http://nuand.com/fx3/latest.img ; bladeRF-cli -f latest.img\n");
                printf("********************************************************************************\n");
            }

            if (!(opened_device->legacy & LEGACY_ALT_SETTING)) {

                status = bladerf_get_and_cache_vctcxo_trim(opened_device);
                if (status < 0) {
                    log_warning( "Could not extract VCTCXO trim value\n" ) ;
                }

                status = bladerf_get_and_cache_fpga_size(opened_device);
                if (status < 0) {
                    log_warning( "Could not extract FPGA size\n" ) ;
                }

                /* If any of these routines failed, the dev structure should
                 * still have had it's fields dummied, so they're safe to
                 * print here (i.e., not uninitialized) */
                log_debug("%s: fw=v%s serial=%s trim=0x%.4x fpga_size=%d\n",
                        __FUNCTION__, opened_device->fw_version.describe,
                        opened_device->ident.serial, opened_device->dac_trim,
                        opened_device->fpga_size);
            }

            /* All status in here is not fatal, so whatever */
            status = 0 ;
            if (bladerf_is_fpga_configured(opened_device)) {
                bladerf_init_device(opened_device);
            }
        }
    }

    return status;
}