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