]> sigrok.org Git - libsigrok.git/blame - src/hardware/rdtech-dps/api.c
Backport recent changes from mainline.
[libsigrok.git] / src / hardware / rdtech-dps / api.c
CommitLineData
8cd15dd4
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
e4204b17 5 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
8cd15dd4
UH
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <config.h>
e4204b17 22#include <math.h>
8cd15dd4
UH
23#include "protocol.h"
24
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
27 SR_CONF_SERIALCOMM,
28 SR_CONF_MODBUSADDR,
29};
30
31static const uint32_t drvopts[] = {
32 SR_CONF_POWER_SUPPLY,
33};
34
35static const uint32_t devopts[] = {
36 SR_CONF_CONTINUOUS,
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
39 SR_CONF_VOLTAGE | SR_CONF_GET,
40 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_CURRENT | SR_CONF_GET,
42 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_REGULATION | SR_CONF_GET,
45 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
46 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
47 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
48 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
49};
50
51/* Model ID, model name, max current, max voltage, max power */
52static const struct rdtech_dps_model supported_models[] = {
e4204b17
UH
53 { 3005, "DPS3005", 5, 30, 160, 3, 2 },
54 { 5005, "DPS5005", 5, 50, 250, 3, 2 },
55 { 5205, "DPH5005", 5, 50, 250, 3, 2 },
56 { 5015, "DPS5015", 15, 50, 750, 2, 2 },
57 { 5020, "DPS5020", 20, 50, 1000, 2, 2 },
58 { 8005, "DPS8005", 5, 80, 408, 3, 2 },
8cd15dd4
UH
59};
60
61static struct sr_dev_driver rdtech_dps_driver_info;
62
63static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
64{
65 const struct rdtech_dps_model *model = NULL;
66 struct dev_context *devc;
67 struct sr_dev_inst *sdi;
68 uint16_t id, version;
69 unsigned int i;
70
71 int ret = rdtech_dps_get_model_version(modbus, &id, &version);
72 if (ret != SR_OK)
73 return NULL;
74 for (i = 0; i < ARRAY_SIZE(supported_models); i++)
75 if (id == supported_models[i].id) {
76 model = &supported_models[i];
77 break;
78 }
79 if (model == NULL) {
80 sr_err("Unknown model: %d.", id);
81 return NULL;
82 }
83
84 sdi = g_malloc0(sizeof(struct sr_dev_inst));
85 sdi->status = SR_ST_INACTIVE;
86 sdi->vendor = g_strdup("RDTech");
87 sdi->model = g_strdup(model->name);
88 sdi->version = g_strdup_printf("v%d", version);
89 sdi->conn = modbus;
90 sdi->driver = &rdtech_dps_driver_info;
91 sdi->inst_type = SR_INST_MODBUS;
92
93 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
94 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
95 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P");
96
97 devc = g_malloc0(sizeof(struct dev_context));
98 sr_sw_limits_init(&devc->limits);
99 devc->model = model;
e4204b17
UH
100 devc->current_multiplier = pow(10.0, model->current_digits);
101 devc->voltage_multiplier = pow(10.0, model->voltage_digits);
8cd15dd4
UH
102
103 sdi->priv = devc;
104
105 return sdi;
106}
107
108static int config_compare(gconstpointer a, gconstpointer b)
109{
110 const struct sr_config *ac = a, *bc = b;
111 return ac->key != bc->key;
112}
113
114static GSList *scan(struct sr_dev_driver *di, GSList *options)
115{
116 struct sr_config default_serialcomm = {
117 .key = SR_CONF_SERIALCOMM,
118 .data = g_variant_new_string("9600/8n1"),
119 };
120 struct sr_config default_modbusaddr = {
121 .key = SR_CONF_MODBUSADDR,
122 .data = g_variant_new_uint64(1),
123 };
124 GSList *opts = options, *devices;
125
126 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
127 opts = g_slist_prepend(opts, &default_serialcomm);
128 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
129 opts = g_slist_prepend(opts, &default_modbusaddr);
130
131 devices = sr_modbus_scan(di->context, opts, probe_device);
132
133 while (opts != options)
134 opts = g_slist_delete_link(opts, opts);
135 g_variant_unref(default_serialcomm.data);
136 g_variant_unref(default_modbusaddr.data);
137
138 return devices;
139}
140
141static int dev_open(struct sr_dev_inst *sdi)
142{
143 struct sr_modbus_dev_inst *modbus = sdi->conn;
144
145 if (sr_modbus_open(modbus) < 0)
146 return SR_ERR;
147
e4204b17 148 rdtech_dps_set_reg(sdi, REG_LOCK, 1);
8cd15dd4
UH
149
150 return SR_OK;
151}
152
153static int dev_close(struct sr_dev_inst *sdi)
154{
8cd15dd4
UH
155 struct sr_modbus_dev_inst *modbus;
156
157 modbus = sdi->conn;
8cd15dd4
UH
158 if (!modbus)
159 return SR_ERR_BUG;
160
e4204b17 161 rdtech_dps_set_reg(sdi, REG_LOCK, 0);
8cd15dd4
UH
162
163 return sr_modbus_close(modbus);
164}
165
166static int config_get(uint32_t key, GVariant **data,
167 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
168{
169 struct dev_context *devc;
8cd15dd4
UH
170 int ret;
171 uint16_t ivalue;
172
173 (void)cg;
174
8cd15dd4
UH
175 devc = sdi->priv;
176
177 ret = SR_OK;
178 switch (key) {
179 case SR_CONF_LIMIT_SAMPLES:
180 case SR_CONF_LIMIT_MSEC:
181 ret = sr_sw_limits_config_get(&devc->limits, key, data);
182 break;
183 case SR_CONF_ENABLED:
e4204b17 184 if ((ret = rdtech_dps_get_reg(sdi, REG_ENABLE, &ivalue)) == SR_OK)
8cd15dd4
UH
185 *data = g_variant_new_boolean(ivalue);
186 break;
187 case SR_CONF_REGULATION:
e4204b17 188 if ((ret = rdtech_dps_get_reg(sdi, REG_CV_CC, &ivalue)) != SR_OK)
8cd15dd4
UH
189 break;
190 *data = g_variant_new_string((ivalue == MODE_CC) ? "CC" : "CV");
191 break;
192 case SR_CONF_VOLTAGE:
e4204b17
UH
193 if ((ret = rdtech_dps_get_reg(sdi, REG_UOUT, &ivalue)) == SR_OK)
194 *data = g_variant_new_double((float)ivalue / devc->voltage_multiplier);
8cd15dd4
UH
195 break;
196 case SR_CONF_VOLTAGE_TARGET:
e4204b17
UH
197 if ((ret = rdtech_dps_get_reg(sdi, REG_USET, &ivalue)) == SR_OK)
198 *data = g_variant_new_double((float)ivalue / devc->voltage_multiplier);
8cd15dd4
UH
199 break;
200 case SR_CONF_CURRENT:
e4204b17
UH
201 if ((ret = rdtech_dps_get_reg(sdi, REG_IOUT, &ivalue)) == SR_OK)
202 *data = g_variant_new_double((float)ivalue / devc->current_multiplier);
8cd15dd4
UH
203 break;
204 case SR_CONF_CURRENT_LIMIT:
e4204b17
UH
205 if ((ret = rdtech_dps_get_reg(sdi, REG_ISET, &ivalue)) == SR_OK)
206 *data = g_variant_new_double((float)ivalue / devc->current_multiplier);
8cd15dd4
UH
207 break;
208 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
209 *data = g_variant_new_boolean(TRUE);
210 break;
211 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
e4204b17 212 if ((ret = rdtech_dps_get_reg(sdi, REG_PROTECT, &ivalue)) == SR_OK)
8cd15dd4
UH
213 *data = g_variant_new_boolean(ivalue == STATE_OVP);
214 break;
215 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
e4204b17
UH
216 if ((ret = rdtech_dps_get_reg(sdi, PRE_OVPSET, &ivalue)) == SR_OK)
217 *data = g_variant_new_double((float)ivalue / devc->voltage_multiplier);
8cd15dd4
UH
218 break;
219 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
220 *data = g_variant_new_boolean(TRUE);
221 break;
222 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
e4204b17 223 if ((ret = rdtech_dps_get_reg(sdi, REG_PROTECT, &ivalue)) == SR_OK)
8cd15dd4
UH
224 *data = g_variant_new_boolean(ivalue == STATE_OCP);
225 break;
226 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
e4204b17
UH
227 if ((ret = rdtech_dps_get_reg(sdi, PRE_OCPSET, &ivalue)) == SR_OK)
228 *data = g_variant_new_double((float)ivalue / devc->current_multiplier);
8cd15dd4
UH
229 break;
230 default:
231 return SR_ERR_NA;
232 }
233
234 return ret;
235}
236
237static int config_set(uint32_t key, GVariant *data,
238 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
239{
240 struct dev_context *devc;
8cd15dd4
UH
241
242 (void)cg;
243
8cd15dd4
UH
244 devc = sdi->priv;
245
246 switch (key) {
247 case SR_CONF_LIMIT_SAMPLES:
248 case SR_CONF_LIMIT_MSEC:
249 return sr_sw_limits_config_set(&devc->limits, key, data);
250 case SR_CONF_ENABLED:
e4204b17 251 return rdtech_dps_set_reg(sdi, REG_ENABLE, g_variant_get_boolean(data));
8cd15dd4 252 case SR_CONF_VOLTAGE_TARGET:
e4204b17
UH
253 return rdtech_dps_set_reg(sdi, REG_USET,
254 g_variant_get_double(data) * devc->voltage_multiplier);
8cd15dd4 255 case SR_CONF_CURRENT_LIMIT:
e4204b17
UH
256 return rdtech_dps_set_reg(sdi, REG_ISET,
257 g_variant_get_double(data) * devc->current_multiplier);
8cd15dd4 258 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
e4204b17
UH
259 return rdtech_dps_set_reg(sdi, PRE_OVPSET,
260 g_variant_get_double(data) * devc->voltage_multiplier);
8cd15dd4 261 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
e4204b17
UH
262 return rdtech_dps_set_reg(sdi, PRE_OCPSET,
263 g_variant_get_double(data) * devc->current_multiplier);
8cd15dd4
UH
264 default:
265 return SR_ERR_NA;
266 }
267
268 return SR_OK;
269}
270
271static int config_list(uint32_t key, GVariant **data,
272 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
273{
274 struct dev_context *devc;
275
276 devc = (sdi) ? sdi->priv : NULL;
277
278 switch (key) {
279 case SR_CONF_SCAN_OPTIONS:
280 case SR_CONF_DEVICE_OPTIONS:
281 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
282 case SR_CONF_VOLTAGE_TARGET:
e4204b17
UH
283 *data = std_gvar_min_max_step(0.0, devc->model->max_voltage,
284 1 / devc->voltage_multiplier);
8cd15dd4
UH
285 break;
286 case SR_CONF_CURRENT_LIMIT:
e4204b17
UH
287 *data = std_gvar_min_max_step(0.0, devc->model->max_current,
288 1 / devc->current_multiplier);
8cd15dd4
UH
289 break;
290 default:
291 return SR_ERR_NA;
292 }
293
294 return SR_OK;
295}
296
297static int dev_acquisition_start(const struct sr_dev_inst *sdi)
298{
299 struct dev_context *devc;
300 struct sr_modbus_dev_inst *modbus;
e4204b17 301 uint16_t registers[3];
8cd15dd4
UH
302 int ret;
303
304 modbus = sdi->conn;
305 devc = sdi->priv;
306
e4204b17
UH
307 /* Prefill actual states */
308 ret = rdtech_dps_read_holding_registers(modbus, REG_PROTECT, 3, registers);
309 if (ret != SR_OK)
310 return ret;
311 devc->actual_ovp_state = RB16(registers + 0) == STATE_OVP;
312 devc->actual_ocp_state = RB16(registers + 0) == STATE_OCP;
313 devc->actual_regulation_state = RB16(registers + 1);
314 devc->actual_output_state = RB16(registers + 2);
315
8cd15dd4
UH
316 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
317 rdtech_dps_receive_data, (void *)sdi)) != SR_OK)
318 return ret;
319
320 sr_sw_limits_acquisition_start(&devc->limits);
321 std_session_send_df_header(sdi);
322
e4204b17 323 return SR_OK;
8cd15dd4
UH
324}
325
326static int dev_acquisition_stop(struct sr_dev_inst *sdi)
327{
328 struct sr_modbus_dev_inst *modbus;
329
330 std_session_send_df_end(sdi);
331
332 modbus = sdi->conn;
333 sr_modbus_source_remove(sdi->session, modbus);
334
335 return SR_OK;
336}
337
338static struct sr_dev_driver rdtech_dps_driver_info = {
339 .name = "rdtech-dps",
340 .longname = "RDTech DPS/DPH series power supply",
341 .api_version = 1,
342 .init = std_init,
343 .cleanup = std_cleanup,
344 .scan = scan,
345 .dev_list = std_dev_list,
346 .dev_clear = std_dev_clear,
347 .config_get = config_get,
348 .config_set = config_set,
349 .config_list = config_list,
350 .dev_open = dev_open,
351 .dev_close = dev_close,
352 .dev_acquisition_start = dev_acquisition_start,
353 .dev_acquisition_stop = dev_acquisition_stop,
354 .context = NULL,
355};
356SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);