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