Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
void rxtx_task_exec_running(struct rxtx_data *rxtx)
{
    int status = bladerf_stream(rxtx->data_mgmt.stream, rxtx->module);

    if (status < 0) {
        set_last_error(&rxtx->last_error, ETYPE_BLADERF, status);
    }

    rxtx_set_state(rxtx, RXTX_STATE_STOP);
}
Ejemplo n.º 3
0
void * rx_task_run(void *repeater_)
{
    int status;
    struct repeater *repeater = (struct repeater *)repeater_;

    status = bladerf_stream(repeater->rx_stream, BLADERF_MODULE_RX);
    if (status < 0) {
        print_error(repeater, "RX stream failure: %s\n", bladerf_strerror(status));
    }

    return NULL;
}
Ejemplo n.º 4
0
static void exec_running_state(struct bladerf_sync *s)
{
    int status;
    unsigned int i;

    pthread_mutex_lock(&s->buf_mgmt.lock);

    if (s->stream_config.module == BLADERF_MODULE_TX) {
        /* If we've previously timed out on a stream, we'll likely have some
         * stale buffers marked "in-flight" that have since been cancelled. */
        for (i = 0; i < s->buf_mgmt.num_buffers; i++) {
            if (s->buf_mgmt.status[i] == SYNC_BUFFER_IN_FLIGHT) {
                s->buf_mgmt.status[i] = SYNC_BUFFER_EMPTY;
            }
        }

        pthread_cond_signal(&s->buf_mgmt.buf_ready);
    } else {
        assert(s->stream_config.module == BLADERF_MODULE_RX);
        s->buf_mgmt.prod_i = s->stream_config.num_xfers;

        for (i = 0; i < s->buf_mgmt.num_buffers; i++) {
            if (i < s->stream_config.num_xfers) {
                s->buf_mgmt.status[i] = SYNC_BUFFER_IN_FLIGHT;
            } else if (s->buf_mgmt.status[i] == SYNC_BUFFER_IN_FLIGHT) {
                s->buf_mgmt.status[i] = SYNC_BUFFER_EMPTY;
            }
        }
    }

    pthread_mutex_unlock(&s->buf_mgmt.lock);

    status = bladerf_stream(s->worker->stream, s->stream_config.module);

    log_verbose("%s worker: stream ended with: %s\n",
                MODULE_STR(s), bladerf_strerror(status));

    /* Save off the result of running the stream so we can report what
     * happened to the API caller */
    pthread_mutex_lock(&s->worker->state_lock);
    s->worker->err_code = status;
    pthread_mutex_unlock(&s->worker->state_lock);

    /* Wake the API-side if an error occurred, so that it can propagate
     * the stream error code back to the API caller */
    if (status != 0) {
        pthread_mutex_lock(&s->buf_mgmt.lock);
        pthread_cond_signal(&s->buf_mgmt.buf_ready);
        pthread_mutex_unlock(&s->buf_mgmt.lock);
    }
}
Ejemplo n.º 5
0
void* txrx_thread(void* arg)
{
    struct bladerf_thread_data* thread_data = (struct bladerf_thread_data*)arg;

    /*
    while(*(thread_data->waiting))
        usleep(1);
    */

    thread_data->rv = bladerf_stream(thread_data->stream,
                                     thread_data->stream_data->module);

    return NULL;
}
Ejemplo n.º 6
0
static void exec_running_state(struct bladerf_sync *s)
{
    int status;
    unsigned int i;

    pthread_mutex_lock(&s->buf_mgmt.lock);

    if (s->stream_config.module == BLADERF_MODULE_TX) {
        /* If we've previously timed out on a stream, we'll likely have some
         * stale buffers marked "in-flight" that have since been cancelled. */
        for (i = 0; i < s->buf_mgmt.num_buffers; i++) {
            if (s->buf_mgmt.status[i] == SYNC_BUFFER_IN_FLIGHT) {
                s->buf_mgmt.status[i] = SYNC_BUFFER_EMPTY;
            }
        }

        pthread_cond_signal(&s->buf_mgmt.buf_ready);
    } else {
        assert(s->stream_config.module == BLADERF_MODULE_RX);
        s->buf_mgmt.prod_i = s->stream_config.num_xfers;

        for (i = 0; i < s->buf_mgmt.num_buffers; i++) {
            if (i < s->stream_config.num_xfers) {
                s->buf_mgmt.status[i] = SYNC_BUFFER_IN_FLIGHT;
            } else if (s->buf_mgmt.status[i] == SYNC_BUFFER_IN_FLIGHT) {
                s->buf_mgmt.status[i] = SYNC_BUFFER_EMPTY;
            }
        }
    }

    pthread_mutex_unlock(&s->buf_mgmt.lock);

    status = bladerf_stream(s->worker->stream, s->stream_config.module);

    log_verbose("%s worker: stream ended with: %s\n",
                MODULE_STR(s), bladerf_strerror(status));

    /* Suppress warning if log_verbose is disabled */
    (void)status;
}
Ejemplo n.º 7
0
static void exec_running_state(struct bladerf_sync *s)
{
    int status;

    status = bladerf_stream(s->worker->stream, s->stream_config.module);

    log_verbose("%s worker: stream ended with: %s\n",
                MODULE_STR(s), bladerf_strerror(status));

    /* Save off the result of running the stream so we can report what
     * happened to the API caller */
    pthread_mutex_lock(&s->worker->state_lock);
    s->worker->err_code = status;
    pthread_mutex_unlock(&s->worker->state_lock);

    /* Wake the API-side if an error occurred, so that it can propagate
     * the stream error code back to the API caller */
    if (status != 0) {
        pthread_mutex_lock(&s->buf_mgmt.lock);
        pthread_cond_signal(&s->buf_mgmt.buf_ready);
        pthread_mutex_unlock(&s->buf_mgmt.lock);
    }
}
Ejemplo n.º 8
0
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;
}
Ejemplo n.º 9
0
/* Initialization and stuff
 */
