]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
Rename SR_CONF_NUM_TIMEBASE to SR_CONF_NUM_HDIV.
[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
13f2b9d7 20#include <stdlib.h>
06a3e78a
DJ
21#include "protocol.h"
22
13f2b9d7
DJ
23#define SERIALCOMM "115200/8n1/flow=1"
24
e9a62139
DJ
25SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
26static struct sr_dev_driver *di = &hameg_hmo_driver_info;
27
28static const char *manufacturers[] = {
29 "HAMEG",
30};
31
6ec3ef9b
AJ
32static const uint32_t drvopts[] = {
33 SR_CONF_OSCILLOSCOPE,
34};
35
584560f1 36static const uint32_t scanopts[] = {
13f2b9d7
DJ
37 SR_CONF_CONN,
38 SR_CONF_SERIALCOMM,
39};
40
719eff68 41enum {
61e77667
UH
42 CG_INVALID = -1,
43 CG_NONE,
44 CG_ANALOG,
45 CG_DIGITAL,
719eff68
UH
46};
47
06a3e78a
DJ
48static int init(struct sr_context *sr_ctx)
49{
50 return std_init(sr_ctx, di, LOG_PREFIX);
51}
52
e9a62139
DJ
53static int check_manufacturer(const char *manufacturer)
54{
55 unsigned int i;
56
57 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
58 if (!strcmp(manufacturer, manufacturers[i]))
59 return SR_OK;
60
61 return SR_ERR;
62}
63
ca28abd6 64static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
e9a62139
DJ
65{
66 struct sr_dev_inst *sdi;
67 struct dev_context *devc;
68 struct sr_scpi_hw_info *hw_info;
e9a62139
DJ
69
70 sdi = NULL;
71 devc = NULL;
e9a62139
DJ
72 hw_info = NULL;
73
e9a62139
DJ
74 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
75 sr_info("Couldn't get IDN response.");
76 goto fail;
77 }
78
79 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
80 goto fail;
81
aac29cc1 82 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
83 sdi->status = SR_ST_ACTIVE;
84 sdi->vendor = g_strdup(hw_info->manufacturer);
85 sdi->model = g_strdup(hw_info->model);
86 sdi->version = g_strdup(hw_info->firmware_version);
87 sdi->serial_num = g_strdup(hw_info->serial_number);
b33db61c
SA
88 sdi->driver = di;
89 sdi->inst_type = SR_INST_SCPI;
90 sdi->conn = scpi;
b33db61c 91
e9a62139
DJ
92 sr_scpi_hw_info_free(hw_info);
93 hw_info = NULL;
94
f57d8ffe 95 devc = g_malloc0(sizeof(struct dev_context));
e9a62139 96
e9a62139 97 sdi->priv = devc;
e9a62139
DJ
98
99 if (hmo_init_device(sdi) != SR_OK)
100 goto fail;
101
68e3d070
DJ
102 sr_scpi_close(sdi->conn);
103
104 sdi->status = SR_ST_INACTIVE;
105
e9a62139
DJ
106 return sdi;
107
108fail:
109 if (hw_info)
110 sr_scpi_hw_info_free(hw_info);
e9a62139
DJ
111 if (sdi)
112 sr_dev_inst_free(sdi);
113 if (devc)
114 g_free(devc);
115
116 return NULL;
117}
118
06a3e78a
DJ
119static GSList *scan(GSList *options)
120{
ca28abd6 121 return sr_scpi_scan(di->priv, options, hmo_probe_serial_device);
06a3e78a
DJ
122}
123
124static GSList *dev_list(void)
125{
126 return ((struct drv_context *)(di->priv))->instances;
127}
128
13f2b9d7
DJ
129static void clear_helper(void *priv)
130{
13f2b9d7 131 struct dev_context *devc;
13f2b9d7
DJ
132
133 devc = priv;
13f2b9d7 134
719eff68 135 hmo_scope_state_free(devc->model_state);
13f2b9d7 136
13f2b9d7
DJ
137 g_free(devc->analog_groups);
138 g_free(devc->digital_groups);
139
140 g_free(devc);
141}
142
06a3e78a
DJ
143static int dev_clear(void)
144{
13f2b9d7 145 return std_dev_clear(di, clear_helper);
06a3e78a
DJ
146}
147
148static int dev_open(struct sr_dev_inst *sdi)
149{
23f43dff 150 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
13f2b9d7 151 return SR_ERR;
06a3e78a 152
719eff68 153 if (hmo_scope_state_get(sdi) != SR_OK)
13f2b9d7 154 return SR_ERR;
06a3e78a
DJ
155
156 sdi->status = SR_ST_ACTIVE;
157
158 return SR_OK;
159}
160
161static int dev_close(struct sr_dev_inst *sdi)
162{
13f2b9d7
DJ
163 if (sdi->status == SR_ST_INACTIVE)
164 return SR_OK;
06a3e78a 165
23f43dff 166 sr_scpi_close(sdi->conn);
06a3e78a
DJ
167
168 sdi->status = SR_ST_INACTIVE;
169
170 return SR_OK;
171}
172
173static int cleanup(void)
174{
175 dev_clear();
176
06a3e78a
DJ
177 return SR_OK;
178}
179
660e398f 180static int check_channel_group(struct dev_context *devc,
53b4680f 181 const struct sr_channel_group *cg)
13f2b9d7
DJ
182{
183 unsigned int i;
184 struct scope_config *model;
185
186 model = devc->model_config;
187
53b4680f 188 if (!cg)
61e77667 189 return CG_NONE;
13f2b9d7
DJ
190
191 for (i = 0; i < model->analog_channels; ++i)
562b7ae5 192 if (cg == devc->analog_groups[i])
61e77667 193 return CG_ANALOG;
13f2b9d7
DJ
194
195 for (i = 0; i < model->digital_pods; ++i)
562b7ae5 196 if (cg == devc->digital_groups[i])
61e77667 197 return CG_DIGITAL;
13f2b9d7 198
660e398f 199 sr_err("Invalid channel group specified.");
89280b1a 200
61e77667 201 return CG_INVALID;
13f2b9d7
DJ
202}
203
584560f1 204static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 205 const struct sr_channel_group *cg)
06a3e78a 206{
61e77667 207 int ret, cg_type;
13f2b9d7 208 unsigned int i;
13f2b9d7
DJ
209 struct dev_context *devc;
210 struct scope_config *model;
14a2f74d 211 struct scope_state *state;
13f2b9d7
DJ
212
213 if (!sdi || !(devc = sdi->priv))
214 return SR_ERR_ARG;
215
61e77667 216 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7 217 return SR_ERR;
06a3e78a 218
13f2b9d7
DJ
219 ret = SR_ERR_NA;
220 model = devc->model_config;
14a2f74d 221 state = devc->model_state;
06a3e78a 222
06a3e78a 223 switch (key) {
bf622e6d 224 case SR_CONF_NUM_HDIV:
13f2b9d7
DJ
225 *data = g_variant_new_int32(model->num_xdivs);
226 ret = SR_OK;
227 break;
3fd2dca2
DJ
228 case SR_CONF_TIMEBASE:
229 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
230 (*model->timebases)[state->timebase][1]);
231 ret = SR_OK;
232 break;
13f2b9d7 233 case SR_CONF_NUM_VDIV:
61e77667 234 if (cg_type == CG_NONE) {
660e398f
UH
235 sr_err("No channel group specified.");
236 return SR_ERR_CHANNEL_GROUP;
61e77667 237 } else if (cg_type == CG_ANALOG) {
13f2b9d7 238 for (i = 0; i < model->analog_channels; ++i) {
562b7ae5 239 if (cg != devc->analog_groups[i])
082972e8
UH
240 continue;
241 *data = g_variant_new_int32(model->num_ydivs);
242 ret = SR_OK;
243 break;
13f2b9d7
DJ
244 }
245
3fd2dca2
DJ
246 } else {
247 ret = SR_ERR_NA;
248 }
249 break;
250 case SR_CONF_VDIV:
61e77667
UH
251 if (cg_type == CG_NONE) {
252 sr_err("No channel group specified.");
253 return SR_ERR_CHANNEL_GROUP;
254 } else if (cg_type == CG_ANALOG) {
3fd2dca2 255 for (i = 0; i < model->analog_channels; ++i) {
562b7ae5 256 if (cg != devc->analog_groups[i])
3fd2dca2
DJ
257 continue;
258 *data = g_variant_new("(tt)",
259 (*model->vdivs)[state->analog_channels[i].vdiv][0],
260 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
261 ret = SR_OK;
262 break;
263 }
264
ccf14618
DJ
265 } else {
266 ret = SR_ERR_NA;
267 }
268 break;
269 case SR_CONF_TRIGGER_SOURCE:
270 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
271 ret = SR_OK;
272 break;
3fd2dca2
DJ
273 case SR_CONF_TRIGGER_SLOPE:
274 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
275 ret = SR_OK;
276 break;
277 case SR_CONF_HORIZ_TRIGGERPOS:
278 *data = g_variant_new_double(state->horiz_triggerpos);
279 ret = SR_OK;
280 break;
ccf14618 281 case SR_CONF_COUPLING:
61e77667 282 if (cg_type == CG_NONE) {
660e398f
UH
283 sr_err("No channel group specified.");
284 return SR_ERR_CHANNEL_GROUP;
61e77667 285 } else if (cg_type == CG_ANALOG) {
ccf14618 286 for (i = 0; i < model->analog_channels; ++i) {
562b7ae5 287 if (cg != devc->analog_groups[i])
ccf14618
DJ
288 continue;
289 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
290 ret = SR_OK;
291 break;
292 }
293
13f2b9d7
DJ
294 } else {
295 ret = SR_ERR_NA;
296 }
297 break;
14a2f74d
DJ
298 case SR_CONF_SAMPLERATE:
299 *data = g_variant_new_uint64(state->sample_rate);
300 ret = SR_OK;
301 break;
06a3e78a 302 default:
13f2b9d7 303 ret = SR_ERR_NA;
06a3e78a
DJ
304 }
305
306 return ret;
307}
308
13f2b9d7
DJ
309static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
310{
311 unsigned int i;
13f2b9d7
DJ
312 GVariant *rational[2];
313 GVariantBuilder gvb;
314
315 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
316
317 for (i = 0; i < n; i++) {
318 rational[0] = g_variant_new_uint64((*array)[i][0]);
319 rational[1] = g_variant_new_uint64((*array)[i][1]);
320
89280b1a 321 /* FIXME: Valgrind reports a memory leak here. */
13f2b9d7
DJ
322 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
323 }
324
325 return g_variant_builder_end(&gvb);
326}
327
584560f1 328static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 329 const struct sr_channel_group *cg)
06a3e78a 330{
61e77667 331 int ret, cg_type;
89280b1a 332 unsigned int i, j;
965b463d 333 char command[MAX_COMMAND_SIZE], float_str[30];
13f2b9d7
DJ
334 struct dev_context *devc;
335 struct scope_config *model;
336 struct scope_state *state;
89280b1a 337 const char *tmp;
ca9b9f48 338 uint64_t p, q;
89280b1a 339 double tmp_d;
23e1ea7a 340 gboolean update_sample_rate;
06a3e78a 341
13f2b9d7
DJ
342 if (!sdi || !(devc = sdi->priv))
343 return SR_ERR_ARG;
344
61e77667 345 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
346 return SR_ERR;
347
348 model = devc->model_config;
349 state = devc->model_state;
23e1ea7a 350 update_sample_rate = FALSE;
13f2b9d7
DJ
351
352 ret = SR_ERR_NA;
06a3e78a 353
06a3e78a 354 switch (key) {
13f2b9d7
DJ
355 case SR_CONF_LIMIT_FRAMES:
356 devc->frame_limit = g_variant_get_uint64(data);
357 ret = SR_OK;
358 break;
13f2b9d7 359 case SR_CONF_TRIGGER_SOURCE:
13f2b9d7 360 tmp = g_variant_get_string(data, NULL);
13f2b9d7 361 for (i = 0; (*model->trigger_sources)[i]; i++) {
082972e8
UH
362 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
363 continue;
364 state->trigger_source = i;
365 g_snprintf(command, sizeof(command),
366 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
367 (*model->trigger_sources)[i]);
13f2b9d7 368
082972e8
UH
369 ret = sr_scpi_send(sdi->conn, command);
370 break;
13f2b9d7 371 }
89280b1a 372 break;
13f2b9d7 373 case SR_CONF_VDIV:
61e77667 374 if (cg_type == CG_NONE) {
660e398f
UH
375 sr_err("No channel group specified.");
376 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
377 }
378
379 g_variant_get(data, "(tt)", &p, &q);
380
381 for (i = 0; i < model->num_vdivs; i++) {
082972e8
UH
382 if (p != (*model->vdivs)[i][0] ||
383 q != (*model->vdivs)[i][1])
384 continue;
385 for (j = 1; j <= model->analog_channels; ++j) {
562b7ae5 386 if (cg != devc->analog_groups[j - 1])
082972e8 387 continue;
8de2dc3b 388 state->analog_channels[j - 1].vdiv = i;
965b463d 389 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
390 g_snprintf(command, sizeof(command),
391 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
965b463d 392 j, float_str);
082972e8
UH
393
394 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
395 sr_scpi_get_opc(sdi->conn) != SR_OK)
396 return SR_ERR;
13f2b9d7 397
13f2b9d7
DJ
398 break;
399 }
082972e8
UH
400
401 ret = SR_OK;
402 break;
13f2b9d7 403 }
89280b1a 404 break;
13f2b9d7 405 case SR_CONF_TIMEBASE:
13f2b9d7
DJ
406 g_variant_get(data, "(tt)", &p, &q);
407
408 for (i = 0; i < model->num_timebases; i++) {
082972e8
UH
409 if (p != (*model->timebases)[i][0] ||
410 q != (*model->timebases)[i][1])
411 continue;
8de2dc3b 412 state->timebase = i;
965b463d 413 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
414 g_snprintf(command, sizeof(command),
415 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
965b463d 416 float_str);
13f2b9d7 417
082972e8 418 ret = sr_scpi_send(sdi->conn, command);
23e1ea7a 419 update_sample_rate = TRUE;
082972e8 420 break;
13f2b9d7 421 }
89280b1a 422 break;
13f2b9d7 423 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 424 tmp_d = g_variant_get_double(data);
13f2b9d7 425
89280b1a 426 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7
DJ
427 return SR_ERR;
428
422a1c0d
DJ
429 state->horiz_triggerpos = tmp_d;
430 tmp_d = -(tmp_d - 0.5) *
431 ((double) (*model->timebases)[state->timebase][0] /
432 (*model->timebases)[state->timebase][1])
433 * model->num_xdivs;
434
435 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
13f2b9d7
DJ
436 g_snprintf(command, sizeof(command),
437 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 438 float_str);
13f2b9d7
DJ
439
440 ret = sr_scpi_send(sdi->conn, command);
89280b1a 441 break;
13f2b9d7 442 case SR_CONF_TRIGGER_SLOPE:
ca9b9f48 443 tmp = g_variant_get_string(data, NULL);
13f2b9d7 444
ca9b9f48
DE
445 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
446 return SR_ERR_ARG;
13f2b9d7 447
ca9b9f48 448 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
13f2b9d7
DJ
449
450 g_snprintf(command, sizeof(command),
451 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
ca9b9f48 452 (state->trigger_slope == 0) ? "POS" : "NEG");
13f2b9d7
DJ
453
454 ret = sr_scpi_send(sdi->conn, command);
89280b1a 455 break;
13f2b9d7 456 case SR_CONF_COUPLING:
61e77667 457 if (cg_type == CG_NONE) {
660e398f
UH
458 sr_err("No channel group specified.");
459 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
460 }
461
462 tmp = g_variant_get_string(data, NULL);
463
464 for (i = 0; (*model->coupling_options)[i]; i++) {
082972e8
UH
465 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
466 continue;
467 for (j = 1; j <= model->analog_channels; ++j) {
562b7ae5 468 if (cg != devc->analog_groups[j - 1])
082972e8
UH
469 continue;
470 state->analog_channels[j-1].coupling = i;
13f2b9d7 471
082972e8
UH
472 g_snprintf(command, sizeof(command),
473 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
474 j, tmp);
475
476 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
477 sr_scpi_get_opc(sdi->conn) != SR_OK)
478 return SR_ERR;
13f2b9d7
DJ
479 break;
480 }
082972e8
UH
481
482 ret = SR_OK;
483 break;
13f2b9d7 484 }
89280b1a 485 break;
06a3e78a
DJ
486 default:
487 ret = SR_ERR_NA;
13f2b9d7 488 break;
06a3e78a
DJ
489 }
490
13f2b9d7
DJ
491 if (ret == SR_OK)
492 ret = sr_scpi_get_opc(sdi->conn);
493
23e1ea7a
DJ
494 if (ret == SR_OK && update_sample_rate)
495 ret = hmo_update_sample_rate(sdi);
496
06a3e78a
DJ
497 return ret;
498}
499
584560f1 500static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 501 const struct sr_channel_group *cg)
06a3e78a 502{
6ec3ef9b
AJ
503 int cg_type = CG_NONE;
504 struct dev_context *devc = NULL;
505 struct scope_config *model = NULL;
13f2b9d7 506
6ec3ef9b
AJ
507 if (sdi && (devc = sdi->priv)) {
508 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
509 return SR_ERR;
13f2b9d7 510
6ec3ef9b
AJ
511 model = devc->model_config;
512 }
06a3e78a 513
06a3e78a 514 switch (key) {
226363c4 515 case SR_CONF_SCAN_OPTIONS:
584560f1
BV
516 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
517 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
226363c4 518 break;
13f2b9d7 519 case SR_CONF_DEVICE_OPTIONS:
61e77667 520 if (cg_type == CG_NONE) {
6ec3ef9b
AJ
521 if (model)
522 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
523 model->devopts, model->num_devopts, sizeof(uint32_t));
524 else
525 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
526 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
61e77667 527 } else if (cg_type == CG_ANALOG) {
584560f1 528 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 529 model->analog_devopts, model->num_analog_devopts,
584560f1 530 sizeof(uint32_t));
13f2b9d7 531 } else {
584560f1
BV
532 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
533 NULL, 0, sizeof(uint32_t));
13f2b9d7
DJ
534 }
535 break;
13f2b9d7 536 case SR_CONF_COUPLING:
61e77667 537 if (cg_type == CG_NONE)
660e398f 538 return SR_ERR_CHANNEL_GROUP;
13f2b9d7 539 *data = g_variant_new_strv(*model->coupling_options,
89280b1a 540 g_strv_length((char **)*model->coupling_options));
13f2b9d7 541 break;
13f2b9d7 542 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
543 if (!model)
544 return SR_ERR_ARG;
13f2b9d7 545 *data = g_variant_new_strv(*model->trigger_sources,
89280b1a 546 g_strv_length((char **)*model->trigger_sources));
13f2b9d7 547 break;
bbabdaf1 548 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
549 if (!model)
550 return SR_ERR_ARG;
bbabdaf1
DJ
551 *data = g_variant_new_strv(*model->trigger_slopes,
552 g_strv_length((char **)*model->trigger_slopes));
553 break;
13f2b9d7 554 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
555 if (!model)
556 return SR_ERR_ARG;
13f2b9d7
DJ
557 *data = build_tuples(model->timebases, model->num_timebases);
558 break;
13f2b9d7 559 case SR_CONF_VDIV:
61e77667 560 if (cg_type == CG_NONE)
660e398f 561 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
562 *data = build_tuples(model->vdivs, model->num_vdivs);
563 break;
06a3e78a
DJ
564 default:
565 return SR_ERR_NA;
566 }
567
13f2b9d7 568 return SR_OK;
06a3e78a
DJ
569}
570
13f2b9d7 571SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 572{
13f2b9d7 573 char command[MAX_COMMAND_SIZE];
ba7dd8bb 574 struct sr_channel *ch;
13f2b9d7
DJ
575 struct dev_context *devc;
576 struct scope_config *model;
577
578 devc = sdi->priv;
579 model = devc->model_config;
580
ba7dd8bb 581 ch = devc->current_channel->data;
13f2b9d7 582
ba7dd8bb 583 switch (ch->type) {
3f239f08 584 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
585 g_snprintf(command, sizeof(command),
586 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
ba7dd8bb 587 ch->index + 1);
13f2b9d7 588 break;
3f239f08 589 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
590 g_snprintf(command, sizeof(command),
591 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 592 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
593 break;
594 default:
ba7dd8bb 595 sr_err("Invalid channel type.");
13f2b9d7
DJ
596 break;
597 }
598
599 return sr_scpi_send(sdi->conn, command);
600}
601
ba7dd8bb 602static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
603{
604 GSList *l;
ba7dd8bb 605 struct sr_channel *ch;
89280b1a 606 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
13f2b9d7 607
89280b1a 608 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
13f2b9d7 609
ba7dd8bb
UH
610 for (l = channels; l; l = l->next) {
611 ch = l->data;
612 switch (ch->type) {
3f239f08 613 case SR_CHANNEL_ANALOG:
ba7dd8bb 614 if (ch->index == 2)
13f2b9d7 615 enabled_chan3 = TRUE;
ba7dd8bb 616 else if (ch->index == 3)
13f2b9d7
DJ
617 enabled_chan4 = TRUE;
618 break;
3f239f08 619 case SR_CHANNEL_LOGIC:
ba7dd8bb 620 if (ch->index < 8)
13f2b9d7
DJ
621 enabled_pod1 = TRUE;
622 else
623 enabled_pod2 = TRUE;
624 break;
13f2b9d7
DJ
625 default:
626 return SR_ERR;
627 }
628 }
629
630 if ((enabled_pod1 && enabled_chan3) ||
631 (enabled_pod2 && enabled_chan4))
632 return SR_ERR;
633
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
DJ
643 struct scope_state *state;
644 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
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
717static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
718{
719 GSList *l;
720 gboolean digital_added;
ba7dd8bb 721 struct sr_channel *ch;
13f2b9d7 722 struct dev_context *devc;
23f43dff 723 struct sr_scpi_dev_inst *scpi;
06a3e78a
DJ
724
725 if (sdi->status != SR_ST_ACTIVE)
726 return SR_ERR_DEV_CLOSED;
727
23f43dff 728 scpi = sdi->conn;
13f2b9d7
DJ
729 devc = sdi->priv;
730 digital_added = FALSE;
731
db81fbb5
SA
732 g_slist_free(devc->enabled_channels);
733 devc->enabled_channels = NULL;
734
ba7dd8bb
UH
735 for (l = sdi->channels; l; l = l->next) {
736 ch = l->data;
737 if (!ch->enabled)
082972e8 738 continue;
ba7dd8bb 739 /* Only add a single digital channel. */
3f239f08 740 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
ba7dd8bb
UH
741 devc->enabled_channels = g_slist_append(
742 devc->enabled_channels, ch);
3f239f08 743 if (ch->type == SR_CHANNEL_LOGIC)
082972e8 744 digital_added = TRUE;
13f2b9d7
DJ
745 }
746 }
06a3e78a 747
ba7dd8bb 748 if (!devc->enabled_channels)
13f2b9d7
DJ
749 return SR_ERR;
750
ba7dd8bb
UH
751 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
752 sr_err("Invalid channel configuration specified!");
13f2b9d7
DJ
753 return SR_ERR_NA;
754 }
755
ba7dd8bb
UH
756 if (hmo_setup_channels(sdi) != SR_OK) {
757 sr_err("Failed to setup channel configuration!");
13f2b9d7
DJ
758 return SR_ERR;
759 }
760
102f1239
BV
761 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
762 hmo_receive_data, (void *)sdi);
13f2b9d7
DJ
763
764 /* Send header packet to the session bus. */
765 std_session_send_df_header(cb_data, LOG_PREFIX);
766
ba7dd8bb 767 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
768
769 return hmo_request_data(sdi);
06a3e78a
DJ
770}
771
772static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
773{
13f2b9d7 774 struct dev_context *devc;
23f43dff 775 struct sr_scpi_dev_inst *scpi;
66e3219d 776 struct sr_datafeed_packet packet;
13f2b9d7 777
06a3e78a
DJ
778 (void)cb_data;
779
66e3219d
DJ
780 packet.type = SR_DF_END;
781 packet.payload = NULL;
782 sr_session_send(sdi, &packet);
783
06a3e78a
DJ
784 if (sdi->status != SR_ST_ACTIVE)
785 return SR_ERR_DEV_CLOSED;
786
13f2b9d7
DJ
787 devc = sdi->priv;
788
ef1a346b 789 devc->num_frames = 0;
ba7dd8bb
UH
790 g_slist_free(devc->enabled_channels);
791 devc->enabled_channels = NULL;
23f43dff 792 scpi = sdi->conn;
102f1239 793 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
794
795 return SR_OK;
796}
797
798SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
799 .name = "hameg-hmo",
89280b1a 800 .longname = "Hameg HMO",
06a3e78a
DJ
801 .api_version = 1,
802 .init = init,
803 .cleanup = cleanup,
804 .scan = scan,
805 .dev_list = dev_list,
806 .dev_clear = dev_clear,
807 .config_get = config_get,
808 .config_set = config_set,
809 .config_list = config_list,
810 .dev_open = dev_open,
811 .dev_close = dev_close,
812 .dev_acquisition_start = dev_acquisition_start,
813 .dev_acquisition_stop = dev_acquisition_stop,
814 .priv = NULL,
815};