]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gpd/api.c
gwinstek-gpd: Initial implementation.
[libsigrok.git] / src / hardware / gwinstek-gpd / api.c
CommitLineData
b872ab7d
BS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2018 Bastian Schmitz <bastian.schmitz@udo.edu>
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 <string.h>
22#include "protocol.h"
23
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
26 SR_CONF_SERIALCOMM,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_POWER_SUPPLY,
31};
32
33static const uint32_t devopts[] = {
34 SR_CONF_CONTINUOUS,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
39};
40
41static const uint32_t devopts_cg[] = {
42 SR_CONF_VOLTAGE | SR_CONF_GET,
43 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_CURRENT | SR_CONF_GET,
45 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46};
47
48static const char *channel_modes[] = {
49 "Independent",
50};
51
52static const struct gpd_model models[] = {
53 { GPD_2303S, "GPD-2303S",
54 CHANMODE_INDEPENDENT,
55 2,
56 {
57 /* Channel 1 */
58 { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
59 /* Channel 2 */
60 { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
61 },
62 },
63};
64
65static GSList *scan(struct sr_dev_driver *di, GSList *options)
66{
67 const char *conn, *serialcomm;
68 const struct gpd_model *model;
69 const struct sr_config *src;
70 struct sr_channel *ch;
71 struct sr_channel_group *cg;
72 GSList *l;
73 struct sr_serial_dev_inst *serial;
74 struct sr_dev_inst *sdi;
75 char reply[50];
76 unsigned int i;
77 struct dev_context *devc;
78 char channel[10];
79 GRegex *regex;
80 GMatchInfo *match_info;
81 unsigned int cc_cv_ch1, cc_cv_ch2, track1, track2, beep, baud1, baud2;
82
83 serial = NULL;
84 match_info = NULL;
85 regex = NULL;
86 conn = NULL;
87 serialcomm = NULL;
88
89 for (l = options; l; l = l->next) {
90 src = l->data;
91 switch (src->key) {
92 case SR_CONF_CONN:
93 conn = g_variant_get_string(src->data, NULL);
94 break;
95 case SR_CONF_SERIALCOMM:
96 serialcomm = g_variant_get_string(src->data, NULL);
97 break;
98 }
99 }
100
101 if (!conn)
102 return NULL;
103 if (!serialcomm)
104 serialcomm = "115200/8n1";
105 sr_info("Probing serial port %s.", conn);
106 serial = sr_serial_dev_inst_new(conn, serialcomm);
107 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
108 return NULL;
109
110 serial_flush(serial);
111 gpd_send_cmd(serial, "*IDN?\n");
112 if (gpd_receive_reply(serial, reply, sizeof(reply)) != SR_OK) {
113 sr_err("Device did not reply.");
114 goto error;
115 }
116 serial_flush(serial);
117
118 /*
119 * Returned identification string is for example:
120 * "GW INSTEK,GPD-2303S,SN:ER915277,V2.10"
121 */
122 regex = g_regex_new("GW INSTEK,(.+),SN:(.+),(V.+)", 0, 0, NULL);
123 if (!g_regex_match(regex, reply, 0, &match_info)) {
124 sr_err("Unsupported model '%s'.", reply);
125 goto error;
126 }
127
128 model = NULL;
129 for (i = 0; i < ARRAY_SIZE(models); i++) {
130 if (!strcmp(g_match_info_fetch(match_info, 1), models[i].name)) {
131 model = &models[i];
132 break;
133 }
134 }
135 if (!model) {
136 sr_err("Unsupported model '%s'.", reply);
137 goto error;
138 }
139
140 sr_info("Detected model '%s'.", model->name);
141
142 sdi = g_malloc0(sizeof(struct sr_dev_inst));
143 sdi->status = SR_ST_INACTIVE;
144 sdi->vendor = g_strdup("GW Instek");
145 sdi->model = g_strdup(model->name);
146 sdi->inst_type = SR_INST_SERIAL;
147 sdi->conn = serial;
148
149 for (i = 0; i < model->num_channels; i++) {
150 snprintf(channel, sizeof(channel), "CH%d", i + 1);
151 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel);
152 cg = g_malloc(sizeof(struct sr_channel_group));
153 cg->name = g_strdup(channel);
154 cg->channels = g_slist_append(NULL, ch);
155 cg->priv = NULL;
156 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
157 }
158
159 devc = g_malloc0(sizeof(struct dev_context));
160 sr_sw_limits_init(&devc->limits);
161 devc->model = model;
162 devc->config = g_malloc0(sizeof(struct per_channel_config)
163 * model->num_channels);
164 sdi->priv = devc;
165
166 serial_flush(serial);
167 gpd_send_cmd(serial, "STATUS?\n");
168 gpd_receive_reply(serial, reply, sizeof(reply));
169
170 if (sscanf(reply, "%1u%1u%1u%1u%1u%1u%1u%1u", &cc_cv_ch1,
171 &cc_cv_ch2, &track1, &track2, &beep,
172 &devc->output_enabled, &baud1, &baud2) != 8) {
173 sr_err("Invalid reply to STATUS: '%s'.", reply);
174 goto error;
175 }
176
177 for (i = 0; i < model->num_channels; ++i) {
178 gpd_send_cmd(serial, "ISET%d?\n", i + 1);
179 gpd_receive_reply(serial, reply, sizeof(reply));
180 if (sscanf(reply, "%f", &devc->config[i].output_current_max) != 1) {
181 sr_err("Invalid reply to ISETn?: '%s'.", reply);
182 goto error;
183 }
184
185 gpd_send_cmd(serial, "VSET%d?\n", i + 1);
186 gpd_receive_reply(serial, reply, sizeof(reply));
187 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
188 sr_err("Invalid reply to VSETn?: '%s'.", reply);
189 goto error;
190 }
191 gpd_send_cmd(serial, "IOUT%d?\n", i + 1);
192 gpd_receive_reply(serial, reply, sizeof(reply));
193 if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
194 sr_err("Invalid reply to IOUTn?: '%s'.", reply);
195 goto error;
196 }
197 gpd_send_cmd(serial, "VOUT%d?\n", i + 1);
198 gpd_receive_reply(serial, reply, sizeof(reply));
199 if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
200 sr_err("Invalid reply to VOUTn?: '%s'.", reply);
201 goto error;
202 }
203 }
204
205 serial_close(serial);
206
207 return std_scan_complete(di, g_slist_append(NULL, sdi));
208
209error:
210 if (match_info)
211 g_match_info_free(match_info);
212 if (regex)
213 g_regex_unref(regex);
214 if (serial)
215 serial_close(serial);
216
217 return NULL;
218}
219
220static int config_get(uint32_t key, GVariant **data,
221 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
222{
223 int ret, channel;
224 const struct dev_context *devc;
225 const struct sr_channel *ch;
226 const struct sr_key_info *ki;
227
228 ki = sr_key_info_get(SR_KEY_CONFIG, key);
229
230 sr_info("%s(%d, %s)", __func__, key, (ki != NULL) ? ki->name : NULL);
231
232 if (!sdi)
233 return SR_ERR_ARG;
234
235 devc = sdi->priv;
236
237 if (!cg) {
238 switch (key) {
239 case SR_CONF_CHANNEL_CONFIG:
240 *data = g_variant_new_string(
241 channel_modes[devc->channel_mode]);
242 break;
243 case SR_CONF_ENABLED:
244 *data = g_variant_new_boolean(devc->output_enabled);
245 break;
246 default:
247 return SR_ERR_NA;
248 }
249 } else {
250 ch = cg->channels->data;
251 channel = ch->index;
252 sr_info("%s(%d, %s, %d)", __func__, key,
253 (ki != NULL) ? ki->name : NULL, channel);
254 ret = SR_OK;
255 switch (key) {
256 case SR_CONF_VOLTAGE:
257 *data = g_variant_new_double(
258 devc->config[channel].output_voltage_last);
259 break;
260 case SR_CONF_VOLTAGE_TARGET:
261 *data = g_variant_new_double(
262 devc->config[channel].output_voltage_max);
263 break;
264 case SR_CONF_CURRENT:
265 *data = g_variant_new_double(
266 devc->config[channel].output_current_last);
267 break;
268 case SR_CONF_CURRENT_LIMIT:
269 *data = g_variant_new_double(
270 devc->config[channel].output_current_max);
271 break;
272 default:
273 return SR_ERR_NA;
274 }
275 }
276
277 return ret;
278}
279
280static int config_set(uint32_t key, GVariant *data,
281 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
282{
283 int ret, channel;
284 const struct sr_channel *ch;
285 double dval;
286 gboolean bval;
287 struct dev_context *devc;
288
289 devc = sdi->priv;
290 sr_info("%s(%d)", __func__, key);
291
292 ret = SR_OK;
293
294 switch (key) {
295 case SR_CONF_LIMIT_MSEC:
296 case SR_CONF_LIMIT_SAMPLES:
297 return sr_sw_limits_config_set(&devc->limits, key, data);
298 case SR_CONF_ENABLED:
299 bval = g_variant_get_boolean(data);
300 gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0');
301 devc->output_enabled = bval;
302 break;
303 case SR_CONF_VOLTAGE_TARGET:
304 ch = cg->channels->data;
305 channel = ch->index;
306 dval = g_variant_get_double(data);
307 if (dval < devc->model->channels[channel].voltage[0]
308 || dval > devc->model->channels[channel].voltage[1])
309 return SR_ERR_ARG;
310 gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval);
311 devc->config[channel].output_voltage_max = dval;
312 break;
313 case SR_CONF_CURRENT_LIMIT:
314 ch = cg->channels->data;
315 channel = ch->index;
316 dval = g_variant_get_double(data);
317 if (dval < devc->model->channels[channel].current[0]
318 || dval > devc->model->channels[channel].current[1])
319 return SR_ERR_ARG;
320 gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval);
321 devc->config[channel].output_current_max = dval;
322 break;
323 default:
324 ret = SR_ERR_NA;
325 break;
326 }
327
328 return ret;
329}
330
331static int config_list(uint32_t key, GVariant **data,
332 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
333{
334 const struct dev_context *devc;
335 const struct sr_channel *ch;
336 int channel;
337
338 devc = (sdi) ? sdi->priv : NULL;
339
340 if (!cg) {
341 switch (key) {
342 case SR_CONF_SCAN_OPTIONS:
343 case SR_CONF_DEVICE_OPTIONS:
344 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
345 drvopts, devopts);
346 case SR_CONF_CHANNEL_CONFIG:
347 *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
348 break;
349 default:
350 return SR_ERR_NA;
351 }
352 } else {
353 ch = cg->channels->data;
354 channel = ch->index;
355
356 switch (key) {
357 case SR_CONF_DEVICE_OPTIONS:
358 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
359 break;
360 case SR_CONF_VOLTAGE_TARGET:
361 *data = std_gvar_min_max_step_array(
362 devc->model->channels[channel].voltage);
363 break;
364 case SR_CONF_CURRENT_LIMIT:
365 *data = std_gvar_min_max_step_array(
366 devc->model->channels[channel].current);
367 break;
368 default:
369 return SR_ERR_NA;
370 }
371 }
372
373 return SR_OK;
374}
375
376static int dev_acquisition_start(const struct sr_dev_inst *sdi)
377{
378 struct dev_context *devc;
379 struct sr_serial_dev_inst *serial;
380
381 sr_info("%s()", __func__);
382 devc = sdi->priv;
383
384 sr_sw_limits_acquisition_start(&devc->limits);
385 std_session_send_df_header(sdi);
386
387 devc->reply_pending = FALSE;
388 devc->req_sent_at = 0;
389 serial = sdi->conn;
390 serial_source_add(sdi->session, serial, G_IO_IN, 100,
391 gpd_receive_data, (void *)sdi);
392
393 return SR_OK;
394}
395
396SR_PRIV const struct sr_dev_driver gwinstek_gpd_driver_info = {
397 .name = "gwinstek-gpd",
398 .longname = "GW Instek GPD",
399 .api_version = 1,
400 .init = std_init,
401 .cleanup = std_cleanup,
402 .scan = scan,
403 .dev_list = std_dev_list,
404 .dev_clear = std_dev_clear,
405 .config_get = config_get,
406 .config_set = config_set,
407 .config_list = config_list,
408 .dev_open = std_serial_dev_open,
409 .dev_close = std_serial_dev_close,
410 .dev_acquisition_start = dev_acquisition_start,
411 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
412 .context = NULL,
413};
414SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info);