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