2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 Alexandru Gagniuc <mr.nuke.me@gmail.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/>.
25 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
33 static const uint32_t devopts[] = {
34 SR_CONF_CONTINUOUS | SR_CONF_SET,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36 SR_CONF_MEASURED_QUANTITY | SR_CONF_SET,
37 SR_CONF_ADC_POWERLINE_CYCLES | SR_CONF_SET | SR_CONF_GET,
40 SR_PRIV struct sr_dev_driver hp_3457a_driver_info;
42 static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx)
44 struct sr_channel *channel;
45 struct sr_channel_group *front;
47 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
50 front = g_malloc0(sizeof(*front));
51 front->name = g_strdup("Front");
52 front->channels = g_slist_append(front->channels, channel);
53 sdi->channel_groups = g_slist_append(sdi->channel_groups, front);
58 static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx,
59 const struct rear_card_info *card)
63 /* When card is NULL, we couldn't identify the type of card. */
67 /* TODO: Create channel descriptor for plug-in cards here. */
71 static gchar *get_revision(struct sr_scpi_dev_inst *scpi)
73 int ret, major, minor;
76 /* Report a version of '0.0' if we can't parse the response. */
79 ret = sr_scpi_get_floatv(scpi, "REV?", &rev_numbers);
80 if ((ret == SR_OK) && (rev_numbers->len >= 2)) {
81 major = (int)g_array_index(rev_numbers, float, 0);
82 minor = (int)g_array_index(rev_numbers, float, 1);
85 g_array_free(rev_numbers, TRUE);
87 return g_strdup_printf("%d.%d", major, minor);
90 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
94 struct sr_dev_inst *sdi;
95 struct dev_context *devc;
98 * This command ensures we receive an EOI after every response, so that
99 * we don't wait the entire timeout after the response is received.
101 if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK)
104 ret = sr_scpi_get_string(scpi, "ID?", &response);
105 if ((ret != SR_OK) || !response)
108 if (strcmp(response, "HP3457A"))
113 devc = g_malloc0(sizeof(struct dev_context));
114 sdi = g_malloc0(sizeof(struct sr_dev_inst));
115 sdi->vendor = g_strdup("Hewlett-Packard");
116 sdi->model = g_strdup("3457A");
117 sdi->version = get_revision(scpi);
119 sdi->driver = &hp_3457a_driver_info;
120 sdi->inst_type = SR_INST_SCPI;
123 /* There is no way to probe the measurement mode. It must be set. */
124 devc->measurement_mq = 0;
125 devc->measurement_unit = 0;
127 /* Probe rear card option and create channels accordingly (TODO). */
128 devc->rear_card = hp_3457a_probe_rear_card(scpi);
130 idx = create_front_channel(sdi, idx);
131 create_rear_channels(sdi, idx, devc->rear_card);
136 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
138 return std_init(sr_ctx, di, LOG_PREFIX);
141 static GSList *scan(struct sr_dev_driver *di, GSList *options)
143 return sr_scpi_scan(di->context, options, probe_device);
146 static GSList *dev_list(const struct sr_dev_driver *di)
148 return ((struct drv_context *)(di->context))->instances;
151 static int dev_clear(const struct sr_dev_driver *di)
153 return std_dev_clear(di, NULL);
157 * We need to set the HP 3457A to a known state, and there are quite a number
158 * of knobs to tweak. Here's a brief explanation of what's going on. For more
159 * details, print out and consult the user manual.
161 * Set the instrument to a pre-determined state. This is easier and faster
162 * than sending a few dozen commands. Some of the PRESET defaults include
163 * ASCII output format, and synchronous triggering. See user manual for
166 * After the PRESET command, the instrument is in a known state, and only those
167 * parameters for which the default is unsuitable are modified:
169 * Enable the HP-IB input buffer. This allows the instrument to release the
170 * HP-IB bus before processing the command, and increases throughput on
171 * GPIB buses with more than one device.
173 * Do not trigger new measurements until instructed to do so.
175 static int dev_open(struct sr_dev_inst *sdi)
177 struct sr_scpi_dev_inst *scpi = sdi->conn;
178 struct dev_context *devc;
180 if (sr_scpi_open(scpi) != SR_OK)
185 sr_scpi_send(scpi, "PRESET");
186 sr_scpi_send(scpi, "INBUF ON");
187 sr_scpi_send(scpi, "TRIG HOLD");
188 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
190 sdi->status = SR_ST_ACTIVE;
195 static int dev_close(struct sr_dev_inst *sdi)
197 struct sr_scpi_dev_inst *scpi = sdi->conn;
199 if (sdi->status != SR_ST_ACTIVE)
200 return SR_ERR_DEV_CLOSED;
202 /* Switch back to auto-triggering. */
203 sr_scpi_send(scpi, "TRIG AUTO");
207 sdi->status = SR_ST_INACTIVE;
212 static int cleanup(const struct sr_dev_driver *di)
214 return dev_clear(di);
217 static int config_get(uint32_t key, GVariant **data,
218 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
221 struct dev_context *devc;
229 case SR_CONF_ADC_POWERLINE_CYCLES:
230 *data = g_variant_new_double(devc->nplc);
239 static int config_set(uint32_t key, GVariant *data,
240 const struct sr_dev_inst *sdi,
241 const struct sr_channel_group *cg)
245 struct dev_context *devc;
246 GVariant *tuple_child;
250 if (sdi->status != SR_ST_ACTIVE)
251 return SR_ERR_DEV_CLOSED;
257 case SR_CONF_LIMIT_SAMPLES:
258 devc->limit_samples = g_variant_get_uint64(data);
260 case SR_CONF_MEASURED_QUANTITY:
261 tuple_child = g_variant_get_child_value(data, 0);
262 mq = g_variant_get_uint32(tuple_child);
263 ret = hp_3457a_set_mq(sdi, mq);
264 g_variant_unref(tuple_child);
266 case SR_CONF_ADC_POWERLINE_CYCLES:
267 ret = hp_3457a_set_nplc(sdi, g_variant_get_double(data));
276 static int config_list(uint32_t key, GVariant **data,
277 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
281 if (key == SR_CONF_SCAN_OPTIONS) {
282 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
283 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
285 } else if ((key == SR_CONF_DEVICE_OPTIONS) && !sdi) {
286 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
287 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
289 } else if ((key == SR_CONF_DEVICE_OPTIONS) && !cg) {
290 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
291 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
295 /* From here on, we're only concerned with channel group config. */
300 * TODO: Implement channel group configuration when adding support for
315 * Trigger the first measurement, then hold. We can't let the instrument
316 * auto-trigger because we read several registers to make a complete
317 * reading. If the instrument were auto-triggering, we could get the
318 * reading for sample N, but a new measurement is made and when we read the
319 * HIRES register, it contains data for sample N+1. This would produce
322 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
325 struct sr_scpi_dev_inst *scpi;
326 struct dev_context *devc;
330 if (sdi->status != SR_ST_ACTIVE)
331 return SR_ERR_DEV_CLOSED;
336 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
337 hp_3457a_receive_data, (void *)sdi);
341 std_session_send_df_header(sdi, LOG_PREFIX);
343 /* Start first measurement. */
344 sr_scpi_send(scpi, "TRIG SGL");
345 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
346 devc->num_samples = 0;
351 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
355 if (sdi->status != SR_ST_ACTIVE)
356 return SR_ERR_DEV_CLOSED;
361 SR_PRIV struct sr_dev_driver hp_3457a_driver_info = {
363 .longname = "HP 3457A",
368 .dev_list = dev_list,
369 .dev_clear = dev_clear,
370 .config_get = config_get,
371 .config_set = config_set,
372 .config_list = config_list,
373 .dev_open = dev_open,
374 .dev_close = dev_close,
375 .dev_acquisition_start = dev_acquisition_start,
376 .dev_acquisition_stop = dev_acquisition_stop,