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