]> sigrok.org Git - libsigrok.git/blob - src/hardware/hp-3457a/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / hp-3457a / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include <scpi.h>
22 #include <string.h>
23 #include "protocol.h"
24
25 static const uint32_t scanopts[] = {
26         SR_CONF_CONN,
27 };
28
29 static const uint32_t drvopts[] = {
30         SR_CONF_MULTIMETER,
31 };
32
33 static const uint32_t devopts[] = {
34         SR_CONF_CONTINUOUS,
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,
38 };
39
40 static struct sr_dev_driver hp_3457a_driver_info;
41
42 static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx)
43 {
44         struct sr_channel *channel;
45         struct sr_channel_group *front;
46         struct channel_context *chanc;
47
48         chanc = g_malloc(sizeof(*chanc));
49         chanc->location = CONN_FRONT;
50
51         channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
52                                  TRUE, "Front");
53         channel->priv = chanc;
54
55         front = sr_channel_group_new(sdi, "Front", NULL);
56         front->channels = g_slist_append(front->channels, channel);
57
58         return chan_idx;
59 }
60
61 static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx,
62                                  const struct rear_card_info *card)
63 {
64         unsigned int i;
65         struct sr_channel *channel;
66         struct sr_channel_group *group;
67         struct channel_context *chanc;
68         char name[16];
69
70         /* When card is NULL, we couldn't identify the type of card. */
71         if (!card)
72                 return chan_idx;
73
74         group = sr_channel_group_new(sdi, card->cg_name, NULL);
75
76         for (i = 0; i < card->num_channels; i++) {
77
78                 chanc = g_malloc(sizeof(*chanc));
79                 chanc->location = CONN_REAR;
80
81                 if (card->type == REAR_TERMINALS) {
82                         chanc->index = -1;
83                         g_snprintf(name, sizeof(name), "%s", card->cg_name);
84                 } else {
85                         chanc->index = i;
86                         g_snprintf(name, sizeof(name), "%s%u", card->cg_name, i);
87                 }
88
89                 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
90                                         FALSE, name);
91                 channel->priv = chanc;
92                 group->channels = g_slist_append(group->channels, channel);
93         }
94
95         return chan_idx;
96 }
97
98 static gchar *get_revision(struct sr_scpi_dev_inst *scpi)
99 {
100         int ret, major, minor;
101         GArray *rev_numbers;
102
103         /* Report a version of '0.0' if we can't parse the response. */
104         major = minor = 0;
105
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);
110         }
111
112         g_array_free(rev_numbers, TRUE);
113
114         return g_strdup_printf("%d.%d", major, minor);
115 }
116
117 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
118 {
119         int ret, idx;
120         char *response;
121         struct sr_dev_inst *sdi;
122         struct dev_context *devc;
123
124         /*
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.
130          */
131
132         /*
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.
135          */
136         if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK)
137                 return NULL;
138
139         ret = sr_scpi_get_string(scpi, "ID?", &response);
140         if ((ret != SR_OK) || !response)
141                 return NULL;
142
143         if (strcmp(response, "HP3457A") != 0) {
144                 g_free(response);
145                 return NULL;
146         }
147
148         g_free(response);
149
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);
155         sdi->conn = scpi;
156         sdi->driver = &hp_3457a_driver_info;
157         sdi->inst_type = SR_INST_SCPI;
158         sdi->priv = devc;
159
160         /* There is no way to probe the measurement mode. It must be set. */
161         devc->measurement_mq = 0;
162         devc->measurement_unit = 0;
163
164         /* Probe rear card option and create channels accordingly (TODO). */
165         devc->rear_card = hp_3457a_probe_rear_card(scpi);
166         idx = 0;
167         idx = create_front_channel(sdi, idx);
168         create_rear_channels(sdi, idx, devc->rear_card);
169
170         return sdi;
171 }
172
173 static GSList *scan(struct sr_dev_driver *di, GSList *options)
174 {
175         const char *conn;
176
177         /* Only scan for a device when conn= was specified. */
178         conn = NULL;
179         (void)sr_serial_extract_options(options, &conn, NULL);
180         if (!conn)
181                 return NULL;
182
183         return sr_scpi_scan(di->context, options, probe_device);
184 }
185
186 /*
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.
190  *   PRESET
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
194  *     more details.
195  *
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:
198  *   INBUF ON
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.
202  *   TRIG HOLD
203  *     Do not trigger new measurements until instructed to do so.
204  */
205 static int dev_open(struct sr_dev_inst *sdi)
206 {
207         struct sr_scpi_dev_inst *scpi = sdi->conn;
208         struct dev_context *devc;
209
210         if (sr_scpi_open(scpi) != SR_OK)
211                 return SR_ERR;
212
213         devc = sdi->priv;
214
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);
219
220         return SR_OK;
221 }
222
223 static int dev_close(struct sr_dev_inst *sdi)
224 {
225         struct sr_scpi_dev_inst *scpi = sdi->conn;
226
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");
231
232         sr_scpi_close(scpi);
233
234         return SR_OK;
235 }
236
237 static int config_get(uint32_t key, GVariant **data,
238         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
239 {
240         struct dev_context *devc;
241
242         (void)cg;
243
244         devc = sdi->priv;
245
246         switch (key) {
247         case SR_CONF_ADC_POWERLINE_CYCLES:
248                 *data = g_variant_new_double(devc->nplc);
249                 break;
250         default:
251                 return SR_ERR_NA;
252         }
253
254         return SR_OK;
255 }
256
257 static int config_set(uint32_t key, GVariant *data,
258         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
259 {
260         enum sr_mq mq;
261         enum sr_mqflag mq_flags;
262         struct dev_context *devc;
263         GVariant *tuple_child;
264
265         (void)cg;
266
267         devc = sdi->priv;
268
269         switch (key) {
270         case SR_CONF_LIMIT_SAMPLES:
271                 devc->limit_samples = g_variant_get_uint64(data);
272                 break;
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));
282         default:
283                 return SR_ERR_NA;
284         }
285
286         return SR_OK;
287 }
288
289 static int config_list(uint32_t key, GVariant **data,
290         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
291 {
292         /*
293          * TODO: Implement channel group configuration when adding support for
294          * plug-in cards.
295          */
296
297         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
298 }
299
300 static void create_channel_index_list(GSList *channels, GArray **arr)
301 {
302         struct sr_channel *channel;
303         struct channel_context *chanc;
304         GSList *list_elem;
305
306         *arr = g_array_new(FALSE, FALSE, sizeof(unsigned int));
307
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);
312         }
313 }
314
315 /*
316  * TRIG SGL
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
322  *   wrong readings.
323  * SADV AUTO
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.
327  */
328 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
329 {
330         int ret;
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;
336         GArray *ch_list;
337         GSList *channels;
338
339         scpi = sdi->conn;
340         devc = sdi->priv;
341
342         ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
343                                  hp_3457a_receive_data, (void *)sdi);
344         if (ret != SR_OK)
345                 return ret;
346
347         std_session_send_df_header(sdi);
348
349         front_selected = rear_selected = FALSE;
350         devc->active_channels = NULL;
351
352         for (channels = sdi->channels; channels; channels = channels->next) {
353                 channel = channels->data;
354
355                 if (!channel->enabled)
356                         continue;
357
358                 chanc = channel->priv;
359
360                 if (chanc->location == CONN_FRONT)
361                         front_selected = TRUE;
362                 if (chanc->location == CONN_REAR)
363                         rear_selected = TRUE;
364
365                 devc->active_channels = g_slist_append(devc->active_channels, channel);
366         }
367
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);
371                 return SR_ERR_ARG;
372         }
373
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);
378                 return SR_ERR_ARG;
379         }
380         devc->current_channel = devc->active_channels->data;
381
382         hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR);
383
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);
390         }
391
392         /* Start first measurement. */
393         sr_scpi_send(scpi, "TRIG SGL");
394         devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
395         devc->num_samples = 0;
396
397         return SR_OK;
398 }
399
400 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
401 {
402         struct dev_context *devc;
403
404         devc = sdi->priv;
405
406         g_slist_free(devc->active_channels);
407
408         return SR_OK;
409 }
410
411 static struct sr_dev_driver hp_3457a_driver_info = {
412         .name = "hp-3457a",
413         .longname = "HP 3457A",
414         .api_version = 1,
415         .init = std_init,
416         .cleanup = std_cleanup,
417         .scan = scan,
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,
427         .context = NULL,
428 };
429 SR_REGISTER_DEV_DRIVER(hp_3457a_driver_info);