int enable(struct bladerf* dev, bool enabled) { int status; if(enabled) printf("%-50s", "Enabling TX... "); else printf("%-50s", "Disabling TX... "); fflush(stdout); status = bladerf_enable_module(dev, BLADERF_MODULE_TX, enabled); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(dev); return 1; } printf(KGRN "OK" KNRM "\n"); if(enabled) printf("%-50s", "Enabling RX... "); else printf("%-50s", "Disabling RX... "); fflush(stdout); status = bladerf_enable_module(dev, BLADERF_MODULE_RX, enabled); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(dev); return 1; } printf(KGRN "OK" KNRM "\n"); return 0; }
static int set_and_check_paths(struct bladerf *dev, bladerf_module m, bladerf_xb200_path p) { int status; bladerf_xb200_path readback; status = bladerf_xb200_set_path(dev, m, p); if (status != 0) { PR_ERROR("Failed to set XB-200 path: %s\n", bladerf_strerror(status)); return status; } status = bladerf_xb200_get_path(dev, m, &readback); if (status != 0) { PR_ERROR("Failed to set XB-200 path: %s\n", bladerf_strerror(status)); return status; } if (p != readback) { PR_ERROR("Path mismatch -- path=%d, readback=%d\n", p, readback); return -1; } return 0; }
/** [configure_channel] */ int configure_channel(struct bladerf *dev, struct channel_config *c) { int status; status = bladerf_set_frequency(dev, c->channel, c->frequency); if (status != 0) { fprintf(stderr, "Failed to set frequency = %u: %s\n", c->frequency, bladerf_strerror(status)); return status; } status = bladerf_set_sample_rate(dev, c->channel, c->samplerate, NULL); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", c->samplerate, bladerf_strerror(status)); return status; } status = bladerf_set_bandwidth(dev, c->channel, c->bandwidth, NULL); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", c->bandwidth, bladerf_strerror(status)); return status; } status = bladerf_set_gain(dev, c->channel, c->gain); if (status != 0) { fprintf(stderr, "Failed to set gain: %s\n", bladerf_strerror(status)); return status; } return status; }
/** * Initialize synchronous interface */ static int radio_init_sync(struct bladerf *dev) { int status; const unsigned int num_buffers = 64; const unsigned int buffer_size = SYNC_BUFFER_SIZE; /* Must be a multiple of 1024 */ const unsigned int num_transfers = 16; const unsigned int timeout_ms = 3500; /* Configure both the device's RX and TX modules for use with the synchronous * interface. SC16 Q11 samples with metadata are used. */ status = bladerf_sync_config(dev, BLADERF_MODULE_RX, BLADERF_FORMAT_SC16_Q11_META, num_buffers, buffer_size, num_transfers, timeout_ms); if (status != 0) { fprintf(stderr, "Failed to configure RX sync interface: %s\n", bladerf_strerror(status)); return status; } status = bladerf_sync_config(dev, BLADERF_MODULE_TX, BLADERF_FORMAT_SC16_Q11_META, num_buffers, buffer_size, num_transfers, timeout_ms); if (status != 0) { fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(status)); } return status; }
static int set_and_check_rational(struct bladerf *dev, bladerf_module m, struct bladerf_rational_rate *rate) { int status; struct bladerf_rational_rate actual, readback; status = bladerf_set_rational_sample_rate(dev, m, rate, &actual); if (status != 0) { PR_ERROR("Failed to set rational sample rate: %s\n", bladerf_strerror(status)); return status; } status = bladerf_get_rational_sample_rate(dev, m, &readback); if (status != 0) { PR_ERROR("Failed to read back rational sample rate: %s\n", bladerf_strerror(status)); return status; } if (actual.integer != readback.integer || actual.num != readback.num || actual.den != readback.den ) { PR_ERROR("Readback mismatch:\n" " actual: int=%"PRIu64" num=%"PRIu64" den=%"PRIu64"\n" " readback: int=%"PRIu64" num=%"PRIu64" den=%"PRIu64"\n", actual.integer, actual.num, actual.den, readback.integer, readback.num, readback.den); return status; } return 0; }
static int set_and_check(struct bladerf *dev, bladerf_module module, bladerf_lpf_mode mode) { bladerf_lpf_mode readback; int status; status = bladerf_set_lpf_mode(dev, module, mode); if (status != 0) { PR_ERROR("Failed to set LPF mode: %s\n", bladerf_strerror(status)); return status; } status = bladerf_get_lpf_mode(dev, module, &readback); if (status != 0) { PR_ERROR("Failed to get LPF mode: %s\n", bladerf_strerror(status)); /* Last ditch effor to restore normal configuration */ bladerf_set_lpf_mode(dev, module, BLADERF_LPF_NORMAL); return status; } if (readback != mode) { PR_ERROR("Readback failure -- value=%d, expected=%d\n", readback, mode); return -1; } return 0; }
static int set_and_check(struct bladerf *dev, bladerf_module m, unsigned int freq) { int status; unsigned int readback; status = bladerf_set_frequency(dev, m, freq); if (status != 0) { PR_ERROR("Failed to set frequency: %u Hz: %s\n", freq, bladerf_strerror(status)); return status; } status = bladerf_get_frequency(dev, m, &readback); if (status != 0) { PR_ERROR("Failed to get frequency: %s\n", bladerf_strerror(status)); return status; } if (!freq_match(freq, readback)) { PR_ERROR("Frequency (%u) != Readback value (%u)\n", freq, readback); return -1; } return status; }
double rf_blade_set_rx_srate(void *h, double freq) { uint32_t bw; rf_blade_handler_t *handler = (rf_blade_handler_t*) h; int status = bladerf_set_sample_rate(handler->dev, BLADERF_MODULE_RX, (uint32_t) freq, &handler->rx_rate); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", (uint32_t) freq, bladerf_strerror(status)); return -1; } if (handler->rx_rate < 2000000) { status = bladerf_set_bandwidth(handler->dev, BLADERF_MODULE_RX, handler->rx_rate, &bw); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", handler->rx_rate, bladerf_strerror(status)); return -1; } } else { status = bladerf_set_bandwidth(handler->dev, BLADERF_MODULE_RX, handler->rx_rate*0.8, &bw); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", handler->rx_rate, bladerf_strerror(status)); return -1; } } printf("Set RX sampling rate %.2f Mhz, filter BW: %.2f Mhz\n", (float) handler->rx_rate/1e6, (float) bw/1e6); return (double) handler->rx_rate; }
/** [tx_meta_now] */ int sync_tx_meta_now_example(struct bladerf *dev, int16_t *samples, unsigned int num_samples, unsigned int tx_count, unsigned int timeout_ms) { int status = 0; struct bladerf_metadata meta; unsigned int i; memset(&meta, 0, sizeof(meta)); /* Send entire burst worth of samples in one function call */ meta.flags = BLADERF_META_FLAG_TX_BURST_START | BLADERF_META_FLAG_TX_NOW | BLADERF_META_FLAG_TX_BURST_END; for (i = 0; i < tx_count && status == 0; i++) { /* Fetch or produce IQ samples...*/ produce_samples(samples, num_samples); status = bladerf_sync_tx(dev, samples, num_samples, &meta, timeout_ms); if (status != 0) { fprintf(stderr, "TX failed: %s\n", bladerf_strerror(status)); } else { uint64_t curr_ts; status = bladerf_get_timestamp(dev, BLADERF_MODULE_TX, &curr_ts); if (status != 0) { fprintf(stderr, "Failed to get current TX timestamp: %s\n", bladerf_strerror(status)); } else { printf("TX'd at approximately t=%016"PRIu64"\n", curr_ts); } /* Delay next transmission by approximately 5 ms * * This is a very imprecise, "quick and dirty" means to do so in * cases where no particular intra-burst time is required. */ usleep(5000); } } /* Wait for samples to be TX'd before completing. */ if (status == 0) { status = bladerf_get_timestamp(dev, BLADERF_MODULE_TX, &meta.timestamp); if (status != 0) { fprintf(stderr, "Failed to get current TX timestamp: %s\n", bladerf_strerror(status)); return status; } else { status = wait_for_timestamp(dev, BLADERF_MODULE_TX, meta.timestamp + 2 * num_samples, timeout_ms); if (status != 0) { fprintf(stderr, "Failed to wait for timestamp.\n"); } } } return status; }
/* Buf is assumed to be NIOS_PKT_LEN bytes */ static int nios_access(struct bladerf *dev, uint8_t *buf) { int status; void *driver; struct bladerf_usb *usb = usb_backend(dev, &driver); print_buf("NIOS II request:\n", buf, NIOS_PKT_LEN); /* Send the command */ status = usb->fn->bulk_transfer(driver, PERIPHERAL_EP_OUT, buf, NIOS_PKT_LEN, PERIPHERAL_TIMEOUT_MS); if (status != 0) { log_debug("Failed to send NIOS II request: %s\n", bladerf_strerror(status)); return status; } /* Retrieve the request */ status = usb->fn->bulk_transfer(driver, PERIPHERAL_EP_IN, buf, NIOS_PKT_LEN, PERIPHERAL_TIMEOUT_MS); if (status != 0) { log_debug("Failed to receive NIOS II response: %s\n", bladerf_strerror(status)); } print_buf("NIOS II response:\n", buf, NIOS_PKT_LEN); return status; }
static int set_and_check_filterbank(struct bladerf *dev, bladerf_module m, bladerf_xb200_filter f) { int status; bladerf_xb200_filter readback; status = bladerf_xb200_set_filterbank(dev, m, f); if (status != 0) { PR_ERROR("Failed to set XB-200 filter bank: %s\n", bladerf_strerror(status)); return status; } status = bladerf_xb200_get_filterbank(dev, m, &readback); if (status != 0) { PR_ERROR("Failed to read back filter bank: %s\n", bladerf_strerror(status)); return status; } if (f != readback) { PR_ERROR("Mismatch detected -- fiterbank=%d, readback=%d\n", f, readback); return -1; } return 0; }
static int set_and_check(struct bladerf *dev, bladerf_module m, unsigned int bandwidth) { int status; unsigned int actual, readback; status = bladerf_set_bandwidth(dev, m, bandwidth, &actual); if (status != 0) { PR_ERROR("Failed to set bandwidth: %s\n", bladerf_strerror(status)); return status; } status = bladerf_get_bandwidth(dev, m, &readback); if (status != 0) { PR_ERROR("Failed to read back bandwidth: %s\n", bladerf_strerror(status)); return status; } if (readback != actual) { PR_ERROR("Unexpected bandwidth. requested=%u, actual=%u, readback=%u\n", bandwidth, actual, readback); return -1; } return 0; }
int example(struct bladerf *dev, bladerf_channel ch) { /** [example] */ int status; unsigned int i, j; // clang-format off const unsigned int frequencies[NUM_FREQUENCIES] = { 902000000, 903000000, 904000000, 925000000, 926000000, 927000000, }; // clang-format on struct bladerf_quick_tune quick_tunes[NUM_FREQUENCIES]; /* Get our quick tune parameters for each frequency we'll be using */ for (i = 0; i < NUM_FREQUENCIES; i++) { status = bladerf_set_frequency(dev, ch, frequencies[i]); if (status != 0) { fprintf(stderr, "Failed to set frequency to %u Hz: %s\n", frequencies[i], bladerf_strerror(status)); return status; } status = bladerf_get_quick_tune(dev, ch, &quick_tunes[i]); if (status != 0) { fprintf(stderr, "Failed to get quick tune for %u Hz: %s\n", frequencies[i], bladerf_strerror(status)); return status; } } for (i = j = 0; i < ITERATIONS; i++) { /* Tune to the specified frequency immediately via BLADERF_RETUNE_NOW. * * Alternatively, this re-tune could be scheduled by providing a * timestamp counter value */ status = bladerf_schedule_retune(dev, ch, BLADERF_RETUNE_NOW, 0, &quick_tunes[j]); if (status != 0) { fprintf(stderr, "Failed to apply quick tune: %s\n", bladerf_strerror(status)); return status; } j = (j + 1) % NUM_FREQUENCIES; /* ... Handle signals at current frequency ... */ } /** [example] */ return status; }
/* Access device/module via the legacy NIOS II packet format. */ static int nios_access(struct bladerf *dev, uint8_t peripheral, usb_direction dir, struct uart_cmd *cmd, size_t len) { void *driver; struct bladerf_usb *usb = usb_backend(dev, &driver); int status; size_t i; uint8_t buf[16] = { 0 }; const uint8_t pkt_mode_dir = (dir == USB_DIR_HOST_TO_DEVICE) ? NIOS_PKT_LEGACY_MODE_DIR_WRITE : NIOS_PKT_LEGACY_MODE_DIR_READ; assert(len <= ((sizeof(buf) - 2) / 2)); /* Populate the buffer for transfer, given address data pairs */ buf[0] = NIOS_PKT_LEGACY_MAGIC; buf[1] = pkt_mode_dir | peripheral | (uint8_t)len; for (i = 0; i < len; i++) { buf[i * 2 + 2] = cmd[i].addr; buf[i * 2 + 3] = cmd[i].data; } print_buf("NIOS II access request:\n", buf, 16); /* Send the command */ status = usb->fn->bulk_transfer(driver, PERIPHERAL_EP_OUT, buf, sizeof(buf), PERIPHERAL_TIMEOUT_MS); if (status != 0) { log_debug("Failed to submit NIOS II request: %s\n", bladerf_strerror(status)); return status; } /* Read back the ACK. The command data is only used for a read operation, * and is thrown away otherwise */ status = usb->fn->bulk_transfer(driver, PERIPHERAL_EP_IN, buf, sizeof(buf), PERIPHERAL_TIMEOUT_MS); if (dir == NIOS_PKT_LEGACY_MODE_DIR_READ && status == 0) { for (i = 0; i < len; i++) { cmd[i].data = buf[i * 2 + 3]; } } if (status == 0) { print_buf("NIOS II access response:\n", buf, 16); } else { log_debug("Failed to receive NIOS II response: %s\n", bladerf_strerror(status)); } return status; }
static int write_page(struct bladerf *dev, uint16_t page, const uint8_t *buf) { int status; int32_t commit_status; uint16_t offset; uint16_t write_size; void *driver; struct bladerf_usb *usb = usb_backend(dev, &driver); if (dev->usb_speed == BLADERF_DEVICE_SPEED_SUPER) { write_size = BLADERF_FLASH_PAGE_SIZE; } else if (dev->usb_speed == BLADERF_DEVICE_SPEED_HIGH) { write_size = 64; } else { assert(!"BUG - unexpected device speed"); return BLADERF_ERR_UNEXPECTED; } /* Write the data to the firmware's page buffer. * Casting away the buffer's const-ness here is gross, but this buffer * will not be written to on an out transfer. */ for (offset = 0; offset < BLADERF_FLASH_PAGE_SIZE; offset += write_size) { status = usb->fn->control_transfer(driver, USB_TARGET_INTERFACE, USB_REQUEST_VENDOR, USB_DIR_HOST_TO_DEVICE, BLADE_USB_CMD_WRITE_PAGE_BUFFER, 0, offset, (uint8_t*)&buf[offset], write_size, CTRL_TIMEOUT_MS); if(status < 0) { log_error("Failed to write page buffer at offset 0x%02x " "for page %u: %s\n", offset, page, bladerf_strerror(status)); return status; } } /* Commit the page to flash */ status = vendor_cmd_int_windex(dev, BLADE_USB_CMD_FLASH_WRITE, page, &commit_status); if (status != 0) { log_error("Failed to commit page %u: %s\n", page, bladerf_strerror(status)); return status; } else if (commit_status != 0) { log_error("Failed to commit page %u, FW returned %d\n", page, commit_status); return BLADERF_ERR_UNEXPECTED; } return 0; }
/** [tx_meta_sched] */ int sync_tx_meta_sched_example(struct bladerf *dev, int16_t *samples, unsigned int num_samples, unsigned int tx_count, unsigned int samplerate, unsigned int timeout_ms) { int status = 0; unsigned int i; struct bladerf_metadata meta; memset(&meta, 0, sizeof(meta)); /* Send entire burst worth of samples in one function call */ meta.flags = BLADERF_META_FLAG_TX_BURST_START | BLADERF_META_FLAG_TX_BURST_END; /* Retrieve the current timestamp so we can schedule our transmission * in the future. */ status = bladerf_get_timestamp(dev, BLADERF_MODULE_TX, &meta.timestamp); if (status != 0) { fprintf(stderr, "Failed to get current TX timestamp: %s\n", bladerf_strerror(status)); return status; } else { printf("Current TX timestamp: %016"PRIu64"\n", meta.timestamp); } for (i = 0; i < tx_count && status == 0; i++) { /* Get sample to transmit... */ produce_samples(samples, num_samples); /* Schedule burst 5 ms into the future */ meta.timestamp += samplerate / 200; status = bladerf_sync_tx(dev, samples, num_samples, &meta, timeout_ms); if (status != 0) { fprintf(stderr, "TX failed: %s\n", bladerf_strerror(status)); return status; } else { printf("TX'd @ t=%016"PRIu64"\n", meta.timestamp); } } /* Wait for samples to finish being transmitted. */ if (status == 0) { meta.timestamp += 2 * num_samples; status = wait_for_timestamp(dev, BLADERF_MODULE_TX, meta.timestamp, timeout_ms); if (status != 0) { fprintf(stderr, "Failed to wait for timestamp.\n"); } } return status; }
static int init_streams(struct repeater *repeater, struct repeater_config *config) { int status; /* TODO Until we can provide NULL to the init stream call to indicate that * we want to use user-probveded buffers, we'll just allocate and not * use some dummy buffers for TX */ void **dummy; status = bladerf_init_stream(&repeater->rx_stream, repeater->device, rx_stream_callback, &repeater->buf_mgmt.samples, config->num_buffers, BLADERF_FORMAT_SC16_Q12, config->samples_per_buffer, config->num_transfers, repeater); if (status < 0) { fprintf(stderr, "Failed to initialize RX stream: %s\n", bladerf_strerror(status)); return status; } else { repeater->buf_mgmt.rx_idx = 0; } status = bladerf_init_stream(&repeater->tx_stream, repeater->device, tx_stream_callback, &dummy, config->num_buffers, BLADERF_FORMAT_SC16_Q12, config->samples_per_buffer, config->num_transfers, repeater); if (status < 0) { fprintf(stderr, "Failed to initialize TX stream: %s\n", bladerf_strerror(status)); return status; } else { repeater->buf_mgmt.tx_idx = 0; } repeater->buf_mgmt.num_buffers = config->num_buffers; return 0; }
int backend_probe(backend_probe_target probe_target, struct bladerf_devinfo **devinfo_items, size_t *num_items) { int status; int first_backend_error = 0; struct bladerf_devinfo_list list; size_t i; const size_t n_backends = ARRAY_SIZE(backend_list); *devinfo_items = NULL; *num_items = 0; status = bladerf_devinfo_list_init(&list); if (status != 0) { log_debug("Failed to initialize devinfo list: %s\n", bladerf_strerror(status)); return status; } for (i = 0; i < n_backends; i++) { status = backend_list[i]->probe(probe_target, &list); if (status < 0 && status != BLADERF_ERR_NODEV) { log_debug("Probe failed on backend %d: %s\n", i, bladerf_strerror(status)); if (!first_backend_error) { first_backend_error = status; } } } *num_items = list.num_elt; if (*num_items != 0) { *devinfo_items = list.elt; } else { /* For no items, we end up passing back a NULL list to the * API caller, so we'll just free this up now */ free(list.elt); /* Report the first error that occurred if we couldn't find anything */ status = first_backend_error == 0 ? BLADERF_ERR_NODEV : first_backend_error; } return status; }
int setup_tx_stream(struct bladerf* dev, struct bladerf_stream** stream, struct bladerf_stream_data* stream_data) { int status; stream_data->next_buffer = 0; stream_data->module = BLADERF_MODULE_TX; printf("%-50s", "Initialising TX data stream... "); fflush(stdout); status = bladerf_init_stream(stream, dev, stream_cb, &stream_data->buffers, stream_data->num_buffers, BLADERF_FORMAT_SC16_Q12, stream_data->samples_per_buffer, stream_data->num_transfers, stream_data); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(dev); return 1; } fill_tx_buffers(stream_data); printf(KGRN "OK" KNRM "\n"); return 0; }
static inline int verify_flash(struct bladerf *dev, uint8_t *readback_buf, uint8_t *image, uint32_t page, uint32_t count) { int status = 0; size_t i; const size_t len = count * BLADERF_FLASH_PAGE_SIZE; log_info("Verifying %u pages, starting at page %u\n", count, page); status = flash_read(dev, readback_buf, page, count); if (status < 0) { log_debug("Failed to read from flash: %s\n", bladerf_strerror(status)); return status; } for (i = 0; i < len; i++) { if (image[i] != readback_buf[i]) { status = BLADERF_ERR_UNEXPECTED; log_info("Flash verification failed at byte %llu. " "Read %02x, expected %02x\n", i, readback_buf[i], image[i]); break; } } return status; }
void rxtx_print_error(struct rxtx_data *rxtx, const char *prefix, const char *suffix) { enum error_type type; int val; get_last_error(&rxtx->last_error, &type, &val); if (val != 0) { switch (type) { case ETYPE_ERRNO: printf("%s%s%s", prefix, strerror(val), suffix); break; case ETYPE_CLI: printf("%s%s%s", prefix, cmd_strerror(val, 0), suffix); break; case ETYPE_BLADERF: printf("%s%s%s", prefix, bladerf_strerror(val), suffix); break; default: printf("%sBUG: Unexpected status=%d%s", prefix, val, suffix); } } else { printf("%sNone%s", prefix, suffix); } }
static int usb_erase_flash_blocks(struct bladerf *dev, uint32_t eb, uint16_t count) { int status, restore_status; uint16_t i; status = change_setting(dev, USB_IF_SPI_FLASH); if (status != 0) { return status; } log_info("Erasing %u blocks starting at block %u\n", count, eb); for (i = 0; i < count; i++) { status = perform_erase(dev, eb + i); if (status == 0) { log_info("Erased block %u%c", eb + i, (i+1) == count ? '\n':'\r' ); } else { log_debug("Failed to erase block %u: %s\n", eb + i, bladerf_strerror(status)); goto error; } } log_info("Done erasing %u blocks\n", count); error: restore_status = restore_post_flash_setting(dev); return status != 0 ? status : restore_status; }
void BladeRfTxComponent::process() { //Get a DataSet from the input DataBuffer DataSet< complex<float> >* readDataSet = NULL; inBuf_->getReadData(readDataSet); // Check if we have to append dummy frames, samps must be multiple of 1024 size_t size = readDataSet->data.size(); if (size % BLADERF_SAMPLE_BLOCK_SIZE != 0) { int num_samps_to_append = BLADERF_SAMPLE_BLOCK_SIZE - (size % BLADERF_SAMPLE_BLOCK_SIZE); std::vector<std::complex<float> > samples(num_samps_to_append, std::complex<float>(0.0, 0.0)); readDataSet->data.insert(readDataSet->data.end(), samples.begin(), samples.end()); size += num_samps_to_append; } // Adjust raw buffer if needed if (rawSampleBuffer_.data.capacity() < size) rawSampleBuffer_.data.resize(size); // convert samples to bladeRF format for (int i = 0; i < size; i++) { rawSampleBuffer_.data[i].real(0xa000 | (int16_t)((readDataSet->data[i].real()) * 2000)); rawSampleBuffer_.data[i].imag(0x5000 | (int16_t)((readDataSet->data[i].imag()) * 2000)); } // Write samples to device int ret = bladerf_sync_tx(device_, &(rawSampleBuffer_.data.front()), size, NULL, BLADERF_SYNC_TIMEOUT_MS); if (ret != 0) { throw IrisException("Failed to send samples to device!"); LOG(LERROR) << bladerf_strerror(ret); } //Release the DataSet inBuf_->releaseReadData(readDataSet); }
static void print_state(enum rxtx_state state, int error, int error_type) { switch (state) { case RXTX_STATE_IDLE: printf(" State: Idle\n"); break; case RXTX_STATE_ERROR: if (error_type == ETYPE_ERRNO) { printf(" State: Error -- %s\n", strerror(error)); } else if (error_type == ETYPE_BLADERF) { printf(" State: Error -- %s\n", bladerf_strerror(error)); } else if (error_type == ETYPE_CLI) { printf(" State: Error -- %s\n", cmd_strerror(error, 0)); } else { printf(" State: Error -- (%d)\n", error); } break; case RXTX_STATE_SHUTDOWN: printf(" State: Shutting down\n"); break; case RXTX_STATE_RUNNING: printf(" State: Running\n"); break; default: printf(" State: Invalid/Unknown (BUG)\n"); } }
double rf_blade_set_tx_srate(void *h, double freq) { uint32_t bw; rf_blade_handler_t *handler = (rf_blade_handler_t*) h; int status = bladerf_set_sample_rate(handler->dev, BLADERF_MODULE_TX, (uint32_t) freq, &handler->tx_rate); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", (uint32_t) freq, bladerf_strerror(status)); return -1; } status = bladerf_set_bandwidth(handler->dev, BLADERF_MODULE_TX, handler->tx_rate, &bw); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", handler->tx_rate, bladerf_strerror(status)); return -1; } return (double) handler->tx_rate; }
void * tx_task_run(void *repeater_) { int status; struct repeater *repeater = (struct repeater *)repeater_; bool exit_early; /* Wait for RX task to buffer up some samples before we begin * consuming them */ pthread_mutex_lock(&repeater->buf_mgmt.lock); while(repeater->buf_mgmt.num_filled < repeater->buf_mgmt.prefill_count && repeater->buf_mgmt.tx_idx >= 0) { status = pthread_cond_wait(&repeater->buf_mgmt.samples_available, &repeater->buf_mgmt.lock); } exit_early = repeater->buf_mgmt.tx_idx < 0; pthread_mutex_unlock(&repeater->buf_mgmt.lock); if (!exit_early) { /* Call stream */ status = bladerf_stream(repeater->tx_stream, BLADERF_MODULE_TX); if (status < 0) { print_error(repeater, "TX stream failure: %s\n", bladerf_strerror(status)); } } else { printf("EARLY EXIT\n"); } return NULL; }
static int open_device(struct rc_config *rc, struct cli_state *state, int status) { if (!status) { status = bladerf_open(&state->dev, rc->device); if (status) { /* Just warn if no device is attached; don't error out */ if (!rc->device && status == BLADERF_ERR_NODEV) { fprintf(stderr, "\n"); fprintf(stderr, "No bladeRF device(s) available.\n"); fprintf(stderr, "\n"); fprintf(stderr, "If one is attached, ensure it is not in use by another program\n"); fprintf(stderr, "and that the current user has permission to access it.\n"); fprintf(stderr, "\n"); status = 0; } else { fprintf(stderr, "Failed to open device (%s): %s\n", rc->device ? rc->device : "first available", bladerf_strerror(status)); status = -1; } } } return status; }
static int flash_fpga(struct rc_config *rc, struct cli_state *state, int status) { if (!status && rc->flash_fpga_file) { if (!state->dev) { print_error_need_devarg(); status = -1; } else { if (!strcmp("X", rc->flash_fpga_file)) { printf("Erasing stored FPGA to disable autoloading...\n"); status = bladerf_erase_stored_fpga(state->dev); } else { printf("Writing FPGA to flash for autoloading...\n"); status = bladerf_flash_fpga(state->dev, rc->flash_fpga_file); } if (status) { fprintf(stderr, "Error: %s\n", bladerf_strerror(status)); } else { printf("Done.\n"); } } } return status; }
static int flash_fw(struct rc_config *rc, struct cli_state *state, int status) { if (!status && rc->fw_file) { if (!state->dev) { print_error_need_devarg(); status = -1; } else { printf("Flashing firmware...\n"); status = bladerf_flash_firmware(state->dev, rc->fw_file); if (status) { fprintf(stderr, "Error: failed to flash firmware: %s\n", bladerf_strerror(status)); } else { printf("Done.\n"); } bladerf_device_reset(state->dev); bladerf_close(state->dev); state->dev = NULL; } } /* TODO Do we have to fire off some sort of reset after flashing * the firmware, and before loading the FPGA? */ return status; }
int rf_blade_stop_rx_stream(void *h) { rf_blade_handler_t *handler = (rf_blade_handler_t*) h; int status = bladerf_enable_module(handler->dev, BLADERF_MODULE_RX, false); if (status != 0) { fprintf(stderr, "Failed to enable RX module: %s\n", bladerf_strerror(status)); return status; } status = bladerf_enable_module(handler->dev, BLADERF_MODULE_TX, false); if (status != 0) { fprintf(stderr, "Failed to enable TX module: %s\n", bladerf_strerror(status)); return status; } handler->rx_stream_enabled = false; handler->tx_stream_enabled = false; return 0; }