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