]>
Commit | Line | Data |
---|---|---|
06a3e78a DJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com> | |
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 | ||
6ec6c43b | 20 | #include <config.h> |
13f2b9d7 | 21 | #include <stdlib.h> |
5a1afc09 | 22 | #include "scpi.h" |
06a3e78a DJ |
23 | #include "protocol.h" |
24 | ||
13f2b9d7 DJ |
25 | #define SERIALCOMM "115200/8n1/flow=1" |
26 | ||
e9a62139 | 27 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info; |
e9a62139 DJ |
28 | |
29 | static const char *manufacturers[] = { | |
30 | "HAMEG", | |
da1726cc | 31 | "Rohde&Schwarz", |
e9a62139 DJ |
32 | }; |
33 | ||
6ec3ef9b AJ |
34 | static const uint32_t drvopts[] = { |
35 | SR_CONF_OSCILLOSCOPE, | |
36 | }; | |
37 | ||
584560f1 | 38 | static const uint32_t scanopts[] = { |
13f2b9d7 DJ |
39 | SR_CONF_CONN, |
40 | SR_CONF_SERIALCOMM, | |
41 | }; | |
42 | ||
719eff68 | 43 | enum { |
61e77667 UH |
44 | CG_INVALID = -1, |
45 | CG_NONE, | |
46 | CG_ANALOG, | |
47 | CG_DIGITAL, | |
719eff68 UH |
48 | }; |
49 | ||
e9a62139 DJ |
50 | static int check_manufacturer(const char *manufacturer) |
51 | { | |
52 | unsigned int i; | |
53 | ||
0a1f7b09 | 54 | for (i = 0; i < ARRAY_SIZE(manufacturers); i++) |
e9a62139 DJ |
55 | if (!strcmp(manufacturer, manufacturers[i])) |
56 | return SR_OK; | |
57 | ||
58 | return SR_ERR; | |
59 | } | |
60 | ||
ca28abd6 | 61 | static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi) |
e9a62139 DJ |
62 | { |
63 | struct sr_dev_inst *sdi; | |
64 | struct dev_context *devc; | |
65 | struct sr_scpi_hw_info *hw_info; | |
e9a62139 DJ |
66 | |
67 | sdi = NULL; | |
68 | devc = NULL; | |
e9a62139 DJ |
69 | hw_info = NULL; |
70 | ||
e9a62139 DJ |
71 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { |
72 | sr_info("Couldn't get IDN response."); | |
73 | goto fail; | |
74 | } | |
75 | ||
76 | if (check_manufacturer(hw_info->manufacturer) != SR_OK) | |
77 | goto fail; | |
78 | ||
aac29cc1 | 79 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
80 | sdi->vendor = g_strdup(hw_info->manufacturer); |
81 | sdi->model = g_strdup(hw_info->model); | |
82 | sdi->version = g_strdup(hw_info->firmware_version); | |
83 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
4f840ce9 | 84 | sdi->driver = &hameg_hmo_driver_info; |
b33db61c SA |
85 | sdi->inst_type = SR_INST_SCPI; |
86 | sdi->conn = scpi; | |
b33db61c | 87 | |
e9a62139 DJ |
88 | sr_scpi_hw_info_free(hw_info); |
89 | hw_info = NULL; | |
90 | ||
f57d8ffe | 91 | devc = g_malloc0(sizeof(struct dev_context)); |
e9a62139 | 92 | |
e9a62139 | 93 | sdi->priv = devc; |
e9a62139 DJ |
94 | |
95 | if (hmo_init_device(sdi) != SR_OK) | |
96 | goto fail; | |
97 | ||
98 | return sdi; | |
99 | ||
100 | fail: | |
101 | if (hw_info) | |
102 | sr_scpi_hw_info_free(hw_info); | |
e9a62139 DJ |
103 | if (sdi) |
104 | sr_dev_inst_free(sdi); | |
b1f83103 | 105 | g_free(devc); |
e9a62139 DJ |
106 | |
107 | return NULL; | |
108 | } | |
109 | ||
4f840ce9 | 110 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
06a3e78a | 111 | { |
41812aca | 112 | return sr_scpi_scan(di->context, options, hmo_probe_serial_device); |
06a3e78a DJ |
113 | } |
114 | ||
13f2b9d7 DJ |
115 | static void clear_helper(void *priv) |
116 | { | |
13f2b9d7 | 117 | struct dev_context *devc; |
13f2b9d7 DJ |
118 | |
119 | devc = priv; | |
13f2b9d7 | 120 | |
719eff68 | 121 | hmo_scope_state_free(devc->model_state); |
13f2b9d7 | 122 | |
13f2b9d7 DJ |
123 | g_free(devc->analog_groups); |
124 | g_free(devc->digital_groups); | |
125 | ||
126 | g_free(devc); | |
127 | } | |
128 | ||
4f840ce9 | 129 | static int dev_clear(const struct sr_dev_driver *di) |
06a3e78a | 130 | { |
13f2b9d7 | 131 | return std_dev_clear(di, clear_helper); |
06a3e78a DJ |
132 | } |
133 | ||
134 | static int dev_open(struct sr_dev_inst *sdi) | |
135 | { | |
23f43dff | 136 | if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) |
13f2b9d7 | 137 | return SR_ERR; |
06a3e78a | 138 | |
719eff68 | 139 | if (hmo_scope_state_get(sdi) != SR_OK) |
13f2b9d7 | 140 | return SR_ERR; |
06a3e78a DJ |
141 | |
142 | sdi->status = SR_ST_ACTIVE; | |
143 | ||
144 | return SR_OK; | |
145 | } | |
146 | ||
147 | static int dev_close(struct sr_dev_inst *sdi) | |
148 | { | |
13f2b9d7 DJ |
149 | if (sdi->status == SR_ST_INACTIVE) |
150 | return SR_OK; | |
06a3e78a | 151 | |
23f43dff | 152 | sr_scpi_close(sdi->conn); |
06a3e78a DJ |
153 | |
154 | sdi->status = SR_ST_INACTIVE; | |
155 | ||
156 | return SR_OK; | |
157 | } | |
158 | ||
660e398f | 159 | static int check_channel_group(struct dev_context *devc, |
53b4680f | 160 | const struct sr_channel_group *cg) |
13f2b9d7 DJ |
161 | { |
162 | unsigned int i; | |
329733d9 | 163 | const struct scope_config *model; |
13f2b9d7 DJ |
164 | |
165 | model = devc->model_config; | |
166 | ||
53b4680f | 167 | if (!cg) |
61e77667 | 168 | return CG_NONE; |
13f2b9d7 | 169 | |
0a1f7b09 | 170 | for (i = 0; i < model->analog_channels; i++) |
562b7ae5 | 171 | if (cg == devc->analog_groups[i]) |
61e77667 | 172 | return CG_ANALOG; |
13f2b9d7 | 173 | |
0a1f7b09 | 174 | for (i = 0; i < model->digital_pods; i++) |
562b7ae5 | 175 | if (cg == devc->digital_groups[i]) |
61e77667 | 176 | return CG_DIGITAL; |
13f2b9d7 | 177 | |
660e398f | 178 | sr_err("Invalid channel group specified."); |
89280b1a | 179 | |
61e77667 | 180 | return CG_INVALID; |
13f2b9d7 DJ |
181 | } |
182 | ||
584560f1 | 183 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 184 | const struct sr_channel_group *cg) |
06a3e78a | 185 | { |
61e77667 | 186 | int ret, cg_type; |
13f2b9d7 | 187 | unsigned int i; |
13f2b9d7 | 188 | struct dev_context *devc; |
329733d9 | 189 | const struct scope_config *model; |
14a2f74d | 190 | struct scope_state *state; |
13f2b9d7 DJ |
191 | |
192 | if (!sdi || !(devc = sdi->priv)) | |
193 | return SR_ERR_ARG; | |
194 | ||
61e77667 | 195 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 | 196 | return SR_ERR; |
06a3e78a | 197 | |
13f2b9d7 DJ |
198 | ret = SR_ERR_NA; |
199 | model = devc->model_config; | |
14a2f74d | 200 | state = devc->model_state; |
06a3e78a | 201 | |
06a3e78a | 202 | switch (key) { |
bf622e6d | 203 | case SR_CONF_NUM_HDIV: |
13f2b9d7 DJ |
204 | *data = g_variant_new_int32(model->num_xdivs); |
205 | ret = SR_OK; | |
206 | break; | |
3fd2dca2 DJ |
207 | case SR_CONF_TIMEBASE: |
208 | *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0], | |
209 | (*model->timebases)[state->timebase][1]); | |
210 | ret = SR_OK; | |
211 | break; | |
13f2b9d7 | 212 | case SR_CONF_NUM_VDIV: |
61e77667 | 213 | if (cg_type == CG_NONE) { |
660e398f UH |
214 | sr_err("No channel group specified."); |
215 | return SR_ERR_CHANNEL_GROUP; | |
61e77667 | 216 | } else if (cg_type == CG_ANALOG) { |
0a1f7b09 | 217 | for (i = 0; i < model->analog_channels; i++) { |
562b7ae5 | 218 | if (cg != devc->analog_groups[i]) |
082972e8 UH |
219 | continue; |
220 | *data = g_variant_new_int32(model->num_ydivs); | |
221 | ret = SR_OK; | |
222 | break; | |
13f2b9d7 DJ |
223 | } |
224 | ||
3fd2dca2 DJ |
225 | } else { |
226 | ret = SR_ERR_NA; | |
227 | } | |
228 | break; | |
229 | case SR_CONF_VDIV: | |
61e77667 UH |
230 | if (cg_type == CG_NONE) { |
231 | sr_err("No channel group specified."); | |
232 | return SR_ERR_CHANNEL_GROUP; | |
233 | } else if (cg_type == CG_ANALOG) { | |
0a1f7b09 | 234 | for (i = 0; i < model->analog_channels; i++) { |
562b7ae5 | 235 | if (cg != devc->analog_groups[i]) |
3fd2dca2 DJ |
236 | continue; |
237 | *data = g_variant_new("(tt)", | |
238 | (*model->vdivs)[state->analog_channels[i].vdiv][0], | |
239 | (*model->vdivs)[state->analog_channels[i].vdiv][1]); | |
240 | ret = SR_OK; | |
241 | break; | |
242 | } | |
243 | ||
ccf14618 DJ |
244 | } else { |
245 | ret = SR_ERR_NA; | |
246 | } | |
247 | break; | |
248 | case SR_CONF_TRIGGER_SOURCE: | |
249 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
250 | ret = SR_OK; | |
251 | break; | |
3fd2dca2 DJ |
252 | case SR_CONF_TRIGGER_SLOPE: |
253 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
254 | ret = SR_OK; | |
255 | break; | |
256 | case SR_CONF_HORIZ_TRIGGERPOS: | |
257 | *data = g_variant_new_double(state->horiz_triggerpos); | |
258 | ret = SR_OK; | |
259 | break; | |
ccf14618 | 260 | case SR_CONF_COUPLING: |
61e77667 | 261 | if (cg_type == CG_NONE) { |
660e398f UH |
262 | sr_err("No channel group specified."); |
263 | return SR_ERR_CHANNEL_GROUP; | |
61e77667 | 264 | } else if (cg_type == CG_ANALOG) { |
0a1f7b09 | 265 | for (i = 0; i < model->analog_channels; i++) { |
562b7ae5 | 266 | if (cg != devc->analog_groups[i]) |
ccf14618 DJ |
267 | continue; |
268 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]); | |
269 | ret = SR_OK; | |
270 | break; | |
271 | } | |
272 | ||
13f2b9d7 DJ |
273 | } else { |
274 | ret = SR_ERR_NA; | |
275 | } | |
276 | break; | |
14a2f74d DJ |
277 | case SR_CONF_SAMPLERATE: |
278 | *data = g_variant_new_uint64(state->sample_rate); | |
279 | ret = SR_OK; | |
280 | break; | |
06a3e78a | 281 | default: |
13f2b9d7 | 282 | ret = SR_ERR_NA; |
06a3e78a DJ |
283 | } |
284 | ||
285 | return ret; | |
286 | } | |
287 | ||
13f2b9d7 DJ |
288 | static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n) |
289 | { | |
290 | unsigned int i; | |
13f2b9d7 DJ |
291 | GVariant *rational[2]; |
292 | GVariantBuilder gvb; | |
293 | ||
294 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
295 | ||
296 | for (i = 0; i < n; i++) { | |
297 | rational[0] = g_variant_new_uint64((*array)[i][0]); | |
298 | rational[1] = g_variant_new_uint64((*array)[i][1]); | |
299 | ||
89280b1a | 300 | /* FIXME: Valgrind reports a memory leak here. */ |
13f2b9d7 DJ |
301 | g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); |
302 | } | |
303 | ||
304 | return g_variant_builder_end(&gvb); | |
305 | } | |
306 | ||
584560f1 | 307 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 308 | const struct sr_channel_group *cg) |
06a3e78a | 309 | { |
61e77667 | 310 | int ret, cg_type; |
89280b1a | 311 | unsigned int i, j; |
965b463d | 312 | char command[MAX_COMMAND_SIZE], float_str[30]; |
13f2b9d7 | 313 | struct dev_context *devc; |
329733d9 | 314 | const struct scope_config *model; |
13f2b9d7 | 315 | struct scope_state *state; |
89280b1a | 316 | const char *tmp; |
ca9b9f48 | 317 | uint64_t p, q; |
89280b1a | 318 | double tmp_d; |
23e1ea7a | 319 | gboolean update_sample_rate; |
06a3e78a | 320 | |
13f2b9d7 DJ |
321 | if (!sdi || !(devc = sdi->priv)) |
322 | return SR_ERR_ARG; | |
323 | ||
61e77667 | 324 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 DJ |
325 | return SR_ERR; |
326 | ||
327 | model = devc->model_config; | |
328 | state = devc->model_state; | |
23e1ea7a | 329 | update_sample_rate = FALSE; |
13f2b9d7 DJ |
330 | |
331 | ret = SR_ERR_NA; | |
06a3e78a | 332 | |
06a3e78a | 333 | switch (key) { |
13f2b9d7 DJ |
334 | case SR_CONF_LIMIT_FRAMES: |
335 | devc->frame_limit = g_variant_get_uint64(data); | |
336 | ret = SR_OK; | |
337 | break; | |
13f2b9d7 | 338 | case SR_CONF_TRIGGER_SOURCE: |
13f2b9d7 | 339 | tmp = g_variant_get_string(data, NULL); |
13f2b9d7 | 340 | for (i = 0; (*model->trigger_sources)[i]; i++) { |
082972e8 UH |
341 | if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0) |
342 | continue; | |
343 | state->trigger_source = i; | |
344 | g_snprintf(command, sizeof(command), | |
345 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
346 | (*model->trigger_sources)[i]); | |
13f2b9d7 | 347 | |
082972e8 UH |
348 | ret = sr_scpi_send(sdi->conn, command); |
349 | break; | |
13f2b9d7 | 350 | } |
89280b1a | 351 | break; |
13f2b9d7 | 352 | case SR_CONF_VDIV: |
61e77667 | 353 | if (cg_type == CG_NONE) { |
660e398f UH |
354 | sr_err("No channel group specified."); |
355 | return SR_ERR_CHANNEL_GROUP; | |
13f2b9d7 DJ |
356 | } |
357 | ||
358 | g_variant_get(data, "(tt)", &p, &q); | |
359 | ||
360 | for (i = 0; i < model->num_vdivs; i++) { | |
082972e8 UH |
361 | if (p != (*model->vdivs)[i][0] || |
362 | q != (*model->vdivs)[i][1]) | |
363 | continue; | |
0a1f7b09 | 364 | for (j = 1; j <= model->analog_channels; j++) { |
562b7ae5 | 365 | if (cg != devc->analog_groups[j - 1]) |
082972e8 | 366 | continue; |
8de2dc3b | 367 | state->analog_channels[j - 1].vdiv = i; |
965b463d | 368 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); |
082972e8 UH |
369 | g_snprintf(command, sizeof(command), |
370 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
965b463d | 371 | j, float_str); |
082972e8 UH |
372 | |
373 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
374 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
375 | return SR_ERR; | |
13f2b9d7 | 376 | |
13f2b9d7 DJ |
377 | break; |
378 | } | |
082972e8 UH |
379 | |
380 | ret = SR_OK; | |
381 | break; | |
13f2b9d7 | 382 | } |
89280b1a | 383 | break; |
13f2b9d7 | 384 | case SR_CONF_TIMEBASE: |
13f2b9d7 DJ |
385 | g_variant_get(data, "(tt)", &p, &q); |
386 | ||
387 | for (i = 0; i < model->num_timebases; i++) { | |
082972e8 UH |
388 | if (p != (*model->timebases)[i][0] || |
389 | q != (*model->timebases)[i][1]) | |
390 | continue; | |
8de2dc3b | 391 | state->timebase = i; |
965b463d | 392 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); |
082972e8 UH |
393 | g_snprintf(command, sizeof(command), |
394 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
965b463d | 395 | float_str); |
13f2b9d7 | 396 | |
082972e8 | 397 | ret = sr_scpi_send(sdi->conn, command); |
23e1ea7a | 398 | update_sample_rate = TRUE; |
082972e8 | 399 | break; |
13f2b9d7 | 400 | } |
89280b1a | 401 | break; |
13f2b9d7 | 402 | case SR_CONF_HORIZ_TRIGGERPOS: |
89280b1a | 403 | tmp_d = g_variant_get_double(data); |
13f2b9d7 | 404 | |
89280b1a | 405 | if (tmp_d < 0.0 || tmp_d > 1.0) |
13f2b9d7 DJ |
406 | return SR_ERR; |
407 | ||
422a1c0d DJ |
408 | state->horiz_triggerpos = tmp_d; |
409 | tmp_d = -(tmp_d - 0.5) * | |
410 | ((double) (*model->timebases)[state->timebase][0] / | |
411 | (*model->timebases)[state->timebase][1]) | |
412 | * model->num_xdivs; | |
413 | ||
414 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); | |
13f2b9d7 DJ |
415 | g_snprintf(command, sizeof(command), |
416 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
422a1c0d | 417 | float_str); |
13f2b9d7 DJ |
418 | |
419 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 420 | break; |
13f2b9d7 | 421 | case SR_CONF_TRIGGER_SLOPE: |
ca9b9f48 | 422 | tmp = g_variant_get_string(data, NULL); |
e3abd15d SB |
423 | for (i = 0; (*model->trigger_slopes)[i]; i++) { |
424 | if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0) | |
425 | continue; | |
426 | state->trigger_slope = i; | |
427 | g_snprintf(command, sizeof(command), | |
428 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
429 | (*model->trigger_slopes)[i]); | |
13f2b9d7 | 430 | |
e3abd15d SB |
431 | ret = sr_scpi_send(sdi->conn, command); |
432 | break; | |
433 | } | |
89280b1a | 434 | break; |
13f2b9d7 | 435 | case SR_CONF_COUPLING: |
61e77667 | 436 | if (cg_type == CG_NONE) { |
660e398f UH |
437 | sr_err("No channel group specified."); |
438 | return SR_ERR_CHANNEL_GROUP; | |
13f2b9d7 DJ |
439 | } |
440 | ||
441 | tmp = g_variant_get_string(data, NULL); | |
442 | ||
443 | for (i = 0; (*model->coupling_options)[i]; i++) { | |
082972e8 UH |
444 | if (strcmp(tmp, (*model->coupling_options)[i]) != 0) |
445 | continue; | |
0a1f7b09 | 446 | for (j = 1; j <= model->analog_channels; j++) { |
562b7ae5 | 447 | if (cg != devc->analog_groups[j - 1]) |
082972e8 UH |
448 | continue; |
449 | state->analog_channels[j-1].coupling = i; | |
13f2b9d7 | 450 | |
082972e8 UH |
451 | g_snprintf(command, sizeof(command), |
452 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
453 | j, tmp); | |
454 | ||
455 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
456 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
457 | return SR_ERR; | |
13f2b9d7 DJ |
458 | break; |
459 | } | |
082972e8 UH |
460 | |
461 | ret = SR_OK; | |
462 | break; | |
13f2b9d7 | 463 | } |
89280b1a | 464 | break; |
06a3e78a DJ |
465 | default: |
466 | ret = SR_ERR_NA; | |
13f2b9d7 | 467 | break; |
06a3e78a DJ |
468 | } |
469 | ||
13f2b9d7 DJ |
470 | if (ret == SR_OK) |
471 | ret = sr_scpi_get_opc(sdi->conn); | |
472 | ||
23e1ea7a DJ |
473 | if (ret == SR_OK && update_sample_rate) |
474 | ret = hmo_update_sample_rate(sdi); | |
475 | ||
06a3e78a DJ |
476 | return ret; |
477 | } | |
478 | ||
584560f1 | 479 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 480 | const struct sr_channel_group *cg) |
06a3e78a | 481 | { |
6ec3ef9b AJ |
482 | int cg_type = CG_NONE; |
483 | struct dev_context *devc = NULL; | |
329733d9 | 484 | const struct scope_config *model = NULL; |
13f2b9d7 | 485 | |
6ec3ef9b AJ |
486 | if (sdi && (devc = sdi->priv)) { |
487 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
488 | return SR_ERR; | |
13f2b9d7 | 489 | |
6ec3ef9b AJ |
490 | model = devc->model_config; |
491 | } | |
06a3e78a | 492 | |
06a3e78a | 493 | switch (key) { |
226363c4 | 494 | case SR_CONF_SCAN_OPTIONS: |
584560f1 BV |
495 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
496 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
226363c4 | 497 | break; |
13f2b9d7 | 498 | case SR_CONF_DEVICE_OPTIONS: |
61e77667 | 499 | if (cg_type == CG_NONE) { |
6ec3ef9b AJ |
500 | if (model) |
501 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
502 | model->devopts, model->num_devopts, sizeof(uint32_t)); | |
503 | else | |
504 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
505 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
61e77667 | 506 | } else if (cg_type == CG_ANALOG) { |
584560f1 | 507 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 508 | model->analog_devopts, model->num_analog_devopts, |
584560f1 | 509 | sizeof(uint32_t)); |
13f2b9d7 | 510 | } else { |
584560f1 BV |
511 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
512 | NULL, 0, sizeof(uint32_t)); | |
13f2b9d7 DJ |
513 | } |
514 | break; | |
13f2b9d7 | 515 | case SR_CONF_COUPLING: |
61e77667 | 516 | if (cg_type == CG_NONE) |
660e398f | 517 | return SR_ERR_CHANNEL_GROUP; |
13f2b9d7 | 518 | *data = g_variant_new_strv(*model->coupling_options, |
89280b1a | 519 | g_strv_length((char **)*model->coupling_options)); |
13f2b9d7 | 520 | break; |
13f2b9d7 | 521 | case SR_CONF_TRIGGER_SOURCE: |
6ec3ef9b AJ |
522 | if (!model) |
523 | return SR_ERR_ARG; | |
13f2b9d7 | 524 | *data = g_variant_new_strv(*model->trigger_sources, |
89280b1a | 525 | g_strv_length((char **)*model->trigger_sources)); |
13f2b9d7 | 526 | break; |
bbabdaf1 | 527 | case SR_CONF_TRIGGER_SLOPE: |
6ec3ef9b AJ |
528 | if (!model) |
529 | return SR_ERR_ARG; | |
bbabdaf1 DJ |
530 | *data = g_variant_new_strv(*model->trigger_slopes, |
531 | g_strv_length((char **)*model->trigger_slopes)); | |
532 | break; | |
13f2b9d7 | 533 | case SR_CONF_TIMEBASE: |
6ec3ef9b AJ |
534 | if (!model) |
535 | return SR_ERR_ARG; | |
13f2b9d7 DJ |
536 | *data = build_tuples(model->timebases, model->num_timebases); |
537 | break; | |
13f2b9d7 | 538 | case SR_CONF_VDIV: |
61e77667 | 539 | if (cg_type == CG_NONE) |
660e398f | 540 | return SR_ERR_CHANNEL_GROUP; |
13f2b9d7 DJ |
541 | *data = build_tuples(model->vdivs, model->num_vdivs); |
542 | break; | |
06a3e78a DJ |
543 | default: |
544 | return SR_ERR_NA; | |
545 | } | |
546 | ||
13f2b9d7 | 547 | return SR_OK; |
06a3e78a DJ |
548 | } |
549 | ||
13f2b9d7 | 550 | SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi) |
06a3e78a | 551 | { |
13f2b9d7 | 552 | char command[MAX_COMMAND_SIZE]; |
ba7dd8bb | 553 | struct sr_channel *ch; |
13f2b9d7 | 554 | struct dev_context *devc; |
329733d9 | 555 | const struct scope_config *model; |
13f2b9d7 DJ |
556 | |
557 | devc = sdi->priv; | |
558 | model = devc->model_config; | |
559 | ||
ba7dd8bb | 560 | ch = devc->current_channel->data; |
13f2b9d7 | 561 | |
ba7dd8bb | 562 | switch (ch->type) { |
3f239f08 | 563 | case SR_CHANNEL_ANALOG: |
13f2b9d7 DJ |
564 | g_snprintf(command, sizeof(command), |
565 | (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA], | |
ba7dd8bb | 566 | ch->index + 1); |
13f2b9d7 | 567 | break; |
3f239f08 | 568 | case SR_CHANNEL_LOGIC: |
13f2b9d7 DJ |
569 | g_snprintf(command, sizeof(command), |
570 | (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA], | |
ba7dd8bb | 571 | ch->index < 8 ? 1 : 2); |
13f2b9d7 DJ |
572 | break; |
573 | default: | |
ba7dd8bb | 574 | sr_err("Invalid channel type."); |
13f2b9d7 DJ |
575 | break; |
576 | } | |
577 | ||
578 | return sr_scpi_send(sdi->conn, command); | |
579 | } | |
580 | ||
ba7dd8bb | 581 | static int hmo_check_channels(GSList *channels) |
13f2b9d7 DJ |
582 | { |
583 | GSList *l; | |
ba7dd8bb | 584 | struct sr_channel *ch; |
89280b1a | 585 | gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4; |
13f2b9d7 | 586 | |
89280b1a | 587 | enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE; |
13f2b9d7 | 588 | |
ba7dd8bb UH |
589 | for (l = channels; l; l = l->next) { |
590 | ch = l->data; | |
591 | switch (ch->type) { | |
3f239f08 | 592 | case SR_CHANNEL_ANALOG: |
ba7dd8bb | 593 | if (ch->index == 2) |
13f2b9d7 | 594 | enabled_chan3 = TRUE; |
ba7dd8bb | 595 | else if (ch->index == 3) |
13f2b9d7 DJ |
596 | enabled_chan4 = TRUE; |
597 | break; | |
3f239f08 | 598 | case SR_CHANNEL_LOGIC: |
ba7dd8bb | 599 | if (ch->index < 8) |
13f2b9d7 DJ |
600 | enabled_pod1 = TRUE; |
601 | else | |
602 | enabled_pod2 = TRUE; | |
603 | break; | |
13f2b9d7 DJ |
604 | default: |
605 | return SR_ERR; | |
606 | } | |
607 | } | |
608 | ||
609 | if ((enabled_pod1 && enabled_chan3) || | |
610 | (enabled_pod2 && enabled_chan4)) | |
611 | return SR_ERR; | |
612 | ||
613 | return SR_OK; | |
614 | } | |
615 | ||
ba7dd8bb | 616 | static int hmo_setup_channels(const struct sr_dev_inst *sdi) |
13f2b9d7 DJ |
617 | { |
618 | GSList *l; | |
619 | unsigned int i; | |
23e1ea7a | 620 | gboolean *pod_enabled, setup_changed; |
13f2b9d7 | 621 | char command[MAX_COMMAND_SIZE]; |
13f2b9d7 | 622 | struct scope_state *state; |
329733d9 | 623 | const struct scope_config *model; |
ba7dd8bb | 624 | struct sr_channel *ch; |
13f2b9d7 | 625 | struct dev_context *devc; |
23f43dff | 626 | struct sr_scpi_dev_inst *scpi; |
13f2b9d7 DJ |
627 | |
628 | devc = sdi->priv; | |
23f43dff | 629 | scpi = sdi->conn; |
13f2b9d7 DJ |
630 | state = devc->model_state; |
631 | model = devc->model_config; | |
23e1ea7a | 632 | setup_changed = FALSE; |
13f2b9d7 DJ |
633 | |
634 | pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods); | |
635 | ||
ba7dd8bb UH |
636 | for (l = sdi->channels; l; l = l->next) { |
637 | ch = l->data; | |
638 | switch (ch->type) { | |
3f239f08 | 639 | case SR_CHANNEL_ANALOG: |
ba7dd8bb | 640 | if (ch->enabled == state->analog_channels[ch->index].state) |
082972e8 UH |
641 | break; |
642 | g_snprintf(command, sizeof(command), | |
643 | (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE], | |
ba7dd8bb | 644 | ch->index + 1, ch->enabled); |
13f2b9d7 | 645 | |
23f43dff | 646 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 | 647 | return SR_ERR; |
ba7dd8bb | 648 | state->analog_channels[ch->index].state = ch->enabled; |
23e1ea7a | 649 | setup_changed = TRUE; |
89280b1a | 650 | break; |
3f239f08 | 651 | case SR_CHANNEL_LOGIC: |
13f2b9d7 DJ |
652 | /* |
653 | * A digital POD needs to be enabled for every group of | |
ba7dd8bb | 654 | * 8 channels. |
13f2b9d7 | 655 | */ |
ba7dd8bb UH |
656 | if (ch->enabled) |
657 | pod_enabled[ch->index < 8 ? 0 : 1] = TRUE; | |
13f2b9d7 | 658 | |
ba7dd8bb | 659 | if (ch->enabled == state->digital_channels[ch->index]) |
082972e8 UH |
660 | break; |
661 | g_snprintf(command, sizeof(command), | |
662 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE], | |
ba7dd8bb | 663 | ch->index, ch->enabled); |
13f2b9d7 | 664 | |
23f43dff | 665 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 | 666 | return SR_ERR; |
13f2b9d7 | 667 | |
ba7dd8bb | 668 | state->digital_channels[ch->index] = ch->enabled; |
23e1ea7a | 669 | setup_changed = TRUE; |
89280b1a | 670 | break; |
13f2b9d7 DJ |
671 | default: |
672 | return SR_ERR; | |
673 | } | |
674 | } | |
675 | ||
0a1f7b09 | 676 | for (i = 1; i <= model->digital_pods; i++) { |
082972e8 UH |
677 | if (state->digital_pods[i - 1] == pod_enabled[i - 1]) |
678 | continue; | |
679 | g_snprintf(command, sizeof(command), | |
680 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE], | |
681 | i, pod_enabled[i - 1]); | |
23f43dff | 682 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 UH |
683 | return SR_ERR; |
684 | state->digital_pods[i - 1] = pod_enabled[i - 1]; | |
23e1ea7a | 685 | setup_changed = TRUE; |
13f2b9d7 DJ |
686 | } |
687 | ||
688 | g_free(pod_enabled); | |
689 | ||
23e1ea7a DJ |
690 | if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK) |
691 | return SR_ERR; | |
692 | ||
13f2b9d7 DJ |
693 | return SR_OK; |
694 | } | |
695 | ||
695dc859 | 696 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
13f2b9d7 DJ |
697 | { |
698 | GSList *l; | |
699 | gboolean digital_added; | |
ba7dd8bb | 700 | struct sr_channel *ch; |
13f2b9d7 | 701 | struct dev_context *devc; |
23f43dff | 702 | struct sr_scpi_dev_inst *scpi; |
06a3e78a DJ |
703 | |
704 | if (sdi->status != SR_ST_ACTIVE) | |
705 | return SR_ERR_DEV_CLOSED; | |
706 | ||
23f43dff | 707 | scpi = sdi->conn; |
13f2b9d7 DJ |
708 | devc = sdi->priv; |
709 | digital_added = FALSE; | |
710 | ||
db81fbb5 SA |
711 | g_slist_free(devc->enabled_channels); |
712 | devc->enabled_channels = NULL; | |
713 | ||
ba7dd8bb UH |
714 | for (l = sdi->channels; l; l = l->next) { |
715 | ch = l->data; | |
716 | if (!ch->enabled) | |
082972e8 | 717 | continue; |
ba7dd8bb | 718 | /* Only add a single digital channel. */ |
3f239f08 | 719 | if (ch->type != SR_CHANNEL_LOGIC || !digital_added) { |
ba7dd8bb UH |
720 | devc->enabled_channels = g_slist_append( |
721 | devc->enabled_channels, ch); | |
3f239f08 | 722 | if (ch->type == SR_CHANNEL_LOGIC) |
082972e8 | 723 | digital_added = TRUE; |
13f2b9d7 DJ |
724 | } |
725 | } | |
06a3e78a | 726 | |
ba7dd8bb | 727 | if (!devc->enabled_channels) |
13f2b9d7 DJ |
728 | return SR_ERR; |
729 | ||
ba7dd8bb UH |
730 | if (hmo_check_channels(devc->enabled_channels) != SR_OK) { |
731 | sr_err("Invalid channel configuration specified!"); | |
13f2b9d7 DJ |
732 | return SR_ERR_NA; |
733 | } | |
734 | ||
ba7dd8bb UH |
735 | if (hmo_setup_channels(sdi) != SR_OK) { |
736 | sr_err("Failed to setup channel configuration!"); | |
13f2b9d7 DJ |
737 | return SR_ERR; |
738 | } | |
739 | ||
102f1239 BV |
740 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, |
741 | hmo_receive_data, (void *)sdi); | |
13f2b9d7 | 742 | |
695dc859 | 743 | std_session_send_df_header(sdi, LOG_PREFIX); |
13f2b9d7 | 744 | |
ba7dd8bb | 745 | devc->current_channel = devc->enabled_channels; |
13f2b9d7 DJ |
746 | |
747 | return hmo_request_data(sdi); | |
06a3e78a DJ |
748 | } |
749 | ||
695dc859 | 750 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
06a3e78a | 751 | { |
13f2b9d7 | 752 | struct dev_context *devc; |
23f43dff | 753 | struct sr_scpi_dev_inst *scpi; |
13f2b9d7 | 754 | |
3be42bc2 | 755 | std_session_send_df_end(sdi, LOG_PREFIX); |
66e3219d | 756 | |
06a3e78a DJ |
757 | if (sdi->status != SR_ST_ACTIVE) |
758 | return SR_ERR_DEV_CLOSED; | |
759 | ||
13f2b9d7 DJ |
760 | devc = sdi->priv; |
761 | ||
ef1a346b | 762 | devc->num_frames = 0; |
ba7dd8bb UH |
763 | g_slist_free(devc->enabled_channels); |
764 | devc->enabled_channels = NULL; | |
23f43dff | 765 | scpi = sdi->conn; |
102f1239 | 766 | sr_scpi_source_remove(sdi->session, scpi); |
06a3e78a DJ |
767 | |
768 | return SR_OK; | |
769 | } | |
770 | ||
771 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = { | |
772 | .name = "hameg-hmo", | |
89280b1a | 773 | .longname = "Hameg HMO", |
06a3e78a | 774 | .api_version = 1, |
c2fdcc25 | 775 | .init = std_init, |
700d6b64 | 776 | .cleanup = std_cleanup, |
06a3e78a | 777 | .scan = scan, |
c01bf34c | 778 | .dev_list = std_dev_list, |
06a3e78a DJ |
779 | .dev_clear = dev_clear, |
780 | .config_get = config_get, | |
781 | .config_set = config_set, | |
782 | .config_list = config_list, | |
783 | .dev_open = dev_open, | |
784 | .dev_close = dev_close, | |
785 | .dev_acquisition_start = dev_acquisition_start, | |
786 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 787 | .context = NULL, |
06a3e78a | 788 | }; |