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