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 *data = g_variant_new_string((ivalue == MODE_CC) ? "CC" : "CV");
202 case SR_CONF_VOLTAGE:
203 if ((ret = rdtech_dps_get_reg(modbus, REG_UOUT, &ivalue)) == SR_OK)
204 *data = g_variant_new_double((float)ivalue / 100.0f);
206 case SR_CONF_VOLTAGE_TARGET:
207 if ((ret = rdtech_dps_get_reg(modbus, REG_USET, &ivalue)) == SR_OK)
208 *data = g_variant_new_double((float)ivalue / 100.0f);
210 case SR_CONF_CURRENT:
211 if ((ret = rdtech_dps_get_reg(modbus, REG_IOUT, &ivalue)) == SR_OK)
212 *data = g_variant_new_double((float)ivalue / 100.0f);
214 case SR_CONF_CURRENT_LIMIT:
215 if ((ret = rdtech_dps_get_reg(modbus, REG_ISET, &ivalue)) == SR_OK)
216 *data = g_variant_new_double((float)ivalue / 1000.0f);
218 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
219 *data = g_variant_new_boolean(TRUE);
221 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
222 if ((ret = rdtech_dps_get_reg(modbus, REG_PROTECT, &ivalue)) == SR_OK)
223 *data = g_variant_new_boolean(ivalue == STATE_OVP);
225 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
226 if ((ret = rdtech_dps_get_reg(modbus, PRE_OVPSET, &ivalue)) == SR_OK)
227 *data = g_variant_new_double((float)ivalue / 100.0f);
229 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
230 *data = g_variant_new_boolean(TRUE);
232 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
233 if ((ret = rdtech_dps_get_reg(modbus, REG_PROTECT, &ivalue)) == SR_OK)
234 *data = g_variant_new_boolean(ivalue == STATE_OCP);
236 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
237 if ((ret = rdtech_dps_get_reg(modbus, PRE_OCPSET, &ivalue)) == SR_OK)
238 *data = g_variant_new_double((float)ivalue / 1000.0f);
247 static int config_set(uint32_t key, GVariant *data,
248 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
250 struct dev_context *devc;
251 struct sr_modbus_dev_inst *modbus;
259 case SR_CONF_LIMIT_SAMPLES:
260 case SR_CONF_LIMIT_MSEC:
261 return sr_sw_limits_config_set(&devc->limits, key, data);
262 case SR_CONF_ENABLED:
263 return rdtech_dps_set_reg(modbus, REG_ENABLE, g_variant_get_boolean(data));
264 case SR_CONF_VOLTAGE_TARGET:
265 return rdtech_dps_set_reg(modbus, REG_USET, g_variant_get_double(data) * 100);
266 case SR_CONF_CURRENT_LIMIT:
267 return rdtech_dps_set_reg(modbus, REG_ISET, g_variant_get_double(data) * 1000);
268 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
269 return rdtech_dps_set_reg(modbus, PRE_OVPSET, g_variant_get_double(data) * 100);
270 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
271 return rdtech_dps_set_reg(modbus, PRE_OCPSET, g_variant_get_double(data) * 1000);
279 static int config_list(uint32_t key, GVariant **data,
280 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
282 struct dev_context *devc;
284 devc = (sdi) ? sdi->priv : NULL;
287 case SR_CONF_SCAN_OPTIONS:
288 case SR_CONF_DEVICE_OPTIONS:
289 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
290 case SR_CONF_VOLTAGE_TARGET:
291 *data = std_gvar_min_max_step(0.0, devc->model->max_voltage, 0.001);
293 case SR_CONF_CURRENT_LIMIT:
294 *data = std_gvar_min_max_step(0.0, devc->model->max_current, 0.0001);
303 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
305 struct dev_context *devc;
306 struct sr_modbus_dev_inst *modbus;
312 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
313 rdtech_dps_receive_data, (void *)sdi)) != SR_OK)
316 sr_sw_limits_acquisition_start(&devc->limits);
317 std_session_send_df_header(sdi);
319 return rdtech_dps_capture_start(sdi);
322 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
324 struct sr_modbus_dev_inst *modbus;
326 std_session_send_df_end(sdi);
329 sr_modbus_source_remove(sdi->session, modbus);
334 static struct sr_dev_driver rdtech_dps_driver_info = {
335 .name = "rdtech-dps",
336 .longname = "RDTech DPS/DPH series power supply",
339 .cleanup = std_cleanup,
341 .dev_list = std_dev_list,
342 .dev_clear = std_dev_clear,
343 .config_get = config_get,
344 .config_set = config_set,
345 .config_list = config_list,
346 .dev_open = dev_open,
347 .dev_close = dev_close,
348 .dev_acquisition_start = dev_acquisition_start,
349 .dev_acquisition_stop = dev_acquisition_stop,
352 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);