Mercurial > hg > fxanalyse
view ad9912.c @ 197:79db2c87d1b0
New build. Do not install configuration file.
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 31 Mar 2014 17:03:37 +0200 |
parents | 87dae6d62a61 |
children | f8118b90490e |
line wrap: on
line source
#ifdef _CVI_ #include <ansi_c.h> #include <toolbox.h> #include <tcpsupp.h> #define TIMEOUT 0 #define send(fd, buf, len, f) ClientTCPWrite(fd, buf, len, TIMEOUT) #define recv(fd, buf, len, f) ClientTCPRead(fd, buf, len, TIMEOUT) #define usleep(t) Delay(t / 1000000.0) #define strdup(s) StrDup(s) #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 "ad9912.h" #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)(1 << 48) / clock); ftw = ftw & ~1ULL; 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; } static 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; } static 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; } static 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; } int ad9912_init(struct ad9912 *d, const char *hostname, double clock) { int n, r; char buffer[256]; d->hostname = StrDup(hostname); d->port = 1234; d->clock = clock; #ifdef _CVI_ unsigned int sock; r = ConnectToTCPServer(&sock, d->port, d->hostname, NULL, NULL, TIMEOUT); if (r < 0) return -1; #else int sock; struct sockaddr_in addr; struct hostent* host; 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(port); r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); if (r < 0) return -3; #endif 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 -1; return 0; } int ad9912_set_frequency(struct ad9912 *d, unsigned channel, double f) { int r; uint64 value = ftw(d->clock, f); r = command(d->fd, "SET CH%d:FTW0 0x%llX", channel, f); if (r < 0) return r; d->frequency[channel] = freq(d->clock, value); return 0; } int ad9912_get_frequency(struct ad9912 *d, unsigned channel, double *f) { int r, n; uint64 value; char buffer[256]; n = snprintf(buffer, sizeof(buffer), "GET CH%d:FTW0", channel); 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[channel] = freq(d->clock, value); if (f) *f = d->frequency[channel]; return 0; } /** * Ramp DDS frequency from @f1 to @f2 in steps @fstep with 0.01 * seconds delay after each step. */ int ad9912_ramp_frequency2(struct ad9912 *d, unsigned channel, double f1, double f2, double fstep) { const int delay = 10000; /* f2 > f1 */ while ((f2 - f1) > fstep) { f1 += fstep; ad9912_set_frequency(d, channel, f1); usleep(delay); } /* f2 < f1 */ while ((f1 - f2) > fstep) { f1 -= fstep; ad9912_set_frequency(d, channel, f1); usleep(delay); } /* final adjustment */ ad9912_set_frequency(d, channel, f2); return 0; } int ad9912_ramp_frequency(struct ad9912 *d, unsigned channel, double f, double fstep) { return ad9912_ramp_frequency2(d, channel, d->frequency[channel], f, fstep); }