# HG changeset patch # User Daniele Nicolodi # Date 1396278218 -7200 # Node ID 9e0c3541104bd3ed0113863a609182973ae2fcc9 # Parent c700a2d38fb84b37614ab0593630558b50dbf0b5 Move common AD99xx code to dds.{ch} diff -r c700a2d38fb8 -r 9e0c3541104b ad9912.c --- a/ad9912.c Mon Mar 31 17:03:38 2014 +0200 +++ b/ad9912.c Mon Mar 31 17:03:38 2014 +0200 @@ -1,12 +1,7 @@ #ifdef _CVI_ +#include #include #include -#include -#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 #include @@ -19,117 +14,29 @@ #include #include #include -#include #include #endif +#include "dds.h" #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)(1ULL << 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; + +#ifdef _CVI_ + WSADATA wsdata; + WSAStartup(MAKEWORD(2,2), &wsdata); +#endif + + d->hostname = strdup(hostname); + d->port = 1234; + d->clock = clock; host = gethostbyname(hostname); if (host == NULL) @@ -141,13 +48,12 @@ 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); + addr.sin_port = htons(d->port); r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); if (r < 0) return -3; -#endif - + d->fd = sock; /* check compatibility */ @@ -169,6 +75,9 @@ { int r; uint64 value = ftw(d->clock, f); + + /* ad9912 ignores ftw last bit */ + value = value & ~1ULL; r = command(d->fd, "SET CH%d:FTW0 0x%llX", channel, value); if (r < 0) diff -r c700a2d38fb8 -r 9e0c3541104b ad9912.h --- a/ad9912.h Mon Mar 31 17:03:38 2014 +0200 +++ b/ad9912.h Mon Mar 31 17:03:38 2014 +0200 @@ -6,7 +6,7 @@ struct ad9912 { char *hostname; int port; - unsigned fd; + int fd; double clock; double frequency[4]; };