view ad9956.c @ 241:65ab87e5a8d6

Add ZMQ library
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 12 Feb 2015 22:43:46 +0100
parents fcc988c6f841
children 5296f3bcd160
line wrap: on
line source

#ifdef _CVI_
#include <winsock2.h>
#include <ansi_c.h>
#include <toolbox.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <stdarg.h>
#endif

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


int ad9956_init(struct ad9956 *d, const char *hostname, double clock)
{
	int sock, n, r;
	char buffer[256];
	struct sockaddr_in addr;
	struct hostent* host;
	uint64 value;

#ifdef _CVI_
	WSADATA wsdata;
	WSAStartup(MAKEWORD(2,2), &wsdata);	
#endif
	
	d->hostname = strdup(hostname);
	d->port = 1234;
	d->profile = 0;
	d->clock = clock;

	host = gethostbyname(hostname);
	if (host == NULL)
		return -1;
  
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0)
		return -2;

	addr.sin_family = host->h_addrtype;
	memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
	addr.sin_port = htons(d->port);

	r = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
	if (r < 0)
		return -3;

	d->fd = sock;

	/* check compatibility */
	n = snprintf(buffer, sizeof(buffer), "REVISION");
	r = msend(d->fd, buffer, n);
	if (r < 0)
		return r;
	r = mrecv(d->fd, buffer, sizeof(buffer));
	if (r < 0)
		return r;
	if (atoi(buffer) != REVISION)
		return -EINVAL;

	/* disable clock divider */
	n = snprintf(buffer, sizeof(buffer), "GET CFR2");
	r = msend(d->fd, buffer, n);
	if (r < 0)
		return r;
	r = mrecv(d->fd, buffer, sizeof(buffer));
	if (r < 0)
		return r;
	r = strtouint64(buffer, &value);
	if (r < 0)
		return r;	
        value = value | (1ULL << 16);
	r = command(d->fd, "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 = command(d->fd, "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 = msend(d->fd, buffer, n);
	if (r < 0)
		return r;

	r = mrecv(d->fd, 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 command(d->fd, "SWEEP SET DFTW %lld", value);
}


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


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