]> sigrok.org Git - libsigrok.git/blame - src/hardware/fluke-45/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / fluke-45 / api.c
CommitLineData
e756c595
J
1/*
2 * This file is part of the libsigrok project.
3 *
ab2b21fb 4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
e756c595
J
5 * Copyright (C) 2017 John Chajecki <subs@qcontinuum.plus.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
ab2b21fb
J
22#include <glib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <string.h>
27#include <stdint.h>
28#include <stdbool.h>
29#include <libsigrok/libsigrok.h>
30#include "libsigrok-internal.h"
31#include "scpi.h"
e756c595
J
32#include "protocol.h"
33
ab2b21fb
J
34static const uint32_t scanopts[] = {
35 SR_CONF_CONN,
36 SR_CONF_SERIALCOMM,
37};
e756c595 38
ab2b21fb
J
39static const uint32_t drvopts[] = {
40 SR_CONF_MULTIMETER,
41};
e756c595 42
ab2b21fb
J
43static const uint32_t devopts[] = {
44 SR_CONF_CONTINUOUS,
45 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
46 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
47};
e756c595 48
ab2b21fb
J
49/* Vendor, model, number of channels, poll period */
50static const struct fluke_scpi_dmm_model supported_models[] = {
51 { "FLUKE", "45", 2, 0 },
52};
e756c595 53
ab2b21fb 54static struct sr_dev_driver fluke_45_driver_info;
e756c595 55
ab2b21fb 56static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
e756c595 57{
ab2b21fb
J
58 struct dev_context *devc;
59 struct sr_dev_inst *sdi;
60 struct sr_scpi_hw_info *hw_info;
61 const struct scpi_command *cmdset = fluke_45_cmdset;
62 unsigned int i;
63 const struct fluke_scpi_dmm_model *model = NULL;
64 gchar *channel_name;
e756c595 65
ab2b21fb
J
66 /* Get device IDN. */
67 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
c9cfcd25 68 sr_scpi_hw_info_free(hw_info);
ab2b21fb
J
69 sr_info("Couldn't get IDN response, retrying.");
70 sr_scpi_close(scpi);
71 sr_scpi_open(scpi);
72 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
c9cfcd25 73 sr_scpi_hw_info_free(hw_info);
ab2b21fb
J
74 sr_info("Couldn't get IDN response.");
75 return NULL;
76 }
77 }
e756c595 78
ab2b21fb
J
79 /* Check IDN. */
80 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
81 if (!g_ascii_strcasecmp(hw_info->manufacturer,
82 supported_models[i].vendor) &&
83 !strcmp(hw_info->model, supported_models[i].model)) {
84 model = &supported_models[i];
85 break;
86 }
87 }
88 if (!model) {
89 sr_scpi_hw_info_free(hw_info);
90 return NULL;
91 }
92
93 /* Set up device parameters. */
c10b0276 94 sdi = g_malloc0(sizeof(struct sr_dev_inst));
ab2b21fb
J
95 sdi->vendor = g_strdup(model->vendor);
96 sdi->model = g_strdup(model->model);
97 sdi->version = g_strdup(hw_info->firmware_version);
98 sdi->serial_num = g_strdup(hw_info->serial_number);
99 sdi->conn = scpi;
100 sdi->driver = &fluke_45_driver_info;
101 sdi->inst_type = SR_INST_SCPI;
c10b0276 102 sr_scpi_hw_info_free(hw_info);
ab2b21fb
J
103
104 devc = g_malloc0(sizeof(struct dev_context));
105 devc->num_channels = model->num_channels;
106 devc->cmdset = cmdset;
c10b0276 107 sdi->priv = devc;
ab2b21fb
J
108
109 /* Create channels. */
110 for (i = 0; i < devc->num_channels; i++) {
111 channel_name = g_strdup_printf("P%d", i + 1);
112 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
113 }
114
ab2b21fb 115 return sdi;
e756c595
J
116}
117
ab2b21fb
J
118static GSList *scan(struct sr_dev_driver *di, GSList *options)
119{
120 return sr_scpi_scan(di->context, options, probe_device);
121}
122
123static int dev_open(struct sr_dev_inst *sdi)
e756c595 124{
ab2b21fb
J
125 struct sr_scpi_dev_inst *scpi;
126 int ret;
e756c595 127
ab2b21fb
J
128 scpi = sdi->conn;
129
130 if ((ret = sr_scpi_open(scpi) < 0)) {
131 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
132 return SR_ERR;
133 }
e756c595
J
134
135 return SR_OK;
136}
137
ab2b21fb 138static int dev_close(struct sr_dev_inst *sdi)
e756c595 139{
ab2b21fb 140 struct sr_scpi_dev_inst *scpi;
e756c595 141
ab2b21fb 142 scpi = sdi->conn;
e756c595 143
ab2b21fb
J
144 if (!scpi)
145 return SR_ERR_BUG;
e756c595 146
ab2b21fb 147 return sr_scpi_close(scpi);
e756c595
J
148}
149
150static int config_set(uint32_t key, GVariant *data,
151 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
152{
ab2b21fb 153 struct dev_context *devc;
e756c595 154
e756c595
J
155 (void)cg;
156
ab2b21fb 157 devc = sdi->priv;
e756c595 158
ab2b21fb 159 return sr_sw_limits_config_set(&devc->limits, key, data);
e756c595
J
160}
161
162static int config_list(uint32_t key, GVariant **data,
163 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
164{
ab2b21fb
J
165 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
166}
e756c595 167
ab2b21fb
J
168static int config_get(uint32_t key, GVariant **data,
169 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
170{
171 struct dev_context *devc = sdi->priv;
e756c595 172
ab2b21fb 173 (void)cg;
e756c595 174
ab2b21fb 175 return sr_sw_limits_config_get(&devc->limits, key, data);
e756c595
J
176}
177
178static int dev_acquisition_start(const struct sr_dev_inst *sdi)
179{
ab2b21fb
J
180 struct sr_scpi_dev_inst *scpi;
181 struct dev_context *devc;
182 int ret;
183
184 scpi = sdi->conn;
185 devc = sdi->priv;
186
187 sr_sw_limits_acquisition_start(&devc->limits);
188 std_session_send_df_header(sdi);
e756c595 189
ab2b21fb
J
190 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
191 fl45_scpi_receive_data, (void *)sdi)) != SR_OK)
192 return ret;
e756c595
J
193
194 return SR_OK;
195}
196
197static int dev_acquisition_stop(struct sr_dev_inst *sdi)
198{
ab2b21fb
J
199 struct sr_scpi_dev_inst *scpi;
200 double d;
e756c595 201
ab2b21fb
J
202 scpi = sdi->conn;
203
204 /*
205 * A requested value is certainly on the way. Retrieve it now,
206 * to avoid leaving the device in a state where it's not expecting
207 * commands.
208 */
209 sr_scpi_get_double(scpi, NULL, &d);
210 sr_scpi_source_remove(sdi->session, scpi);
211
212 std_session_send_df_end(sdi);
e756c595
J
213
214 return SR_OK;
215}
216
ab2b21fb 217static struct sr_dev_driver fluke_45_driver_info = {
e756c595
J
218 .name = "fluke-45",
219 .longname = "Fluke 45",
220 .api_version = 1,
221 .init = std_init,
222 .cleanup = std_cleanup,
223 .scan = scan,
224 .dev_list = std_dev_list,
225 .dev_clear = std_dev_clear,
226 .config_get = config_get,
227 .config_set = config_set,
228 .config_list = config_list,
229 .dev_open = dev_open,
230 .dev_close = dev_close,
231 .dev_acquisition_start = dev_acquisition_start,
232 .dev_acquisition_stop = dev_acquisition_stop,
233 .context = NULL,
234};
e756c595 235SR_REGISTER_DEV_DRIVER(fluke_45_driver_info);