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