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