]> sigrok.org Git - libsigrok.git/blob - src/hardware/fluke-45/api.c
fluke-45: disable ECHO test, it confuses other SCPI devices
[libsigrok.git] / src / hardware / fluke-45 / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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>
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"
32 #include "protocol.h"
33
34 /*
35  * This test violates the SCPI protocol, and confuses other devices.
36  * Disable it for now, until a better location was found.
37  */
38 #define ECHO_TEST 0
39
40 static const uint32_t scanopts[] = {
41         SR_CONF_CONN,
42         SR_CONF_SERIALCOMM,
43 };
44
45 static const uint32_t drvopts[] = {
46         SR_CONF_MULTIMETER,
47 };
48
49 static const uint32_t devopts[] = {
50         SR_CONF_CONTINUOUS,
51         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
52         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
53 };
54
55 /* Vendor, model, number of channels, poll period */
56 static const struct fluke_scpi_dmm_model supported_models[] = {
57         { "FLUKE", "45", 2, 0 },
58 };
59
60 static struct sr_dev_driver fluke_45_driver_info;
61
62 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
63 {
64         struct dev_context *devc;
65         struct sr_dev_inst *sdi;
66         struct sr_scpi_hw_info *hw_info;
67         const struct scpi_command *cmdset = fluke_45_cmdset;
68         unsigned int i;
69         const struct fluke_scpi_dmm_model *model = NULL;
70         gchar *channel_name;
71 #if ECHO_TEST
72         char *response;
73 #endif
74
75         sdi = g_malloc0(sizeof(struct sr_dev_inst));
76         sdi->conn = scpi;
77
78 #if ECHO_TEST
79         /* Test for serial port ECHO enabled. */
80         response = NULL;
81         sr_scpi_get_string(scpi, "ECHO-TEST", &response);
82         if (response && strcmp(response, "ECHO-TEST") == 0) {
83                 sr_err("Serial port ECHO is ON. Please turn it OFF!");
84                 return NULL;
85         }
86 #endif
87
88         /* Get device IDN. */
89         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
90                 sr_info("Couldn't get IDN response, retrying.");
91                 sr_scpi_close(scpi);
92                 sr_scpi_open(scpi);
93                 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
94                         sr_info("Couldn't get IDN response.");
95                         return NULL;
96                 }
97         }
98
99         /* Check IDN. */
100         for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
101                 if (!g_ascii_strcasecmp(hw_info->manufacturer,
102                                         supported_models[i].vendor) &&
103                                 !strcmp(hw_info->model, supported_models[i].model)) {
104                         model = &supported_models[i];
105                         break;
106                 }
107         }
108         if (!model) {
109                 sr_scpi_hw_info_free(hw_info);
110                 return NULL;
111         }
112
113         /* Set up device parameters. */
114         sdi->vendor = g_strdup(model->vendor);
115         sdi->model = g_strdup(model->model);
116         sdi->version = g_strdup(hw_info->firmware_version);
117         sdi->serial_num = g_strdup(hw_info->serial_number);
118         sdi->conn = scpi;
119         sdi->driver = &fluke_45_driver_info;
120         sdi->inst_type = SR_INST_SCPI;
121
122         devc = g_malloc0(sizeof(struct dev_context));
123         devc->num_channels = model->num_channels;
124         devc->cmdset = cmdset;
125
126         /* Create channels. */
127         for (i = 0; i < devc->num_channels; i++) {
128                 channel_name = g_strdup_printf("P%d", i + 1);
129                 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
130         }
131
132         sdi->priv = devc;
133
134         return sdi;
135 }
136
137 static GSList *scan(struct sr_dev_driver *di, GSList *options)
138 {
139         return sr_scpi_scan(di->context, options, probe_device);
140 }
141
142 static int dev_open(struct sr_dev_inst *sdi)
143 {
144         struct sr_scpi_dev_inst *scpi;
145         int ret;
146
147         scpi = sdi->conn;
148
149         if ((ret = sr_scpi_open(scpi) < 0)) {
150                 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
151                 return SR_ERR;
152         }
153
154         return SR_OK;
155 }
156
157 static int dev_close(struct sr_dev_inst *sdi)
158 {
159         struct sr_scpi_dev_inst *scpi;
160
161         scpi = sdi->conn;
162
163         if (!scpi)
164                 return SR_ERR_BUG;
165
166         return sr_scpi_close(scpi);
167 }
168
169 static int config_set(uint32_t key, GVariant *data,
170         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
171 {
172         struct dev_context *devc;
173
174         (void)cg;
175
176         devc = sdi->priv;
177
178         return sr_sw_limits_config_set(&devc->limits, key, data);
179 }
180
181 static int config_list(uint32_t key, GVariant **data,
182         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
183 {
184         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
185 }
186
187 static int config_get(uint32_t key, GVariant **data,
188         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
189 {
190         struct dev_context *devc = sdi->priv;
191
192         (void)cg;
193
194         return sr_sw_limits_config_get(&devc->limits, key, data);
195 }
196
197 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
198 {
199         struct sr_scpi_dev_inst *scpi;
200         struct dev_context *devc;
201         int ret;
202
203         scpi = sdi->conn;
204         devc = sdi->priv;
205
206         sr_sw_limits_acquisition_start(&devc->limits);
207         std_session_send_df_header(sdi);
208
209         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
210                         fl45_scpi_receive_data, (void *)sdi)) != SR_OK)
211                 return ret;
212
213         return SR_OK;
214 }
215
216 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
217 {
218         struct sr_scpi_dev_inst *scpi;
219         double d;
220
221         scpi = sdi->conn;
222
223         /*
224          * A requested value is certainly on the way. Retrieve it now,
225          * to avoid leaving the device in a state where it's not expecting
226          * commands.
227          */
228         sr_scpi_get_double(scpi, NULL, &d);
229         sr_scpi_source_remove(sdi->session, scpi);
230
231         std_session_send_df_end(sdi);
232
233         return SR_OK;
234 }
235
236 static struct sr_dev_driver fluke_45_driver_info = {
237         .name = "fluke-45",
238         .longname = "Fluke 45",
239         .api_version = 1,
240         .init = std_init,
241         .cleanup = std_cleanup,
242         .scan = scan,
243         .dev_list = std_dev_list,
244         .dev_clear = std_dev_clear,
245         .config_get = config_get,
246         .config_set = config_set,
247         .config_list = config_list,
248         .dev_open = dev_open,
249         .dev_close = dev_close,
250         .dev_acquisition_start = dev_acquisition_start,
251         .dev_acquisition_stop = dev_acquisition_stop,
252         .context = NULL,
253 };
254 SR_REGISTER_DEV_DRIVER(fluke_45_driver_info);