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