2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com>
5 * Copyright (C) 2018-2019 Frank Stettner <frank-stettner@gmx.net>
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.
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.
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/>.
27 static const uint32_t scanopts[] = {
33 static const uint32_t drvopts[] = {
37 static const uint32_t devopts[] = {
38 SR_CONF_CONN | SR_CONF_GET,
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,
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, };
58 static const struct korad_kaxxxxp_model models[] = {
59 /* Vendor, model name, ID reply, channels, voltage, current, quirks. */
60 {"Korad", "KA3005P", "KORADKA3005PV2.0",
61 1, volts_30, amps_5, KORAD_QUIRK_ID_TRAILING},
62 /* Some KA3005P have extra bytes after the ID text. */
63 {"Korad", "KA3005P", "KORADKA3005PV2.0\x01", 1, volts_30, amps_5, 0},
64 {"Korad", "KA3005P", "KORADKA3005PV2.0\xBC", 1, volts_30, amps_5, 0},
65 {"Korad", "KA3005P", "KORAD KA3005P V4.2", 1, volts_30, amps_5, 0},
66 {"Korad", "KA3005P", "KORAD KA3005P V5.5", 1, volts_30, amps_5, 0},
67 {"Korad", "KD3005P", "KORAD KD3005P V2.0", 1, volts_30, amps_5, 0},
68 {"Korad", "KD3005P", "KORADKD3005PV2.0", 1, volts_30, amps_5, 0},
69 {"Korad", "KD3005P", "KORADKD3005PV2.1", 1, volts_30, amps_5, 0},
70 {"Korad", "KD3005P", "KORAD KD3005P V4.1", 1, volts_30, amps_5, 0},
71 {"Korad", "KD3005P", "KORAD KD3005P V6.8", 1, volts_30, amps_5, 0},
72 {"Korad", "KD6005P", "KORAD KD6005P V2.2", 1, volts_60, amps_5, 0},
73 {"RND", "KA3005P", "RND 320-KA3005P V5.5", 1, volts_30, amps_5, 0},
74 {"RND", "KD3005P", "RND 320-KD3005P V4.2", 1, volts_30, amps_5, 0},
75 {"RND", "KA3005P", "RND 320-KA3005P V2.0", 1, volts_30, amps_5, 0},
76 {"Stamos Soldering", "S-LS-31", "S-LS-31 V2.0",
77 1, volts_30, amps_5, KORAD_QUIRK_ID_NO_VENDOR},
78 {"Tenma", "72-2535", "TENMA 72-2535 V2.1", 1, volts_30, amps_3, 0},
79 {"Tenma", "72-2540", "TENMA72-2540V2.0", 1, volts_30, amps_5, 0},
80 {"Tenma", "72-2540", "TENMA 72-2540 V2.1", 1, volts_30, amps_5, 0},
81 {"Tenma", "72-2540", "TENMA 72-2540 V5.2", 1, volts_30, amps_5, 0},
82 {"Tenma", "72-2550", "TENMA72-2550V2.0", 1, volts_60, amps_3, 0},
83 {"Tenma", "72-2710", "TENMA 72-2710 V6.6", 1, volts_30, amps_5, 0},
84 {"Velleman", "LABPS3005D", "VELLEMANLABPS3005DV2.0",
85 1, volts_30, amps_5, KORAD_QUIRK_LABPS_OVP_EN},
86 {"Velleman", "PS3005D", "VELLEMANPS3005DV2.0",
87 1, volts_30, amps_5, 0},
92 * Bump this when adding new models[] above. Make sure the text buffer
93 * for the ID response can hold the longest sequence that we expect in
94 * the field which consists of: vendor + model [ + version ][ + serno ].
95 * Don't be too generous here, the maximum receive buffer size affects
96 * the timeout within which the first response character is expected.
98 static const size_t id_text_buffer_size = 48;
101 * Check whether the device's "*IDN?" response matches a supported model.
102 * The caller already stripped off the optional serial number.
104 static gboolean model_matches(const struct korad_kaxxxxp_model *model,
108 gboolean skip_vendor, accept_trail;
115 * When the models[] entry contains a specific response text,
116 * then expect to see this very text in literal form. This
117 * lets the driver map weird and untypical responses to a
118 * specific set of display texts for vendor and model names.
120 if (model->id && model->id[0]) {
121 matches = g_strcmp0(model->id, id_text) == 0;
123 sr_dbg("Matches expected ID text: '%s'.", model->id);
128 * A more generic approach which covers most devices: Check
129 * for the very vendor and model names which also are shown
130 * to users (the display texts). Weakened to match responses
131 * more widely: Case insensitive checks, optional whitespace
132 * in responses, optional version details. Optional trailing
133 * garbage. Optional omission of the vendor name. Shall match
134 * all the devices which were individually listed in earlier
135 * implementations of the driver, and shall also match firmware
136 * versions that were not listed before.
138 skip_vendor = model->quirks & KORAD_QUIRK_ID_NO_VENDOR;
139 accept_trail = model->quirks & KORAD_QUIRK_ID_TRAILING;
141 want = model->vendor;
142 matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0;
145 id_text += strlen(want);
146 while (isspace((int)*id_text))
150 matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0;
153 id_text += strlen(want);
154 while (isspace((int)*id_text))
156 if (*id_text == 'V') {
157 /* TODO Isolate and (also) return version details? */
159 while (*id_text == '.' || isdigit((int)*id_text))
161 while (isspace((int)*id_text))
166 * TODO Determine how many non-printables to accept here
167 * and how strict to check for "known" garbage variants.
183 sr_dbg("Matches generic '[vendor] model [vers] [trail]' pattern.");
187 static GSList *scan(struct sr_dev_driver *di, GSList *options)
189 static const char *serno_prefix = " SN:";
191 struct dev_context *devc;
193 struct sr_dev_inst *sdi;
194 struct sr_config *src;
195 const char *conn, *serialcomm;
196 const char *force_detect;
197 struct sr_serial_dev_inst *serial;
201 const struct korad_kaxxxxp_model *model;
209 for (l = options; l; l = l->next) {
213 conn = g_variant_get_string(src->data, NULL);
215 case SR_CONF_SERIALCOMM:
216 serialcomm = g_variant_get_string(src->data, NULL);
218 case SR_CONF_FORCE_DETECT:
219 force_detect = g_variant_get_string(src->data, NULL);
222 sr_err("Unknown option %d, skipping.", src->key);
230 serialcomm = "9600/8n1";
231 if (force_detect && !*force_detect)
234 serial = sr_serial_dev_inst_new(conn, serialcomm);
235 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
238 /* Communicate the identification request. */
239 len = id_text_buffer_size;
240 if (len > sizeof(reply) - 1)
241 len = sizeof(reply) - 1;
242 sr_dbg("Want max %zu bytes.", len);
244 ret = korad_kaxxxxp_send_cmd(serial, "*IDN?");
248 ret = korad_kaxxxxp_read_chars(serial, len, reply);
251 sr_dbg("Received: %d, %s", ret, reply);
254 * Isolate the optional serial number at the response's end.
255 * Lookup the response's model ID in the list of known models.
257 serno = g_strrstr(reply, serno_prefix);
260 serno += strlen(serno_prefix);
264 for (i = 0; models[i].name; i++) {
265 if (!model_matches(&models[i], reply))
270 if (!model && force_detect) {
271 sr_warn("Found unknown model ID '%s', trying '%s' spec.",
272 reply, force_detect);
273 for (i = 0; models[i].name; i++) {
274 if (!model_matches(&models[i], force_detect))
276 sr_info("Found replacement, using it instead.");
282 sr_err("Found unknown model ID '%s', aborting.", reply);
285 sr_dbg("Found: %s %s (idx %zu).", model->vendor, model->name,
288 sdi = g_malloc0(sizeof(struct sr_dev_inst));
289 sdi->status = SR_ST_INACTIVE;
290 sdi->vendor = g_strdup(model->vendor);
291 sdi->model = g_strdup(model->name);
293 sdi->serial_num = g_strdup(serno);
294 sdi->inst_type = SR_INST_SERIAL;
296 sdi->connection_id = g_strdup(conn);
298 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
299 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I");
301 devc = g_malloc0(sizeof(struct dev_context));
302 sr_sw_limits_init(&devc->limits);
303 g_mutex_init(&devc->rw_mutex);
305 devc->req_sent_at = 0;
306 devc->cc_mode_1_changed = FALSE;
307 devc->cc_mode_2_changed = FALSE;
308 devc->output_enabled_changed = FALSE;
309 devc->ocp_enabled_changed = FALSE;
310 devc->ovp_enabled_changed = FALSE;
313 /* Get current status of device. */
314 if (korad_kaxxxxp_get_all_values(serial, devc) < 0)
317 serial_close(serial);
319 return std_scan_complete(di, g_slist_append(NULL, sdi));
322 sr_dev_inst_free(sdi);
324 sr_dbg("Scan failed.");
329 static int config_get(uint32_t key, GVariant **data,
330 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
332 struct dev_context *devc;
342 case SR_CONF_LIMIT_SAMPLES:
343 case SR_CONF_LIMIT_MSEC:
344 return sr_sw_limits_config_get(&devc->limits, key, data);
346 *data = g_variant_new_string(sdi->connection_id);
348 case SR_CONF_VOLTAGE:
349 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE, devc);
350 *data = g_variant_new_double(devc->voltage);
352 case SR_CONF_VOLTAGE_TARGET:
353 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc);
354 *data = g_variant_new_double(devc->voltage_target);
356 case SR_CONF_CURRENT:
357 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT, devc);
358 *data = g_variant_new_double(devc->current);
360 case SR_CONF_CURRENT_LIMIT:
361 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc);
362 *data = g_variant_new_double(devc->current_limit);
364 case SR_CONF_ENABLED:
365 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OUTPUT, devc);
366 *data = g_variant_new_boolean(devc->output_enabled);
368 case SR_CONF_REGULATION:
369 /* Dual channel not supported. */
370 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_STATUS, devc);
371 *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV");
373 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
374 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OCP, devc);
375 *data = g_variant_new_boolean(devc->ocp_enabled);
377 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
378 korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OVP, devc);
379 *data = g_variant_new_boolean(devc->ovp_enabled);
388 static int config_set(uint32_t key, GVariant *data,
389 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
391 struct dev_context *devc;
400 case SR_CONF_LIMIT_MSEC:
401 case SR_CONF_LIMIT_SAMPLES:
402 return sr_sw_limits_config_set(&devc->limits, key, data);
403 case SR_CONF_VOLTAGE_TARGET:
404 dval = g_variant_get_double(data);
405 if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1])
407 devc->set_voltage_target = dval;
408 if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc) < 0)
411 case SR_CONF_CURRENT_LIMIT:
412 dval = g_variant_get_double(data);
413 if (dval < devc->model->current[0] || dval > devc->model->current[1])
415 devc->set_current_limit = dval;
416 if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc) < 0)
419 case SR_CONF_ENABLED:
420 bval = g_variant_get_boolean(data);
421 /* Set always so it is possible turn off with sigrok-cli. */
422 devc->set_output_enabled = bval;
423 if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OUTPUT, devc) < 0)
426 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
427 bval = g_variant_get_boolean(data);
428 devc->set_ocp_enabled = bval;
429 if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OCP, devc) < 0)
432 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
433 bval = g_variant_get_boolean(data);
434 devc->set_ovp_enabled = bval;
435 if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OVP, devc) < 0)
445 static int config_list(uint32_t key, GVariant **data,
446 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
448 struct dev_context *devc;
450 devc = (sdi) ? sdi->priv : NULL;
453 case SR_CONF_SCAN_OPTIONS:
454 case SR_CONF_DEVICE_OPTIONS:
455 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
456 case SR_CONF_VOLTAGE_TARGET:
457 if (!devc || !devc->model)
459 *data = std_gvar_min_max_step_array(devc->model->voltage);
461 case SR_CONF_CURRENT_LIMIT:
462 if (!devc || !devc->model)
464 *data = std_gvar_min_max_step_array(devc->model->current);
473 static int dev_close(struct sr_dev_inst *sdi)
475 struct dev_context *devc;
477 devc = (sdi) ? sdi->priv : NULL;
479 g_mutex_clear(&devc->rw_mutex);
481 return std_serial_dev_close(sdi);
484 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
486 struct dev_context *devc;
487 struct sr_serial_dev_inst *serial;
491 sr_sw_limits_acquisition_start(&devc->limits);
492 std_session_send_df_header(sdi);
494 devc->req_sent_at = 0;
496 serial_source_add(sdi->session, serial, G_IO_IN,
497 KAXXXXP_POLL_INTERVAL_MS,
498 korad_kaxxxxp_receive_data, (void *)sdi);
503 static struct sr_dev_driver korad_kaxxxxp_driver_info = {
504 .name = "korad-kaxxxxp",
505 .longname = "Korad KAxxxxP",
508 .cleanup = std_cleanup,
510 .dev_list = std_dev_list,
511 .dev_clear = std_dev_clear,
512 .config_get = config_get,
513 .config_set = config_set,
514 .config_list = config_list,
515 .dev_open = std_serial_dev_open,
516 .dev_close = dev_close,
517 .dev_acquisition_start = dev_acquisition_start,
518 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
521 SR_REGISTER_DEV_DRIVER(korad_kaxxxxp_driver_info);