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_u123x[];
43 extern const struct agdmm_recv agdmm_recvs_u123x[];
44 extern const struct agdmm_job agdmm_jobs_u125x[];
45 extern const struct agdmm_recv agdmm_recvs_u125x[];
47 /* This works on all the Agilent U12xxA series, although the
48 * U127xA can apparently also run at 19200/8n1. */
49 #define SERIALCOMM "9600/8n1"
51 static const struct agdmm_profile supported_agdmm[] = {
52 { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x },
53 { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x },
54 { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x },
55 { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x },
56 { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x },
57 { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x },
58 { 0, NULL, NULL, NULL }
61 SR_PRIV struct sr_dev_driver agdmm_driver_info;
62 static struct sr_dev_driver *di = &agdmm_driver_info;
64 static int init(struct sr_context *sr_ctx)
66 return std_init(sr_ctx, di, LOG_PREFIX);
69 static GSList *scan(GSList *options)
71 struct sr_dev_inst *sdi;
72 struct drv_context *drvc;
73 struct dev_context *devc;
74 struct sr_config *src;
75 struct sr_channel *ch;
76 struct sr_serial_dev_inst *serial;
79 const char *conn, *serialcomm;
83 drvc->instances = NULL;
86 conn = serialcomm = NULL;
87 for (l = options; l; l = l->next) {
91 conn = g_variant_get_string(src->data, NULL);
93 case SR_CONF_SERIALCOMM:
94 serialcomm = g_variant_get_string(src->data, NULL);
101 serialcomm = SERIALCOMM;
103 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
106 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
109 serial_flush(serial);
110 if (serial_write(serial, "*IDN?\r\n", 7) == -1) {
111 sr_err("Unable to send identification string: %s.",
117 if (!(buf = g_try_malloc(len))) {
118 sr_err("Serial buffer malloc failed.");
121 serial_readline(serial, &buf, &len, 150);
125 tokens = g_strsplit(buf, ",", 4);
126 if (!strcmp("Agilent Technologies", tokens[0])
127 && tokens[1] && tokens[2] && tokens[3]) {
128 for (i = 0; supported_agdmm[i].model; i++) {
129 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
131 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Agilent",
132 tokens[1], tokens[3])))
134 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
135 sr_err("Device context malloc failed.");
138 devc->profile = &supported_agdmm[i];
140 sdi->inst_type = SR_INST_SERIAL;
144 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
146 sdi->channels = g_slist_append(sdi->channels, ch);
147 drvc->instances = g_slist_append(drvc->instances, sdi);
148 devices = g_slist_append(devices, sdi);
155 serial_close(serial);
157 sr_serial_dev_inst_free(serial);
162 static GSList *dev_list(void)
164 return ((struct drv_context *)(di->priv))->instances;
167 static int cleanup(void)
169 return std_dev_clear(di, NULL);
172 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
173 const struct sr_channel_group *cg)
175 struct dev_context *devc;
179 if (sdi->status != SR_ST_ACTIVE)
180 return SR_ERR_DEV_CLOSED;
182 if (!(devc = sdi->priv)) {
183 sr_err("sdi->priv was NULL.");
188 case SR_CONF_LIMIT_MSEC:
189 /* TODO: not yet implemented */
190 if (g_variant_get_uint64(data) == 0) {
191 sr_err("LIMIT_MSEC can't be 0.");
194 devc->limit_msec = g_variant_get_uint64(data);
195 sr_dbg("Setting time limit to %" PRIu64 "ms.",
198 case SR_CONF_LIMIT_SAMPLES:
199 devc->limit_samples = g_variant_get_uint64(data);
200 sr_dbg("Setting sample limit to %" PRIu64 ".",
201 devc->limit_samples);
210 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
211 const struct sr_channel_group *cg)
217 case SR_CONF_SCAN_OPTIONS:
218 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
219 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
221 case SR_CONF_DEVICE_OPTIONS:
222 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
223 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
232 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
234 struct dev_context *devc;
235 struct sr_serial_dev_inst *serial;
237 if (sdi->status != SR_ST_ACTIVE)
238 return SR_ERR_DEV_CLOSED;
240 if (!(devc = sdi->priv)) {
241 sr_err("sdi->priv was NULL.");
245 devc->cb_data = cb_data;
247 /* Send header packet to the session bus. */
248 std_session_send_df_header(cb_data, LOG_PREFIX);
250 /* Poll every 100ms, or whenever some data comes in. */
252 serial_source_add(sdi->session, serial, G_IO_IN, 100,
253 agdmm_receive_data, (void *)sdi);
258 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
260 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
261 sdi->conn, LOG_PREFIX);
264 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
265 .name = "agilent-dmm",
266 .longname = "Agilent U12xx series DMMs",
271 .dev_list = dev_list,
274 .config_set = config_set,
275 .config_list = config_list,
276 .dev_open = std_serial_dev_open,
277 .dev_close = std_serial_dev_close,
278 .dev_acquisition_start = dev_acquisition_start,
279 .dev_acquisition_stop = dev_acquisition_stop,