]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gds-800/api.c
scpi-pps: don't break SCPI devices when scanning for HP-IB devices
[libsigrok.git] / src / hardware / gwinstek-gds-800 / api.c
CommitLineData
7c198f96
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Martin Lederhilger <martin.lederhilger@gmx.at>
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
b11afbb1 20#include <string.h>
7c198f96
ML
21#include "protocol.h"
22
b11afbb1
ML
23static const uint32_t scanopts[] = {
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
05199c0a 28static const uint32_t drvopts[] = {
b11afbb1 29 SR_CONF_OSCILLOSCOPE,
05199c0a
UH
30};
31
32static const uint32_t devopts[] = {
7c48d434 33 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
b11afbb1
ML
34 SR_CONF_SAMPLERATE | SR_CONF_GET,
35};
36
dd5c48a6 37static struct sr_dev_driver gwinstek_gds_800_driver_info;
7c198f96 38
b11afbb1 39static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
7c198f96 40{
b11afbb1
ML
41 struct dev_context *devc;
42 struct sr_dev_inst *sdi;
43 struct sr_scpi_hw_info *hw_info;
44 struct sr_channel_group *cg;
45
46 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
47 sr_info("Couldn't get IDN response.");
48 return NULL;
49 }
7c198f96 50
b11afbb1
ML
51 if (strcmp(hw_info->manufacturer, "GW") != 0 ||
52 strncmp(hw_info->model, "GDS-8", 5) != 0) {
53 sr_scpi_hw_info_free(hw_info);
54 return NULL;
55 }
7c198f96 56
b11afbb1 57 sdi = g_malloc0(sizeof(struct sr_dev_inst));
b11afbb1
ML
58 sdi->vendor = g_strdup(hw_info->manufacturer);
59 sdi->model = g_strdup(hw_info->model);
60 sdi->version = g_strdup(hw_info->firmware_version);
61 sdi->conn = scpi;
62 sdi->driver = &gwinstek_gds_800_driver_info;
63 sdi->inst_type = SR_INST_SCPI;
64 sdi->serial_num = g_strdup(hw_info->serial_number);
65 sdi->channels = NULL;
66 sdi->channel_groups = NULL;
67
68 sr_scpi_hw_info_free(hw_info);
69
70 devc = g_malloc0(sizeof(struct dev_context));
71 devc->frame_limit = 1;
d9251a2c
UH
72 devc->sample_rate = 0.0;
73 devc->df_started = FALSE;
b11afbb1
ML
74 sdi->priv = devc;
75
76 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
77 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "CH2");
78
79 cg = g_malloc0(sizeof(struct sr_channel_group));
80 cg->name = g_strdup("");
81 cg->channels = g_slist_append(cg->channels, g_slist_nth_data(sdi->channels, 0));
82 cg->channels = g_slist_append(cg->channels, g_slist_nth_data(sdi->channels, 1));
83 cg->priv = NULL;
84 sdi->channel_groups = g_slist_append(NULL, cg);
85
86 return sdi;
87}
7c198f96 88
b11afbb1
ML
89static GSList *scan(struct sr_dev_driver *di, GSList *options)
90{
91 return sr_scpi_scan(di->context, options, probe_device);
7c198f96
ML
92}
93
7c198f96
ML
94static int dev_open(struct sr_dev_inst *sdi)
95{
b11afbb1
ML
96 int ret;
97 struct sr_scpi_dev_inst *scpi = sdi->conn;
7c198f96 98
b11afbb1
ML
99 if ((ret = sr_scpi_open(scpi)) < 0) {
100 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
101 return SR_ERR;
102 }
7c198f96 103
7c198f96
ML
104 return SR_OK;
105}
106
107static int dev_close(struct sr_dev_inst *sdi)
108{
b11afbb1 109 struct sr_scpi_dev_inst *scpi;
7c198f96 110
b11afbb1 111 scpi = sdi->conn;
7c198f96 112
f1ba6b4b
UH
113 if (!scpi)
114 return SR_ERR_BUG;
115
116 return sr_scpi_close(scpi);
7c198f96
ML
117}
118
dd7a72ea
UH
119static int config_get(uint32_t key, GVariant **data,
120 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
7c198f96 121{
b11afbb1 122 struct dev_context *devc;
7c198f96 123
7c198f96
ML
124 (void)cg;
125
709468ba 126 if (!sdi)
b11afbb1
ML
127 return SR_ERR_ARG;
128
709468ba
UH
129 devc = sdi->priv;
130
7c198f96 131 switch (key) {
b11afbb1
ML
132 case SR_CONF_SAMPLERATE:
133 *data = g_variant_new_uint64(devc->sample_rate);
134 break;
7c48d434
VO
135 case SR_CONF_LIMIT_FRAMES:
136 *data = g_variant_new_uint64(devc->frame_limit);
137 break;
7c198f96
ML
138 default:
139 return SR_ERR_NA;
140 }
141
b11afbb1 142 return SR_OK;
7c198f96
ML
143}
144
dd7a72ea
UH
145static int config_set(uint32_t key, GVariant *data,
146 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
7c198f96 147{
b11afbb1 148 struct dev_context *devc;
7c198f96 149
7c198f96
ML
150 (void)cg;
151
b0baddef 152 if (!sdi)
b11afbb1
ML
153 return SR_ERR_ARG;
154
b0baddef
UH
155 devc = sdi->priv;
156
7c198f96 157 switch (key) {
b11afbb1
ML
158 case SR_CONF_LIMIT_FRAMES:
159 devc->frame_limit = g_variant_get_uint64(data);
160 break;
7c198f96 161 default:
b11afbb1 162 return SR_ERR_NA;
7c198f96
ML
163 }
164
b11afbb1 165 return SR_OK;
7c198f96
ML
166}
167
dd7a72ea
UH
168static int config_list(uint32_t key, GVariant **data,
169 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
7c198f96 170{
05199c0a 171 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
7c198f96
ML
172}
173
695dc859 174static int dev_acquisition_start(const struct sr_dev_inst *sdi)
7c198f96 175{
b11afbb1
ML
176 struct sr_scpi_dev_inst *scpi;
177 struct dev_context *devc;
178
b11afbb1
ML
179 scpi = sdi->conn;
180 devc = sdi->priv;
181
b11afbb1
ML
182 devc->state = START_ACQUISITION;
183 devc->cur_acq_frame = 0;
184
185 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
186 gwinstek_gds_800_receive_data, (void *)sdi);
7c198f96
ML
187
188 return SR_OK;
189}
190
695dc859 191static int dev_acquisition_stop(struct sr_dev_inst *sdi)
7c198f96 192{
b11afbb1
ML
193 struct sr_scpi_dev_inst *scpi;
194 struct dev_context *devc;
b11afbb1 195
b11afbb1
ML
196 scpi = sdi->conn;
197 devc = sdi->priv;
198
b11afbb1 199 if (devc->df_started) {
4c5f7006 200 std_session_send_df_frame_end(sdi);
bee2b016 201 std_session_send_df_end(sdi);
b11afbb1
ML
202 devc->df_started = FALSE;
203 }
7c198f96 204
b11afbb1 205 sr_scpi_source_remove(sdi->session, scpi);
7c198f96
ML
206
207 return SR_OK;
208}
209
dd5c48a6 210static struct sr_dev_driver gwinstek_gds_800_driver_info = {
7c198f96 211 .name = "gwinstek-gds-800",
b11afbb1 212 .longname = "GW Instek GDS-800 series",
7c198f96 213 .api_version = 1,
c2fdcc25 214 .init = std_init,
700d6b64 215 .cleanup = std_cleanup,
7c198f96 216 .scan = scan,
c01bf34c 217 .dev_list = std_dev_list,
f778bf02 218 .dev_clear = std_dev_clear,
7c198f96
ML
219 .config_get = config_get,
220 .config_set = config_set,
221 .config_list = config_list,
222 .dev_open = dev_open,
223 .dev_close = dev_close,
224 .dev_acquisition_start = dev_acquisition_start,
225 .dev_acquisition_stop = dev_acquisition_stop,
226 .context = NULL,
227};
dd5c48a6 228SR_REGISTER_DEV_DRIVER(gwinstek_gds_800_driver_info);