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