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