]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/hameg-hmo/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / hameg-hmo / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22#include <stdlib.h>
23#include "scpi.h"
24#include "protocol.h"
25
26static struct sr_dev_driver hameg_hmo_driver_info;
27
28static const char *manufacturers[] = {
29 "HAMEG",
30 "Rohde&Schwarz",
31};
32
33static const uint32_t scanopts[] = {
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
36};
37
38static const uint32_t drvopts[] = {
39 SR_CONF_OSCILLOSCOPE,
40 SR_CONF_LOGIC_ANALYZER,
41};
42
43enum {
44 CG_INVALID = -1,
45 CG_NONE,
46 CG_ANALOG,
47 CG_DIGITAL,
48};
49
50static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
51{
52 struct sr_dev_inst *sdi;
53 struct dev_context *devc;
54 struct sr_scpi_hw_info *hw_info;
55
56 sdi = NULL;
57 devc = NULL;
58 hw_info = NULL;
59
60 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
61 sr_info("Couldn't get IDN response.");
62 goto fail;
63 }
64
65 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
66 goto fail;
67
68 sdi = g_malloc0(sizeof(struct sr_dev_inst));
69 sdi->vendor = g_strdup(hw_info->manufacturer);
70 sdi->model = g_strdup(hw_info->model);
71 sdi->version = g_strdup(hw_info->firmware_version);
72 sdi->serial_num = g_strdup(hw_info->serial_number);
73 sdi->driver = &hameg_hmo_driver_info;
74 sdi->inst_type = SR_INST_SCPI;
75 sdi->conn = scpi;
76
77 sr_scpi_hw_info_free(hw_info);
78 hw_info = NULL;
79
80 devc = g_malloc0(sizeof(struct dev_context));
81
82 sdi->priv = devc;
83
84 if (hmo_init_device(sdi) != SR_OK)
85 goto fail;
86
87 return sdi;
88
89fail:
90 sr_scpi_hw_info_free(hw_info);
91 sr_dev_inst_free(sdi);
92 g_free(devc);
93
94 return NULL;
95}
96
97static GSList *scan(struct sr_dev_driver *di, GSList *options)
98{
99 return sr_scpi_scan(di->context, options, probe_device);
100}
101
102static void clear_helper(struct dev_context *devc)
103{
104 hmo_scope_state_free(devc->model_state);
105 g_free(devc->analog_groups);
106 g_free(devc->digital_groups);
107}
108
109static int dev_clear(const struct sr_dev_driver *di)
110{
111 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
112}
113
114static int dev_open(struct sr_dev_inst *sdi)
115{
116 if (sr_scpi_open(sdi->conn) != SR_OK)
117 return SR_ERR;
118
119 if (hmo_scope_state_get(sdi) != SR_OK)
120 return SR_ERR;
121
122 return SR_OK;
123}
124
125static int dev_close(struct sr_dev_inst *sdi)
126{
127 return sr_scpi_close(sdi->conn);
128}
129
130static int check_channel_group(struct dev_context *devc,
131 const struct sr_channel_group *cg)
132{
133 const struct scope_config *model;
134
135 model = devc->model_config;
136
137 if (!cg)
138 return CG_NONE;
139
140 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
141 return CG_ANALOG;
142
143 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
144 return CG_DIGITAL;
145
146 sr_err("Invalid channel group specified.");
147
148 return CG_INVALID;
149}
150
151static int config_get(uint32_t key, GVariant **data,
152 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
153{
154 int cg_type, idx, i;
155 struct dev_context *devc;
156 const struct scope_config *model;
157 struct scope_state *state;
158
159 if (!sdi)
160 return SR_ERR_ARG;
161
162 devc = sdi->priv;
163
164 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
165 return SR_ERR;
166
167 model = devc->model_config;
168 state = devc->model_state;
169
170 switch (key) {
171 case SR_CONF_NUM_HDIV:
172 *data = g_variant_new_int32(model->num_xdivs);
173 break;
174 case SR_CONF_TIMEBASE:
175 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
176 (*model->timebases)[state->timebase][1]);
177 break;
178 case SR_CONF_NUM_VDIV:
179 if (!cg)
180 return SR_ERR_CHANNEL_GROUP;
181 if (cg_type != CG_ANALOG)
182 return SR_ERR_NA;
183 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
184 return SR_ERR_ARG;
185 *data = g_variant_new_int32(model->num_ydivs);
186 break;
187 case SR_CONF_VDIV:
188 if (!cg)
189 return SR_ERR_CHANNEL_GROUP;
190 if (cg_type != CG_ANALOG)
191 return SR_ERR_NA;
192 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
193 return SR_ERR_ARG;
194 *data = g_variant_new("(tt)",
195 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
196 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
197 break;
198 case SR_CONF_TRIGGER_SOURCE:
199 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
200 break;
201 case SR_CONF_TRIGGER_SLOPE:
202 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
203 break;
204 case SR_CONF_TRIGGER_PATTERN:
205 *data = g_variant_new_string(state->trigger_pattern);
206 break;
207 case SR_CONF_HIGH_RESOLUTION:
208 *data = g_variant_new_boolean(state->high_resolution);
209 break;
210 case SR_CONF_PEAK_DETECTION:
211 *data = g_variant_new_boolean(state->peak_detection);
212 break;
213 case SR_CONF_HORIZ_TRIGGERPOS:
214 *data = g_variant_new_double(state->horiz_triggerpos);
215 break;
216 case SR_CONF_COUPLING:
217 if (!cg)
218 return SR_ERR_CHANNEL_GROUP;
219 if (cg_type != CG_ANALOG)
220 return SR_ERR_NA;
221 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
222 return SR_ERR_ARG;
223 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
224 break;
225 case SR_CONF_SAMPLERATE:
226 *data = g_variant_new_uint64(state->sample_rate);
227 break;
228 case SR_CONF_LOGIC_THRESHOLD:
229 if (!cg)
230 return SR_ERR_CHANNEL_GROUP;
231 if (cg_type != CG_DIGITAL)
232 return SR_ERR_NA;
233 if (!model)
234 return SR_ERR_ARG;
235 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
236 return SR_ERR_ARG;
237 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
238 break;
239 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
240 if (!cg)
241 return SR_ERR_CHANNEL_GROUP;
242 if (cg_type != CG_DIGITAL)
243 return SR_ERR_NA;
244 if (!model)
245 return SR_ERR_ARG;
246 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
247 return SR_ERR_ARG;
248 /* Check if the oscilloscope is currently in custom threshold mode. */
249 for (i = 0; i < model->num_logic_threshold; i++) {
250 if (!strcmp("USER2", (*model->logic_threshold)[i]))
251 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
252 return SR_ERR_NA;
253 if (!strcmp("USER", (*model->logic_threshold)[i]))
254 if (strcmp("USER", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
255 return SR_ERR_NA;
256 if (!strcmp("MAN", (*model->logic_threshold)[i]))
257 if (strcmp("MAN", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
258 return SR_ERR_NA;
259 }
260 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
261 break;
262 case SR_CONF_LIMIT_FRAMES:
263 *data = g_variant_new_uint64(devc->frame_limit);
264 break;
265 default:
266 return SR_ERR_NA;
267 }
268
269 return SR_OK;
270}
271
272static int config_set(uint32_t key, GVariant *data,
273 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
274{
275 int ret, cg_type, idx, i, j;
276 char command[MAX_COMMAND_SIZE], command2[MAX_COMMAND_SIZE];
277 char float_str[30], *tmp_str;
278 struct dev_context *devc;
279 const struct scope_config *model;
280 struct scope_state *state;
281 double tmp_d, tmp_d2;
282 gboolean update_sample_rate, tmp_bool;
283
284 if (!sdi)
285 return SR_ERR_ARG;
286
287 devc = sdi->priv;
288
289 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
290 return SR_ERR;
291
292 model = devc->model_config;
293 state = devc->model_state;
294 update_sample_rate = FALSE;
295
296 switch (key) {
297 case SR_CONF_LIMIT_SAMPLES:
298 devc->samples_limit = g_variant_get_uint64(data);
299 ret = SR_OK;
300 break;
301 case SR_CONF_LIMIT_FRAMES:
302 devc->frame_limit = g_variant_get_uint64(data);
303 ret = SR_OK;
304 break;
305 case SR_CONF_VDIV:
306 if (!cg)
307 return SR_ERR_CHANNEL_GROUP;
308 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
309 return SR_ERR_ARG;
310 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
311 return SR_ERR_ARG;
312 g_ascii_formatd(float_str, sizeof(float_str), "%E",
313 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
314 g_snprintf(command, sizeof(command),
315 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_SCALE],
316 j + 1, float_str);
317 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
318 sr_scpi_get_opc(sdi->conn) != SR_OK)
319 return SR_ERR;
320 state->analog_channels[j].vdiv = idx;
321 ret = SR_OK;
322 break;
323 case SR_CONF_TIMEBASE:
324 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
325 return SR_ERR_ARG;
326 g_ascii_formatd(float_str, sizeof(float_str), "%E",
327 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
328 g_snprintf(command, sizeof(command),
329 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
330 float_str);
331 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
332 sr_scpi_get_opc(sdi->conn) != SR_OK)
333 return SR_ERR;
334 state->timebase = idx;
335 ret = SR_OK;
336 update_sample_rate = TRUE;
337 break;
338 case SR_CONF_HORIZ_TRIGGERPOS:
339 tmp_d = g_variant_get_double(data);
340 if (tmp_d < 0.0 || tmp_d > 1.0)
341 return SR_ERR;
342 tmp_d2 = -(tmp_d - 0.5) *
343 ((double) (*model->timebases)[state->timebase][0] /
344 (*model->timebases)[state->timebase][1])
345 * model->num_xdivs;
346 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
347 g_snprintf(command, sizeof(command),
348 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
349 float_str);
350 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
351 sr_scpi_get_opc(sdi->conn) != SR_OK)
352 return SR_ERR;
353 state->horiz_triggerpos = tmp_d;
354 ret = SR_OK;
355 break;
356 case SR_CONF_TRIGGER_SOURCE:
357 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
358 return SR_ERR_ARG;
359 g_snprintf(command, sizeof(command),
360 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
361 (*model->trigger_sources)[idx]);
362 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
363 sr_scpi_get_opc(sdi->conn) != SR_OK)
364 return SR_ERR;
365 state->trigger_source = idx;
366 ret = SR_OK;
367 break;
368 case SR_CONF_TRIGGER_SLOPE:
369 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
370 return SR_ERR_ARG;
371 g_snprintf(command, sizeof(command),
372 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
373 (*model->trigger_slopes)[idx]);
374 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
375 sr_scpi_get_opc(sdi->conn) != SR_OK)
376 return SR_ERR;
377 state->trigger_slope = idx;
378 ret = SR_OK;
379 break;
380 case SR_CONF_TRIGGER_PATTERN:
381 tmp_str = (char *)g_variant_get_string(data, 0);
382 idx = strlen(tmp_str);
383 if (idx == 0 || idx > model->analog_channels + model->digital_channels)
384 return SR_ERR_ARG;
385 g_snprintf(command, sizeof(command),
386 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_PATTERN],
387 tmp_str);
388 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
389 sr_scpi_get_opc(sdi->conn) != SR_OK)
390 return SR_ERR;
391 strncpy(state->trigger_pattern,
392 tmp_str,
393 MAX_ANALOG_CHANNEL_COUNT + MAX_DIGITAL_CHANNEL_COUNT);
394 ret = SR_OK;
395 break;
396 case SR_CONF_HIGH_RESOLUTION:
397 tmp_bool = g_variant_get_boolean(data);
398 g_snprintf(command, sizeof(command),
399 (*model->scpi_dialect)[SCPI_CMD_SET_HIGH_RESOLUTION],
400 tmp_bool ? "AUTO" : "OFF");
401 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
402 sr_scpi_get_opc(sdi->conn) != SR_OK)
403 return SR_ERR;
404 /* High Resolution mode automatically switches off Peak Detection. */
405 if (tmp_bool) {
406 g_snprintf(command, sizeof(command),
407 (*model->scpi_dialect)[SCPI_CMD_SET_PEAK_DETECTION],
408 "OFF");
409 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
410 sr_scpi_get_opc(sdi->conn) != SR_OK)
411 return SR_ERR;
412 state->peak_detection = FALSE;
413 }
414 state->high_resolution = tmp_bool;
415 ret = SR_OK;
416 break;
417 case SR_CONF_PEAK_DETECTION:
418 tmp_bool = g_variant_get_boolean(data);
419 g_snprintf(command, sizeof(command),
420 (*model->scpi_dialect)[SCPI_CMD_SET_PEAK_DETECTION],
421 tmp_bool ? "AUTO" : "OFF");
422 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
423 sr_scpi_get_opc(sdi->conn) != SR_OK)
424 return SR_ERR;
425 /* Peak Detection automatically switches off High Resolution mode. */
426 if (tmp_bool) {
427 g_snprintf(command, sizeof(command),
428 (*model->scpi_dialect)[SCPI_CMD_SET_HIGH_RESOLUTION],
429 "OFF");
430 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
431 sr_scpi_get_opc(sdi->conn) != SR_OK)
432 return SR_ERR;
433 state->high_resolution = FALSE;
434 }
435 state->peak_detection = tmp_bool;
436 ret = SR_OK;
437 break;
438 case SR_CONF_COUPLING:
439 if (!cg)
440 return SR_ERR_CHANNEL_GROUP;
441 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
442 return SR_ERR_ARG;
443 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
444 return SR_ERR_ARG;
445 g_snprintf(command, sizeof(command),
446 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
447 j + 1, (*model->coupling_options)[idx]);
448 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
449 sr_scpi_get_opc(sdi->conn) != SR_OK)
450 return SR_ERR;
451 state->analog_channels[j].coupling = idx;
452 ret = SR_OK;
453 break;
454 case SR_CONF_LOGIC_THRESHOLD:
455 if (!cg)
456 return SR_ERR_CHANNEL_GROUP;
457 if (cg_type != CG_DIGITAL)
458 return SR_ERR_NA;
459 if (!model)
460 return SR_ERR_ARG;
461 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
462 return SR_ERR_ARG;
463 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
464 return SR_ERR_ARG;
465 /* Check if the threshold command is based on the POD or digital channel index. */
466 if (model->logic_threshold_for_pod)
467 i = j + 1;
468 else
469 i = j * DIGITAL_CHANNELS_PER_POD;
470 g_snprintf(command, sizeof(command),
471 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
472 i, (*model->logic_threshold)[idx]);
473 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
474 sr_scpi_get_opc(sdi->conn) != SR_OK)
475 return SR_ERR;
476 state->digital_pods[j].threshold = idx;
477 ret = SR_OK;
478 break;
479 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
480 if (!cg)
481 return SR_ERR_CHANNEL_GROUP;
482 if (cg_type != CG_DIGITAL)
483 return SR_ERR_NA;
484 if (!model)
485 return SR_ERR_ARG;
486 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
487 return SR_ERR_ARG;
488 tmp_d = g_variant_get_double(data);
489 if (tmp_d < -2.0 || tmp_d > 8.0)
490 return SR_ERR;
491 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
492 /* Check if the threshold command is based on the POD or digital channel index. */
493 if (model->logic_threshold_for_pod)
494 idx = j + 1;
495 else
496 idx = j * DIGITAL_CHANNELS_PER_POD;
497 /* Try to support different dialects exhaustively. */
498 for (i = 0; i < model->num_logic_threshold; i++) {
499 if (!strcmp("USER2", (*model->logic_threshold)[i])) {
500 g_snprintf(command, sizeof(command),
501 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
502 idx, 2, float_str); /* USER2 */
503 g_snprintf(command2, sizeof(command2),
504 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
505 idx, "USER2");
506 break;
507 }
508 if (!strcmp("USER", (*model->logic_threshold)[i])) {
509 g_snprintf(command, sizeof(command),
510 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
511 idx, float_str);
512 g_snprintf(command2, sizeof(command2),
513 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
514 idx, "USER");
515 break;
516 }
517 if (!strcmp("MAN", (*model->logic_threshold)[i])) {
518 g_snprintf(command, sizeof(command),
519 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
520 idx, float_str);
521 g_snprintf(command2, sizeof(command2),
522 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
523 idx, "MAN");
524 break;
525 }
526 }
527 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
528 sr_scpi_get_opc(sdi->conn) != SR_OK)
529 return SR_ERR;
530 if (sr_scpi_send(sdi->conn, command2) != SR_OK ||
531 sr_scpi_get_opc(sdi->conn) != SR_OK)
532 return SR_ERR;
533 state->digital_pods[j].user_threshold = tmp_d;
534 ret = SR_OK;
535 break;
536 default:
537 ret = SR_ERR_NA;
538 break;
539 }
540
541 if (ret == SR_OK && update_sample_rate)
542 ret = hmo_update_sample_rate(sdi);
543
544 return ret;
545}
546
547static int config_list(uint32_t key, GVariant **data,
548 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
549{
550 int cg_type = CG_NONE;
551 struct dev_context *devc = NULL;
552 const struct scope_config *model = NULL;
553
554 if (sdi) {
555 devc = sdi->priv;
556 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
557 return SR_ERR;
558
559 model = devc->model_config;
560 }
561
562 switch (key) {
563 case SR_CONF_SCAN_OPTIONS:
564 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
565 break;
566 case SR_CONF_DEVICE_OPTIONS:
567 if (!cg) {
568 if (model)
569 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
570 else
571 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
572 } else if (cg_type == CG_ANALOG) {
573 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
574 } else if (cg_type == CG_DIGITAL) {
575 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
576 } else {
577 *data = std_gvar_array_u32(NULL, 0);
578 }
579 break;
580 case SR_CONF_COUPLING:
581 if (!cg)
582 return SR_ERR_CHANNEL_GROUP;
583 if (!model)
584 return SR_ERR_ARG;
585 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
586 break;
587 case SR_CONF_TRIGGER_SOURCE:
588 if (!model)
589 return SR_ERR_ARG;
590 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
591 break;
592 case SR_CONF_TRIGGER_SLOPE:
593 if (!model)
594 return SR_ERR_ARG;
595 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
596 break;
597 case SR_CONF_TIMEBASE:
598 if (!model)
599 return SR_ERR_ARG;
600 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
601 break;
602 case SR_CONF_VDIV:
603 if (!cg)
604 return SR_ERR_CHANNEL_GROUP;
605 if (!model)
606 return SR_ERR_ARG;
607 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
608 break;
609 case SR_CONF_LOGIC_THRESHOLD:
610 if (!cg)
611 return SR_ERR_CHANNEL_GROUP;
612 if (!model)
613 return SR_ERR_ARG;
614 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
615 break;
616 default:
617 return SR_ERR_NA;
618 }
619
620 return SR_OK;
621}
622
623SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
624{
625 char command[MAX_COMMAND_SIZE];
626 struct sr_channel *ch;
627 struct dev_context *devc;
628 const struct scope_config *model;
629
630 devc = sdi->priv;
631 model = devc->model_config;
632
633 ch = devc->current_channel->data;
634
635 switch (ch->type) {
636 case SR_CHANNEL_ANALOG:
637 g_snprintf(command, sizeof(command),
638 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
639#ifdef WORDS_BIGENDIAN
640 "MSBF",
641#else
642 "LSBF",
643#endif
644 ch->index + 1);
645 break;
646 case SR_CHANNEL_LOGIC:
647 g_snprintf(command, sizeof(command),
648 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
649 ch->index / DIGITAL_CHANNELS_PER_POD + 1);
650 break;
651 default:
652 sr_err("Invalid channel type.");
653 break;
654 }
655
656 return sr_scpi_send(sdi->conn, command);
657}
658
659static int hmo_check_channels(GSList *channels)
660{
661 GSList *l;
662 struct sr_channel *ch;
663 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
664 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
665 size_t idx;
666
667 /* Preset "not enabled" for all channels / pods. */
668 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
669 enabled_chan[idx] = FALSE;
670 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
671 enabled_pod[idx] = FALSE;
672
673 /*
674 * Determine which channels / pods are required for the caller's
675 * specified configuration.
676 */
677 for (l = channels; l; l = l->next) {
678 ch = l->data;
679 switch (ch->type) {
680 case SR_CHANNEL_ANALOG:
681 idx = ch->index;
682 if (idx < ARRAY_SIZE(enabled_chan))
683 enabled_chan[idx] = TRUE;
684 break;
685 case SR_CHANNEL_LOGIC:
686 idx = ch->index / DIGITAL_CHANNELS_PER_POD;
687 if (idx < ARRAY_SIZE(enabled_pod))
688 enabled_pod[idx] = TRUE;
689 break;
690 default:
691 return SR_ERR;
692 }
693 }
694
695 /*
696 * Check for resource conflicts. Some channels can be either
697 * analog or digital, but never both at the same time.
698 *
699 * Note that the constraints might depend on the specific model.
700 * These tests might need some adjustment when support for more
701 * models gets added to the driver.
702 */
703 if (enabled_pod[0] && enabled_chan[2])
704 return SR_ERR;
705 if (enabled_pod[1] && enabled_chan[3])
706 return SR_ERR;
707 return SR_OK;
708}
709
710static int hmo_setup_channels(const struct sr_dev_inst *sdi)
711{
712 GSList *l;
713 unsigned int i;
714 gboolean *pod_enabled, setup_changed;
715 char command[MAX_COMMAND_SIZE];
716 struct scope_state *state;
717 const struct scope_config *model;
718 struct sr_channel *ch;
719 struct dev_context *devc;
720 struct sr_scpi_dev_inst *scpi;
721 int ret;
722
723 devc = sdi->priv;
724 scpi = sdi->conn;
725 state = devc->model_state;
726 model = devc->model_config;
727 setup_changed = FALSE;
728
729 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
730
731 for (l = sdi->channels; l; l = l->next) {
732 ch = l->data;
733 switch (ch->type) {
734 case SR_CHANNEL_ANALOG:
735 if (ch->enabled == state->analog_channels[ch->index].state)
736 break;
737 g_snprintf(command, sizeof(command),
738 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
739 ch->index + 1, ch->enabled);
740
741 if (sr_scpi_send(scpi, command) != SR_OK) {
742 g_free(pod_enabled);
743 return SR_ERR;
744 }
745 state->analog_channels[ch->index].state = ch->enabled;
746 setup_changed = TRUE;
747 break;
748 case SR_CHANNEL_LOGIC:
749 /*
750 * A digital POD needs to be enabled for every group of
751 * DIGITAL_CHANNELS_PER_POD channels.
752 */
753 if (ch->enabled)
754 pod_enabled[ch->index / DIGITAL_CHANNELS_PER_POD] = TRUE;
755
756 if (ch->enabled == state->digital_channels[ch->index])
757 break;
758 g_snprintf(command, sizeof(command),
759 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
760 ch->index, ch->enabled);
761
762 if (sr_scpi_send(scpi, command) != SR_OK) {
763 g_free(pod_enabled);
764 return SR_ERR;
765 }
766
767 state->digital_channels[ch->index] = ch->enabled;
768 setup_changed = TRUE;
769 break;
770 default:
771 g_free(pod_enabled);
772 return SR_ERR;
773 }
774 }
775
776 ret = SR_OK;
777 for (i = 0; i < model->digital_pods; i++) {
778 if (state->digital_pods[i].state == pod_enabled[i])
779 continue;
780 g_snprintf(command, sizeof(command),
781 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
782 i + 1, pod_enabled[i]);
783 if (sr_scpi_send(scpi, command) != SR_OK) {
784 ret = SR_ERR;
785 break;
786 }
787 state->digital_pods[i].state = pod_enabled[i];
788 setup_changed = TRUE;
789 }
790 g_free(pod_enabled);
791 if (ret != SR_OK)
792 return ret;
793
794 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
795 return SR_ERR;
796
797 return SR_OK;
798}
799
800static int dev_acquisition_start(const struct sr_dev_inst *sdi)
801{
802 GSList *l;
803 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
804 size_t group, pod_count;
805 struct sr_channel *ch;
806 struct dev_context *devc;
807 struct sr_scpi_dev_inst *scpi;
808 int ret;
809
810 scpi = sdi->conn;
811 devc = sdi->priv;
812
813 devc->num_samples = 0;
814 devc->num_frames = 0;
815
816 /* Preset empty results. */
817 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
818 digital_added[group] = FALSE;
819 g_slist_free(devc->enabled_channels);
820 devc->enabled_channels = NULL;
821
822 /*
823 * Contruct the list of enabled channels. Determine the highest
824 * number of digital pods involved in the acquisition.
825 */
826 pod_count = 0;
827 for (l = sdi->channels; l; l = l->next) {
828 ch = l->data;
829 if (!ch->enabled)
830 continue;
831 /* Only add a single digital channel per group (pod). */
832 group = ch->index / DIGITAL_CHANNELS_PER_POD;
833 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
834 devc->enabled_channels = g_slist_append(
835 devc->enabled_channels, ch);
836 if (ch->type == SR_CHANNEL_LOGIC) {
837 digital_added[group] = TRUE;
838 if (pod_count < group + 1)
839 pod_count = group + 1;
840 }
841 }
842 }
843 if (!devc->enabled_channels)
844 return SR_ERR;
845 devc->pod_count = pod_count;
846 devc->logic_data = NULL;
847
848 /*
849 * Check constraints. Some channels can be either analog or
850 * digital, but not both at the same time.
851 */
852 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
853 sr_err("Invalid channel configuration specified!");
854 ret = SR_ERR_NA;
855 goto free_enabled;
856 }
857
858 /*
859 * Configure the analog and digital channels and the
860 * corresponding digital pods.
861 */
862 if (hmo_setup_channels(sdi) != SR_OK) {
863 sr_err("Failed to setup channel configuration!");
864 ret = SR_ERR;
865 goto free_enabled;
866 }
867
868 /*
869 * Start acquisition on the first enabled channel. The
870 * receive routine will continue driving the acquisition.
871 */
872 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
873 hmo_receive_data, (void *)sdi);
874
875 std_session_send_df_header(sdi);
876
877 devc->current_channel = devc->enabled_channels;
878
879 return hmo_request_data(sdi);
880
881free_enabled:
882 g_slist_free(devc->enabled_channels);
883 devc->enabled_channels = NULL;
884 return ret;
885}
886
887static int dev_acquisition_stop(struct sr_dev_inst *sdi)
888{
889 struct dev_context *devc;
890 struct sr_scpi_dev_inst *scpi;
891
892 std_session_send_df_end(sdi);
893
894 devc = sdi->priv;
895
896 devc->num_samples = 0;
897 devc->num_frames = 0;
898 g_slist_free(devc->enabled_channels);
899 devc->enabled_channels = NULL;
900 scpi = sdi->conn;
901 sr_scpi_source_remove(sdi->session, scpi);
902
903 return SR_OK;
904}
905
906static struct sr_dev_driver hameg_hmo_driver_info = {
907 .name = "hameg-hmo",
908 .longname = "Hameg HMO",
909 .api_version = 1,
910 .init = std_init,
911 .cleanup = std_cleanup,
912 .scan = scan,
913 .dev_list = std_dev_list,
914 .dev_clear = dev_clear,
915 .config_get = config_get,
916 .config_set = config_set,
917 .config_list = config_list,
918 .dev_open = dev_open,
919 .dev_close = dev_close,
920 .dev_acquisition_start = dev_acquisition_start,
921 .dev_acquisition_stop = dev_acquisition_stop,
922 .context = NULL,
923};
924SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);