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_HWCAP_LIMIT_SAMPLES,
44 static const char *probe_names[] = {
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[];
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"
58 static const struct agdmm_profile supported_agdmm[] = {
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 }
68 SR_PRIV struct sr_dev_driver agdmm_driver_info;
69 static struct sr_dev_driver *di = &agdmm_driver_info;
71 /* Properly close and free all devices. */
72 static int clear_instances(void)
74 struct sr_dev_inst *sdi;
75 struct drv_context *drvc;
76 struct dev_context *devc;
79 if (!(drvc = di->priv))
83 for (l = drvc->instances; l; l = l->next) {
86 if (!(devc = sdi->priv))
88 sr_serial_dev_inst_free(devc->serial);
89 sr_dev_inst_free(sdi);
91 g_slist_free(drvc->instances);
92 drvc->instances = NULL;
97 static int hw_init(void)
99 struct drv_context *drvc;
101 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
102 sr_err("Driver context malloc failed.");
103 return SR_ERR_MALLOC;
111 static GSList *hw_scan(GSList *options)
113 struct sr_dev_inst *sdi;
114 struct drv_context *drvc;
115 struct dev_context *devc;
116 struct sr_hwopt *opt;
117 struct sr_probe *probe;
120 const char *conn, *serialcomm;
124 drvc->instances = NULL;
127 conn = serialcomm = NULL;
128 for (l = options; l; l = l->next) {
130 switch (opt->hwopt) {
134 case SR_HWOPT_SERIALCOMM:
135 serialcomm = opt->value;
142 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
143 sr_err("Unable to open %s: %s.", conn, strerror(errno));
148 serialcomm = SERIALCOMM;
149 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
150 sr_err("Unable to set serial parameters: %s.",
156 if (serial_write(fd, "*IDN?\r\n", 7) == -1) {
157 sr_err("Unable to send identification string: %s.",
163 if (!(buf = g_try_malloc(len))) {
164 sr_err("Serial buffer malloc failed.");
167 serial_readline(fd, &buf, &len, 150);
171 tokens = g_strsplit(buf, ",", 4);
172 if (!strcmp("Agilent Technologies", tokens[0])
173 && tokens[2] && tokens[3]) {
174 for (i = 0; supported_agdmm[i].model; i++) {
175 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
177 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0],
178 tokens[1], tokens[3])))
180 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
181 sr_err("Device context malloc failed.");
184 devc->profile = &supported_agdmm[i];
185 devc->serial = sr_serial_dev_inst_new(conn, -1);
189 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
191 sdi->probes = g_slist_append(sdi->probes, probe);
192 drvc->instances = g_slist_append(drvc->instances, sdi);
193 devices = g_slist_append(devices, sdi);
205 static GSList *hw_dev_list(void)
207 struct drv_context *drvc;
211 return drvc->instances;
214 static int hw_dev_open(struct sr_dev_inst *sdi)
216 struct dev_context *devc;
218 if (!(devc = sdi->priv)) {
219 sr_err("sdi->priv was NULL.");
223 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
224 if (devc->serial->fd == -1) {
225 sr_err("Couldn't open serial port '%s'.",
229 serial_set_paramstr(devc->serial->fd, SERIALCOMM);
230 sdi->status = SR_ST_ACTIVE;
235 static int hw_dev_close(struct sr_dev_inst *sdi)
237 struct dev_context *devc;
239 if (!(devc = sdi->priv)) {
240 sr_err("sdi->priv was NULL.");
244 if (devc->serial && devc->serial->fd != -1) {
245 serial_close(devc->serial->fd);
246 devc->serial->fd = -1;
247 sdi->status = SR_ST_INACTIVE;
253 static int hw_cleanup(void)
261 static int hw_info_get(int info_id, const void **data,
262 const struct sr_dev_inst *sdi)
274 case SR_DI_NUM_PROBES:
275 *data = GINT_TO_POINTER(1);
277 case SR_DI_PROBE_NAMES:
287 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
290 struct dev_context *devc;
292 if (sdi->status != SR_ST_ACTIVE)
295 if (!(devc = sdi->priv)) {
296 sr_err("sdi->priv was NULL.");
301 case SR_HWCAP_LIMIT_MSEC:
302 /* TODO: not yet implemented */
303 if (*(const uint64_t *)value == 0) {
304 sr_err("LIMIT_MSEC can't be 0.");
307 devc->limit_msec = *(const uint64_t *)value;
308 sr_dbg("Setting time limit to %" PRIu64 "ms.",
311 case SR_HWCAP_LIMIT_SAMPLES:
312 devc->limit_samples = *(const uint64_t *)value;
313 sr_dbg("Setting sample limit to %" PRIu64 ".",
314 devc->limit_samples);
317 sr_err("Unknown capability: %d.", hwcap);
325 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
328 struct sr_datafeed_packet packet;
329 struct sr_datafeed_header header;
330 struct sr_datafeed_meta_analog meta;
331 struct dev_context *devc;
333 if (!(devc = sdi->priv)) {
334 sr_err("sdi->priv was NULL.");
338 sr_dbg("Starting acquisition.");
340 devc->cb_data = cb_data;
342 /* Send header packet to the session bus. */
343 sr_dbg("Sending SR_DF_HEADER.");
344 packet.type = SR_DF_HEADER;
345 packet.payload = (uint8_t *)&header;
346 header.feed_version = 1;
347 gettimeofday(&header.starttime, NULL);
348 sr_session_send(devc->cb_data, &packet);
350 /* Send metadata about the SR_DF_ANALOG packets to come. */
351 sr_dbg("Sending SR_DF_META_ANALOG.");
352 packet.type = SR_DF_META_ANALOG;
353 packet.payload = &meta;
355 sr_session_send(devc->cb_data, &packet);
357 /* Poll every 100ms, or whenever some data comes in. */
358 sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi);
363 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
365 struct sr_datafeed_packet packet;
366 struct dev_context *devc;
368 if (sdi->status != SR_ST_ACTIVE)
371 if (!(devc = sdi->priv)) {
372 sr_err("sdi->priv was NULL.");
376 sr_dbg("Stopping acquisition.");
378 sr_source_remove(devc->serial->fd);
379 hw_dev_close((struct sr_dev_inst *)sdi);
381 /* Send end packet to the session bus. */
382 sr_dbg("Sending SR_DF_END.");
383 packet.type = SR_DF_END;
384 sr_session_send(cb_data, &packet);
390 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
391 .name = "agilent-dmm",
392 .longname = "Agilent U12xx series DMMs",
395 .cleanup = hw_cleanup,
397 .dev_list = hw_dev_list,
398 .dev_clear = clear_instances,
399 .dev_open = hw_dev_open,
400 .dev_close = hw_dev_close,
401 .info_get = hw_info_get,
402 .dev_config_set = hw_dev_config_set,
403 .dev_acquisition_start = hw_dev_acquisition_start,
404 .dev_acquisition_stop = hw_dev_acquisition_stop,