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