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