2 * This file is part of the libsigrok 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 int32_t hwopts[] = {
35 static const int32_t hwcaps[] = {
37 SR_CONF_LIMIT_SAMPLES,
42 extern const struct agdmm_job agdmm_jobs_u12xx[];
43 extern const struct agdmm_recv agdmm_recvs_u123x[];
44 extern const struct agdmm_recv agdmm_recvs_u125x[];
46 /* This works on all the Agilent U12xxA series, although the
47 * U127xA can apparently also run at 19200/8n1. */
48 #define SERIALCOMM "9600/8n1"
50 static const struct agdmm_profile supported_agdmm[] = {
51 { AGILENT_U1231, "U1231A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
52 { AGILENT_U1232, "U1232A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
53 { AGILENT_U1233, "U1233A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
55 { AGILENT_U1251, "U1251A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
56 { AGILENT_U1252, "U1252A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
57 { AGILENT_U1253, "U1253A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
58 { AGILENT_U1251, "U1251B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
59 { AGILENT_U1252, "U1252B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
60 { AGILENT_U1253, "U1253B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
64 SR_PRIV struct sr_dev_driver agdmm_driver_info;
65 static struct sr_dev_driver *di = &agdmm_driver_info;
67 static int init(struct sr_context *sr_ctx)
69 return std_init(sr_ctx, di, LOG_PREFIX);
72 static GSList *scan(GSList *options)
74 struct sr_dev_inst *sdi;
75 struct drv_context *drvc;
76 struct dev_context *devc;
77 struct sr_config *src;
78 struct sr_channel *ch;
79 struct sr_serial_dev_inst *serial;
82 const char *conn, *serialcomm;
86 drvc->instances = NULL;
89 conn = serialcomm = NULL;
90 for (l = options; l; l = l->next) {
94 conn = g_variant_get_string(src->data, NULL);
96 case SR_CONF_SERIALCOMM:
97 serialcomm = g_variant_get_string(src->data, NULL);
104 serialcomm = SERIALCOMM;
106 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
109 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
112 serial_flush(serial);
113 if (serial_write(serial, "*IDN?\r\n", 7) == -1) {
114 sr_err("Unable to send identification string: %s.",
120 if (!(buf = g_try_malloc(len))) {
121 sr_err("Serial buffer malloc failed.");
124 serial_readline(serial, &buf, &len, 150);
128 tokens = g_strsplit(buf, ",", 4);
129 if (!strcmp("Agilent Technologies", tokens[0])
130 && tokens[1] && tokens[2] && tokens[3]) {
131 for (i = 0; supported_agdmm[i].model; i++) {
132 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
134 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Agilent",
135 tokens[1], tokens[3])))
137 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
138 sr_err("Device context malloc failed.");
141 devc->profile = &supported_agdmm[i];
143 sdi->inst_type = SR_INST_SERIAL;
147 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
149 sdi->channels = g_slist_append(sdi->channels, ch);
150 drvc->instances = g_slist_append(drvc->instances, sdi);
151 devices = g_slist_append(devices, sdi);
158 serial_close(serial);
160 sr_serial_dev_inst_free(serial);
165 static GSList *dev_list(void)
167 return ((struct drv_context *)(di->priv))->instances;
170 static int cleanup(void)
172 return std_dev_clear(di, NULL);
175 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
176 const struct sr_channel_group *cg)
178 struct dev_context *devc;
182 if (sdi->status != SR_ST_ACTIVE)
183 return SR_ERR_DEV_CLOSED;
185 if (!(devc = sdi->priv)) {
186 sr_err("sdi->priv was NULL.");
191 case SR_CONF_LIMIT_MSEC:
192 /* TODO: not yet implemented */
193 if (g_variant_get_uint64(data) == 0) {
194 sr_err("LIMIT_MSEC can't be 0.");
197 devc->limit_msec = g_variant_get_uint64(data);
198 sr_dbg("Setting time limit to %" PRIu64 "ms.",
201 case SR_CONF_LIMIT_SAMPLES:
202 devc->limit_samples = g_variant_get_uint64(data);
203 sr_dbg("Setting sample limit to %" PRIu64 ".",
204 devc->limit_samples);
213 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
214 const struct sr_channel_group *cg)
220 case SR_CONF_SCAN_OPTIONS:
221 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
222 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
224 case SR_CONF_DEVICE_OPTIONS:
225 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
226 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
235 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
237 struct dev_context *devc;
238 struct sr_serial_dev_inst *serial;
240 if (sdi->status != SR_ST_ACTIVE)
241 return SR_ERR_DEV_CLOSED;
243 if (!(devc = sdi->priv)) {
244 sr_err("sdi->priv was NULL.");
248 devc->cb_data = cb_data;
250 /* Send header packet to the session bus. */
251 std_session_send_df_header(cb_data, LOG_PREFIX);
253 /* Poll every 100ms, or whenever some data comes in. */
255 serial_source_add(sdi->session, serial, G_IO_IN, 100,
256 agdmm_receive_data, (void *)sdi);
261 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
263 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
264 sdi->conn, LOG_PREFIX);
267 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
268 .name = "agilent-dmm",
269 .longname = "Agilent U12xx series DMMs",
274 .dev_list = dev_list,
277 .config_set = config_set,
278 .config_list = config_list,
279 .dev_open = std_serial_dev_open,
280 .dev_close = std_serial_dev_close,
281 .dev_acquisition_start = dev_acquisition_start,
282 .dev_acquisition_stop = dev_acquisition_stop,