sweep_test.c 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Simple demo that reads samples using the read call */

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/select.h>

#define FDELAY_INTERNAL // for sysfs_get/set
#include "fdelay_lib.h"

int configure_board(fdelay_device_t *b, int argc, char *argv[])
{
	fdelay_time_t t;
	
22 23 24 25 26 27 28
	if(spec_fdelay_create(b, argc, argv) < 0)
	{
		printf("Probe failed\n");
		exit(-1);
	}
	
	if(fdelay_init(b, 0) < 0)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	{
		printf("Init failed\n");
		exit(-1);
	}

	fdelay_configure_sync(b, FDELAY_SYNC_LOCAL);

	fdelay_get_time(b, &t);
	t.frac = 0;
	t.coarse = 0;
	t.utc += 2;

	sleep(1);
	
	fdelay_configure_pulse_gen(b, 1, 1, t, 10 * 1000000, 99999990, -1);

	fdelay_configure_trigger(b, 0, 1);	

	fdelay_raw_readout(b, 0);	
	fdelay_configure_readout(b, 0);	
	fdelay_configure_readout(b, 1);	
	fdelay_configure_trigger(b, 1, 0);	

	printf("Configuration complete\n");
	fflush(stdout);

    return 0;
}

void handle_readout(fdelay_device_t *b, int n_samples)
{
    int64_t t_ps;
    fdelay_time_t t;
    static time_t start;
    static int64_t t_prev;
    static int prev_seq = -1;
    int done;

	for(;;)
	    while(fdelay_read(b, &t, 1) == 1)
    	{	    
			t_ps = (t.coarse * 8000LL) + ((t.frac * 8000LL) >> 12);
			int64_t delta =  t_ps-t_prev;
		
			if(delta < 0)
				delta += 1000000000000LL;
		
			if(prev_seq >= 0)
				printf("Samp %lli.%03lli %lli\n", t_ps / 1000LL, t_ps % 1000LL, delta) ;
			
			prev_seq = t.seq_id;
			t_prev=t_ps;
			n_samples--;
			if(!n_samples) return;
    	}
}

int main(int argc, char *argv[])
{
	fdelay_device_t b;

	configure_board(&b, argc, argv);
	handle_readout(&b, 40000);
}