Mercurial > hg > fxanalyse
comparison ad9912.c @ 207:9e0c3541104b
Move common AD99xx code to dds.{ch}
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 31 Mar 2014 17:03:38 +0200 |
parents | f8118b90490e |
children | 5296f3bcd160 |
comparison
equal
deleted
inserted
replaced
206:c700a2d38fb8 | 207:9e0c3541104b |
---|---|
1 #ifdef _CVI_ | 1 #ifdef _CVI_ |
2 #include <winsock2.h> | |
2 #include <ansi_c.h> | 3 #include <ansi_c.h> |
3 #include <toolbox.h> | 4 #include <toolbox.h> |
4 #include <tcpsupp.h> | |
5 #define TIMEOUT 0 | |
6 #define send(fd, buf, len, f) ClientTCPWrite(fd, buf, len, TIMEOUT) | |
7 #define recv(fd, buf, len, f) ClientTCPRead(fd, buf, len, TIMEOUT) | |
8 #define usleep(t) Delay(t / 1000000.0) | |
9 #define strdup(s) StrDup(s) | |
10 #else | 5 #else |
11 #include <stdio.h> | 6 #include <stdio.h> |
12 #include <stdlib.h> | 7 #include <stdlib.h> |
13 #include <unistd.h> | 8 #include <unistd.h> |
14 #include <errno.h> | 9 #include <errno.h> |
17 #include <sys/socket.h> | 12 #include <sys/socket.h> |
18 #include <netinet/in.h> | 13 #include <netinet/in.h> |
19 #include <arpa/inet.h> | 14 #include <arpa/inet.h> |
20 #include <netdb.h> | 15 #include <netdb.h> |
21 #include <ctype.h> | 16 #include <ctype.h> |
22 #include <math.h> | |
23 #include <stdarg.h> | 17 #include <stdarg.h> |
24 #endif | 18 #endif |
25 | 19 |
20 #include "dds.h" | |
26 #include "ad9912.h" | 21 #include "ad9912.h" |
27 | |
28 #define streq(x, y) (strcmp((x), (y)) == 0) | |
29 | |
30 typedef unsigned long long uint64; | |
31 | |
32 | |
33 static inline uint64 ftw(double clock, double freq) | |
34 { | |
35 uint64 ftw = freq * ((double)(1ULL << 48) / clock); | |
36 ftw = ftw & ~1ULL; | |
37 return ftw; | |
38 } | |
39 | |
40 | |
41 static inline double freq(double clock, uint64 ftw) | |
42 { | |
43 double freq = (double)ftw * (clock / (double)(1ULL << 48)); | |
44 return freq; | |
45 } | |
46 | |
47 | |
48 static inline int strtouint64(const char *str, uint64 *v) | |
49 { | |
50 char *end; | |
51 *v = strtoull(str, &end, 0); | |
52 if (*end != '\0') | |
53 return -1; | |
54 return 0; | |
55 } | |
56 | |
57 | |
58 static int msend(int fd, char *buffer, int n) | |
59 { | |
60 int r; | |
61 | |
62 buffer[n++] = '\r'; | |
63 buffer[n++] = '\n'; | |
64 | |
65 r = send(fd, buffer, n, 0); | |
66 if (r < 0) | |
67 return r; | |
68 | |
69 return 0; | |
70 } | |
71 | |
72 | |
73 static int mrecv(int fd, char *buffer, int len) | |
74 { | |
75 int n; | |
76 | |
77 n = recv(fd, buffer, len, 0); | |
78 if (n < 0) | |
79 return n; | |
80 | |
81 if ((buffer[--n] != '\n') || (buffer[--n] != '\r')) | |
82 return -1; | |
83 | |
84 buffer[n] = '\0'; | |
85 | |
86 return n; | |
87 } | |
88 | |
89 | |
90 static int command(int fd, char *frmt, ...) | |
91 { | |
92 int r, n; | |
93 char buffer[1024]; | |
94 va_list v; | |
95 | |
96 va_start(v, frmt); | |
97 n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); | |
98 va_end(v); | |
99 | |
100 r = msend(fd, buffer, n); | |
101 if (r < 0) | |
102 return r; | |
103 | |
104 r = mrecv(fd, buffer, sizeof(buffer)); | |
105 if (r < 0) | |
106 return r; | |
107 | |
108 if (! streq(buffer, "OK")) | |
109 return -1; | |
110 | |
111 return 0; | |
112 } | |
113 | 22 |
114 | 23 |
115 int ad9912_init(struct ad9912 *d, const char *hostname, double clock) | 24 int ad9912_init(struct ad9912 *d, const char *hostname, double clock) |
116 { | 25 { |
117 int n, r; | 26 int n, r; |
118 char buffer[256]; | 27 char buffer[256]; |
119 | |
120 d->hostname = StrDup(hostname); | |
121 d->port = 1234; | |
122 d->clock = clock; | |
123 | |
124 #ifdef _CVI_ | |
125 unsigned int sock; | |
126 r = ConnectToTCPServer(&sock, d->port, d->hostname, NULL, NULL, TIMEOUT); | |
127 if (r < 0) | |
128 return -1; | |
129 #else | |
130 int sock; | 28 int sock; |
131 struct sockaddr_in addr; | 29 struct sockaddr_in addr; |
132 struct hostent* host; | 30 struct hostent* host; |
31 | |
32 #ifdef _CVI_ | |
33 WSADATA wsdata; | |
34 WSAStartup(MAKEWORD(2,2), &wsdata); | |
35 #endif | |
36 | |
37 d->hostname = strdup(hostname); | |
38 d->port = 1234; | |
39 d->clock = clock; | |
133 | 40 |
134 host = gethostbyname(hostname); | 41 host = gethostbyname(hostname); |
135 if (host == NULL) | 42 if (host == NULL) |
136 return -1; | 43 return -1; |
137 | 44 |
139 if (sock < 0) | 46 if (sock < 0) |
140 return -2; | 47 return -2; |
141 | 48 |
142 addr.sin_family = host->h_addrtype; | 49 addr.sin_family = host->h_addrtype; |
143 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); | 50 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); |
144 addr.sin_port = htons(port); | 51 addr.sin_port = htons(d->port); |
145 | 52 |
146 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); | 53 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); |
147 if (r < 0) | 54 if (r < 0) |
148 return -3; | 55 return -3; |
149 #endif | 56 |
150 | |
151 d->fd = sock; | 57 d->fd = sock; |
152 | 58 |
153 /* check compatibility */ | 59 /* check compatibility */ |
154 n = snprintf(buffer, sizeof(buffer), "REVISION"); | 60 n = snprintf(buffer, sizeof(buffer), "REVISION"); |
155 r = msend(d->fd, buffer, n); | 61 r = msend(d->fd, buffer, n); |
167 | 73 |
168 int ad9912_set_frequency(struct ad9912 *d, unsigned channel, double f) | 74 int ad9912_set_frequency(struct ad9912 *d, unsigned channel, double f) |
169 { | 75 { |
170 int r; | 76 int r; |
171 uint64 value = ftw(d->clock, f); | 77 uint64 value = ftw(d->clock, f); |
78 | |
79 /* ad9912 ignores ftw last bit */ | |
80 value = value & ~1ULL; | |
172 | 81 |
173 r = command(d->fd, "SET CH%d:FTW0 0x%llX", channel, value); | 82 r = command(d->fd, "SET CH%d:FTW0 0x%llX", channel, value); |
174 if (r < 0) | 83 if (r < 0) |
175 return r; | 84 return r; |
176 | 85 |