]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com> | |
5 | * Copyright (C) 2018-2019 Frank Stettner <frank-stettner@gmx.net> | |
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> | |
22 | #include "protocol.h" | |
23 | ||
24 | static const uint32_t scanopts[] = { | |
25 | SR_CONF_CONN, | |
26 | SR_CONF_SERIALCOMM, | |
27 | SR_CONF_FORCE_DETECT, | |
28 | }; | |
29 | ||
30 | static const uint32_t drvopts[] = { | |
31 | SR_CONF_POWER_SUPPLY, | |
32 | }; | |
33 | ||
34 | static const uint32_t devopts[] = { | |
35 | SR_CONF_CONN | SR_CONF_GET, | |
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_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
46 | SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
47 | }; | |
48 | ||
49 | static const struct korad_kaxxxxp_model models[] = { | |
50 | /* Device enum, vendor, model, ID reply, channels, voltage, current */ | |
51 | {VELLEMAN_PS3005D, "Velleman", "PS3005D", | |
52 | "VELLEMANPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
53 | {VELLEMAN_LABPS3005D, "Velleman", "LABPS3005D", | |
54 | "VELLEMANLABPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
55 | {KORAD_KA3005P, "Korad", "KA3005P", | |
56 | "KORADKA3005PV2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
57 | /* Sometimes the KA3005P has an extra 0x01 after the ID. */ | |
58 | {KORAD_KA3005P_0X01, "Korad", "KA3005P", | |
59 | "KORADKA3005PV2.0\x01", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
60 | /* Sometimes the KA3005P has an extra 0xBC after the ID. */ | |
61 | {KORAD_KA3005P_0XBC, "Korad", "KA3005P", | |
62 | "KORADKA3005PV2.0\xBC", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
63 | {KORAD_KA3005P_V42, "Korad", "KA3005P", | |
64 | "KORAD KA3005P V4.2", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
65 | {KORAD_KA3005P_V55, "Korad", "KA3005P", | |
66 | "KORAD KA3005P V5.5", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
67 | {KORAD_KD3005P, "Korad", "KD3005P", | |
68 | "KORAD KD3005P V2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
69 | {KORAD_KD3005P_V20_NOSP, "Korad", "KD3005P", | |
70 | "KORADKD3005PV2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
71 | {RND_320_KD3005P, "RND", "KD3005P", | |
72 | "RND 320-KD3005P V4.2", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
73 | {RND_320_KA3005P, "RND", "KA3005P", | |
74 | "RND 320-KA3005P V5.5", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
75 | {RND_320K30PV, "RND", "KA3005P", | |
76 | "RND 320-KA3005P V2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
77 | {TENMA_72_2550_V2, "Tenma", "72-2550", | |
78 | "TENMA72-2550V2.0", 1, {0, 61, 0.01}, {0, 3.1, 0.001}}, | |
79 | {TENMA_72_2540_V20, "Tenma", "72-2540", | |
80 | "TENMA72-2540V2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
81 | {TENMA_72_2540_V21, "Tenma", "72-2540", | |
82 | "TENMA 72-2540 V2.1", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
83 | {TENMA_72_2540_V52, "Tenma", "72-2540", | |
84 | "TENMA 72-2540 V5.2", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
85 | {TENMA_72_2535_V21, "Tenma", "72-2535", | |
86 | "TENMA 72-2535 V2.1", 1, {0, 31, 0.01}, {0, 3.1, 0.001}}, | |
87 | {STAMOS_SLS31_V20, "Stamos Soldering", "S-LS-31", | |
88 | "S-LS-31 V2.0", 1, {0, 31, 0.01}, {0, 5.1, 0.001}}, | |
89 | {KORAD_KD6005P, "Korad", "KD6005P", | |
90 | "KORAD KD6005P V2.2", 1, {0, 61, 0.01}, {0, 5.1, 0.001}}, | |
91 | ALL_ZERO | |
92 | }; | |
93 | ||
94 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
95 | { | |
96 | static const char *serno_prefix = " SN:"; | |
97 | ||
98 | struct dev_context *devc; | |
99 | GSList *l; | |
100 | struct sr_dev_inst *sdi; | |
101 | struct sr_config *src; | |
102 | const char *conn, *serialcomm; | |
103 | const char *force_detect; | |
104 | struct sr_serial_dev_inst *serial; | |
105 | char reply[50]; | |
106 | int ret, i, model_id; | |
107 | size_t len; | |
108 | char *serno; | |
109 | ||
110 | conn = NULL; | |
111 | serialcomm = NULL; | |
112 | force_detect = NULL; | |
113 | ||
114 | for (l = options; l; l = l->next) { | |
115 | src = l->data; | |
116 | switch (src->key) { | |
117 | case SR_CONF_CONN: | |
118 | conn = g_variant_get_string(src->data, NULL); | |
119 | break; | |
120 | case SR_CONF_SERIALCOMM: | |
121 | serialcomm = g_variant_get_string(src->data, NULL); | |
122 | break; | |
123 | case SR_CONF_FORCE_DETECT: | |
124 | force_detect = g_variant_get_string(src->data, NULL); | |
125 | break; | |
126 | default: | |
127 | sr_err("Unknown option %d, skipping.", src->key); | |
128 | break; | |
129 | } | |
130 | } | |
131 | ||
132 | if (!conn) | |
133 | return NULL; | |
134 | if (!serialcomm) | |
135 | serialcomm = "9600/8n1"; | |
136 | if (force_detect && !*force_detect) | |
137 | force_detect = NULL; | |
138 | ||
139 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
140 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
141 | return NULL; | |
142 | ||
143 | /* | |
144 | * Prepare a receive buffer for the identification response that | |
145 | * is large enough to hold the longest known model name, and an | |
146 | * optional serial number. Communicate the identification request. | |
147 | */ | |
148 | len = 0; | |
149 | for (i = 0; models[i].id; i++) { | |
150 | if (len < strlen(models[i].id)) | |
151 | len = strlen(models[i].id); | |
152 | } | |
153 | len += strlen(serno_prefix) + 12; | |
154 | if (len > sizeof(reply) - 1) | |
155 | len = sizeof(reply) - 1; | |
156 | sr_dbg("Want max %zu bytes.", len); | |
157 | ||
158 | ret = korad_kaxxxxp_send_cmd(serial, "*IDN?"); | |
159 | if (ret < 0) | |
160 | return NULL; | |
161 | ||
162 | ret = korad_kaxxxxp_read_chars(serial, len, reply); | |
163 | if (ret < 0) | |
164 | return NULL; | |
165 | sr_dbg("Received: %d, %s", ret, reply); | |
166 | ||
167 | /* | |
168 | * Isolate the optional serial number at the response's end. | |
169 | * Lookup the response's model ID in the list of known models. | |
170 | */ | |
171 | serno = g_strrstr(reply, serno_prefix); | |
172 | if (serno) { | |
173 | *serno = '\0'; | |
174 | serno += strlen(serno_prefix); | |
175 | } | |
176 | ||
177 | model_id = -1; | |
178 | for (i = 0; models[i].id; i++) { | |
179 | if (g_strcmp0(models[i].id, reply) != 0) | |
180 | continue; | |
181 | model_id = i; | |
182 | break; | |
183 | } | |
184 | if (model_id < 0 && force_detect) { | |
185 | sr_warn("Found model ID '%s' is unknown, trying '%s' spec.", | |
186 | reply, force_detect); | |
187 | for (i = 0; models[i].id; i++) { | |
188 | if (strcmp(models[i].id, force_detect) != 0) | |
189 | continue; | |
190 | sr_info("Found replacement, using it instead."); | |
191 | model_id = i; | |
192 | break; | |
193 | } | |
194 | } | |
195 | if (model_id < 0) { | |
196 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
197 | return NULL; | |
198 | } | |
199 | sr_dbg("Found: %s %s (idx %d, ID '%s').", models[model_id].vendor, | |
200 | models[model_id].name, model_id, models[model_id].id); | |
201 | ||
202 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
203 | sdi->status = SR_ST_INACTIVE; | |
204 | sdi->vendor = g_strdup(models[model_id].vendor); | |
205 | sdi->model = g_strdup(models[model_id].name); | |
206 | if (serno) | |
207 | sdi->serial_num = g_strdup(serno); | |
208 | sdi->inst_type = SR_INST_SERIAL; | |
209 | sdi->conn = serial; | |
210 | sdi->connection_id = g_strdup(conn); | |
211 | ||
212 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V"); | |
213 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I"); | |
214 | ||
215 | devc = g_malloc0(sizeof(struct dev_context)); | |
216 | sr_sw_limits_init(&devc->limits); | |
217 | g_mutex_init(&devc->rw_mutex); | |
218 | devc->model = &models[model_id]; | |
219 | devc->req_sent_at = 0; | |
220 | devc->cc_mode_1_changed = FALSE; | |
221 | devc->cc_mode_2_changed = FALSE; | |
222 | devc->output_enabled_changed = FALSE; | |
223 | devc->ocp_enabled_changed = FALSE; | |
224 | devc->ovp_enabled_changed = FALSE; | |
225 | sdi->priv = devc; | |
226 | ||
227 | /* Get current status of device. */ | |
228 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) | |
229 | goto exit_err; | |
230 | ||
231 | serial_close(serial); | |
232 | ||
233 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
234 | ||
235 | exit_err: | |
236 | sr_dev_inst_free(sdi); | |
237 | g_free(devc); | |
238 | sr_dbg("Scan failed."); | |
239 | ||
240 | return NULL; | |
241 | } | |
242 | ||
243 | static int config_get(uint32_t key, GVariant **data, | |
244 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
245 | { | |
246 | struct dev_context *devc; | |
247 | ||
248 | (void)cg; | |
249 | ||
250 | if (!sdi || !data) | |
251 | return SR_ERR_ARG; | |
252 | ||
253 | devc = sdi->priv; | |
254 | ||
255 | switch (key) { | |
256 | case SR_CONF_LIMIT_SAMPLES: | |
257 | case SR_CONF_LIMIT_MSEC: | |
258 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
259 | case SR_CONF_CONN: | |
260 | *data = g_variant_new_string(sdi->connection_id); | |
261 | break; | |
262 | case SR_CONF_VOLTAGE: | |
263 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE, devc); | |
264 | *data = g_variant_new_double(devc->voltage); | |
265 | break; | |
266 | case SR_CONF_VOLTAGE_TARGET: | |
267 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc); | |
268 | *data = g_variant_new_double(devc->voltage_target); | |
269 | break; | |
270 | case SR_CONF_CURRENT: | |
271 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT, devc); | |
272 | *data = g_variant_new_double(devc->current); | |
273 | break; | |
274 | case SR_CONF_CURRENT_LIMIT: | |
275 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc); | |
276 | *data = g_variant_new_double(devc->current_limit); | |
277 | break; | |
278 | case SR_CONF_ENABLED: | |
279 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OUTPUT, devc); | |
280 | *data = g_variant_new_boolean(devc->output_enabled); | |
281 | break; | |
282 | case SR_CONF_REGULATION: | |
283 | /* Dual channel not supported. */ | |
284 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_STATUS, devc); | |
285 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
286 | break; | |
287 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
288 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OCP, devc); | |
289 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
290 | break; | |
291 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
292 | korad_kaxxxxp_get_value(sdi->conn, KAXXXXP_OVP, devc); | |
293 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
294 | break; | |
295 | default: | |
296 | return SR_ERR_NA; | |
297 | } | |
298 | ||
299 | return SR_OK; | |
300 | } | |
301 | ||
302 | static int config_set(uint32_t key, GVariant *data, | |
303 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
304 | { | |
305 | struct dev_context *devc; | |
306 | double dval; | |
307 | gboolean bval; | |
308 | ||
309 | (void)cg; | |
310 | ||
311 | devc = sdi->priv; | |
312 | ||
313 | switch (key) { | |
314 | case SR_CONF_LIMIT_MSEC: | |
315 | case SR_CONF_LIMIT_SAMPLES: | |
316 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
317 | case SR_CONF_VOLTAGE_TARGET: | |
318 | dval = g_variant_get_double(data); | |
319 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
320 | return SR_ERR_ARG; | |
321 | devc->set_voltage_target = dval; | |
322 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_VOLTAGE_TARGET, devc) < 0) | |
323 | return SR_ERR; | |
324 | break; | |
325 | case SR_CONF_CURRENT_LIMIT: | |
326 | dval = g_variant_get_double(data); | |
327 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
328 | return SR_ERR_ARG; | |
329 | devc->set_current_limit = dval; | |
330 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_CURRENT_LIMIT, devc) < 0) | |
331 | return SR_ERR; | |
332 | break; | |
333 | case SR_CONF_ENABLED: | |
334 | bval = g_variant_get_boolean(data); | |
335 | /* Set always so it is possible turn off with sigrok-cli. */ | |
336 | devc->set_output_enabled = bval; | |
337 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OUTPUT, devc) < 0) | |
338 | return SR_ERR; | |
339 | break; | |
340 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
341 | bval = g_variant_get_boolean(data); | |
342 | devc->set_ocp_enabled = bval; | |
343 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OCP, devc) < 0) | |
344 | return SR_ERR; | |
345 | break; | |
346 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
347 | bval = g_variant_get_boolean(data); | |
348 | devc->set_ovp_enabled = bval; | |
349 | if (korad_kaxxxxp_set_value(sdi->conn, KAXXXXP_OVP, devc) < 0) | |
350 | return SR_ERR; | |
351 | break; | |
352 | default: | |
353 | return SR_ERR_NA; | |
354 | } | |
355 | ||
356 | return SR_OK; | |
357 | } | |
358 | ||
359 | static int config_list(uint32_t key, GVariant **data, | |
360 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
361 | { | |
362 | struct dev_context *devc; | |
363 | ||
364 | devc = (sdi) ? sdi->priv : NULL; | |
365 | ||
366 | switch (key) { | |
367 | case SR_CONF_SCAN_OPTIONS: | |
368 | case SR_CONF_DEVICE_OPTIONS: | |
369 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
370 | case SR_CONF_VOLTAGE_TARGET: | |
371 | if (!devc || !devc->model) | |
372 | return SR_ERR_ARG; | |
373 | *data = std_gvar_min_max_step_array(devc->model->voltage); | |
374 | break; | |
375 | case SR_CONF_CURRENT_LIMIT: | |
376 | if (!devc || !devc->model) | |
377 | return SR_ERR_ARG; | |
378 | *data = std_gvar_min_max_step_array(devc->model->current); | |
379 | break; | |
380 | default: | |
381 | return SR_ERR_NA; | |
382 | } | |
383 | ||
384 | return SR_OK; | |
385 | } | |
386 | ||
387 | static int dev_close(struct sr_dev_inst *sdi) | |
388 | { | |
389 | struct dev_context *devc; | |
390 | ||
391 | devc = (sdi) ? sdi->priv : NULL; | |
392 | if (devc) | |
393 | g_mutex_clear(&devc->rw_mutex); | |
394 | ||
395 | return std_serial_dev_close(sdi); | |
396 | } | |
397 | ||
398 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
399 | { | |
400 | struct dev_context *devc; | |
401 | struct sr_serial_dev_inst *serial; | |
402 | ||
403 | devc = sdi->priv; | |
404 | ||
405 | sr_sw_limits_acquisition_start(&devc->limits); | |
406 | std_session_send_df_header(sdi); | |
407 | ||
408 | devc->req_sent_at = 0; | |
409 | serial = sdi->conn; | |
410 | serial_source_add(sdi->session, serial, G_IO_IN, | |
411 | KAXXXXP_POLL_INTERVAL_MS, | |
412 | korad_kaxxxxp_receive_data, (void *)sdi); | |
413 | ||
414 | return SR_OK; | |
415 | } | |
416 | ||
417 | static struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
418 | .name = "korad-kaxxxxp", | |
419 | .longname = "Korad KAxxxxP", | |
420 | .api_version = 1, | |
421 | .init = std_init, | |
422 | .cleanup = std_cleanup, | |
423 | .scan = scan, | |
424 | .dev_list = std_dev_list, | |
425 | .dev_clear = std_dev_clear, | |
426 | .config_get = config_get, | |
427 | .config_set = config_set, | |
428 | .config_list = config_list, | |
429 | .dev_open = std_serial_dev_open, | |
430 | .dev_close = dev_close, | |
431 | .dev_acquisition_start = dev_acquisition_start, | |
432 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
433 | .context = NULL, | |
434 | }; | |
435 | SR_REGISTER_DEV_DRIVER(korad_kaxxxxp_driver_info); |