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