]>
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, |
c40ed60f HV |
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, | |
d7083042 HV |
49 | }; |
50 | ||
16fc7ee2 | 51 | static const struct korad_kaxxxxp_model models[] = { |
d7083042 | 52 | /* Device enum, vendor, model, ID reply, channels, voltage, current */ |
ae9ca5b1 UH |
53 | {VELLEMAN_PS3005D, "Velleman", "PS3005D", |
54 | "VELLEMANPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
55 | {VELLEMAN_LABPS3005D, "Velleman", "LABPS3005D", | |
d7083042 | 56 | "VELLEMANLABPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, |
bcf9384d | 57 | {KORAD_KA3005P, "Korad", "KA3005P", |
2c5bdf1b | 58 | "KORADKA3005PV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, |
a078d3ec UH |
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}}, | |
9e9dba7b | 62 | ALL_ZERO |
d7083042 HV |
63 | }; |
64 | ||
16fc7ee2 | 65 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info; |
e75ee7de HV |
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; | |
d7083042 HV |
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; | |
e75ee7de HV |
84 | |
85 | devices = NULL; | |
d7083042 HV |
86 | conn = NULL; |
87 | serialcomm = NULL; | |
e75ee7de HV |
88 | drvc = di->context; |
89 | drvc->instances = NULL; | |
90 | ||
d7083042 HV |
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); | |
16fc7ee2 | 125 | if ((korad_kaxxxxp_send_cmd(serial, "*IDN?") < 0)) |
d7083042 HV |
126 | return NULL; |
127 | ||
128 | /* i is used here for debug purposes only. */ | |
16fc7ee2 | 129 | if ((i = korad_kaxxxxp_read_chars(serial, len, reply)) < 0) |
d7083042 HV |
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 | } | |
a078d3ec UH |
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); | |
d7083042 HV |
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)); | |
597deef9 | 156 | sr_sw_limits_init(&devc->limits); |
d7083042 HV |
157 | devc->model = &models[model_id]; |
158 | devc->reply[5] = 0; | |
159 | devc->req_sent_at = 0; | |
160 | sdi->priv = devc; | |
161 | ||
162 | /* Get current status of device. */ | |
16fc7ee2 | 163 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) |
d7083042 HV |
164 | goto exit_err; |
165 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
166 | devices = g_slist_append(devices, sdi); | |
167 | ||
168 | serial_close(serial); | |
169 | if (!devices) | |
170 | sr_serial_dev_inst_free(serial); | |
e75ee7de HV |
171 | |
172 | return devices; | |
d7083042 HV |
173 | |
174 | exit_err: | |
175 | sr_dev_inst_free(sdi); | |
176 | g_free(devc); | |
177 | sr_dbg("Scan failed."); | |
178 | ||
179 | return NULL; | |
e75ee7de HV |
180 | } |
181 | ||
e75ee7de HV |
182 | static int config_get(uint32_t key, GVariant **data, |
183 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
184 | { | |
d7083042 | 185 | struct dev_context *devc; |
e75ee7de | 186 | |
e75ee7de HV |
187 | (void)cg; |
188 | ||
d7083042 HV |
189 | if (!sdi || !data) |
190 | return SR_ERR_ARG; | |
191 | ||
192 | devc = sdi->priv; | |
193 | ||
e75ee7de | 194 | switch (key) { |
597deef9 | 195 | return sr_sw_limits_config_get(&devc->limits, key, data); |
d7083042 HV |
196 | case SR_CONF_VOLTAGE: |
197 | *data = g_variant_new_double(devc->voltage); | |
198 | break; | |
199 | case SR_CONF_VOLTAGE_TARGET: | |
200 | *data = g_variant_new_double(devc->voltage_max); | |
201 | break; | |
202 | case SR_CONF_CURRENT: | |
203 | *data = g_variant_new_double(devc->current); | |
204 | break; | |
205 | case SR_CONF_CURRENT_LIMIT: | |
206 | *data = g_variant_new_double(devc->current_max); | |
207 | break; | |
208 | case SR_CONF_ENABLED: | |
209 | *data = g_variant_new_boolean(devc->output_enabled); | |
210 | break; | |
b16d975a HV |
211 | case SR_CONF_REGULATION: |
212 | /* Dual channel not supported. */ | |
9e5366df | 213 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); |
b16d975a | 214 | break; |
c40ed60f | 215 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: |
9e5366df | 216 | *data = g_variant_new_boolean(devc->ocp_enabled); |
c40ed60f HV |
217 | break; |
218 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
9e5366df | 219 | *data = g_variant_new_boolean(devc->ovp_enabled); |
c40ed60f | 220 | break; |
e75ee7de HV |
221 | default: |
222 | return SR_ERR_NA; | |
223 | } | |
224 | ||
d7083042 | 225 | return SR_OK; |
e75ee7de HV |
226 | } |
227 | ||
228 | static int config_set(uint32_t key, GVariant *data, | |
229 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
230 | { | |
d7083042 HV |
231 | struct dev_context *devc; |
232 | double dval; | |
233 | gboolean bval; | |
e75ee7de | 234 | |
e75ee7de HV |
235 | (void)cg; |
236 | ||
237 | if (sdi->status != SR_ST_ACTIVE) | |
238 | return SR_ERR_DEV_CLOSED; | |
239 | ||
d7083042 HV |
240 | devc = sdi->priv; |
241 | ||
e75ee7de | 242 | switch (key) { |
d7083042 | 243 | case SR_CONF_LIMIT_MSEC: |
d7083042 | 244 | case SR_CONF_LIMIT_SAMPLES: |
597deef9 | 245 | return sr_sw_limits_config_set(&devc->limits, key, data); |
d7083042 HV |
246 | case SR_CONF_VOLTAGE_TARGET: |
247 | dval = g_variant_get_double(data); | |
248 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
249 | return SR_ERR_ARG; | |
250 | devc->voltage_max = dval; | |
16fc7ee2 UH |
251 | devc->target = KAXXXXP_VOLTAGE_MAX; |
252 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
d7083042 HV |
253 | return SR_ERR; |
254 | break; | |
255 | case SR_CONF_CURRENT_LIMIT: | |
256 | dval = g_variant_get_double(data); | |
257 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
258 | return SR_ERR_ARG; | |
259 | devc->current_max = dval; | |
16fc7ee2 UH |
260 | devc->target = KAXXXXP_CURRENT_MAX; |
261 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
d7083042 HV |
262 | return SR_ERR; |
263 | break; | |
264 | case SR_CONF_ENABLED: | |
265 | bval = g_variant_get_boolean(data); | |
266 | /* Set always so it is possible turn off with sigrok-cli. */ | |
267 | devc->output_enabled = bval; | |
16fc7ee2 UH |
268 | devc->target = KAXXXXP_OUTPUT; |
269 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
d7083042 | 270 | return SR_ERR; |
c40ed60f HV |
271 | break; |
272 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
273 | bval = g_variant_get_boolean(data); | |
9e5366df | 274 | devc->ocp_enabled = bval; |
16fc7ee2 UH |
275 | devc->target = KAXXXXP_OCP; |
276 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
c40ed60f HV |
277 | return SR_ERR; |
278 | break; | |
279 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
280 | bval = g_variant_get_boolean(data); | |
9e5366df | 281 | devc->ovp_enabled = bval; |
16fc7ee2 UH |
282 | devc->target = KAXXXXP_OVP; |
283 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
c40ed60f | 284 | return SR_ERR; |
d7083042 | 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 | ||
695dc859 | 357 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
e75ee7de | 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 | 365 | devc = sdi->priv; |
d7083042 | 366 | |
597deef9 | 367 | sr_sw_limits_acquisition_start(&devc->limits); |
695dc859 | 368 | std_session_send_df_header(sdi, LOG_PREFIX); |
d7083042 | 369 | |
d7083042 HV |
370 | devc->reply_pending = FALSE; |
371 | devc->req_sent_at = 0; | |
372 | serial = sdi->conn; | |
373 | serial_source_add(sdi->session, serial, G_IO_IN, | |
16fc7ee2 UH |
374 | KAXXXXP_POLL_INTERVAL_MS, |
375 | korad_kaxxxxp_receive_data, (void *)sdi); | |
e75ee7de HV |
376 | |
377 | return SR_OK; | |
378 | } | |
379 | ||
695dc859 | 380 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
e75ee7de | 381 | { |
e75ee7de HV |
382 | if (sdi->status != SR_ST_ACTIVE) |
383 | return SR_ERR_DEV_CLOSED; | |
384 | ||
6525d819 | 385 | return std_serial_dev_acquisition_stop(sdi, |
d7083042 | 386 | std_serial_dev_close, sdi->conn, LOG_PREFIX); |
e75ee7de HV |
387 | } |
388 | ||
16fc7ee2 UH |
389 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info = { |
390 | .name = "korad-kaxxxxp", | |
391 | .longname = "Korad KAxxxxP", | |
e75ee7de HV |
392 | .api_version = 1, |
393 | .init = init, | |
700d6b64 | 394 | .cleanup = std_cleanup, |
e75ee7de | 395 | .scan = scan, |
c01bf34c | 396 | .dev_list = std_dev_list, |
e75ee7de HV |
397 | .config_get = config_get, |
398 | .config_set = config_set, | |
399 | .config_list = config_list, | |
d7083042 HV |
400 | .dev_open = std_serial_dev_open, |
401 | .dev_close = std_serial_dev_close, | |
e75ee7de HV |
402 | .dev_acquisition_start = dev_acquisition_start, |
403 | .dev_acquisition_stop = dev_acquisition_stop, | |
404 | .context = NULL, | |
405 | }; |