]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
hameg-hmo: Update the default serial port options.
[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
dd5c48a6 25static struct sr_dev_driver hameg_hmo_driver_info;
e9a62139
DJ
26
27static const char *manufacturers[] = {
28 "HAMEG",
da1726cc 29 "Rohde&Schwarz",
e9a62139
DJ
30};
31
584560f1 32static const uint32_t scanopts[] = {
13f2b9d7
DJ
33 SR_CONF_CONN,
34 SR_CONF_SERIALCOMM,
35};
36
55fb76b3
UH
37static const uint32_t drvopts[] = {
38 SR_CONF_OSCILLOSCOPE,
39};
40
719eff68 41enum {
61e77667
UH
42 CG_INVALID = -1,
43 CG_NONE,
44 CG_ANALOG,
45 CG_DIGITAL,
719eff68
UH
46};
47
373e92a4 48static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
e9a62139
DJ
49{
50 struct sr_dev_inst *sdi;
51 struct dev_context *devc;
52 struct sr_scpi_hw_info *hw_info;
e9a62139
DJ
53
54 sdi = NULL;
55 devc = NULL;
e9a62139
DJ
56 hw_info = NULL;
57
e9a62139
DJ
58 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
59 sr_info("Couldn't get IDN response.");
60 goto fail;
61 }
62
697fb6dd 63 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
e9a62139
DJ
64 goto fail;
65
aac29cc1 66 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
67 sdi->vendor = g_strdup(hw_info->manufacturer);
68 sdi->model = g_strdup(hw_info->model);
69 sdi->version = g_strdup(hw_info->firmware_version);
70 sdi->serial_num = g_strdup(hw_info->serial_number);
4f840ce9 71 sdi->driver = &hameg_hmo_driver_info;
b33db61c
SA
72 sdi->inst_type = SR_INST_SCPI;
73 sdi->conn = scpi;
b33db61c 74
e9a62139
DJ
75 sr_scpi_hw_info_free(hw_info);
76 hw_info = NULL;
77
f57d8ffe 78 devc = g_malloc0(sizeof(struct dev_context));
e9a62139 79
e9a62139 80 sdi->priv = devc;
e9a62139
DJ
81
82 if (hmo_init_device(sdi) != SR_OK)
83 goto fail;
84
85 return sdi;
86
87fail:
7b365c47 88 sr_scpi_hw_info_free(hw_info);
4bf93988 89 sr_dev_inst_free(sdi);
b1f83103 90 g_free(devc);
e9a62139
DJ
91
92 return NULL;
93}
94
4f840ce9 95static GSList *scan(struct sr_dev_driver *di, GSList *options)
06a3e78a 96{
373e92a4 97 return sr_scpi_scan(di->context, options, probe_device);
06a3e78a
DJ
98}
99
3553451f 100static void clear_helper(struct dev_context *devc)
13f2b9d7 101{
719eff68 102 hmo_scope_state_free(devc->model_state);
13f2b9d7
DJ
103 g_free(devc->analog_groups);
104 g_free(devc->digital_groups);
13f2b9d7
DJ
105}
106
4f840ce9 107static int dev_clear(const struct sr_dev_driver *di)
06a3e78a 108{
3553451f 109 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
06a3e78a
DJ
110}
111
112static int dev_open(struct sr_dev_inst *sdi)
113{
6402c379 114 if (sr_scpi_open(sdi->conn) != SR_OK)
13f2b9d7 115 return SR_ERR;
06a3e78a 116
719eff68 117 if (hmo_scope_state_get(sdi) != SR_OK)
13f2b9d7 118 return SR_ERR;
06a3e78a 119
06a3e78a
DJ
120 return SR_OK;
121}
122
123static int dev_close(struct sr_dev_inst *sdi)
124{
f1ba6b4b 125 return sr_scpi_close(sdi->conn);
06a3e78a
DJ
126}
127
660e398f 128static int check_channel_group(struct dev_context *devc,
53b4680f 129 const struct sr_channel_group *cg)
13f2b9d7 130{
329733d9 131 const struct scope_config *model;
13f2b9d7
DJ
132
133 model = devc->model_config;
134
53b4680f 135 if (!cg)
61e77667 136 return CG_NONE;
13f2b9d7 137
fcd6a8bd
UH
138 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
139 return CG_ANALOG;
13f2b9d7 140
fcd6a8bd
UH
141 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
142 return CG_DIGITAL;
13f2b9d7 143
660e398f 144 sr_err("Invalid channel group specified.");
89280b1a 145
61e77667 146 return CG_INVALID;
13f2b9d7
DJ
147}
148
dd7a72ea
UH
149static int config_get(uint32_t key, GVariant **data,
150 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 151{
fcd6a8bd 152 int cg_type, idx;
13f2b9d7 153 struct dev_context *devc;
329733d9 154 const struct scope_config *model;
14a2f74d 155 struct scope_state *state;
13f2b9d7 156
709468ba 157 if (!sdi)
13f2b9d7
DJ
158 return SR_ERR_ARG;
159
709468ba
UH
160 devc = sdi->priv;
161
61e77667 162 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7 163 return SR_ERR;
06a3e78a 164
13f2b9d7 165 model = devc->model_config;
14a2f74d 166 state = devc->model_state;
06a3e78a 167
06a3e78a 168 switch (key) {
bf622e6d 169 case SR_CONF_NUM_HDIV:
13f2b9d7 170 *data = g_variant_new_int32(model->num_xdivs);
13f2b9d7 171 break;
3fd2dca2
DJ
172 case SR_CONF_TIMEBASE:
173 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
174 (*model->timebases)[state->timebase][1]);
3fd2dca2 175 break;
13f2b9d7 176 case SR_CONF_NUM_VDIV:
3782e571 177 if (!cg)
660e398f 178 return SR_ERR_CHANNEL_GROUP;
3782e571 179 if (cg_type != CG_ANALOG)
758906aa 180 return SR_ERR_NA;
3782e571
UH
181 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
182 return SR_ERR_ARG;
183 *data = g_variant_new_int32(model->num_ydivs);
3fd2dca2
DJ
184 break;
185 case SR_CONF_VDIV:
3782e571 186 if (!cg)
61e77667 187 return SR_ERR_CHANNEL_GROUP;
3782e571 188 if (cg_type != CG_ANALOG)
758906aa 189 return SR_ERR_NA;
3782e571
UH
190 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
191 return SR_ERR_ARG;
192 *data = g_variant_new("(tt)",
193 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
194 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
ccf14618
DJ
195 break;
196 case SR_CONF_TRIGGER_SOURCE:
197 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
ccf14618 198 break;
3fd2dca2
DJ
199 case SR_CONF_TRIGGER_SLOPE:
200 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
3fd2dca2
DJ
201 break;
202 case SR_CONF_HORIZ_TRIGGERPOS:
203 *data = g_variant_new_double(state->horiz_triggerpos);
3fd2dca2 204 break;
ccf14618 205 case SR_CONF_COUPLING:
3782e571 206 if (!cg)
660e398f 207 return SR_ERR_CHANNEL_GROUP;
3782e571 208 if (cg_type != CG_ANALOG)
758906aa 209 return SR_ERR_NA;
3782e571
UH
210 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
211 return SR_ERR_ARG;
212 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
13f2b9d7 213 break;
14a2f74d
DJ
214 case SR_CONF_SAMPLERATE:
215 *data = g_variant_new_uint64(state->sample_rate);
14a2f74d 216 break;
06a3e78a 217 default:
758906aa 218 return SR_ERR_NA;
06a3e78a
DJ
219 }
220
758906aa 221 return SR_OK;
06a3e78a
DJ
222}
223
dd7a72ea
UH
224static int config_set(uint32_t key, GVariant *data,
225 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 226{
fcd6a8bd 227 int ret, cg_type, idx, j;
965b463d 228 char command[MAX_COMMAND_SIZE], float_str[30];
13f2b9d7 229 struct dev_context *devc;
329733d9 230 const struct scope_config *model;
13f2b9d7 231 struct scope_state *state;
89280b1a 232 double tmp_d;
23e1ea7a 233 gboolean update_sample_rate;
06a3e78a 234
b0baddef 235 if (!sdi)
13f2b9d7
DJ
236 return SR_ERR_ARG;
237
b0baddef
UH
238 devc = sdi->priv;
239
61e77667 240 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
241 return SR_ERR;
242
243 model = devc->model_config;
244 state = devc->model_state;
23e1ea7a 245 update_sample_rate = FALSE;
13f2b9d7 246
06a3e78a 247 switch (key) {
d779dcac
GT
248 case SR_CONF_LIMIT_SAMPLES:
249 devc->samples_limit = g_variant_get_uint64(data);
250 ret = SR_OK;
251 break;
13f2b9d7
DJ
252 case SR_CONF_LIMIT_FRAMES:
253 devc->frame_limit = g_variant_get_uint64(data);
254 ret = SR_OK;
255 break;
13f2b9d7 256 case SR_CONF_TRIGGER_SOURCE:
bd633efa
UH
257 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
258 return SR_ERR_ARG;
259 state->trigger_source = idx;
260 g_snprintf(command, sizeof(command),
261 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
262 (*model->trigger_sources)[idx]);
263 ret = sr_scpi_send(sdi->conn, command);
89280b1a 264 break;
13f2b9d7 265 case SR_CONF_VDIV:
61233697 266 if (!cg)
660e398f 267 return SR_ERR_CHANNEL_GROUP;
697fb6dd
UH
268 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
269 return SR_ERR_ARG;
fcd6a8bd
UH
270 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
271 return SR_ERR_ARG;
272 state->analog_channels[j].vdiv = idx;
273 g_ascii_formatd(float_str, sizeof(float_str), "%E",
274 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
275 g_snprintf(command, sizeof(command),
276 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
277 j + 1, float_str);
278 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
279 sr_scpi_get_opc(sdi->conn) != SR_OK)
280 return SR_ERR;
697fb6dd 281 ret = SR_OK;
89280b1a 282 break;
13f2b9d7 283 case SR_CONF_TIMEBASE:
697fb6dd
UH
284 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
285 return SR_ERR_ARG;
286 state->timebase = idx;
287 g_ascii_formatd(float_str, sizeof(float_str), "%E",
288 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
289 g_snprintf(command, sizeof(command),
290 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
291 float_str);
292 ret = sr_scpi_send(sdi->conn, command);
293 update_sample_rate = TRUE;
89280b1a 294 break;
13f2b9d7 295 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 296 tmp_d = g_variant_get_double(data);
89280b1a 297 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7 298 return SR_ERR;
422a1c0d
DJ
299 state->horiz_triggerpos = tmp_d;
300 tmp_d = -(tmp_d - 0.5) *
301 ((double) (*model->timebases)[state->timebase][0] /
302 (*model->timebases)[state->timebase][1])
303 * model->num_xdivs;
422a1c0d 304 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
13f2b9d7
DJ
305 g_snprintf(command, sizeof(command),
306 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 307 float_str);
13f2b9d7 308 ret = sr_scpi_send(sdi->conn, command);
89280b1a 309 break;
13f2b9d7 310 case SR_CONF_TRIGGER_SLOPE:
bd633efa
UH
311 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
312 return SR_ERR_ARG;
313 state->trigger_slope = idx;
314 g_snprintf(command, sizeof(command),
315 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
316 (*model->trigger_slopes)[idx]);
317 ret = sr_scpi_send(sdi->conn, command);
89280b1a 318 break;
13f2b9d7 319 case SR_CONF_COUPLING:
61233697 320 if (!cg)
660e398f 321 return SR_ERR_CHANNEL_GROUP;
bd633efa
UH
322 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
323 return SR_ERR_ARG;
fcd6a8bd
UH
324 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
325 return SR_ERR_ARG;
326 state->analog_channels[j].coupling = idx;
327 g_snprintf(command, sizeof(command),
328 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
329 j + 1, (*model->coupling_options)[idx]);
330 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
331 sr_scpi_get_opc(sdi->conn) != SR_OK)
332 return SR_ERR;
bd633efa 333 ret = SR_OK;
89280b1a 334 break;
06a3e78a
DJ
335 default:
336 ret = SR_ERR_NA;
13f2b9d7 337 break;
06a3e78a
DJ
338 }
339
13f2b9d7
DJ
340 if (ret == SR_OK)
341 ret = sr_scpi_get_opc(sdi->conn);
342
23e1ea7a
DJ
343 if (ret == SR_OK && update_sample_rate)
344 ret = hmo_update_sample_rate(sdi);
345
06a3e78a
DJ
346 return ret;
347}
348
dd7a72ea
UH
349static int config_list(uint32_t key, GVariant **data,
350 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 351{
6ec3ef9b
AJ
352 int cg_type = CG_NONE;
353 struct dev_context *devc = NULL;
329733d9 354 const struct scope_config *model = NULL;
13f2b9d7 355
4d399734
UH
356 if (sdi) {
357 devc = sdi->priv;
6ec3ef9b
AJ
358 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
359 return SR_ERR;
13f2b9d7 360
6ec3ef9b
AJ
361 model = devc->model_config;
362 }
06a3e78a 363
06a3e78a 364 switch (key) {
226363c4 365 case SR_CONF_SCAN_OPTIONS:
53012da6 366 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
226363c4 367 break;
13f2b9d7 368 case SR_CONF_DEVICE_OPTIONS:
61233697 369 if (!cg) {
6ec3ef9b 370 if (model)
769561cb 371 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
6ec3ef9b 372 else
53012da6 373 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
61e77667 374 } else if (cg_type == CG_ANALOG) {
769561cb 375 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
13f2b9d7 376 } else {
105df674 377 *data = std_gvar_array_u32(NULL, 0);
13f2b9d7
DJ
378 }
379 break;
13f2b9d7 380 case SR_CONF_COUPLING:
61233697 381 if (!cg)
660e398f 382 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
383 if (!model)
384 return SR_ERR_ARG;
692716f5 385 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
13f2b9d7 386 break;
13f2b9d7 387 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
388 if (!model)
389 return SR_ERR_ARG;
692716f5 390 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
13f2b9d7 391 break;
bbabdaf1 392 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
393 if (!model)
394 return SR_ERR_ARG;
692716f5 395 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
bbabdaf1 396 break;
13f2b9d7 397 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
398 if (!model)
399 return SR_ERR_ARG;
58ffcf97 400 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
13f2b9d7 401 break;
13f2b9d7 402 case SR_CONF_VDIV:
61233697 403 if (!cg)
660e398f 404 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
405 if (!model)
406 return SR_ERR_ARG;
58ffcf97 407 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
13f2b9d7 408 break;
06a3e78a
DJ
409 default:
410 return SR_ERR_NA;
411 }
412
13f2b9d7 413 return SR_OK;
06a3e78a
DJ
414}
415
13f2b9d7 416SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 417{
13f2b9d7 418 char command[MAX_COMMAND_SIZE];
ba7dd8bb 419 struct sr_channel *ch;
13f2b9d7 420 struct dev_context *devc;
329733d9 421 const struct scope_config *model;
13f2b9d7
DJ
422
423 devc = sdi->priv;
424 model = devc->model_config;
425
ba7dd8bb 426 ch = devc->current_channel->data;
13f2b9d7 427
ba7dd8bb 428 switch (ch->type) {
3f239f08 429 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
430 g_snprintf(command, sizeof(command),
431 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
65a6794e
GS
432#ifdef WORDS_BIGENDIAN
433 "MSBF",
434#else
435 "LSBF",
436#endif
ba7dd8bb 437 ch->index + 1);
13f2b9d7 438 break;
3f239f08 439 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
440 g_snprintf(command, sizeof(command),
441 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 442 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
443 break;
444 default:
ba7dd8bb 445 sr_err("Invalid channel type.");
13f2b9d7
DJ
446 break;
447 }
448
449 return sr_scpi_send(sdi->conn, command);
450}
451
ba7dd8bb 452static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
453{
454 GSList *l;
ba7dd8bb 455 struct sr_channel *ch;
4889acef
GS
456 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
457 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
458 size_t idx;
459
460 /* Preset "not enabled" for all channels / pods. */
461 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
462 enabled_chan[idx] = FALSE;
463 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
464 enabled_pod[idx] = FALSE;
465
466 /*
467 * Determine which channels / pods are required for the caller's
468 * specified configuration.
469 */
ba7dd8bb
UH
470 for (l = channels; l; l = l->next) {
471 ch = l->data;
472 switch (ch->type) {
3f239f08 473 case SR_CHANNEL_ANALOG:
4889acef
GS
474 idx = ch->index;
475 if (idx < ARRAY_SIZE(enabled_chan))
476 enabled_chan[idx] = TRUE;
13f2b9d7 477 break;
3f239f08 478 case SR_CHANNEL_LOGIC:
4889acef
GS
479 idx = ch->index / 8;
480 if (idx < ARRAY_SIZE(enabled_pod))
481 enabled_pod[idx] = TRUE;
13f2b9d7 482 break;
13f2b9d7
DJ
483 default:
484 return SR_ERR;
485 }
486 }
487
4889acef
GS
488 /*
489 * Check for resource conflicts. Some channels can be either
490 * analog or digital, but never both at the same time.
491 *
492 * Note that the constraints might depend on the specific model.
493 * These tests might need some adjustment when support for more
494 * models gets added to the driver.
495 */
496 if (enabled_pod[0] && enabled_chan[2])
497 return SR_ERR;
498 if (enabled_pod[1] && enabled_chan[3])
13f2b9d7 499 return SR_ERR;
13f2b9d7
DJ
500 return SR_OK;
501}
502
ba7dd8bb 503static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
504{
505 GSList *l;
506 unsigned int i;
23e1ea7a 507 gboolean *pod_enabled, setup_changed;
13f2b9d7 508 char command[MAX_COMMAND_SIZE];
13f2b9d7 509 struct scope_state *state;
329733d9 510 const struct scope_config *model;
ba7dd8bb 511 struct sr_channel *ch;
13f2b9d7 512 struct dev_context *devc;
23f43dff 513 struct sr_scpi_dev_inst *scpi;
addbb09b 514 int ret;
13f2b9d7
DJ
515
516 devc = sdi->priv;
23f43dff 517 scpi = sdi->conn;
13f2b9d7
DJ
518 state = devc->model_state;
519 model = devc->model_config;
23e1ea7a 520 setup_changed = FALSE;
13f2b9d7
DJ
521
522 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
523
ba7dd8bb
UH
524 for (l = sdi->channels; l; l = l->next) {
525 ch = l->data;
526 switch (ch->type) {
3f239f08 527 case SR_CHANNEL_ANALOG:
ba7dd8bb 528 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
529 break;
530 g_snprintf(command, sizeof(command),
531 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 532 ch->index + 1, ch->enabled);
13f2b9d7 533
4fc4b8e7
UH
534 if (sr_scpi_send(scpi, command) != SR_OK) {
535 g_free(pod_enabled);
082972e8 536 return SR_ERR;
4fc4b8e7 537 }
ba7dd8bb 538 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 539 setup_changed = TRUE;
89280b1a 540 break;
3f239f08 541 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
542 /*
543 * A digital POD needs to be enabled for every group of
ba7dd8bb 544 * 8 channels.
13f2b9d7 545 */
ba7dd8bb
UH
546 if (ch->enabled)
547 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
13f2b9d7 548
ba7dd8bb 549 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
550 break;
551 g_snprintf(command, sizeof(command),
552 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 553 ch->index, ch->enabled);
13f2b9d7 554
4fc4b8e7
UH
555 if (sr_scpi_send(scpi, command) != SR_OK) {
556 g_free(pod_enabled);
082972e8 557 return SR_ERR;
4fc4b8e7 558 }
13f2b9d7 559
ba7dd8bb 560 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 561 setup_changed = TRUE;
89280b1a 562 break;
13f2b9d7 563 default:
b0e80e9a 564 g_free(pod_enabled);
13f2b9d7
DJ
565 return SR_ERR;
566 }
567 }
568
addbb09b 569 ret = SR_OK;
d1ac53cc
UH
570 for (i = 0; i < model->digital_pods; i++) {
571 if (state->digital_pods[i] == pod_enabled[i])
082972e8
UH
572 continue;
573 g_snprintf(command, sizeof(command),
574 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
d1ac53cc 575 i + 1, pod_enabled[i]);
addbb09b
GS
576 if (sr_scpi_send(scpi, command) != SR_OK) {
577 ret = SR_ERR;
578 break;
579 }
d1ac53cc 580 state->digital_pods[i] = pod_enabled[i];
23e1ea7a 581 setup_changed = TRUE;
13f2b9d7 582 }
13f2b9d7 583 g_free(pod_enabled);
addbb09b
GS
584 if (ret != SR_OK)
585 return ret;
13f2b9d7 586
23e1ea7a
DJ
587 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
588 return SR_ERR;
589
13f2b9d7
DJ
590 return SR_OK;
591}
592
695dc859 593static int dev_acquisition_start(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
594{
595 GSList *l;
1b0f62df 596 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
e06875b2 597 size_t group, pod_count;
ba7dd8bb 598 struct sr_channel *ch;
13f2b9d7 599 struct dev_context *devc;
23f43dff 600 struct sr_scpi_dev_inst *scpi;
40a75c8e 601 int ret;
06a3e78a 602
23f43dff 603 scpi = sdi->conn;
13f2b9d7 604 devc = sdi->priv;
13f2b9d7 605
d779dcac
GT
606 devc->num_samples = 0;
607 devc->num_frames = 0;
608
1b0f62df
GS
609 /* Preset empty results. */
610 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
611 digital_added[group] = FALSE;
db81fbb5
SA
612 g_slist_free(devc->enabled_channels);
613 devc->enabled_channels = NULL;
614
1b0f62df 615 /*
e06875b2
GS
616 * Contruct the list of enabled channels. Determine the highest
617 * number of digital pods involved in the acquisition.
1b0f62df 618 */
e06875b2 619 pod_count = 0;
ba7dd8bb
UH
620 for (l = sdi->channels; l; l = l->next) {
621 ch = l->data;
622 if (!ch->enabled)
082972e8 623 continue;
1b0f62df
GS
624 /* Only add a single digital channel per group (pod). */
625 group = ch->index / 8;
626 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
ba7dd8bb
UH
627 devc->enabled_channels = g_slist_append(
628 devc->enabled_channels, ch);
e06875b2 629 if (ch->type == SR_CHANNEL_LOGIC) {
1b0f62df 630 digital_added[group] = TRUE;
e06875b2
GS
631 if (pod_count < group + 1)
632 pod_count = group + 1;
633 }
13f2b9d7
DJ
634 }
635 }
ba7dd8bb 636 if (!devc->enabled_channels)
13f2b9d7 637 return SR_ERR;
e06875b2
GS
638 devc->pod_count = pod_count;
639 devc->logic_data = NULL;
13f2b9d7 640
1b0f62df
GS
641 /*
642 * Check constraints. Some channels can be either analog or
643 * digital, but not both at the same time.
644 */
ba7dd8bb
UH
645 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
646 sr_err("Invalid channel configuration specified!");
40a75c8e
GS
647 ret = SR_ERR_NA;
648 goto free_enabled;
13f2b9d7
DJ
649 }
650
1b0f62df
GS
651 /*
652 * Configure the analog and digital channels and the
653 * corresponding digital pods.
654 */
ba7dd8bb
UH
655 if (hmo_setup_channels(sdi) != SR_OK) {
656 sr_err("Failed to setup channel configuration!");
40a75c8e
GS
657 ret = SR_ERR;
658 goto free_enabled;
13f2b9d7
DJ
659 }
660
1b0f62df
GS
661 /*
662 * Start acquisition on the first enabled channel. The
663 * receive routine will continue driving the acquisition.
664 */
102f1239
BV
665 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
666 hmo_receive_data, (void *)sdi);
13f2b9d7 667
bee2b016 668 std_session_send_df_header(sdi);
13f2b9d7 669
ba7dd8bb 670 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
671
672 return hmo_request_data(sdi);
40a75c8e
GS
673
674free_enabled:
675 g_slist_free(devc->enabled_channels);
676 devc->enabled_channels = NULL;
677 return ret;
06a3e78a
DJ
678}
679
695dc859 680static int dev_acquisition_stop(struct sr_dev_inst *sdi)
06a3e78a 681{
13f2b9d7 682 struct dev_context *devc;
23f43dff 683 struct sr_scpi_dev_inst *scpi;
13f2b9d7 684
bee2b016 685 std_session_send_df_end(sdi);
66e3219d 686
13f2b9d7
DJ
687 devc = sdi->priv;
688
d779dcac 689 devc->num_samples = 0;
ef1a346b 690 devc->num_frames = 0;
ba7dd8bb
UH
691 g_slist_free(devc->enabled_channels);
692 devc->enabled_channels = NULL;
23f43dff 693 scpi = sdi->conn;
102f1239 694 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
695
696 return SR_OK;
697}
698
dd5c48a6 699static struct sr_dev_driver hameg_hmo_driver_info = {
06a3e78a 700 .name = "hameg-hmo",
89280b1a 701 .longname = "Hameg HMO",
06a3e78a 702 .api_version = 1,
c2fdcc25 703 .init = std_init,
700d6b64 704 .cleanup = std_cleanup,
06a3e78a 705 .scan = scan,
c01bf34c 706 .dev_list = std_dev_list,
06a3e78a
DJ
707 .dev_clear = dev_clear,
708 .config_get = config_get,
709 .config_set = config_set,
710 .config_list = config_list,
711 .dev_open = dev_open,
712 .dev_close = dev_close,
713 .dev_acquisition_start = dev_acquisition_start,
714 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 715 .context = NULL,
06a3e78a 716};
dd5c48a6 717SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);