comparison ad9956.c @ 206:c700a2d38fb8

New AD9956 interface
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:38 +0200
parents
children fcc988c6f841
comparison
equal deleted inserted replaced
205:31093786d41d 206:c700a2d38fb8
1 #ifdef _CVI_
2 #include <winsock2.h>
3 #include <ansi_c.h>
4 #include <toolbox.h>
5 #else
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netdb.h>
16 #include <ctype.h>
17 #include <stdarg.h>
18 #endif
19
20 #include "dds.h"
21 #include "ad9956.h"
22
23
24 int ad9956_init(struct ad9956 *d, const char *hostname, double clock)
25 {
26 int sock, n, r;
27 char buffer[256];
28 struct sockaddr_in addr;
29 struct hostent* host;
30
31 #ifdef _CVI_
32 WSADATA wsdata;
33 WSAStartup(MAKEWORD(2,2), &wsdata);
34 #endif
35
36 d->hostname = strdup(hostname);
37 d->port = 1234;
38 d->profile = 0;
39 d->clock = clock;
40
41 host = gethostbyname(hostname);
42 if (host == NULL)
43 return -1;
44
45 sock = socket(AF_INET, SOCK_STREAM, 0);
46 if (sock < 0)
47 return -2;
48
49 addr.sin_family = host->h_addrtype;
50 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
51 addr.sin_port = htons(d->port);
52
53 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
54 if (r < 0)
55 return -3;
56
57 d->fd = sock;
58
59 /* check compatibility */
60 n = snprintf(buffer, sizeof(buffer), "REVISION");
61 r = msend(d->fd, buffer, n);
62 if (r < 0)
63 return r;
64 r = mrecv(d->fd, buffer, sizeof(buffer));
65 if (r < 0)
66 return r;
67 if (atoi(buffer) != REVISION)
68 return -EINVAL;
69
70 return 0;
71 }
72
73
74 int ad9956_set_frequency(struct ad9956 *d, double f)
75 {
76 int r;
77 uint64 value = ftw(d->clock, f);
78
79 r = command(d->fd, "SET PCR%d 0x%llX", d->profile, value);
80 if (r < 0)
81 return r;
82
83 d->frequency = freq(d->clock, value);
84
85 return 0;
86 }
87
88
89 int ad9956_get_frequency(struct ad9956 *d, double *f)
90 {
91 int r, n;
92 uint64 value;
93 char buffer[256];
94
95 n = snprintf(buffer, sizeof(buffer), "GET PCR%d", d->profile);
96
97 r = msend(d->fd, buffer, n);
98 if (r < 0)
99 return r;
100
101 r = mrecv(d->fd, buffer, sizeof(buffer));
102 if (r < 0)
103 return r;
104
105 r = strtouint64(buffer, &value);
106 if (r < 0)
107 return r;
108
109 d->frequency = freq(d->clock, value);
110 if (f)
111 *f = d->frequency;
112
113 return 0;
114 }
115
116
117 int ad9956_set_sweep_rate(struct ad9956 *d, double rate)
118 {
119 int r;
120 uint64 value = ftw(d->clock, rate);
121
122 r = command(d->fd, "SWEEP SET DFTW %lld", d->profile, value);
123 if (r < 0)
124 return r;
125
126 return 0;
127 }
128
129
130 int ad9956_sweep_start(struct ad9956 *d)
131 {
132 int r;
133
134 r = command(d->fd, "SWEEP START");
135 if (r < 0)
136 return r;
137
138 return 0;
139 }
140
141
142 int ad9956_sweep_stop(struct ad9956 *d)
143 {
144 int r;
145
146 r = command(d->fd, "SWEEP STOP");
147 if (r < 0)
148 return r;
149
150 return 0;
151 }