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