annotate ad9956.c @ 262:ebbe0f198322

Fix DDS client connection retry
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 17:16:11 +0200
parents 5296f3bcd160
children 8d9a4c5eb7a4
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
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24 #ifdef _CVI_
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 WSADATA wsdata;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 WSAStartup(MAKEWORD(2,2), &wsdata);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27 #endif
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
28
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
29 d->sock = xsocket(hostname, 1234);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
30 d->profile = 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31 d->clock = clock;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
32
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
33 r = xconnect(d->sock);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 if (r < 0)
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
35 return r;
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
36
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
37 /* check compatibility */
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
38 r = xask(d->sock, "REVISION", strlen("REVISION"), buffer, sizeof(buffer));
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
39 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
40 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
41 if (atoi(buffer) != REVISION)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 return -EINVAL;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
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 /* disable clock divider */
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
45 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
46 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
47 return r;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
48
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
49 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
50 if (r < 0)
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
51 return r;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
52
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
53 value = value | (1ULL << 16);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
54 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
55 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
56 return r;
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
57
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 int ad9956_set_frequency(struct ad9956 *d, double f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63 {
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 int r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 uint64 value = ftw(d->clock, f);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
67 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
68 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 return r;
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 d->frequency = freq(d->clock, value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77 int ad9956_get_frequency(struct ad9956 *d, double *f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
78 {
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
79 int r, n;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80 uint64 value;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
81 char buffer[256];
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 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
84 r = xask(d->sock, buffer, n, buffer, sizeof(buffer));
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
85 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
86 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88 r = strtouint64(buffer, &value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
89 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
90 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
91
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
92 d->frequency = freq(d->clock, value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
93 if (f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
94 *f = d->frequency;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
95
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
97 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
98
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
99
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
100 int ad9956_set_sweep_rate(struct ad9956 *d, double rate)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
101 {
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
102 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
103 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
104 value = -value;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
105
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
106 return xcommand(d->sock, "SWEEP SET DFTW %lld", value);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
107 }
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 int ad9956_sweep_start(struct ad9956 *d)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
111 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
112 return xcommand(d->sock, "SWEEP START");
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
113 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
114
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
115
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
116 int ad9956_sweep_stop(struct ad9956 *d)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
117 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 258
diff changeset
118 return xcommand(d->sock, "SWEEP STOP");
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
119 }