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/>.
22 #include <sys/types.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 static const uint32_t scanopts[] = {
35 static const uint32_t drvopts[] = {
39 static const uint32_t devopts[] = {
41 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
43 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 static const uint64_t samplerates[] = {
53 static const char *data_sources[] = {
61 extern const struct agdmm_job agdmm_jobs_live[];
62 extern const struct agdmm_job agdmm_jobs_log[];
63 extern const struct agdmm_recv agdmm_recvs_u123x[];
64 extern const struct agdmm_recv agdmm_recvs_u124x[];
65 extern const struct agdmm_recv agdmm_recvs_u125x[];
66 extern const struct agdmm_recv agdmm_recvs_u128x[];
68 /* This works on all the Agilent U12xxA series, although the
69 * U127xA can apparently also run at 19200/8n1. */
70 #define SERIALCOMM "9600/8n1"
72 static const struct agdmm_profile supported_agdmm[] = {
73 { AGILENT_U1231, "U1231A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x },
74 { AGILENT_U1232, "U1232A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x },
75 { AGILENT_U1233, "U1233A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x },
77 { AGILENT_U1241, "U1241A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x },
78 { AGILENT_U1242, "U1242A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x },
79 { AGILENT_U1241, "U1241B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x },
80 { AGILENT_U1242, "U1242B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x },
82 { AGILENT_U1251, "U1251A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x },
83 { AGILENT_U1252, "U1252A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x },
84 { AGILENT_U1253, "U1253A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x },
85 { AGILENT_U1251, "U1251B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x },
86 { AGILENT_U1252, "U1252B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x },
87 { AGILENT_U1253, "U1253B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x },
89 { KEYSIGHT_U1281, "U1281A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x },
90 { KEYSIGHT_U1282, "U1282A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x },
94 static GSList *scan(struct sr_dev_driver *di, GSList *options)
96 struct sr_dev_inst *sdi;
97 struct dev_context *devc;
98 struct sr_config *src;
99 struct sr_serial_dev_inst *serial;
102 const char *conn, *serialcomm;
106 conn = serialcomm = NULL;
107 for (l = options; l; l = l->next) {
111 conn = g_variant_get_string(src->data, NULL);
113 case SR_CONF_SERIALCOMM:
114 serialcomm = g_variant_get_string(src->data, NULL);
121 serialcomm = SERIALCOMM;
123 serial = sr_serial_dev_inst_new(conn, serialcomm);
125 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
128 serial_flush(serial);
129 if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) {
130 sr_err("Unable to send identification string.");
136 serial_readline(serial, &buf, &len, 250);
140 tokens = g_strsplit(buf, ",", 4);
141 if ((!strcmp("Agilent Technologies", tokens[0]) ||
142 !strcmp("Keysight Technologies", tokens[0]))
143 && tokens[1] && tokens[2] && tokens[3]) {
144 for (i = 0; supported_agdmm[i].model; i++) {
145 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
147 sdi = g_malloc0(sizeof(struct sr_dev_inst));
148 sdi->status = SR_ST_INACTIVE;
149 sdi->vendor = g_strdup(tokens[0][0] == 'A' ? "Agilent" : "Keysight");
150 sdi->model = g_strdup(tokens[1]);
151 sdi->version = g_strdup(tokens[3]);
152 devc = g_malloc0(sizeof(struct dev_context));
153 sr_sw_limits_init(&devc->limits);
154 devc->profile = &supported_agdmm[i];
155 devc->data_source = DEFAULT_DATA_SOURCE;
156 devc->cur_samplerate = 5;
157 if (supported_agdmm[i].nb_channels > 1) {
158 int temp_chan = supported_agdmm[i].nb_channels - 1;
159 devc->cur_mq[temp_chan] = SR_MQ_TEMPERATURE;
160 devc->cur_unit[temp_chan] = SR_UNIT_CELSIUS;
161 devc->cur_digits[temp_chan] = 1;
162 devc->cur_encoding[temp_chan] = 2;
164 sdi->inst_type = SR_INST_SERIAL;
167 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
168 if (supported_agdmm[i].nb_channels > 1)
169 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "P2");
170 if (supported_agdmm[i].nb_channels > 2)
171 sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P3");
172 devices = g_slist_append(devices, sdi);
179 serial_close(serial);
181 sr_serial_dev_inst_free(serial);
183 return std_scan_complete(di, devices);
186 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
187 const struct sr_channel_group *cg)
189 struct dev_context *devc;
198 case SR_CONF_SAMPLERATE:
199 *data = g_variant_new_uint64(devc->cur_samplerate);
201 case SR_CONF_LIMIT_SAMPLES:
202 case SR_CONF_LIMIT_MSEC:
203 ret = sr_sw_limits_config_get(&devc->limits, key, data);
205 case SR_CONF_DATA_SOURCE:
206 *data = g_variant_new_string(data_sources[devc->data_source]);
215 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
216 const struct sr_channel_group *cg)
218 struct dev_context *devc;
226 if (sdi->status != SR_ST_ACTIVE)
227 return SR_ERR_DEV_CLOSED;
233 case SR_CONF_SAMPLERATE:
234 samplerate = g_variant_get_uint64(data);
235 if (samplerate < samplerates[0] || samplerate > samplerates[1])
238 devc->cur_samplerate = g_variant_get_uint64(data);
240 case SR_CONF_LIMIT_SAMPLES:
241 case SR_CONF_LIMIT_MSEC:
242 ret = sr_sw_limits_config_set(&devc->limits, key, data);
244 case SR_CONF_DATA_SOURCE: {
245 tmp_str = g_variant_get_string(data, NULL);
246 for (i = 0; i < ARRAY_SIZE(data_sources); i++)
247 if (!strcmp(tmp_str, data_sources[i])) {
248 devc->data_source = i;
251 if (i == ARRAY_SIZE(data_sources))
262 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
263 const struct sr_channel_group *cg)
268 if (key == SR_CONF_SCAN_OPTIONS) {
269 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
270 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
274 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
275 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
276 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
284 case SR_CONF_DEVICE_OPTIONS:
285 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
286 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
288 case SR_CONF_SAMPLERATE:
289 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
290 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
291 ARRAY_SIZE(samplerates), sizeof(uint64_t));
292 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
293 *data = g_variant_builder_end(&gvb);
295 case SR_CONF_DATA_SOURCE:
296 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
305 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
307 struct dev_context *devc = sdi->priv;
308 struct sr_serial_dev_inst *serial;
310 if (sdi->status != SR_ST_ACTIVE)
311 return SR_ERR_DEV_CLOSED;
313 devc->cur_channel = sr_next_enabled_channel(sdi, NULL);
314 devc->cur_conf = sr_next_enabled_channel(sdi, NULL);
315 devc->cur_sample = 1;
316 devc->cur_mq[0] = -1;
317 if (devc->profile->nb_channels > 2)
318 devc->cur_mq[1] = -1;
320 if (devc->data_source == DATA_SOURCE_LIVE) {
321 devc->jobs = devc->profile->jobs_live;
323 devc->jobs = devc->profile->jobs_log;
325 sr_err("Log data source is not implemented for this model.");
328 if (!((struct sr_channel *)sdi->channels->data)->enabled) {
329 sr_err("Log data is only available for channel P1.");
334 sr_sw_limits_acquisition_start(&devc->limits);
335 std_session_send_df_header(sdi);
337 /* Poll every 10ms, or whenever some data comes in. */
339 serial_source_add(sdi->session, serial, G_IO_IN, 10,
340 agdmm_receive_data, (void *)sdi);
345 static struct sr_dev_driver agdmm_driver_info = {
346 .name = "agilent-dmm",
347 .longname = "Agilent U12xx series DMMs",
350 .cleanup = std_cleanup,
352 .dev_list = std_dev_list,
354 .config_get = config_get,
355 .config_set = config_set,
356 .config_list = config_list,
357 .dev_open = std_serial_dev_open,
358 .dev_close = std_serial_dev_close,
359 .dev_acquisition_start = dev_acquisition_start,
360 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
363 SR_REGISTER_DEV_DRIVER(agdmm_driver_info);