]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
hameg-hmo: Add support for SR_CONF_LOGIC_THRESHOLD/_CUSTOM.
[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
DJ
202 break;
203 case SR_CONF_HORIZ_TRIGGERPOS:
204 *data = g_variant_new_double(state->horiz_triggerpos);
3fd2dca2 205 break;
ccf14618 206 case SR_CONF_COUPLING:
3782e571 207 if (!cg)
660e398f 208 return SR_ERR_CHANNEL_GROUP;
3782e571 209 if (cg_type != CG_ANALOG)
758906aa 210 return SR_ERR_NA;
3782e571
UH
211 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
212 return SR_ERR_ARG;
213 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
13f2b9d7 214 break;
14a2f74d
DJ
215 case SR_CONF_SAMPLERATE:
216 *data = g_variant_new_uint64(state->sample_rate);
14a2f74d 217 break;
e131be0a
GT
218 case SR_CONF_LOGIC_THRESHOLD:
219 if (!cg)
220 return SR_ERR_CHANNEL_GROUP;
221 if (cg_type != CG_DIGITAL)
222 return SR_ERR_NA;
223 if (!model)
224 return SR_ERR_ARG;
225 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
226 return SR_ERR_ARG;
227 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
228 break;
229 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
230 if (!cg)
231 return SR_ERR_CHANNEL_GROUP;
232 if (cg_type != CG_DIGITAL)
233 return SR_ERR_NA;
234 if (!model)
235 return SR_ERR_ARG;
236 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
237 return SR_ERR_ARG;
238 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
239 return SR_ERR_NA;
240 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
241 break;
06a3e78a 242 default:
758906aa 243 return SR_ERR_NA;
06a3e78a
DJ
244 }
245
758906aa 246 return SR_OK;
06a3e78a
DJ
247}
248
dd7a72ea
UH
249static int config_set(uint32_t key, GVariant *data,
250 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 251{
fcd6a8bd 252 int ret, cg_type, idx, j;
965b463d 253 char command[MAX_COMMAND_SIZE], float_str[30];
13f2b9d7 254 struct dev_context *devc;
329733d9 255 const struct scope_config *model;
13f2b9d7 256 struct scope_state *state;
89280b1a 257 double tmp_d;
23e1ea7a 258 gboolean update_sample_rate;
06a3e78a 259
b0baddef 260 if (!sdi)
13f2b9d7
DJ
261 return SR_ERR_ARG;
262
b0baddef
UH
263 devc = sdi->priv;
264
61e77667 265 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
266 return SR_ERR;
267
268 model = devc->model_config;
269 state = devc->model_state;
23e1ea7a 270 update_sample_rate = FALSE;
13f2b9d7 271
06a3e78a 272 switch (key) {
d779dcac
GT
273 case SR_CONF_LIMIT_SAMPLES:
274 devc->samples_limit = g_variant_get_uint64(data);
275 ret = SR_OK;
276 break;
13f2b9d7
DJ
277 case SR_CONF_LIMIT_FRAMES:
278 devc->frame_limit = g_variant_get_uint64(data);
279 ret = SR_OK;
280 break;
13f2b9d7 281 case SR_CONF_TRIGGER_SOURCE:
bd633efa
UH
282 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
283 return SR_ERR_ARG;
284 state->trigger_source = idx;
285 g_snprintf(command, sizeof(command),
286 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
287 (*model->trigger_sources)[idx]);
288 ret = sr_scpi_send(sdi->conn, command);
89280b1a 289 break;
13f2b9d7 290 case SR_CONF_VDIV:
61233697 291 if (!cg)
660e398f 292 return SR_ERR_CHANNEL_GROUP;
697fb6dd
UH
293 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
294 return SR_ERR_ARG;
fcd6a8bd
UH
295 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
296 return SR_ERR_ARG;
297 state->analog_channels[j].vdiv = idx;
298 g_ascii_formatd(float_str, sizeof(float_str), "%E",
299 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
300 g_snprintf(command, sizeof(command),
301 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
302 j + 1, float_str);
303 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
304 sr_scpi_get_opc(sdi->conn) != SR_OK)
305 return SR_ERR;
697fb6dd 306 ret = SR_OK;
89280b1a 307 break;
13f2b9d7 308 case SR_CONF_TIMEBASE:
697fb6dd
UH
309 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
310 return SR_ERR_ARG;
311 state->timebase = idx;
312 g_ascii_formatd(float_str, sizeof(float_str), "%E",
313 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
314 g_snprintf(command, sizeof(command),
315 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
316 float_str);
317 ret = sr_scpi_send(sdi->conn, command);
318 update_sample_rate = TRUE;
89280b1a 319 break;
13f2b9d7 320 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 321 tmp_d = g_variant_get_double(data);
89280b1a 322 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7 323 return SR_ERR;
422a1c0d
DJ
324 state->horiz_triggerpos = tmp_d;
325 tmp_d = -(tmp_d - 0.5) *
326 ((double) (*model->timebases)[state->timebase][0] /
327 (*model->timebases)[state->timebase][1])
328 * model->num_xdivs;
422a1c0d 329 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
13f2b9d7
DJ
330 g_snprintf(command, sizeof(command),
331 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 332 float_str);
13f2b9d7 333 ret = sr_scpi_send(sdi->conn, command);
89280b1a 334 break;
13f2b9d7 335 case SR_CONF_TRIGGER_SLOPE:
bd633efa
UH
336 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
337 return SR_ERR_ARG;
338 state->trigger_slope = idx;
339 g_snprintf(command, sizeof(command),
340 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
341 (*model->trigger_slopes)[idx]);
342 ret = sr_scpi_send(sdi->conn, command);
89280b1a 343 break;
13f2b9d7 344 case SR_CONF_COUPLING:
61233697 345 if (!cg)
660e398f 346 return SR_ERR_CHANNEL_GROUP;
bd633efa
UH
347 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
348 return SR_ERR_ARG;
fcd6a8bd
UH
349 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
350 return SR_ERR_ARG;
351 state->analog_channels[j].coupling = idx;
352 g_snprintf(command, sizeof(command),
353 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
354 j + 1, (*model->coupling_options)[idx]);
355 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
356 sr_scpi_get_opc(sdi->conn) != SR_OK)
357 return SR_ERR;
bd633efa 358 ret = SR_OK;
89280b1a 359 break;
e131be0a
GT
360 case SR_CONF_LOGIC_THRESHOLD:
361 if (!cg)
362 return SR_ERR_CHANNEL_GROUP;
363 if (cg_type != CG_DIGITAL)
364 return SR_ERR_NA;
365 if (!model)
366 return SR_ERR_ARG;
367 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
368 return SR_ERR_ARG;
369 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
370 return SR_ERR_ARG;
371 g_snprintf(command, sizeof(command),
372 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
373 j + 1, (*model->logic_threshold)[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->digital_pods[j].threshold = idx;
378 ret = SR_OK;
379 break;
380 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
381 if (!cg)
382 return SR_ERR_CHANNEL_GROUP;
383 if (cg_type != CG_DIGITAL)
384 return SR_ERR_NA;
385 if (!model)
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 tmp_d = g_variant_get_double(data);
390 if (tmp_d < -2.0 || tmp_d > 8.0)
391 return SR_ERR;
392 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
393 g_snprintf(command, sizeof(command),
394 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
395 j + 1, 2, float_str); // USER2 for custom logic_threshold setting
396 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
397 sr_scpi_get_opc(sdi->conn) != SR_OK)
398 return SR_ERR;
399 g_snprintf(command, sizeof(command),
400 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
401 j + 1, "USER2");
402 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
403 sr_scpi_get_opc(sdi->conn) != SR_OK)
404 return SR_ERR;
405 state->digital_pods[j].user_threshold = tmp_d;
406 ret = SR_OK;
407 break;
06a3e78a
DJ
408 default:
409 ret = SR_ERR_NA;
13f2b9d7 410 break;
06a3e78a
DJ
411 }
412
13f2b9d7
DJ
413 if (ret == SR_OK)
414 ret = sr_scpi_get_opc(sdi->conn);
415
23e1ea7a
DJ
416 if (ret == SR_OK && update_sample_rate)
417 ret = hmo_update_sample_rate(sdi);
418
06a3e78a
DJ
419 return ret;
420}
421
dd7a72ea
UH
422static int config_list(uint32_t key, GVariant **data,
423 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 424{
6ec3ef9b
AJ
425 int cg_type = CG_NONE;
426 struct dev_context *devc = NULL;
329733d9 427 const struct scope_config *model = NULL;
13f2b9d7 428
4d399734
UH
429 if (sdi) {
430 devc = sdi->priv;
6ec3ef9b
AJ
431 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
432 return SR_ERR;
13f2b9d7 433
6ec3ef9b
AJ
434 model = devc->model_config;
435 }
06a3e78a 436
06a3e78a 437 switch (key) {
226363c4 438 case SR_CONF_SCAN_OPTIONS:
53012da6 439 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
226363c4 440 break;
13f2b9d7 441 case SR_CONF_DEVICE_OPTIONS:
61233697 442 if (!cg) {
6ec3ef9b 443 if (model)
769561cb 444 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
6ec3ef9b 445 else
53012da6 446 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
61e77667 447 } else if (cg_type == CG_ANALOG) {
769561cb 448 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
e131be0a
GT
449 } else if (cg_type == CG_DIGITAL) {
450 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
13f2b9d7 451 } else {
105df674 452 *data = std_gvar_array_u32(NULL, 0);
13f2b9d7
DJ
453 }
454 break;
13f2b9d7 455 case SR_CONF_COUPLING:
61233697 456 if (!cg)
660e398f 457 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
458 if (!model)
459 return SR_ERR_ARG;
692716f5 460 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
13f2b9d7 461 break;
13f2b9d7 462 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
463 if (!model)
464 return SR_ERR_ARG;
692716f5 465 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
13f2b9d7 466 break;
bbabdaf1 467 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
468 if (!model)
469 return SR_ERR_ARG;
692716f5 470 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
bbabdaf1 471 break;
13f2b9d7 472 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
473 if (!model)
474 return SR_ERR_ARG;
58ffcf97 475 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
13f2b9d7 476 break;
13f2b9d7 477 case SR_CONF_VDIV:
61233697 478 if (!cg)
660e398f 479 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
480 if (!model)
481 return SR_ERR_ARG;
58ffcf97 482 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
13f2b9d7 483 break;
e131be0a
GT
484 case SR_CONF_LOGIC_THRESHOLD:
485 if (!cg)
486 return SR_ERR_CHANNEL_GROUP;
487 if (!model)
488 return SR_ERR_ARG;
489 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
490 break;
06a3e78a
DJ
491 default:
492 return SR_ERR_NA;
493 }
494
13f2b9d7 495 return SR_OK;
06a3e78a
DJ
496}
497
13f2b9d7 498SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 499{
13f2b9d7 500 char command[MAX_COMMAND_SIZE];
ba7dd8bb 501 struct sr_channel *ch;
13f2b9d7 502 struct dev_context *devc;
329733d9 503 const struct scope_config *model;
13f2b9d7
DJ
504
505 devc = sdi->priv;
506 model = devc->model_config;
507
ba7dd8bb 508 ch = devc->current_channel->data;
13f2b9d7 509
ba7dd8bb 510 switch (ch->type) {
3f239f08 511 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
512 g_snprintf(command, sizeof(command),
513 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
65a6794e
GS
514#ifdef WORDS_BIGENDIAN
515 "MSBF",
516#else
517 "LSBF",
518#endif
ba7dd8bb 519 ch->index + 1);
13f2b9d7 520 break;
3f239f08 521 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
522 g_snprintf(command, sizeof(command),
523 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 524 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
525 break;
526 default:
ba7dd8bb 527 sr_err("Invalid channel type.");
13f2b9d7
DJ
528 break;
529 }
530
531 return sr_scpi_send(sdi->conn, command);
532}
533
ba7dd8bb 534static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
535{
536 GSList *l;
ba7dd8bb 537 struct sr_channel *ch;
4889acef
GS
538 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
539 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
540 size_t idx;
541
542 /* Preset "not enabled" for all channels / pods. */
543 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
544 enabled_chan[idx] = FALSE;
545 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
546 enabled_pod[idx] = FALSE;
547
548 /*
549 * Determine which channels / pods are required for the caller's
550 * specified configuration.
551 */
ba7dd8bb
UH
552 for (l = channels; l; l = l->next) {
553 ch = l->data;
554 switch (ch->type) {
3f239f08 555 case SR_CHANNEL_ANALOG:
4889acef
GS
556 idx = ch->index;
557 if (idx < ARRAY_SIZE(enabled_chan))
558 enabled_chan[idx] = TRUE;
13f2b9d7 559 break;
3f239f08 560 case SR_CHANNEL_LOGIC:
4889acef
GS
561 idx = ch->index / 8;
562 if (idx < ARRAY_SIZE(enabled_pod))
563 enabled_pod[idx] = TRUE;
13f2b9d7 564 break;
13f2b9d7
DJ
565 default:
566 return SR_ERR;
567 }
568 }
569
4889acef
GS
570 /*
571 * Check for resource conflicts. Some channels can be either
572 * analog or digital, but never both at the same time.
573 *
574 * Note that the constraints might depend on the specific model.
575 * These tests might need some adjustment when support for more
576 * models gets added to the driver.
577 */
578 if (enabled_pod[0] && enabled_chan[2])
579 return SR_ERR;
580 if (enabled_pod[1] && enabled_chan[3])
13f2b9d7 581 return SR_ERR;
13f2b9d7
DJ
582 return SR_OK;
583}
584
ba7dd8bb 585static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
586{
587 GSList *l;
588 unsigned int i;
23e1ea7a 589 gboolean *pod_enabled, setup_changed;
13f2b9d7 590 char command[MAX_COMMAND_SIZE];
13f2b9d7 591 struct scope_state *state;
329733d9 592 const struct scope_config *model;
ba7dd8bb 593 struct sr_channel *ch;
13f2b9d7 594 struct dev_context *devc;
23f43dff 595 struct sr_scpi_dev_inst *scpi;
addbb09b 596 int ret;
13f2b9d7
DJ
597
598 devc = sdi->priv;
23f43dff 599 scpi = sdi->conn;
13f2b9d7
DJ
600 state = devc->model_state;
601 model = devc->model_config;
23e1ea7a 602 setup_changed = FALSE;
13f2b9d7
DJ
603
604 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
605
ba7dd8bb
UH
606 for (l = sdi->channels; l; l = l->next) {
607 ch = l->data;
608 switch (ch->type) {
3f239f08 609 case SR_CHANNEL_ANALOG:
ba7dd8bb 610 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
611 break;
612 g_snprintf(command, sizeof(command),
613 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 614 ch->index + 1, ch->enabled);
13f2b9d7 615
4fc4b8e7
UH
616 if (sr_scpi_send(scpi, command) != SR_OK) {
617 g_free(pod_enabled);
082972e8 618 return SR_ERR;
4fc4b8e7 619 }
ba7dd8bb 620 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 621 setup_changed = TRUE;
89280b1a 622 break;
3f239f08 623 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
624 /*
625 * A digital POD needs to be enabled for every group of
ba7dd8bb 626 * 8 channels.
13f2b9d7 627 */
ba7dd8bb
UH
628 if (ch->enabled)
629 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
13f2b9d7 630
ba7dd8bb 631 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
632 break;
633 g_snprintf(command, sizeof(command),
634 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 635 ch->index, ch->enabled);
13f2b9d7 636
4fc4b8e7
UH
637 if (sr_scpi_send(scpi, command) != SR_OK) {
638 g_free(pod_enabled);
082972e8 639 return SR_ERR;
4fc4b8e7 640 }
13f2b9d7 641
ba7dd8bb 642 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 643 setup_changed = TRUE;
89280b1a 644 break;
13f2b9d7 645 default:
b0e80e9a 646 g_free(pod_enabled);
13f2b9d7
DJ
647 return SR_ERR;
648 }
649 }
650
addbb09b 651 ret = SR_OK;
d1ac53cc 652 for (i = 0; i < model->digital_pods; i++) {
e131be0a 653 if (state->digital_pods[i].state == pod_enabled[i])
082972e8
UH
654 continue;
655 g_snprintf(command, sizeof(command),
656 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
d1ac53cc 657 i + 1, pod_enabled[i]);
addbb09b
GS
658 if (sr_scpi_send(scpi, command) != SR_OK) {
659 ret = SR_ERR;
660 break;
661 }
e131be0a 662 state->digital_pods[i].state = pod_enabled[i];
23e1ea7a 663 setup_changed = TRUE;
13f2b9d7 664 }
13f2b9d7 665 g_free(pod_enabled);
addbb09b
GS
666 if (ret != SR_OK)
667 return ret;
13f2b9d7 668
23e1ea7a
DJ
669 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
670 return SR_ERR;
671
13f2b9d7
DJ
672 return SR_OK;
673}
674
695dc859 675static int dev_acquisition_start(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
676{
677 GSList *l;
1b0f62df 678 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
e06875b2 679 size_t group, pod_count;
ba7dd8bb 680 struct sr_channel *ch;
13f2b9d7 681 struct dev_context *devc;
23f43dff 682 struct sr_scpi_dev_inst *scpi;
40a75c8e 683 int ret;
06a3e78a 684
23f43dff 685 scpi = sdi->conn;
13f2b9d7 686 devc = sdi->priv;
13f2b9d7 687
d779dcac
GT
688 devc->num_samples = 0;
689 devc->num_frames = 0;
690
1b0f62df
GS
691 /* Preset empty results. */
692 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
693 digital_added[group] = FALSE;
db81fbb5
SA
694 g_slist_free(devc->enabled_channels);
695 devc->enabled_channels = NULL;
696
1b0f62df 697 /*
e06875b2
GS
698 * Contruct the list of enabled channels. Determine the highest
699 * number of digital pods involved in the acquisition.
1b0f62df 700 */
e06875b2 701 pod_count = 0;
ba7dd8bb
UH
702 for (l = sdi->channels; l; l = l->next) {
703 ch = l->data;
704 if (!ch->enabled)
082972e8 705 continue;
1b0f62df
GS
706 /* Only add a single digital channel per group (pod). */
707 group = ch->index / 8;
708 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
ba7dd8bb
UH
709 devc->enabled_channels = g_slist_append(
710 devc->enabled_channels, ch);
e06875b2 711 if (ch->type == SR_CHANNEL_LOGIC) {
1b0f62df 712 digital_added[group] = TRUE;
e06875b2
GS
713 if (pod_count < group + 1)
714 pod_count = group + 1;
715 }
13f2b9d7
DJ
716 }
717 }
ba7dd8bb 718 if (!devc->enabled_channels)
13f2b9d7 719 return SR_ERR;
e06875b2
GS
720 devc->pod_count = pod_count;
721 devc->logic_data = NULL;
13f2b9d7 722
1b0f62df
GS
723 /*
724 * Check constraints. Some channels can be either analog or
725 * digital, but not both at the same time.
726 */
ba7dd8bb
UH
727 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
728 sr_err("Invalid channel configuration specified!");
40a75c8e
GS
729 ret = SR_ERR_NA;
730 goto free_enabled;
13f2b9d7
DJ
731 }
732
1b0f62df
GS
733 /*
734 * Configure the analog and digital channels and the
735 * corresponding digital pods.
736 */
ba7dd8bb
UH
737 if (hmo_setup_channels(sdi) != SR_OK) {
738 sr_err("Failed to setup channel configuration!");
40a75c8e
GS
739 ret = SR_ERR;
740 goto free_enabled;
13f2b9d7
DJ
741 }
742
1b0f62df
GS
743 /*
744 * Start acquisition on the first enabled channel. The
745 * receive routine will continue driving the acquisition.
746 */
102f1239
BV
747 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
748 hmo_receive_data, (void *)sdi);
13f2b9d7 749
bee2b016 750 std_session_send_df_header(sdi);
13f2b9d7 751
ba7dd8bb 752 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
753
754 return hmo_request_data(sdi);
40a75c8e
GS
755
756free_enabled:
757 g_slist_free(devc->enabled_channels);
758 devc->enabled_channels = NULL;
759 return ret;
06a3e78a
DJ
760}
761
695dc859 762static int dev_acquisition_stop(struct sr_dev_inst *sdi)
06a3e78a 763{
13f2b9d7 764 struct dev_context *devc;
23f43dff 765 struct sr_scpi_dev_inst *scpi;
13f2b9d7 766
bee2b016 767 std_session_send_df_end(sdi);
66e3219d 768
13f2b9d7
DJ
769 devc = sdi->priv;
770
d779dcac 771 devc->num_samples = 0;
ef1a346b 772 devc->num_frames = 0;
ba7dd8bb
UH
773 g_slist_free(devc->enabled_channels);
774 devc->enabled_channels = NULL;
23f43dff 775 scpi = sdi->conn;
102f1239 776 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
777
778 return SR_OK;
779}
780
dd5c48a6 781static struct sr_dev_driver hameg_hmo_driver_info = {
06a3e78a 782 .name = "hameg-hmo",
89280b1a 783 .longname = "Hameg HMO",
06a3e78a 784 .api_version = 1,
c2fdcc25 785 .init = std_init,
700d6b64 786 .cleanup = std_cleanup,
06a3e78a 787 .scan = scan,
c01bf34c 788 .dev_list = std_dev_list,
06a3e78a
DJ
789 .dev_clear = dev_clear,
790 .config_get = config_get,
791 .config_set = config_set,
792 .config_list = config_list,
793 .dev_open = dev_open,
794 .dev_close = dev_close,
795 .dev_acquisition_start = dev_acquisition_start,
796 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 797 .context = NULL,
06a3e78a 798};
dd5c48a6 799SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);