changeset 206:c700a2d38fb8

New AD9956 interface
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:38 +0200
parents 31093786d41d
children 9e0c3541104b
files ad9956.c ad9956.h dds.c dds.h
diffstat 4 files changed, 285 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ad9956.c	Mon Mar 31 17:03:38 2014 +0200
@@ -0,0 +1,151 @@
+#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;	
+
+#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;
+
+	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)
+{
+	int r;
+	uint64 value = ftw(d->clock, rate);
+	
+	r = command(d->fd, "SWEEP SET DFTW %lld", d->profile, value);
+	if (r < 0)
+		return r;
+
+	return 0;
+}
+
+
+int ad9956_sweep_start(struct ad9956 *d)
+{
+	int r;
+
+	r = command(d->fd, "SWEEP START");
+	if (r < 0)
+		return r;
+
+	return 0;
+}
+
+
+int ad9956_sweep_stop(struct ad9956 *d)
+{
+	int r;
+
+	r = command(d->fd, "SWEEP STOP");
+	if (r < 0)
+		return r;
+
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ad9956.h	Mon Mar 31 17:03:38 2014 +0200
@@ -0,0 +1,22 @@
+#ifndef __AD9956_H__
+#define __AD9956_H__
+
+#define REVISION 1
+
+struct ad9956 {
+	char *hostname;
+	int port;
+	int profile;
+	int fd;
+	double clock;
+	double frequency;
+};
+
+int ad9956_init(struct ad9956 *d, const char *hostname, double clock);
+int ad9956_get_frequency(struct ad9956 *d, double *f);
+int ad9956_set_frequency(struct ad9956 *d, double f);
+int ad9956_set_sweep_rate(struct ad9956 *d, double rate);
+int ad9956_sweep_start(struct ad9956 *d);
+int ad9956_sweep_stop(struct ad9956 *d);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dds.c	Mon Mar 31 17:03:38 2014 +0200
@@ -0,0 +1,76 @@
+#ifdef _CVI_
+#include <winsock2.h>
+#include <ansi_c.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 <math.h>
+#include <stdarg.h>
+#endif
+
+#include "dds.h"
+
+int msend(int fd, char *buffer, int n)
+{
+	int r;
+
+	buffer[n++] = '\r';
+	buffer[n++] = '\n';
+
+	r = send(fd, buffer, n, 0);
+	if (r < 0)
+		return r;     
+
+	return 0;
+}
+
+
+int mrecv(int fd, char *buffer, int len)
+{
+	int n;
+
+	n = recv(fd, buffer, len, 0);
+	if (n < 0)
+		return n;
+
+	if ((buffer[--n] != '\n') || (buffer[--n] != '\r'))
+		return -1;
+
+	buffer[n] = '\0';
+
+	return n;
+}
+
+
+int command(int fd, char *frmt, ...)
+{
+	int r, n;
+	char buffer[1024];
+	va_list v;
+
+	va_start(v, frmt);
+	n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v);
+	va_end(v);
+
+	r = msend(fd, buffer, n);
+	if (r < 0)
+		return r;
+
+	r = mrecv(fd, buffer, sizeof(buffer));
+	if (r < 0)
+		return r;
+
+	if (! streq(buffer, "OK"))
+		return -1;
+
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dds.h	Mon Mar 31 17:03:38 2014 +0200
@@ -0,0 +1,36 @@
+#ifndef __FXANALYSE_DDS_H__
+#define __FXANALYSE_DDS_H__
+
+#define usleep(t) Delay((t) / 1000000.0)
+#define strdup(s) StrDup(s)
+#define streq(x, y) (strcmp((x), (y)) == 0)
+
+typedef unsigned long long uint64;
+
+static inline uint64 ftw(double clock, double freq)
+{
+	uint64 ftw = freq * ((double)(1ULL << 48) / clock);
+	return ftw;
+}
+
+static inline double freq(double clock, uint64 ftw)
+{
+	double freq = (double)ftw * (clock / (double)(1ULL << 48));
+	return freq;
+}
+
+static inline int strtouint64(const char *str, uint64 *v)
+{
+	char *end;
+	*v = strtoull(str, &end, 0);
+	if (*end != '\0')
+		return -1;
+	return 0;
+}
+
+int msend(int fd, char *buffer, int n);
+int mrecv(int fd, char *buffer, int len);
+int command(int fd, char *frmt, ...);
+
+#endif
+