]>
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 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
66 | { | |
67 | struct drv_context *drvc; | |
68 | struct dev_context *devc; | |
69 | GSList *devices, *l; | |
70 | struct sr_dev_inst *sdi; | |
71 | struct sr_config *src; | |
72 | const char *conn, *serialcomm; | |
73 | struct sr_serial_dev_inst *serial; | |
74 | char reply[50]; | |
75 | int i, model_id; | |
76 | unsigned int len; | |
77 | ||
78 | devices = NULL; | |
79 | conn = NULL; | |
80 | serialcomm = NULL; | |
81 | drvc = di->context; | |
82 | drvc->instances = NULL; | |
83 | ||
84 | for (l = options; l; l = l->next) { | |
85 | src = l->data; | |
86 | switch (src->key) { | |
87 | case SR_CONF_CONN: | |
88 | conn = g_variant_get_string(src->data, NULL); | |
89 | break; | |
90 | case SR_CONF_SERIALCOMM: | |
91 | serialcomm = g_variant_get_string(src->data, NULL); | |
92 | break; | |
93 | default: | |
94 | sr_err("Unknown option %d, skipping.", src->key); | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
99 | if (!conn) | |
100 | return NULL; | |
101 | if (!serialcomm) | |
102 | serialcomm = "9600/8n1"; | |
103 | ||
104 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
105 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
106 | return NULL; | |
107 | ||
108 | serial_flush(serial); | |
109 | ||
110 | /* Get the device model. */ | |
111 | len = 0; | |
112 | for (i = 0; models[i].id; i++) { | |
113 | if (strlen(models[i].id) > len) | |
114 | len = strlen(models[i].id); | |
115 | } | |
116 | memset(&reply, 0, sizeof(reply)); | |
117 | sr_dbg("Want max %d bytes.", len); | |
118 | if ((korad_kaxxxxp_send_cmd(serial, "*IDN?") < 0)) | |
119 | return NULL; | |
120 | ||
121 | /* i is used here for debug purposes only. */ | |
122 | if ((i = korad_kaxxxxp_read_chars(serial, len, reply)) < 0) | |
123 | return NULL; | |
124 | sr_dbg("Received: %d, %s", i, reply); | |
125 | model_id = -1; | |
126 | for (i = 0; models[i].id; i++) { | |
127 | if (!strcmp(models[i].id, reply)) | |
128 | model_id = i; | |
129 | } | |
130 | if (model_id < 0) { | |
131 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
132 | return NULL; | |
133 | } | |
134 | sr_dbg("Found: %s %s (idx %d, ID '%s').", models[model_id].vendor, | |
135 | models[model_id].name, model_id, models[model_id].id); | |
136 | ||
137 | /* Init device instance, etc. */ | |
138 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
139 | sdi->status = SR_ST_INACTIVE; | |
140 | sdi->vendor = g_strdup(models[model_id].vendor); | |
141 | sdi->model = g_strdup(models[model_id].name); | |
142 | sdi->inst_type = SR_INST_SERIAL; | |
143 | sdi->conn = serial; | |
144 | sdi->driver = di; | |
145 | ||
146 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1"); | |
147 | ||
148 | devc = g_malloc0(sizeof(struct dev_context)); | |
149 | sr_sw_limits_init(&devc->limits); | |
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 int config_get(uint32_t key, GVariant **data, | |
176 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
177 | { | |
178 | struct dev_context *devc; | |
179 | ||
180 | (void)cg; | |
181 | ||
182 | if (!sdi || !data) | |
183 | return SR_ERR_ARG; | |
184 | ||
185 | devc = sdi->priv; | |
186 | ||
187 | switch (key) { | |
188 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
189 | case SR_CONF_VOLTAGE: | |
190 | *data = g_variant_new_double(devc->voltage); | |
191 | break; | |
192 | case SR_CONF_VOLTAGE_TARGET: | |
193 | *data = g_variant_new_double(devc->voltage_max); | |
194 | break; | |
195 | case SR_CONF_CURRENT: | |
196 | *data = g_variant_new_double(devc->current); | |
197 | break; | |
198 | case SR_CONF_CURRENT_LIMIT: | |
199 | *data = g_variant_new_double(devc->current_max); | |
200 | break; | |
201 | case SR_CONF_ENABLED: | |
202 | *data = g_variant_new_boolean(devc->output_enabled); | |
203 | break; | |
204 | case SR_CONF_REGULATION: | |
205 | /* Dual channel not supported. */ | |
206 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
207 | break; | |
208 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
209 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
210 | break; | |
211 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
212 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
213 | break; | |
214 | default: | |
215 | return SR_ERR_NA; | |
216 | } | |
217 | ||
218 | return SR_OK; | |
219 | } | |
220 | ||
221 | static int config_set(uint32_t key, GVariant *data, | |
222 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
223 | { | |
224 | struct dev_context *devc; | |
225 | double dval; | |
226 | gboolean bval; | |
227 | ||
228 | (void)cg; | |
229 | ||
230 | if (sdi->status != SR_ST_ACTIVE) | |
231 | return SR_ERR_DEV_CLOSED; | |
232 | ||
233 | devc = sdi->priv; | |
234 | ||
235 | switch (key) { | |
236 | case SR_CONF_LIMIT_MSEC: | |
237 | case SR_CONF_LIMIT_SAMPLES: | |
238 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
239 | case SR_CONF_VOLTAGE_TARGET: | |
240 | dval = g_variant_get_double(data); | |
241 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
242 | return SR_ERR_ARG; | |
243 | devc->voltage_max = dval; | |
244 | devc->target = KAXXXXP_VOLTAGE_MAX; | |
245 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
246 | return SR_ERR; | |
247 | break; | |
248 | case SR_CONF_CURRENT_LIMIT: | |
249 | dval = g_variant_get_double(data); | |
250 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
251 | return SR_ERR_ARG; | |
252 | devc->current_max = dval; | |
253 | devc->target = KAXXXXP_CURRENT_MAX; | |
254 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
255 | return SR_ERR; | |
256 | break; | |
257 | case SR_CONF_ENABLED: | |
258 | bval = g_variant_get_boolean(data); | |
259 | /* Set always so it is possible turn off with sigrok-cli. */ | |
260 | devc->output_enabled = bval; | |
261 | devc->target = KAXXXXP_OUTPUT; | |
262 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
263 | return SR_ERR; | |
264 | break; | |
265 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
266 | bval = g_variant_get_boolean(data); | |
267 | devc->ocp_enabled = bval; | |
268 | devc->target = KAXXXXP_OCP; | |
269 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
270 | return SR_ERR; | |
271 | break; | |
272 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
273 | bval = g_variant_get_boolean(data); | |
274 | devc->ovp_enabled = bval; | |
275 | devc->target = KAXXXXP_OVP; | |
276 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
277 | return SR_ERR; | |
278 | break; | |
279 | default: | |
280 | return SR_ERR_NA; | |
281 | } | |
282 | ||
283 | return SR_OK; | |
284 | } | |
285 | ||
286 | static int config_list(uint32_t key, GVariant **data, | |
287 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
288 | { | |
289 | struct dev_context *devc; | |
290 | GVariant *gvar; | |
291 | GVariantBuilder gvb; | |
292 | double dval; | |
293 | int idx; | |
294 | ||
295 | (void)cg; | |
296 | ||
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 | ||
317 | switch (key) { | |
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; | |
342 | default: | |
343 | return SR_ERR_NA; | |
344 | } | |
345 | ||
346 | return SR_OK; | |
347 | } | |
348 | ||
349 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
350 | { | |
351 | struct dev_context *devc; | |
352 | struct sr_serial_dev_inst *serial; | |
353 | ||
354 | if (sdi->status != SR_ST_ACTIVE) | |
355 | return SR_ERR_DEV_CLOSED; | |
356 | ||
357 | devc = sdi->priv; | |
358 | ||
359 | sr_sw_limits_acquisition_start(&devc->limits); | |
360 | std_session_send_df_header(sdi, LOG_PREFIX); | |
361 | ||
362 | devc->reply_pending = FALSE; | |
363 | devc->req_sent_at = 0; | |
364 | serial = sdi->conn; | |
365 | serial_source_add(sdi->session, serial, G_IO_IN, | |
366 | KAXXXXP_POLL_INTERVAL_MS, | |
367 | korad_kaxxxxp_receive_data, (void *)sdi); | |
368 | ||
369 | return SR_OK; | |
370 | } | |
371 | ||
372 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
373 | { | |
374 | if (sdi->status != SR_ST_ACTIVE) | |
375 | return SR_ERR_DEV_CLOSED; | |
376 | ||
377 | return std_serial_dev_acquisition_stop(sdi, | |
378 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
379 | } | |
380 | ||
381 | SR_PRIV struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
382 | .name = "korad-kaxxxxp", | |
383 | .longname = "Korad KAxxxxP", | |
384 | .api_version = 1, | |
385 | .init = std_init, | |
386 | .cleanup = std_cleanup, | |
387 | .scan = scan, | |
388 | .dev_list = std_dev_list, | |
389 | .config_get = config_get, | |
390 | .config_set = config_set, | |
391 | .config_list = config_list, | |
392 | .dev_open = std_serial_dev_open, | |
393 | .dev_close = std_serial_dev_close, | |
394 | .dev_acquisition_start = dev_acquisition_start, | |
395 | .dev_acquisition_stop = dev_acquisition_stop, | |
396 | .context = NULL, | |
397 | }; |