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