]>
Commit | Line | Data |
---|---|---|
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 | ||
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, | |
46 | SR_CONF_REGULATION | SR_CONF_GET, | |
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, | |
49 | }; | |
50 | ||
51 | static const struct korad_kaxxxxp_model models[] = { | |
52 | /* Device enum, vendor, model, ID reply, channels, voltage, current */ | |
53 | {VELLEMAN_PS3005D, "Velleman", "PS3005D", | |
54 | "VELLEMANPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
55 | {VELLEMAN_LABPS3005D, "Velleman", "LABPS3005D", | |
56 | "VELLEMANLABPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
57 | {KORAD_KA3005P, "Korad", "KA3005P", | |
58 | "KORADKA3005PV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
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}}, | |
62 | ALL_ZERO | |
63 | }; | |
64 | ||
65 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info; | |
66 | ||
67 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
68 | { | |
69 | return std_init(sr_ctx, di, LOG_PREFIX); | |
70 | } | |
71 | ||
72 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
73 | { | |
74 | struct drv_context *drvc; | |
75 | struct dev_context *devc; | |
76 | GSList *devices, *l; | |
77 | struct sr_dev_inst *sdi; | |
78 | struct sr_config *src; | |
79 | const char *conn, *serialcomm; | |
80 | struct sr_serial_dev_inst *serial; | |
81 | char reply[50]; | |
82 | int i, model_id; | |
83 | unsigned int len; | |
84 | ||
85 | devices = NULL; | |
86 | conn = NULL; | |
87 | serialcomm = NULL; | |
88 | drvc = di->context; | |
89 | drvc->instances = NULL; | |
90 | ||
91 | for (l = options; l; l = l->next) { | |
92 | src = l->data; | |
93 | switch (src->key) { | |
94 | case SR_CONF_CONN: | |
95 | conn = g_variant_get_string(src->data, NULL); | |
96 | break; | |
97 | case SR_CONF_SERIALCOMM: | |
98 | serialcomm = g_variant_get_string(src->data, NULL); | |
99 | break; | |
100 | default: | |
101 | sr_err("Unknown option %d, skipping.", src->key); | |
102 | break; | |
103 | } | |
104 | } | |
105 | ||
106 | if (!conn) | |
107 | return NULL; | |
108 | if (!serialcomm) | |
109 | serialcomm = "9600/8n1"; | |
110 | ||
111 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
112 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
113 | return NULL; | |
114 | ||
115 | serial_flush(serial); | |
116 | ||
117 | /* Get the device model. */ | |
118 | len = 0; | |
119 | for (i = 0; models[i].id; i++) { | |
120 | if (strlen(models[i].id) > len) | |
121 | len = strlen(models[i].id); | |
122 | } | |
123 | memset(&reply, 0, sizeof(reply)); | |
124 | sr_dbg("Want max %d bytes.", len); | |
125 | if ((korad_kaxxxxp_send_cmd(serial, "*IDN?") < 0)) | |
126 | return NULL; | |
127 | ||
128 | /* i is used here for debug purposes only. */ | |
129 | if ((i = korad_kaxxxxp_read_chars(serial, len, reply)) < 0) | |
130 | return NULL; | |
131 | sr_dbg("Received: %d, %s", i, reply); | |
132 | model_id = -1; | |
133 | for (i = 0; models[i].id; i++) { | |
134 | if (!strcmp(models[i].id, reply)) | |
135 | model_id = i; | |
136 | } | |
137 | if (model_id < 0) { | |
138 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
139 | return NULL; | |
140 | } | |
141 | sr_dbg("Found: %s %s (idx %d, ID '%s').", models[model_id].vendor, | |
142 | models[model_id].name, model_id, models[model_id].id); | |
143 | ||
144 | /* Init device instance, etc. */ | |
145 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
146 | sdi->status = SR_ST_INACTIVE; | |
147 | sdi->vendor = g_strdup(models[model_id].vendor); | |
148 | sdi->model = g_strdup(models[model_id].name); | |
149 | sdi->inst_type = SR_INST_SERIAL; | |
150 | sdi->conn = serial; | |
151 | sdi->driver = di; | |
152 | ||
153 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1"); | |
154 | ||
155 | devc = g_malloc0(sizeof(struct dev_context)); | |
156 | devc->model = &models[model_id]; | |
157 | devc->reply[5] = 0; | |
158 | devc->req_sent_at = 0; | |
159 | sdi->priv = devc; | |
160 | ||
161 | /* Get current status of device. */ | |
162 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) | |
163 | goto exit_err; | |
164 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
165 | devices = g_slist_append(devices, sdi); | |
166 | ||
167 | serial_close(serial); | |
168 | if (!devices) | |
169 | sr_serial_dev_inst_free(serial); | |
170 | ||
171 | return devices; | |
172 | ||
173 | exit_err: | |
174 | sr_dev_inst_free(sdi); | |
175 | g_free(devc); | |
176 | sr_dbg("Scan failed."); | |
177 | ||
178 | return NULL; | |
179 | } | |
180 | ||
181 | static GSList *dev_list(const struct sr_dev_driver *di) | |
182 | { | |
183 | return ((struct drv_context *)(di->context))->instances; | |
184 | } | |
185 | ||
186 | static int dev_clear(const struct sr_dev_driver *di) | |
187 | { | |
188 | return std_dev_clear(di, NULL); | |
189 | } | |
190 | ||
191 | static int cleanup(const struct sr_dev_driver *di) | |
192 | { | |
193 | dev_clear(di); | |
194 | return SR_OK; | |
195 | } | |
196 | ||
197 | static int config_get(uint32_t key, GVariant **data, | |
198 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
199 | { | |
200 | struct dev_context *devc; | |
201 | ||
202 | (void)cg; | |
203 | ||
204 | if (!sdi || !data) | |
205 | return SR_ERR_ARG; | |
206 | ||
207 | devc = sdi->priv; | |
208 | ||
209 | switch (key) { | |
210 | case SR_CONF_LIMIT_SAMPLES: | |
211 | *data = g_variant_new_uint64(devc->limit_samples); | |
212 | break; | |
213 | case SR_CONF_LIMIT_MSEC: | |
214 | *data = g_variant_new_uint64(devc->limit_msec); | |
215 | break; | |
216 | case SR_CONF_VOLTAGE: | |
217 | *data = g_variant_new_double(devc->voltage); | |
218 | break; | |
219 | case SR_CONF_VOLTAGE_TARGET: | |
220 | *data = g_variant_new_double(devc->voltage_max); | |
221 | break; | |
222 | case SR_CONF_CURRENT: | |
223 | *data = g_variant_new_double(devc->current); | |
224 | break; | |
225 | case SR_CONF_CURRENT_LIMIT: | |
226 | *data = g_variant_new_double(devc->current_max); | |
227 | break; | |
228 | case SR_CONF_ENABLED: | |
229 | *data = g_variant_new_boolean(devc->output_enabled); | |
230 | break; | |
231 | case SR_CONF_REGULATION: | |
232 | /* Dual channel not supported. */ | |
233 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
234 | break; | |
235 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
236 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
237 | break; | |
238 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
239 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
240 | break; | |
241 | default: | |
242 | return SR_ERR_NA; | |
243 | } | |
244 | ||
245 | return SR_OK; | |
246 | } | |
247 | ||
248 | static int config_set(uint32_t key, GVariant *data, | |
249 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
250 | { | |
251 | struct dev_context *devc; | |
252 | double dval; | |
253 | gboolean bval; | |
254 | ||
255 | (void)cg; | |
256 | ||
257 | if (sdi->status != SR_ST_ACTIVE) | |
258 | return SR_ERR_DEV_CLOSED; | |
259 | ||
260 | devc = sdi->priv; | |
261 | ||
262 | switch (key) { | |
263 | case SR_CONF_LIMIT_MSEC: | |
264 | if (g_variant_get_uint64(data) == 0) | |
265 | return SR_ERR_ARG; | |
266 | devc->limit_msec = g_variant_get_uint64(data); | |
267 | break; | |
268 | case SR_CONF_LIMIT_SAMPLES: | |
269 | if (g_variant_get_uint64(data) == 0) | |
270 | return SR_ERR_ARG; | |
271 | devc->limit_samples = g_variant_get_uint64(data); | |
272 | break; | |
273 | case SR_CONF_VOLTAGE_TARGET: | |
274 | dval = g_variant_get_double(data); | |
275 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
276 | return SR_ERR_ARG; | |
277 | devc->voltage_max = dval; | |
278 | devc->target = KAXXXXP_VOLTAGE_MAX; | |
279 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
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; | |
286 | devc->current_max = dval; | |
287 | devc->target = KAXXXXP_CURRENT_MAX; | |
288 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
289 | return SR_ERR; | |
290 | break; | |
291 | case SR_CONF_ENABLED: | |
292 | bval = g_variant_get_boolean(data); | |
293 | /* Set always so it is possible turn off with sigrok-cli. */ | |
294 | devc->output_enabled = bval; | |
295 | devc->target = KAXXXXP_OUTPUT; | |
296 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
297 | return SR_ERR; | |
298 | break; | |
299 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
300 | bval = g_variant_get_boolean(data); | |
301 | devc->ocp_enabled = bval; | |
302 | devc->target = KAXXXXP_OCP; | |
303 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
304 | return SR_ERR; | |
305 | break; | |
306 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
307 | bval = g_variant_get_boolean(data); | |
308 | devc->ovp_enabled = bval; | |
309 | devc->target = KAXXXXP_OVP; | |
310 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
311 | return SR_ERR; | |
312 | break; | |
313 | default: | |
314 | return SR_ERR_NA; | |
315 | } | |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | static int config_list(uint32_t key, GVariant **data, | |
321 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
322 | { | |
323 | ||
324 | struct dev_context *devc; | |
325 | GVariant *gvar; | |
326 | GVariantBuilder gvb; | |
327 | double dval; | |
328 | int idx; | |
329 | ||
330 | (void)cg; | |
331 | ||
332 | /* Always available (with or without sdi). */ | |
333 | if (key == SR_CONF_SCAN_OPTIONS) { | |
334 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
335 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
336 | return SR_OK; | |
337 | } | |
338 | ||
339 | /* Return drvopts without sdi (and devopts with sdi, see below). */ | |
340 | if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
341 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
342 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
343 | return SR_OK; | |
344 | } | |
345 | ||
346 | /* Every other key needs an sdi. */ | |
347 | if (!sdi) | |
348 | return SR_ERR_ARG; | |
349 | ||
350 | devc = sdi->priv; | |
351 | ||
352 | switch (key) { | |
353 | case SR_CONF_DEVICE_OPTIONS: | |
354 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
355 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
356 | break; | |
357 | case SR_CONF_VOLTAGE_TARGET: | |
358 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
359 | /* Min, max, step. */ | |
360 | for (idx = 0; idx < 3; idx++) { | |
361 | dval = devc->model->voltage[idx]; | |
362 | gvar = g_variant_new_double(dval); | |
363 | g_variant_builder_add_value(&gvb, gvar); | |
364 | } | |
365 | *data = g_variant_builder_end(&gvb); | |
366 | break; | |
367 | case SR_CONF_CURRENT_LIMIT: | |
368 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
369 | /* Min, max, step. */ | |
370 | for (idx = 0; idx < 3; idx++) { | |
371 | dval = devc->model->current[idx]; | |
372 | gvar = g_variant_new_double(dval); | |
373 | g_variant_builder_add_value(&gvb, gvar); | |
374 | } | |
375 | *data = g_variant_builder_end(&gvb); | |
376 | break; | |
377 | default: | |
378 | return SR_ERR_NA; | |
379 | } | |
380 | ||
381 | return SR_OK; | |
382 | } | |
383 | ||
384 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
385 | { | |
386 | struct dev_context *devc; | |
387 | struct sr_serial_dev_inst *serial; | |
388 | ||
389 | if (sdi->status != SR_ST_ACTIVE) | |
390 | return SR_ERR_DEV_CLOSED; | |
391 | ||
392 | devc = sdi->priv; | |
393 | devc->cb_data = cb_data; | |
394 | ||
395 | /* Send header packet to the session bus. */ | |
396 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
397 | ||
398 | devc->starttime = g_get_monotonic_time(); | |
399 | devc->num_samples = 0; | |
400 | devc->reply_pending = FALSE; | |
401 | devc->req_sent_at = 0; | |
402 | serial = sdi->conn; | |
403 | serial_source_add(sdi->session, serial, G_IO_IN, | |
404 | KAXXXXP_POLL_INTERVAL_MS, | |
405 | korad_kaxxxxp_receive_data, (void *)sdi); | |
406 | ||
407 | return SR_OK; | |
408 | } | |
409 | ||
410 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
411 | { | |
412 | if (sdi->status != SR_ST_ACTIVE) | |
413 | return SR_ERR_DEV_CLOSED; | |
414 | ||
415 | return std_serial_dev_acquisition_stop(sdi, cb_data, | |
416 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
417 | } | |
418 | ||
419 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
420 | .name = "korad-kaxxxxp", | |
421 | .longname = "Korad KAxxxxP", | |
422 | .api_version = 1, | |
423 | .init = init, | |
424 | .cleanup = cleanup, | |
425 | .scan = scan, | |
426 | .dev_list = dev_list, | |
427 | .dev_clear = dev_clear, | |
428 | .config_get = config_get, | |
429 | .config_set = config_set, | |
430 | .config_list = config_list, | |
431 | .dev_open = std_serial_dev_open, | |
432 | .dev_close = std_serial_dev_close, | |
433 | .dev_acquisition_start = dev_acquisition_start, | |
434 | .dev_acquisition_stop = dev_acquisition_stop, | |
435 | .context = NULL, | |
436 | }; |