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