annotate ad9956.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
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 */
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
38 r = msend(d->sock, "REVISION", strlen("REVISION"));
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;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
41 r = mrecv(d->sock, buffer, sizeof(buffer));
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 return r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44 if (atoi(buffer) != REVISION)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45 return -EINVAL;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
46
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
47 /* disable clock divider */
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
48 r = msend(d->sock, "GET CFR2", strlen("GET CFR2"));
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
49 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
50 return r;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
51 r = mrecv(d->sock, 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
52 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
53 return r;
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
54 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
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 value = value | (1ULL << 16);
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
58 r = command(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
59 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
60 return r;
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
61
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 return 0;
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
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 int ad9956_set_frequency(struct ad9956 *d, double f)
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 int r;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 uint64 value = ftw(d->clock, f);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
71 r = command(d->sock, "SET PCR%d 0x%llX", d->profile, value);
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 return r;
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 d->frequency = freq(d->clock, value);
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 return 0;
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
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
81 int ad9956_get_frequency(struct ad9956 *d, double *f)
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 int r, n;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
84 uint64 value;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
85 char buffer[256];
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 n = snprintf(buffer, sizeof(buffer), "GET PCR%d", d->profile);
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
88 r = msend(d->sock, buffer, n);
206
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
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
92 r = mrecv(d->sock, buffer, sizeof(buffer));
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
93 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
94 return r;
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 r = strtouint64(buffer, &value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
97 if (r < 0)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
98 return r;
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 d->frequency = freq(d->clock, value);
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
101 if (f)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
102 *f = d->frequency;
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 return 0;
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
105 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
106
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 int ad9956_set_sweep_rate(struct ad9956 *d, double rate)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
109 {
213
fcc988c6f841 Fix ad9956 sweep rate setting. Disable clock divider on init. Simplify
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 206
diff changeset
110 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
111 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
112 value = -value;
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
113
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
114 return command(d->sock, "SWEEP SET DFTW %lld", value);
206
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
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
117
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
118 int ad9956_sweep_start(struct ad9956 *d)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
119 {
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
120 return command(d->sock, "SWEEP START");
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
121 }
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
122
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
123
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
124 int ad9956_sweep_stop(struct ad9956 *d)
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
125 {
258
5296f3bcd160 Implement DDS clients reconnect
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 213
diff changeset
126 return command(d->sock, "SWEEP STOP");
206
c700a2d38fb8 New AD9956 interface
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
127 }