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/>.
29 static const uint32_t scanopts[] = {
35 static const uint32_t drvopts[] = {
39 static const uint32_t devopts[] = {
41 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
43 SR_CONF_VOLTAGE | SR_CONF_GET,
44 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_CURRENT | SR_CONF_GET,
46 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
48 SR_CONF_REGULATION | SR_CONF_GET,
49 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
50 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
51 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
52 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
55 /* Model ID, model name, max current/voltage/power, current/voltage digits. */
56 static const struct rdtech_dps_model supported_models[] = {
57 { MODEL_DPS, 3005, "DPS3005", 5, 30, 160, 3, 2 },
58 { MODEL_DPS, 5005, "DPS5005", 5, 50, 250, 3, 2 },
59 { MODEL_DPS, 5205, "DPH5005", 5, 50, 250, 3, 2 },
60 { MODEL_DPS, 5015, "DPS5015", 15, 50, 750, 2, 2 },
61 { MODEL_DPS, 5020, "DPS5020", 20, 50, 1000, 2, 2 },
62 { MODEL_DPS, 8005, "DPS8005", 5, 80, 408, 3, 2 },
63 /* All RD specs taken from the 2020.12.2 instruction manual. */
64 { MODEL_RD , 6006, "RD6006" , 6, 60, 360, 3, 2 },
65 { MODEL_RD , 6012, "RD6012" , 12, 60, 720, 2, 2 },
66 { MODEL_RD , 6018, "RD6018" , 18, 60, 1080, 2, 2 },
69 static struct sr_dev_driver rdtech_dps_driver_info;
70 static struct sr_dev_driver rdtech_rd_driver_info;
72 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus,
73 enum rdtech_dps_model_type model_type)
75 static const char *type_prefix[] = {
83 const struct rdtech_dps_model *model, *supported;
85 struct sr_dev_inst *sdi;
86 struct dev_context *devc;
88 ret = rdtech_dps_get_model_version(modbus,
89 model_type, &id, &version, &serno);
90 sr_dbg("probe: ret %d, type %s, model %u, vers %u, snr %u.",
91 ret, type_prefix[model_type], id, version, serno);
95 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
96 supported = &supported_models[i];
97 if (model_type != supported->model_type)
99 if (id != supported->id)
105 sr_err("Unknown model: %s%u.", type_prefix[model_type], id);
109 sdi = g_malloc0(sizeof(*sdi));
110 sdi->status = SR_ST_INACTIVE;
111 sdi->vendor = g_strdup("RDTech");
112 switch (model_type) {
114 sdi->model = g_strdup(model->name);
115 sdi->version = g_strdup_printf("v%u", version);
116 sdi->driver = &rdtech_dps_driver_info;
119 sdi->model = g_strdup(model->name);
120 sdi->version = g_strdup_printf("v%u.%u",
121 version / 100, version % 100);
123 sdi->serial_num = g_strdup_printf("%u", serno);
124 sdi->driver = &rdtech_rd_driver_info;
127 sr_err("Programming error, unhandled DPS/DPH/RD device type.");
133 sdi->inst_type = SR_INST_MODBUS;
135 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
136 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I");
137 sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P");
139 devc = g_malloc0(sizeof(*devc));
140 sr_sw_limits_init(&devc->limits);
142 devc->current_multiplier = pow(10.0, model->current_digits);
143 devc->voltage_multiplier = pow(10.0, model->voltage_digits);
150 static struct sr_dev_inst *probe_device_dps(struct sr_modbus_dev_inst *modbus)
152 return probe_device(modbus, MODEL_DPS);
155 static struct sr_dev_inst *probe_device_rd(struct sr_modbus_dev_inst *modbus)
157 return probe_device(modbus, MODEL_RD);
160 static int config_compare(gconstpointer a, gconstpointer b)
162 const struct sr_config *ac = a, *bc = b;
163 return ac->key != bc->key;
166 static GSList *scan(struct sr_dev_driver *di, GSList *options,
167 enum rdtech_dps_model_type model_type)
169 static const char *default_serialcomm_dps = "9600/8n1";
170 static const char *default_serialcomm_rd = "115200/8n1";
172 struct sr_config default_serialcomm = {
173 .key = SR_CONF_SERIALCOMM,
176 struct sr_config default_modbusaddr = {
177 .key = SR_CONF_MODBUSADDR,
178 .data = g_variant_new_uint64(1),
180 GSList *opts, *devices;
181 const char *serialcomm;
182 struct sr_dev_inst *(*probe_func)(struct sr_modbus_dev_inst *modbus);
184 /* TODO See why di->context isn't available yet at this time. */
187 if (di->context == &rdtech_dps_driver_info || model_type == MODEL_DPS) {
188 serialcomm = default_serialcomm_dps;
189 probe_func = probe_device_dps;
191 if (di->context == &rdtech_rd_driver_info || model_type == MODEL_RD) {
192 serialcomm = default_serialcomm_rd;
193 probe_func = probe_device_rd;
197 if (serialcomm && *serialcomm)
198 default_serialcomm.data = g_variant_new_string(serialcomm);
201 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
202 opts = g_slist_prepend(opts, &default_serialcomm);
203 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
204 opts = g_slist_prepend(opts, &default_modbusaddr);
206 devices = sr_modbus_scan(di->context, opts, probe_func);
208 while (opts != options)
209 opts = g_slist_delete_link(opts, opts);
210 g_variant_unref(default_serialcomm.data);
211 g_variant_unref(default_modbusaddr.data);
216 static GSList *scan_dps(struct sr_dev_driver *di, GSList *options)
218 return scan(di, options, MODEL_DPS);
221 static GSList *scan_rd(struct sr_dev_driver *di, GSList *options)
223 return scan(di, options, MODEL_RD);
226 static int dev_open(struct sr_dev_inst *sdi)
228 struct sr_modbus_dev_inst *modbus;
229 struct rdtech_dps_state state;
233 if (sr_modbus_open(modbus) < 0)
236 memset(&state, 0, sizeof(state));
238 state.mask |= STATE_LOCK;
239 ret = rdtech_dps_set_state(sdi, &state);
246 static int dev_close(struct sr_dev_inst *sdi)
248 struct sr_modbus_dev_inst *modbus;
249 struct rdtech_dps_state state;
255 memset(&state, 0, sizeof(state));
257 state.mask |= STATE_LOCK;
258 (void)rdtech_dps_set_state(sdi, &state);
260 return sr_modbus_close(modbus);
263 static int config_get(uint32_t key, GVariant **data,
264 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
266 struct dev_context *devc;
267 struct rdtech_dps_state state;
277 case SR_CONF_LIMIT_SAMPLES:
278 case SR_CONF_LIMIT_MSEC:
279 ret = sr_sw_limits_config_get(&devc->limits, key, data);
281 case SR_CONF_ENABLED:
282 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
285 if (!(state.mask & STATE_OUTPUT_ENABLED))
287 *data = g_variant_new_boolean(state.output_enabled);
289 case SR_CONF_REGULATION:
290 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
293 if (!(state.mask & STATE_REGULATION_CC))
295 cc_text = state.regulation_cc ? "CC" : "CV";
296 *data = g_variant_new_string(cc_text);
298 case SR_CONF_VOLTAGE:
299 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
302 if (!(state.mask & STATE_VOLTAGE))
304 *data = g_variant_new_double(state.voltage);
306 case SR_CONF_VOLTAGE_TARGET:
307 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
310 if (!(state.mask & STATE_VOLTAGE_TARGET))
312 *data = g_variant_new_double(state.voltage_target);
314 case SR_CONF_CURRENT:
315 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
318 if (!(state.mask & STATE_CURRENT))
320 *data = g_variant_new_double(state.current);
322 case SR_CONF_CURRENT_LIMIT:
323 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
326 if (!(state.mask & STATE_CURRENT_LIMIT))
328 *data = g_variant_new_double(state.current_limit);
330 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
331 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
334 if (!(state.mask & STATE_PROTECT_ENABLED))
336 *data = g_variant_new_boolean(state.protect_enabled);
338 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
339 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
342 if (!(state.mask & STATE_PROTECT_OVP))
344 *data = g_variant_new_boolean(state.protect_ovp);
346 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
347 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
350 if (!(state.mask & STATE_OUTPUT_ENABLED))
352 *data = g_variant_new_double(state.ovp_threshold);
354 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
355 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
358 if (!(state.mask & STATE_PROTECT_ENABLED))
360 *data = g_variant_new_boolean(state.protect_enabled);
362 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
363 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
366 if (!(state.mask & STATE_PROTECT_OCP))
368 *data = g_variant_new_boolean(state.protect_ocp);
370 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
371 ret = rdtech_dps_get_state(sdi, &state, ST_CTX_CONFIG);
374 if (!(state.mask & STATE_OCP_THRESHOLD))
376 *data = g_variant_new_double(state.ocp_threshold);
385 static int config_set(uint32_t key, GVariant *data,
386 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
388 struct dev_context *devc;
389 struct rdtech_dps_state state;
394 memset(&state, 0, sizeof(state));
397 case SR_CONF_LIMIT_SAMPLES:
398 case SR_CONF_LIMIT_MSEC:
399 return sr_sw_limits_config_set(&devc->limits, key, data);
400 case SR_CONF_ENABLED:
401 state.output_enabled = g_variant_get_boolean(data);
402 state.mask |= STATE_OUTPUT_ENABLED;
403 return rdtech_dps_set_state(sdi, &state);
404 case SR_CONF_VOLTAGE_TARGET:
405 state.voltage_target = g_variant_get_double(data);
406 state.mask |= STATE_VOLTAGE_TARGET;
407 return rdtech_dps_set_state(sdi, &state);
408 case SR_CONF_CURRENT_LIMIT:
409 state.current_limit = g_variant_get_double(data);
410 state.mask |= STATE_CURRENT_LIMIT;
411 return rdtech_dps_set_state(sdi, &state);
412 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
413 state.ovp_threshold = g_variant_get_double(data);
414 state.mask |= STATE_OVP_THRESHOLD;
415 return rdtech_dps_set_state(sdi, &state);
416 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
417 state.ocp_threshold = g_variant_get_double(data);
418 state.mask |= STATE_OCP_THRESHOLD;
419 return rdtech_dps_set_state(sdi, &state);
427 static int config_list(uint32_t key, GVariant **data,
428 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
430 struct dev_context *devc;
432 devc = (sdi) ? sdi->priv : NULL;
435 case SR_CONF_SCAN_OPTIONS:
436 case SR_CONF_DEVICE_OPTIONS:
437 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
438 case SR_CONF_VOLTAGE_TARGET:
439 *data = std_gvar_min_max_step(0.0, devc->model->max_voltage,
440 1 / devc->voltage_multiplier);
442 case SR_CONF_CURRENT_LIMIT:
443 *data = std_gvar_min_max_step(0.0, devc->model->max_current,
444 1 / devc->current_multiplier);
453 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
455 struct dev_context *devc;
456 struct sr_modbus_dev_inst *modbus;
462 /* Seed internal state from current data. */
463 ret = rdtech_dps_seed_receive(sdi);
467 /* Register the periodic data reception callback. */
468 ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
469 rdtech_dps_receive_data, (void *)sdi);
473 sr_sw_limits_acquisition_start(&devc->limits);
474 std_session_send_df_header(sdi);
479 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
481 struct sr_modbus_dev_inst *modbus;
483 std_session_send_df_end(sdi);
486 sr_modbus_source_remove(sdi->session, modbus);
491 static struct sr_dev_driver rdtech_dps_driver_info = {
492 .name = "rdtech-dps",
493 .longname = "RDTech DPS/DPH series power supply",
496 .cleanup = std_cleanup,
498 .dev_list = std_dev_list,
499 .dev_clear = std_dev_clear,
500 .config_get = config_get,
501 .config_set = config_set,
502 .config_list = config_list,
503 .dev_open = dev_open,
504 .dev_close = dev_close,
505 .dev_acquisition_start = dev_acquisition_start,
506 .dev_acquisition_stop = dev_acquisition_stop,
509 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);
511 static struct sr_dev_driver rdtech_rd_driver_info = {
513 .longname = "RDTech RD series power supply",
516 .cleanup = std_cleanup,
518 .dev_list = std_dev_list,
519 .dev_clear = std_dev_clear,
520 .config_get = config_get,
521 .config_set = config_set,
522 .config_list = config_list,
523 .dev_open = dev_open,
524 .dev_close = dev_close,
525 .dev_acquisition_start = dev_acquisition_start,
526 .dev_acquisition_stop = dev_acquisition_stop,
529 SR_REGISTER_DEV_DRIVER(rdtech_rd_driver_info);