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