int main(int argc, char **argv)
{
	struct bladerf_devinfo *devs;
	struct sigaction sigact;
	struct devinfo_s device;
	int show_help = false;
	int n, ret;
	char ch;

	/* Set up default values, bandwidth and num_transfers
	 * are automatically calculated later */
	device.device_id = DEFAULT_DEVICE_ID;
	device.frequency = DEFAULT_FREQUENCY;
	device.samplerate = DEFAULT_SAMPLERATE;
	device.bandwidth = 0;
	device.txvga1 = DEFAULT_TXVGA1;
	device.txvga2 = DEFAULT_TXVGA2;

	device.buffers.gain = DEFAULT_GAIN;
	device.buffers.again = DEFAULT_AGAIN;
	device.buffers.num_buffers = DEFAULT_BUFFERS;
	device.buffers.num_samples = DEFAULT_SAMPLES;
	device.buffers.num_transfers = 0;
	device.buffers.pos = 0;

	/* Evaluate command line options */
	while((ch = getopt(argc, argv, "hd:f:r:b:g:G:a:m:n:s:t:")) != -1)
	{
		switch(ch)
		{
			case 'd':
				device.device_id = optarg; break;
			case 'f':
				device.frequency = atoi(optarg); break;
			case 'r':
				device.samplerate = atoi(optarg); break;
			case 'b':
				device.bandwidth = atoi(optarg); break;
			case 'g':
				device.txvga1 = atoi(optarg); break;
			case 'G':
				device.txvga2 = atoi(optarg); break;
			case 'm':
				device.buffers.gain = atof(optarg); break;
			case 'a':
				device.buffers.again = atof(optarg); break;
			case 'n':
				device.buffers.num_buffers = atoi(optarg); break;
			case 's':
				device.buffers.num_samples = atoi(optarg); break;
			case 't':
				device.buffers.num_transfers = atoi(optarg); break;
			case 'h':
			default:
				show_help = true;
		}
	}

	/* Now calculate bandwidth and num_transfers if the user didn't
	 * configure them manually */
	if(device.bandwidth == 0)
		device.bandwidth = device.samplerate * 3 / 4;
	if(device.buffers.num_transfers == 0)
		device.buffers.num_transfers = device.buffers.num_buffers / 2;

	if(show_help)
	{
		usage(argv[0], &device);
		return EXIT_FAILURE;
	}
	
	argc -= optind;
	argv += optind;

	/* Allocate the float input buffer */
	device.buffers.fbuf =
		malloc(device.buffers.num_samples * 2 * sizeof(float));
	
	/* Set up signal handler to enable clean shutdowns */
	sigact.sa_handler = sighandler;
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = 0;
	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);
	sigaction(SIGQUIT, &sigact, NULL);
	sigaction(SIGPIPE, &sigact, NULL);



	/* Look for devices attached */
	ret = bladerf_get_device_list(&devs);
	if(ret < 1)
	{
		fprintf(stderr, "No devices found.\n");
		return EXIT_FAILURE;
	}

	/* Print some information about all the devices */
	for(n = 0; n < ret; n++)
	{
		fprintf(stderr, 
			"Serial:\t%s\n"
			"USB bus:\t%i\n"
			"USB address:\t%i\n"
			"Instance:\t%i\n\n",
			devs[n].serial,
			devs[n].usb_bus,
			devs[n].usb_addr,
			devs[n].instance
		);
	}

	/* the list is not needed any more */
	bladerf_free_device_list(devs);



	/* Open a device by given device string
	 */
	ret = bladerf_open(&device.dev, device.device_id);
	if(ret != 0)
	{
		fprintf(stderr, "Error opening device %s: %s.\n",
			device.device_id, bladerf_strerror(ret));
		goto out0;
	}
	else
	{
		fprintf(stderr, "Device \"%s\" opened successfully.\n",
			device.device_id);
	}

	/* Set the device parameters */
	ret = bladerf_set_sample_rate(device.dev,
		BLADERF_MODULE_TX, device.samplerate, &device.samplerate);
	if(ret != 0)
	{
		fprintf(stderr, "Error setting sample rate to %i: %s.\n",
			device.samplerate, bladerf_strerror(ret));
		goto out1;
	}
	else
	{
		fprintf(stderr, "Actual sample rate is %i.\n",
			device.samplerate);
	}

	ret = bladerf_set_frequency(device.dev,
		BLADERF_MODULE_TX, device.frequency);
	if(ret != 0)
	{
		fprintf(stderr, "Error setting frequency to %iHz: %s.\n",
			device.frequency, bladerf_strerror(ret));
		goto out1;
	}
	else
	{
		fprintf(stderr, "Frequency set to %iHz.\n", device.frequency);
	}

	ret = bladerf_set_txvga1(device.dev, device.txvga1);
	if(ret != 0)
	{
		fprintf(stderr, "Error setting gain for txvga1: %s.\n",
			bladerf_strerror(ret));
		goto out1;
	}

	ret = bladerf_set_txvga2(device.dev, device.txvga2);
	if(ret != 0)
	{
		fprintf(stderr, "Error setting gain for txvga2: %s.\n",
			bladerf_strerror(ret));
		goto out1;
	}

	ret = bladerf_set_bandwidth(device.dev,
		BLADERF_MODULE_TX, device.bandwidth, &device.bandwidth);
	if(ret != 0)
	{
		fprintf(stderr, "Error setting LPF bandwidth: %s.\n",
			bladerf_strerror(ret));
		goto out1;
	}
	else
	{
		fprintf(stderr, "Bandwidth set to %iHz.\n", device.bandwidth);
	}

	/* Set up the sample stream */
	ret = bladerf_init_stream(&device.stream,
		device.dev, stream_callback, &device.buffers.sbuf,
		device.buffers.num_buffers,	BLADERF_FORMAT_SC16_Q12,
		device.buffers.num_samples, device.buffers.num_transfers,
		&device.buffers);
	if(ret != 0)
	{
		fprintf(stderr, "Failed setting up stream: %s.\n",
			bladerf_strerror(ret));
		goto out1;
	}
	

	/* Finally enable TX... */
	ret = bladerf_enable_module(device.dev, BLADERF_MODULE_TX, true);
	if(ret != 0)
	{
		fprintf(stderr, "Error enabling TX module: %s.\n",
			bladerf_strerror(ret));
		goto out1;
	}
	else
	{
		fprintf(stderr, "Successfully enabled TX module.\n");
	}

	/* ...and start the stream.
	 * Execution stops here until stream has finished. */
	ret = bladerf_stream(device.stream, BLADERF_MODULE_TX);
	if(ret != 0)
	{
		fprintf(stderr, "Failed starting stream: %s.\n",
			bladerf_strerror(ret));
		goto out2;
	}


	/* Cleanup the mess */
out2:
	bladerf_deinit_stream(device.stream);

out1:
	ret = bladerf_enable_module(device.dev, BLADERF_MODULE_TX, false);
	if(ret != 0)
	{
		fprintf(stderr, "Error disabling TX module: %s.\n",
			bladerf_strerror(ret));
	}
	else
	{
		fprintf(stderr, "Successfully disabled TX module.\n");
	}
	
	bladerf_close(device.dev);
	fprintf(stderr, "Device closed.\n");
	
out0:
	return EXIT_SUCCESS;
}