annotate ad9912.c @ 259:6c748ed6a7c5

Stricter error checks ad9912 ramp frequency function
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 14:33:36 +0200
parents 5296f3bcd160
children ebbe0f198322
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;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
28
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
29 /* check compatibility */
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
30 r = msend(d->sock, "REVISION", strlen("REVISION"));
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
31 if (r < 0)
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
32 return r;
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
33 r = mrecv(d->sock, buffer, sizeof(buffer));
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
35 return r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
36 if (atoi(buffer) != REVISION)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
37 return -1;
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 return 0;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
40 }
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
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 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
44 {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45 int r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
46 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
47
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
48 /* ad9912 ignores ftw last bit */
9e0c3541104b Move common AD99xx code to dds.{ch}
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 201
diff changeset
49 value = value & ~1ULL;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
51 r = command(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
52 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53 return r;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 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
56
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57 return 0;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 }
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
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 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
62 {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63 int r, n;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 uint64 value;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 char buffer[256];
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67 n = snprintf(buffer, sizeof(buffer), "GET CH%d:FTW0", channel);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
69 r = msend(d->sock, buffer, n);
196
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
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 207
diff changeset
73 r = mrecv(d->sock, buffer, sizeof(buffer));
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 return r;
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 r = strtouint64(buffer, &value);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
78 if (r < 0)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
79 return r;
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 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
82 if (f)
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
83 *f = d->frequency[channel];
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 return 0;
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
86 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88
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 * 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
91 * 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
92 */
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
93 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
94 {
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
95 int r = 0;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96 const int delay = 10000;
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 /* f2 > f1 */
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
99 while ((f2 - f1) > fstep) {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
100 f1 += fstep;
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
101 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
102 if (r < 0)
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
103 return r;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
104 usleep(delay);
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
105 }
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 /* f2 < f1 */
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
108 while ((f1 - f2) > fstep) {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
109 f1 -= fstep;
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
110 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
111 if (r < 0)
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
112 return r;
196
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
113 usleep(delay);
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
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
116 /* final adjustment */
259
6c748ed6a7c5 Stricter error checks ad9912 ramp frequency function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
117 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
118 }
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
119
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
120
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
121 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
122 {
87dae6d62a61 New AD9912 client code to communicate with improved server code
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
123 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
124 }