2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Martin Lederhilger <martin.lederhilger@gmx.at>
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/>.
23 static const uint32_t scanopts[] = {
28 static const uint32_t devopts[] = {
30 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
31 SR_CONF_SAMPLERATE | SR_CONF_GET,
34 SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info;
36 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
38 return std_init(sr_ctx, di, LOG_PREFIX);
41 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
43 struct dev_context *devc;
44 struct sr_dev_inst *sdi;
45 struct sr_scpi_hw_info *hw_info;
46 struct sr_channel_group *cg;
48 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
49 sr_info("Couldn't get IDN response.");
53 if (strcmp(hw_info->manufacturer, "GW") != 0 ||
54 strncmp(hw_info->model, "GDS-8", 5) != 0) {
55 sr_scpi_hw_info_free(hw_info);
59 sdi = g_malloc0(sizeof(struct sr_dev_inst));
60 sdi->vendor = g_strdup(hw_info->manufacturer);
61 sdi->model = g_strdup(hw_info->model);
62 sdi->version = g_strdup(hw_info->firmware_version);
64 sdi->driver = &gwinstek_gds_800_driver_info;
65 sdi->inst_type = SR_INST_SCPI;
66 sdi->serial_num = g_strdup(hw_info->serial_number);
68 sdi->channel_groups = NULL;
70 sr_scpi_hw_info_free(hw_info);
72 devc = g_malloc0(sizeof(struct dev_context));
73 devc->frame_limit = 1;
74 devc->sample_rate = 0.;
75 devc->df_started = FALSE;
78 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
79 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "CH2");
81 cg = g_malloc0(sizeof(struct sr_channel_group));
82 cg->name = g_strdup("");
83 cg->channels = g_slist_append(cg->channels, g_slist_nth_data(sdi->channels, 0));
84 cg->channels = g_slist_append(cg->channels, g_slist_nth_data(sdi->channels, 1));
86 sdi->channel_groups = g_slist_append(NULL, cg);
91 static GSList *scan(struct sr_dev_driver *di, GSList *options)
93 return sr_scpi_scan(di->context, options, probe_device);
96 static GSList *dev_list(const struct sr_dev_driver *di)
98 return ((struct drv_context *)(di->context))->instances;
101 static int dev_clear(const struct sr_dev_driver *di)
103 return std_dev_clear(di, NULL);
106 static int dev_open(struct sr_dev_inst *sdi)
109 struct sr_scpi_dev_inst *scpi = sdi->conn;
111 if ((ret = sr_scpi_open(scpi)) < 0) {
112 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
116 sdi->status = SR_ST_ACTIVE;
121 static int dev_close(struct sr_dev_inst *sdi)
123 struct sr_scpi_dev_inst *scpi;
125 if (sdi->status != SR_ST_ACTIVE)
126 return SR_ERR_DEV_CLOSED;
130 if (sr_scpi_close(scpi) < 0)
132 sdi->status = SR_ST_INACTIVE;
138 static int cleanup(const struct sr_dev_driver *di)
145 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
146 const struct sr_channel_group *cg)
148 struct dev_context *devc;
152 if (!sdi || !(devc = sdi->priv))
156 case SR_CONF_SAMPLERATE:
157 *data = g_variant_new_uint64(devc->sample_rate);
166 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
167 const struct sr_channel_group *cg)
169 struct dev_context *devc;
173 if (!sdi || !(devc = sdi->priv))
176 if (sdi->status != SR_ST_ACTIVE)
177 return SR_ERR_DEV_CLOSED;
180 case SR_CONF_LIMIT_FRAMES:
181 devc->frame_limit = g_variant_get_uint64(data);
190 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
191 const struct sr_channel_group *cg)
197 case SR_CONF_SCAN_OPTIONS:
198 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
199 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
201 case SR_CONF_DEVICE_OPTIONS:
202 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
203 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
212 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
214 struct sr_scpi_dev_inst *scpi;
215 struct dev_context *devc;
220 if (sdi->status != SR_ST_ACTIVE)
221 return SR_ERR_DEV_CLOSED;
223 devc->state = START_ACQUISITION;
224 devc->cur_acq_frame = 0;
226 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
227 gwinstek_gds_800_receive_data, (void *)sdi);
232 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
234 struct sr_scpi_dev_inst *scpi;
235 struct dev_context *devc;
236 struct sr_datafeed_packet packet;
241 if (sdi->status != SR_ST_ACTIVE) {
242 sr_err("Device inactive, can't stop acquisition.");
246 if (devc->df_started) {
247 packet.type = SR_DF_FRAME_END;
248 sr_session_send(sdi, &packet);
250 std_session_send_df_end(sdi, LOG_PREFIX);
252 devc->df_started = FALSE;
255 sr_scpi_source_remove(sdi->session, scpi);
260 SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = {
261 .name = "gwinstek-gds-800",
262 .longname = "GW Instek GDS-800 series",
267 .dev_list = dev_list,
268 .dev_clear = dev_clear,
269 .config_get = config_get,
270 .config_set = config_set,
271 .config_list = config_list,
272 .dev_open = dev_open,
273 .dev_close = dev_close,
274 .dev_acquisition_start = dev_acquisition_start,
275 .dev_acquisition_stop = dev_acquisition_stop,