]>
Commit | Line | Data |
---|---|---|
e93cdf42 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <glib.h> | |
e93cdf42 BV |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
6f22a8ef UH |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
28 | #include "agilent-dmm.h" | |
e93cdf42 BV |
29 | |
30 | static const int hwopts[] = { | |
31 | SR_HWOPT_CONN, | |
32 | SR_HWOPT_SERIALCOMM, | |
33 | 0, | |
34 | }; | |
35 | ||
36 | static const int hwcaps[] = { | |
37 | SR_HWCAP_MULTIMETER, | |
38 | SR_HWCAP_LIMIT_SAMPLES, | |
39 | SR_HWCAP_LIMIT_MSEC, | |
40 | SR_HWCAP_CONTINUOUS, | |
41 | 0, | |
42 | }; | |
43 | ||
44 | static const char *probe_names[] = { | |
45 | "Probe", | |
46 | NULL, | |
47 | }; | |
48 | ||
8c0152f2 BV |
49 | extern const struct agdmm_job agdmm_jobs_u123x[]; |
50 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
51 | extern const struct agdmm_job agdmm_jobs_u125x[]; | |
52 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
e93cdf42 | 53 | |
74ac7d7f BV |
54 | /* This works on all the Agilent U12xxA series, although the |
55 | * U127xA can apparently also run at 19200/8n1. */ | |
56 | #define SERIALCOMM "9600/8n1" | |
57 | ||
e93cdf42 | 58 | static const struct agdmm_profile supported_agdmm[] = { |
74ac7d7f BV |
59 | { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x }, |
60 | { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
61 | { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
62 | { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
63 | { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
64 | { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
65 | { 0, NULL, NULL, NULL } | |
e93cdf42 BV |
66 | }; |
67 | ||
68 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
69 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
70 | ||
71 | /* Properly close and free all devices. */ | |
72 | static int clear_instances(void) | |
73 | { | |
74 | struct sr_dev_inst *sdi; | |
75 | struct drv_context *drvc; | |
76 | struct dev_context *devc; | |
77 | GSList *l; | |
78 | ||
79 | if (!(drvc = di->priv)) | |
80 | return SR_OK; | |
81 | ||
82 | drvc = di->priv; | |
83 | for (l = drvc->instances; l; l = l->next) { | |
84 | if (!(sdi = l->data)) | |
85 | continue; | |
86 | if (!(devc = sdi->priv)) | |
87 | continue; | |
88 | sr_serial_dev_inst_free(devc->serial); | |
89 | sr_dev_inst_free(sdi); | |
90 | } | |
91 | g_slist_free(drvc->instances); | |
92 | drvc->instances = NULL; | |
93 | ||
94 | return SR_OK; | |
95 | } | |
96 | ||
34f06b90 | 97 | static int hw_init(struct sr_context *sr_ctx) |
e93cdf42 BV |
98 | { |
99 | struct drv_context *drvc; | |
100 | ||
101 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
38d326e8 | 102 | sr_err("Driver context malloc failed."); |
886a52b6 | 103 | return SR_ERR_MALLOC; |
e93cdf42 BV |
104 | } |
105 | ||
1ebe4b4e | 106 | drvc->sr_ctx = sr_ctx; |
e93cdf42 BV |
107 | di->priv = drvc; |
108 | ||
109 | return SR_OK; | |
110 | } | |
111 | ||
e93cdf42 BV |
112 | static GSList *hw_scan(GSList *options) |
113 | { | |
114 | struct sr_dev_inst *sdi; | |
115 | struct drv_context *drvc; | |
116 | struct dev_context *devc; | |
117 | struct sr_hwopt *opt; | |
118 | struct sr_probe *probe; | |
109a3ba4 | 119 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 120 | GSList *l, *devices; |
109a3ba4 | 121 | int len, i; |
e93cdf42 BV |
122 | const char *conn, *serialcomm; |
123 | char *buf, **tokens; | |
124 | ||
125 | drvc = di->priv; | |
126 | drvc->instances = NULL; | |
127 | ||
128 | devices = NULL; | |
129 | conn = serialcomm = NULL; | |
130 | for (l = options; l; l = l->next) { | |
131 | opt = l->data; | |
132 | switch (opt->hwopt) { | |
133 | case SR_HWOPT_CONN: | |
134 | conn = opt->value; | |
135 | break; | |
136 | case SR_HWOPT_SERIALCOMM: | |
137 | serialcomm = opt->value; | |
138 | break; | |
139 | } | |
140 | } | |
74ac7d7f | 141 | if (!conn) |
e93cdf42 | 142 | return NULL; |
109a3ba4 BV |
143 | if (!serialcomm) |
144 | serialcomm = SERIALCOMM; | |
e93cdf42 | 145 | |
109a3ba4 | 146 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
e93cdf42 | 147 | return NULL; |
e93cdf42 | 148 | |
a54dd31e | 149 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 150 | return NULL; |
e93cdf42 | 151 | |
109a3ba4 BV |
152 | serial_flush(serial); |
153 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
154 | sr_err("Unable to send identification string: %s.", |
155 | strerror(errno)); | |
e93cdf42 BV |
156 | return NULL; |
157 | } | |
158 | ||
159 | len = 128; | |
886a52b6 UH |
160 | if (!(buf = g_try_malloc(len))) { |
161 | sr_err("Serial buffer malloc failed."); | |
162 | return NULL; | |
163 | } | |
109a3ba4 | 164 | serial_readline(serial, &buf, &len, 150); |
e93cdf42 BV |
165 | if (!len) |
166 | return NULL; | |
167 | ||
168 | tokens = g_strsplit(buf, ",", 4); | |
169 | if (!strcmp("Agilent Technologies", tokens[0]) | |
170 | && tokens[2] && tokens[3]) { | |
171 | for (i = 0; supported_agdmm[i].model; i++) { | |
172 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
173 | continue; | |
174 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0], | |
175 | tokens[1], tokens[3]))) | |
176 | return NULL; | |
177 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
886a52b6 | 178 | sr_err("Device context malloc failed."); |
e93cdf42 BV |
179 | return NULL; |
180 | } | |
181 | devc->profile = &supported_agdmm[i]; | |
109a3ba4 | 182 | devc->serial = serial; |
e93cdf42 BV |
183 | devc->cur_mq = -1; |
184 | sdi->priv = devc; | |
185 | sdi->driver = di; | |
186 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
187 | return NULL; | |
188 | sdi->probes = g_slist_append(sdi->probes, probe); | |
189 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
190 | devices = g_slist_append(devices, sdi); | |
191 | break; | |
192 | } | |
193 | } | |
194 | g_strfreev(tokens); | |
195 | g_free(buf); | |
196 | ||
109a3ba4 BV |
197 | serial_close(serial); |
198 | if (!devices) | |
199 | sr_serial_dev_inst_free(serial); | |
e93cdf42 BV |
200 | |
201 | return devices; | |
202 | } | |
203 | ||
204 | static GSList *hw_dev_list(void) | |
205 | { | |
206 | struct drv_context *drvc; | |
207 | ||
208 | drvc = di->priv; | |
209 | ||
210 | return drvc->instances; | |
211 | } | |
212 | ||
213 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
214 | { | |
215 | struct dev_context *devc; | |
216 | ||
217 | if (!(devc = sdi->priv)) { | |
38d326e8 | 218 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
219 | return SR_ERR_BUG; |
220 | } | |
221 | ||
a54dd31e | 222 | if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 223 | return SR_ERR; |
109a3ba4 | 224 | |
e93cdf42 BV |
225 | sdi->status = SR_ST_ACTIVE; |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
230 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
231 | { | |
232 | struct dev_context *devc; | |
233 | ||
234 | if (!(devc = sdi->priv)) { | |
38d326e8 | 235 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
236 | return SR_ERR_BUG; |
237 | } | |
238 | ||
239 | if (devc->serial && devc->serial->fd != -1) { | |
109a3ba4 | 240 | serial_close(devc->serial); |
e93cdf42 BV |
241 | sdi->status = SR_ST_INACTIVE; |
242 | } | |
243 | ||
244 | return SR_OK; | |
245 | } | |
246 | ||
247 | static int hw_cleanup(void) | |
248 | { | |
249 | ||
250 | clear_instances(); | |
251 | ||
252 | return SR_OK; | |
253 | } | |
254 | ||
255 | static int hw_info_get(int info_id, const void **data, | |
256 | const struct sr_dev_inst *sdi) | |
257 | { | |
258 | ||
259 | (void)sdi; | |
260 | ||
261 | switch (info_id) { | |
262 | case SR_DI_HWOPTS: | |
263 | *data = hwopts; | |
264 | break; | |
265 | case SR_DI_HWCAPS: | |
266 | *data = hwcaps; | |
267 | break; | |
268 | case SR_DI_NUM_PROBES: | |
269 | *data = GINT_TO_POINTER(1); | |
270 | break; | |
271 | case SR_DI_PROBE_NAMES: | |
272 | *data = probe_names; | |
273 | break; | |
274 | default: | |
275 | return SR_ERR_ARG; | |
276 | } | |
277 | ||
278 | return SR_OK; | |
279 | } | |
280 | ||
281 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
282 | const void *value) | |
283 | { | |
284 | struct dev_context *devc; | |
285 | ||
286 | if (sdi->status != SR_ST_ACTIVE) | |
287 | return SR_ERR; | |
288 | ||
289 | if (!(devc = sdi->priv)) { | |
38d326e8 | 290 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
291 | return SR_ERR_BUG; |
292 | } | |
293 | ||
294 | switch (hwcap) { | |
295 | case SR_HWCAP_LIMIT_MSEC: | |
296 | /* TODO: not yet implemented */ | |
297 | if (*(const uint64_t *)value == 0) { | |
38d326e8 | 298 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
299 | return SR_ERR; |
300 | } | |
301 | devc->limit_msec = *(const uint64_t *)value; | |
38d326e8 | 302 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
303 | devc->limit_msec); |
304 | break; | |
305 | case SR_HWCAP_LIMIT_SAMPLES: | |
306 | devc->limit_samples = *(const uint64_t *)value; | |
38d326e8 | 307 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
308 | devc->limit_samples); |
309 | break; | |
310 | default: | |
38d326e8 | 311 | sr_err("Unknown capability: %d.", hwcap); |
e93cdf42 BV |
312 | return SR_ERR; |
313 | break; | |
314 | } | |
315 | ||
316 | return SR_OK; | |
317 | } | |
318 | ||
319 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
320 | void *cb_data) | |
321 | { | |
322 | struct sr_datafeed_packet packet; | |
323 | struct sr_datafeed_header header; | |
324 | struct sr_datafeed_meta_analog meta; | |
325 | struct dev_context *devc; | |
326 | ||
327 | if (!(devc = sdi->priv)) { | |
38d326e8 | 328 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
329 | return SR_ERR_BUG; |
330 | } | |
331 | ||
38d326e8 | 332 | sr_dbg("Starting acquisition."); |
e93cdf42 BV |
333 | |
334 | devc->cb_data = cb_data; | |
335 | ||
336 | /* Send header packet to the session bus. */ | |
38d326e8 | 337 | sr_dbg("Sending SR_DF_HEADER."); |
e93cdf42 BV |
338 | packet.type = SR_DF_HEADER; |
339 | packet.payload = (uint8_t *)&header; | |
340 | header.feed_version = 1; | |
341 | gettimeofday(&header.starttime, NULL); | |
342 | sr_session_send(devc->cb_data, &packet); | |
343 | ||
344 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
38d326e8 | 345 | sr_dbg("Sending SR_DF_META_ANALOG."); |
e93cdf42 BV |
346 | packet.type = SR_DF_META_ANALOG; |
347 | packet.payload = &meta; | |
348 | meta.num_probes = 1; | |
349 | sr_session_send(devc->cb_data, &packet); | |
350 | ||
351 | /* Poll every 100ms, or whenever some data comes in. */ | |
352 | sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
353 | ||
354 | return SR_OK; | |
355 | } | |
356 | ||
69b07d14 | 357 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 BV |
358 | { |
359 | struct sr_datafeed_packet packet; | |
360 | struct dev_context *devc; | |
361 | ||
362 | if (sdi->status != SR_ST_ACTIVE) | |
363 | return SR_ERR; | |
364 | ||
365 | if (!(devc = sdi->priv)) { | |
38d326e8 | 366 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
367 | return SR_ERR_BUG; |
368 | } | |
369 | ||
38d326e8 | 370 | sr_dbg("Stopping acquisition."); |
e93cdf42 BV |
371 | |
372 | sr_source_remove(devc->serial->fd); | |
373 | hw_dev_close((struct sr_dev_inst *)sdi); | |
374 | ||
375 | /* Send end packet to the session bus. */ | |
38d326e8 | 376 | sr_dbg("Sending SR_DF_END."); |
e93cdf42 BV |
377 | packet.type = SR_DF_END; |
378 | sr_session_send(cb_data, &packet); | |
379 | ||
380 | ||
381 | return SR_OK; | |
382 | } | |
383 | ||
384 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
385 | .name = "agilent-dmm", | |
386 | .longname = "Agilent U12xx series DMMs", | |
387 | .api_version = 1, | |
388 | .init = hw_init, | |
389 | .cleanup = hw_cleanup, | |
390 | .scan = hw_scan, | |
391 | .dev_list = hw_dev_list, | |
392 | .dev_clear = clear_instances, | |
393 | .dev_open = hw_dev_open, | |
394 | .dev_close = hw_dev_close, | |
395 | .info_get = hw_info_get, | |
396 | .dev_config_set = hw_dev_config_set, | |
397 | .dev_acquisition_start = hw_dev_acquisition_start, | |
398 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
399 | .priv = NULL, | |
400 | }; |