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 = g_malloc0(sizeof(*front));
56 front->name = g_strdup("Front");
57 front->channels = g_slist_append(front->channels, channel);
59 sdi->channel_groups = g_slist_append(sdi->channel_groups, front);
64 static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx,
65 const struct rear_card_info *card)
68 struct sr_channel *channel;
69 struct sr_channel_group *group;
70 struct channel_context *chanc;
73 /* When card is NULL, we couldn't identify the type of card. */
77 group = g_malloc0(sizeof(*group));
79 group->name = g_strdup(card->cg_name);
80 sdi->channel_groups = g_slist_append(sdi->channel_groups, group);
82 for (i = 0; i < card->num_channels; i++) {
84 chanc = g_malloc(sizeof(*chanc));
85 chanc->location = CONN_REAR;
87 if (card->type == REAR_TERMINALS) {
89 g_snprintf(name, sizeof(name), "%s", card->cg_name);
92 g_snprintf(name, sizeof(name), "%s%u", card->cg_name, i);
95 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
97 channel->priv = chanc;
98 group->channels = g_slist_append(group->channels, channel);
104 static gchar *get_revision(struct sr_scpi_dev_inst *scpi)
106 int ret, major, minor;
109 /* Report a version of '0.0' if we can't parse the response. */
112 ret = sr_scpi_get_floatv(scpi, "REV?", &rev_numbers);
113 if ((ret == SR_OK) && (rev_numbers->len >= 2)) {
114 major = (int)g_array_index(rev_numbers, float, 0);
115 minor = (int)g_array_index(rev_numbers, float, 1);
118 g_array_free(rev_numbers, TRUE);
120 return g_strdup_printf("%d.%d", major, minor);
123 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
127 struct sr_dev_inst *sdi;
128 struct dev_context *devc;
131 * This command ensures we receive an EOI after every response, so that
132 * we don't wait the entire timeout after the response is received.
134 if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK)
137 ret = sr_scpi_get_string(scpi, "ID?", &response);
138 if ((ret != SR_OK) || !response)
141 if (strcmp(response, "HP3457A"))
146 devc = g_malloc0(sizeof(struct dev_context));
147 sdi = g_malloc0(sizeof(struct sr_dev_inst));
148 sdi->vendor = g_strdup("Hewlett-Packard");
149 sdi->model = g_strdup("3457A");
150 sdi->version = get_revision(scpi);
152 sdi->driver = &hp_3457a_driver_info;
153 sdi->inst_type = SR_INST_SCPI;
156 /* There is no way to probe the measurement mode. It must be set. */
157 devc->measurement_mq = 0;
158 devc->measurement_unit = 0;
160 /* Probe rear card option and create channels accordingly (TODO). */
161 devc->rear_card = hp_3457a_probe_rear_card(scpi);
163 idx = create_front_channel(sdi, idx);
164 create_rear_channels(sdi, idx, devc->rear_card);
169 static GSList *scan(struct sr_dev_driver *di, GSList *options)
171 return sr_scpi_scan(di->context, options, probe_device);
175 * We need to set the HP 3457A to a known state, and there are quite a number
176 * of knobs to tweak. Here's a brief explanation of what's going on. For more
177 * details, print out and consult the user manual.
179 * Set the instrument to a pre-determined state. This is easier and faster
180 * than sending a few dozen commands. Some of the PRESET defaults include
181 * ASCII output format, and synchronous triggering. See user manual for
184 * After the PRESET command, the instrument is in a known state, and only those
185 * parameters for which the default is unsuitable are modified:
187 * Enable the HP-IB input buffer. This allows the instrument to release the
188 * HP-IB bus before processing the command, and increases throughput on
189 * GPIB buses with more than one device.
191 * Do not trigger new measurements until instructed to do so.
193 static int dev_open(struct sr_dev_inst *sdi)
195 struct sr_scpi_dev_inst *scpi = sdi->conn;
196 struct dev_context *devc;
198 if (sr_scpi_open(scpi) != SR_OK)
203 sr_scpi_send(scpi, "PRESET");
204 sr_scpi_send(scpi, "INBUF ON");
205 sr_scpi_send(scpi, "TRIG HOLD");
206 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
208 sdi->status = SR_ST_ACTIVE;
213 static int dev_close(struct sr_dev_inst *sdi)
215 struct sr_scpi_dev_inst *scpi = sdi->conn;
217 if (sdi->status != SR_ST_ACTIVE)
218 return SR_ERR_DEV_CLOSED;
220 /* Disable scan-advance (preserve relay life). */
221 sr_scpi_send(scpi, "SADV HOLD");
222 /* Switch back to auto-triggering. */
223 sr_scpi_send(scpi, "TRIG AUTO");
227 sdi->status = SR_ST_INACTIVE;
232 static int config_get(uint32_t key, GVariant **data,
233 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
236 struct dev_context *devc;
244 case SR_CONF_ADC_POWERLINE_CYCLES:
245 *data = g_variant_new_double(devc->nplc);
254 static int config_set(uint32_t key, GVariant *data,
255 const struct sr_dev_inst *sdi,
256 const struct sr_channel_group *cg)
260 enum sr_mqflag mq_flags;
261 struct dev_context *devc;
262 GVariant *tuple_child;
266 if (sdi->status != SR_ST_ACTIVE)
267 return SR_ERR_DEV_CLOSED;
273 case SR_CONF_LIMIT_SAMPLES:
274 devc->limit_samples = g_variant_get_uint64(data);
276 case SR_CONF_MEASURED_QUANTITY:
277 tuple_child = g_variant_get_child_value(data, 0);
278 mq = g_variant_get_uint32(tuple_child);
279 tuple_child = g_variant_get_child_value(data, 1);
280 mq_flags = g_variant_get_uint64(tuple_child);
281 ret = hp_3457a_set_mq(sdi, mq, mq_flags);
282 g_variant_unref(tuple_child);
284 case SR_CONF_ADC_POWERLINE_CYCLES:
285 ret = hp_3457a_set_nplc(sdi, g_variant_get_double(data));
294 static int config_list(uint32_t key, GVariant **data,
295 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
299 if (key == SR_CONF_SCAN_OPTIONS) {
300 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
301 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
303 } else if ((key == SR_CONF_DEVICE_OPTIONS) && !sdi) {
304 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
305 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
307 } else if ((key == SR_CONF_DEVICE_OPTIONS) && !cg) {
308 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
309 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
313 /* From here on, we're only concerned with channel group config. */
318 * TODO: Implement channel group configuration when adding support for
331 static void create_channel_index_list(GSList *channels, GArray **arr)
333 struct sr_channel *channel;
334 struct channel_context *chanc;
337 *arr = g_array_new(FALSE, FALSE, sizeof(unsigned int));
339 for (list_elem = channels; list_elem; list_elem = list_elem->next) {
340 channel = list_elem->data;
341 chanc = channel->priv;
342 g_array_append_val(*arr, chanc->index);
348 * Trigger the first measurement, then hold. We can't let the instrument
349 * auto-trigger because we read several registers to make a complete
350 * reading. If the instrument were auto-triggering, we could get the
351 * reading for sample N, but a new measurement is made and when we read the
352 * HIRES register, it contains data for sample N+1. This would produce
355 * Activate the scan-advance feature. This automatically connects the next
356 * channel in the scan list to the A/D converter. This way, we do not need to
357 * occupy the HP-IB bus to send channel select commands.
359 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
362 gboolean front_selected, rear_selected;
363 struct sr_scpi_dev_inst *scpi;
364 struct sr_channel *channel;
365 struct dev_context *devc;
366 struct channel_context *chanc;
370 if (sdi->status != SR_ST_ACTIVE)
371 return SR_ERR_DEV_CLOSED;
376 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
377 hp_3457a_receive_data, (void *)sdi);
381 std_session_send_df_header(sdi);
383 front_selected = rear_selected = FALSE;
384 devc->active_channels = NULL;
386 for (channels = sdi->channels; channels; channels = channels->next) {
387 channel = channels->data;
389 if (!channel->enabled)
392 chanc = channel->priv;
394 if (chanc->location == CONN_FRONT)
395 front_selected = TRUE;
396 if (chanc->location == CONN_REAR)
397 rear_selected = TRUE;
399 devc->active_channels = g_slist_append(devc->active_channels, channel);
402 if (front_selected && rear_selected) {
403 sr_err("Can not use front and rear channels at the same time!");
404 g_slist_free(devc->active_channels);
408 devc->current_channel = devc->active_channels->data;
409 devc->num_active_channels = g_slist_length(devc->active_channels);
411 hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR);
413 /* For plug-in cards, use the scan-advance features to scan channels. */
414 if (rear_selected && (devc->rear_card->card_id != REAR_TERMINALS)) {
415 create_channel_index_list(devc->active_channels, &ch_list);
416 hp_3457a_send_scan_list(sdi, (void *)ch_list->data, ch_list->len);
417 sr_scpi_send(scpi, "SADV AUTO");
418 g_array_free(ch_list, TRUE);
421 /* Start first measurement. */
422 sr_scpi_send(scpi, "TRIG SGL");
423 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
424 devc->num_samples = 0;
429 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
431 struct dev_context *devc;
435 g_slist_free(devc->active_channels);
440 static struct sr_dev_driver hp_3457a_driver_info = {
442 .longname = "HP 3457A",
445 .cleanup = std_cleanup,
447 .dev_list = std_dev_list,
448 .config_get = config_get,
449 .config_set = config_set,
450 .config_list = config_list,
451 .dev_open = dev_open,
452 .dev_close = dev_close,
453 .dev_acquisition_start = dev_acquisition_start,
454 .dev_acquisition_stop = dev_acquisition_stop,
457 SR_REGISTER_DEV_DRIVER(hp_3457a_driver_info);