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 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
33 static const uint32_t devopts[] = {
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,
41 static 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,
48 static const char *channel_modes[] = {
52 static const struct gpd_model models[] = {
53 { GPD_2303S, "GPD-2303S",
58 { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
60 { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
65 static GSList *scan(struct sr_dev_driver *di, GSList *options)
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;
73 struct sr_serial_dev_inst *serial;
74 struct sr_dev_inst *sdi;
77 struct dev_context *devc;
80 GMatchInfo *match_info;
81 unsigned int cc_cv_ch1, cc_cv_ch2, track1, track2, beep, baud1, baud2;
89 for (l = options; l; l = l->next) {
93 conn = g_variant_get_string(src->data, NULL);
95 case SR_CONF_SERIALCOMM:
96 serialcomm = g_variant_get_string(src->data, NULL);
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)
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.");
116 serial_flush(serial);
119 * Returned identification string is for example:
120 * "GW INSTEK,GPD-2303S,SN:ER915277,V2.10"
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);
129 for (i = 0; i < ARRAY_SIZE(models); i++) {
130 if (!strcmp(g_match_info_fetch(match_info, 1), models[i].name)) {
136 sr_err("Unsupported model '%s'.", reply);
140 sr_info("Detected model '%s'.", model->name);
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;
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);
156 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
159 devc = g_malloc0(sizeof(struct dev_context));
160 sr_sw_limits_init(&devc->limits);
162 devc->config = g_malloc0(sizeof(struct per_channel_config)
163 * model->num_channels);
166 serial_flush(serial);
167 gpd_send_cmd(serial, "STATUS?\n");
168 gpd_receive_reply(serial, reply, sizeof(reply));
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);
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);
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);
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);
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);
205 serial_close(serial);
207 return std_scan_complete(di, g_slist_append(NULL, sdi));
211 g_match_info_free(match_info);
213 g_regex_unref(regex);
215 serial_close(serial);
220 static int config_get(uint32_t key, GVariant **data,
221 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
224 const struct dev_context *devc;
225 const struct sr_channel *ch;
234 case SR_CONF_CHANNEL_CONFIG:
235 *data = g_variant_new_string(
236 channel_modes[devc->channel_mode]);
238 case SR_CONF_ENABLED:
239 *data = g_variant_new_boolean(devc->output_enabled);
245 ch = cg->channels->data;
249 case SR_CONF_VOLTAGE:
250 *data = g_variant_new_double(
251 devc->config[channel].output_voltage_last);
253 case SR_CONF_VOLTAGE_TARGET:
254 *data = g_variant_new_double(
255 devc->config[channel].output_voltage_max);
257 case SR_CONF_CURRENT:
258 *data = g_variant_new_double(
259 devc->config[channel].output_current_last);
261 case SR_CONF_CURRENT_LIMIT:
262 *data = g_variant_new_double(
263 devc->config[channel].output_current_max);
273 static int config_set(uint32_t key, GVariant *data,
274 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
277 const struct sr_channel *ch;
280 struct dev_context *devc;
287 case SR_CONF_LIMIT_MSEC:
288 case SR_CONF_LIMIT_SAMPLES:
289 return sr_sw_limits_config_set(&devc->limits, key, data);
290 case SR_CONF_ENABLED:
291 bval = g_variant_get_boolean(data);
292 gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0');
293 devc->output_enabled = bval;
295 case SR_CONF_VOLTAGE_TARGET:
296 ch = cg->channels->data;
298 dval = g_variant_get_double(data);
299 if (dval < devc->model->channels[channel].voltage[0]
300 || dval > devc->model->channels[channel].voltage[1])
302 gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval);
303 devc->config[channel].output_voltage_max = dval;
305 case SR_CONF_CURRENT_LIMIT:
306 ch = cg->channels->data;
308 dval = g_variant_get_double(data);
309 if (dval < devc->model->channels[channel].current[0]
310 || dval > devc->model->channels[channel].current[1])
312 gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval);
313 devc->config[channel].output_current_max = dval;
323 static int config_list(uint32_t key, GVariant **data,
324 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
326 const struct dev_context *devc;
327 const struct sr_channel *ch;
330 devc = (sdi) ? sdi->priv : NULL;
334 case SR_CONF_SCAN_OPTIONS:
335 case SR_CONF_DEVICE_OPTIONS:
336 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
338 case SR_CONF_CHANNEL_CONFIG:
339 *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
345 ch = cg->channels->data;
349 case SR_CONF_DEVICE_OPTIONS:
350 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
352 case SR_CONF_VOLTAGE_TARGET:
353 *data = std_gvar_min_max_step_array(
354 devc->model->channels[channel].voltage);
356 case SR_CONF_CURRENT_LIMIT:
357 *data = std_gvar_min_max_step_array(
358 devc->model->channels[channel].current);
368 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
370 struct dev_context *devc;
371 struct sr_serial_dev_inst *serial;
375 sr_sw_limits_acquisition_start(&devc->limits);
376 std_session_send_df_header(sdi);
378 devc->reply_pending = FALSE;
379 devc->req_sent_at = 0;
381 serial_source_add(sdi->session, serial, G_IO_IN, 100,
382 gpd_receive_data, (void *)sdi);
387 static struct sr_dev_driver gwinstek_gpd_driver_info = {
388 .name = "gwinstek-gpd",
389 .longname = "GW Instek GPD",
392 .cleanup = std_cleanup,
394 .dev_list = std_dev_list,
395 .dev_clear = std_dev_clear,
396 .config_get = config_get,
397 .config_set = config_set,
398 .config_list = config_list,
399 .dev_open = std_serial_dev_open,
400 .dev_close = std_serial_dev_close,
401 .dev_acquisition_start = dev_acquisition_start,
402 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
405 SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info);