]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
hameg-hmo: Rename SCPI_CMD_GET_VERTICAL_DIV to *_SCALE.
[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>
e131be0a 5 * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
06a3e78a
DJ
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
6ec6c43b 21#include <config.h>
13f2b9d7 22#include <stdlib.h>
5a1afc09 23#include "scpi.h"
06a3e78a
DJ
24#include "protocol.h"
25
dd5c48a6 26static struct sr_dev_driver hameg_hmo_driver_info;
e9a62139
DJ
27
28static const char *manufacturers[] = {
29 "HAMEG",
da1726cc 30 "Rohde&Schwarz",
e9a62139
DJ
31};
32
584560f1 33static const uint32_t scanopts[] = {
13f2b9d7
DJ
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
36};
37
55fb76b3
UH
38static const uint32_t drvopts[] = {
39 SR_CONF_OSCILLOSCOPE,
3a247d03 40 SR_CONF_LOGIC_ANALYZER,
55fb76b3
UH
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 132{
329733d9 133 const struct scope_config *model;
13f2b9d7
DJ
134
135 model = devc->model_config;
136
53b4680f 137 if (!cg)
61e77667 138 return CG_NONE;
13f2b9d7 139
fcd6a8bd
UH
140 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
141 return CG_ANALOG;
13f2b9d7 142
fcd6a8bd
UH
143 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
144 return CG_DIGITAL;
13f2b9d7 145
660e398f 146 sr_err("Invalid channel group specified.");
89280b1a 147
61e77667 148 return CG_INVALID;
13f2b9d7
DJ
149}
150
dd7a72ea
UH
151static int config_get(uint32_t key, GVariant **data,
152 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 153{
aac30633 154 int cg_type, idx, i;
13f2b9d7 155 struct dev_context *devc;
329733d9 156 const struct scope_config *model;
14a2f74d 157 struct scope_state *state;
13f2b9d7 158
709468ba 159 if (!sdi)
13f2b9d7
DJ
160 return SR_ERR_ARG;
161
709468ba
UH
162 devc = sdi->priv;
163
61e77667 164 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7 165 return SR_ERR;
06a3e78a 166
13f2b9d7 167 model = devc->model_config;
14a2f74d 168 state = devc->model_state;
06a3e78a 169
06a3e78a 170 switch (key) {
bf622e6d 171 case SR_CONF_NUM_HDIV:
13f2b9d7 172 *data = g_variant_new_int32(model->num_xdivs);
13f2b9d7 173 break;
3fd2dca2
DJ
174 case SR_CONF_TIMEBASE:
175 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
176 (*model->timebases)[state->timebase][1]);
3fd2dca2 177 break;
13f2b9d7 178 case SR_CONF_NUM_VDIV:
3782e571 179 if (!cg)
660e398f 180 return SR_ERR_CHANNEL_GROUP;
3782e571 181 if (cg_type != CG_ANALOG)
758906aa 182 return SR_ERR_NA;
3782e571
UH
183 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
184 return SR_ERR_ARG;
185 *data = g_variant_new_int32(model->num_ydivs);
3fd2dca2
DJ
186 break;
187 case SR_CONF_VDIV:
3782e571 188 if (!cg)
61e77667 189 return SR_ERR_CHANNEL_GROUP;
3782e571 190 if (cg_type != CG_ANALOG)
758906aa 191 return SR_ERR_NA;
3782e571
UH
192 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
193 return SR_ERR_ARG;
194 *data = g_variant_new("(tt)",
195 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
196 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
ccf14618
DJ
197 break;
198 case SR_CONF_TRIGGER_SOURCE:
199 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
ccf14618 200 break;
3fd2dca2
DJ
201 case SR_CONF_TRIGGER_SLOPE:
202 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
3fd2dca2 203 break;
4fa4db2c
GT
204 case SR_CONF_TRIGGER_PATTERN:
205 *data = g_variant_new_string(state->trigger_pattern);
206 break;
3fd2dca2
DJ
207 case SR_CONF_HORIZ_TRIGGERPOS:
208 *data = g_variant_new_double(state->horiz_triggerpos);
3fd2dca2 209 break;
ccf14618 210 case SR_CONF_COUPLING:
3782e571 211 if (!cg)
660e398f 212 return SR_ERR_CHANNEL_GROUP;
3782e571 213 if (cg_type != CG_ANALOG)
758906aa 214 return SR_ERR_NA;
3782e571
UH
215 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
216 return SR_ERR_ARG;
217 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
13f2b9d7 218 break;
14a2f74d
DJ
219 case SR_CONF_SAMPLERATE:
220 *data = g_variant_new_uint64(state->sample_rate);
14a2f74d 221 break;
e131be0a
GT
222 case SR_CONF_LOGIC_THRESHOLD:
223 if (!cg)
224 return SR_ERR_CHANNEL_GROUP;
225 if (cg_type != CG_DIGITAL)
226 return SR_ERR_NA;
227 if (!model)
228 return SR_ERR_ARG;
229 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
230 return SR_ERR_ARG;
231 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
232 break;
233 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
234 if (!cg)
235 return SR_ERR_CHANNEL_GROUP;
236 if (cg_type != CG_DIGITAL)
237 return SR_ERR_NA;
238 if (!model)
239 return SR_ERR_ARG;
240 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
241 return SR_ERR_ARG;
aac30633
GT
242 /* Check if the oscilloscope is currently in custom threshold mode. */
243 for (i = 0; i < model->num_logic_threshold; i++) {
244 if (!strcmp("USER2", (*model->logic_threshold)[i]))
245 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
246 return SR_ERR_NA;
247 if (!strcmp("USER", (*model->logic_threshold)[i]))
248 if (strcmp("USER", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
249 return SR_ERR_NA;
250 if (!strcmp("MAN", (*model->logic_threshold)[i]))
251 if (strcmp("MAN", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
252 return SR_ERR_NA;
253 }
e131be0a
GT
254 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
255 break;
06a3e78a 256 default:
758906aa 257 return SR_ERR_NA;
06a3e78a
DJ
258 }
259
758906aa 260 return SR_OK;
06a3e78a
DJ
261}
262
dd7a72ea
UH
263static int config_set(uint32_t key, GVariant *data,
264 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 265{
aac30633
GT
266 int ret, cg_type, idx, i, j;
267 char command[MAX_COMMAND_SIZE], command2[MAX_COMMAND_SIZE];
268 char float_str[30], *tmp_str;
13f2b9d7 269 struct dev_context *devc;
329733d9 270 const struct scope_config *model;
13f2b9d7 271 struct scope_state *state;
1203acc7 272 double tmp_d, tmp_d2;
23e1ea7a 273 gboolean update_sample_rate;
06a3e78a 274
b0baddef 275 if (!sdi)
13f2b9d7
DJ
276 return SR_ERR_ARG;
277
b0baddef
UH
278 devc = sdi->priv;
279
61e77667 280 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
281 return SR_ERR;
282
283 model = devc->model_config;
284 state = devc->model_state;
23e1ea7a 285 update_sample_rate = FALSE;
13f2b9d7 286
06a3e78a 287 switch (key) {
d779dcac
GT
288 case SR_CONF_LIMIT_SAMPLES:
289 devc->samples_limit = g_variant_get_uint64(data);
290 ret = SR_OK;
291 break;
13f2b9d7
DJ
292 case SR_CONF_LIMIT_FRAMES:
293 devc->frame_limit = g_variant_get_uint64(data);
294 ret = SR_OK;
295 break;
13f2b9d7 296 case SR_CONF_TRIGGER_SOURCE:
bd633efa
UH
297 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
298 return SR_ERR_ARG;
bd633efa
UH
299 g_snprintf(command, sizeof(command),
300 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
301 (*model->trigger_sources)[idx]);
38839344
GT
302 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
303 sr_scpi_get_opc(sdi->conn) != SR_OK)
304 return SR_ERR;
1203acc7 305 state->trigger_source = idx;
38839344 306 ret = SR_OK;
89280b1a 307 break;
13f2b9d7 308 case SR_CONF_VDIV:
61233697 309 if (!cg)
660e398f 310 return SR_ERR_CHANNEL_GROUP;
697fb6dd
UH
311 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
312 return SR_ERR_ARG;
fcd6a8bd
UH
313 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
314 return SR_ERR_ARG;
fcd6a8bd
UH
315 g_ascii_formatd(float_str, sizeof(float_str), "%E",
316 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
317 g_snprintf(command, sizeof(command),
66ddc22a 318 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_SCALE],
fcd6a8bd
UH
319 j + 1, float_str);
320 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
321 sr_scpi_get_opc(sdi->conn) != SR_OK)
322 return SR_ERR;
1203acc7 323 state->analog_channels[j].vdiv = idx;
697fb6dd 324 ret = SR_OK;
89280b1a 325 break;
13f2b9d7 326 case SR_CONF_TIMEBASE:
697fb6dd
UH
327 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
328 return SR_ERR_ARG;
697fb6dd
UH
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);
38839344
GT
334 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
335 sr_scpi_get_opc(sdi->conn) != SR_OK)
336 return SR_ERR;
1203acc7 337 state->timebase = idx;
38839344 338 ret = SR_OK;
697fb6dd 339 update_sample_rate = TRUE;
89280b1a 340 break;
13f2b9d7 341 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 342 tmp_d = g_variant_get_double(data);
89280b1a 343 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7 344 return SR_ERR;
1203acc7 345 tmp_d2 = -(tmp_d - 0.5) *
422a1c0d
DJ
346 ((double) (*model->timebases)[state->timebase][0] /
347 (*model->timebases)[state->timebase][1])
348 * model->num_xdivs;
1203acc7 349 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
13f2b9d7
DJ
350 g_snprintf(command, sizeof(command),
351 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 352 float_str);
38839344
GT
353 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
354 sr_scpi_get_opc(sdi->conn) != SR_OK)
355 return SR_ERR;
1203acc7 356 state->horiz_triggerpos = tmp_d;
38839344 357 ret = SR_OK;
89280b1a 358 break;
13f2b9d7 359 case SR_CONF_TRIGGER_SLOPE:
bd633efa
UH
360 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
361 return SR_ERR_ARG;
bd633efa
UH
362 g_snprintf(command, sizeof(command),
363 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
364 (*model->trigger_slopes)[idx]);
38839344
GT
365 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
366 sr_scpi_get_opc(sdi->conn) != SR_OK)
367 return SR_ERR;
1203acc7 368 state->trigger_slope = idx;
38839344 369 ret = SR_OK;
89280b1a 370 break;
4fa4db2c
GT
371 case SR_CONF_TRIGGER_PATTERN:
372 tmp_str = (char *)g_variant_get_string(data, 0);
373 idx = strlen(tmp_str);
374 if (idx == 0 || idx > model->analog_channels + model->digital_channels)
375 return SR_ERR_ARG;
376 g_snprintf(command, sizeof(command),
377 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_PATTERN],
378 tmp_str);
379 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
380 sr_scpi_get_opc(sdi->conn) != SR_OK)
381 return SR_ERR;
396af5ad
GT
382 strncpy(state->trigger_pattern,
383 tmp_str,
384 MAX_ANALOG_CHANNEL_COUNT + MAX_DIGITAL_CHANNEL_COUNT);
4fa4db2c
GT
385 ret = SR_OK;
386 break;
13f2b9d7 387 case SR_CONF_COUPLING:
61233697 388 if (!cg)
660e398f 389 return SR_ERR_CHANNEL_GROUP;
bd633efa
UH
390 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
391 return SR_ERR_ARG;
fcd6a8bd
UH
392 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
393 return SR_ERR_ARG;
fcd6a8bd
UH
394 g_snprintf(command, sizeof(command),
395 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
396 j + 1, (*model->coupling_options)[idx]);
397 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
398 sr_scpi_get_opc(sdi->conn) != SR_OK)
399 return SR_ERR;
1203acc7 400 state->analog_channels[j].coupling = idx;
bd633efa 401 ret = SR_OK;
89280b1a 402 break;
e131be0a
GT
403 case SR_CONF_LOGIC_THRESHOLD:
404 if (!cg)
405 return SR_ERR_CHANNEL_GROUP;
406 if (cg_type != CG_DIGITAL)
407 return SR_ERR_NA;
408 if (!model)
409 return SR_ERR_ARG;
410 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
411 return SR_ERR_ARG;
412 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
413 return SR_ERR_ARG;
aac30633
GT
414 /* Check if the threshold command is based on the POD or digital channel index. */
415 if (model->logic_threshold_for_pod)
416 i = j + 1;
417 else
418 i = j * 8;
e131be0a
GT
419 g_snprintf(command, sizeof(command),
420 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
aac30633 421 i, (*model->logic_threshold)[idx]);
e131be0a
GT
422 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
423 sr_scpi_get_opc(sdi->conn) != SR_OK)
424 return SR_ERR;
425 state->digital_pods[j].threshold = idx;
426 ret = SR_OK;
427 break;
428 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
429 if (!cg)
430 return SR_ERR_CHANNEL_GROUP;
431 if (cg_type != CG_DIGITAL)
432 return SR_ERR_NA;
433 if (!model)
434 return SR_ERR_ARG;
435 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
436 return SR_ERR_ARG;
437 tmp_d = g_variant_get_double(data);
438 if (tmp_d < -2.0 || tmp_d > 8.0)
439 return SR_ERR;
440 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
aac30633
GT
441 /* Check if the threshold command is based on the POD or digital channel index. */
442 if (model->logic_threshold_for_pod)
443 idx = j + 1;
444 else
445 idx = j * 8;
446 /* Try to support different dialects exhaustively. */
447 for (i = 0; i < model->num_logic_threshold; i++) {
448 if (!strcmp("USER2", (*model->logic_threshold)[i])) {
449 g_snprintf(command, sizeof(command),
450 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
451 idx, 2, float_str); /* USER2 */
452 g_snprintf(command2, sizeof(command2),
453 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
454 idx, "USER2");
455 break;
456 }
457 if (!strcmp("USER", (*model->logic_threshold)[i])) {
458 g_snprintf(command, sizeof(command),
459 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
460 idx, float_str);
461 g_snprintf(command2, sizeof(command2),
462 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
463 idx, "USER");
464 break;
465 }
466 if (!strcmp("MAN", (*model->logic_threshold)[i])) {
467 g_snprintf(command, sizeof(command),
468 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
469 idx, float_str);
470 g_snprintf(command2, sizeof(command2),
471 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
472 idx, "MAN");
473 break;
474 }
475 }
e131be0a
GT
476 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
477 sr_scpi_get_opc(sdi->conn) != SR_OK)
478 return SR_ERR;
aac30633 479 if (sr_scpi_send(sdi->conn, command2) != SR_OK ||
e131be0a
GT
480 sr_scpi_get_opc(sdi->conn) != SR_OK)
481 return SR_ERR;
482 state->digital_pods[j].user_threshold = tmp_d;
483 ret = SR_OK;
484 break;
06a3e78a
DJ
485 default:
486 ret = SR_ERR_NA;
13f2b9d7 487 break;
06a3e78a
DJ
488 }
489
23e1ea7a
DJ
490 if (ret == SR_OK && update_sample_rate)
491 ret = hmo_update_sample_rate(sdi);
492
06a3e78a
DJ
493 return ret;
494}
495
dd7a72ea
UH
496static int config_list(uint32_t key, GVariant **data,
497 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 498{
6ec3ef9b
AJ
499 int cg_type = CG_NONE;
500 struct dev_context *devc = NULL;
329733d9 501 const struct scope_config *model = NULL;
13f2b9d7 502
4d399734
UH
503 if (sdi) {
504 devc = sdi->priv;
6ec3ef9b
AJ
505 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
506 return SR_ERR;
13f2b9d7 507
6ec3ef9b
AJ
508 model = devc->model_config;
509 }
06a3e78a 510
06a3e78a 511 switch (key) {
226363c4 512 case SR_CONF_SCAN_OPTIONS:
53012da6 513 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
226363c4 514 break;
13f2b9d7 515 case SR_CONF_DEVICE_OPTIONS:
61233697 516 if (!cg) {
6ec3ef9b 517 if (model)
769561cb 518 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
6ec3ef9b 519 else
53012da6 520 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
61e77667 521 } else if (cg_type == CG_ANALOG) {
769561cb 522 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
e131be0a
GT
523 } else if (cg_type == CG_DIGITAL) {
524 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
13f2b9d7 525 } else {
105df674 526 *data = std_gvar_array_u32(NULL, 0);
13f2b9d7
DJ
527 }
528 break;
13f2b9d7 529 case SR_CONF_COUPLING:
61233697 530 if (!cg)
660e398f 531 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
532 if (!model)
533 return SR_ERR_ARG;
692716f5 534 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
13f2b9d7 535 break;
13f2b9d7 536 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
537 if (!model)
538 return SR_ERR_ARG;
692716f5 539 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
13f2b9d7 540 break;
bbabdaf1 541 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
542 if (!model)
543 return SR_ERR_ARG;
692716f5 544 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
bbabdaf1 545 break;
13f2b9d7 546 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
547 if (!model)
548 return SR_ERR_ARG;
58ffcf97 549 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
13f2b9d7 550 break;
13f2b9d7 551 case SR_CONF_VDIV:
61233697 552 if (!cg)
660e398f 553 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
554 if (!model)
555 return SR_ERR_ARG;
58ffcf97 556 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
13f2b9d7 557 break;
e131be0a
GT
558 case SR_CONF_LOGIC_THRESHOLD:
559 if (!cg)
560 return SR_ERR_CHANNEL_GROUP;
561 if (!model)
562 return SR_ERR_ARG;
563 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
564 break;
06a3e78a
DJ
565 default:
566 return SR_ERR_NA;
567 }
568
13f2b9d7 569 return SR_OK;
06a3e78a
DJ
570}
571
13f2b9d7 572SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 573{
13f2b9d7 574 char command[MAX_COMMAND_SIZE];
ba7dd8bb 575 struct sr_channel *ch;
13f2b9d7 576 struct dev_context *devc;
329733d9 577 const struct scope_config *model;
13f2b9d7
DJ
578
579 devc = sdi->priv;
580 model = devc->model_config;
581
ba7dd8bb 582 ch = devc->current_channel->data;
13f2b9d7 583
ba7dd8bb 584 switch (ch->type) {
3f239f08 585 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
586 g_snprintf(command, sizeof(command),
587 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
65a6794e
GS
588#ifdef WORDS_BIGENDIAN
589 "MSBF",
590#else
591 "LSBF",
592#endif
ba7dd8bb 593 ch->index + 1);
13f2b9d7 594 break;
3f239f08 595 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
596 g_snprintf(command, sizeof(command),
597 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 598 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
599 break;
600 default:
ba7dd8bb 601 sr_err("Invalid channel type.");
13f2b9d7
DJ
602 break;
603 }
604
605 return sr_scpi_send(sdi->conn, command);
606}
607
ba7dd8bb 608static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
609{
610 GSList *l;
ba7dd8bb 611 struct sr_channel *ch;
4889acef
GS
612 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
613 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
614 size_t idx;
615
616 /* Preset "not enabled" for all channels / pods. */
617 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
618 enabled_chan[idx] = FALSE;
619 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
620 enabled_pod[idx] = FALSE;
621
622 /*
623 * Determine which channels / pods are required for the caller's
624 * specified configuration.
625 */
ba7dd8bb
UH
626 for (l = channels; l; l = l->next) {
627 ch = l->data;
628 switch (ch->type) {
3f239f08 629 case SR_CHANNEL_ANALOG:
4889acef
GS
630 idx = ch->index;
631 if (idx < ARRAY_SIZE(enabled_chan))
632 enabled_chan[idx] = TRUE;
13f2b9d7 633 break;
3f239f08 634 case SR_CHANNEL_LOGIC:
4889acef
GS
635 idx = ch->index / 8;
636 if (idx < ARRAY_SIZE(enabled_pod))
637 enabled_pod[idx] = TRUE;
13f2b9d7 638 break;
13f2b9d7
DJ
639 default:
640 return SR_ERR;
641 }
642 }
643
4889acef
GS
644 /*
645 * Check for resource conflicts. Some channels can be either
646 * analog or digital, but never both at the same time.
647 *
648 * Note that the constraints might depend on the specific model.
649 * These tests might need some adjustment when support for more
650 * models gets added to the driver.
651 */
652 if (enabled_pod[0] && enabled_chan[2])
653 return SR_ERR;
654 if (enabled_pod[1] && enabled_chan[3])
13f2b9d7 655 return SR_ERR;
13f2b9d7
DJ
656 return SR_OK;
657}
658
ba7dd8bb 659static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
660{
661 GSList *l;
662 unsigned int i;
23e1ea7a 663 gboolean *pod_enabled, setup_changed;
13f2b9d7 664 char command[MAX_COMMAND_SIZE];
13f2b9d7 665 struct scope_state *state;
329733d9 666 const struct scope_config *model;
ba7dd8bb 667 struct sr_channel *ch;
13f2b9d7 668 struct dev_context *devc;
23f43dff 669 struct sr_scpi_dev_inst *scpi;
addbb09b 670 int ret;
13f2b9d7
DJ
671
672 devc = sdi->priv;
23f43dff 673 scpi = sdi->conn;
13f2b9d7
DJ
674 state = devc->model_state;
675 model = devc->model_config;
23e1ea7a 676 setup_changed = FALSE;
13f2b9d7
DJ
677
678 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
679
ba7dd8bb
UH
680 for (l = sdi->channels; l; l = l->next) {
681 ch = l->data;
682 switch (ch->type) {
3f239f08 683 case SR_CHANNEL_ANALOG:
ba7dd8bb 684 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
685 break;
686 g_snprintf(command, sizeof(command),
687 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 688 ch->index + 1, ch->enabled);
13f2b9d7 689
4fc4b8e7
UH
690 if (sr_scpi_send(scpi, command) != SR_OK) {
691 g_free(pod_enabled);
082972e8 692 return SR_ERR;
4fc4b8e7 693 }
ba7dd8bb 694 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 695 setup_changed = TRUE;
89280b1a 696 break;
3f239f08 697 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
698 /*
699 * A digital POD needs to be enabled for every group of
ba7dd8bb 700 * 8 channels.
13f2b9d7 701 */
ba7dd8bb
UH
702 if (ch->enabled)
703 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
13f2b9d7 704
ba7dd8bb 705 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
706 break;
707 g_snprintf(command, sizeof(command),
708 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 709 ch->index, ch->enabled);
13f2b9d7 710
4fc4b8e7
UH
711 if (sr_scpi_send(scpi, command) != SR_OK) {
712 g_free(pod_enabled);
082972e8 713 return SR_ERR;
4fc4b8e7 714 }
13f2b9d7 715
ba7dd8bb 716 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 717 setup_changed = TRUE;
89280b1a 718 break;
13f2b9d7 719 default:
b0e80e9a 720 g_free(pod_enabled);
13f2b9d7
DJ
721 return SR_ERR;
722 }
723 }
724
addbb09b 725 ret = SR_OK;
d1ac53cc 726 for (i = 0; i < model->digital_pods; i++) {
e131be0a 727 if (state->digital_pods[i].state == pod_enabled[i])
082972e8
UH
728 continue;
729 g_snprintf(command, sizeof(command),
730 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
d1ac53cc 731 i + 1, pod_enabled[i]);
addbb09b
GS
732 if (sr_scpi_send(scpi, command) != SR_OK) {
733 ret = SR_ERR;
734 break;
735 }
e131be0a 736 state->digital_pods[i].state = pod_enabled[i];
23e1ea7a 737 setup_changed = TRUE;
13f2b9d7 738 }
13f2b9d7 739 g_free(pod_enabled);
addbb09b
GS
740 if (ret != SR_OK)
741 return ret;
13f2b9d7 742
23e1ea7a
DJ
743 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
744 return SR_ERR;
745
13f2b9d7
DJ
746 return SR_OK;
747}
748
695dc859 749static int dev_acquisition_start(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
750{
751 GSList *l;
1b0f62df 752 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
e06875b2 753 size_t group, pod_count;
ba7dd8bb 754 struct sr_channel *ch;
13f2b9d7 755 struct dev_context *devc;
23f43dff 756 struct sr_scpi_dev_inst *scpi;
40a75c8e 757 int ret;
06a3e78a 758
23f43dff 759 scpi = sdi->conn;
13f2b9d7 760 devc = sdi->priv;
13f2b9d7 761
d779dcac
GT
762 devc->num_samples = 0;
763 devc->num_frames = 0;
764
1b0f62df
GS
765 /* Preset empty results. */
766 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
767 digital_added[group] = FALSE;
db81fbb5
SA
768 g_slist_free(devc->enabled_channels);
769 devc->enabled_channels = NULL;
770
1b0f62df 771 /*
e06875b2
GS
772 * Contruct the list of enabled channels. Determine the highest
773 * number of digital pods involved in the acquisition.
1b0f62df 774 */
e06875b2 775 pod_count = 0;
ba7dd8bb
UH
776 for (l = sdi->channels; l; l = l->next) {
777 ch = l->data;
778 if (!ch->enabled)
082972e8 779 continue;
1b0f62df
GS
780 /* Only add a single digital channel per group (pod). */
781 group = ch->index / 8;
782 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
ba7dd8bb
UH
783 devc->enabled_channels = g_slist_append(
784 devc->enabled_channels, ch);
e06875b2 785 if (ch->type == SR_CHANNEL_LOGIC) {
1b0f62df 786 digital_added[group] = TRUE;
e06875b2
GS
787 if (pod_count < group + 1)
788 pod_count = group + 1;
789 }
13f2b9d7
DJ
790 }
791 }
ba7dd8bb 792 if (!devc->enabled_channels)
13f2b9d7 793 return SR_ERR;
e06875b2
GS
794 devc->pod_count = pod_count;
795 devc->logic_data = NULL;
13f2b9d7 796
1b0f62df
GS
797 /*
798 * Check constraints. Some channels can be either analog or
799 * digital, but not both at the same time.
800 */
ba7dd8bb
UH
801 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
802 sr_err("Invalid channel configuration specified!");
40a75c8e
GS
803 ret = SR_ERR_NA;
804 goto free_enabled;
13f2b9d7
DJ
805 }
806
1b0f62df
GS
807 /*
808 * Configure the analog and digital channels and the
809 * corresponding digital pods.
810 */
ba7dd8bb
UH
811 if (hmo_setup_channels(sdi) != SR_OK) {
812 sr_err("Failed to setup channel configuration!");
40a75c8e
GS
813 ret = SR_ERR;
814 goto free_enabled;
13f2b9d7
DJ
815 }
816
1b0f62df
GS
817 /*
818 * Start acquisition on the first enabled channel. The
819 * receive routine will continue driving the acquisition.
820 */
102f1239
BV
821 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
822 hmo_receive_data, (void *)sdi);
13f2b9d7 823
bee2b016 824 std_session_send_df_header(sdi);
13f2b9d7 825
ba7dd8bb 826 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
827
828 return hmo_request_data(sdi);
40a75c8e
GS
829
830free_enabled:
831 g_slist_free(devc->enabled_channels);
832 devc->enabled_channels = NULL;
833 return ret;
06a3e78a
DJ
834}
835
695dc859 836static int dev_acquisition_stop(struct sr_dev_inst *sdi)
06a3e78a 837{
13f2b9d7 838 struct dev_context *devc;
23f43dff 839 struct sr_scpi_dev_inst *scpi;
13f2b9d7 840
bee2b016 841 std_session_send_df_end(sdi);
66e3219d 842
13f2b9d7
DJ
843 devc = sdi->priv;
844
d779dcac 845 devc->num_samples = 0;
ef1a346b 846 devc->num_frames = 0;
ba7dd8bb
UH
847 g_slist_free(devc->enabled_channels);
848 devc->enabled_channels = NULL;
23f43dff 849 scpi = sdi->conn;
102f1239 850 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
851
852 return SR_OK;
853}
854
dd5c48a6 855static struct sr_dev_driver hameg_hmo_driver_info = {
06a3e78a 856 .name = "hameg-hmo",
89280b1a 857 .longname = "Hameg HMO",
06a3e78a 858 .api_version = 1,
c2fdcc25 859 .init = std_init,
700d6b64 860 .cleanup = std_cleanup,
06a3e78a 861 .scan = scan,
c01bf34c 862 .dev_list = std_dev_list,
06a3e78a
DJ
863 .dev_clear = dev_clear,
864 .config_get = config_get,
865 .config_set = config_set,
866 .config_list = config_list,
867 .dev_open = dev_open,
868 .dev_close = dev_close,
869 .dev_acquisition_start = dev_acquisition_start,
870 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 871 .context = NULL,
06a3e78a 872};
dd5c48a6 873SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);