annotate ad9956.c @ 266:dfbee05fe464

Fix beatnote sign measurement Breakage was caused by the change of the LO, HG, SR, enum values to use them as array indexes assigning to LO value 0 and thus flase in boolean context.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 09 Jul 2015 23:05:43 +0200
parents 8d9a4c5eb7a4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 #ifdef _CVI_
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2 #include <ansi_c.h>
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
3 #include <toolbox.h>
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4 #else
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 #include <stdio.h>
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #include <stdlib.h>
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7 #include <unistd.h>
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
8 #include <string.h>
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 #include <errno.h>
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
10 #include <math.h>
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 #endif
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13 #include "dds.h"
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
14 #include "xsocket.h"
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 #include "ad9956.h"
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 int ad9956_init(struct ad9956 *d, const char *hostname, double clock)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19 {
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
20 int r;
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 char buffer[256];
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
22 uint64 value;
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
24 d->sock = xsocket(hostname, 1234);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 d->profile = 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 d->clock = clock;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
28 r = xconnect(d->sock);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
29 if (r < 0)
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
30 return r;
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
32 /* check compatibility */
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
33 r = xask(d->sock, "REVISION", strlen("REVISION"), buffer, sizeof(buffer));
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
35 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
36 if (atoi(buffer) != REVISION)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
37 return -EINVAL;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
38
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
39 /* disable clock divider */
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
40 r = xask(d->sock, "GET CFR2", strlen("GET CFR2"), buffer, sizeof(buffer));
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
41 if (r < 0)
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
42 return r;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
43
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
44 r = strtouint64(buffer, &value);
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
45 if (r < 0)
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
46 return r;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
47
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
48 value = value | (1ULL << 16);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
49 r = xcommand(d->sock, "SET CFR2 0x%X", value);
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
50 if (r < 0)
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
51 return r;
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
52
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57 int ad9956_set_frequency(struct ad9956 *d, double f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 {
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 int r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 uint64 value = ftw(d->clock, f);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
62 r = xcommand(d->sock, "SET PCR%d 0x%llX", d->profile, value);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66 d->frequency = freq(d->clock, value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
71
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72 int ad9956_get_frequency(struct ad9956 *d, double *f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 {
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 int r, n;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 uint64 value;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76 char buffer[256];
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
78 n = snprintf(buffer, sizeof(buffer), "GET PCR%d", d->profile);
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
79 r = xask(d->sock, buffer, n, buffer, sizeof(buffer));
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
81 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
82
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
83 r = strtouint64(buffer, &value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
84 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
85 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
86
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87 d->frequency = freq(d->clock, value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88 if (f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
89 *f = d->frequency;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
90
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
91 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
92 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
93
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
94
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
95 int ad9956_set_sweep_rate(struct ad9956 *d, double rate)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96 {
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
97 int64 value = (int64)ftw(d->clock, fabs(rate * 0.01));
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
98 if (rate < 0.0)
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
99 value = -value;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
100
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
101 return xcommand(d->sock, "SWEEP SET DFTW %lld", value);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
102 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
103
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
104
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
105 int ad9956_sweep_start(struct ad9956 *d)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
106 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
107 return xcommand(d->sock, "SWEEP START");
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
108 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
109
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
110
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
111 int ad9956_sweep_stop(struct ad9956 *d)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
112 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
113 return xcommand(d->sock, "SWEEP STOP");
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
114 }