2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "agilent-dmm.h"
30 static const int hwopts[] = {
36 static const int hwcaps[] = {
38 SR_CONF_LIMIT_SAMPLES,
44 extern const struct agdmm_job agdmm_jobs_u123x[];
45 extern const struct agdmm_recv agdmm_recvs_u123x[];
46 extern const struct agdmm_job agdmm_jobs_u125x[];
47 extern const struct agdmm_recv agdmm_recvs_u125x[];
49 /* This works on all the Agilent U12xxA series, although the
50 * U127xA can apparently also run at 19200/8n1. */
51 #define SERIALCOMM "9600/8n1"
53 static const struct agdmm_profile supported_agdmm[] = {
54 { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x },
55 { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x },
56 { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x },
57 { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x },
58 { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x },
59 { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x },
60 { 0, NULL, NULL, NULL }
63 SR_PRIV struct sr_dev_driver agdmm_driver_info;
64 static struct sr_dev_driver *di = &agdmm_driver_info;
66 /* Properly close and free all devices. */
67 static int clear_instances(void)
69 struct sr_dev_inst *sdi;
70 struct drv_context *drvc;
71 struct dev_context *devc;
74 if (!(drvc = di->priv))
78 for (l = drvc->instances; l; l = l->next) {
81 if (!(devc = sdi->priv))
83 sr_serial_dev_inst_free(devc->serial);
84 sr_dev_inst_free(sdi);
86 g_slist_free(drvc->instances);
87 drvc->instances = NULL;
92 static int hw_init(struct sr_context *sr_ctx)
94 struct drv_context *drvc;
96 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
97 sr_err("Driver context malloc failed.");
101 drvc->sr_ctx = sr_ctx;
107 static GSList *hw_scan(GSList *options)
109 struct sr_dev_inst *sdi;
110 struct drv_context *drvc;
111 struct dev_context *devc;
112 struct sr_config *src;
113 struct sr_probe *probe;
114 struct sr_serial_dev_inst *serial;
117 const char *conn, *serialcomm;
121 drvc->instances = NULL;
124 conn = serialcomm = NULL;
125 for (l = options; l; l = l->next) {
131 case SR_CONF_SERIALCOMM:
132 serialcomm = src->value;
139 serialcomm = SERIALCOMM;
141 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
144 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
147 serial_flush(serial);
148 if (serial_write(serial, "*IDN?\r\n", 7) == -1) {
149 sr_err("Unable to send identification string: %s.",
155 if (!(buf = g_try_malloc(len))) {
156 sr_err("Serial buffer malloc failed.");
159 serial_readline(serial, &buf, &len, 150);
163 tokens = g_strsplit(buf, ",", 4);
164 if (!strcmp("Agilent Technologies", tokens[0])
165 && tokens[2] && tokens[3]) {
166 for (i = 0; supported_agdmm[i].model; i++) {
167 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
169 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0],
170 tokens[1], tokens[3])))
172 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
173 sr_err("Device context malloc failed.");
176 devc->profile = &supported_agdmm[i];
177 devc->serial = serial;
181 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
183 sdi->probes = g_slist_append(sdi->probes, probe);
184 drvc->instances = g_slist_append(drvc->instances, sdi);
185 devices = g_slist_append(devices, sdi);
192 serial_close(serial);
194 sr_serial_dev_inst_free(serial);
199 static GSList *hw_dev_list(void)
201 struct drv_context *drvc;
205 return drvc->instances;
208 static int hw_dev_open(struct sr_dev_inst *sdi)
210 struct dev_context *devc;
212 if (!(devc = sdi->priv)) {
213 sr_err("sdi->priv was NULL.");
217 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
220 sdi->status = SR_ST_ACTIVE;
225 static int hw_dev_close(struct sr_dev_inst *sdi)
227 struct dev_context *devc;
229 if (!(devc = sdi->priv)) {
230 sr_err("sdi->priv was NULL.");
234 if (devc->serial && devc->serial->fd != -1) {
235 serial_close(devc->serial);
236 sdi->status = SR_ST_INACTIVE;
242 static int hw_cleanup(void)
250 static int hw_info_get(int info_id, const void **data,
251 const struct sr_dev_inst *sdi)
269 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
272 struct dev_context *devc;
274 if (sdi->status != SR_ST_ACTIVE)
277 if (!(devc = sdi->priv)) {
278 sr_err("sdi->priv was NULL.");
283 case SR_CONF_LIMIT_MSEC:
284 /* TODO: not yet implemented */
285 if (*(const uint64_t *)value == 0) {
286 sr_err("LIMIT_MSEC can't be 0.");
289 devc->limit_msec = *(const uint64_t *)value;
290 sr_dbg("Setting time limit to %" PRIu64 "ms.",
293 case SR_CONF_LIMIT_SAMPLES:
294 devc->limit_samples = *(const uint64_t *)value;
295 sr_dbg("Setting sample limit to %" PRIu64 ".",
296 devc->limit_samples);
299 sr_err("Unknown capability: %d.", hwcap);
307 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
310 struct sr_datafeed_packet packet;
311 struct sr_datafeed_header header;
312 struct dev_context *devc;
314 if (!(devc = sdi->priv)) {
315 sr_err("sdi->priv was NULL.");
319 sr_dbg("Starting acquisition.");
321 devc->cb_data = cb_data;
323 /* Send header packet to the session bus. */
324 sr_dbg("Sending SR_DF_HEADER.");
325 packet.type = SR_DF_HEADER;
326 packet.payload = (uint8_t *)&header;
327 header.feed_version = 1;
328 gettimeofday(&header.starttime, NULL);
329 sr_session_send(devc->cb_data, &packet);
331 /* Poll every 100ms, or whenever some data comes in. */
332 sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi);
337 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
339 struct sr_datafeed_packet packet;
340 struct dev_context *devc;
342 if (sdi->status != SR_ST_ACTIVE)
345 if (!(devc = sdi->priv)) {
346 sr_err("sdi->priv was NULL.");
350 sr_dbg("Stopping acquisition.");
352 sr_source_remove(devc->serial->fd);
353 hw_dev_close((struct sr_dev_inst *)sdi);
355 /* Send end packet to the session bus. */
356 sr_dbg("Sending SR_DF_END.");
357 packet.type = SR_DF_END;
358 sr_session_send(cb_data, &packet);
364 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
365 .name = "agilent-dmm",
366 .longname = "Agilent U12xx series DMMs",
369 .cleanup = hw_cleanup,
371 .dev_list = hw_dev_list,
372 .dev_clear = clear_instances,
373 .dev_open = hw_dev_open,
374 .dev_close = hw_dev_close,
375 .info_get = hw_info_get,
376 .dev_config_set = hw_dev_config_set,
377 .dev_acquisition_start = hw_dev_acquisition_start,
378 .dev_acquisition_stop = hw_dev_acquisition_stop,