scpi-pps: Add a missing "break" in config_get().
[libsigrok.git] / src / hardware / rdtech-dps / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include "protocol.h"
22
23 static const uint32_t scanopts[] = {
24         SR_CONF_CONN,
25         SR_CONF_SERIALCOMM,
26         SR_CONF_MODBUSADDR,
27 };
28
29 static const uint32_t drvopts[] = {
30         SR_CONF_POWER_SUPPLY,
31 };
32
33 static const uint32_t devopts[] = {
34         SR_CONF_CONTINUOUS,
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,
47 };
48
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 },
57 };
58
59 static struct sr_dev_driver rdtech_dps_driver_info;
60
61 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
62 {
63         const struct rdtech_dps_model *model = NULL;
64         struct dev_context *devc;
65         struct sr_dev_inst *sdi;
66         uint16_t id, version;
67         unsigned int i;
68
69         int ret = rdtech_dps_get_model_version(modbus, &id, &version);
70         if (ret != SR_OK)
71                 return NULL;
72         for (i = 0; i < ARRAY_SIZE(supported_models); i++)
73                 if (id == supported_models[i].id) {
74                         model = &supported_models[i];
75                         break;
76                 }
77         if (model == NULL) {
78                 sr_err("Unknown model: %d.", id);
79                 return NULL;
80         }
81
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);
87         sdi->conn = modbus;
88         sdi->driver = &rdtech_dps_driver_info;
89         sdi->inst_type = SR_INST_MODBUS;
90
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");
94
95         devc = g_malloc0(sizeof(struct dev_context));
96         sr_sw_limits_init(&devc->limits);
97         devc->model = model;
98         devc->expecting_registers = 0;
99
100         sdi->priv = devc;
101
102         return sdi;
103 }
104
105 static int config_compare(gconstpointer a, gconstpointer b)
106 {
107         const struct sr_config *ac = a, *bc = b;
108         return ac->key != bc->key;
109 }
110
111 static GSList *scan(struct sr_dev_driver *di, GSList *options)
112 {
113         struct sr_config default_serialcomm = {
114                 .key = SR_CONF_SERIALCOMM,
115                 .data = g_variant_new_string("9600/8n1"),
116         };
117         struct sr_config default_modbusaddr = {
118                 .key = SR_CONF_MODBUSADDR,
119                 .data = g_variant_new_uint64(1),
120         };
121         GSList *opts = options, *devices;
122
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);
127
128         devices = sr_modbus_scan(di->context, opts, probe_device);
129
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);
134
135         return devices;
136 }
137
138 static int dev_open(struct sr_dev_inst *sdi)
139 {
140         struct sr_modbus_dev_inst *modbus = sdi->conn;
141
142         if (sr_modbus_open(modbus) < 0)
143                 return SR_ERR;
144
145         rdtech_dps_set_reg(modbus, REG_LOCK, 1);
146
147         return SR_OK;
148 }
149
150 static int dev_close(struct sr_dev_inst *sdi)
151 {
152         struct dev_context *devc;
153         struct sr_modbus_dev_inst *modbus;
154
155         modbus = sdi->conn;
156
157         if (!modbus)
158                 return SR_ERR_BUG;
159
160         devc = sdi->priv;
161
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);
167         }
168
169         rdtech_dps_set_reg(modbus, REG_LOCK, 0);
170
171         return sr_modbus_close(modbus);
172 }
173
174 static int config_get(uint32_t key, GVariant **data,
175         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
176 {
177         struct dev_context *devc;
178         struct sr_modbus_dev_inst *modbus;
179         int ret;
180         uint16_t ivalue;
181
182         (void)cg;
183
184         modbus = sdi->conn;
185         devc = sdi->priv;
186
187         ret = SR_OK;
188         switch (key) {
189         case SR_CONF_LIMIT_SAMPLES:
190         case SR_CONF_LIMIT_MSEC:
191                 ret = sr_sw_limits_config_get(&devc->limits, key, data);
192                 break;
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);
196                 break;
197         case SR_CONF_REGULATION:
198                 if ((ret = rdtech_dps_get_reg(modbus, REG_CV_CC, &ivalue)) != SR_OK)
199                         break;
200                 *data = g_variant_new_string((ivalue == MODE_CC) ? "CC" : "CV");
201                 break;
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);
205                 break;
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);
209                 break;
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);
213                 break;
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);
217                 break;
218         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
219                 *data = g_variant_new_boolean(TRUE);
220                 break;
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);
224                 break;
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);
228                 break;
229         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
230                 *data = g_variant_new_boolean(TRUE);
231                 break;
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);
235                 break;
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);
239                 break;
240         default:
241                 return SR_ERR_NA;
242         }
243
244         return ret;
245 }
246
247 static int config_set(uint32_t key, GVariant *data,
248         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
249 {
250         struct dev_context *devc;
251         struct sr_modbus_dev_inst *modbus;
252
253         (void)cg;
254
255         modbus = sdi->conn;
256         devc = sdi->priv;
257
258         switch (key) {
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);
272         default:
273                 return SR_ERR_NA;
274         }
275
276         return SR_OK;
277 }
278
279 static int config_list(uint32_t key, GVariant **data,
280         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
281 {
282         struct dev_context *devc;
283
284         devc = (sdi) ? sdi->priv : NULL;
285
286         switch (key) {
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);
292                 break;
293         case SR_CONF_CURRENT_LIMIT:
294                 *data = std_gvar_min_max_step(0.0, devc->model->max_current, 0.0001);
295                 break;
296         default:
297                 return SR_ERR_NA;
298         }
299
300         return SR_OK;
301 }
302
303 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
304 {
305         struct dev_context *devc;
306         struct sr_modbus_dev_inst *modbus;
307         int ret;
308
309         modbus = sdi->conn;
310         devc = sdi->priv;
311
312         if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
313                         rdtech_dps_receive_data, (void *)sdi)) != SR_OK)
314                 return ret;
315
316         sr_sw_limits_acquisition_start(&devc->limits);
317         std_session_send_df_header(sdi);
318
319         return rdtech_dps_capture_start(sdi);
320 }
321
322 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
323 {
324         struct sr_modbus_dev_inst *modbus;
325
326         std_session_send_df_end(sdi);
327
328         modbus = sdi->conn;
329         sr_modbus_source_remove(sdi->session, modbus);
330
331         return SR_OK;
332 }
333
334 static struct sr_dev_driver rdtech_dps_driver_info = {
335         .name = "rdtech-dps",
336         .longname = "RDTech DPS/DPH series power supply",
337         .api_version = 1,
338         .init = std_init,
339         .cleanup = std_cleanup,
340         .scan = scan,
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,
350         .context = NULL,
351 };
352 SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info);