]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gds-800/api.c
Factor out std_session_send_df_end() helper.
[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
28static const uint32_t devopts[] = {
29 SR_CONF_OSCILLOSCOPE,
30 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
31 SR_CONF_SAMPLERATE | SR_CONF_GET,
32};
33
7c198f96
ML
34SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info;
35
36static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
37{
38 return std_init(sr_ctx, di, LOG_PREFIX);
39}
40
b11afbb1 41static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
7c198f96 42{
b11afbb1
ML
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;
47
48 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
49 sr_info("Couldn't get IDN response.");
50 return NULL;
51 }
7c198f96 52
b11afbb1
ML
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);
56 return NULL;
57 }
7c198f96 58
b11afbb1 59 sdi = g_malloc0(sizeof(struct sr_dev_inst));
b11afbb1
ML
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);
63 sdi->conn = scpi;
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);
67 sdi->channels = NULL;
68 sdi->channel_groups = NULL;
69
70 sr_scpi_hw_info_free(hw_info);
71
72 devc = g_malloc0(sizeof(struct dev_context));
73 devc->frame_limit = 1;
74 devc->sample_rate = 0.;
75 devc->df_started = FALSE;
76 sdi->priv = devc;
77
78 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
79 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "CH2");
80
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));
85 cg->priv = NULL;
86 sdi->channel_groups = g_slist_append(NULL, cg);
87
88 return sdi;
89}
7c198f96 90
b11afbb1
ML
91static GSList *scan(struct sr_dev_driver *di, GSList *options)
92{
93 return sr_scpi_scan(di->context, options, probe_device);
7c198f96
ML
94}
95
96static GSList *dev_list(const struct sr_dev_driver *di)
97{
98 return ((struct drv_context *)(di->context))->instances;
99}
100
101static int dev_clear(const struct sr_dev_driver *di)
102{
103 return std_dev_clear(di, NULL);
104}
105
106static int dev_open(struct sr_dev_inst *sdi)
107{
b11afbb1
ML
108 int ret;
109 struct sr_scpi_dev_inst *scpi = sdi->conn;
7c198f96 110
b11afbb1
ML
111 if ((ret = sr_scpi_open(scpi)) < 0) {
112 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
113 return SR_ERR;
114 }
7c198f96
ML
115
116 sdi->status = SR_ST_ACTIVE;
117
118 return SR_OK;
119}
120
121static int dev_close(struct sr_dev_inst *sdi)
122{
b11afbb1 123 struct sr_scpi_dev_inst *scpi;
7c198f96 124
b11afbb1
ML
125 if (sdi->status != SR_ST_ACTIVE)
126 return SR_ERR_DEV_CLOSED;
7c198f96 127
b11afbb1
ML
128 scpi = sdi->conn;
129 if (scpi) {
130 if (sr_scpi_close(scpi) < 0)
131 return SR_ERR;
132 sdi->status = SR_ST_INACTIVE;
133 }
7c198f96
ML
134
135 return SR_OK;
136}
137
138static int cleanup(const struct sr_dev_driver *di)
139{
140 dev_clear(di);
141
7c198f96
ML
142 return SR_OK;
143}
144
145static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
146 const struct sr_channel_group *cg)
147{
b11afbb1 148 struct dev_context *devc;
7c198f96 149
7c198f96
ML
150 (void)cg;
151
b11afbb1
ML
152 if (!sdi || !(devc = sdi->priv))
153 return SR_ERR_ARG;
154
7c198f96 155 switch (key) {
b11afbb1
ML
156 case SR_CONF_SAMPLERATE:
157 *data = g_variant_new_uint64(devc->sample_rate);
158 break;
7c198f96
ML
159 default:
160 return SR_ERR_NA;
161 }
162
b11afbb1 163 return SR_OK;
7c198f96
ML
164}
165
166static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
167 const struct sr_channel_group *cg)
168{
b11afbb1 169 struct dev_context *devc;
7c198f96 170
7c198f96
ML
171 (void)cg;
172
b11afbb1
ML
173 if (!sdi || !(devc = sdi->priv))
174 return SR_ERR_ARG;
175
7c198f96
ML
176 if (sdi->status != SR_ST_ACTIVE)
177 return SR_ERR_DEV_CLOSED;
178
7c198f96 179 switch (key) {
b11afbb1
ML
180 case SR_CONF_LIMIT_FRAMES:
181 devc->frame_limit = g_variant_get_uint64(data);
182 break;
7c198f96 183 default:
b11afbb1 184 return SR_ERR_NA;
7c198f96
ML
185 }
186
b11afbb1 187 return SR_OK;
7c198f96
ML
188}
189
190static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
191 const struct sr_channel_group *cg)
192{
7c198f96 193 (void)sdi;
7c198f96
ML
194 (void)cg;
195
7c198f96 196 switch (key) {
b11afbb1
ML
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));
200 return SR_OK;
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));
204 return SR_OK;
7c198f96
ML
205 default:
206 return SR_ERR_NA;
207 }
208
b11afbb1 209 return SR_OK;
7c198f96
ML
210}
211
b11afbb1 212static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
7c198f96 213{
b11afbb1
ML
214 struct sr_scpi_dev_inst *scpi;
215 struct dev_context *devc;
216
7c198f96
ML
217 (void)cb_data;
218
b11afbb1
ML
219 scpi = sdi->conn;
220 devc = sdi->priv;
221
7c198f96
ML
222 if (sdi->status != SR_ST_ACTIVE)
223 return SR_ERR_DEV_CLOSED;
224
b11afbb1
ML
225 devc->state = START_ACQUISITION;
226 devc->cur_acq_frame = 0;
227
228 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
229 gwinstek_gds_800_receive_data, (void *)sdi);
7c198f96
ML
230
231 return SR_OK;
232}
233
234static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
235{
b11afbb1
ML
236 struct sr_scpi_dev_inst *scpi;
237 struct dev_context *devc;
238 struct sr_datafeed_packet packet;
239
7c198f96
ML
240 (void)cb_data;
241
b11afbb1
ML
242 scpi = sdi->conn;
243 devc = sdi->priv;
244
245 if (sdi->status != SR_ST_ACTIVE) {
246 sr_err("Device inactive, can't stop acquisition.");
247 return SR_ERR;
248 }
249
250 if (devc->df_started) {
251 packet.type = SR_DF_FRAME_END;
252 sr_session_send(sdi, &packet);
253
3be42bc2 254 std_session_send_df_end(sdi, LOG_PREFIX);
b11afbb1
ML
255
256 devc->df_started = FALSE;
257 }
7c198f96 258
b11afbb1 259 sr_scpi_source_remove(sdi->session, scpi);
7c198f96
ML
260
261 return SR_OK;
262}
263
264SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = {
265 .name = "gwinstek-gds-800",
b11afbb1 266 .longname = "GW Instek GDS-800 series",
7c198f96
ML
267 .api_version = 1,
268 .init = init,
269 .cleanup = cleanup,
270 .scan = scan,
271 .dev_list = dev_list,
272 .dev_clear = dev_clear,
273 .config_get = config_get,
274 .config_set = config_set,
275 .config_list = config_list,
276 .dev_open = dev_open,
277 .dev_close = dev_close,
278 .dev_acquisition_start = dev_acquisition_start,
279 .dev_acquisition_stop = dev_acquisition_stop,
280 .context = NULL,
281};