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