]>
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> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
d7083042 HV |
23 | static const uint32_t drvopts[] = { |
24 | /* Device class */ | |
25 | SR_CONF_POWER_SUPPLY, | |
26 | }; | |
27 | ||
28 | static const uint32_t scanopts[] = { | |
29 | SR_CONF_CONN, | |
30 | SR_CONF_SERIALCOMM, | |
31 | }; | |
32 | ||
33 | static const uint32_t devopts[] = { | |
34 | /* Device class */ | |
35 | SR_CONF_POWER_SUPPLY, | |
36 | /* Acquisition modes. */ | |
37 | SR_CONF_CONTINUOUS, | |
38 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
39 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
40 | /* Device configuration */ | |
41 | SR_CONF_VOLTAGE | SR_CONF_GET, | |
42 | SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
43 | SR_CONF_CURRENT | SR_CONF_GET, | |
44 | SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
45 | SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
b16d975a | 46 | SR_CONF_REGULATION | SR_CONF_GET, |
c40ed60f HV |
47 | SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, |
48 | SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
d7083042 HV |
49 | }; |
50 | ||
16fc7ee2 | 51 | static const struct korad_kaxxxxp_model models[] = { |
d7083042 | 52 | /* Device enum, vendor, model, ID reply, channels, voltage, current */ |
ae9ca5b1 UH |
53 | {VELLEMAN_PS3005D, "Velleman", "PS3005D", |
54 | "VELLEMANPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
55 | {VELLEMAN_LABPS3005D, "Velleman", "LABPS3005D", | |
d7083042 | 56 | "VELLEMANLABPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, |
bcf9384d | 57 | {KORAD_KA3005P, "Korad", "KA3005P", |
2c5bdf1b | 58 | "KORADKA3005PV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, |
a078d3ec UH |
59 | /* Sometimes the KA3005P has an extra 0x01 after the ID. */ |
60 | {KORAD_KA3005P_0X01, "Korad", "KA3005P", | |
61 | "KORADKA3005PV2.0\x01", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
9e9dba7b | 62 | ALL_ZERO |
d7083042 HV |
63 | }; |
64 | ||
e75ee7de HV |
65 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
66 | { | |
67 | struct drv_context *drvc; | |
d7083042 HV |
68 | struct dev_context *devc; |
69 | GSList *devices, *l; | |
70 | struct sr_dev_inst *sdi; | |
71 | struct sr_config *src; | |
72 | const char *conn, *serialcomm; | |
73 | struct sr_serial_dev_inst *serial; | |
74 | char reply[50]; | |
75 | int i, model_id; | |
76 | unsigned int len; | |
e75ee7de HV |
77 | |
78 | devices = NULL; | |
d7083042 HV |
79 | conn = NULL; |
80 | serialcomm = NULL; | |
e75ee7de HV |
81 | drvc = di->context; |
82 | drvc->instances = NULL; | |
83 | ||
d7083042 HV |
84 | for (l = options; l; l = l->next) { |
85 | src = l->data; | |
86 | switch (src->key) { | |
87 | case SR_CONF_CONN: | |
88 | conn = g_variant_get_string(src->data, NULL); | |
89 | break; | |
90 | case SR_CONF_SERIALCOMM: | |
91 | serialcomm = g_variant_get_string(src->data, NULL); | |
92 | break; | |
93 | default: | |
94 | sr_err("Unknown option %d, skipping.", src->key); | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
99 | if (!conn) | |
100 | return NULL; | |
101 | if (!serialcomm) | |
102 | serialcomm = "9600/8n1"; | |
103 | ||
104 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
105 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
106 | return NULL; | |
107 | ||
108 | serial_flush(serial); | |
109 | ||
110 | /* Get the device model. */ | |
111 | len = 0; | |
112 | for (i = 0; models[i].id; i++) { | |
113 | if (strlen(models[i].id) > len) | |
114 | len = strlen(models[i].id); | |
115 | } | |
116 | memset(&reply, 0, sizeof(reply)); | |
117 | sr_dbg("Want max %d bytes.", len); | |
16fc7ee2 | 118 | if ((korad_kaxxxxp_send_cmd(serial, "*IDN?") < 0)) |
d7083042 HV |
119 | return NULL; |
120 | ||
121 | /* i is used here for debug purposes only. */ | |
16fc7ee2 | 122 | if ((i = korad_kaxxxxp_read_chars(serial, len, reply)) < 0) |
d7083042 HV |
123 | return NULL; |
124 | sr_dbg("Received: %d, %s", i, reply); | |
125 | model_id = -1; | |
126 | for (i = 0; models[i].id; i++) { | |
127 | if (!strcmp(models[i].id, reply)) | |
128 | model_id = i; | |
129 | } | |
130 | if (model_id < 0) { | |
131 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
132 | return NULL; | |
133 | } | |
a078d3ec UH |
134 | sr_dbg("Found: %s %s (idx %d, ID '%s').", models[model_id].vendor, |
135 | models[model_id].name, model_id, models[model_id].id); | |
d7083042 HV |
136 | |
137 | /* Init device instance, etc. */ | |
138 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
139 | sdi->status = SR_ST_INACTIVE; | |
140 | sdi->vendor = g_strdup(models[model_id].vendor); | |
141 | sdi->model = g_strdup(models[model_id].name); | |
142 | sdi->inst_type = SR_INST_SERIAL; | |
143 | sdi->conn = serial; | |
144 | sdi->driver = di; | |
145 | ||
146 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1"); | |
147 | ||
148 | devc = g_malloc0(sizeof(struct dev_context)); | |
597deef9 | 149 | sr_sw_limits_init(&devc->limits); |
d7083042 HV |
150 | devc->model = &models[model_id]; |
151 | devc->reply[5] = 0; | |
152 | devc->req_sent_at = 0; | |
153 | sdi->priv = devc; | |
154 | ||
155 | /* Get current status of device. */ | |
16fc7ee2 | 156 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) |
d7083042 HV |
157 | goto exit_err; |
158 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
159 | devices = g_slist_append(devices, sdi); | |
160 | ||
161 | serial_close(serial); | |
162 | if (!devices) | |
163 | sr_serial_dev_inst_free(serial); | |
e75ee7de HV |
164 | |
165 | return devices; | |
d7083042 HV |
166 | |
167 | exit_err: | |
168 | sr_dev_inst_free(sdi); | |
169 | g_free(devc); | |
170 | sr_dbg("Scan failed."); | |
171 | ||
172 | return NULL; | |
e75ee7de HV |
173 | } |
174 | ||
e75ee7de HV |
175 | static int config_get(uint32_t key, GVariant **data, |
176 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
177 | { | |
d7083042 | 178 | struct dev_context *devc; |
e75ee7de | 179 | |
e75ee7de HV |
180 | (void)cg; |
181 | ||
d7083042 HV |
182 | if (!sdi || !data) |
183 | return SR_ERR_ARG; | |
184 | ||
185 | devc = sdi->priv; | |
186 | ||
e75ee7de | 187 | switch (key) { |
597deef9 | 188 | return sr_sw_limits_config_get(&devc->limits, key, data); |
d7083042 HV |
189 | case SR_CONF_VOLTAGE: |
190 | *data = g_variant_new_double(devc->voltage); | |
191 | break; | |
192 | case SR_CONF_VOLTAGE_TARGET: | |
193 | *data = g_variant_new_double(devc->voltage_max); | |
194 | break; | |
195 | case SR_CONF_CURRENT: | |
196 | *data = g_variant_new_double(devc->current); | |
197 | break; | |
198 | case SR_CONF_CURRENT_LIMIT: | |
199 | *data = g_variant_new_double(devc->current_max); | |
200 | break; | |
201 | case SR_CONF_ENABLED: | |
202 | *data = g_variant_new_boolean(devc->output_enabled); | |
203 | break; | |
b16d975a HV |
204 | case SR_CONF_REGULATION: |
205 | /* Dual channel not supported. */ | |
9e5366df | 206 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); |
b16d975a | 207 | break; |
c40ed60f | 208 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: |
9e5366df | 209 | *data = g_variant_new_boolean(devc->ocp_enabled); |
c40ed60f HV |
210 | break; |
211 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
9e5366df | 212 | *data = g_variant_new_boolean(devc->ovp_enabled); |
c40ed60f | 213 | break; |
e75ee7de HV |
214 | default: |
215 | return SR_ERR_NA; | |
216 | } | |
217 | ||
d7083042 | 218 | return SR_OK; |
e75ee7de HV |
219 | } |
220 | ||
221 | static int config_set(uint32_t key, GVariant *data, | |
222 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
223 | { | |
d7083042 HV |
224 | struct dev_context *devc; |
225 | double dval; | |
226 | gboolean bval; | |
e75ee7de | 227 | |
e75ee7de HV |
228 | (void)cg; |
229 | ||
230 | if (sdi->status != SR_ST_ACTIVE) | |
231 | return SR_ERR_DEV_CLOSED; | |
232 | ||
d7083042 HV |
233 | devc = sdi->priv; |
234 | ||
e75ee7de | 235 | switch (key) { |
d7083042 | 236 | case SR_CONF_LIMIT_MSEC: |
d7083042 | 237 | case SR_CONF_LIMIT_SAMPLES: |
597deef9 | 238 | return sr_sw_limits_config_set(&devc->limits, key, data); |
d7083042 HV |
239 | case SR_CONF_VOLTAGE_TARGET: |
240 | dval = g_variant_get_double(data); | |
241 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
242 | return SR_ERR_ARG; | |
243 | devc->voltage_max = dval; | |
16fc7ee2 UH |
244 | devc->target = KAXXXXP_VOLTAGE_MAX; |
245 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
d7083042 HV |
246 | return SR_ERR; |
247 | break; | |
248 | case SR_CONF_CURRENT_LIMIT: | |
249 | dval = g_variant_get_double(data); | |
250 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
251 | return SR_ERR_ARG; | |
252 | devc->current_max = dval; | |
16fc7ee2 UH |
253 | devc->target = KAXXXXP_CURRENT_MAX; |
254 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
d7083042 HV |
255 | return SR_ERR; |
256 | break; | |
257 | case SR_CONF_ENABLED: | |
258 | bval = g_variant_get_boolean(data); | |
259 | /* Set always so it is possible turn off with sigrok-cli. */ | |
260 | devc->output_enabled = bval; | |
16fc7ee2 UH |
261 | devc->target = KAXXXXP_OUTPUT; |
262 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
d7083042 | 263 | return SR_ERR; |
c40ed60f HV |
264 | break; |
265 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
266 | bval = g_variant_get_boolean(data); | |
9e5366df | 267 | devc->ocp_enabled = bval; |
16fc7ee2 UH |
268 | devc->target = KAXXXXP_OCP; |
269 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
c40ed60f HV |
270 | return SR_ERR; |
271 | break; | |
272 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
273 | bval = g_variant_get_boolean(data); | |
9e5366df | 274 | devc->ovp_enabled = bval; |
16fc7ee2 UH |
275 | devc->target = KAXXXXP_OVP; |
276 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
c40ed60f | 277 | return SR_ERR; |
d7083042 | 278 | break; |
e75ee7de | 279 | default: |
d7083042 | 280 | return SR_ERR_NA; |
e75ee7de HV |
281 | } |
282 | ||
d7083042 | 283 | return SR_OK; |
e75ee7de HV |
284 | } |
285 | ||
286 | static int config_list(uint32_t key, GVariant **data, | |
287 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
288 | { | |
d7083042 HV |
289 | struct dev_context *devc; |
290 | GVariant *gvar; | |
291 | GVariantBuilder gvb; | |
292 | double dval; | |
293 | int idx; | |
294 | ||
e75ee7de HV |
295 | (void)cg; |
296 | ||
d7083042 HV |
297 | /* Always available (with or without sdi). */ |
298 | if (key == SR_CONF_SCAN_OPTIONS) { | |
299 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
300 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
301 | return SR_OK; | |
302 | } | |
303 | ||
304 | /* Return drvopts without sdi (and devopts with sdi, see below). */ | |
305 | if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
306 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
307 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
308 | return SR_OK; | |
309 | } | |
310 | ||
311 | /* Every other key needs an sdi. */ | |
312 | if (!sdi) | |
313 | return SR_ERR_ARG; | |
314 | ||
315 | devc = sdi->priv; | |
316 | ||
e75ee7de | 317 | switch (key) { |
d7083042 HV |
318 | case SR_CONF_DEVICE_OPTIONS: |
319 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
320 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
321 | break; | |
322 | case SR_CONF_VOLTAGE_TARGET: | |
323 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
324 | /* Min, max, step. */ | |
325 | for (idx = 0; idx < 3; idx++) { | |
326 | dval = devc->model->voltage[idx]; | |
327 | gvar = g_variant_new_double(dval); | |
328 | g_variant_builder_add_value(&gvb, gvar); | |
329 | } | |
330 | *data = g_variant_builder_end(&gvb); | |
331 | break; | |
332 | case SR_CONF_CURRENT_LIMIT: | |
333 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
334 | /* Min, max, step. */ | |
335 | for (idx = 0; idx < 3; idx++) { | |
336 | dval = devc->model->current[idx]; | |
337 | gvar = g_variant_new_double(dval); | |
338 | g_variant_builder_add_value(&gvb, gvar); | |
339 | } | |
340 | *data = g_variant_builder_end(&gvb); | |
341 | break; | |
e75ee7de HV |
342 | default: |
343 | return SR_ERR_NA; | |
344 | } | |
345 | ||
d7083042 | 346 | return SR_OK; |
e75ee7de HV |
347 | } |
348 | ||
695dc859 | 349 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
e75ee7de | 350 | { |
d7083042 HV |
351 | struct dev_context *devc; |
352 | struct sr_serial_dev_inst *serial; | |
e75ee7de HV |
353 | |
354 | if (sdi->status != SR_ST_ACTIVE) | |
355 | return SR_ERR_DEV_CLOSED; | |
356 | ||
d7083042 | 357 | devc = sdi->priv; |
d7083042 | 358 | |
597deef9 | 359 | sr_sw_limits_acquisition_start(&devc->limits); |
695dc859 | 360 | std_session_send_df_header(sdi, LOG_PREFIX); |
d7083042 | 361 | |
d7083042 HV |
362 | devc->reply_pending = FALSE; |
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 | ||
695dc859 | 372 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
e75ee7de | 373 | { |
e75ee7de HV |
374 | if (sdi->status != SR_ST_ACTIVE) |
375 | return SR_ERR_DEV_CLOSED; | |
376 | ||
6525d819 | 377 | return std_serial_dev_acquisition_stop(sdi, |
d7083042 | 378 | std_serial_dev_close, sdi->conn, LOG_PREFIX); |
e75ee7de HV |
379 | } |
380 | ||
16fc7ee2 UH |
381 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info = { |
382 | .name = "korad-kaxxxxp", | |
383 | .longname = "Korad KAxxxxP", | |
e75ee7de | 384 | .api_version = 1, |
c2fdcc25 | 385 | .init = std_init, |
700d6b64 | 386 | .cleanup = std_cleanup, |
e75ee7de | 387 | .scan = scan, |
c01bf34c | 388 | .dev_list = std_dev_list, |
e75ee7de HV |
389 | .config_get = config_get, |
390 | .config_set = config_set, | |
391 | .config_list = config_list, | |
d7083042 HV |
392 | .dev_open = std_serial_dev_open, |
393 | .dev_close = std_serial_dev_close, | |
e75ee7de HV |
394 | .dev_acquisition_start = dev_acquisition_start, |
395 | .dev_acquisition_stop = dev_acquisition_stop, | |
396 | .context = NULL, | |
397 | }; |