]>
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 dev_context *devc; | |
68 | GSList *l; | |
69 | struct sr_dev_inst *sdi; | |
70 | struct sr_config *src; | |
71 | const char *conn, *serialcomm; | |
72 | struct sr_serial_dev_inst *serial; | |
73 | char reply[50]; | |
74 | int i, model_id; | |
75 | unsigned int len; | |
76 | ||
77 | conn = NULL; | |
78 | serialcomm = NULL; | |
79 | ||
80 | for (l = options; l; l = l->next) { | |
81 | src = l->data; | |
82 | switch (src->key) { | |
83 | case SR_CONF_CONN: | |
84 | conn = g_variant_get_string(src->data, NULL); | |
85 | break; | |
86 | case SR_CONF_SERIALCOMM: | |
87 | serialcomm = g_variant_get_string(src->data, NULL); | |
88 | break; | |
89 | default: | |
90 | sr_err("Unknown option %d, skipping.", src->key); | |
91 | break; | |
92 | } | |
93 | } | |
94 | ||
95 | if (!conn) | |
96 | return NULL; | |
97 | if (!serialcomm) | |
98 | serialcomm = "9600/8n1"; | |
99 | ||
100 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
101 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
102 | return NULL; | |
103 | ||
104 | serial_flush(serial); | |
105 | ||
106 | /* Get the device model. */ | |
107 | len = 0; | |
108 | for (i = 0; models[i].id; i++) { | |
109 | if (strlen(models[i].id) > len) | |
110 | len = strlen(models[i].id); | |
111 | } | |
112 | memset(&reply, 0, sizeof(reply)); | |
113 | sr_dbg("Want max %d bytes.", len); | |
114 | if ((korad_kaxxxxp_send_cmd(serial, "*IDN?") < 0)) | |
115 | return NULL; | |
116 | ||
117 | /* i is used here for debug purposes only. */ | |
118 | if ((i = korad_kaxxxxp_read_chars(serial, len, reply)) < 0) | |
119 | return NULL; | |
120 | sr_dbg("Received: %d, %s", i, reply); | |
121 | model_id = -1; | |
122 | for (i = 0; models[i].id; i++) { | |
123 | if (!strcmp(models[i].id, reply)) | |
124 | model_id = i; | |
125 | } | |
126 | if (model_id < 0) { | |
127 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
128 | return NULL; | |
129 | } | |
130 | sr_dbg("Found: %s %s (idx %d, ID '%s').", models[model_id].vendor, | |
131 | models[model_id].name, model_id, models[model_id].id); | |
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 | ||
141 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1"); | |
142 | ||
143 | devc = g_malloc0(sizeof(struct dev_context)); | |
144 | sr_sw_limits_init(&devc->limits); | |
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_kaxxxxp_get_all_values(serial, devc) < 0) | |
152 | goto exit_err; | |
153 | ||
154 | serial_close(serial); | |
155 | ||
156 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
157 | ||
158 | exit_err: | |
159 | sr_dev_inst_free(sdi); | |
160 | g_free(devc); | |
161 | sr_dbg("Scan failed."); | |
162 | ||
163 | return NULL; | |
164 | } | |
165 | ||
166 | static int config_get(uint32_t key, GVariant **data, | |
167 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
168 | { | |
169 | struct dev_context *devc; | |
170 | ||
171 | (void)cg; | |
172 | ||
173 | if (!sdi || !data) | |
174 | return SR_ERR_ARG; | |
175 | ||
176 | devc = sdi->priv; | |
177 | ||
178 | switch (key) { | |
179 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
180 | case SR_CONF_VOLTAGE: | |
181 | *data = g_variant_new_double(devc->voltage); | |
182 | break; | |
183 | case SR_CONF_VOLTAGE_TARGET: | |
184 | *data = g_variant_new_double(devc->voltage_max); | |
185 | break; | |
186 | case SR_CONF_CURRENT: | |
187 | *data = g_variant_new_double(devc->current); | |
188 | break; | |
189 | case SR_CONF_CURRENT_LIMIT: | |
190 | *data = g_variant_new_double(devc->current_max); | |
191 | break; | |
192 | case SR_CONF_ENABLED: | |
193 | *data = g_variant_new_boolean(devc->output_enabled); | |
194 | break; | |
195 | case SR_CONF_REGULATION: | |
196 | /* Dual channel not supported. */ | |
197 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
198 | break; | |
199 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
200 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
201 | break; | |
202 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
203 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
204 | break; | |
205 | default: | |
206 | return SR_ERR_NA; | |
207 | } | |
208 | ||
209 | return SR_OK; | |
210 | } | |
211 | ||
212 | static int config_set(uint32_t key, GVariant *data, | |
213 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
214 | { | |
215 | struct dev_context *devc; | |
216 | double dval; | |
217 | gboolean bval; | |
218 | ||
219 | (void)cg; | |
220 | ||
221 | if (sdi->status != SR_ST_ACTIVE) | |
222 | return SR_ERR_DEV_CLOSED; | |
223 | ||
224 | devc = sdi->priv; | |
225 | ||
226 | switch (key) { | |
227 | case SR_CONF_LIMIT_MSEC: | |
228 | case SR_CONF_LIMIT_SAMPLES: | |
229 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
230 | case SR_CONF_VOLTAGE_TARGET: | |
231 | dval = g_variant_get_double(data); | |
232 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
233 | return SR_ERR_ARG; | |
234 | devc->voltage_max = dval; | |
235 | devc->target = KAXXXXP_VOLTAGE_MAX; | |
236 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
237 | return SR_ERR; | |
238 | break; | |
239 | case SR_CONF_CURRENT_LIMIT: | |
240 | dval = g_variant_get_double(data); | |
241 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
242 | return SR_ERR_ARG; | |
243 | devc->current_max = dval; | |
244 | devc->target = KAXXXXP_CURRENT_MAX; | |
245 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
246 | return SR_ERR; | |
247 | break; | |
248 | case SR_CONF_ENABLED: | |
249 | bval = g_variant_get_boolean(data); | |
250 | /* Set always so it is possible turn off with sigrok-cli. */ | |
251 | devc->output_enabled = bval; | |
252 | devc->target = KAXXXXP_OUTPUT; | |
253 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
254 | return SR_ERR; | |
255 | break; | |
256 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
257 | bval = g_variant_get_boolean(data); | |
258 | devc->ocp_enabled = bval; | |
259 | devc->target = KAXXXXP_OCP; | |
260 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
261 | return SR_ERR; | |
262 | break; | |
263 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
264 | bval = g_variant_get_boolean(data); | |
265 | devc->ovp_enabled = bval; | |
266 | devc->target = KAXXXXP_OVP; | |
267 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
268 | return SR_ERR; | |
269 | break; | |
270 | default: | |
271 | return SR_ERR_NA; | |
272 | } | |
273 | ||
274 | return SR_OK; | |
275 | } | |
276 | ||
277 | static int config_list(uint32_t key, GVariant **data, | |
278 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
279 | { | |
280 | struct dev_context *devc; | |
281 | GVariant *gvar; | |
282 | GVariantBuilder gvb; | |
283 | double dval; | |
284 | int idx; | |
285 | ||
286 | (void)cg; | |
287 | ||
288 | /* Always available (with or without sdi). */ | |
289 | if (key == SR_CONF_SCAN_OPTIONS) { | |
290 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
291 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
292 | return SR_OK; | |
293 | } | |
294 | ||
295 | /* Return drvopts without sdi (and devopts with sdi, see below). */ | |
296 | if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
297 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
298 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
299 | return SR_OK; | |
300 | } | |
301 | ||
302 | /* Every other key needs an sdi. */ | |
303 | if (!sdi) | |
304 | return SR_ERR_ARG; | |
305 | ||
306 | devc = sdi->priv; | |
307 | ||
308 | switch (key) { | |
309 | case SR_CONF_DEVICE_OPTIONS: | |
310 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
311 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
312 | break; | |
313 | case SR_CONF_VOLTAGE_TARGET: | |
314 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
315 | /* Min, max, step. */ | |
316 | for (idx = 0; idx < 3; idx++) { | |
317 | dval = devc->model->voltage[idx]; | |
318 | gvar = g_variant_new_double(dval); | |
319 | g_variant_builder_add_value(&gvb, gvar); | |
320 | } | |
321 | *data = g_variant_builder_end(&gvb); | |
322 | break; | |
323 | case SR_CONF_CURRENT_LIMIT: | |
324 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
325 | /* Min, max, step. */ | |
326 | for (idx = 0; idx < 3; idx++) { | |
327 | dval = devc->model->current[idx]; | |
328 | gvar = g_variant_new_double(dval); | |
329 | g_variant_builder_add_value(&gvb, gvar); | |
330 | } | |
331 | *data = g_variant_builder_end(&gvb); | |
332 | break; | |
333 | default: | |
334 | return SR_ERR_NA; | |
335 | } | |
336 | ||
337 | return SR_OK; | |
338 | } | |
339 | ||
340 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
341 | { | |
342 | struct dev_context *devc; | |
343 | struct sr_serial_dev_inst *serial; | |
344 | ||
345 | if (sdi->status != SR_ST_ACTIVE) | |
346 | return SR_ERR_DEV_CLOSED; | |
347 | ||
348 | devc = sdi->priv; | |
349 | ||
350 | sr_sw_limits_acquisition_start(&devc->limits); | |
351 | std_session_send_df_header(sdi, LOG_PREFIX); | |
352 | ||
353 | devc->reply_pending = FALSE; | |
354 | devc->req_sent_at = 0; | |
355 | serial = sdi->conn; | |
356 | serial_source_add(sdi->session, serial, G_IO_IN, | |
357 | KAXXXXP_POLL_INTERVAL_MS, | |
358 | korad_kaxxxxp_receive_data, (void *)sdi); | |
359 | ||
360 | return SR_OK; | |
361 | } | |
362 | ||
363 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
364 | { | |
365 | if (sdi->status != SR_ST_ACTIVE) | |
366 | return SR_ERR_DEV_CLOSED; | |
367 | ||
368 | return std_serial_dev_acquisition_stop(sdi, | |
369 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
370 | } | |
371 | ||
372 | static struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
373 | .name = "korad-kaxxxxp", | |
374 | .longname = "Korad KAxxxxP", | |
375 | .api_version = 1, | |
376 | .init = std_init, | |
377 | .cleanup = std_cleanup, | |
378 | .scan = scan, | |
379 | .dev_list = std_dev_list, | |
380 | .config_get = config_get, | |
381 | .config_set = config_set, | |
382 | .config_list = config_list, | |
383 | .dev_open = std_serial_dev_open, | |
384 | .dev_close = std_serial_dev_close, | |
385 | .dev_acquisition_start = dev_acquisition_start, | |
386 | .dev_acquisition_stop = dev_acquisition_stop, | |
387 | .context = NULL, | |
388 | }; | |
389 | SR_REGISTER_DEV_DRIVER(korad_kaxxxxp_driver_info); |