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