]>
Commit | Line | Data |
---|---|---|
b5089195 JH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com> | |
bf5c4d46 | 5 | * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net> |
b5089195 JH |
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 | ||
6ec6c43b | 21 | #include <config.h> |
b5089195 | 22 | #include <glib.h> |
c1aae900 | 23 | #include <libsigrok/libsigrok.h> |
b5089195 | 24 | #include "libsigrok-internal.h" |
bf5c4d46 GS |
25 | #include <math.h> |
26 | #include "protocol.h" | |
27 | #include <stdint.h> | |
28 | #include <string.h> | |
b5089195 | 29 | |
bf5c4d46 GS |
30 | static const uint32_t scanopts[] = { |
31 | SR_CONF_CONN, | |
32 | SR_CONF_SERIALCOMM, | |
33 | }; | |
34 | ||
35 | static const uint32_t drvopts[] = { | |
36 | SR_CONF_LCRMETER, | |
37 | }; | |
beedfa06 | 38 | |
bf5c4d46 GS |
39 | static const uint32_t devopts[] = { |
40 | SR_CONF_CONTINUOUS, | |
e434f624 | 41 | SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET, |
bf5c4d46 GS |
42 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, |
43 | SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST, | |
44 | SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST, | |
beedfa06 | 45 | }; |
b5089195 | 46 | |
bf5c4d46 GS |
47 | static struct sr_dev_inst *scan_packet_check_devinst; |
48 | ||
49 | static void scan_packet_check_setup(struct sr_dev_inst *sdi) | |
50 | { | |
51 | scan_packet_check_devinst = sdi; | |
52 | } | |
53 | ||
54 | static gboolean scan_packet_check_func(const uint8_t *buf) | |
b5089195 | 55 | { |
bf5c4d46 GS |
56 | struct sr_dev_inst *sdi; |
57 | struct dev_context *devc; | |
58 | const struct lcr_info *lcr; | |
59 | struct lcr_parse_info *info; | |
60 | ||
61 | /* Get a reference to the LCR model that is getting checked. */ | |
62 | sdi = scan_packet_check_devinst; | |
63 | if (!sdi) | |
64 | return FALSE; | |
65 | devc = sdi->priv; | |
66 | if (!devc) | |
67 | return FALSE; | |
68 | lcr = devc->lcr_info; | |
69 | if (!lcr) | |
70 | return FALSE; | |
71 | ||
72 | /* Synchronize to the stream of LCR packets. */ | |
73 | if (!lcr->packet_valid(buf)) | |
74 | return FALSE; | |
75 | ||
76 | /* Have LCR packets _processed_, gather current configuration. */ | |
77 | info = &devc->parse_info; | |
78 | memset(info, 0, sizeof(*info)); | |
79 | if (lcr->packet_parse(buf, NULL, NULL, info) == SR_OK) { | |
80 | devc->output_freq = info->output_freq; | |
81 | if (info->circuit_model) | |
82 | devc->circuit_model = info->circuit_model; | |
83 | } | |
84 | ||
85 | return TRUE; | |
b5089195 JH |
86 | } |
87 | ||
c4d2e6fa GS |
88 | static int scan_lcr_port(const struct lcr_info *lcr, |
89 | const char *conn, struct sr_serial_dev_inst *serial) | |
b5089195 | 90 | { |
c4d2e6fa | 91 | size_t len; |
bf5c4d46 | 92 | uint8_t buf[128]; |
bf5c4d46 | 93 | int ret; |
c4d2e6fa | 94 | size_t dropped; |
b5089195 | 95 | |
bf5c4d46 | 96 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
c4d2e6fa | 97 | return SR_ERR_IO; |
bf5c4d46 GS |
98 | sr_info("Probing serial port %s.", conn); |
99 | ||
100 | /* | |
101 | * See if we can detect a device of specified type. | |
102 | * | |
103 | * No supported device provides a means to "identify" yet. No | |
104 | * supported device requires "packet request" yet. They all just | |
105 | * send data periodically. So we check if the packets match the | |
106 | * probed device's expected format. | |
107 | */ | |
cb5cd153 GS |
108 | if (lcr->packet_request) { |
109 | ret = lcr->packet_request(serial); | |
110 | if (ret < 0) { | |
111 | sr_err("Failed to request packet: %d.", ret); | |
c4d2e6fa | 112 | goto scan_port_cleanup; |
cb5cd153 GS |
113 | } |
114 | } | |
bf5c4d46 GS |
115 | len = sizeof(buf); |
116 | ret = serial_stream_detect(serial, buf, &len, | |
117 | lcr->packet_size, lcr->packet_valid, 3000); | |
118 | if (ret != SR_OK) | |
c4d2e6fa | 119 | goto scan_port_cleanup; |
bf5c4d46 GS |
120 | |
121 | /* | |
122 | * If the packets were found to match after more than two packets | |
123 | * got dropped, something is wrong. This is worth warning about, | |
124 | * but isn't fatal. The dropped bytes might be due to nonstandard | |
125 | * cables that ship with some devices. | |
126 | */ | |
127 | dropped = len - lcr->packet_size; | |
128 | if (dropped > 2 * lcr->packet_size) | |
129 | sr_warn("Had to drop unexpected amounts of data."); | |
130 | ||
131 | /* Create a device instance for the found device. */ | |
132 | sr_info("Found %s %s device on port %s.", lcr->vendor, lcr->model, conn); | |
c4d2e6fa GS |
133 | |
134 | scan_port_cleanup: | |
135 | /* Keep serial port open if probe succeeded. */ | |
136 | if (ret != SR_OK) | |
137 | serial_close(serial); | |
138 | ||
139 | return ret; | |
140 | } | |
141 | ||
142 | static struct sr_dev_inst *create_lcr_sdi(struct lcr_info *lcr, | |
143 | struct sr_serial_dev_inst *serial) | |
144 | { | |
145 | struct sr_dev_inst *sdi; | |
146 | struct dev_context *devc; | |
147 | size_t ch_idx; | |
148 | const char **ch_fmts; | |
149 | const char *fmt; | |
150 | char ch_name[8]; | |
151 | ||
bf5c4d46 GS |
152 | sdi = g_malloc0(sizeof(*sdi)); |
153 | sdi->status = SR_ST_INACTIVE; | |
154 | sdi->vendor = g_strdup(lcr->vendor); | |
155 | sdi->model = g_strdup(lcr->model); | |
156 | sdi->inst_type = SR_INST_SERIAL; | |
157 | sdi->conn = serial; | |
158 | devc = g_malloc0(sizeof(*devc)); | |
159 | sdi->priv = devc; | |
160 | devc->lcr_info = lcr; | |
161 | sr_sw_limits_init(&devc->limits); | |
3f5473dd | 162 | ch_fmts = lcr->channel_formats; |
bf5c4d46 | 163 | for (ch_idx = 0; ch_idx < lcr->channel_count; ch_idx++) { |
3f5473dd GS |
164 | fmt = (ch_fmts && ch_fmts[ch_idx]) ? ch_fmts[ch_idx] : "P%zu"; |
165 | snprintf(ch_name, sizeof(ch_name), fmt, ch_idx + 1); | |
bf5c4d46 GS |
166 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, ch_name); |
167 | } | |
c4d2e6fa GS |
168 | |
169 | return sdi; | |
170 | } | |
171 | ||
172 | static int read_lcr_port(struct sr_dev_inst *sdi, | |
173 | const struct lcr_info *lcr, struct sr_serial_dev_inst *serial) | |
174 | { | |
175 | size_t len; | |
176 | uint8_t buf[128]; | |
177 | int ret; | |
bf5c4d46 | 178 | |
66c300c4 GS |
179 | serial_flush(serial); |
180 | if (lcr->packet_request) { | |
181 | ret = lcr->packet_request(serial); | |
182 | if (ret < 0) { | |
183 | sr_err("Failed to request packet: %d.", ret); | |
184 | return ret; | |
185 | } | |
186 | } | |
187 | ||
bf5c4d46 GS |
188 | /* |
189 | * Receive a few more packets (and process them!) to have the | |
190 | * current output frequency and circuit model parameter values | |
191 | * detected. The above "stream detect" phase only synchronized | |
192 | * to the packets by checking their validity, but it cannot | |
193 | * provide details. This phase here runs a modified "checker" | |
194 | * routine which also extracts details from LCR packets after | |
195 | * the device got detected and parameter storage was prepared. | |
196 | */ | |
197 | sr_info("Retrieving current acquisition parameters."); | |
198 | len = sizeof(buf); | |
199 | scan_packet_check_setup(sdi); | |
200 | ret = serial_stream_detect(serial, buf, &len, | |
66c300c4 | 201 | lcr->packet_size, scan_packet_check_func, 1500); |
bf5c4d46 GS |
202 | scan_packet_check_setup(NULL); |
203 | ||
c4d2e6fa GS |
204 | return ret; |
205 | } | |
206 | ||
207 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
208 | { | |
209 | struct lcr_info *lcr; | |
210 | struct sr_config *src; | |
211 | GSList *l, *devices; | |
212 | const char *conn, *serialcomm; | |
213 | struct sr_serial_dev_inst *serial; | |
214 | int ret; | |
215 | struct sr_dev_inst *sdi; | |
216 | ||
217 | lcr = (struct lcr_info *)di; | |
218 | ||
219 | /* Get serial port name and communication parameters. */ | |
220 | conn = NULL; | |
221 | serialcomm = lcr->comm; | |
222 | for (l = options; l; l = l->next) { | |
223 | src = l->data; | |
224 | switch (src->key) { | |
225 | case SR_CONF_CONN: | |
226 | conn = g_variant_get_string(src->data, NULL); | |
227 | break; | |
228 | case SR_CONF_SERIALCOMM: | |
229 | serialcomm = g_variant_get_string(src->data, NULL); | |
230 | break; | |
231 | } | |
232 | } | |
233 | if (!conn) | |
234 | return NULL; | |
235 | ||
236 | devices = NULL; | |
237 | /* TODO Handle ambiguous conn= specs, see serial-dmm. */ | |
238 | ||
239 | /* Open the serial port, check data packets. */ | |
240 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
241 | ret = scan_lcr_port(lcr, conn, serial); | |
242 | if (ret != SR_OK) { | |
243 | /* Probe failed, release 'serial'. */ | |
244 | sr_serial_dev_inst_free(serial); | |
245 | } else { | |
246 | /* Create and return device instance, keep 'serial' alive. */ | |
247 | sdi = create_lcr_sdi(lcr, serial); | |
248 | devices = g_slist_append(devices, sdi); | |
249 | (void)read_lcr_port(sdi, lcr, serial); | |
250 | serial_close(serial); | |
251 | } | |
bf5c4d46 GS |
252 | |
253 | return std_scan_complete(di, devices); | |
254 | } | |
255 | ||
256 | static int config_get(uint32_t key, GVariant **data, | |
257 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
258 | { | |
259 | struct dev_context *devc; | |
260 | const struct lcr_info *lcr; | |
261 | ||
262 | if (!sdi) | |
263 | return SR_ERR_ARG; | |
264 | devc = sdi->priv; | |
265 | ||
266 | switch (key) { | |
267 | case SR_CONF_LIMIT_FRAMES: | |
268 | case SR_CONF_LIMIT_MSEC: | |
269 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
270 | case SR_CONF_OUTPUT_FREQUENCY: | |
271 | *data = g_variant_new_double(devc->output_freq); | |
272 | return SR_OK; | |
273 | case SR_CONF_EQUIV_CIRCUIT_MODEL: | |
274 | if (!devc->circuit_model) | |
275 | return SR_ERR_NA; | |
276 | *data = g_variant_new_string(devc->circuit_model); | |
277 | return SR_OK; | |
278 | default: | |
279 | lcr = devc->lcr_info; | |
280 | if (!lcr) | |
281 | return SR_ERR_NA; | |
282 | if (!lcr->config_get) | |
283 | return SR_ERR_NA; | |
284 | return lcr->config_get(key, data, sdi, cg); | |
285 | } | |
286 | /* UNREACH */ | |
287 | } | |
288 | ||
289 | static int config_set(uint32_t key, GVariant *data, | |
290 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
291 | { | |
292 | struct dev_context *devc; | |
293 | const struct lcr_info *lcr; | |
294 | ||
295 | if (!sdi) | |
296 | return SR_ERR_ARG; | |
297 | devc = sdi->priv; | |
298 | ||
299 | switch (key) { | |
300 | case SR_CONF_LIMIT_FRAMES: | |
301 | case SR_CONF_LIMIT_MSEC: | |
302 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
303 | default: | |
304 | lcr = devc->lcr_info; | |
305 | if (!lcr) | |
306 | return SR_ERR_NA; | |
307 | if (!lcr->config_set) | |
308 | return SR_ERR_NA; | |
309 | return lcr->config_set(key, data, sdi, cg); | |
310 | } | |
311 | /* UNREACH */ | |
312 | } | |
313 | ||
314 | static int config_list(uint32_t key, GVariant **data, | |
315 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
316 | { | |
317 | struct dev_context *devc; | |
318 | const struct lcr_info *lcr; | |
319 | ||
320 | switch (key) { | |
321 | case SR_CONF_SCAN_OPTIONS: | |
322 | case SR_CONF_DEVICE_OPTIONS: | |
323 | return STD_CONFIG_LIST(key, data, sdi, cg, | |
324 | scanopts, drvopts, devopts); | |
325 | default: | |
326 | break; | |
327 | } | |
328 | ||
329 | if (!sdi) | |
330 | return SR_ERR_ARG; | |
331 | devc = sdi->priv; | |
332 | switch (key) { | |
333 | default: | |
334 | lcr = devc->lcr_info; | |
335 | if (!lcr || !lcr->config_list) | |
336 | return SR_ERR_NA; | |
337 | return lcr->config_list(key, data, sdi, cg); | |
338 | } | |
339 | /* UNREACH */ | |
340 | } | |
341 | ||
342 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
343 | { | |
344 | struct dev_context *devc; | |
345 | struct sr_serial_dev_inst *serial; | |
346 | ||
347 | devc = sdi->priv; | |
348 | ||
349 | /* | |
350 | * Clear values that were gathered during scan or in a previous | |
351 | * acquisition, so that this acquisition's data feed immediately | |
352 | * starts with meta packets before first measurement values, and | |
353 | * also communicates subsequent parameter changes. | |
354 | */ | |
355 | devc->output_freq = 0; | |
356 | devc->circuit_model = NULL; | |
357 | ||
358 | sr_sw_limits_acquisition_start(&devc->limits); | |
359 | std_session_send_df_header(sdi); | |
360 | ||
361 | serial = sdi->conn; | |
362 | serial_source_add(sdi->session, serial, G_IO_IN, 50, | |
363 | lcr_receive_data, (void *)sdi); | |
364 | ||
365 | return SR_OK; | |
b5089195 JH |
366 | } |
367 | ||
beedfa06 | 368 | #define LCR_ES51919(id, vendor, model) \ |
bf5c4d46 | 369 | &((struct lcr_info) { \ |
beedfa06 GS |
370 | { \ |
371 | .name = id, \ | |
372 | .longname = vendor " " model, \ | |
373 | .api_version = 1, \ | |
374 | .init = std_init, \ | |
375 | .cleanup = std_cleanup, \ | |
376 | .scan = scan, \ | |
377 | .dev_list = std_dev_list, \ | |
bf5c4d46 GS |
378 | .dev_clear = std_dev_clear, \ |
379 | .config_get = config_get, \ | |
380 | .config_set = config_set, \ | |
381 | .config_list = config_list, \ | |
beedfa06 GS |
382 | .dev_open = std_serial_dev_open, \ |
383 | .dev_close = std_serial_dev_close, \ | |
bf5c4d46 | 384 | .dev_acquisition_start = dev_acquisition_start, \ |
beedfa06 GS |
385 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, \ |
386 | .context = NULL, \ | |
387 | }, \ | |
3f5473dd | 388 | vendor, model, ES51919_CHANNEL_COUNT, NULL, \ |
bf5c4d46 | 389 | ES51919_COMM_PARAM, ES51919_PACKET_SIZE, \ |
cb5cd153 | 390 | 0, NULL, \ |
bf5c4d46 GS |
391 | es51919_packet_valid, es51919_packet_parse, \ |
392 | NULL, NULL, es51919_config_list, \ | |
beedfa06 GS |
393 | }).di |
394 | ||
395 | SR_REGISTER_DEV_DRIVER_LIST(lcr_es51919_drivers, | |
396 | LCR_ES51919("deree-de5000", "DER EE", "DE-5000"), | |
ca9f8961 | 397 | LCR_ES51919("mastech-ms5308", "MASTECH", "MS5308"), |
6c62c605 | 398 | LCR_ES51919("peaktech-2170", "PeakTech", "2170"), |
6e407e98 | 399 | LCR_ES51919("uni-t-ut612", "UNI-T", "UT612"), |
beedfa06 | 400 | ); |
b50970a5 GS |
401 | |
402 | #define LCR_VC4080(id, vendor, model) \ | |
403 | &((struct lcr_info) { \ | |
404 | { \ | |
405 | .name = id, \ | |
406 | .longname = vendor " " model, \ | |
407 | .api_version = 1, \ | |
408 | .init = std_init, \ | |
409 | .cleanup = std_cleanup, \ | |
410 | .scan = scan, \ | |
411 | .dev_list = std_dev_list, \ | |
412 | .dev_clear = std_dev_clear, \ | |
413 | .config_get = config_get, \ | |
414 | .config_set = config_set, \ | |
415 | .config_list = config_list, \ | |
416 | .dev_open = std_serial_dev_open, \ | |
417 | .dev_close = std_serial_dev_close, \ | |
418 | .dev_acquisition_start = dev_acquisition_start, \ | |
419 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, \ | |
420 | .context = NULL, \ | |
421 | }, \ | |
422 | vendor, model, \ | |
423 | VC4080_CHANNEL_COUNT, vc4080_channel_formats, \ | |
424 | VC4080_COMM_PARAM, VC4080_PACKET_SIZE, \ | |
425 | 500, vc4080_packet_request, \ | |
426 | vc4080_packet_valid, vc4080_packet_parse, \ | |
427 | NULL, NULL, vc4080_config_list, \ | |
428 | }).di | |
429 | ||
430 | SR_REGISTER_DEV_DRIVER_LIST(lcr_vc4080_drivers, | |
431 | LCR_VC4080("peaktech-2165", "PeakTech", "2165"), | |
432 | LCR_VC4080("voltcraft-4080", "Voltcraft", "4080"), | |
433 | ); |