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 = sr_channel_group_new(sdi, channel, NULL);
198 cg->channels = g_slist_append(NULL, ch);
201 devc = g_malloc0(sizeof(struct dev_context));
202 sr_sw_limits_init(&devc->limits);
204 devc->config = g_malloc0(sizeof(struct per_channel_config)
205 * model->num_channels);
208 serial_flush(serial);
209 gpd_send_cmd(serial, "STATUS?\n");
210 gpd_receive_reply(serial, reply, sizeof(reply));
212 if (sscanf(reply, "%1u%1u%1u%1u%1u%1u%1u%1u", &cc_cv_ch1,
213 &cc_cv_ch2, &track1, &track2, &beep,
214 &devc->output_enabled, &baud1, &baud2) != 8) {
215 /* old firmware (< 2.00?) responds with different format */
216 if (sscanf(reply, "%1u %1u %1u %1u %1u X %1u X", &cc_cv_ch1,
217 &cc_cv_ch2, &track1, &track2, &beep,
218 &devc->output_enabled) != 6) {
219 sr_err("Invalid reply to STATUS: '%s'.", reply);
222 /* ignore remaining two lines of status message */
223 gpd_receive_reply(serial, reply, sizeof(reply));
224 gpd_receive_reply(serial, reply, sizeof(reply));
227 for (i = 0; i < model->num_channels; i++) {
228 gpd_send_cmd(serial, "ISET%d?\n", i + 1);
229 gpd_receive_reply(serial, reply, sizeof(reply));
230 if (sscanf(reply, "%f", &devc->config[i].output_current_max) != 1) {
231 sr_err("Invalid reply to ISETn?: '%s'.", reply);
235 gpd_send_cmd(serial, "VSET%d?\n", i + 1);
236 gpd_receive_reply(serial, reply, sizeof(reply));
237 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
238 sr_err("Invalid reply to VSETn?: '%s'.", reply);
241 gpd_send_cmd(serial, "IOUT%d?\n", i + 1);
242 gpd_receive_reply(serial, reply, sizeof(reply));
243 if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
244 sr_err("Invalid reply to IOUTn?: '%s'.", reply);
247 gpd_send_cmd(serial, "VOUT%d?\n", i + 1);
248 gpd_receive_reply(serial, reply, sizeof(reply));
249 if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
250 sr_err("Invalid reply to VOUTn?: '%s'.", reply);
255 return std_scan_complete(di, g_slist_append(NULL, sdi));
259 g_match_info_free(match_info);
261 g_regex_unref(regex);
263 serial_close(serial);
269 static int config_get(uint32_t key, GVariant **data,
270 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
273 const struct dev_context *devc;
274 const struct sr_channel *ch;
283 case SR_CONF_LIMIT_SAMPLES:
284 case SR_CONF_LIMIT_MSEC:
285 return sr_sw_limits_config_get(&devc->limits, key, data);
286 case SR_CONF_CHANNEL_CONFIG:
287 *data = g_variant_new_string(
288 channel_modes[devc->channel_mode]);
290 case SR_CONF_ENABLED:
291 *data = g_variant_new_boolean(devc->output_enabled);
297 ch = cg->channels->data;
300 case SR_CONF_VOLTAGE:
301 *data = g_variant_new_double(
302 devc->config[channel].output_voltage_last);
304 case SR_CONF_VOLTAGE_TARGET:
305 *data = g_variant_new_double(
306 devc->config[channel].output_voltage_max);
308 case SR_CONF_CURRENT:
309 *data = g_variant_new_double(
310 devc->config[channel].output_current_last);
312 case SR_CONF_CURRENT_LIMIT:
313 *data = g_variant_new_double(
314 devc->config[channel].output_current_max);
324 static int config_set(uint32_t key, GVariant *data,
325 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
328 const struct sr_channel *ch;
331 struct dev_context *devc;
338 case SR_CONF_LIMIT_MSEC:
339 case SR_CONF_LIMIT_SAMPLES:
340 return sr_sw_limits_config_set(&devc->limits, key, data);
341 case SR_CONF_ENABLED:
342 bval = g_variant_get_boolean(data);
343 gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0');
344 devc->output_enabled = bval;
346 case SR_CONF_VOLTAGE_TARGET:
347 ch = cg->channels->data;
349 dval = g_variant_get_double(data);
350 if (dval < devc->model->channels[channel].voltage[0]
351 || dval > devc->model->channels[channel].voltage[1])
353 gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval);
354 devc->config[channel].output_voltage_max = dval;
356 case SR_CONF_CURRENT_LIMIT:
357 ch = cg->channels->data;
359 dval = g_variant_get_double(data);
360 if (dval < devc->model->channels[channel].current[0]
361 || dval > devc->model->channels[channel].current[1])
363 gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval);
364 devc->config[channel].output_current_max = dval;
374 static int config_list(uint32_t key, GVariant **data,
375 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
377 const struct dev_context *devc;
378 const struct sr_channel *ch;
381 devc = (sdi) ? sdi->priv : NULL;
385 case SR_CONF_SCAN_OPTIONS:
386 case SR_CONF_DEVICE_OPTIONS:
387 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
389 case SR_CONF_CHANNEL_CONFIG:
390 *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
396 ch = cg->channels->data;
400 case SR_CONF_DEVICE_OPTIONS:
401 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
403 case SR_CONF_VOLTAGE_TARGET:
404 *data = std_gvar_min_max_step_array(
405 devc->model->channels[channel].voltage);
407 case SR_CONF_CURRENT_LIMIT:
408 *data = std_gvar_min_max_step_array(
409 devc->model->channels[channel].current);
419 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
421 struct dev_context *devc;
422 struct sr_serial_dev_inst *serial;
426 sr_sw_limits_acquisition_start(&devc->limits);
427 std_session_send_df_header(sdi);
429 devc->reply_pending = FALSE;
430 devc->req_sent_at = 0;
432 serial_source_add(sdi->session, serial, G_IO_IN, 100,
433 gpd_receive_data, (void *)sdi);
438 static struct sr_dev_driver gwinstek_gpd_driver_info = {
439 .name = "gwinstek-gpd",
440 .longname = "GW Instek GPD",
443 .cleanup = std_cleanup,
445 .dev_list = std_dev_list,
446 .dev_clear = std_dev_clear,
447 .config_get = config_get,
448 .config_set = config_set,
449 .config_list = config_list,
450 .dev_open = std_serial_dev_open,
451 .dev_close = std_serial_dev_close,
452 .dev_acquisition_start = dev_acquisition_start,
453 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
456 SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info);