]>
Commit | Line | Data |
---|---|---|
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 |
23 | static const uint32_t scanopts[] = { |
24 | SR_CONF_CONN, | |
25 | SR_CONF_SERIALCOMM, | |
26 | }; | |
27 | ||
28 | static 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 |
34 | SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info; |
35 | ||
36 | static 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 | 41 | static 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 |
91 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
92 | { | |
93 | return sr_scpi_scan(di->context, options, probe_device); | |
7c198f96 ML |
94 | } |
95 | ||
96 | static GSList *dev_list(const struct sr_dev_driver *di) | |
97 | { | |
98 | return ((struct drv_context *)(di->context))->instances; | |
99 | } | |
100 | ||
7c198f96 ML |
101 | static int dev_open(struct sr_dev_inst *sdi) |
102 | { | |
b11afbb1 ML |
103 | int ret; |
104 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
7c198f96 | 105 | |
b11afbb1 ML |
106 | if ((ret = sr_scpi_open(scpi)) < 0) { |
107 | sr_err("Failed to open SCPI device: %s.", sr_strerror(ret)); | |
108 | return SR_ERR; | |
109 | } | |
7c198f96 ML |
110 | |
111 | sdi->status = SR_ST_ACTIVE; | |
112 | ||
113 | return SR_OK; | |
114 | } | |
115 | ||
116 | static int dev_close(struct sr_dev_inst *sdi) | |
117 | { | |
b11afbb1 | 118 | struct sr_scpi_dev_inst *scpi; |
7c198f96 | 119 | |
b11afbb1 ML |
120 | if (sdi->status != SR_ST_ACTIVE) |
121 | return SR_ERR_DEV_CLOSED; | |
7c198f96 | 122 | |
b11afbb1 ML |
123 | scpi = sdi->conn; |
124 | if (scpi) { | |
125 | if (sr_scpi_close(scpi) < 0) | |
126 | return SR_ERR; | |
127 | sdi->status = SR_ST_INACTIVE; | |
128 | } | |
7c198f96 ML |
129 | |
130 | return SR_OK; | |
131 | } | |
132 | ||
7c198f96 ML |
133 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
134 | const struct sr_channel_group *cg) | |
135 | { | |
b11afbb1 | 136 | struct dev_context *devc; |
7c198f96 | 137 | |
7c198f96 ML |
138 | (void)cg; |
139 | ||
b11afbb1 ML |
140 | if (!sdi || !(devc = sdi->priv)) |
141 | return SR_ERR_ARG; | |
142 | ||
7c198f96 | 143 | switch (key) { |
b11afbb1 ML |
144 | case SR_CONF_SAMPLERATE: |
145 | *data = g_variant_new_uint64(devc->sample_rate); | |
146 | break; | |
7c198f96 ML |
147 | default: |
148 | return SR_ERR_NA; | |
149 | } | |
150 | ||
b11afbb1 | 151 | return SR_OK; |
7c198f96 ML |
152 | } |
153 | ||
154 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
155 | const struct sr_channel_group *cg) | |
156 | { | |
b11afbb1 | 157 | struct dev_context *devc; |
7c198f96 | 158 | |
7c198f96 ML |
159 | (void)cg; |
160 | ||
b11afbb1 ML |
161 | if (!sdi || !(devc = sdi->priv)) |
162 | return SR_ERR_ARG; | |
163 | ||
7c198f96 ML |
164 | if (sdi->status != SR_ST_ACTIVE) |
165 | return SR_ERR_DEV_CLOSED; | |
166 | ||
7c198f96 | 167 | switch (key) { |
b11afbb1 ML |
168 | case SR_CONF_LIMIT_FRAMES: |
169 | devc->frame_limit = g_variant_get_uint64(data); | |
170 | break; | |
7c198f96 | 171 | default: |
b11afbb1 | 172 | return SR_ERR_NA; |
7c198f96 ML |
173 | } |
174 | ||
b11afbb1 | 175 | return SR_OK; |
7c198f96 ML |
176 | } |
177 | ||
178 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
179 | const struct sr_channel_group *cg) | |
180 | { | |
7c198f96 | 181 | (void)sdi; |
7c198f96 ML |
182 | (void)cg; |
183 | ||
7c198f96 | 184 | switch (key) { |
b11afbb1 ML |
185 | case SR_CONF_SCAN_OPTIONS: |
186 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
187 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
188 | return SR_OK; | |
189 | case SR_CONF_DEVICE_OPTIONS: | |
190 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
191 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
192 | return SR_OK; | |
7c198f96 ML |
193 | default: |
194 | return SR_ERR_NA; | |
195 | } | |
196 | ||
b11afbb1 | 197 | return SR_OK; |
7c198f96 ML |
198 | } |
199 | ||
695dc859 | 200 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
7c198f96 | 201 | { |
b11afbb1 ML |
202 | struct sr_scpi_dev_inst *scpi; |
203 | struct dev_context *devc; | |
204 | ||
b11afbb1 ML |
205 | scpi = sdi->conn; |
206 | devc = sdi->priv; | |
207 | ||
7c198f96 ML |
208 | if (sdi->status != SR_ST_ACTIVE) |
209 | return SR_ERR_DEV_CLOSED; | |
210 | ||
b11afbb1 ML |
211 | devc->state = START_ACQUISITION; |
212 | devc->cur_acq_frame = 0; | |
213 | ||
214 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, | |
215 | gwinstek_gds_800_receive_data, (void *)sdi); | |
7c198f96 ML |
216 | |
217 | return SR_OK; | |
218 | } | |
219 | ||
695dc859 | 220 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
7c198f96 | 221 | { |
b11afbb1 ML |
222 | struct sr_scpi_dev_inst *scpi; |
223 | struct dev_context *devc; | |
224 | struct sr_datafeed_packet packet; | |
225 | ||
b11afbb1 ML |
226 | scpi = sdi->conn; |
227 | devc = sdi->priv; | |
228 | ||
229 | if (sdi->status != SR_ST_ACTIVE) { | |
230 | sr_err("Device inactive, can't stop acquisition."); | |
231 | return SR_ERR; | |
232 | } | |
233 | ||
234 | if (devc->df_started) { | |
235 | packet.type = SR_DF_FRAME_END; | |
236 | sr_session_send(sdi, &packet); | |
237 | ||
3be42bc2 | 238 | std_session_send_df_end(sdi, LOG_PREFIX); |
b11afbb1 ML |
239 | |
240 | devc->df_started = FALSE; | |
241 | } | |
7c198f96 | 242 | |
b11afbb1 | 243 | sr_scpi_source_remove(sdi->session, scpi); |
7c198f96 ML |
244 | |
245 | return SR_OK; | |
246 | } | |
247 | ||
248 | SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = { | |
249 | .name = "gwinstek-gds-800", | |
b11afbb1 | 250 | .longname = "GW Instek GDS-800 series", |
7c198f96 ML |
251 | .api_version = 1, |
252 | .init = init, | |
700d6b64 | 253 | .cleanup = std_cleanup, |
7c198f96 ML |
254 | .scan = scan, |
255 | .dev_list = dev_list, | |
7c198f96 ML |
256 | .config_get = config_get, |
257 | .config_set = config_set, | |
258 | .config_list = config_list, | |
259 | .dev_open = dev_open, | |
260 | .dev_close = dev_close, | |
261 | .dev_acquisition_start = dev_acquisition_start, | |
262 | .dev_acquisition_stop = dev_acquisition_stop, | |
263 | .context = NULL, | |
264 | }; |