random_pulse_test.c 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/* 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"

#define PACKED __attribute__((packed))

#define MAX_QUEUE_SIZE 1024

struct pulse_queue {
	fdelay_time_t p[MAX_QUEUE_SIZE];
	int head, tail, count, size;
};

void pqueue_clear(struct pulse_queue *p)
{
 	p->head = p->tail = p->count = 0;
 	p->size = MAX_QUEUE_SIZE;
}

void pqueue_push(struct pulse_queue *p, fdelay_time_t *t)
{
	if(p->count == MAX_QUEUE_SIZE)
		return;
		
	p->p[p->tail] = *t;
	p->tail++;
	p->count++;
	if(p->tail == MAX_QUEUE_SIZE)
		p->tail = 0;
}

int pqueue_empty(struct pulse_queue *p)
{
 	return p->count == 0 ? 1 : 0;
}

int pqueue_pop(struct pulse_queue *p, fdelay_time_t *t)
{
	if(p->count == 0)
		return;
		
	*t = p->p[p->head];
	p->head++;
	p->count--;
	if(p->head == MAX_QUEUE_SIZE)
		p->head = 0;
}


#define INPUT_OFFSET -69100
#define OUTPUT_OFFSET 14400

struct pulse_queue outgoing, incoming;

/* Add two timestamps */
static fdelay_time_t ts_add(fdelay_time_t a, fdelay_time_t b)
{
    a.frac += b.frac;
    if(a.frac >= 4096)
    {
        a.frac -= 4096;
		a.coarse++;
    }
    a.coarse += b.coarse;
    if(a.coarse >= 125000000)
    {
        a.coarse -= 125000000;
	a.utc ++;
    }
    a.utc += b.utc;
    return a;
}

fdelay_time_t ts_sub(fdelay_time_t a, fdelay_time_t b)
{
	a.frac -= b.frac;
  	if(a.frac < 0)
   	{
  		a.frac += 4096;
     	a.coarse--;
    }
    a.coarse -= b.coarse;
    if(a.coarse < 0)
    {
    	a.coarse += 125000000;
        a.utc --;
    }
    a.utc-=b.utc;
    return a;
}
             
             
                                                                    

#define MIN_SPACING_US 5000LL
#define MAX_SPACING_US 10000LL

int64_t rrand64(int64_t min, int64_t max)
{
	int i;
	uint64_t tmp = 0;
	for(i=0;i<32;i++) 
		tmp ^= ((uint64_t)random()) << i;
	
	tmp %= (max-min+1);
	return min+tmp;
}

int armed = 0;

void produce_pulses(fdelay_device_t *b)
{
	int64_t delta;
	fdelay_time_t t, td;

	if(armed && !fdelay_channel_triggered(b, 1))
		return ;

	fdelay_get_time(b, &t);
	t.frac = 0;
	delta = rrand64(MIN_SPACING_US * 1000000LL, MAX_SPACING_US*1000000LL);
	td = fdelay_from_picos(delta);
	
//	printf("TD %lld:%d:%d\n", t.utc, t.coarse, t.frac);
	t = ts_add(t, td);

	fdelay_configure_pulse_gen(b, 1, 1, t, 1*1000000, 0, 1);
	pqueue_push(&outgoing, &t);
	
	armed = 1;

}



void enable_wr(fdelay_device_t *b, int index)
{
	int lock_retries = 50;
	printf("Locking to WR network [board=%d]...", index);
	fflush(stdout);
	fdelay_configure_sync(b, FDELAY_SYNC_LOCAL);
	sleep(2);
	return ;
	fdelay_configure_sync(b, FDELAY_SYNC_WR);

	while(fdelay_check_sync(b) <= 0)
	{
		printf(".");
	    fflush(stdout);
	    sleep(1);
	    if(lock_retries-- == 0)
	    {
				fprintf(stderr," WR lock timed out\n");
				exit(1);
	    }
	}

	printf("\n");
	fflush(stdout);
}


176
int configure_board(fdelay_device_t *b, int argc, char *argv[])
177 178
{
	
179 180 181 182 183 184 185
	if(spec_fdelay_create(b, 1, NULL) < 0)
	{
		printf("Probe failed\n");
		exit(-1);
	}

	if(fdelay_init(b, 0) < 0)
186 187 188 189 190 191 192 193 194 195 196 197
	{
		printf("Init failed\n");
		exit(-1);
	}

	enable_wr(b, 0);
	
	fdelay_configure_trigger(b, 0, 1);	

	fdelay_set_user_offset(b, 1, INPUT_OFFSET);
	fdelay_set_user_offset(b, 0, OUTPUT_OFFSET);

198
	fdelay_raw_readout(b, 1);	
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	fdelay_configure_readout(b, 0);	
	fdelay_configure_readout(b, 1);	
	fdelay_configure_trigger(b, 1, 1);	

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

    return 0;
}

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

    while(fdelay_read(b, &t, 1) == 1)
    {	    
		pqueue_push(&incoming, &t);
	
		t_ps = (t.coarse * 8000LL) + ((t.frac * 8000LL) >> 12);
#if 0
		printf("card 0x%04x, seq %5i: time %lli s, %lli.%03lli ns [count %d] ", 0, t.seq_id, t.utc, t_ps / 1000LL, t_ps % 1000LL, (t.raw.tsbcr >> 10) & 0x3ff);
		
		if(((prev_seq + 1) & 0xffff) != (t.seq_id & 0xffff))
		{
			printf("MISMATCH\n");
		} else printf("\n");
#endif
		
		prev_seq = t.seq_id;
    }
}

void verify()
{
	static int64_t delta_min = 10000000000LL, delta_max = -10000000000LL;
	static int event_count = 0;
	
 	if(!pqueue_empty(&outgoing) && !pqueue_empty(&incoming))
 	{
 	 	fdelay_time_t to, ti, delta;
 	 	int64_t delta_ps;
 	 	int new_ext = 0;
 	 	pqueue_pop(&outgoing, &to);
 	 	pqueue_pop(&incoming, &ti);
 	 	
 	 	delta = ts_sub(to, ti);
 	 	delta_ps = fdelay_to_picos(delta);
 	 	
 	 	if(delta_ps < delta_min)
 	 	{
 	 		delta_min = delta_ps;
 	 		new_ext = 1;
 	 	} else if (delta_ps > delta_max)
 	 	{
 	 		delta_max = delta_ps;
 	 		new_ext = 1;
 	 	}
 	 	 event_count++;
 	 	if(new_ext || (event_count % 10) == 0)
 	 	{
 	 	 
 	 	  	 	printf("events %.8d [q %.5lld:%.8d:%.5d i %.5lld:%.8d:%.5d] delta: %.10lldps [min %.10lldps max %.10lldps span %.10lldps] raw %d %d %d %d \n",
 	 	  	 	event_count,
	 	 	to.utc, to.coarse,to.frac,
	 	 	ti.utc, ti.coarse,ti.frac,
 		 	delta_ps,  delta_min ,delta_max, delta_max-delta_min,
 		 	ti.raw.coarse, ti.raw.start_offset, ti.raw.subcycle_offset, ti.raw.frac-30000);
        	fflush(stdout);
 	 	 //	printf("NewErrorRange: %lld-%lldps\n", delta_min, delta_max); 	 	 	
 	 	 
 	 	}
 	 	
 	 	
 	}
}

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

283
	configure_board(&b, argc, argv);
284 285 286 287 288 289 290 291 292 293
	pqueue_clear(&incoming);
	pqueue_clear(&outgoing);
	
	for(;;)
	{
		produce_pulses(&b);
		handle_readout(&b);
		verify();
	}
}