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