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[] = {
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 static 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;
46 struct channel_context *chanc;
48 chanc = g_malloc(sizeof(*chanc));
49 chanc->location = CONN_FRONT;
51 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
53 channel->priv = chanc;
55 front = sr_channel_group_new(sdi, "Front", NULL);
56 front->channels = g_slist_append(front->channels, channel);
61 static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx,
62 const struct rear_card_info *card)
65 struct sr_channel *channel;
66 struct sr_channel_group *group;
67 struct channel_context *chanc;
70 /* When card is NULL, we couldn't identify the type of card. */
74 group = sr_channel_group_new(sdi, card->cg_name, NULL);
76 for (i = 0; i < card->num_channels; i++) {
78 chanc = g_malloc(sizeof(*chanc));
79 chanc->location = CONN_REAR;
81 if (card->type == REAR_TERMINALS) {
83 g_snprintf(name, sizeof(name), "%s", card->cg_name);
86 g_snprintf(name, sizeof(name), "%s%u", card->cg_name, i);
89 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
91 channel->priv = chanc;
92 group->channels = g_slist_append(group->channels, channel);
98 static gchar *get_revision(struct sr_scpi_dev_inst *scpi)
100 int ret, major, minor;
103 /* Report a version of '0.0' if we can't parse the response. */
106 ret = sr_scpi_get_floatv(scpi, "REV?", &rev_numbers);
107 if ((ret == SR_OK) && (rev_numbers->len >= 2)) {
108 major = (int)g_array_index(rev_numbers, float, 0);
109 minor = (int)g_array_index(rev_numbers, float, 1);
112 g_array_free(rev_numbers, TRUE);
114 return g_strdup_printf("%d.%d", major, minor);
117 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
121 struct sr_dev_inst *sdi;
122 struct dev_context *devc;
125 * The device cannot get identified by means of SCPI queries.
126 * Neither shall non-SCPI requests get emitted before reliable
127 * identification of the device. Assume that we only get here
128 * when user specs led us to believe it's safe to communicate
129 * to the expected kind of device.
133 * This command ensures we receive an EOI after every response, so that
134 * we don't wait the entire timeout after the response is received.
136 if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK)
139 ret = sr_scpi_get_string(scpi, "ID?", &response);
140 if ((ret != SR_OK) || !response)
143 if (strcmp(response, "HP3457A") != 0) {
150 devc = g_malloc0(sizeof(*devc));
151 sdi = g_malloc0(sizeof(*sdi));
152 sdi->vendor = g_strdup("Hewlett-Packard");
153 sdi->model = g_strdup("3457A");
154 sdi->version = get_revision(scpi);
156 sdi->driver = &hp_3457a_driver_info;
157 sdi->inst_type = SR_INST_SCPI;
160 /* There is no way to probe the measurement mode. It must be set. */
161 devc->measurement_mq = 0;
162 devc->measurement_unit = 0;
164 /* Probe rear card option and create channels accordingly (TODO). */
165 devc->rear_card = hp_3457a_probe_rear_card(scpi);
167 idx = create_front_channel(sdi, idx);
168 create_rear_channels(sdi, idx, devc->rear_card);
173 static GSList *scan(struct sr_dev_driver *di, GSList *options)
177 /* Only scan for a device when conn= was specified. */
179 (void)sr_serial_extract_options(options, &conn, NULL);
183 return sr_scpi_scan(di->context, options, probe_device);
187 * We need to set the HP 3457A to a known state, and there are quite a number
188 * of knobs to tweak. Here's a brief explanation of what's going on. For more
189 * details, print out and consult the user manual.
191 * Set the instrument to a pre-determined state. This is easier and faster
192 * than sending a few dozen commands. Some of the PRESET defaults include
193 * ASCII output format, and synchronous triggering. See user manual for
196 * After the PRESET command, the instrument is in a known state, and only those
197 * parameters for which the default is unsuitable are modified:
199 * Enable the HP-IB input buffer. This allows the instrument to release the
200 * HP-IB bus before processing the command, and increases throughput on
201 * GPIB buses with more than one device.
203 * Do not trigger new measurements until instructed to do so.
205 static int dev_open(struct sr_dev_inst *sdi)
207 struct sr_scpi_dev_inst *scpi = sdi->conn;
208 struct dev_context *devc;
210 if (sr_scpi_open(scpi) != SR_OK)
215 sr_scpi_send(scpi, "PRESET");
216 sr_scpi_send(scpi, "INBUF ON");
217 sr_scpi_send(scpi, "TRIG HOLD");
218 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
223 static int dev_close(struct sr_dev_inst *sdi)
225 struct sr_scpi_dev_inst *scpi = sdi->conn;
227 /* Disable scan-advance (preserve relay life). */
228 sr_scpi_send(scpi, "SADV HOLD");
229 /* Switch back to auto-triggering. */
230 sr_scpi_send(scpi, "TRIG AUTO");
237 static int config_get(uint32_t key, GVariant **data,
238 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
240 struct dev_context *devc;
247 case SR_CONF_ADC_POWERLINE_CYCLES:
248 *data = g_variant_new_double(devc->nplc);
257 static int config_set(uint32_t key, GVariant *data,
258 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
261 enum sr_mqflag mq_flags;
262 struct dev_context *devc;
263 GVariant *tuple_child;
270 case SR_CONF_LIMIT_SAMPLES:
271 devc->limit_samples = g_variant_get_uint64(data);
273 case SR_CONF_MEASURED_QUANTITY:
274 tuple_child = g_variant_get_child_value(data, 0);
275 mq = g_variant_get_uint32(tuple_child);
276 tuple_child = g_variant_get_child_value(data, 1);
277 mq_flags = g_variant_get_uint64(tuple_child);
278 g_variant_unref(tuple_child);
279 return hp_3457a_set_mq(sdi, mq, mq_flags);
280 case SR_CONF_ADC_POWERLINE_CYCLES:
281 return hp_3457a_set_nplc(sdi, g_variant_get_double(data));
289 static int config_list(uint32_t key, GVariant **data,
290 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
293 * TODO: Implement channel group configuration when adding support for
297 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
300 static void create_channel_index_list(GSList *channels, GArray **arr)
302 struct sr_channel *channel;
303 struct channel_context *chanc;
306 *arr = g_array_new(FALSE, FALSE, sizeof(unsigned int));
308 for (list_elem = channels; list_elem; list_elem = list_elem->next) {
309 channel = list_elem->data;
310 chanc = channel->priv;
311 g_array_append_val(*arr, chanc->index);
317 * Trigger the first measurement, then hold. We can't let the instrument
318 * auto-trigger because we read several registers to make a complete
319 * reading. If the instrument were auto-triggering, we could get the
320 * reading for sample N, but a new measurement is made and when we read the
321 * HIRES register, it contains data for sample N+1. This would produce
324 * Activate the scan-advance feature. This automatically connects the next
325 * channel in the scan list to the A/D converter. This way, we do not need to
326 * occupy the HP-IB bus to send channel select commands.
328 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
331 gboolean front_selected, rear_selected;
332 struct sr_scpi_dev_inst *scpi;
333 struct sr_channel *channel;
334 struct dev_context *devc;
335 struct channel_context *chanc;
342 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
343 hp_3457a_receive_data, (void *)sdi);
347 std_session_send_df_header(sdi);
349 front_selected = rear_selected = FALSE;
350 devc->active_channels = NULL;
352 for (channels = sdi->channels; channels; channels = channels->next) {
353 channel = channels->data;
355 if (!channel->enabled)
358 chanc = channel->priv;
360 if (chanc->location == CONN_FRONT)
361 front_selected = TRUE;
362 if (chanc->location == CONN_REAR)
363 rear_selected = TRUE;
365 devc->active_channels = g_slist_append(devc->active_channels, channel);
368 if (front_selected && rear_selected) {
369 sr_err("Can not use front and rear channels at the same time!");
370 g_slist_free(devc->active_channels);
374 devc->num_active_channels = g_slist_length(devc->active_channels);
375 if (!devc->num_active_channels) {
376 sr_err("Need at least one active channel!");
377 g_slist_free(devc->active_channels);
380 devc->current_channel = devc->active_channels->data;
382 hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR);
384 /* For plug-in cards, use the scan-advance features to scan channels. */
385 if (rear_selected && (devc->rear_card->card_id != REAR_TERMINALS)) {
386 create_channel_index_list(devc->active_channels, &ch_list);
387 hp_3457a_send_scan_list(sdi, (void *)ch_list->data, ch_list->len);
388 sr_scpi_send(scpi, "SADV AUTO");
389 g_array_free(ch_list, TRUE);
392 /* Start first measurement. */
393 sr_scpi_send(scpi, "TRIG SGL");
394 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
395 devc->num_samples = 0;
400 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
402 struct dev_context *devc;
406 g_slist_free(devc->active_channels);
411 static struct sr_dev_driver hp_3457a_driver_info = {
413 .longname = "HP 3457A",
416 .cleanup = std_cleanup,
418 .dev_list = std_dev_list,
419 .dev_clear = std_dev_clear,
420 .config_get = config_get,
421 .config_set = config_set,
422 .config_list = config_list,
423 .dev_open = dev_open,
424 .dev_close = dev_close,
425 .dev_acquisition_start = dev_acquisition_start,
426 .dev_acquisition_stop = dev_acquisition_stop,
429 SR_REGISTER_DEV_DRIVER(hp_3457a_driver_info);