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