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