]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com> | |
5 | * Copyright (C) 2018-2019 Frank Stettner <frank-stettner@gmx.net> | |
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 | ||
23 | #include <ctype.h> | |
24 | ||
25 | #include "protocol.h" | |
26 | ||
27 | static const uint32_t scanopts[] = { | |
28 | SR_CONF_CONN, | |
29 | SR_CONF_SERIALCOMM, | |
30 | SR_CONF_FORCE_DETECT, | |
31 | }; | |
32 | ||
33 | static const uint32_t drvopts[] = { | |
34 | SR_CONF_POWER_SUPPLY, | |
35 | }; | |
36 | ||
37 | static const uint32_t devopts[] = { | |
38 | SR_CONF_CONN | SR_CONF_GET, | |
39 | SR_CONF_CONTINUOUS, | |
40 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
41 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
42 | SR_CONF_VOLTAGE | SR_CONF_GET, | |
43 | SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
44 | SR_CONF_CURRENT | SR_CONF_GET, | |
45 | SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
46 | SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
47 | SR_CONF_REGULATION | SR_CONF_GET, | |
48 | SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
49 | SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
50 | }; | |
51 | ||
52 | /* Voltage and current ranges. Values are: Min, max, step. */ | |
53 | static const double volts_30[] = { 0, 31, 0.01, }; | |
54 | static const double volts_60[] = { 0, 61, 0.01, }; | |
55 | static const double amps_3[] = { 0, 3.1, 0.001, }; | |
56 | static const double amps_5[] = { 0, 5.1, 0.001, }; | |
57 | ||
58 | static const struct korad_kaxxxxp_model models[] = { | |
59 | /* Vendor, model name, ID reply, channels, voltage, current, quirks. */ | |
60 | {"Korad", "KA3005P", "", 1, volts_30, amps_5, | |
61 | KORAD_QUIRK_ID_TRAILING}, | |
62 | {"Korad", "KD3005P", "", 1, volts_30, amps_5, 0}, | |
63 | {"Korad", "KD6005P", "", 1, volts_60, amps_5, 0}, | |
64 | {"RND", "320-KA3005P", "", 1, volts_30, amps_5, 0}, | |
65 | {"RND", "320-KD3005P", "", 1, volts_30, amps_5, 0}, | |
66 | {"Stamos Soldering", "S-LS-31", "", 1, volts_30, amps_5, | |
67 | KORAD_QUIRK_ID_NO_VENDOR}, | |
68 | {"Tenma", "72-2535", "", 1, volts_30, amps_3, 0}, | |
69 | {"Tenma", "72-2540", "", 1, volts_30, amps_5, 0}, | |
70 | {"Tenma", "72-2550", "", 1, volts_60, amps_3, 0}, | |
71 | {"Tenma", "72-2710", "", 1, volts_30, amps_5, 0}, | |
72 | {"Velleman", "LABPS3005D", "", 1, volts_30, amps_5, | |
73 | KORAD_QUIRK_LABPS_OVP_EN}, | |
74 | {"Velleman", "PS3005D", "", 1, volts_30, amps_5, 0}, | |
75 | ALL_ZERO | |
76 | }; | |
77 | ||
78 | /* | |
79 | * Bump this when adding new models[] above. Make sure the text buffer | |
80 | * for the ID response can hold the longest sequence that we expect in | |
81 | * the field which consists of: vendor + model [ + version ][ + serno ]. | |
82 | * Don't be too generous here, the maximum receive buffer size affects | |
83 | * the timeout within which the first response character is expected. | |
84 | */ | |
85 | static const size_t id_text_buffer_size = 48; | |
86 | ||
87 | /* | |
88 | * Check whether the device's "*IDN?" response matches a supported model. | |
89 | * The caller already stripped off the optional serial number. | |
90 | */ | |
91 | static gboolean model_matches(const struct korad_kaxxxxp_model *model, | |
92 | const char *id_text) | |
93 | { | |
94 | gboolean matches; | |
95 | gboolean opt_version, skip_vendor, accept_trail; | |
96 | const char *want; | |
97 | ||
98 | if (!model) | |
99 | return FALSE; | |
100 | ||
101 | /* | |
102 | * When the models[] entry contains a specific response text, | |
103 | * then expect to see this very text in literal form. This | |
104 | * lets the driver map weird and untypical responses to a | |
105 | * specific set of display texts for vendor and model names. | |
106 | * Accept an optionally trailing version if models[] says so. | |
107 | */ | |
108 | if (model->id && model->id[0]) { | |
109 | opt_version = model->quirks & KORAD_QUIRK_ID_OPT_VERSION; | |
110 | if (!opt_version) { | |
111 | matches = g_strcmp0(id_text, model->id) == 0; | |
112 | if (!matches) | |
113 | return FALSE; | |
114 | sr_dbg("Matches expected ID text: '%s'.", model->id); | |
115 | return TRUE; | |
116 | } | |
117 | matches = g_str_has_prefix(id_text, model->id); | |
118 | if (!matches) | |
119 | return FALSE; | |
120 | id_text += strlen(model->id); | |
121 | while (isspace((int)*id_text)) | |
122 | id_text++; | |
123 | if (*id_text == 'V') { | |
124 | id_text++; | |
125 | while (*id_text == '.' || isdigit((int)*id_text)) | |
126 | id_text++; | |
127 | while (isspace((int)*id_text)) | |
128 | id_text++; | |
129 | } | |
130 | if (*id_text) | |
131 | return FALSE; | |
132 | sr_dbg("Matches expected ID text [vers]: '%s'.", model->id); | |
133 | return TRUE; | |
134 | } | |
135 | ||
136 | /* | |
137 | * A more generic approach which covers most devices: Check | |
138 | * for the very vendor and model names which also are shown | |
139 | * to users (the display texts). Weakened to match responses | |
140 | * more widely: Case insensitive checks, optional whitespace | |
141 | * in responses, optional version details. Optional trailing | |
142 | * garbage. Optional omission of the vendor name. Shall match | |
143 | * all the devices which were individually listed in earlier | |
144 | * implementations of the driver, and shall also match firmware | |
145 | * versions that were not listed before. | |
146 | */ | |
147 | skip_vendor = model->quirks & KORAD_QUIRK_ID_NO_VENDOR; | |
148 | accept_trail = model->quirks & KORAD_QUIRK_ID_TRAILING; | |
149 | if (!skip_vendor) { | |
150 | want = model->vendor; | |
151 | matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0; | |
152 | if (!matches) | |
153 | return FALSE; | |
154 | id_text += strlen(want); | |
155 | while (isspace((int)*id_text)) | |
156 | id_text++; | |
157 | } | |
158 | want = model->name; | |
159 | matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0; | |
160 | if (!matches) | |
161 | return FALSE; | |
162 | id_text += strlen(want); | |
163 | while (isspace((int)*id_text)) | |
164 | id_text++; | |
165 | if (*id_text == 'V') { | |
166 | /* TODO Isolate and (also) return version details? */ | |
167 | id_text++; | |
168 | while (*id_text == '.' || isdigit((int)*id_text)) | |
169 | id_text++; | |
170 | while (isspace((int)*id_text)) | |
171 | id_text++; | |
172 | } | |
173 | if (accept_trail) { | |
174 | /* | |
175 | * TODO Determine how many non-printables to accept here | |
176 | * and how strict to check for "known" garbage variants. | |
177 | */ | |
178 | switch (*id_text) { | |
179 | case '\x01': | |
180 | case '\xbc': | |
181 | id_text++; | |
182 | break; | |
183 | case '\x00': | |
184 | /* EMPTY */ | |
185 | break; | |
186 | default: | |
187 | return FALSE; | |
188 | } | |
189 | } | |
190 | if (*id_text) | |
191 | return FALSE; | |
192 | sr_dbg("Matches generic '[vendor] model [vers] [trail]' pattern."); | |
193 | return TRUE; | |
194 | } | |
195 | ||
196 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
197 | { | |
198 | static const char *serno_prefix = " SN:"; | |
199 | ||
200 | struct dev_context *devc; | |
201 | GSList *l; | |
202 | struct sr_dev_inst *sdi; | |
203 | struct sr_config *src; | |
204 | const char *conn, *serialcomm; | |
205 | const char *force_detect; | |
206 | struct sr_serial_dev_inst *serial; | |
207 | size_t i; | |
208 | char reply[50]; | |
209 | int ret; | |
210 | const struct korad_kaxxxxp_model *model; | |
211 | size_t len; | |
212 | char *serno; | |
213 | ||
214 | conn = NULL; | |
215 | serialcomm = NULL; | |
216 | force_detect = NULL; | |
217 | ||
218 | for (l = options; l; l = l->next) { | |
219 | src = l->data; | |
220 | switch (src->key) { | |
221 | case SR_CONF_CONN: | |
222 | conn = g_variant_get_string(src->data, NULL); | |
223 | break; | |
224 | case SR_CONF_SERIALCOMM: | |
225 | serialcomm = g_variant_get_string(src->data, NULL); | |
226 | break; | |
227 | case SR_CONF_FORCE_DETECT: | |
228 | force_detect = g_variant_get_string(src->data, NULL); | |
229 | break; | |
230 | default: | |
231 | sr_err("Unknown option %d, skipping.", src->key); | |
232 | break; | |
233 | } | |
234 | } | |
235 | ||
236 | if (!conn) | |
237 | return NULL; | |
238 | if (!serialcomm) | |
239 | serialcomm = "9600/8n1"; | |
240 | if (force_detect && !*force_detect) | |
241 | force_detect = NULL; | |
242 | ||
243 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
244 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
245 | return NULL; | |
246 | ||
247 | /* Communicate the identification request. */ | |
248 | len = id_text_buffer_size; | |
249 | if (len > sizeof(reply) - 1) | |
250 | len = sizeof(reply) - 1; | |
251 | sr_dbg("Want max %zu bytes.", len); | |
252 | ||
253 | ret = korad_kaxxxxp_send_cmd(serial, "*IDN?"); | |
254 | if (ret < 0) | |
255 | return NULL; | |
256 | ||
257 | ret = korad_kaxxxxp_read_chars(serial, len, reply); | |
258 | if (ret < 0) | |
259 | return NULL; | |
260 | sr_dbg("Received: %d, %s", ret, reply); | |
261 | ||
262 | /* | |
263 | * Isolate the optional serial number at the response's end. | |
264 | * Lookup the response's model ID in the list of known models. | |
265 | */ | |
266 | serno = g_strrstr(reply, serno_prefix); | |
267 | if (serno) { | |
268 | *serno = '\0'; | |
269 | serno += strlen(serno_prefix); | |
270 | } | |
271 | ||
272 | model = NULL; | |
273 | for (i = 0; models[i].name; i++) { | |
274 | if (!model_matches(&models[i], reply)) | |
275 | continue; | |
276 | model = &models[i]; | |
277 | break; | |
278 | } | |
279 | if (!model && force_detect) { | |
280 | sr_warn("Found unknown model ID '%s', trying '%s' spec.", | |
281 | reply, force_detect); | |
282 | for (i = 0; models[i].name; i++) { | |
283 | if (!model_matches(&models[i], force_detect)) | |
284 | continue; | |
285 | sr_info("Found replacement, using it instead."); | |
286 | model = &models[i]; | |
287 | break; | |
288 | } | |
289 | } | |
290 | if (!model) { | |
291 | sr_err("Found unknown model ID '%s', aborting.", reply); | |
292 | return NULL; | |
293 | } | |
294 | sr_dbg("Found: %s %s (idx %zu).", model->vendor, model->name, | |
295 | model - &models[0]); | |
296 | ||
297 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
298 | sdi->status = SR_ST_INACTIVE; | |
299 | sdi->vendor = g_strdup(model->vendor); | |
300 | sdi->model = g_strdup(model->name); | |
301 | if (serno) | |
302 | sdi->serial_num = g_strdup(serno); | |
303 | sdi->inst_type = SR_INST_SERIAL; | |
304 | sdi->conn = serial; | |
305 | sdi->connection_id = g_strdup(conn); | |
306 | ||
307 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V"); | |
308 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I"); | |
309 | ||
310 | devc = g_malloc0(sizeof(struct dev_context)); | |
311 | sr_sw_limits_init(&devc->limits); | |
312 | g_mutex_init(&devc->rw_mutex); | |
313 | devc->model = model; | |
314 | devc->req_sent_at = 0; | |
315 | devc->cc_mode_1_changed = FALSE; | |
316 | devc->cc_mode_2_changed = FALSE; | |
317 | devc->output_enabled_changed = FALSE; | |
318 | devc->ocp_enabled_changed = FALSE; | |
319 | devc->ovp_enabled_changed = FALSE; | |
320 | sdi->priv = devc; | |
321 | ||
322 | /* Get current status of device. */ | |
323 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) | |
324 | goto exit_err; | |
325 | ||
326 | serial_close(serial); | |
327 | ||
328 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
329 | ||
330 | exit_err: | |
331 | sr_dev_inst_free(sdi); | |
332 | g_free(devc); | |
333 | sr_dbg("Scan failed."); | |
334 | ||
335 | return NULL; | |
336 | } | |
337 | ||
338 | static int config_get(uint32_t key, GVariant **data, | |
339 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
340 | { | |
341 | struct dev_context *devc; | |
342 | ||
343 | (void)cg; | |
344 | ||
345 | if (!sdi || !data) | |
346 | return SR_ERR_ARG; | |
347 | ||
348 | devc = sdi->priv; | |
349 | ||
350 | switch (key) { | |
351 | case SR_CONF_LIMIT_SAMPLES: | |
352 | case SR_CONF_LIMIT_MSEC: | |
353 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
354 | case SR_CONF_CONN: | |
355 | *data = g_variant_new_string(sdi->connection_id); | |
356 | break; | |
357 | case SR_CONF_VOLTAGE: | |
358 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE, devc); | |
359 | *data = g_variant_new_double(devc->voltage); | |
360 | break; | |
361 | case SR_CONF_VOLTAGE_TARGET: | |
362 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc); | |
363 | *data = g_variant_new_double(devc->voltage_target); | |
364 | break; | |
365 | case SR_CONF_CURRENT: | |
366 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT, devc); | |
367 | *data = g_variant_new_double(devc->current); | |
368 | break; | |
369 | case SR_CONF_CURRENT_LIMIT: | |
370 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc); | |
371 | *data = g_variant_new_double(devc->current_limit); | |
372 | break; | |
373 | case SR_CONF_ENABLED: | |
374 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OUTPUT, devc); | |
375 | *data = g_variant_new_boolean(devc->output_enabled); | |
376 | break; | |
377 | case SR_CONF_REGULATION: | |
378 | /* Dual channel not supported. */ | |
379 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_STATUS, devc); | |
380 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
381 | break; | |
382 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
383 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OCP, devc); | |
384 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
385 | break; | |
386 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
387 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OVP, devc); | |
388 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
389 | break; | |
390 | default: | |
391 | return SR_ERR_NA; | |
392 | } | |
393 | ||
394 | return SR_OK; | |
395 | } | |
396 | ||
397 | static int config_set(uint32_t key, GVariant *data, | |
398 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
399 | { | |
400 | struct dev_context *devc; | |
401 | double dval; | |
402 | gboolean bval; | |
403 | ||
404 | (void)cg; | |
405 | ||
406 | devc = sdi->priv; | |
407 | ||
408 | switch (key) { | |
409 | case SR_CONF_LIMIT_MSEC: | |
410 | case SR_CONF_LIMIT_SAMPLES: | |
411 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
412 | case SR_CONF_VOLTAGE_TARGET: | |
413 | dval = g_variant_get_double(data); | |
414 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
415 | return SR_ERR_ARG; | |
416 | devc->set_voltage_target = dval; | |
417 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc) < 0) | |
418 | return SR_ERR; | |
419 | break; | |
420 | case SR_CONF_CURRENT_LIMIT: | |
421 | dval = g_variant_get_double(data); | |
422 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
423 | return SR_ERR_ARG; | |
424 | devc->set_current_limit = dval; | |
425 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc) < 0) | |
426 | return SR_ERR; | |
427 | break; | |
428 | case SR_CONF_ENABLED: | |
429 | bval = g_variant_get_boolean(data); | |
430 | /* Set always so it is possible turn off with sigrok-cli. */ | |
431 | devc->set_output_enabled = bval; | |
432 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OUTPUT, devc) < 0) | |
433 | return SR_ERR; | |
434 | break; | |
435 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
436 | bval = g_variant_get_boolean(data); | |
437 | devc->set_ocp_enabled = bval; | |
438 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OCP, devc) < 0) | |
439 | return SR_ERR; | |
440 | break; | |
441 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
442 | bval = g_variant_get_boolean(data); | |
443 | devc->set_ovp_enabled = bval; | |
444 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OVP, devc) < 0) | |
445 | return SR_ERR; | |
446 | break; | |
447 | default: | |
448 | return SR_ERR_NA; | |
449 | } | |
450 | ||
451 | return SR_OK; | |
452 | } | |
453 | ||
454 | static int config_list(uint32_t key, GVariant **data, | |
455 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
456 | { | |
457 | struct dev_context *devc; | |
458 | ||
459 | devc = (sdi) ? sdi->priv : NULL; | |
460 | ||
461 | switch (key) { | |
462 | case SR_CONF_SCAN_OPTIONS: | |
463 | case SR_CONF_DEVICE_OPTIONS: | |
464 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
465 | case SR_CONF_VOLTAGE_TARGET: | |
466 | if (!devc || !devc->model) | |
467 | return SR_ERR_ARG; | |
468 | *data = std_gvar_min_max_step_array(devc->model->voltage); | |
469 | break; | |
470 | case SR_CONF_CURRENT_LIMIT: | |
471 | if (!devc || !devc->model) | |
472 | return SR_ERR_ARG; | |
473 | *data = std_gvar_min_max_step_array(devc->model->current); | |
474 | break; | |
475 | default: | |
476 | return SR_ERR_NA; | |
477 | } | |
478 | ||
479 | return SR_OK; | |
480 | } | |
481 | ||
482 | static int dev_close(struct sr_dev_inst *sdi) | |
483 | { | |
484 | struct dev_context *devc; | |
485 | ||
486 | devc = (sdi) ? sdi->priv : NULL; | |
487 | if (devc) | |
488 | g_mutex_clear(&devc->rw_mutex); | |
489 | ||
490 | return std_serial_dev_close(sdi); | |
491 | } | |
492 | ||
493 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
494 | { | |
495 | struct dev_context *devc; | |
496 | struct sr_serial_dev_inst *serial; | |
497 | ||
498 | devc = sdi->priv; | |
499 | ||
500 | sr_sw_limits_acquisition_start(&devc->limits); | |
501 | std_session_send_df_header(sdi); | |
502 | ||
503 | devc->req_sent_at = 0; | |
504 | serial = sdi->conn; | |
505 | serial_source_add(sdi->session, serial, G_IO_IN, | |
506 | KAXXXXP_POLL_INTERVAL_MS, | |
507 | korad_kaxxxxp_receive_data, (void *)sdi); | |
508 | ||
509 | return SR_OK; | |
510 | } | |
511 | ||
512 | static struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
513 | .name = "korad-kaxxxxp", | |
514 | .longname = "Korad KAxxxxP", | |
515 | .api_version = 1, | |
516 | .init = std_init, | |
517 | .cleanup = std_cleanup, | |
518 | .scan = scan, | |
519 | .dev_list = std_dev_list, | |
520 | .dev_clear = std_dev_clear, | |
521 | .config_get = config_get, | |
522 | .config_set = config_set, | |
523 | .config_list = config_list, | |
524 | .dev_open = std_serial_dev_open, | |
525 | .dev_close = dev_close, | |
526 | .dev_acquisition_start = dev_acquisition_start, | |
527 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
528 | .context = NULL, | |
529 | }; | |
530 | SR_REGISTER_DEV_DRIVER(korad_kaxxxxp_driver_info); |