annotate ad9912.c @ 271:4a2fa10b8421 default tip

Publish DDS and measured frequencies through ZMQ
author Daniele Nicolodi <daniele.nicolodi@nist.gov>
date Sat, 15 Aug 2020 18:22:42 -0600
parents ebbe0f198322
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 #ifdef _CVI_
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2 #include <ansi_c.h>
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
3 #include <toolbox.h>
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
4 #define usleep(t) Delay((t) / 1000000.0)
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 #else
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #include <stdio.h>
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7 #include <stdlib.h>
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8 #include <unistd.h>
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 #include <string.h>
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 #endif
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11
207
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
12 #include "dds.h"
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
13 #include "xsocket.h"
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 #include "ad9912.h"
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 int ad9912_init(struct ad9912 *d, const char *hostname, double clock)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 {
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
19 int r;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
20 char buffer[256];
207
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
21
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
22 d->clock = clock;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
23 d->sock = xsocket(hostname, 1234);
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
25 r = xconnect(d->sock);
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27 return r;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 259
diff changeset
28
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
29 /* check compatibility */
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 259
diff changeset
30 r = xask(d->sock, "REVISION", strlen("REVISION"), buffer, sizeof(buffer));
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
32 return r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
33 if (atoi(buffer) != REVISION)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 return -1;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
35
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
36 return 0;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
37 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
38
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
39
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
40 int ad9912_set_frequency(struct ad9912 *d, unsigned channel, double f)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
41 {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 int r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 uint64 value = ftw(d->clock, f);
207
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
44
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
45 /* ad9912 ignores ftw last bit */
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
46 value = value & ~1ULL;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 259
diff changeset
48 r = xcommand(d->sock, "SET CH%d:FTW0 0x%llX", channel, value);
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50 return r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 d->frequency[channel] = freq(d->clock, value);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54 return 0;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 int ad9912_get_frequency(struct ad9912 *d, unsigned channel, double *f)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 int r, n;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 uint64 value;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 char buffer[256];
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 n = snprintf(buffer, sizeof(buffer), "GET CH%d:FTW0", channel);
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 259
diff changeset
65 r = xask(d->sock, buffer, n, buffer, sizeof(buffer));
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67 return r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 r = strtouint64(buffer, &value);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
71 return r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 d->frequency[channel] = freq(d->clock, value);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 if (f)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 *f = d->frequency[channel];
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77 return 0;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
78 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
79
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
81 /**
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
82 * Ramp DDS frequency from @f1 to @f2 in steps @fstep with 0.01
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
83 * seconds delay after each step.
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
84 */
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
85 int ad9912_ramp_frequency2(struct ad9912 *d, unsigned channel, double f1, double f2, double fstep)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
86 {
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
87 int r = 0;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88 const int delay = 10000;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
89
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
90 /* f2 > f1 */
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
91 while ((f2 - f1) > fstep) {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
92 f1 += fstep;
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
93 r = ad9912_set_frequency(d, channel, f1);
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
94 if (r < 0)
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
95 return r;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96 usleep(delay);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
97 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
98
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
99 /* f2 < f1 */
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
100 while ((f1 - f2) > fstep) {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
101 f1 -= fstep;
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
102 r = ad9912_set_frequency(d, channel, f1);
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
103 if (r < 0)
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
104 return r;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
105 usleep(delay);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
106 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
107
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
108 /* final adjustment */
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
109 return ad9912_set_frequency(d, channel, f2);
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
110 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
111
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
112
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
113 int ad9912_ramp_frequency(struct ad9912 *d, unsigned channel, double f, double fstep)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
114 {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
115 return ad9912_ramp_frequency2(d, channel, d->frequency[channel], f, fstep);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
116 }