]>
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 | ||
97 | static int hw_init(void) | |
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 | ||
106 | di->priv = drvc; | |
107 | ||
108 | return SR_OK; | |
109 | } | |
110 | ||
6f22a8ef UH |
111 | /* TODO: Merge into serial_readline() from serial.c. */ |
112 | static int serial_readline2(int fd, char **buf, int *buflen, uint64_t timeout_ms) | |
e93cdf42 BV |
113 | { |
114 | uint64_t start; | |
115 | int maxlen, len; | |
116 | ||
117 | timeout_ms *= 1000; | |
118 | start = g_get_monotonic_time(); | |
119 | ||
120 | maxlen = *buflen; | |
121 | *buflen = len = 0; | |
122 | while(1) { | |
123 | len = maxlen - *buflen - 1; | |
124 | if (len < 1) | |
125 | break; | |
126 | len = serial_read(fd, *buf + *buflen, len); | |
127 | if (len > 0) { | |
128 | *buflen += len; | |
129 | *(*buf + *buflen) = '\0'; | |
130 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\n') | |
131 | /* End of line */ | |
132 | break; | |
133 | } | |
134 | if (g_get_monotonic_time() - start > timeout_ms) | |
135 | /* Timeout */ | |
136 | break; | |
137 | g_usleep(2000); | |
138 | } | |
139 | ||
140 | /* Strip CRLF */ | |
141 | while (*buflen) { | |
142 | if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n') | |
143 | *(*buf + --*buflen) = '\0'; | |
144 | else | |
145 | break; | |
146 | } | |
147 | if (*buflen) | |
38d326e8 | 148 | sr_dbg("Received '%s'.", *buf); |
e93cdf42 BV |
149 | |
150 | return SR_OK; | |
151 | } | |
152 | ||
153 | static GSList *hw_scan(GSList *options) | |
154 | { | |
155 | struct sr_dev_inst *sdi; | |
156 | struct drv_context *drvc; | |
157 | struct dev_context *devc; | |
158 | struct sr_hwopt *opt; | |
159 | struct sr_probe *probe; | |
160 | GSList *l, *devices; | |
161 | int len, fd, i; | |
162 | const char *conn, *serialcomm; | |
163 | char *buf, **tokens; | |
164 | ||
165 | drvc = di->priv; | |
166 | drvc->instances = NULL; | |
167 | ||
168 | devices = NULL; | |
169 | conn = serialcomm = NULL; | |
170 | for (l = options; l; l = l->next) { | |
171 | opt = l->data; | |
172 | switch (opt->hwopt) { | |
173 | case SR_HWOPT_CONN: | |
174 | conn = opt->value; | |
175 | break; | |
176 | case SR_HWOPT_SERIALCOMM: | |
177 | serialcomm = opt->value; | |
178 | break; | |
179 | } | |
180 | } | |
74ac7d7f | 181 | if (!conn) |
e93cdf42 | 182 | return NULL; |
e93cdf42 BV |
183 | |
184 | if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) { | |
38d326e8 | 185 | sr_err("Unable to open %s: %s.", conn, strerror(errno)); |
e93cdf42 BV |
186 | return NULL; |
187 | } | |
188 | ||
74ac7d7f BV |
189 | if (!serialcomm) |
190 | serialcomm = SERIALCOMM; | |
e93cdf42 | 191 | if (serial_set_paramstr(fd, serialcomm) != SR_OK) { |
38d326e8 UH |
192 | sr_err("Unable to set serial parameters: %s.", |
193 | strerror(errno)); | |
e93cdf42 BV |
194 | return NULL; |
195 | } | |
196 | ||
197 | serial_flush(fd); | |
198 | if (serial_write(fd, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
199 | sr_err("Unable to send identification string: %s.", |
200 | strerror(errno)); | |
e93cdf42 BV |
201 | return NULL; |
202 | } | |
203 | ||
204 | len = 128; | |
886a52b6 UH |
205 | if (!(buf = g_try_malloc(len))) { |
206 | sr_err("Serial buffer malloc failed."); | |
207 | return NULL; | |
208 | } | |
6f22a8ef | 209 | serial_readline2(fd, &buf, &len, 150); |
e93cdf42 BV |
210 | if (!len) |
211 | return NULL; | |
212 | ||
213 | tokens = g_strsplit(buf, ",", 4); | |
214 | if (!strcmp("Agilent Technologies", tokens[0]) | |
215 | && tokens[2] && tokens[3]) { | |
216 | for (i = 0; supported_agdmm[i].model; i++) { | |
217 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
218 | continue; | |
219 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0], | |
220 | tokens[1], tokens[3]))) | |
221 | return NULL; | |
222 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
886a52b6 | 223 | sr_err("Device context malloc failed."); |
e93cdf42 BV |
224 | return NULL; |
225 | } | |
226 | devc->profile = &supported_agdmm[i]; | |
227 | devc->serial = sr_serial_dev_inst_new(conn, -1); | |
228 | devc->cur_mq = -1; | |
229 | sdi->priv = devc; | |
230 | sdi->driver = di; | |
231 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
232 | return NULL; | |
233 | sdi->probes = g_slist_append(sdi->probes, probe); | |
234 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
235 | devices = g_slist_append(devices, sdi); | |
236 | break; | |
237 | } | |
238 | } | |
239 | g_strfreev(tokens); | |
240 | g_free(buf); | |
241 | ||
242 | serial_close(fd); | |
243 | ||
244 | return devices; | |
245 | } | |
246 | ||
247 | static GSList *hw_dev_list(void) | |
248 | { | |
249 | struct drv_context *drvc; | |
250 | ||
251 | drvc = di->priv; | |
252 | ||
253 | return drvc->instances; | |
254 | } | |
255 | ||
256 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
257 | { | |
258 | struct dev_context *devc; | |
259 | ||
260 | if (!(devc = sdi->priv)) { | |
38d326e8 | 261 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
262 | return SR_ERR_BUG; |
263 | } | |
264 | ||
265 | devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); | |
266 | if (devc->serial->fd == -1) { | |
38d326e8 | 267 | sr_err("Couldn't open serial port '%s'.", |
e93cdf42 BV |
268 | devc->serial->port); |
269 | return SR_ERR; | |
270 | } | |
74ac7d7f | 271 | serial_set_paramstr(devc->serial->fd, SERIALCOMM); |
e93cdf42 BV |
272 | sdi->status = SR_ST_ACTIVE; |
273 | ||
274 | return SR_OK; | |
275 | } | |
276 | ||
277 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
278 | { | |
279 | struct dev_context *devc; | |
280 | ||
281 | if (!(devc = sdi->priv)) { | |
38d326e8 | 282 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
283 | return SR_ERR_BUG; |
284 | } | |
285 | ||
286 | if (devc->serial && devc->serial->fd != -1) { | |
287 | serial_close(devc->serial->fd); | |
288 | devc->serial->fd = -1; | |
289 | sdi->status = SR_ST_INACTIVE; | |
290 | } | |
291 | ||
292 | return SR_OK; | |
293 | } | |
294 | ||
295 | static int hw_cleanup(void) | |
296 | { | |
297 | ||
298 | clear_instances(); | |
299 | ||
300 | return SR_OK; | |
301 | } | |
302 | ||
303 | static int hw_info_get(int info_id, const void **data, | |
304 | const struct sr_dev_inst *sdi) | |
305 | { | |
306 | ||
307 | (void)sdi; | |
308 | ||
309 | switch (info_id) { | |
310 | case SR_DI_HWOPTS: | |
311 | *data = hwopts; | |
312 | break; | |
313 | case SR_DI_HWCAPS: | |
314 | *data = hwcaps; | |
315 | break; | |
316 | case SR_DI_NUM_PROBES: | |
317 | *data = GINT_TO_POINTER(1); | |
318 | break; | |
319 | case SR_DI_PROBE_NAMES: | |
320 | *data = probe_names; | |
321 | break; | |
322 | default: | |
323 | return SR_ERR_ARG; | |
324 | } | |
325 | ||
326 | return SR_OK; | |
327 | } | |
328 | ||
329 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
330 | const void *value) | |
331 | { | |
332 | struct dev_context *devc; | |
333 | ||
334 | if (sdi->status != SR_ST_ACTIVE) | |
335 | return SR_ERR; | |
336 | ||
337 | if (!(devc = sdi->priv)) { | |
38d326e8 | 338 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
339 | return SR_ERR_BUG; |
340 | } | |
341 | ||
342 | switch (hwcap) { | |
343 | case SR_HWCAP_LIMIT_MSEC: | |
344 | /* TODO: not yet implemented */ | |
345 | if (*(const uint64_t *)value == 0) { | |
38d326e8 | 346 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
347 | return SR_ERR; |
348 | } | |
349 | devc->limit_msec = *(const uint64_t *)value; | |
38d326e8 | 350 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
351 | devc->limit_msec); |
352 | break; | |
353 | case SR_HWCAP_LIMIT_SAMPLES: | |
354 | devc->limit_samples = *(const uint64_t *)value; | |
38d326e8 | 355 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
356 | devc->limit_samples); |
357 | break; | |
358 | default: | |
38d326e8 | 359 | sr_err("Unknown capability: %d.", hwcap); |
e93cdf42 BV |
360 | return SR_ERR; |
361 | break; | |
362 | } | |
363 | ||
364 | return SR_OK; | |
365 | } | |
366 | ||
367 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
368 | void *cb_data) | |
369 | { | |
370 | struct sr_datafeed_packet packet; | |
371 | struct sr_datafeed_header header; | |
372 | struct sr_datafeed_meta_analog meta; | |
373 | struct dev_context *devc; | |
374 | ||
375 | if (!(devc = sdi->priv)) { | |
38d326e8 | 376 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
377 | return SR_ERR_BUG; |
378 | } | |
379 | ||
38d326e8 | 380 | sr_dbg("Starting acquisition."); |
e93cdf42 BV |
381 | |
382 | devc->cb_data = cb_data; | |
383 | ||
384 | /* Send header packet to the session bus. */ | |
38d326e8 | 385 | sr_dbg("Sending SR_DF_HEADER."); |
e93cdf42 BV |
386 | packet.type = SR_DF_HEADER; |
387 | packet.payload = (uint8_t *)&header; | |
388 | header.feed_version = 1; | |
389 | gettimeofday(&header.starttime, NULL); | |
390 | sr_session_send(devc->cb_data, &packet); | |
391 | ||
392 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
38d326e8 | 393 | sr_dbg("Sending SR_DF_META_ANALOG."); |
e93cdf42 BV |
394 | packet.type = SR_DF_META_ANALOG; |
395 | packet.payload = &meta; | |
396 | meta.num_probes = 1; | |
397 | sr_session_send(devc->cb_data, &packet); | |
398 | ||
399 | /* Poll every 100ms, or whenever some data comes in. */ | |
400 | sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
401 | ||
402 | return SR_OK; | |
403 | } | |
404 | ||
69b07d14 | 405 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 BV |
406 | { |
407 | struct sr_datafeed_packet packet; | |
408 | struct dev_context *devc; | |
409 | ||
410 | if (sdi->status != SR_ST_ACTIVE) | |
411 | return SR_ERR; | |
412 | ||
413 | if (!(devc = sdi->priv)) { | |
38d326e8 | 414 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
415 | return SR_ERR_BUG; |
416 | } | |
417 | ||
38d326e8 | 418 | sr_dbg("Stopping acquisition."); |
e93cdf42 BV |
419 | |
420 | sr_source_remove(devc->serial->fd); | |
421 | hw_dev_close((struct sr_dev_inst *)sdi); | |
422 | ||
423 | /* Send end packet to the session bus. */ | |
38d326e8 | 424 | sr_dbg("Sending SR_DF_END."); |
e93cdf42 BV |
425 | packet.type = SR_DF_END; |
426 | sr_session_send(cb_data, &packet); | |
427 | ||
428 | ||
429 | return SR_OK; | |
430 | } | |
431 | ||
432 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
433 | .name = "agilent-dmm", | |
434 | .longname = "Agilent U12xx series DMMs", | |
435 | .api_version = 1, | |
436 | .init = hw_init, | |
437 | .cleanup = hw_cleanup, | |
438 | .scan = hw_scan, | |
439 | .dev_list = hw_dev_list, | |
440 | .dev_clear = clear_instances, | |
441 | .dev_open = hw_dev_open, | |
442 | .dev_close = hw_dev_close, | |
443 | .info_get = hw_info_get, | |
444 | .dev_config_set = hw_dev_config_set, | |
445 | .dev_acquisition_start = hw_dev_acquisition_start, | |
446 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
447 | .priv = NULL, | |
448 | }; |