2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
33 static const uint32_t devopts[] = {
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_VOLTAGE | SR_CONF_GET,
38 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_CURRENT | SR_CONF_GET,
40 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_REGULATION | SR_CONF_GET,
43 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
44 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
46 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
49 /* Model ID, model name, max current, max voltage, max power */
50 static const struct rdtech_dps_model supported_models[] = {
51 { 3005, "DPS3005", 3, 50, 160 },
52 { 5005, "DPS5005", 5, 50, 250 },
53 { 5205, "DPH5005", 5, 50, 250 },
54 { 5015, "DPS5015", 15, 50, 750 },
55 { 5020, "DPS5020", 20, 50, 1000 },
56 { 8005, "DPS8005", 5, 80, 408 },
59 static struct sr_dev_driver rdtech_dps_driver_info;
61 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
63 const struct rdtech_dps_model *model = NULL;
64 struct dev_context *devc;
65 struct sr_dev_inst *sdi;
69 int ret = rdtech_dps_get_model_version(modbus, &id, &version);
72 for (i = 0; i < ARRAY_SIZE(supported_models); i++)
73 if (id == supported_models[i].id) {
74 model = &supported_models[i];
78 sr_err("Unknown model: %d.", id);
82 sdi = g_malloc0(sizeof(struct sr_dev_inst));
83 sdi->status = SR_ST_INACTIVE;
84 sdi->vendor = g_strdup("RDTech");
85 sdi->model = g_strdup(model->name);
86 sdi->version = g_strdup_printf("v%d", version);
88 sdi->driver = &rdtech_dps_driver_info;
89 sdi->inst_type = SR_INST_MODBUS;
91 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
92 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
93 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P");
95 devc = g_malloc0(sizeof(struct dev_context));
96 sr_sw_limits_init(&devc->limits);
98 devc->expecting_registers = 0;
105 static int config_compare(gconstpointer a, gconstpointer b)
107 const struct sr_config *ac = a, *bc = b;
108 return ac->key != bc->key;
111 static GSList *scan(struct sr_dev_driver *di, GSList *options)
113 struct sr_config default_serialcomm = {
114 .key = SR_CONF_SERIALCOMM,
115 .data = g_variant_new_string("9600/8n1"),
117 struct sr_config default_modbusaddr = {
118 .key = SR_CONF_MODBUSADDR,
119 .data = g_variant_new_uint64(1),
121 GSList *opts = options, *devices;
123 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
124 opts = g_slist_prepend(opts, &default_serialcomm);
125 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
126 opts = g_slist_prepend(opts, &default_modbusaddr);
128 devices = sr_modbus_scan(di->context, opts, probe_device);
130 while (opts != options)
131 opts = g_slist_delete_link(opts, opts);
132 g_variant_unref(default_serialcomm.data);
133 g_variant_unref(default_modbusaddr.data);
138 static int dev_open(struct sr_dev_inst *sdi)
140 struct sr_modbus_dev_inst *modbus = sdi->conn;
142 if (sr_modbus_open(modbus) < 0)
145 rdtech_dps_set_reg(modbus, REG_LOCK, 1);
150 static int dev_close(struct sr_dev_inst *sdi)
152 struct dev_context *devc;
153 struct sr_modbus_dev_inst *modbus;
162 if (devc->expecting_registers) {
163 /* Wait for the last data that was requested from the device. */
164 uint16_t registers[devc->expecting_registers];
165 sr_modbus_read_holding_registers(modbus, -1,
166 devc->expecting_registers, registers);
169 rdtech_dps_set_reg(modbus, REG_LOCK, 0);
171 return sr_modbus_close(modbus);
174 static int config_get(uint32_t key, GVariant **data,
175 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
177 struct dev_context *devc;
178 struct sr_modbus_dev_inst *modbus;
189 case SR_CONF_LIMIT_SAMPLES:
190 case SR_CONF_LIMIT_MSEC:
191 ret = sr_sw_limits_config_get(&devc->limits, key, data);
193 case SR_CONF_ENABLED:
194 if ((ret = rdtech_dps_get_reg(modbus, REG_ENABLE, &ivalue)) == SR_OK)
195 *data = g_variant_new_boolean(ivalue);
197 case SR_CONF_REGULATION:
198 if ((ret = rdtech_dps_get_reg(modbus, REG_CV_CC, &ivalue)) != SR_OK)
200 if (ivalue == MODE_CC)
201 *data = g_variant_new_string("CC");
203 *data = g_variant_new_string("CV");
205 case SR_CONF_VOLTAGE:
206 if ((ret = rdtech_dps_get_reg(modbus, REG_UOUT, &ivalue)) == SR_OK)
207 *data = g_variant_new_double((float)ivalue / 100.0f);
209 case SR_CONF_VOLTAGE_TARGET:
210 if ((ret = rdtech_dps_get_reg(modbus, REG_USET, &ivalue)) == SR_OK)
211 *data = g_variant_new_double((float)ivalue / 100.0f);
213 case SR_CONF_CURRENT:
214 if ((ret = rdtech_dps_get_reg(modbus, REG_IOUT, &ivalue)) == SR_OK)
215 *data = g_variant_new_double((float)ivalue / 100.0f);
217 case SR_CONF_CURRENT_LIMIT:
218 if ((ret = rdtech_dps_get_reg(modbus, REG_ISET, &ivalue)) == SR_OK)
219 *data = g_variant_new_double((float)ivalue / 1000.0f);
221 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
222 *data = g_variant_new_boolean(TRUE);
224 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
225 if ((ret = rdtech_dps_get_reg(modbus, REG_PROTECT, &ivalue)) == SR_OK)
226 *data = g_variant_new_boolean(ivalue == STATE_OVP);
228 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
229 if ((ret = rdtech_dps_get_reg(modbus, PRE_OVPSET, &ivalue)) == SR_OK)
230 *data = g_variant_new_double((float)ivalue / 100.0f);
232 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
233 *data = g_variant_new_boolean(TRUE);
235 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
236 if ((ret = rdtech_dps_get_reg(modbus, REG_PROTECT, &ivalue)) == SR_OK)
237 *data = g_variant_new_boolean(ivalue == STATE_OCP);
239 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
240 if ((ret = rdtech_dps_get_reg(modbus, PRE_OCPSET, &ivalue)) == SR_OK)
241 *data = g_variant_new_double((float)ivalue / 1000.0f);
250 static int config_set(uint32_t key, GVariant *data,
251 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
253 struct dev_context *devc;
254 struct sr_modbus_dev_inst *modbus;
262 case SR_CONF_LIMIT_SAMPLES:
263 case SR_CONF_LIMIT_MSEC:
264 return sr_sw_limits_config_set(&devc->limits, key, data);
265 case SR_CONF_ENABLED:
266 return rdtech_dps_set_reg(modbus, REG_ENABLE, g_variant_get_boolean(data));
267 case SR_CONF_VOLTAGE_TARGET:
268 return rdtech_dps_set_reg(modbus, REG_USET, g_variant_get_double(data) * 100);
269 case SR_CONF_CURRENT_LIMIT:
270 return rdtech_dps_set_reg(modbus, REG_ISET, g_variant_get_double(data) * 1000);
271 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
272 return rdtech_dps_set_reg(modbus, PRE_OVPSET, g_variant_get_double(data) * 100);
273 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
274 return rdtech_dps_set_reg(modbus, PRE_OCPSET, g_variant_get_double(data) * 1000);
282 static int config_list(uint32_t key, GVariant **data,
283 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
285 struct dev_context *devc;
287 devc = (sdi) ? sdi->priv : NULL;
290 case SR_CONF_SCAN_OPTIONS:
291 case SR_CONF_DEVICE_OPTIONS:
292 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
293 case SR_CONF_VOLTAGE_TARGET:
294 *data = std_gvar_min_max_step(0.0, devc->model->max_voltage, 0.001);
296 case SR_CONF_CURRENT_LIMIT:
297 *data = std_gvar_min_max_step(0.0, devc->model->max_current, 0.0001);
306 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
308 struct dev_context *devc;
309 struct sr_modbus_dev_inst *modbus;
315 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
316 rdtech_dps_receive_data, (void *)sdi)) != SR_OK)
319 sr_sw_limits_acquisition_start(&devc->limits);
320 std_session_send_df_header(sdi);
322 return rdtech_dps_capture_start(sdi);
325 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
327 struct sr_modbus_dev_inst *modbus;
329 std_session_send_df_end(sdi);
332 sr_modbus_source_remove(sdi->session, modbus);
337 static struct sr_dev_driver rdtech_dps_driver_info = {
338 .name = "rdtech-dps",
339 .longname = "RDTech DPS/DPH series power supply",
342 .cleanup = std_cleanup,
344 .dev_list = std_dev_list,
345 .dev_clear = std_dev_clear,
346 .config_get = config_get,
347 .config_set = config_set,
348 .config_list = config_list,
349 .dev_open = dev_open,
350 .dev_close = dev_close,
351 .dev_acquisition_start = dev_acquisition_start,
352 .dev_acquisition_stop = dev_acquisition_stop,
355 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);