2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
5 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
6 * Copyright (C) 2021 Gerhard Sittig <gerhard.sittig@gmx.net>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 static const uint32_t scanopts[] = {
34 static const uint32_t drvopts[] = {
38 static const uint32_t devopts[] = {
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_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
49 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
50 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
51 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
54 static const uint32_t devopts_w_range[] = {
56 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
57 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
58 SR_CONF_VOLTAGE | SR_CONF_GET,
59 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60 SR_CONF_CURRENT | SR_CONF_GET,
61 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
62 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
63 SR_CONF_REGULATION | SR_CONF_GET,
64 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
65 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
66 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
67 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
68 SR_CONF_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
71 /* range name, max current/voltage/power, current/voltage digits */
72 static struct rdtech_dps_range ranges_dps3005[] = {
73 { "5A", 5, 30, 160, 3, 2 }
76 static struct rdtech_dps_range ranges_dps5005[] = {
77 { "5A", 5, 50, 250, 3, 2 }
80 static struct rdtech_dps_range ranges_dps5015[] = {
81 { "15A", 15, 50, 750, 2, 2 }
84 static struct rdtech_dps_range ranges_dps5020[] = {
85 { "20A", 20, 50, 1000, 2, 2 }
88 static struct rdtech_dps_range ranges_dps8005[] = {
89 { "5A", 5, 80, 408, 3, 2 }
92 static struct rdtech_dps_range ranges_rd6006[] = {
93 { "6A", 6, 60, 360, 3, 2 }
96 static struct rdtech_dps_range ranges_rd6006p[] = {
97 { "6A", 6, 60, 360, 4, 3 }
100 static struct rdtech_dps_range ranges_rd6012[] = {
101 { "12A", 12, 60, 720, 2, 2 }
105 * Current digits for RD6012P is 4 for the 6A range (RTU reg 20 = 0) and
106 * 3 for the 12A range (RTU reg 20 = 1)
108 static struct rdtech_dps_range ranges_rd6012p[] = {
109 { "6A", 6, 60, 360, 4, 3 },
110 { "12A", 12, 60, 720, 3, 3 }
113 static struct rdtech_dps_range ranges_rd6018[] = {
114 { "18A", 18, 60, 1080, 2, 2 }
117 static struct rdtech_dps_range ranges_rd6024[] = {
118 { "24A", 24, 60, 1440, 2, 2 }
121 /* model ID, model name, range */
122 static const struct rdtech_dps_model supported_models[] = {
123 { MODEL_DPS, 3005, "DPS3005", ARRAY_AND_SIZE(ranges_dps3005) },
124 { MODEL_DPS, 5005, "DPS5005", ARRAY_AND_SIZE(ranges_dps5005) },
125 { MODEL_DPS, 5205, "DPH5005", ARRAY_AND_SIZE(ranges_dps5005) },
126 { MODEL_DPS, 5015, "DPS5015", ARRAY_AND_SIZE(ranges_dps5015) },
127 { MODEL_DPS, 5020, "DPS5020", ARRAY_AND_SIZE(ranges_dps5020) },
128 { MODEL_DPS, 8005, "DPS8005", ARRAY_AND_SIZE(ranges_dps8005) },
130 * Specs for models RD60nn taken from the 2020.12.2 instruction manual,
131 * specs for RD6006P from the 2021.2.26 (english) manual,
132 * specs for RD6012P from the 2021.10.26 (english) manual,
133 * and specs for RD6024P from the 2021.1.7 (english) manual.
135 { MODEL_RD, 60061, "RD6006" , ARRAY_AND_SIZE(ranges_rd6006) },
136 { MODEL_RD, 60062, "RD6006" , ARRAY_AND_SIZE(ranges_rd6006) },
137 { MODEL_RD, 60065, "RD6006P", ARRAY_AND_SIZE(ranges_rd6006p) },
138 { MODEL_RD, 60121, "RD6012" , ARRAY_AND_SIZE(ranges_rd6012) },
139 { MODEL_RD, 60125, "RD6012P", ARRAY_AND_SIZE(ranges_rd6012p) },
140 { MODEL_RD, 60181, "RD6018" , ARRAY_AND_SIZE(ranges_rd6018) },
141 { MODEL_RD, 60241, "RD6024" , ARRAY_AND_SIZE(ranges_rd6024) },
144 static struct sr_dev_driver rdtech_dps_driver_info;
145 static struct sr_dev_driver rdtech_rd_driver_info;
147 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus,
148 enum rdtech_dps_model_type model_type)
150 static const char *type_prefix[] = {
155 uint16_t id, version;
158 const struct rdtech_dps_model *model, *supported;
160 struct sr_dev_inst *sdi;
161 struct dev_context *devc;
163 ret = rdtech_dps_get_model_version(modbus,
164 model_type, &id, &version, &serno);
165 sr_dbg("probe: ret %d, type %s, model %u, vers %u, snr %u.",
166 ret, type_prefix[model_type], id, version, serno);
170 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
171 supported = &supported_models[i];
172 if (model_type != supported->model_type)
174 if (id != supported->id)
180 sr_err("Unknown model: %s%u.", type_prefix[model_type], id);
184 sdi = g_malloc0(sizeof(*sdi));
185 sdi->status = SR_ST_INACTIVE;
186 sdi->vendor = g_strdup("RDTech");
187 switch (model_type) {
189 sdi->model = g_strdup(model->name);
190 sdi->version = g_strdup_printf("v%u", version);
191 sdi->driver = &rdtech_dps_driver_info;
194 sdi->model = g_strdup(model->name);
195 sdi->version = g_strdup_printf("v%u.%u",
196 version / 100, version % 100);
198 sdi->serial_num = g_strdup_printf("%u", serno);
199 sdi->driver = &rdtech_rd_driver_info;
202 sr_err("Programming error, unhandled DPS/DPH/RD device type.");
208 sdi->inst_type = SR_INST_MODBUS;
210 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
211 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I");
212 sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P");
214 devc = g_malloc0(sizeof(*devc));
215 sr_sw_limits_init(&devc->limits);
218 ret = rdtech_dps_update_range(sdi);
225 static struct sr_dev_inst *probe_device_dps(struct sr_modbus_dev_inst *modbus)
227 return probe_device(modbus, MODEL_DPS);
230 static struct sr_dev_inst *probe_device_rd(struct sr_modbus_dev_inst *modbus)
232 return probe_device(modbus, MODEL_RD);
235 static int config_compare(gconstpointer a, gconstpointer b)
237 const struct sr_config *ac = a, *bc = b;
238 return ac->key != bc->key;
241 static GSList *scan(struct sr_dev_driver *di, GSList *options,
242 enum rdtech_dps_model_type model_type)
244 static const char *default_serialcomm_dps = "9600/8n1";
245 static const char *default_serialcomm_rd = "115200/8n1";
247 struct sr_config default_serialcomm = {
248 .key = SR_CONF_SERIALCOMM,
251 struct sr_config default_modbusaddr = {
252 .key = SR_CONF_MODBUSADDR,
253 .data = g_variant_new_uint64(1),
255 GSList *opts, *devices;
256 const char *serialcomm;
257 struct sr_dev_inst *(*probe_func)(struct sr_modbus_dev_inst *modbus);
259 /* TODO See why di->context isn't available yet at this time. */
262 if (di->context == &rdtech_dps_driver_info || model_type == MODEL_DPS) {
263 serialcomm = default_serialcomm_dps;
264 probe_func = probe_device_dps;
266 if (di->context == &rdtech_rd_driver_info || model_type == MODEL_RD) {
267 serialcomm = default_serialcomm_rd;
268 probe_func = probe_device_rd;
272 if (serialcomm && *serialcomm)
273 default_serialcomm.data = g_variant_new_string(serialcomm);
276 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
277 opts = g_slist_prepend(opts, &default_serialcomm);
278 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
279 opts = g_slist_prepend(opts, &default_modbusaddr);
281 devices = sr_modbus_scan(di->context, opts, probe_func);
283 while (opts != options)
284 opts = g_slist_delete_link(opts, opts);
285 g_variant_unref(default_serialcomm.data);
286 g_variant_unref(default_modbusaddr.data);
291 static GSList *scan_dps(struct sr_dev_driver *di, GSList *options)
293 return scan(di, options, MODEL_DPS);
296 static GSList *scan_rd(struct sr_dev_driver *di, GSList *options)
298 return scan(di, options, MODEL_RD);
301 static int dev_open(struct sr_dev_inst *sdi)
303 struct sr_modbus_dev_inst *modbus;
304 struct rdtech_dps_state state;
308 if (sr_modbus_open(modbus) < 0)
311 memset(&state, 0, sizeof(state));
313 state.mask |= STATE_LOCK;
314 ret = rdtech_dps_set_state(sdi, &state);
321 static int dev_close(struct sr_dev_inst *sdi)
323 struct sr_modbus_dev_inst *modbus;
324 struct rdtech_dps_state state;
330 memset(&state, 0, sizeof(state));
332 state.mask |= STATE_LOCK;
333 (void)rdtech_dps_set_state(sdi, &state);
335 return sr_modbus_close(modbus);
338 static int config_get(uint32_t key, GVariant **data,
339 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
341 struct dev_context *devc;
342 struct rdtech_dps_state state;
352 case SR_CONF_LIMIT_SAMPLES:
353 case SR_CONF_LIMIT_MSEC:
354 ret = sr_sw_limits_config_get(&devc->limits, key, data);
356 case SR_CONF_ENABLED:
357 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
360 if (!(state.mask & STATE_OUTPUT_ENABLED))
362 *data = g_variant_new_boolean(state.output_enabled);
364 case SR_CONF_REGULATION:
365 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
368 if (!(state.mask & STATE_REGULATION_CC))
370 cc_text = state.regulation_cc ? "CC" : "CV";
371 *data = g_variant_new_string(cc_text);
373 case SR_CONF_VOLTAGE:
374 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
377 if (!(state.mask & STATE_VOLTAGE))
379 *data = g_variant_new_double(state.voltage);
381 case SR_CONF_VOLTAGE_TARGET:
382 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
385 if (!(state.mask & STATE_VOLTAGE_TARGET))
387 *data = g_variant_new_double(state.voltage_target);
389 case SR_CONF_CURRENT:
390 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
393 if (!(state.mask & STATE_CURRENT))
395 *data = g_variant_new_double(state.current);
397 case SR_CONF_CURRENT_LIMIT:
398 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
401 if (!(state.mask & STATE_CURRENT_LIMIT))
403 *data = g_variant_new_double(state.current_limit);
405 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
406 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
409 if (!(state.mask & STATE_PROTECT_ENABLED))
411 *data = g_variant_new_boolean(state.protect_enabled);
413 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
414 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
417 if (!(state.mask & STATE_PROTECT_OVP))
419 *data = g_variant_new_boolean(state.protect_ovp);
421 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
422 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
425 if (!(state.mask & STATE_OUTPUT_ENABLED))
427 *data = g_variant_new_double(state.ovp_threshold);
429 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
430 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
433 if (!(state.mask & STATE_PROTECT_ENABLED))
435 *data = g_variant_new_boolean(state.protect_enabled);
437 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
438 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
441 if (!(state.mask & STATE_PROTECT_OCP))
443 *data = g_variant_new_boolean(state.protect_ocp);
445 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
446 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
449 if (!(state.mask & STATE_OCP_THRESHOLD))
451 *data = g_variant_new_double(state.ocp_threshold);
454 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
457 if (!(state.mask & STATE_RANGE))
459 *data = g_variant_new_string(
460 devc->model->ranges[state.range].range_str);
469 static int config_set(uint32_t key, GVariant *data,
470 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
472 struct dev_context *devc;
473 struct rdtech_dps_state state;
474 const char *range_str;
480 memset(&state, 0, sizeof(state));
483 case SR_CONF_LIMIT_SAMPLES:
484 case SR_CONF_LIMIT_MSEC:
485 return sr_sw_limits_config_set(&devc->limits, key, data);
486 case SR_CONF_ENABLED:
487 state.output_enabled = g_variant_get_boolean(data);
488 state.mask |= STATE_OUTPUT_ENABLED;
489 return rdtech_dps_set_state(sdi, &state);
490 case SR_CONF_VOLTAGE_TARGET:
491 state.voltage_target = g_variant_get_double(data);
492 state.mask |= STATE_VOLTAGE_TARGET;
493 return rdtech_dps_set_state(sdi, &state);
494 case SR_CONF_CURRENT_LIMIT:
495 state.current_limit = g_variant_get_double(data);
496 state.mask |= STATE_CURRENT_LIMIT;
497 return rdtech_dps_set_state(sdi, &state);
498 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
499 state.ovp_threshold = g_variant_get_double(data);
500 state.mask |= STATE_OVP_THRESHOLD;
501 return rdtech_dps_set_state(sdi, &state);
502 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
503 state.ocp_threshold = g_variant_get_double(data);
504 state.mask |= STATE_OCP_THRESHOLD;
505 return rdtech_dps_set_state(sdi, &state);
507 range_str = g_variant_get_string(data, NULL);
508 for (i = 0; i < devc->model->n_ranges; ++i) {
509 if (g_strcmp0(devc->model->ranges[i].range_str, range_str)
512 state.mask |= STATE_RANGE;
513 return rdtech_dps_set_state(sdi, &state);
524 static int config_list(uint32_t key, GVariant **data,
525 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
527 struct dev_context *devc;
528 struct rdtech_dps_range *range;
532 devc = (sdi) ? sdi->priv : NULL;
535 case SR_CONF_SCAN_OPTIONS:
536 case SR_CONF_DEVICE_OPTIONS:
537 if (devc->model->n_ranges > 1)
538 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
539 drvopts, devopts_w_range);
541 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
543 case SR_CONF_VOLTAGE_TARGET:
544 rdtech_dps_update_range(sdi);
545 range = &devc->model->ranges[devc->curr_range];
546 *data = std_gvar_min_max_step(0.0, range->max_voltage,
547 1 / devc->voltage_multiplier);
549 case SR_CONF_CURRENT_LIMIT:
550 rdtech_dps_update_range(sdi);
551 range = &devc->model->ranges[devc->curr_range];
552 *data = std_gvar_min_max_step(0.0, range->max_current,
553 1 / devc->current_multiplier);
556 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
557 for (i = 0; i < devc->model->n_ranges; ++i)
558 g_variant_builder_add(&gvb, "s",
559 devc->model->ranges[i].range_str);
560 *data = g_variant_builder_end(&gvb);
569 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
571 struct dev_context *devc;
572 struct sr_modbus_dev_inst *modbus;
578 /* Seed internal state from current data. */
579 ret = rdtech_dps_seed_receive(sdi);
583 /* Register the periodic data reception callback. */
584 ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
585 rdtech_dps_receive_data, (void *)sdi);
589 sr_sw_limits_acquisition_start(&devc->limits);
590 std_session_send_df_header(sdi);
595 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
597 struct sr_modbus_dev_inst *modbus;
599 std_session_send_df_end(sdi);
602 sr_modbus_source_remove(sdi->session, modbus);
607 static struct sr_dev_driver rdtech_dps_driver_info = {
608 .name = "rdtech-dps",
609 .longname = "RDTech DPS/DPH series power supply",
612 .cleanup = std_cleanup,
614 .dev_list = std_dev_list,
615 .dev_clear = std_dev_clear,
616 .config_get = config_get,
617 .config_set = config_set,
618 .config_list = config_list,
619 .dev_open = dev_open,
620 .dev_close = dev_close,
621 .dev_acquisition_start = dev_acquisition_start,
622 .dev_acquisition_stop = dev_acquisition_stop,
625 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);
627 static struct sr_dev_driver rdtech_rd_driver_info = {
629 .longname = "RDTech RD series power supply",
632 .cleanup = std_cleanup,
634 .dev_list = std_dev_list,
635 .dev_clear = std_dev_clear,
636 .config_get = config_get,
637 .config_set = config_set,
638 .config_list = config_list,
639 .dev_open = dev_open,
640 .dev_close = dev_close,
641 .dev_acquisition_start = dev_acquisition_start,
642 .dev_acquisition_stop = dev_acquisition_stop,
645 SR_REGISTER_DEV_DRIVER(rdtech_rd_driver_info);