2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Bastian Schmitz <bastian.schmitz@udo.edu>
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.
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.
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/>.
24 #define IDN_RETRIES 3 /* at least 2 */
26 static const uint32_t scanopts[] = {
31 static const uint32_t drvopts[] = {
35 static const uint32_t devopts[] = {
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
39 SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
43 static const uint32_t devopts_cg[] = {
44 SR_CONF_VOLTAGE | SR_CONF_GET,
45 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_CURRENT | SR_CONF_GET,
47 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 static const char *channel_modes[] = {
54 static const char *gpd_serialcomms[] = {
60 static const struct gpd_model models[] = {
61 { GPD_2303S, "GPD-2303S",
66 { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
68 { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
71 { GPD_3303S, "GPD-3303S",
76 { { 0, 32, 0.001 }, { 0, 3.2, 0.001 } },
78 { { 0, 32, 0.001 }, { 0, 3.2, 0.001 } },
83 static GSList *scan(struct sr_dev_driver *di, GSList *options)
85 const char *conn, *serialcomm, **serialcomms;
86 const struct gpd_model *model;
87 const struct sr_config *src;
88 struct sr_channel *ch;
89 struct sr_channel_group *cg;
91 struct sr_serial_dev_inst *serial;
92 struct sr_dev_inst *sdi;
94 unsigned int i, b, serialcomms_count;
95 struct dev_context *devc;
98 GMatchInfo *match_info;
99 unsigned int cc_cv_ch1, cc_cv_ch2, track1, track2, beep, baud1, baud2;
107 for (l = options; l; l = l->next) {
111 conn = g_variant_get_string(src->data, NULL);
113 case SR_CONF_SERIALCOMM:
114 serialcomm = g_variant_get_string(src->data, NULL);
122 serialcomms = &serialcomm;
123 serialcomms_count = 1;
125 serialcomms = gpd_serialcomms;
126 serialcomms_count = sizeof(gpd_serialcomms) / sizeof(gpd_serialcomms[0]);
129 for( b = 0; b < serialcomms_count; b++) {
130 serialcomm = serialcomms[b];
131 sr_info("Probing serial port %s @ %s", conn, serialcomm);
132 serial = sr_serial_dev_inst_new(conn, serialcomm);
133 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
137 * Problem: we need to clear the GPD receive buffer before we
138 * can expect it to process commands correctly.
140 * Do not just send a newline, since that may cause it to
141 * execute a currently buffered command.
143 * Solution: Send identification request a few times.
144 * The first should corrupt any previous buffered command if present
145 * and respond with "Invalid Character." or respond directly with
146 * an identification string.
148 for (i = 0; i < IDN_RETRIES; i++) {
149 /* Request the GPD to identify itself */
150 gpd_send_cmd(serial, "*IDN?\n");
151 if (gpd_receive_reply(serial, reply, sizeof(reply)) == SR_OK) {
152 if (0 == strncmp(reply, "GW INSTEK", 9)) {
157 if (i == IDN_RETRIES) {
158 sr_err("Device did not reply to identification request.");
159 serial_flush(serial);
164 * Returned identification string is for example:
165 * "GW INSTEK,GPD-2303S,SN:ER915277,V2.10"
167 regex = g_regex_new("GW INSTEK,(.+),SN:(.+),(V.+)", 0, 0, NULL);
168 if (!g_regex_match(regex, reply, 0, &match_info)) {
169 sr_err("Unsupported model '%s'.", reply);
174 for (i = 0; i < ARRAY_SIZE(models); i++) {
175 if (!strcmp(g_match_info_fetch(match_info, 1), models[i].name)) {
181 sr_err("Unsupported model '%s'.", reply);
185 sr_info("Detected model '%s'.", model->name);
187 sdi = g_malloc0(sizeof(struct sr_dev_inst));
188 sdi->status = SR_ST_INACTIVE;
189 sdi->vendor = g_strdup("GW Instek");
190 sdi->model = g_strdup(model->name);
191 sdi->inst_type = SR_INST_SERIAL;
194 for (i = 0; i < model->num_channels; i++) {
195 snprintf(channel, sizeof(channel), "CH%d", i + 1);
196 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel);
197 cg = g_malloc(sizeof(struct sr_channel_group));
198 cg->name = g_strdup(channel);
199 cg->channels = g_slist_append(NULL, ch);
201 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
204 devc = g_malloc0(sizeof(struct dev_context));
205 sr_sw_limits_init(&devc->limits);
207 devc->config = g_malloc0(sizeof(struct per_channel_config)
208 * model->num_channels);
211 serial_flush(serial);
212 gpd_send_cmd(serial, "STATUS?\n");
213 gpd_receive_reply(serial, reply, sizeof(reply));
215 if (sscanf(reply, "%1u%1u%1u%1u%1u%1u%1u%1u", &cc_cv_ch1,
216 &cc_cv_ch2, &track1, &track2, &beep,
217 &devc->output_enabled, &baud1, &baud2) != 8) {
218 /* old firmware (< 2.00?) responds with different format */
219 if (sscanf(reply, "%1u %1u %1u %1u %1u X %1u X", &cc_cv_ch1,
220 &cc_cv_ch2, &track1, &track2, &beep,
221 &devc->output_enabled) != 6) {
222 sr_err("Invalid reply to STATUS: '%s'.", reply);
225 /* ignore remaining two lines of status message */
226 gpd_receive_reply(serial, reply, sizeof(reply));
227 gpd_receive_reply(serial, reply, sizeof(reply));
230 for (i = 0; i < model->num_channels; i++) {
231 gpd_send_cmd(serial, "ISET%d?\n", i + 1);
232 gpd_receive_reply(serial, reply, sizeof(reply));
233 if (sscanf(reply, "%f", &devc->config[i].output_current_max) != 1) {
234 sr_err("Invalid reply to ISETn?: '%s'.", reply);
238 gpd_send_cmd(serial, "VSET%d?\n", i + 1);
239 gpd_receive_reply(serial, reply, sizeof(reply));
240 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
241 sr_err("Invalid reply to VSETn?: '%s'.", reply);
244 gpd_send_cmd(serial, "IOUT%d?\n", i + 1);
245 gpd_receive_reply(serial, reply, sizeof(reply));
246 if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
247 sr_err("Invalid reply to IOUTn?: '%s'.", reply);
250 gpd_send_cmd(serial, "VOUT%d?\n", i + 1);
251 gpd_receive_reply(serial, reply, sizeof(reply));
252 if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
253 sr_err("Invalid reply to VOUTn?: '%s'.", reply);
258 return std_scan_complete(di, g_slist_append(NULL, sdi));
262 g_match_info_free(match_info);
264 g_regex_unref(regex);
266 serial_close(serial);
272 static int config_get(uint32_t key, GVariant **data,
273 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
276 const struct dev_context *devc;
277 const struct sr_channel *ch;
286 case SR_CONF_LIMIT_SAMPLES:
287 case SR_CONF_LIMIT_MSEC:
288 return sr_sw_limits_config_get(&devc->limits, key, data);
289 case SR_CONF_CHANNEL_CONFIG:
290 *data = g_variant_new_string(
291 channel_modes[devc->channel_mode]);
293 case SR_CONF_ENABLED:
294 *data = g_variant_new_boolean(devc->output_enabled);
300 ch = cg->channels->data;
303 case SR_CONF_VOLTAGE:
304 *data = g_variant_new_double(
305 devc->config[channel].output_voltage_last);
307 case SR_CONF_VOLTAGE_TARGET:
308 *data = g_variant_new_double(
309 devc->config[channel].output_voltage_max);
311 case SR_CONF_CURRENT:
312 *data = g_variant_new_double(
313 devc->config[channel].output_current_last);
315 case SR_CONF_CURRENT_LIMIT:
316 *data = g_variant_new_double(
317 devc->config[channel].output_current_max);
327 static int config_set(uint32_t key, GVariant *data,
328 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
331 const struct sr_channel *ch;
334 struct dev_context *devc;
341 case SR_CONF_LIMIT_MSEC:
342 case SR_CONF_LIMIT_SAMPLES:
343 return sr_sw_limits_config_set(&devc->limits, key, data);
344 case SR_CONF_ENABLED:
345 bval = g_variant_get_boolean(data);
346 gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0');
347 devc->output_enabled = bval;
349 case SR_CONF_VOLTAGE_TARGET:
350 ch = cg->channels->data;
352 dval = g_variant_get_double(data);
353 if (dval < devc->model->channels[channel].voltage[0]
354 || dval > devc->model->channels[channel].voltage[1])
356 gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval);
357 devc->config[channel].output_voltage_max = dval;
359 case SR_CONF_CURRENT_LIMIT:
360 ch = cg->channels->data;
362 dval = g_variant_get_double(data);
363 if (dval < devc->model->channels[channel].current[0]
364 || dval > devc->model->channels[channel].current[1])
366 gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval);
367 devc->config[channel].output_current_max = dval;
377 static int config_list(uint32_t key, GVariant **data,
378 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
380 const struct dev_context *devc;
381 const struct sr_channel *ch;
384 devc = (sdi) ? sdi->priv : NULL;
388 case SR_CONF_SCAN_OPTIONS:
389 case SR_CONF_DEVICE_OPTIONS:
390 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
392 case SR_CONF_CHANNEL_CONFIG:
393 *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
399 ch = cg->channels->data;
403 case SR_CONF_DEVICE_OPTIONS:
404 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
406 case SR_CONF_VOLTAGE_TARGET:
407 *data = std_gvar_min_max_step_array(
408 devc->model->channels[channel].voltage);
410 case SR_CONF_CURRENT_LIMIT:
411 *data = std_gvar_min_max_step_array(
412 devc->model->channels[channel].current);
422 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
424 struct dev_context *devc;
425 struct sr_serial_dev_inst *serial;
429 sr_sw_limits_acquisition_start(&devc->limits);
430 std_session_send_df_header(sdi);
432 devc->reply_pending = FALSE;
433 devc->req_sent_at = 0;
435 serial_source_add(sdi->session, serial, G_IO_IN, 100,
436 gpd_receive_data, (void *)sdi);
441 static struct sr_dev_driver gwinstek_gpd_driver_info = {
442 .name = "gwinstek-gpd",
443 .longname = "GW Instek GPD",
446 .cleanup = std_cleanup,
448 .dev_list = std_dev_list,
449 .dev_clear = std_dev_clear,
450 .config_get = config_get,
451 .config_set = config_set,
452 .config_list = config_list,
453 .dev_open = std_serial_dev_open,
454 .dev_close = std_serial_dev_close,
455 .dev_acquisition_start = dev_acquisition_start,
456 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
459 SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info);