view ad9956.c @ 262:ebbe0f198322

Fix DDS client connection retry
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 17:16:11 +0200
parents 5296f3bcd160
children 8d9a4c5eb7a4
line wrap: on
line source

#ifdef _CVI_
#include <ansi_c.h>
#include <toolbox.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#endif

#include "dds.h"
#include "xsocket.h"
#include "ad9956.h"


int ad9956_init(struct ad9956 *d, const char *hostname, double clock)
{
	int r;
	char buffer[256];
	uint64 value;

#ifdef _CVI_
	WSADATA wsdata;
	WSAStartup(MAKEWORD(2,2), &wsdata);	
#endif

	d->sock = xsocket(hostname, 1234);
	d->profile = 0;
	d->clock = clock;

	r = xconnect(d->sock);
	if (r < 0)
		return r;

	/* check compatibility */
	r = xask(d->sock, "REVISION", strlen("REVISION"), buffer, sizeof(buffer));
	if (r < 0)
		return r;
	if (atoi(buffer) != REVISION)
		return -EINVAL;

	/* disable clock divider */
	r = xask(d->sock, "GET CFR2", strlen("GET CFR2"), buffer, sizeof(buffer));
	if (r < 0)
		return r;

	r = strtouint64(buffer, &value);
	if (r < 0)
		return r;
	
	value = value | (1ULL << 16);
	r = xcommand(d->sock, "SET CFR2 0x%X", value);
	if (r < 0)
		return r;

	return 0;
}


int ad9956_set_frequency(struct ad9956 *d, double f)
{
	int r;
	uint64 value = ftw(d->clock, f);

	r = xcommand(d->sock, "SET PCR%d 0x%llX", d->profile, value);
	if (r < 0)
		return r;

	d->frequency = freq(d->clock, value);
     
	return 0;
}


int ad9956_get_frequency(struct ad9956 *d, double *f)
{
	int r, n;
	uint64 value;
	char buffer[256];

	n = snprintf(buffer, sizeof(buffer), "GET PCR%d", d->profile);
	r = xask(d->sock, buffer, n, buffer, sizeof(buffer));
	if (r < 0)
		return r;

	r = strtouint64(buffer, &value);
	if (r < 0)
		return r;

	d->frequency = freq(d->clock, value);
	if (f)
		*f = d->frequency;

	return 0;
}
     

int ad9956_set_sweep_rate(struct ad9956 *d, double rate)
{
	int64 value = (int64)ftw(d->clock, fabs(rate * 0.01));
	if (rate < 0.0)
		value = -value;
	
	return xcommand(d->sock, "SWEEP SET DFTW %lld", value);
}


int ad9956_sweep_start(struct ad9956 *d)
{
	return xcommand(d->sock, "SWEEP START");
}


int ad9956_sweep_stop(struct ad9956 *d)
{
	return xcommand(d->sock, "SWEEP STOP");
}