]>
Commit | Line | Data |
---|---|---|
0549416e JC |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2018 James Churchill <pelrun@gmail.com> | |
7c0891b0 | 5 | * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net> |
0549416e JC |
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> | |
cce6a8a1 | 22 | #include <math.h> |
0549416e JC |
23 | #include "protocol.h" |
24 | ||
69b05583 JC |
25 | static const uint32_t scanopts[] = { |
26 | SR_CONF_CONN, | |
27 | SR_CONF_SERIALCOMM, | |
28 | SR_CONF_MODBUSADDR, | |
29 | }; | |
30 | ||
31 | static const uint32_t drvopts[] = { | |
32 | SR_CONF_POWER_SUPPLY, | |
33 | }; | |
34 | ||
35 | static 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 */ | |
52 | static const struct rdtech_dps_model supported_models[] = { | |
cce6a8a1 FS |
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 }, | |
69b05583 JC |
59 | }; |
60 | ||
61 | static struct sr_dev_driver rdtech_dps_driver_info; | |
62 | ||
63 | static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus) | |
0549416e | 64 | { |
69b05583 JC |
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; | |
0549416e | 92 | |
69b05583 JC |
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"); | |
0549416e | 96 | |
69b05583 JC |
97 | devc = g_malloc0(sizeof(struct dev_context)); |
98 | sr_sw_limits_init(&devc->limits); | |
99 | devc->model = model; | |
cce6a8a1 FS |
100 | devc->current_multiplier = pow(10.0, model->current_digits); |
101 | devc->voltage_multiplier = pow(10.0, model->voltage_digits); | |
0549416e | 102 | |
69b05583 JC |
103 | sdi->priv = devc; |
104 | ||
105 | return sdi; | |
106 | } | |
107 | ||
108 | static 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 | ||
114 | static 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); | |
0549416e JC |
137 | |
138 | return devices; | |
139 | } | |
140 | ||
141 | static int dev_open(struct sr_dev_inst *sdi) | |
142 | { | |
69b05583 JC |
143 | struct sr_modbus_dev_inst *modbus = sdi->conn; |
144 | ||
145 | if (sr_modbus_open(modbus) < 0) | |
146 | return SR_ERR; | |
0549416e | 147 | |
7c0891b0 | 148 | rdtech_dps_set_reg(sdi, REG_LOCK, 1); |
0549416e JC |
149 | |
150 | return SR_OK; | |
151 | } | |
152 | ||
153 | static int dev_close(struct sr_dev_inst *sdi) | |
154 | { | |
69b05583 | 155 | struct sr_modbus_dev_inst *modbus; |
0549416e | 156 | |
69b05583 | 157 | modbus = sdi->conn; |
69b05583 JC |
158 | if (!modbus) |
159 | return SR_ERR_BUG; | |
160 | ||
7c0891b0 | 161 | rdtech_dps_set_reg(sdi, REG_LOCK, 0); |
69b05583 JC |
162 | |
163 | return sr_modbus_close(modbus); | |
0549416e JC |
164 | } |
165 | ||
166 | static int config_get(uint32_t key, GVariant **data, | |
167 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
168 | { | |
69b05583 | 169 | struct dev_context *devc; |
0549416e | 170 | int ret; |
69b05583 | 171 | uint16_t ivalue; |
0549416e | 172 | |
0549416e JC |
173 | (void)cg; |
174 | ||
69b05583 JC |
175 | devc = sdi->priv; |
176 | ||
0549416e JC |
177 | ret = SR_OK; |
178 | switch (key) { | |
69b05583 JC |
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: | |
7c0891b0 | 184 | if ((ret = rdtech_dps_get_reg(sdi, REG_ENABLE, &ivalue)) == SR_OK) |
69b05583 JC |
185 | *data = g_variant_new_boolean(ivalue); |
186 | break; | |
187 | case SR_CONF_REGULATION: | |
7c0891b0 | 188 | if ((ret = rdtech_dps_get_reg(sdi, REG_CV_CC, &ivalue)) != SR_OK) |
69b05583 | 189 | break; |
66d2cc3a | 190 | *data = g_variant_new_string((ivalue == MODE_CC) ? "CC" : "CV"); |
69b05583 JC |
191 | break; |
192 | case SR_CONF_VOLTAGE: | |
7c0891b0 | 193 | if ((ret = rdtech_dps_get_reg(sdi, REG_UOUT, &ivalue)) == SR_OK) |
cce6a8a1 | 194 | *data = g_variant_new_double((float)ivalue / devc->voltage_multiplier); |
69b05583 JC |
195 | break; |
196 | case SR_CONF_VOLTAGE_TARGET: | |
7c0891b0 | 197 | if ((ret = rdtech_dps_get_reg(sdi, REG_USET, &ivalue)) == SR_OK) |
cce6a8a1 | 198 | *data = g_variant_new_double((float)ivalue / devc->voltage_multiplier); |
69b05583 JC |
199 | break; |
200 | case SR_CONF_CURRENT: | |
7c0891b0 | 201 | if ((ret = rdtech_dps_get_reg(sdi, REG_IOUT, &ivalue)) == SR_OK) |
cce6a8a1 | 202 | *data = g_variant_new_double((float)ivalue / devc->current_multiplier); |
69b05583 JC |
203 | break; |
204 | case SR_CONF_CURRENT_LIMIT: | |
7c0891b0 | 205 | if ((ret = rdtech_dps_get_reg(sdi, REG_ISET, &ivalue)) == SR_OK) |
cce6a8a1 | 206 | *data = g_variant_new_double((float)ivalue / devc->current_multiplier); |
69b05583 JC |
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: | |
7c0891b0 | 212 | if ((ret = rdtech_dps_get_reg(sdi, REG_PROTECT, &ivalue)) == SR_OK) |
69b05583 JC |
213 | *data = g_variant_new_boolean(ivalue == STATE_OVP); |
214 | break; | |
215 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
7c0891b0 | 216 | if ((ret = rdtech_dps_get_reg(sdi, PRE_OVPSET, &ivalue)) == SR_OK) |
cce6a8a1 | 217 | *data = g_variant_new_double((float)ivalue / devc->voltage_multiplier); |
69b05583 JC |
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: | |
7c0891b0 | 223 | if ((ret = rdtech_dps_get_reg(sdi, REG_PROTECT, &ivalue)) == SR_OK) |
69b05583 JC |
224 | *data = g_variant_new_boolean(ivalue == STATE_OCP); |
225 | break; | |
226 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
7c0891b0 | 227 | if ((ret = rdtech_dps_get_reg(sdi, PRE_OCPSET, &ivalue)) == SR_OK) |
cce6a8a1 | 228 | *data = g_variant_new_double((float)ivalue / devc->current_multiplier); |
69b05583 | 229 | break; |
0549416e JC |
230 | default: |
231 | return SR_ERR_NA; | |
232 | } | |
233 | ||
234 | return ret; | |
235 | } | |
236 | ||
237 | static int config_set(uint32_t key, GVariant *data, | |
238 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
239 | { | |
69b05583 | 240 | struct dev_context *devc; |
0549416e | 241 | |
0549416e JC |
242 | (void)cg; |
243 | ||
69b05583 JC |
244 | devc = sdi->priv; |
245 | ||
0549416e | 246 | switch (key) { |
69b05583 JC |
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: | |
7c0891b0 | 251 | return rdtech_dps_set_reg(sdi, REG_ENABLE, g_variant_get_boolean(data)); |
69b05583 | 252 | case SR_CONF_VOLTAGE_TARGET: |
cce6a8a1 FS |
253 | return rdtech_dps_set_reg(sdi, REG_USET, |
254 | g_variant_get_double(data) * devc->voltage_multiplier); | |
69b05583 | 255 | case SR_CONF_CURRENT_LIMIT: |
cce6a8a1 FS |
256 | return rdtech_dps_set_reg(sdi, REG_ISET, |
257 | g_variant_get_double(data) * devc->current_multiplier); | |
69b05583 | 258 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: |
cce6a8a1 FS |
259 | return rdtech_dps_set_reg(sdi, PRE_OVPSET, |
260 | g_variant_get_double(data) * devc->voltage_multiplier); | |
69b05583 | 261 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: |
cce6a8a1 FS |
262 | return rdtech_dps_set_reg(sdi, PRE_OCPSET, |
263 | g_variant_get_double(data) * devc->current_multiplier); | |
0549416e | 264 | default: |
69b05583 | 265 | return SR_ERR_NA; |
0549416e JC |
266 | } |
267 | ||
69b05583 | 268 | return SR_OK; |
0549416e JC |
269 | } |
270 | ||
271 | static int config_list(uint32_t key, GVariant **data, | |
272 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
273 | { | |
69b05583 | 274 | struct dev_context *devc; |
0549416e | 275 | |
69b05583 | 276 | devc = (sdi) ? sdi->priv : NULL; |
0549416e | 277 | |
0549416e | 278 | switch (key) { |
69b05583 JC |
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: | |
cce6a8a1 FS |
283 | *data = std_gvar_min_max_step(0.0, devc->model->max_voltage, |
284 | 1 / devc->voltage_multiplier); | |
69b05583 JC |
285 | break; |
286 | case SR_CONF_CURRENT_LIMIT: | |
cce6a8a1 FS |
287 | *data = std_gvar_min_max_step(0.0, devc->model->max_current, |
288 | 1 / devc->current_multiplier); | |
69b05583 | 289 | break; |
0549416e JC |
290 | default: |
291 | return SR_ERR_NA; | |
292 | } | |
293 | ||
69b05583 | 294 | return SR_OK; |
0549416e JC |
295 | } |
296 | ||
297 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
298 | { | |
69b05583 JC |
299 | struct dev_context *devc; |
300 | struct sr_modbus_dev_inst *modbus; | |
dfdf4c83 | 301 | uint16_t registers[3]; |
69b05583 | 302 | int ret; |
0549416e | 303 | |
69b05583 JC |
304 | modbus = sdi->conn; |
305 | devc = sdi->priv; | |
0549416e | 306 | |
dfdf4c83 FS |
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 | ||
69b05583 JC |
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 | ||
7c0891b0 | 323 | return SR_OK; |
0549416e JC |
324 | } |
325 | ||
326 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
327 | { | |
69b05583 | 328 | struct sr_modbus_dev_inst *modbus; |
0549416e | 329 | |
69b05583 JC |
330 | std_session_send_df_end(sdi); |
331 | ||
332 | modbus = sdi->conn; | |
333 | sr_modbus_source_remove(sdi->session, modbus); | |
0549416e JC |
334 | |
335 | return SR_OK; | |
336 | } | |
337 | ||
69b05583 | 338 | static struct sr_dev_driver rdtech_dps_driver_info = { |
0549416e | 339 | .name = "rdtech-dps", |
69b05583 | 340 | .longname = "RDTech DPS/DPH series power supply", |
0549416e JC |
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 | }; | |
0549416e | 356 | SR_REGISTER_DEV_DRIVER(rdtech_dps_driver_info); |