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