]> sigrok.org Git - libsigrok.git/blob - src/hardware/hp-3457a/api.c
hp-3457a: free memory that was allocated by SCPI get routines
[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 = g_malloc0(sizeof(*front));
56         front->name = g_strdup("Front");
57         front->channels = g_slist_append(front->channels, channel);
58
59         sdi->channel_groups = g_slist_append(sdi->channel_groups, front);
60
61         return chan_idx;
62 }
63
64 static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx,
65                                  const struct rear_card_info *card)
66 {
67         unsigned int i;
68         struct sr_channel *channel;
69         struct sr_channel_group *group;
70         struct channel_context *chanc;
71         char name[16];
72
73         /* When card is NULL, we couldn't identify the type of card. */
74         if (!card)
75                 return chan_idx;
76
77         group = g_malloc0(sizeof(*group));
78         group->priv = NULL;
79         group->name = g_strdup(card->cg_name);
80         sdi->channel_groups = g_slist_append(sdi->channel_groups, group);
81
82         for (i = 0; i < card->num_channels; i++) {
83
84                 chanc = g_malloc(sizeof(*chanc));
85                 chanc->location = CONN_REAR;
86
87                 if (card->type == REAR_TERMINALS) {
88                         chanc->index = -1;
89                         g_snprintf(name, sizeof(name), "%s", card->cg_name);
90                 } else {
91                         chanc->index = i;
92                         g_snprintf(name, sizeof(name), "%s%u", card->cg_name, i);
93                 }
94
95                 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
96                                         FALSE, name);
97                 channel->priv = chanc;
98                 group->channels = g_slist_append(group->channels, channel);
99         }
100
101         return chan_idx;
102 }
103
104 static gchar *get_revision(struct sr_scpi_dev_inst *scpi)
105 {
106         int ret, major, minor;
107         GArray *rev_numbers;
108
109         /* Report a version of '0.0' if we can't parse the response. */
110         major = minor = 0;
111
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);
116         }
117
118         g_array_free(rev_numbers, TRUE);
119
120         return g_strdup_printf("%d.%d", major, minor);
121 }
122
123 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
124 {
125         int ret, idx;
126         char *response;
127         struct sr_dev_inst *sdi;
128         struct dev_context *devc;
129
130         /*
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.
133          */
134         if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK)
135                 return NULL;
136
137         ret = sr_scpi_get_string(scpi, "ID?", &response);
138         if ((ret != SR_OK) || !response)
139                 return NULL;
140
141         if (strcmp(response, "HP3457A")) {
142                 g_free(response);
143                 return NULL;
144         }
145
146         g_free(response);
147
148         devc = g_malloc0(sizeof(struct dev_context));
149         sdi = g_malloc0(sizeof(struct sr_dev_inst));
150         sdi->vendor = g_strdup("Hewlett-Packard");
151         sdi->model = g_strdup("3457A");
152         sdi->version = get_revision(scpi);
153         sdi->conn = scpi;
154         sdi->driver = &hp_3457a_driver_info;
155         sdi->inst_type = SR_INST_SCPI;
156         sdi->priv = devc;
157
158         /* There is no way to probe the measurement mode. It must be set. */
159         devc->measurement_mq = 0;
160         devc->measurement_unit = 0;
161
162         /* Probe rear card option and create channels accordingly (TODO). */
163         devc->rear_card = hp_3457a_probe_rear_card(scpi);
164         idx = 0;
165         idx = create_front_channel(sdi, idx);
166         create_rear_channels(sdi, idx, devc->rear_card);
167
168         return sdi;
169 }
170
171 static GSList *scan(struct sr_dev_driver *di, GSList *options)
172 {
173         return sr_scpi_scan(di->context, options, probe_device);
174 }
175
176 /*
177  * We need to set the HP 3457A to a known state, and there are quite a number
178  * of knobs to tweak. Here's a brief explanation of what's going on. For more
179  * details, print out and consult the user manual.
180  *   PRESET
181  *     Set the instrument to a pre-determined state. This is easier and faster
182  *     than sending a few dozen commands. Some of the PRESET defaults include
183  *     ASCII output format, and synchronous triggering. See user manual for
184  *     more details.
185  *
186  * After the PRESET command, the instrument is in a known state, and only those
187  * parameters for which the default is unsuitable are modified:
188  *   INBUF ON
189  *     Enable the HP-IB input buffer. This allows the instrument to release the
190  *     HP-IB bus before processing the command, and increases throughput on
191  *     GPIB buses with more than one device.
192  *   TRIG HOLD
193  *     Do not trigger new measurements until instructed to do so.
194  */
195 static int dev_open(struct sr_dev_inst *sdi)
196 {
197         struct sr_scpi_dev_inst *scpi = sdi->conn;
198         struct dev_context *devc;
199
200         if (sr_scpi_open(scpi) != SR_OK)
201                 return SR_ERR;
202
203         devc = sdi->priv;
204
205         sr_scpi_send(scpi, "PRESET");
206         sr_scpi_send(scpi, "INBUF ON");
207         sr_scpi_send(scpi, "TRIG HOLD");
208         sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
209
210         return SR_OK;
211 }
212
213 static int dev_close(struct sr_dev_inst *sdi)
214 {
215         struct sr_scpi_dev_inst *scpi = sdi->conn;
216
217         /* Disable scan-advance (preserve relay life). */
218         sr_scpi_send(scpi, "SADV HOLD");
219         /* Switch back to auto-triggering. */
220         sr_scpi_send(scpi, "TRIG AUTO");
221
222         sr_scpi_close(scpi);
223
224         return SR_OK;
225 }
226
227 static int config_get(uint32_t key, GVariant **data,
228         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
229 {
230         struct dev_context *devc;
231
232         (void)cg;
233
234         devc = sdi->priv;
235
236         switch (key) {
237         case SR_CONF_ADC_POWERLINE_CYCLES:
238                 *data = g_variant_new_double(devc->nplc);
239                 break;
240         default:
241                 return SR_ERR_NA;
242         }
243
244         return SR_OK;
245 }
246
247 static int config_set(uint32_t key, GVariant *data,
248         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
249 {
250         enum sr_mq mq;
251         enum sr_mqflag mq_flags;
252         struct dev_context *devc;
253         GVariant *tuple_child;
254
255         (void)cg;
256
257         devc = sdi->priv;
258
259         switch (key) {
260         case SR_CONF_LIMIT_SAMPLES:
261                 devc->limit_samples = g_variant_get_uint64(data);
262                 break;
263         case SR_CONF_MEASURED_QUANTITY:
264                 tuple_child = g_variant_get_child_value(data, 0);
265                 mq = g_variant_get_uint32(tuple_child);
266                 tuple_child = g_variant_get_child_value(data, 1);
267                 mq_flags = g_variant_get_uint64(tuple_child);
268                 g_variant_unref(tuple_child);
269                 return hp_3457a_set_mq(sdi, mq, mq_flags);
270         case SR_CONF_ADC_POWERLINE_CYCLES:
271                 return hp_3457a_set_nplc(sdi, g_variant_get_double(data));
272         default:
273                 return SR_ERR_NA;
274         }
275
276         return SR_OK;
277 }
278
279 static int config_list(uint32_t key, GVariant **data,
280         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
281 {
282         /*
283          * TODO: Implement channel group configuration when adding support for
284          * plug-in cards.
285          */
286
287         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
288 }
289
290 static void create_channel_index_list(GSList *channels, GArray **arr)
291 {
292         struct sr_channel *channel;
293         struct channel_context *chanc;
294         GSList *list_elem;
295
296         *arr = g_array_new(FALSE, FALSE, sizeof(unsigned int));
297
298         for (list_elem = channels; list_elem; list_elem = list_elem->next) {
299                 channel = list_elem->data;
300                 chanc = channel->priv;
301                 g_array_append_val(*arr, chanc->index);
302         }
303 }
304
305 /*
306  * TRIG SGL
307  *   Trigger the first measurement, then hold. We can't let the instrument
308  *   auto-trigger because we read several registers to make a complete
309  *   reading. If the instrument were auto-triggering, we could get the
310  *   reading for sample N, but a new measurement is made and when we read the
311  *   HIRES register, it contains data for sample N+1. This would produce
312  *   wrong readings.
313  * SADV AUTO
314  *   Activate the scan-advance feature. This automatically connects the next
315  *   channel in the scan list to the A/D converter. This way, we do not need to
316  *   occupy the HP-IB bus to send channel select commands.
317  */
318 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
319 {
320         int ret;
321         gboolean front_selected, rear_selected;
322         struct sr_scpi_dev_inst *scpi;
323         struct sr_channel *channel;
324         struct dev_context *devc;
325         struct channel_context *chanc;
326         GArray *ch_list;
327         GSList *channels;
328
329         scpi = sdi->conn;
330         devc = sdi->priv;
331
332         ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
333                                  hp_3457a_receive_data, (void *)sdi);
334         if (ret != SR_OK)
335                 return ret;
336
337         std_session_send_df_header(sdi);
338
339         front_selected = rear_selected = FALSE;
340         devc->active_channels = NULL;
341
342         for (channels = sdi->channels; channels; channels = channels->next) {
343                 channel = channels->data;
344
345                 if (!channel->enabled)
346                         continue;
347
348                 chanc = channel->priv;
349
350                 if (chanc->location == CONN_FRONT)
351                         front_selected = TRUE;
352                 if (chanc->location == CONN_REAR)
353                         rear_selected = TRUE;
354
355                 devc->active_channels = g_slist_append(devc->active_channels, channel);
356         }
357
358         if (front_selected && rear_selected) {
359                 sr_err("Can not use front and rear channels at the same time!");
360                 g_slist_free(devc->active_channels);
361                 return SR_ERR_ARG;
362         }
363
364         devc->num_active_channels = g_slist_length(devc->active_channels);
365         if (!devc->num_active_channels) {
366                 sr_err("Need at least one active channel!");
367                 g_slist_free(devc->active_channels);
368                 return SR_ERR_ARG;
369         }
370         devc->current_channel = devc->active_channels->data;
371
372         hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR);
373
374         /* For plug-in cards, use the scan-advance features to scan channels. */
375         if (rear_selected && (devc->rear_card->card_id != REAR_TERMINALS)) {
376                 create_channel_index_list(devc->active_channels, &ch_list);
377                 hp_3457a_send_scan_list(sdi, (void *)ch_list->data, ch_list->len);
378                 sr_scpi_send(scpi, "SADV AUTO");
379                 g_array_free(ch_list, TRUE);
380         }
381
382         /* Start first measurement. */
383         sr_scpi_send(scpi, "TRIG SGL");
384         devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
385         devc->num_samples = 0;
386
387         return SR_OK;
388 }
389
390 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
391 {
392         struct dev_context *devc;
393
394         devc = sdi->priv;
395
396         g_slist_free(devc->active_channels);
397
398         return SR_OK;
399 }
400
401 static struct sr_dev_driver hp_3457a_driver_info = {
402         .name = "hp-3457a",
403         .longname = "HP 3457A",
404         .api_version = 1,
405         .init = std_init,
406         .cleanup = std_cleanup,
407         .scan = scan,
408         .dev_list = std_dev_list,
409         .dev_clear = std_dev_clear,
410         .config_get = config_get,
411         .config_set = config_set,
412         .config_list = config_list,
413         .dev_open = dev_open,
414         .dev_close = dev_close,
415         .dev_acquisition_start = dev_acquisition_start,
416         .dev_acquisition_stop = dev_acquisition_stop,
417         .context = NULL,
418 };
419 SR_REGISTER_DEV_DRIVER(hp_3457a_driver_info);