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 { 3005, "DPS3005", 5, 30, 160, 3, 2 },
58 { 5005, "DPS5005", 5, 50, 250, 3, 2 },
59 { 5205, "DPH5005", 5, 50, 250, 3, 2 },
60 { 5015, "DPS5015", 15, 50, 750, 2, 2 },
61 { 5020, "DPS5020", 20, 50, 1000, 2, 2 },
62 { 8005, "DPS8005", 5, 80, 408, 3, 2 },
65 static struct sr_dev_driver rdtech_dps_driver_info;
67 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
71 const struct rdtech_dps_model *model;
73 struct sr_dev_inst *sdi;
74 struct dev_context *devc;
76 ret = rdtech_dps_get_model_version(modbus, &id, &version);
80 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
81 if (id == supported_models[i].id) {
82 model = &supported_models[i];
87 sr_err("Unknown model: %u.", id);
91 sdi = g_malloc0(sizeof(*sdi));
92 sdi->status = SR_ST_INACTIVE;
93 sdi->vendor = g_strdup("RDTech");
94 sdi->model = g_strdup(model->name);
95 sdi->version = g_strdup_printf("v%u", version);
97 sdi->driver = &rdtech_dps_driver_info;
98 sdi->inst_type = SR_INST_MODBUS;
100 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
101 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I");
102 sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P");
104 devc = g_malloc0(sizeof(*devc));
105 sr_sw_limits_init(&devc->limits);
107 devc->current_multiplier = pow(10.0, model->current_digits);
108 devc->voltage_multiplier = pow(10.0, model->voltage_digits);
115 static int config_compare(gconstpointer a, gconstpointer b)
117 const struct sr_config *ac = a, *bc = b;
118 return ac->key != bc->key;
121 static GSList *scan(struct sr_dev_driver *di, GSList *options)
123 struct sr_config default_serialcomm = {
124 .key = SR_CONF_SERIALCOMM,
125 .data = g_variant_new_string("9600/8n1"),
127 struct sr_config default_modbusaddr = {
128 .key = SR_CONF_MODBUSADDR,
129 .data = g_variant_new_uint64(1),
131 GSList *opts, *devices;
134 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
135 opts = g_slist_prepend(opts, &default_serialcomm);
136 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
137 opts = g_slist_prepend(opts, &default_modbusaddr);
139 devices = sr_modbus_scan(di->context, opts, probe_device);
141 while (opts != options)
142 opts = g_slist_delete_link(opts, opts);
143 g_variant_unref(default_serialcomm.data);
144 g_variant_unref(default_modbusaddr.data);
149 static int dev_open(struct sr_dev_inst *sdi)
151 struct sr_modbus_dev_inst *modbus;
152 struct rdtech_dps_state state;
156 if (sr_modbus_open(modbus) < 0)
159 memset(&state, 0, sizeof(state));
161 state.mask |= STATE_LOCK;
162 ret = rdtech_dps_set_state(sdi, &state);
169 static int dev_close(struct sr_dev_inst *sdi)
171 struct sr_modbus_dev_inst *modbus;
172 struct rdtech_dps_state state;
178 memset(&state, 0, sizeof(state));
180 state.mask |= STATE_LOCK;
181 (void)rdtech_dps_set_state(sdi, &state);
183 return sr_modbus_close(modbus);
186 static int config_get(uint32_t key, GVariant **data,
187 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
189 struct dev_context *devc;
190 struct rdtech_dps_state state;
200 case SR_CONF_LIMIT_SAMPLES:
201 case SR_CONF_LIMIT_MSEC:
202 ret = sr_sw_limits_config_get(&devc->limits, key, data);
204 case SR_CONF_ENABLED:
205 ret = rdtech_dps_get_state(sdi, &state);
208 if (!(state.mask & STATE_OUTPUT_ENABLED))
210 *data = g_variant_new_boolean(state.output_enabled);
212 case SR_CONF_REGULATION:
213 ret = rdtech_dps_get_state(sdi, &state);
216 if (!(state.mask & STATE_REGULATION_CC))
218 cc_text = state.regulation_cc ? "CC" : "CV";
219 *data = g_variant_new_string(cc_text);
221 case SR_CONF_VOLTAGE:
222 ret = rdtech_dps_get_state(sdi, &state);
225 if (!(state.mask & STATE_VOLTAGE))
227 *data = g_variant_new_double(state.voltage);
229 case SR_CONF_VOLTAGE_TARGET:
230 ret = rdtech_dps_get_state(sdi, &state);
233 if (!(state.mask & STATE_VOLTAGE_TARGET))
235 *data = g_variant_new_double(state.voltage_target);
237 case SR_CONF_CURRENT:
238 ret = rdtech_dps_get_state(sdi, &state);
241 if (!(state.mask & STATE_CURRENT))
243 *data = g_variant_new_double(state.current);
245 case SR_CONF_CURRENT_LIMIT:
246 ret = rdtech_dps_get_state(sdi, &state);
249 if (!(state.mask & STATE_CURRENT_LIMIT))
251 *data = g_variant_new_double(state.current_limit);
253 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
254 ret = rdtech_dps_get_state(sdi, &state);
257 if (!(state.mask & STATE_PROTECT_ENABLED))
259 *data = g_variant_new_boolean(state.protect_enabled);
261 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
262 ret = rdtech_dps_get_state(sdi, &state);
265 if (!(state.mask & STATE_PROTECT_OVP))
267 *data = g_variant_new_boolean(state.protect_ovp);
269 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
270 ret = rdtech_dps_get_state(sdi, &state);
273 if (!(state.mask & STATE_OUTPUT_ENABLED))
275 *data = g_variant_new_double(state.ovp_threshold);
277 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
278 ret = rdtech_dps_get_state(sdi, &state);
281 if (!(state.mask & STATE_PROTECT_ENABLED))
283 *data = g_variant_new_boolean(state.protect_enabled);
285 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
286 ret = rdtech_dps_get_state(sdi, &state);
289 if (!(state.mask & STATE_PROTECT_OCP))
291 *data = g_variant_new_boolean(state.protect_ocp);
293 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
294 ret = rdtech_dps_get_state(sdi, &state);
297 if (!(state.mask & STATE_OCP_THRESHOLD))
299 *data = g_variant_new_double(state.ocp_threshold);
308 static int config_set(uint32_t key, GVariant *data,
309 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
311 struct dev_context *devc;
312 struct rdtech_dps_state state;
317 memset(&state, 0, sizeof(state));
320 case SR_CONF_LIMIT_SAMPLES:
321 case SR_CONF_LIMIT_MSEC:
322 return sr_sw_limits_config_set(&devc->limits, key, data);
323 case SR_CONF_ENABLED:
324 state.output_enabled = g_variant_get_boolean(data);
325 state.mask |= STATE_OUTPUT_ENABLED;
326 return rdtech_dps_set_state(sdi, &state);
327 case SR_CONF_VOLTAGE_TARGET:
328 state.voltage_target = g_variant_get_double(data);
329 state.mask |= STATE_VOLTAGE_TARGET;
330 return rdtech_dps_set_state(sdi, &state);
331 case SR_CONF_CURRENT_LIMIT:
332 state.current_limit = g_variant_get_double(data);
333 state.mask |= STATE_CURRENT_LIMIT;
334 return rdtech_dps_set_state(sdi, &state);
335 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
336 state.ovp_threshold = g_variant_get_double(data);
337 state.mask |= STATE_OVP_THRESHOLD;
338 return rdtech_dps_set_state(sdi, &state);
339 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
340 state.ocp_threshold = g_variant_get_double(data);
341 state.mask |= STATE_OCP_THRESHOLD;
342 return rdtech_dps_set_state(sdi, &state);
350 static int config_list(uint32_t key, GVariant **data,
351 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
353 struct dev_context *devc;
355 devc = (sdi) ? sdi->priv : NULL;
358 case SR_CONF_SCAN_OPTIONS:
359 case SR_CONF_DEVICE_OPTIONS:
360 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
361 case SR_CONF_VOLTAGE_TARGET:
362 *data = std_gvar_min_max_step(0.0, devc->model->max_voltage,
363 1 / devc->voltage_multiplier);
365 case SR_CONF_CURRENT_LIMIT:
366 *data = std_gvar_min_max_step(0.0, devc->model->max_current,
367 1 / devc->current_multiplier);
376 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
378 struct dev_context *devc;
379 struct sr_modbus_dev_inst *modbus;
385 /* Seed internal state from current data. */
386 ret = rdtech_dps_seed_receive(sdi);
390 /* Register the periodic data reception callback. */
391 ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
392 rdtech_dps_receive_data, (void *)sdi);
396 sr_sw_limits_acquisition_start(&devc->limits);
397 std_session_send_df_header(sdi);
402 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
404 struct sr_modbus_dev_inst *modbus;
406 std_session_send_df_end(sdi);
409 sr_modbus_source_remove(sdi->session, modbus);
414 static struct sr_dev_driver rdtech_dps_driver_info = {
415 .name = "rdtech-dps",
416 .longname = "RDTech DPS/DPH series power supply",
419 .cleanup = std_cleanup,
421 .dev_list = std_dev_list,
422 .dev_clear = std_dev_clear,
423 .config_get = config_get,
424 .config_set = config_set,
425 .config_list = config_list,
426 .dev_open = dev_open,
427 .dev_close = dev_close,
428 .dev_acquisition_start = dev_acquisition_start,
429 .dev_acquisition_stop = dev_acquisition_stop,
432 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);