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>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 static const uint32_t scanopts[] = {
30 static const uint32_t drvopts[] = {
34 static const uint32_t devopts[] = {
36 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_VOLTAGE | SR_CONF_GET,
39 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_CURRENT | SR_CONF_GET,
41 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
43 SR_CONF_REGULATION | SR_CONF_GET,
44 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
45 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
47 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
50 /* Model ID, model name, max current, max voltage, max power */
51 static const struct rdtech_dps_model supported_models[] = {
52 { 3005, "DPS3005", 3, 50, 160 },
53 { 5005, "DPS5005", 5, 50, 250 },
54 { 5205, "DPH5005", 5, 50, 250 },
55 { 5015, "DPS5015", 15, 50, 750 },
56 { 5020, "DPS5020", 20, 50, 1000 },
57 { 8005, "DPS8005", 5, 80, 408 },
60 static struct sr_dev_driver rdtech_dps_driver_info;
62 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
64 const struct rdtech_dps_model *model = NULL;
65 struct dev_context *devc;
66 struct sr_dev_inst *sdi;
70 int ret = rdtech_dps_get_model_version(modbus, &id, &version);
73 for (i = 0; i < ARRAY_SIZE(supported_models); i++)
74 if (id == supported_models[i].id) {
75 model = &supported_models[i];
79 sr_err("Unknown model: %d.", id);
83 sdi = g_malloc0(sizeof(struct sr_dev_inst));
84 sdi->status = SR_ST_INACTIVE;
85 sdi->vendor = g_strdup("RDTech");
86 sdi->model = g_strdup(model->name);
87 sdi->version = g_strdup_printf("v%d", version);
89 sdi->driver = &rdtech_dps_driver_info;
90 sdi->inst_type = SR_INST_MODBUS;
92 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
93 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
94 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P");
96 devc = g_malloc0(sizeof(struct dev_context));
97 sr_sw_limits_init(&devc->limits);
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(sdi, REG_LOCK, 1);
150 static int dev_close(struct sr_dev_inst *sdi)
152 struct sr_modbus_dev_inst *modbus;
158 rdtech_dps_set_reg(sdi, REG_LOCK, 0);
160 return sr_modbus_close(modbus);
163 static int config_get(uint32_t key, GVariant **data,
164 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
166 struct dev_context *devc;
176 case SR_CONF_LIMIT_SAMPLES:
177 case SR_CONF_LIMIT_MSEC:
178 ret = sr_sw_limits_config_get(&devc->limits, key, data);
180 case SR_CONF_ENABLED:
181 if ((ret = rdtech_dps_get_reg(sdi, REG_ENABLE, &ivalue)) == SR_OK)
182 *data = g_variant_new_boolean(ivalue);
184 case SR_CONF_REGULATION:
185 if ((ret = rdtech_dps_get_reg(sdi, REG_CV_CC, &ivalue)) != SR_OK)
187 *data = g_variant_new_string((ivalue == MODE_CC) ? "CC" : "CV");
189 case SR_CONF_VOLTAGE:
190 if ((ret = rdtech_dps_get_reg(sdi, REG_UOUT, &ivalue)) == SR_OK)
191 *data = g_variant_new_double((float)ivalue / 100.0f);
193 case SR_CONF_VOLTAGE_TARGET:
194 if ((ret = rdtech_dps_get_reg(sdi, REG_USET, &ivalue)) == SR_OK)
195 *data = g_variant_new_double((float)ivalue / 100.0f);
197 case SR_CONF_CURRENT:
198 if ((ret = rdtech_dps_get_reg(sdi, REG_IOUT, &ivalue)) == SR_OK)
199 *data = g_variant_new_double((float)ivalue / 100.0f);
201 case SR_CONF_CURRENT_LIMIT:
202 if ((ret = rdtech_dps_get_reg(sdi, REG_ISET, &ivalue)) == SR_OK)
203 *data = g_variant_new_double((float)ivalue / 1000.0f);
205 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
206 *data = g_variant_new_boolean(TRUE);
208 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
209 if ((ret = rdtech_dps_get_reg(sdi, REG_PROTECT, &ivalue)) == SR_OK)
210 *data = g_variant_new_boolean(ivalue == STATE_OVP);
212 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
213 if ((ret = rdtech_dps_get_reg(sdi, PRE_OVPSET, &ivalue)) == SR_OK)
214 *data = g_variant_new_double((float)ivalue / 100.0f);
216 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
217 *data = g_variant_new_boolean(TRUE);
219 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
220 if ((ret = rdtech_dps_get_reg(sdi, REG_PROTECT, &ivalue)) == SR_OK)
221 *data = g_variant_new_boolean(ivalue == STATE_OCP);
223 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
224 if ((ret = rdtech_dps_get_reg(sdi, PRE_OCPSET, &ivalue)) == SR_OK)
225 *data = g_variant_new_double((float)ivalue / 1000.0f);
234 static int config_set(uint32_t key, GVariant *data,
235 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
237 struct dev_context *devc;
244 case SR_CONF_LIMIT_SAMPLES:
245 case SR_CONF_LIMIT_MSEC:
246 return sr_sw_limits_config_set(&devc->limits, key, data);
247 case SR_CONF_ENABLED:
248 return rdtech_dps_set_reg(sdi, REG_ENABLE, g_variant_get_boolean(data));
249 case SR_CONF_VOLTAGE_TARGET:
250 return rdtech_dps_set_reg(sdi, REG_USET, g_variant_get_double(data) * 100);
251 case SR_CONF_CURRENT_LIMIT:
252 return rdtech_dps_set_reg(sdi, REG_ISET, g_variant_get_double(data) * 1000);
253 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
254 return rdtech_dps_set_reg(sdi, PRE_OVPSET, g_variant_get_double(data) * 100);
255 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
256 return rdtech_dps_set_reg(sdi, PRE_OCPSET, g_variant_get_double(data) * 1000);
264 static int config_list(uint32_t key, GVariant **data,
265 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
267 struct dev_context *devc;
269 devc = (sdi) ? sdi->priv : NULL;
272 case SR_CONF_SCAN_OPTIONS:
273 case SR_CONF_DEVICE_OPTIONS:
274 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
275 case SR_CONF_VOLTAGE_TARGET:
276 *data = std_gvar_min_max_step(0.0, devc->model->max_voltage, 0.001);
278 case SR_CONF_CURRENT_LIMIT:
279 *data = std_gvar_min_max_step(0.0, devc->model->max_current, 0.0001);
288 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
290 struct dev_context *devc;
291 struct sr_modbus_dev_inst *modbus;
297 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
298 rdtech_dps_receive_data, (void *)sdi)) != SR_OK)
301 sr_sw_limits_acquisition_start(&devc->limits);
302 std_session_send_df_header(sdi);
307 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
309 struct sr_modbus_dev_inst *modbus;
311 std_session_send_df_end(sdi);
314 sr_modbus_source_remove(sdi->session, modbus);
319 static struct sr_dev_driver rdtech_dps_driver_info = {
320 .name = "rdtech-dps",
321 .longname = "RDTech DPS/DPH series power supply",
324 .cleanup = std_cleanup,
326 .dev_list = std_dev_list,
327 .dev_clear = std_dev_clear,
328 .config_get = config_get,
329 .config_set = config_set,
330 .config_list = config_list,
331 .dev_open = dev_open,
332 .dev_close = dev_close,
333 .dev_acquisition_start = dev_acquisition_start,
334 .dev_acquisition_stop = dev_acquisition_stop,
337 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);