]>
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 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 | */ | |
107 | if (model->id && model->id[0]) { | |
108 | matches = g_strcmp0(model->id, id_text) == 0; | |
109 | if (matches) | |
110 | sr_dbg("Matches expected ID text: '%s'.", model->id); | |
111 | return matches; | |
112 | } | |
113 | ||
114 | /* | |
115 | * A more generic approach which covers most devices: Check | |
116 | * for the very vendor and model names which also are shown | |
117 | * to users (the display texts). Weakened to match responses | |
118 | * more widely: Case insensitive checks, optional whitespace | |
119 | * in responses, optional version details. Optional trailing | |
120 | * garbage. Optional omission of the vendor name. Shall match | |
121 | * all the devices which were individually listed in earlier | |
122 | * implementations of the driver, and shall also match firmware | |
123 | * versions that were not listed before. | |
124 | */ | |
125 | skip_vendor = model->quirks & KORAD_QUIRK_ID_NO_VENDOR; | |
126 | accept_trail = model->quirks & KORAD_QUIRK_ID_TRAILING; | |
127 | if (!skip_vendor) { | |
128 | want = model->vendor; | |
129 | matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0; | |
130 | if (!matches) | |
131 | return FALSE; | |
132 | id_text += strlen(want); | |
133 | while (isspace((int)*id_text)) | |
134 | id_text++; | |
135 | } | |
136 | want = model->name; | |
137 | matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0; | |
138 | if (!matches) | |
139 | return FALSE; | |
140 | id_text += strlen(want); | |
141 | while (isspace((int)*id_text)) | |
142 | id_text++; | |
143 | if (*id_text == 'V') { | |
144 | /* TODO Isolate and (also) return version details? */ | |
145 | id_text++; | |
146 | while (*id_text == '.' || isdigit((int)*id_text)) | |
147 | id_text++; | |
148 | while (isspace((int)*id_text)) | |
149 | id_text++; | |
150 | } | |
151 | if (accept_trail) { | |
152 | /* | |
153 | * TODO Determine how many non-printables to accept here | |
154 | * and how strict to check for "known" garbage variants. | |
155 | */ | |
156 | switch (*id_text) { | |
157 | case '\x01': | |
158 | case '\xbc': | |
159 | id_text++; | |
160 | break; | |
161 | case '\x00': | |
162 | /* EMPTY */ | |
163 | break; | |
164 | default: | |
165 | return FALSE; | |
166 | } | |
167 | } | |
168 | if (*id_text) | |
169 | return FALSE; | |
170 | sr_dbg("Matches generic '[vendor] model [vers] [trail]' pattern."); | |
171 | return TRUE; | |
172 | } | |
173 | ||
174 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
175 | { | |
176 | static const char *serno_prefix = " SN:"; | |
177 | ||
178 | struct dev_context *devc; | |
179 | GSList *l; | |
180 | struct sr_dev_inst *sdi; | |
181 | struct sr_config *src; | |
182 | const char *conn, *serialcomm; | |
183 | const char *force_detect; | |
184 | struct sr_serial_dev_inst *serial; | |
185 | size_t i; | |
186 | char reply[50]; | |
187 | int ret; | |
188 | const struct korad_kaxxxxp_model *model; | |
189 | size_t len; | |
190 | char *serno; | |
191 | ||
192 | conn = NULL; | |
193 | serialcomm = NULL; | |
194 | force_detect = NULL; | |
195 | ||
196 | for (l = options; l; l = l->next) { | |
197 | src = l->data; | |
198 | switch (src->key) { | |
199 | case SR_CONF_CONN: | |
200 | conn = g_variant_get_string(src->data, NULL); | |
201 | break; | |
202 | case SR_CONF_SERIALCOMM: | |
203 | serialcomm = g_variant_get_string(src->data, NULL); | |
204 | break; | |
205 | case SR_CONF_FORCE_DETECT: | |
206 | force_detect = g_variant_get_string(src->data, NULL); | |
207 | break; | |
208 | default: | |
209 | sr_err("Unknown option %d, skipping.", src->key); | |
210 | break; | |
211 | } | |
212 | } | |
213 | ||
214 | if (!conn) | |
215 | return NULL; | |
216 | if (!serialcomm) | |
217 | serialcomm = "9600/8n1"; | |
218 | if (force_detect && !*force_detect) | |
219 | force_detect = NULL; | |
220 | ||
221 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
222 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
223 | return NULL; | |
224 | ||
225 | /* Communicate the identification request. */ | |
226 | len = id_text_buffer_size; | |
227 | if (len > sizeof(reply) - 1) | |
228 | len = sizeof(reply) - 1; | |
229 | sr_dbg("Want max %zu bytes.", len); | |
230 | ||
231 | ret = korad_kaxxxxp_send_cmd(serial, "*IDN?"); | |
232 | if (ret < 0) | |
233 | return NULL; | |
234 | ||
235 | ret = korad_kaxxxxp_read_chars(serial, len, reply); | |
236 | if (ret < 0) | |
237 | return NULL; | |
238 | sr_dbg("Received: %d, %s", ret, reply); | |
239 | ||
240 | /* | |
241 | * Isolate the optional serial number at the response's end. | |
242 | * Lookup the response's model ID in the list of known models. | |
243 | */ | |
244 | serno = g_strrstr(reply, serno_prefix); | |
245 | if (serno) { | |
246 | *serno = '\0'; | |
247 | serno += strlen(serno_prefix); | |
248 | } | |
249 | ||
250 | model = NULL; | |
251 | for (i = 0; models[i].name; i++) { | |
252 | if (!model_matches(&models[i], reply)) | |
253 | continue; | |
254 | model = &models[i]; | |
255 | break; | |
256 | } | |
257 | if (!model && force_detect) { | |
258 | sr_warn("Found unknown model ID '%s', trying '%s' spec.", | |
259 | reply, force_detect); | |
260 | for (i = 0; models[i].name; i++) { | |
261 | if (!model_matches(&models[i], force_detect)) | |
262 | continue; | |
263 | sr_info("Found replacement, using it instead."); | |
264 | model = &models[i]; | |
265 | break; | |
266 | } | |
267 | } | |
268 | if (!model) { | |
269 | sr_err("Found unknown model ID '%s', aborting.", reply); | |
270 | return NULL; | |
271 | } | |
272 | sr_dbg("Found: %s %s (idx %zu).", model->vendor, model->name, | |
273 | model - &models[0]); | |
274 | ||
275 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
276 | sdi->status = SR_ST_INACTIVE; | |
277 | sdi->vendor = g_strdup(model->vendor); | |
278 | sdi->model = g_strdup(model->name); | |
279 | if (serno) | |
280 | sdi->serial_num = g_strdup(serno); | |
281 | sdi->inst_type = SR_INST_SERIAL; | |
282 | sdi->conn = serial; | |
283 | sdi->connection_id = g_strdup(conn); | |
284 | ||
285 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V"); | |
286 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I"); | |
287 | ||
288 | devc = g_malloc0(sizeof(struct dev_context)); | |
289 | sr_sw_limits_init(&devc->limits); | |
290 | g_mutex_init(&devc->rw_mutex); | |
291 | devc->model = model; | |
292 | devc->req_sent_at = 0; | |
293 | devc->cc_mode_1_changed = FALSE; | |
294 | devc->cc_mode_2_changed = FALSE; | |
295 | devc->output_enabled_changed = FALSE; | |
296 | devc->ocp_enabled_changed = FALSE; | |
297 | devc->ovp_enabled_changed = FALSE; | |
298 | sdi->priv = devc; | |
299 | ||
300 | /* Get current status of device. */ | |
301 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) | |
302 | goto exit_err; | |
303 | ||
304 | serial_close(serial); | |
305 | ||
306 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
307 | ||
308 | exit_err: | |
309 | sr_dev_inst_free(sdi); | |
310 | g_free(devc); | |
311 | sr_dbg("Scan failed."); | |
312 | ||
313 | return NULL; | |
314 | } | |
315 | ||
316 | static int config_get(uint32_t key, GVariant **data, | |
317 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
318 | { | |
319 | struct dev_context *devc; | |
320 | ||
321 | (void)cg; | |
322 | ||
323 | if (!sdi || !data) | |
324 | return SR_ERR_ARG; | |
325 | ||
326 | devc = sdi->priv; | |
327 | ||
328 | switch (key) { | |
329 | case SR_CONF_LIMIT_SAMPLES: | |
330 | case SR_CONF_LIMIT_MSEC: | |
331 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
332 | case SR_CONF_CONN: | |
333 | *data = g_variant_new_string(sdi->connection_id); | |
334 | break; | |
335 | case SR_CONF_VOLTAGE: | |
336 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE, devc); | |
337 | *data = g_variant_new_double(devc->voltage); | |
338 | break; | |
339 | case SR_CONF_VOLTAGE_TARGET: | |
340 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc); | |
341 | *data = g_variant_new_double(devc->voltage_target); | |
342 | break; | |
343 | case SR_CONF_CURRENT: | |
344 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT, devc); | |
345 | *data = g_variant_new_double(devc->current); | |
346 | break; | |
347 | case SR_CONF_CURRENT_LIMIT: | |
348 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc); | |
349 | *data = g_variant_new_double(devc->current_limit); | |
350 | break; | |
351 | case SR_CONF_ENABLED: | |
352 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OUTPUT, devc); | |
353 | *data = g_variant_new_boolean(devc->output_enabled); | |
354 | break; | |
355 | case SR_CONF_REGULATION: | |
356 | /* Dual channel not supported. */ | |
357 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_STATUS, devc); | |
358 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
359 | break; | |
360 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
361 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OCP, devc); | |
362 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
363 | break; | |
364 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
365 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OVP, devc); | |
366 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
367 | break; | |
368 | default: | |
369 | return SR_ERR_NA; | |
370 | } | |
371 | ||
372 | return SR_OK; | |
373 | } | |
374 | ||
375 | static int config_set(uint32_t key, GVariant *data, | |
376 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
377 | { | |
378 | struct dev_context *devc; | |
379 | double dval; | |
380 | gboolean bval; | |
381 | ||
382 | (void)cg; | |
383 | ||
384 | devc = sdi->priv; | |
385 | ||
386 | switch (key) { | |
387 | case SR_CONF_LIMIT_MSEC: | |
388 | case SR_CONF_LIMIT_SAMPLES: | |
389 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
390 | case SR_CONF_VOLTAGE_TARGET: | |
391 | dval = g_variant_get_double(data); | |
392 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
393 | return SR_ERR_ARG; | |
394 | devc->set_voltage_target = dval; | |
395 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc) < 0) | |
396 | return SR_ERR; | |
397 | break; | |
398 | case SR_CONF_CURRENT_LIMIT: | |
399 | dval = g_variant_get_double(data); | |
400 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
401 | return SR_ERR_ARG; | |
402 | devc->set_current_limit = dval; | |
403 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc) < 0) | |
404 | return SR_ERR; | |
405 | break; | |
406 | case SR_CONF_ENABLED: | |
407 | bval = g_variant_get_boolean(data); | |
408 | /* Set always so it is possible turn off with sigrok-cli. */ | |
409 | devc->set_output_enabled = bval; | |
410 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OUTPUT, devc) < 0) | |
411 | return SR_ERR; | |
412 | break; | |
413 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
414 | bval = g_variant_get_boolean(data); | |
415 | devc->set_ocp_enabled = bval; | |
416 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OCP, devc) < 0) | |
417 | return SR_ERR; | |
418 | break; | |
419 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
420 | bval = g_variant_get_boolean(data); | |
421 | devc->set_ovp_enabled = bval; | |
422 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OVP, devc) < 0) | |
423 | return SR_ERR; | |
424 | break; | |
425 | default: | |
426 | return SR_ERR_NA; | |
427 | } | |
428 | ||
429 | return SR_OK; | |
430 | } | |
431 | ||
432 | static int config_list(uint32_t key, GVariant **data, | |
433 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
434 | { | |
435 | struct dev_context *devc; | |
436 | ||
437 | devc = (sdi) ? sdi->priv : NULL; | |
438 | ||
439 | switch (key) { | |
440 | case SR_CONF_SCAN_OPTIONS: | |
441 | case SR_CONF_DEVICE_OPTIONS: | |
442 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
443 | case SR_CONF_VOLTAGE_TARGET: | |
444 | if (!devc || !devc->model) | |
445 | return SR_ERR_ARG; | |
446 | *data = std_gvar_min_max_step_array(devc->model->voltage); | |
447 | break; | |
448 | case SR_CONF_CURRENT_LIMIT: | |
449 | if (!devc || !devc->model) | |
450 | return SR_ERR_ARG; | |
451 | *data = std_gvar_min_max_step_array(devc->model->current); | |
452 | break; | |
453 | default: | |
454 | return SR_ERR_NA; | |
455 | } | |
456 | ||
457 | return SR_OK; | |
458 | } | |
459 | ||
460 | static int dev_close(struct sr_dev_inst *sdi) | |
461 | { | |
462 | struct dev_context *devc; | |
463 | ||
464 | devc = (sdi) ? sdi->priv : NULL; | |
465 | if (devc) | |
466 | g_mutex_clear(&devc->rw_mutex); | |
467 | ||
468 | return std_serial_dev_close(sdi); | |
469 | } | |
470 | ||
471 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
472 | { | |
473 | struct dev_context *devc; | |
474 | struct sr_serial_dev_inst *serial; | |
475 | ||
476 | devc = sdi->priv; | |
477 | ||
478 | sr_sw_limits_acquisition_start(&devc->limits); | |
479 | std_session_send_df_header(sdi); | |
480 | ||
481 | devc->req_sent_at = 0; | |
482 | serial = sdi->conn; | |
483 | serial_source_add(sdi->session, serial, G_IO_IN, | |
484 | KAXXXXP_POLL_INTERVAL_MS, | |
485 | korad_kaxxxxp_receive_data, (void *)sdi); | |
486 | ||
487 | return SR_OK; | |
488 | } | |
489 | ||
490 | static struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
491 | .name = "korad-kaxxxxp", | |
492 | .longname = "Korad KAxxxxP", | |
493 | .api_version = 1, | |
494 | .init = std_init, | |
495 | .cleanup = std_cleanup, | |
496 | .scan = scan, | |
497 | .dev_list = std_dev_list, | |
498 | .dev_clear = std_dev_clear, | |
499 | .config_get = config_get, | |
500 | .config_set = config_set, | |
501 | .config_list = config_list, | |
502 | .dev_open = std_serial_dev_open, | |
503 | .dev_close = dev_close, | |
504 | .dev_acquisition_start = dev_acquisition_start, | |
505 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
506 | .context = NULL, | |
507 | }; | |
508 | SR_REGISTER_DEV_DRIVER(korad_kaxxxxp_driver_info); |