]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
drivers: Use array-based approach in some 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;
89280b1a 249 unsigned int i, 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 const char *tmp;
89280b1a 255 double tmp_d;
23e1ea7a 256 gboolean update_sample_rate;
06a3e78a 257
b0baddef 258 if (!sdi)
13f2b9d7
DJ
259 return SR_ERR_ARG;
260
b0baddef
UH
261 devc = sdi->priv;
262
61e77667 263 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
264 return SR_ERR;
265
266 model = devc->model_config;
267 state = devc->model_state;
23e1ea7a 268 update_sample_rate = FALSE;
13f2b9d7
DJ
269
270 ret = SR_ERR_NA;
06a3e78a 271
06a3e78a 272 switch (key) {
13f2b9d7
DJ
273 case SR_CONF_LIMIT_FRAMES:
274 devc->frame_limit = g_variant_get_uint64(data);
275 ret = SR_OK;
276 break;
13f2b9d7 277 case SR_CONF_TRIGGER_SOURCE:
13f2b9d7 278 tmp = g_variant_get_string(data, NULL);
692716f5 279 for (i = 0; i < model->num_trigger_sources; i++) {
082972e8
UH
280 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
281 continue;
282 state->trigger_source = i;
283 g_snprintf(command, sizeof(command),
284 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
285 (*model->trigger_sources)[i]);
13f2b9d7 286
082972e8
UH
287 ret = sr_scpi_send(sdi->conn, command);
288 break;
13f2b9d7 289 }
89280b1a 290 break;
13f2b9d7 291 case SR_CONF_VDIV:
87dc5410 292 if (cg_type == CG_NONE)
660e398f 293 return SR_ERR_CHANNEL_GROUP;
697fb6dd
UH
294 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
295 return SR_ERR_ARG;
296 for (j = 1; j <= model->analog_channels; j++) {
297 if (cg != devc->analog_groups[j - 1])
082972e8 298 continue;
697fb6dd
UH
299 state->analog_channels[j - 1].vdiv = idx;
300 g_ascii_formatd(float_str, sizeof(float_str), "%E",
301 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
302 g_snprintf(command, sizeof(command),
303 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
304 j, float_str);
305 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
306 sr_scpi_get_opc(sdi->conn) != SR_OK)
307 return SR_ERR;
082972e8 308 break;
13f2b9d7 309 }
697fb6dd 310 ret = SR_OK;
89280b1a 311 break;
13f2b9d7 312 case SR_CONF_TIMEBASE:
697fb6dd
UH
313 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
314 return SR_ERR_ARG;
315 state->timebase = idx;
316 g_ascii_formatd(float_str, sizeof(float_str), "%E",
317 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
318 g_snprintf(command, sizeof(command),
319 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
320 float_str);
321 ret = sr_scpi_send(sdi->conn, command);
322 update_sample_rate = TRUE;
89280b1a 323 break;
13f2b9d7 324 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 325 tmp_d = g_variant_get_double(data);
13f2b9d7 326
89280b1a 327 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7
DJ
328 return SR_ERR;
329
422a1c0d
DJ
330 state->horiz_triggerpos = tmp_d;
331 tmp_d = -(tmp_d - 0.5) *
332 ((double) (*model->timebases)[state->timebase][0] /
333 (*model->timebases)[state->timebase][1])
334 * model->num_xdivs;
335
336 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
13f2b9d7
DJ
337 g_snprintf(command, sizeof(command),
338 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 339 float_str);
13f2b9d7
DJ
340
341 ret = sr_scpi_send(sdi->conn, command);
89280b1a 342 break;
13f2b9d7 343 case SR_CONF_TRIGGER_SLOPE:
ca9b9f48 344 tmp = g_variant_get_string(data, NULL);
692716f5 345 for (i = 0; i < model->num_trigger_slopes; i++) {
e3abd15d
SB
346 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
347 continue;
348 state->trigger_slope = i;
349 g_snprintf(command, sizeof(command),
350 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
351 (*model->trigger_slopes)[i]);
13f2b9d7 352
e3abd15d
SB
353 ret = sr_scpi_send(sdi->conn, command);
354 break;
355 }
89280b1a 356 break;
13f2b9d7 357 case SR_CONF_COUPLING:
87dc5410 358 if (cg_type == CG_NONE)
660e398f 359 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
360
361 tmp = g_variant_get_string(data, NULL);
362
692716f5 363 for (i = 0; i < model->num_coupling_options; i++) {
082972e8
UH
364 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
365 continue;
0a1f7b09 366 for (j = 1; j <= model->analog_channels; j++) {
562b7ae5 367 if (cg != devc->analog_groups[j - 1])
082972e8
UH
368 continue;
369 state->analog_channels[j-1].coupling = i;
13f2b9d7 370
082972e8
UH
371 g_snprintf(command, sizeof(command),
372 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
373 j, tmp);
374
375 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
376 sr_scpi_get_opc(sdi->conn) != SR_OK)
377 return SR_ERR;
13f2b9d7
DJ
378 break;
379 }
082972e8
UH
380
381 ret = SR_OK;
382 break;
13f2b9d7 383 }
89280b1a 384 break;
06a3e78a
DJ
385 default:
386 ret = SR_ERR_NA;
13f2b9d7 387 break;
06a3e78a
DJ
388 }
389
13f2b9d7
DJ
390 if (ret == SR_OK)
391 ret = sr_scpi_get_opc(sdi->conn);
392
23e1ea7a
DJ
393 if (ret == SR_OK && update_sample_rate)
394 ret = hmo_update_sample_rate(sdi);
395
06a3e78a
DJ
396 return ret;
397}
398
dd7a72ea
UH
399static int config_list(uint32_t key, GVariant **data,
400 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 401{
6ec3ef9b
AJ
402 int cg_type = CG_NONE;
403 struct dev_context *devc = NULL;
329733d9 404 const struct scope_config *model = NULL;
13f2b9d7 405
4d399734
UH
406 if (sdi) {
407 devc = sdi->priv;
6ec3ef9b
AJ
408 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
409 return SR_ERR;
13f2b9d7 410
6ec3ef9b
AJ
411 model = devc->model_config;
412 }
06a3e78a 413
06a3e78a 414 switch (key) {
226363c4 415 case SR_CONF_SCAN_OPTIONS:
53012da6 416 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
226363c4 417 break;
13f2b9d7 418 case SR_CONF_DEVICE_OPTIONS:
61e77667 419 if (cg_type == CG_NONE) {
6ec3ef9b 420 if (model)
105df674 421 *data = std_gvar_array_u32((const uint32_t *)model->devopts, model->num_devopts);
6ec3ef9b 422 else
53012da6 423 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
61e77667 424 } else if (cg_type == CG_ANALOG) {
105df674 425 *data = std_gvar_array_u32((const uint32_t *)model->devopts_cg_analog, model->num_devopts_cg_analog);
13f2b9d7 426 } else {
105df674 427 *data = std_gvar_array_u32(NULL, 0);
13f2b9d7
DJ
428 }
429 break;
13f2b9d7 430 case SR_CONF_COUPLING:
61e77667 431 if (cg_type == CG_NONE)
660e398f 432 return SR_ERR_CHANNEL_GROUP;
692716f5 433 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
13f2b9d7 434 break;
13f2b9d7 435 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
436 if (!model)
437 return SR_ERR_ARG;
692716f5 438 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
13f2b9d7 439 break;
bbabdaf1 440 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
441 if (!model)
442 return SR_ERR_ARG;
692716f5 443 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
bbabdaf1 444 break;
13f2b9d7 445 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
446 if (!model)
447 return SR_ERR_ARG;
58ffcf97 448 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
13f2b9d7 449 break;
13f2b9d7 450 case SR_CONF_VDIV:
61e77667 451 if (cg_type == CG_NONE)
660e398f 452 return SR_ERR_CHANNEL_GROUP;
58ffcf97 453 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
13f2b9d7 454 break;
06a3e78a
DJ
455 default:
456 return SR_ERR_NA;
457 }
458
13f2b9d7 459 return SR_OK;
06a3e78a
DJ
460}
461
13f2b9d7 462SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 463{
13f2b9d7 464 char command[MAX_COMMAND_SIZE];
ba7dd8bb 465 struct sr_channel *ch;
13f2b9d7 466 struct dev_context *devc;
329733d9 467 const struct scope_config *model;
13f2b9d7
DJ
468
469 devc = sdi->priv;
470 model = devc->model_config;
471
ba7dd8bb 472 ch = devc->current_channel->data;
13f2b9d7 473
ba7dd8bb 474 switch (ch->type) {
3f239f08 475 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
476 g_snprintf(command, sizeof(command),
477 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
65a6794e
GS
478#ifdef WORDS_BIGENDIAN
479 "MSBF",
480#else
481 "LSBF",
482#endif
ba7dd8bb 483 ch->index + 1);
13f2b9d7 484 break;
3f239f08 485 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
486 g_snprintf(command, sizeof(command),
487 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 488 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
489 break;
490 default:
ba7dd8bb 491 sr_err("Invalid channel type.");
13f2b9d7
DJ
492 break;
493 }
494
495 return sr_scpi_send(sdi->conn, command);
496}
497
ba7dd8bb 498static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
499{
500 GSList *l;
ba7dd8bb 501 struct sr_channel *ch;
4889acef
GS
502 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
503 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
504 size_t idx;
505
506 /* Preset "not enabled" for all channels / pods. */
507 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
508 enabled_chan[idx] = FALSE;
509 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
510 enabled_pod[idx] = FALSE;
511
512 /*
513 * Determine which channels / pods are required for the caller's
514 * specified configuration.
515 */
ba7dd8bb
UH
516 for (l = channels; l; l = l->next) {
517 ch = l->data;
518 switch (ch->type) {
3f239f08 519 case SR_CHANNEL_ANALOG:
4889acef
GS
520 idx = ch->index;
521 if (idx < ARRAY_SIZE(enabled_chan))
522 enabled_chan[idx] = TRUE;
13f2b9d7 523 break;
3f239f08 524 case SR_CHANNEL_LOGIC:
4889acef
GS
525 idx = ch->index / 8;
526 if (idx < ARRAY_SIZE(enabled_pod))
527 enabled_pod[idx] = TRUE;
13f2b9d7 528 break;
13f2b9d7
DJ
529 default:
530 return SR_ERR;
531 }
532 }
533
4889acef
GS
534 /*
535 * Check for resource conflicts. Some channels can be either
536 * analog or digital, but never both at the same time.
537 *
538 * Note that the constraints might depend on the specific model.
539 * These tests might need some adjustment when support for more
540 * models gets added to the driver.
541 */
542 if (enabled_pod[0] && enabled_chan[2])
543 return SR_ERR;
544 if (enabled_pod[1] && enabled_chan[3])
13f2b9d7 545 return SR_ERR;
13f2b9d7
DJ
546 return SR_OK;
547}
548
ba7dd8bb 549static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
550{
551 GSList *l;
552 unsigned int i;
23e1ea7a 553 gboolean *pod_enabled, setup_changed;
13f2b9d7 554 char command[MAX_COMMAND_SIZE];
13f2b9d7 555 struct scope_state *state;
329733d9 556 const struct scope_config *model;
ba7dd8bb 557 struct sr_channel *ch;
13f2b9d7 558 struct dev_context *devc;
23f43dff 559 struct sr_scpi_dev_inst *scpi;
13f2b9d7
DJ
560
561 devc = sdi->priv;
23f43dff 562 scpi = sdi->conn;
13f2b9d7
DJ
563 state = devc->model_state;
564 model = devc->model_config;
23e1ea7a 565 setup_changed = FALSE;
13f2b9d7
DJ
566
567 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
568
ba7dd8bb
UH
569 for (l = sdi->channels; l; l = l->next) {
570 ch = l->data;
571 switch (ch->type) {
3f239f08 572 case SR_CHANNEL_ANALOG:
ba7dd8bb 573 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
574 break;
575 g_snprintf(command, sizeof(command),
576 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 577 ch->index + 1, ch->enabled);
13f2b9d7 578
23f43dff 579 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 580 return SR_ERR;
ba7dd8bb 581 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 582 setup_changed = TRUE;
89280b1a 583 break;
3f239f08 584 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
585 /*
586 * A digital POD needs to be enabled for every group of
ba7dd8bb 587 * 8 channels.
13f2b9d7 588 */
ba7dd8bb
UH
589 if (ch->enabled)
590 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
13f2b9d7 591
ba7dd8bb 592 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
593 break;
594 g_snprintf(command, sizeof(command),
595 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 596 ch->index, ch->enabled);
13f2b9d7 597
23f43dff 598 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 599 return SR_ERR;
13f2b9d7 600
ba7dd8bb 601 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 602 setup_changed = TRUE;
89280b1a 603 break;
13f2b9d7
DJ
604 default:
605 return SR_ERR;
606 }
607 }
608
0a1f7b09 609 for (i = 1; i <= model->digital_pods; i++) {
082972e8
UH
610 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
611 continue;
612 g_snprintf(command, sizeof(command),
613 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
614 i, pod_enabled[i - 1]);
23f43dff 615 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
616 return SR_ERR;
617 state->digital_pods[i - 1] = pod_enabled[i - 1];
23e1ea7a 618 setup_changed = TRUE;
13f2b9d7
DJ
619 }
620
621 g_free(pod_enabled);
622
23e1ea7a
DJ
623 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
624 return SR_ERR;
625
13f2b9d7
DJ
626 return SR_OK;
627}
628
695dc859 629static int dev_acquisition_start(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
630{
631 GSList *l;
1b0f62df 632 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
e06875b2 633 size_t group, pod_count;
ba7dd8bb 634 struct sr_channel *ch;
13f2b9d7 635 struct dev_context *devc;
23f43dff 636 struct sr_scpi_dev_inst *scpi;
40a75c8e 637 int ret;
06a3e78a 638
23f43dff 639 scpi = sdi->conn;
13f2b9d7 640 devc = sdi->priv;
13f2b9d7 641
1b0f62df
GS
642 /* Preset empty results. */
643 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
644 digital_added[group] = FALSE;
db81fbb5
SA
645 g_slist_free(devc->enabled_channels);
646 devc->enabled_channels = NULL;
647
1b0f62df 648 /*
e06875b2
GS
649 * Contruct the list of enabled channels. Determine the highest
650 * number of digital pods involved in the acquisition.
1b0f62df 651 */
e06875b2 652 pod_count = 0;
ba7dd8bb
UH
653 for (l = sdi->channels; l; l = l->next) {
654 ch = l->data;
655 if (!ch->enabled)
082972e8 656 continue;
1b0f62df
GS
657 /* Only add a single digital channel per group (pod). */
658 group = ch->index / 8;
659 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
ba7dd8bb
UH
660 devc->enabled_channels = g_slist_append(
661 devc->enabled_channels, ch);
e06875b2 662 if (ch->type == SR_CHANNEL_LOGIC) {
1b0f62df 663 digital_added[group] = TRUE;
e06875b2
GS
664 if (pod_count < group + 1)
665 pod_count = group + 1;
666 }
13f2b9d7
DJ
667 }
668 }
ba7dd8bb 669 if (!devc->enabled_channels)
13f2b9d7 670 return SR_ERR;
e06875b2
GS
671 devc->pod_count = pod_count;
672 devc->logic_data = NULL;
13f2b9d7 673
1b0f62df
GS
674 /*
675 * Check constraints. Some channels can be either analog or
676 * digital, but not both at the same time.
677 */
ba7dd8bb
UH
678 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
679 sr_err("Invalid channel configuration specified!");
40a75c8e
GS
680 ret = SR_ERR_NA;
681 goto free_enabled;
13f2b9d7
DJ
682 }
683
1b0f62df
GS
684 /*
685 * Configure the analog and digital channels and the
686 * corresponding digital pods.
687 */
ba7dd8bb
UH
688 if (hmo_setup_channels(sdi) != SR_OK) {
689 sr_err("Failed to setup channel configuration!");
40a75c8e
GS
690 ret = SR_ERR;
691 goto free_enabled;
13f2b9d7
DJ
692 }
693
1b0f62df
GS
694 /*
695 * Start acquisition on the first enabled channel. The
696 * receive routine will continue driving the acquisition.
697 */
102f1239
BV
698 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
699 hmo_receive_data, (void *)sdi);
13f2b9d7 700
bee2b016 701 std_session_send_df_header(sdi);
13f2b9d7 702
ba7dd8bb 703 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
704
705 return hmo_request_data(sdi);
40a75c8e
GS
706
707free_enabled:
708 g_slist_free(devc->enabled_channels);
709 devc->enabled_channels = NULL;
710 return ret;
06a3e78a
DJ
711}
712
695dc859 713static int dev_acquisition_stop(struct sr_dev_inst *sdi)
06a3e78a 714{
13f2b9d7 715 struct dev_context *devc;
23f43dff 716 struct sr_scpi_dev_inst *scpi;
13f2b9d7 717
bee2b016 718 std_session_send_df_end(sdi);
66e3219d 719
13f2b9d7
DJ
720 devc = sdi->priv;
721
ef1a346b 722 devc->num_frames = 0;
ba7dd8bb
UH
723 g_slist_free(devc->enabled_channels);
724 devc->enabled_channels = NULL;
23f43dff 725 scpi = sdi->conn;
102f1239 726 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
727
728 return SR_OK;
729}
730
dd5c48a6 731static struct sr_dev_driver hameg_hmo_driver_info = {
06a3e78a 732 .name = "hameg-hmo",
89280b1a 733 .longname = "Hameg HMO",
06a3e78a 734 .api_version = 1,
c2fdcc25 735 .init = std_init,
700d6b64 736 .cleanup = std_cleanup,
06a3e78a 737 .scan = scan,
c01bf34c 738 .dev_list = std_dev_list,
06a3e78a
DJ
739 .dev_clear = dev_clear,
740 .config_get = config_get,
741 .config_set = config_set,
742 .config_list = config_list,
743 .dev_open = dev_open,
744 .dev_close = dev_close,
745 .dev_acquisition_start = dev_acquisition_start,
746 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 747 .context = NULL,
06a3e78a 748};
dd5c48a6 749SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);