]>
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, | |
46 | }; | |
47 | ||
48 | static const struct korad_kdxxxxp_model models[] = { | |
49 | /* Device enum, vendor, model, ID reply, channels, voltage, current */ | |
50 | {VELLEMAN_LABPS_3005D, "Velleman", "LABPS3005D", | |
51 | "VELLEMANLABPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
52 | {0, NULL, NULL, NULL, 0, {0, 0, 0}, {0, 0, 0}} | |
53 | }; | |
54 | ||
e75ee7de HV |
55 | SR_PRIV struct sr_dev_driver korad_kdxxxxp_driver_info; |
56 | ||
57 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
58 | { | |
59 | return std_init(sr_ctx, di, LOG_PREFIX); | |
60 | } | |
61 | ||
62 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
63 | { | |
64 | struct drv_context *drvc; | |
d7083042 HV |
65 | struct dev_context *devc; |
66 | GSList *devices, *l; | |
67 | struct sr_dev_inst *sdi; | |
68 | struct sr_config *src; | |
69 | const char *conn, *serialcomm; | |
70 | struct sr_serial_dev_inst *serial; | |
71 | char reply[50]; | |
72 | int i, model_id; | |
73 | unsigned int len; | |
e75ee7de HV |
74 | |
75 | devices = NULL; | |
d7083042 HV |
76 | conn = NULL; |
77 | serialcomm = NULL; | |
e75ee7de HV |
78 | drvc = di->context; |
79 | drvc->instances = NULL; | |
80 | ||
d7083042 HV |
81 | for (l = options; l; l = l->next) { |
82 | src = l->data; | |
83 | switch (src->key) { | |
84 | case SR_CONF_CONN: | |
85 | conn = g_variant_get_string(src->data, NULL); | |
86 | break; | |
87 | case SR_CONF_SERIALCOMM: | |
88 | serialcomm = g_variant_get_string(src->data, NULL); | |
89 | break; | |
90 | default: | |
91 | sr_err("Unknown option %d, skipping.", src->key); | |
92 | break; | |
93 | } | |
94 | } | |
95 | ||
96 | if (!conn) | |
97 | return NULL; | |
98 | if (!serialcomm) | |
99 | serialcomm = "9600/8n1"; | |
100 | ||
101 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
102 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
103 | return NULL; | |
104 | ||
105 | serial_flush(serial); | |
106 | ||
107 | /* Get the device model. */ | |
108 | len = 0; | |
109 | for (i = 0; models[i].id; i++) { | |
110 | if (strlen(models[i].id) > len) | |
111 | len = strlen(models[i].id); | |
112 | } | |
113 | memset(&reply, 0, sizeof(reply)); | |
114 | sr_dbg("Want max %d bytes.", len); | |
115 | if ((korad_kdxxxxp_send_cmd(serial, "*IDN?") < 0)) | |
116 | return NULL; | |
117 | ||
118 | /* i is used here for debug purposes only. */ | |
119 | if ((i = korad_kdxxxxp_read_chars(serial, len, reply)) < 0) | |
120 | return NULL; | |
121 | sr_dbg("Received: %d, %s", i, reply); | |
122 | model_id = -1; | |
123 | for (i = 0; models[i].id; i++) { | |
124 | if (!strcmp(models[i].id, reply)) | |
125 | model_id = i; | |
126 | } | |
127 | if (model_id < 0) { | |
128 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
129 | return NULL; | |
130 | } | |
131 | sr_dbg("Found: %s %s", models[model_id].vendor, models[model_id].name); | |
132 | ||
133 | /* Init device instance, etc. */ | |
134 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
135 | sdi->status = SR_ST_INACTIVE; | |
136 | sdi->vendor = g_strdup(models[model_id].vendor); | |
137 | sdi->model = g_strdup(models[model_id].name); | |
138 | sdi->inst_type = SR_INST_SERIAL; | |
139 | sdi->conn = serial; | |
140 | sdi->driver = di; | |
141 | ||
142 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1"); | |
143 | ||
144 | devc = g_malloc0(sizeof(struct dev_context)); | |
145 | devc->model = &models[model_id]; | |
146 | devc->reply[5] = 0; | |
147 | devc->req_sent_at = 0; | |
148 | sdi->priv = devc; | |
149 | ||
150 | /* Get current status of device. */ | |
151 | if (korad_kdxxxxp_get_all_values(serial, devc) < 0) | |
152 | goto exit_err; | |
153 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
154 | devices = g_slist_append(devices, sdi); | |
155 | ||
156 | serial_close(serial); | |
157 | if (!devices) | |
158 | sr_serial_dev_inst_free(serial); | |
e75ee7de HV |
159 | |
160 | return devices; | |
d7083042 HV |
161 | |
162 | exit_err: | |
163 | sr_dev_inst_free(sdi); | |
164 | g_free(devc); | |
165 | sr_dbg("Scan failed."); | |
166 | ||
167 | return NULL; | |
e75ee7de HV |
168 | } |
169 | ||
170 | static GSList *dev_list(const struct sr_dev_driver *di) | |
171 | { | |
172 | return ((struct drv_context *)(di->context))->instances; | |
173 | } | |
174 | ||
175 | static int dev_clear(const struct sr_dev_driver *di) | |
176 | { | |
177 | return std_dev_clear(di, NULL); | |
178 | } | |
179 | ||
e75ee7de HV |
180 | static int cleanup(const struct sr_dev_driver *di) |
181 | { | |
182 | dev_clear(di); | |
e75ee7de HV |
183 | return SR_OK; |
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 | { | |
d7083042 | 189 | struct dev_context *devc; |
e75ee7de | 190 | |
e75ee7de HV |
191 | (void)cg; |
192 | ||
d7083042 HV |
193 | if (!sdi || !data) |
194 | return SR_ERR_ARG; | |
195 | ||
196 | devc = sdi->priv; | |
197 | ||
e75ee7de | 198 | switch (key) { |
d7083042 HV |
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; | |
e75ee7de HV |
220 | default: |
221 | return SR_ERR_NA; | |
222 | } | |
223 | ||
d7083042 | 224 | return SR_OK; |
e75ee7de HV |
225 | } |
226 | ||
227 | static int config_set(uint32_t key, GVariant *data, | |
228 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
229 | { | |
d7083042 HV |
230 | struct dev_context *devc; |
231 | double dval; | |
232 | gboolean bval; | |
e75ee7de | 233 | |
e75ee7de HV |
234 | (void)cg; |
235 | ||
236 | if (sdi->status != SR_ST_ACTIVE) | |
237 | return SR_ERR_DEV_CLOSED; | |
238 | ||
d7083042 HV |
239 | devc = sdi->priv; |
240 | ||
e75ee7de | 241 | switch (key) { |
d7083042 HV |
242 | case SR_CONF_LIMIT_MSEC: |
243 | if (g_variant_get_uint64(data) == 0) | |
244 | return SR_ERR_ARG; | |
245 | devc->limit_msec = g_variant_get_uint64(data); | |
246 | break; | |
247 | case SR_CONF_LIMIT_SAMPLES: | |
248 | if (g_variant_get_uint64(data) == 0) | |
249 | return SR_ERR_ARG; | |
250 | devc->limit_samples = g_variant_get_uint64(data); | |
251 | break; | |
252 | case SR_CONF_VOLTAGE_TARGET: | |
253 | dval = g_variant_get_double(data); | |
254 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
255 | return SR_ERR_ARG; | |
256 | devc->voltage_max = dval; | |
257 | devc->target = KDXXXXP_VOLTAGE_MAX; | |
258 | if (korad_kdxxxxp_set_value(sdi->conn, devc) < 0) | |
259 | return SR_ERR; | |
260 | break; | |
261 | case SR_CONF_CURRENT_LIMIT: | |
262 | dval = g_variant_get_double(data); | |
263 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
264 | return SR_ERR_ARG; | |
265 | devc->current_max = dval; | |
266 | devc->target = KDXXXXP_CURRENT_MAX; | |
267 | if (korad_kdxxxxp_set_value(sdi->conn, devc) < 0) | |
268 | return SR_ERR; | |
269 | break; | |
270 | case SR_CONF_ENABLED: | |
271 | bval = g_variant_get_boolean(data); | |
272 | /* Set always so it is possible turn off with sigrok-cli. */ | |
273 | devc->output_enabled = bval; | |
274 | devc->target = KDXXXXP_OUTPUT; | |
275 | if (korad_kdxxxxp_set_value(sdi->conn, devc) < 0) | |
276 | return SR_ERR; | |
277 | break; | |
e75ee7de | 278 | default: |
d7083042 | 279 | return SR_ERR_NA; |
e75ee7de HV |
280 | } |
281 | ||
d7083042 | 282 | return SR_OK; |
e75ee7de HV |
283 | } |
284 | ||
285 | static int config_list(uint32_t key, GVariant **data, | |
286 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
287 | { | |
e75ee7de | 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 | ||
349 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
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 HV |
357 | devc = sdi->priv; |
358 | devc->cb_data = cb_data; | |
359 | ||
360 | /* Send header packet to the session bus. */ | |
361 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
362 | ||
363 | devc->starttime = g_get_monotonic_time(); | |
364 | devc->num_samples = 0; | |
365 | devc->reply_pending = FALSE; | |
366 | devc->req_sent_at = 0; | |
367 | serial = sdi->conn; | |
368 | serial_source_add(sdi->session, serial, G_IO_IN, | |
369 | KDXXXXP_POLL_INTERVAL_MS, | |
370 | korad_kdxxxxp_receive_data, (void *)sdi); | |
e75ee7de HV |
371 | |
372 | return SR_OK; | |
373 | } | |
374 | ||
375 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
376 | { | |
e75ee7de HV |
377 | if (sdi->status != SR_ST_ACTIVE) |
378 | return SR_ERR_DEV_CLOSED; | |
379 | ||
d7083042 HV |
380 | return std_serial_dev_acquisition_stop(sdi, cb_data, |
381 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
e75ee7de HV |
382 | } |
383 | ||
384 | SR_PRIV struct sr_dev_driver korad_kdxxxxp_driver_info = { | |
385 | .name = "korad-kdxxxxp", | |
386 | .longname = "Korad KDxxxxP", | |
387 | .api_version = 1, | |
388 | .init = init, | |
389 | .cleanup = cleanup, | |
390 | .scan = scan, | |
391 | .dev_list = dev_list, | |
392 | .dev_clear = dev_clear, | |
393 | .config_get = config_get, | |
394 | .config_set = config_set, | |
395 | .config_list = config_list, | |
d7083042 HV |
396 | .dev_open = std_serial_dev_open, |
397 | .dev_close = std_serial_dev_close, | |
e75ee7de HV |
398 | .dev_acquisition_start = dev_acquisition_start, |
399 | .dev_acquisition_stop = dev_acquisition_stop, | |
400 | .context = NULL, | |
401 | }; |