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