]>
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 config_get(uint32_t key, GVariant **data, | |
187 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
188 | { | |
189 | struct dev_context *devc; | |
190 | ||
191 | (void)cg; | |
192 | ||
193 | if (!sdi || !data) | |
194 | return SR_ERR_ARG; | |
195 | ||
196 | devc = sdi->priv; | |
197 | ||
198 | switch (key) { | |
199 | case SR_CONF_LIMIT_SAMPLES: | |
200 | *data = g_variant_new_uint64(devc->limit_samples); | |
201 | break; | |
202 | case SR_CONF_LIMIT_MSEC: | |
203 | *data = g_variant_new_uint64(devc->limit_msec); | |
204 | break; | |
205 | case SR_CONF_VOLTAGE: | |
206 | *data = g_variant_new_double(devc->voltage); | |
207 | break; | |
208 | case SR_CONF_VOLTAGE_TARGET: | |
209 | *data = g_variant_new_double(devc->voltage_max); | |
210 | break; | |
211 | case SR_CONF_CURRENT: | |
212 | *data = g_variant_new_double(devc->current); | |
213 | break; | |
214 | case SR_CONF_CURRENT_LIMIT: | |
215 | *data = g_variant_new_double(devc->current_max); | |
216 | break; | |
217 | case SR_CONF_ENABLED: | |
218 | *data = g_variant_new_boolean(devc->output_enabled); | |
219 | break; | |
220 | case SR_CONF_REGULATION: | |
221 | /* Dual channel not supported. */ | |
222 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
223 | break; | |
224 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
225 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
226 | break; | |
227 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
228 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
229 | break; | |
230 | default: | |
231 | return SR_ERR_NA; | |
232 | } | |
233 | ||
234 | return SR_OK; | |
235 | } | |
236 | ||
237 | static int config_set(uint32_t key, GVariant *data, | |
238 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
239 | { | |
240 | struct dev_context *devc; | |
241 | double dval; | |
242 | gboolean bval; | |
243 | ||
244 | (void)cg; | |
245 | ||
246 | if (sdi->status != SR_ST_ACTIVE) | |
247 | return SR_ERR_DEV_CLOSED; | |
248 | ||
249 | devc = sdi->priv; | |
250 | ||
251 | switch (key) { | |
252 | case SR_CONF_LIMIT_MSEC: | |
253 | if (g_variant_get_uint64(data) == 0) | |
254 | return SR_ERR_ARG; | |
255 | devc->limit_msec = g_variant_get_uint64(data); | |
256 | break; | |
257 | case SR_CONF_LIMIT_SAMPLES: | |
258 | if (g_variant_get_uint64(data) == 0) | |
259 | return SR_ERR_ARG; | |
260 | devc->limit_samples = g_variant_get_uint64(data); | |
261 | break; | |
262 | case SR_CONF_VOLTAGE_TARGET: | |
263 | dval = g_variant_get_double(data); | |
264 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
265 | return SR_ERR_ARG; | |
266 | devc->voltage_max = dval; | |
267 | devc->target = KAXXXXP_VOLTAGE_MAX; | |
268 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
269 | return SR_ERR; | |
270 | break; | |
271 | case SR_CONF_CURRENT_LIMIT: | |
272 | dval = g_variant_get_double(data); | |
273 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
274 | return SR_ERR_ARG; | |
275 | devc->current_max = dval; | |
276 | devc->target = KAXXXXP_CURRENT_MAX; | |
277 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
278 | return SR_ERR; | |
279 | break; | |
280 | case SR_CONF_ENABLED: | |
281 | bval = g_variant_get_boolean(data); | |
282 | /* Set always so it is possible turn off with sigrok-cli. */ | |
283 | devc->output_enabled = bval; | |
284 | devc->target = KAXXXXP_OUTPUT; | |
285 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
286 | return SR_ERR; | |
287 | break; | |
288 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
289 | bval = g_variant_get_boolean(data); | |
290 | devc->ocp_enabled = bval; | |
291 | devc->target = KAXXXXP_OCP; | |
292 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
293 | return SR_ERR; | |
294 | break; | |
295 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
296 | bval = g_variant_get_boolean(data); | |
297 | devc->ovp_enabled = bval; | |
298 | devc->target = KAXXXXP_OVP; | |
299 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
300 | return SR_ERR; | |
301 | break; | |
302 | default: | |
303 | return SR_ERR_NA; | |
304 | } | |
305 | ||
306 | return SR_OK; | |
307 | } | |
308 | ||
309 | static int config_list(uint32_t key, GVariant **data, | |
310 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
311 | { | |
312 | ||
313 | struct dev_context *devc; | |
314 | GVariant *gvar; | |
315 | GVariantBuilder gvb; | |
316 | double dval; | |
317 | int idx; | |
318 | ||
319 | (void)cg; | |
320 | ||
321 | /* Always available (with or without sdi). */ | |
322 | if (key == SR_CONF_SCAN_OPTIONS) { | |
323 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
324 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
325 | return SR_OK; | |
326 | } | |
327 | ||
328 | /* Return drvopts without sdi (and devopts with sdi, see below). */ | |
329 | if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
330 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
331 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
332 | return SR_OK; | |
333 | } | |
334 | ||
335 | /* Every other key needs an sdi. */ | |
336 | if (!sdi) | |
337 | return SR_ERR_ARG; | |
338 | ||
339 | devc = sdi->priv; | |
340 | ||
341 | switch (key) { | |
342 | case SR_CONF_DEVICE_OPTIONS: | |
343 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
344 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
345 | break; | |
346 | case SR_CONF_VOLTAGE_TARGET: | |
347 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
348 | /* Min, max, step. */ | |
349 | for (idx = 0; idx < 3; idx++) { | |
350 | dval = devc->model->voltage[idx]; | |
351 | gvar = g_variant_new_double(dval); | |
352 | g_variant_builder_add_value(&gvb, gvar); | |
353 | } | |
354 | *data = g_variant_builder_end(&gvb); | |
355 | break; | |
356 | case SR_CONF_CURRENT_LIMIT: | |
357 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
358 | /* Min, max, step. */ | |
359 | for (idx = 0; idx < 3; idx++) { | |
360 | dval = devc->model->current[idx]; | |
361 | gvar = g_variant_new_double(dval); | |
362 | g_variant_builder_add_value(&gvb, gvar); | |
363 | } | |
364 | *data = g_variant_builder_end(&gvb); | |
365 | break; | |
366 | default: | |
367 | return SR_ERR_NA; | |
368 | } | |
369 | ||
370 | return SR_OK; | |
371 | } | |
372 | ||
373 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
374 | { | |
375 | struct dev_context *devc; | |
376 | struct sr_serial_dev_inst *serial; | |
377 | ||
378 | if (sdi->status != SR_ST_ACTIVE) | |
379 | return SR_ERR_DEV_CLOSED; | |
380 | ||
381 | devc = sdi->priv; | |
382 | ||
383 | std_session_send_df_header(sdi, LOG_PREFIX); | |
384 | ||
385 | devc->starttime = g_get_monotonic_time(); | |
386 | devc->num_samples = 0; | |
387 | devc->reply_pending = FALSE; | |
388 | devc->req_sent_at = 0; | |
389 | serial = sdi->conn; | |
390 | serial_source_add(sdi->session, serial, G_IO_IN, | |
391 | KAXXXXP_POLL_INTERVAL_MS, | |
392 | korad_kaxxxxp_receive_data, (void *)sdi); | |
393 | ||
394 | return SR_OK; | |
395 | } | |
396 | ||
397 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
398 | { | |
399 | if (sdi->status != SR_ST_ACTIVE) | |
400 | return SR_ERR_DEV_CLOSED; | |
401 | ||
402 | return std_serial_dev_acquisition_stop(sdi, | |
403 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
404 | } | |
405 | ||
406 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
407 | .name = "korad-kaxxxxp", | |
408 | .longname = "Korad KAxxxxP", | |
409 | .api_version = 1, | |
410 | .init = init, | |
411 | .cleanup = std_cleanup, | |
412 | .scan = scan, | |
413 | .dev_list = dev_list, | |
414 | .config_get = config_get, | |
415 | .config_set = config_set, | |
416 | .config_list = config_list, | |
417 | .dev_open = std_serial_dev_open, | |
418 | .dev_close = std_serial_dev_close, | |
419 | .dev_acquisition_start = dev_acquisition_start, | |
420 | .dev_acquisition_stop = dev_acquisition_stop, | |
421 | .context = NULL, | |
422 | }; |