]> sigrok.org Git - libsigrok.git/blob - src/hardware/hameg-hmo/protocol.c
hameg-hmo: Use 1-based (not 0-based) POD numbers.
[libsigrok.git] / src / hardware / hameg-hmo / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5  * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
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
21 #include <config.h>
22 #include <math.h>
23 #include <stdlib.h>
24 #include "scpi.h"
25 #include "protocol.h"
26
27 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
28                                   size_t group, GByteArray *pod_data);
29 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
30                                    struct dev_context *devc);
31 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc);
32
33 static const char *hameg_scpi_dialect[] = {
34         [SCPI_CMD_GET_DIG_DATA]               = ":FORM UINT,8;:POD%d:DATA?",
35         [SCPI_CMD_GET_TIMEBASE]               = ":TIM:SCAL?",
36         [SCPI_CMD_SET_TIMEBASE]               = ":TIM:SCAL %s",
37         [SCPI_CMD_GET_COUPLING]               = ":CHAN%d:COUP?",
38         [SCPI_CMD_SET_COUPLING]               = ":CHAN%d:COUP %s",
39         [SCPI_CMD_GET_SAMPLE_RATE]            = ":ACQ:SRAT?",
40         [SCPI_CMD_GET_SAMPLE_RATE_LIVE]       = ":%s:DATA:POINTS?",
41         [SCPI_CMD_GET_ANALOG_DATA]            = ":FORM:BORD %s;" \
42                                                 ":FORM REAL,32;:CHAN%d:DATA?",
43         [SCPI_CMD_GET_VERTICAL_DIV]           = ":CHAN%d:SCAL?",
44         [SCPI_CMD_SET_VERTICAL_DIV]           = ":CHAN%d:SCAL %s",
45         [SCPI_CMD_GET_DIG_POD_STATE]          = ":POD%d:STAT?",
46         [SCPI_CMD_SET_DIG_POD_STATE]          = ":POD%d:STAT %d",
47         [SCPI_CMD_GET_TRIGGER_SLOPE]          = ":TRIG:A:EDGE:SLOP?",
48         [SCPI_CMD_SET_TRIGGER_SLOPE]          = ":TRIG:A:TYPE EDGE;:TRIG:A:EDGE:SLOP %s",
49         [SCPI_CMD_GET_TRIGGER_PATTERN]        = ":TRIG:A:PATT:SOUR?",
50         [SCPI_CMD_SET_TRIGGER_PATTERN]        = ":TRIG:A:TYPE LOGIC;" \
51                                                 ":TRIG:A:PATT:FUNC AND;" \
52                                                 ":TRIG:A:PATT:COND TRUE;" \
53                                                 ":TRIG:A:PATT:MODE OFF;" \
54                                                 ":TRIG:A:PATT:SOUR \"%s\"",
55         [SCPI_CMD_GET_TRIGGER_SOURCE]         = ":TRIG:A:SOUR?",
56         [SCPI_CMD_SET_TRIGGER_SOURCE]         = ":TRIG:A:SOUR %s",
57         [SCPI_CMD_GET_DIG_CHAN_STATE]         = ":LOG%d:STAT?",
58         [SCPI_CMD_SET_DIG_CHAN_STATE]         = ":LOG%d:STAT %d",
59         [SCPI_CMD_GET_VERTICAL_OFFSET]        = ":CHAN%d:POS?",
60         [SCPI_CMD_GET_HORIZ_TRIGGERPOS]       = ":TIM:POS?",
61         [SCPI_CMD_SET_HORIZ_TRIGGERPOS]       = ":TIM:POS %s",
62         [SCPI_CMD_GET_ANALOG_CHAN_STATE]      = ":CHAN%d:STAT?",
63         [SCPI_CMD_SET_ANALOG_CHAN_STATE]      = ":CHAN%d:STAT %d",
64         [SCPI_CMD_GET_PROBE_UNIT]             = ":PROB%d:SET:ATT:UNIT?",
65         [SCPI_CMD_GET_DIG_POD_THRESHOLD]      = ":POD%d:THR?",
66         [SCPI_CMD_SET_DIG_POD_THRESHOLD]      = ":POD%d:THR %s",
67         [SCPI_CMD_GET_DIG_POD_USER_THRESHOLD] = ":POD%d:THR:UDL%d?",
68         [SCPI_CMD_SET_DIG_POD_USER_THRESHOLD] = ":POD%d:THR:UDL%d %s",
69 };
70
71 static const uint32_t devopts[] = {
72         SR_CONF_OSCILLOSCOPE,
73         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
74         SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
75         SR_CONF_SAMPLERATE | SR_CONF_GET,
76         SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
77         SR_CONF_NUM_HDIV | SR_CONF_GET,
78         SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
79         SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
80         SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
81         SR_CONF_TRIGGER_PATTERN | SR_CONF_GET | SR_CONF_SET,
82 };
83
84 static const uint32_t devopts_cg_analog[] = {
85         SR_CONF_NUM_VDIV | SR_CONF_GET,
86         SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
87         SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
88 };
89
90 static const uint32_t devopts_cg_digital[] = {
91         SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
92         SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
93 };
94
95 static const char *coupling_options[] = {
96         "AC",  // AC with 50 Ohm termination (152x, 202x, 30xx, 1202)
97         "ACL", // AC with 1 MOhm termination
98         "DC",  // DC with 50 Ohm termination
99         "DCL", // DC with 1 MOhm termination
100         "GND",
101 };
102
103 static const char *scope_trigger_slopes[] = {
104         "POS",
105         "NEG",
106         "EITH",
107 };
108
109 /* Predefined logic thresholds. */
110 static const char *logic_threshold[] = {
111         "TTL",
112         "ECL",
113         "CMOS",
114         "USER1",
115         "USER2", // overwritten by logic_threshold_custom, use USER1 for permanent setting
116 };
117
118 /* RTC1002, HMO Compact2 and HMO1002/HMO1202 */
119 static const char *an2_dig8_trigger_sources[] = {
120         "CH1", "CH2",
121         "LINE", "EXT", "PATT", "BUS1", "BUS2",
122         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
123 };
124
125 /* HMO3xx2 */
126 static const char *an2_dig16_trigger_sources[] = {
127         "CH1", "CH2",
128         "LINE", "EXT", "PATT", "BUS1", "BUS2",
129         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
130         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
131 };
132
133 /* HMO Compact4 */
134 static const char *an4_dig8_trigger_sources[] = {
135         "CH1", "CH2", "CH3", "CH4",
136         "LINE", "EXT", "PATT", "BUS1", "BUS2",
137         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
138 };
139
140 /* HMO3xx4 and HMO2524 */
141 static const char *an4_dig16_trigger_sources[] = {
142         "CH1", "CH2", "CH3", "CH4",
143         "LINE", "EXT", "PATT", "BUS1", "BUS2",
144         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
145         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
146 };
147
148 static const uint64_t timebases[][2] = {
149         /* nanoseconds */
150         { 2, 1000000000 },
151         { 5, 1000000000 },
152         { 10, 1000000000 },
153         { 20, 1000000000 },
154         { 50, 1000000000 },
155         { 100, 1000000000 },
156         { 200, 1000000000 },
157         { 500, 1000000000 },
158         /* microseconds */
159         { 1, 1000000 },
160         { 2, 1000000 },
161         { 5, 1000000 },
162         { 10, 1000000 },
163         { 20, 1000000 },
164         { 50, 1000000 },
165         { 100, 1000000 },
166         { 200, 1000000 },
167         { 500, 1000000 },
168         /* milliseconds */
169         { 1, 1000 },
170         { 2, 1000 },
171         { 5, 1000 },
172         { 10, 1000 },
173         { 20, 1000 },
174         { 50, 1000 },
175         { 100, 1000 },
176         { 200, 1000 },
177         { 500, 1000 },
178         /* seconds */
179         { 1, 1 },
180         { 2, 1 },
181         { 5, 1 },
182         { 10, 1 },
183         { 20, 1 },
184         { 50, 1 },
185 };
186
187 static const uint64_t vdivs[][2] = {
188         /* millivolts */
189         { 1, 1000 },
190         { 2, 1000 },
191         { 5, 1000 },
192         { 10, 1000 },
193         { 20, 1000 },
194         { 50, 1000 },
195         { 100, 1000 },
196         { 200, 1000 },
197         { 500, 1000 },
198         /* volts */
199         { 1, 1 },
200         { 2, 1 },
201         { 5, 1 },
202         { 10, 1 },
203         { 20, 1 },
204         { 50, 1 },
205 };
206
207 static const char *scope_analog_channel_names[] = {
208         "CH1", "CH2", "CH3", "CH4",
209 };
210
211 static const char *scope_digital_channel_names[] = {
212         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
213         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
214 };
215
216 static const struct scope_config scope_models[] = {
217         {
218                 /* RTC1002 and HMO722/1002/1022/1202/1522/2022 support only 8 digital channels. */
219                 .name = {"RTC1002", "HMO722", "HMO1002", "HMO1022", "HMO1202", "HMO1522", "HMO2022", NULL},
220                 .analog_channels = 2,
221                 .digital_channels = 8,
222                 .digital_pods = 1,
223
224                 .analog_names = &scope_analog_channel_names,
225                 .digital_names = &scope_digital_channel_names,
226
227                 .devopts = &devopts,
228                 .num_devopts = ARRAY_SIZE(devopts),
229
230                 .devopts_cg_analog = &devopts_cg_analog,
231                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
232
233                 .devopts_cg_digital = &devopts_cg_digital,
234                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
235
236                 .coupling_options = &coupling_options,
237                 .num_coupling_options = ARRAY_SIZE(coupling_options),
238
239                 .logic_threshold = &logic_threshold,
240                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
241
242                 .trigger_sources = &an2_dig8_trigger_sources,
243                 .num_trigger_sources = ARRAY_SIZE(an2_dig8_trigger_sources),
244
245                 .trigger_slopes = &scope_trigger_slopes,
246                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
247
248                 .timebases = &timebases,
249                 .num_timebases = ARRAY_SIZE(timebases),
250
251                 .vdivs = &vdivs,
252                 .num_vdivs = ARRAY_SIZE(vdivs),
253
254                 .num_xdivs = 12,
255                 .num_ydivs = 8,
256
257                 .scpi_dialect = &hameg_scpi_dialect,
258         },
259         {
260                 /* HMO3032/3042/3052/3522 support 16 digital channels. */
261                 .name = {"HMO3032", "HMO3042", "HMO3052", "HMO3522", NULL},
262                 .analog_channels = 2,
263                 .digital_channels = 16,
264                 .digital_pods = 2,
265
266                 .analog_names = &scope_analog_channel_names,
267                 .digital_names = &scope_digital_channel_names,
268
269                 .devopts = &devopts,
270                 .num_devopts = ARRAY_SIZE(devopts),
271
272                 .devopts_cg_analog = &devopts_cg_analog,
273                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
274
275                 .devopts_cg_digital = &devopts_cg_digital,
276                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
277
278                 .coupling_options = &coupling_options,
279                 .num_coupling_options = ARRAY_SIZE(coupling_options),
280
281                 .logic_threshold = &logic_threshold,
282                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
283
284                 .trigger_sources = &an2_dig16_trigger_sources,
285                 .num_trigger_sources = ARRAY_SIZE(an2_dig16_trigger_sources),
286
287                 .trigger_slopes = &scope_trigger_slopes,
288                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
289
290                 .timebases = &timebases,
291                 .num_timebases = ARRAY_SIZE(timebases),
292
293                 .vdivs = &vdivs,
294                 .num_vdivs = ARRAY_SIZE(vdivs),
295
296                 .num_xdivs = 12,
297                 .num_ydivs = 8,
298
299                 .scpi_dialect = &hameg_scpi_dialect,
300         },
301         {
302                 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
303                 .analog_channels = 4,
304                 .digital_channels = 8,
305                 .digital_pods = 1,
306
307                 .analog_names = &scope_analog_channel_names,
308                 .digital_names = &scope_digital_channel_names,
309
310                 .devopts = &devopts,
311                 .num_devopts = ARRAY_SIZE(devopts),
312
313                 .devopts_cg_analog = &devopts_cg_analog,
314                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
315
316                 .devopts_cg_digital = &devopts_cg_digital,
317                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
318
319                 .coupling_options = &coupling_options,
320                 .num_coupling_options = ARRAY_SIZE(coupling_options),
321
322                 .logic_threshold = &logic_threshold,
323                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
324
325                 .trigger_sources = &an4_dig8_trigger_sources,
326                 .num_trigger_sources = ARRAY_SIZE(an4_dig8_trigger_sources),
327
328                 .trigger_slopes = &scope_trigger_slopes,
329                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
330
331                 .timebases = &timebases,
332                 .num_timebases = ARRAY_SIZE(timebases),
333
334                 .vdivs = &vdivs,
335                 .num_vdivs = ARRAY_SIZE(vdivs),
336
337                 .num_xdivs = 12,
338                 .num_ydivs = 8,
339
340                 .scpi_dialect = &hameg_scpi_dialect,
341         },
342         {
343                 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", "HMO3524", NULL},
344                 .analog_channels = 4,
345                 .digital_channels = 16,
346                 .digital_pods = 2,
347
348                 .analog_names = &scope_analog_channel_names,
349                 .digital_names = &scope_digital_channel_names,
350
351                 .devopts = &devopts,
352                 .num_devopts = ARRAY_SIZE(devopts),
353
354                 .devopts_cg_analog = &devopts_cg_analog,
355                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
356
357                 .devopts_cg_digital = &devopts_cg_digital,
358                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
359
360                 .coupling_options = &coupling_options,
361                 .num_coupling_options = ARRAY_SIZE(coupling_options),
362
363                 .logic_threshold = &logic_threshold,
364                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
365
366                 .trigger_sources = &an4_dig16_trigger_sources,
367                 .num_trigger_sources = ARRAY_SIZE(an4_dig16_trigger_sources),
368
369                 .trigger_slopes = &scope_trigger_slopes,
370                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
371
372                 .timebases = &timebases,
373                 .num_timebases = ARRAY_SIZE(timebases),
374
375                 .vdivs = &vdivs,
376                 .num_vdivs = ARRAY_SIZE(vdivs),
377
378                 .num_xdivs = 12,
379                 .num_ydivs = 8,
380
381                 .scpi_dialect = &hameg_scpi_dialect,
382         },
383 };
384
385 static void scope_state_dump(const struct scope_config *config,
386                              struct scope_state *state)
387 {
388         unsigned int i;
389         char *tmp;
390
391         for (i = 0; i < config->analog_channels; i++) {
392                 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
393                                              (*config->vdivs)[state->analog_channels[i].vdiv][1]);
394                 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
395                         i + 1, state->analog_channels[i].state ? "On" : "Off",
396                         (*config->coupling_options)[state->analog_channels[i].coupling],
397                         tmp, state->analog_channels[i].vertical_offset);
398         }
399
400         for (i = 0; i < config->digital_channels; i++) {
401                 sr_info("State of digital channel %d -> %s", i,
402                         state->digital_channels[i] ? "On" : "Off");
403         }
404
405         for (i = 0; i < config->digital_pods; i++) {
406                 if (strncmp("USER", (*config->logic_threshold)[state->digital_pods[i].threshold], 4))
407                         sr_info("State of digital POD %d -> %s : %s (threshold)", i + 1,
408                                 state->digital_pods[i].state ? "On" : "Off",
409                                 (*config->logic_threshold)[state->digital_pods[i].threshold]);
410                 else // user-defined or custom logic threshold
411                         sr_info("State of digital POD %d -> %s : %E (threshold)", i + 1,
412                                 state->digital_pods[i].state ? "On" : "Off",
413                                 state->digital_pods[i].user_threshold);
414         }
415
416         tmp = sr_period_string((*config->timebases)[state->timebase][0],
417                                (*config->timebases)[state->timebase][1]);
418         sr_info("Current timebase: %s", tmp);
419         g_free(tmp);
420
421         tmp = sr_samplerate_string(state->sample_rate);
422         sr_info("Current samplerate: %s", tmp);
423         g_free(tmp);
424
425         if (!strcmp("PATT", (*config->trigger_sources)[state->trigger_source]))
426                 sr_info("Current trigger: %s (pattern), %.2f (offset)",
427                         state->trigger_pattern,
428                         state->horiz_triggerpos);
429         else // Edge (slope) trigger
430                 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
431                         (*config->trigger_sources)[state->trigger_source],
432                         (*config->trigger_slopes)[state->trigger_slope],
433                         state->horiz_triggerpos);
434 }
435
436 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
437                 const char *command, const char *(*array)[], unsigned int n, int *result)
438 {
439         char *tmp;
440         int idx;
441
442         if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK)
443                 return SR_ERR;
444
445         if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
446                 g_free(tmp);
447                 return SR_ERR_ARG;
448         }
449
450         *result = idx;
451
452         g_free(tmp);
453
454         return SR_OK;
455 }
456
457 /**
458  * This function takes a value of the form "2.000E-03" and returns the index
459  * of an array where a matching pair was found.
460  *
461  * @param value The string to be parsed.
462  * @param array The array of s/f pairs.
463  * @param array_len The number of pairs in the array.
464  * @param result The index at which a matching pair was found.
465  *
466  * @return SR_ERR on any parsing error, SR_OK otherwise.
467  */
468 static int array_float_get(gchar *value, const uint64_t array[][2],
469                 int array_len, unsigned int *result)
470 {
471         struct sr_rational rval;
472         struct sr_rational aval;
473
474         if (sr_parse_rational(value, &rval) != SR_OK)
475                 return SR_ERR;
476
477         for (int i = 0; i < array_len; i++) {
478                 sr_rational_set(&aval, array[i][0], array[i][1]);
479                 if (sr_rational_eq(&rval, &aval)) {
480                         *result = i;
481                         return SR_OK;
482                 }
483         }
484
485         return SR_ERR;
486 }
487
488 static struct sr_channel *get_channel_by_index_and_type(GSList *channel_lhead,
489                                                         int index, int type)
490 {
491         while (channel_lhead) {
492                 struct sr_channel *ch = channel_lhead->data;
493                 if (ch->index == index && ch->type == type)
494                         return ch;
495
496                 channel_lhead = channel_lhead->next;
497         }
498
499         return 0;
500 }
501
502 static int analog_channel_state_get(struct sr_dev_inst *sdi,
503                                     const struct scope_config *config,
504                                     struct scope_state *state)
505 {
506         unsigned int i, j;
507         char command[MAX_COMMAND_SIZE];
508         char *tmp_str;
509         struct sr_channel *ch;
510         struct sr_scpi_dev_inst *scpi = sdi->conn;
511
512         for (i = 0; i < config->analog_channels; i++) {
513                 g_snprintf(command, sizeof(command),
514                            (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
515                            i + 1);
516
517                 if (sr_scpi_get_bool(scpi, command,
518                                      &state->analog_channels[i].state) != SR_OK)
519                         return SR_ERR;
520
521                 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_ANALOG);
522                 if (ch)
523                         ch->enabled = state->analog_channels[i].state;
524
525                 g_snprintf(command, sizeof(command),
526                            (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
527                            i + 1);
528
529                 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
530                         return SR_ERR;
531
532                 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
533                         g_free(tmp_str);
534                         sr_err("Could not determine array index for vertical div scale.");
535                         return SR_ERR;
536                 }
537
538                 g_free(tmp_str);
539                 state->analog_channels[i].vdiv = j;
540
541                 g_snprintf(command, sizeof(command),
542                            (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
543                            i + 1);
544
545                 if (sr_scpi_get_float(scpi, command,
546                                      &state->analog_channels[i].vertical_offset) != SR_OK)
547                         return SR_ERR;
548
549                 g_snprintf(command, sizeof(command),
550                            (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
551                            i + 1);
552
553                 if (scope_state_get_array_option(scpi, command, config->coupling_options,
554                                          config->num_coupling_options,
555                                          &state->analog_channels[i].coupling) != SR_OK)
556                         return SR_ERR;
557
558                 g_snprintf(command, sizeof(command),
559                            (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
560                            i + 1);
561
562                 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
563                         return SR_ERR;
564
565                 if (tmp_str[0] == 'A')
566                         state->analog_channels[i].probe_unit = 'A';
567                 else
568                         state->analog_channels[i].probe_unit = 'V';
569                 g_free(tmp_str);
570         }
571
572         return SR_OK;
573 }
574
575 static int digital_channel_state_get(struct sr_dev_inst *sdi,
576                                      const struct scope_config *config,
577                                      struct scope_state *state)
578 {
579         unsigned int i;
580         int result = SR_ERR;
581         static char *logic_threshold_short[] = {};
582         char command[MAX_COMMAND_SIZE];
583         struct sr_channel *ch;
584         struct sr_scpi_dev_inst *scpi = sdi->conn;
585
586         for (i = 0; i < config->digital_channels; i++) {
587                 g_snprintf(command, sizeof(command),
588                            (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
589                            i);
590
591                 if (sr_scpi_get_bool(scpi, command,
592                                      &state->digital_channels[i]) != SR_OK)
593                         return SR_ERR;
594
595                 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_LOGIC);
596                 if (ch)
597                         ch->enabled = state->digital_channels[i];
598         }
599
600         /* According to the SCPI standard, the response to the command
601          * SCPI_CMD_GET_DIG_POD_THRESHOLD might return "USER" instead of
602          * "USER1".
603          *
604          * This makes more difficult to validate the response when the logic
605          * threshold is set to "USER1" and therefore we need to prevent device
606          * opening failures in such configuration case...
607          */
608         for (i = 0; i < config->num_logic_threshold; i++) {
609                 logic_threshold_short[i] = g_strdup((*config->logic_threshold)[i]);
610                 if (!strcmp("USER1", (*config->logic_threshold)[i]))
611                         g_strlcpy(logic_threshold_short[i],
612                                   (*config->logic_threshold)[i], strlen((*config->logic_threshold)[i]));
613         }
614
615         for (i = 0; i < config->digital_pods; i++) {
616                 g_snprintf(command, sizeof(command),
617                            (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
618                            i + 1);
619
620                 if (sr_scpi_get_bool(scpi, command,
621                                      &state->digital_pods[i].state) != SR_OK)
622                         goto exit;
623
624                 g_snprintf(command, sizeof(command),
625                            (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_THRESHOLD],
626                            i + 1);
627
628                 /* Check for both standard and shortened responses. */
629                 if (scope_state_get_array_option(scpi, command, config->logic_threshold,
630                                                  config->num_logic_threshold,
631                                                  &state->digital_pods[i].threshold) != SR_OK)
632                         if (scope_state_get_array_option(scpi, command, (const char * (*)[]) &logic_threshold_short,
633                                                          config->num_logic_threshold,
634                                                          &state->digital_pods[i].threshold) != SR_OK)
635                                 goto exit;
636
637                 if (!strcmp("USER1", (*config->logic_threshold)[state->digital_pods[i].threshold]))
638                         g_snprintf(command, sizeof(command),
639                                    (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
640                                    i + 1, 1); // USER1 logic threshold setting
641
642                 if (!strcmp("USER2", (*config->logic_threshold)[state->digital_pods[i].threshold]))
643                         g_snprintf(command, sizeof(command),
644                                    (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
645                                    i + 1, 2); // USER2 for custom logic_threshold setting
646
647                 if (!strcmp("USER1", (*config->logic_threshold)[state->digital_pods[i].threshold]) ||
648                     !strcmp("USER2", (*config->logic_threshold)[state->digital_pods[i].threshold]))
649                         if (sr_scpi_get_float(scpi, command,
650                             &state->digital_pods[i].user_threshold) != SR_OK)
651                                 goto exit;
652         }
653
654         result = SR_OK;
655
656 exit:
657         for (i = 0; i < config->num_logic_threshold; i++)
658                 g_free(logic_threshold_short[i]);
659
660         return result;
661 }
662
663 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
664 {
665         struct dev_context *devc;
666         struct scope_state *state;
667         const struct scope_config *config;
668         int tmp;
669         unsigned int i;
670         float tmp_float;
671         gboolean channel_found;
672         char tmp_str[MAX_COMMAND_SIZE];
673         char chan_name[20];
674
675         devc = sdi->priv;
676         config = devc->model_config;
677         state = devc->model_state;
678         channel_found = FALSE;
679
680         for (i = 0; i < config->analog_channels; i++) {
681                 if (!state->analog_channels[i].state)
682                         continue;
683                 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
684                 g_snprintf(tmp_str, sizeof(tmp_str),
685                            (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
686                            chan_name);
687                 channel_found = TRUE;
688                 break;
689         }
690
691         if (!channel_found) {
692                 for (i = 0; i < config->digital_pods; i++) {
693                         if (!state->digital_pods[i].state)
694                                 continue;
695                         g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
696                         g_snprintf(tmp_str, sizeof(tmp_str),
697                                    (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
698                                    chan_name);
699                         channel_found = TRUE;
700                         break;
701                 }
702         }
703
704         /* No channel is active, ask the instrument for the sample rate
705          * in single shot mode */
706         if (!channel_found) {
707                 if (sr_scpi_get_float(sdi->conn,
708                                       (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
709                                       &tmp_float) != SR_OK)
710                         return SR_ERR;
711
712                 state->sample_rate = tmp_float;
713         } else {
714                 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
715                         return SR_ERR;
716                 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
717                                              (*config->timebases)[state->timebase][1]) *
718                                             config->num_xdivs);
719         }
720
721         return SR_OK;
722 }
723
724 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
725 {
726         struct dev_context *devc;
727         struct scope_state *state;
728         const struct scope_config *config;
729         float tmp_float;
730         unsigned int i;
731         char *tmp_str;
732
733         devc = sdi->priv;
734         config = devc->model_config;
735         state = devc->model_state;
736
737         sr_info("Fetching scope state");
738
739         if (analog_channel_state_get(sdi, config, state) != SR_OK)
740                 return SR_ERR;
741
742         if (digital_channel_state_get(sdi, config, state) != SR_OK)
743                 return SR_ERR;
744
745         if (sr_scpi_get_float(sdi->conn,
746                         (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
747                         &tmp_float) != SR_OK)
748                 return SR_ERR;
749
750         if (sr_scpi_get_string(sdi->conn,
751                         (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
752                         &tmp_str) != SR_OK)
753                 return SR_ERR;
754
755         if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
756                 g_free(tmp_str);
757                 sr_err("Could not determine array index for time base.");
758                 return SR_ERR;
759         }
760         g_free(tmp_str);
761
762         state->timebase = i;
763
764         if (sr_scpi_get_float(sdi->conn,
765                         (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
766                         &tmp_float) != SR_OK)
767                 return SR_ERR;
768         state->horiz_triggerpos = tmp_float /
769                 (((double) (*config->timebases)[state->timebase][0] /
770                   (*config->timebases)[state->timebase][1]) * config->num_xdivs);
771         state->horiz_triggerpos -= 0.5;
772         state->horiz_triggerpos *= -1;
773
774         if (scope_state_get_array_option(sdi->conn,
775                         (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
776                         config->trigger_sources, config->num_trigger_sources,
777                         &state->trigger_source) != SR_OK)
778                 return SR_ERR;
779
780         if (scope_state_get_array_option(sdi->conn,
781                         (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
782                         config->trigger_slopes, config->num_trigger_slopes,
783                         &state->trigger_slope) != SR_OK)
784                 return SR_ERR;
785
786         if (sr_scpi_get_string(sdi->conn,
787                                (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_PATTERN],
788                                &state->trigger_pattern) != SR_OK)
789                 return SR_ERR;
790
791         if (hmo_update_sample_rate(sdi) != SR_OK)
792                 return SR_ERR;
793
794         sr_info("Fetching finished.");
795
796         scope_state_dump(config, state);
797
798         return SR_OK;
799 }
800
801 static struct scope_state *scope_state_new(const struct scope_config *config)
802 {
803         struct scope_state *state;
804
805         state = g_malloc0(sizeof(struct scope_state));
806         state->analog_channels = g_malloc0_n(config->analog_channels,
807                         sizeof(struct analog_channel_state));
808         state->digital_channels = g_malloc0_n(
809                         config->digital_channels, sizeof(gboolean));
810         state->digital_pods = g_malloc0_n(config->digital_pods,
811                         sizeof(struct digital_pod_state));
812
813         return state;
814 }
815
816 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
817 {
818         g_free(state->analog_channels);
819         g_free(state->digital_channels);
820         g_free(state->digital_pods);
821         g_free(state);
822 }
823
824 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
825 {
826         int model_index;
827         unsigned int i, j, group;
828         struct sr_channel *ch;
829         struct dev_context *devc;
830         int ret;
831
832         devc = sdi->priv;
833         model_index = -1;
834
835         /* Find the exact model. */
836         for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
837                 for (j = 0; scope_models[i].name[j]; j++) {
838                         if (!strcmp(sdi->model, scope_models[i].name[j])) {
839                                 model_index = i;
840                                 break;
841                         }
842                 }
843                 if (model_index != -1)
844                         break;
845         }
846
847         if (model_index == -1) {
848                 sr_dbg("Unsupported device.");
849                 return SR_ERR_NA;
850         }
851
852         devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
853                                         scope_models[model_index].analog_channels);
854         devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
855                                          scope_models[model_index].digital_pods);
856         if (!devc->analog_groups || !devc->digital_groups) {
857                 g_free(devc->analog_groups);
858                 g_free(devc->digital_groups);
859                 return SR_ERR_MALLOC;
860         }
861
862         /* Add analog channels. */
863         for (i = 0; i < scope_models[model_index].analog_channels; i++) {
864                 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
865                            (*scope_models[model_index].analog_names)[i]);
866
867                 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
868
869                 devc->analog_groups[i]->name = g_strdup(
870                         (char *)(*scope_models[model_index].analog_names)[i]);
871                 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
872
873                 sdi->channel_groups = g_slist_append(sdi->channel_groups,
874                                                    devc->analog_groups[i]);
875         }
876
877         /* Add digital channel groups. */
878         ret = SR_OK;
879         for (i = 0; i < scope_models[model_index].digital_pods; i++) {
880                 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
881                 if (!devc->digital_groups[i]) {
882                         ret = SR_ERR_MALLOC;
883                         break;
884                 }
885                 devc->digital_groups[i]->name = g_strdup_printf("POD%d", i + 1);
886                 sdi->channel_groups = g_slist_append(sdi->channel_groups,
887                                    devc->digital_groups[i]);
888         }
889         if (ret != SR_OK)
890                 return ret;
891
892         /* Add digital channels. */
893         for (i = 0; i < scope_models[model_index].digital_channels; i++) {
894                 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
895                            (*scope_models[model_index].digital_names)[i]);
896
897                 group = i / 8;
898                 devc->digital_groups[group]->channels = g_slist_append(
899                         devc->digital_groups[group]->channels, ch);
900         }
901
902         devc->model_config = &scope_models[model_index];
903         devc->samples_limit = 0;
904         devc->frame_limit = 0;
905
906         if (!(devc->model_state = scope_state_new(devc->model_config)))
907                 return SR_ERR_MALLOC;
908
909         return SR_OK;
910 }
911
912 /* Queue data of one channel group, for later submission. */
913 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
914                                   size_t group, GByteArray *pod_data)
915 {
916         size_t size;
917         GByteArray *store;
918         uint8_t *logic_data;
919         size_t idx, logic_step;
920
921         /*
922          * Upon first invocation, allocate the array which can hold the
923          * combined logic data for all channels. Assume that each channel
924          * will yield an identical number of samples per receive call.
925          *
926          * As a poor man's safety measure: (Silently) skip processing
927          * for unexpected sample counts, and ignore samples for
928          * unexpected channel groups. Don't bother with complicated
929          * resize logic, considering that many models only support one
930          * pod, and the most capable supported models have two pods of
931          * identical size. We haven't yet seen any "odd" configuration.
932          */
933         if (!devc->logic_data) {
934                 size = pod_data->len * devc->pod_count;
935                 store = g_byte_array_sized_new(size);
936                 memset(store->data, 0, size);
937                 store = g_byte_array_set_size(store, size);
938                 devc->logic_data = store;
939         } else {
940                 store = devc->logic_data;
941                 size = store->len / devc->pod_count;
942                 if (group >= devc->pod_count)
943                         return;
944         }
945
946         /*
947          * Fold the data of the most recently received channel group into
948          * the storage, where data resides for all channels combined.
949          */
950         logic_data = store->data;
951         logic_data += group;
952         logic_step = devc->pod_count;
953         for (idx = 0; idx < pod_data->len; idx++) {
954                 *logic_data = pod_data->data[idx];
955                 logic_data += logic_step;
956         }
957
958         /* Truncate acquisition if a smaller number of samples has been requested. */
959         if (devc->samples_limit > 0 && devc->logic_data->len > devc->samples_limit * devc->pod_count)
960                 devc->logic_data->len = devc->samples_limit * devc->pod_count;
961 }
962
963 /* Submit data for all channels, after the individual groups got collected. */
964 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
965                                    struct dev_context *devc)
966 {
967         struct sr_datafeed_packet packet;
968         struct sr_datafeed_logic logic;
969
970         if (!devc->logic_data)
971                 return;
972
973         logic.data = devc->logic_data->data;
974         logic.length = devc->logic_data->len;
975         logic.unitsize = devc->pod_count;
976
977         packet.type = SR_DF_LOGIC;
978         packet.payload = &logic;
979
980         sr_session_send(sdi, &packet);
981 }
982
983 /* Undo previous resource allocation. */
984 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc)
985 {
986
987         if (devc->logic_data) {
988                 g_byte_array_free(devc->logic_data, TRUE);
989                 devc->logic_data = NULL;
990         }
991         /*
992          * Keep 'pod_count'! It's required when more frames will be
993          * received, and does not harm when kept after acquisition.
994          */
995 }
996
997 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
998 {
999         struct sr_channel *ch;
1000         struct sr_dev_inst *sdi;
1001         struct dev_context *devc;
1002         struct scope_state *state;
1003         struct sr_datafeed_packet packet;
1004         GByteArray *data;
1005         struct sr_datafeed_analog analog;
1006         struct sr_analog_encoding encoding;
1007         struct sr_analog_meaning meaning;
1008         struct sr_analog_spec spec;
1009         struct sr_datafeed_logic logic;
1010         size_t group;
1011
1012         (void)fd;
1013         (void)revents;
1014
1015         data = NULL;
1016
1017         if (!(sdi = cb_data))
1018                 return TRUE;
1019
1020         if (!(devc = sdi->priv))
1021                 return TRUE;
1022
1023         /* Although this is correct in general, the USBTMC libusb implementation
1024          * currently does not generate an event prior to the first read. Often
1025          * it is ok to start reading just after the 50ms timeout. See bug #785.
1026         if (revents != G_IO_IN)
1027                 return TRUE;
1028         */
1029
1030         ch = devc->current_channel->data;
1031         state = devc->model_state;
1032
1033         /*
1034          * Send "frame begin" packet upon reception of data for the
1035          * first enabled channel.
1036          */
1037         if (devc->current_channel == devc->enabled_channels) {
1038                 packet.type = SR_DF_FRAME_BEGIN;
1039                 sr_session_send(sdi, &packet);
1040         }
1041
1042         /*
1043          * Pass on the received data of the channel(s).
1044          */
1045         switch (ch->type) {
1046         case SR_CHANNEL_ANALOG:
1047                 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1048                         if (data)
1049                                 g_byte_array_free(data, TRUE);
1050                         return TRUE;
1051                 }
1052
1053                 packet.type = SR_DF_ANALOG;
1054
1055                 analog.data = data->data;
1056                 analog.num_samples = data->len / sizeof(float);
1057                 /* Truncate acquisition if a smaller number of samples has been requested. */
1058                 if (devc->samples_limit > 0 && analog.num_samples > devc->samples_limit)
1059                         analog.num_samples = devc->samples_limit;
1060                 analog.encoding = &encoding;
1061                 analog.meaning = &meaning;
1062                 analog.spec = &spec;
1063
1064                 encoding.unitsize = sizeof(float);
1065                 encoding.is_signed = TRUE;
1066                 encoding.is_float = TRUE;
1067 #ifdef WORDS_BIGENDIAN
1068                 encoding.is_bigendian = TRUE;
1069 #else
1070                 encoding.is_bigendian = FALSE;
1071 #endif
1072                 /* TODO: Use proper 'digits' value for this device (and its modes). */
1073                 encoding.digits = 2;
1074                 encoding.is_digits_decimal = FALSE;
1075                 encoding.scale.p = 1;
1076                 encoding.scale.q = 1;
1077                 encoding.offset.p = 0;
1078                 encoding.offset.q = 1;
1079                 if (state->analog_channels[ch->index].probe_unit == 'V') {
1080                         meaning.mq = SR_MQ_VOLTAGE;
1081                         meaning.unit = SR_UNIT_VOLT;
1082                 } else {
1083                         meaning.mq = SR_MQ_CURRENT;
1084                         meaning.unit = SR_UNIT_AMPERE;
1085                 }
1086                 meaning.mqflags = 0;
1087                 meaning.channels = g_slist_append(NULL, ch);
1088                 /* TODO: Use proper 'digits' value for this device (and its modes). */
1089                 spec.spec_digits = 2;
1090                 packet.payload = &analog;
1091                 sr_session_send(sdi, &packet);
1092                 devc->num_samples = data->len / sizeof(float);
1093                 g_slist_free(meaning.channels);
1094                 g_byte_array_free(data, TRUE);
1095                 data = NULL;
1096                 break;
1097         case SR_CHANNEL_LOGIC:
1098                 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1099                         if (data)
1100                                 g_byte_array_free(data, TRUE);
1101                         return TRUE;
1102                 }
1103
1104                 /*
1105                  * If only data from the first pod is involved in the
1106                  * acquisition, then the raw input bytes can get passed
1107                  * forward for performance reasons. When the second pod
1108                  * is involved (either alone, or in combination with the
1109                  * first pod), then the received bytes need to be put
1110                  * into memory in such a layout that all channel groups
1111                  * get combined, and a unitsize larger than a single byte
1112                  * applies. The "queue" logic transparently copes with
1113                  * any such configuration. This works around the lack
1114                  * of support for "meaning" to logic data, which is used
1115                  * above for analog data.
1116                  */
1117                 if (devc->pod_count == 1) {
1118                         packet.type = SR_DF_LOGIC;
1119                         logic.data = data->data;
1120                         logic.length = data->len;
1121                         /* Truncate acquisition if a smaller number of samples has been requested. */
1122                         if (devc->samples_limit > 0 && logic.length > devc->samples_limit)
1123                                 logic.length = devc->samples_limit;
1124                         logic.unitsize = 1;
1125                         packet.payload = &logic;
1126                         sr_session_send(sdi, &packet);
1127                 } else {
1128                         group = ch->index / 8;
1129                         hmo_queue_logic_data(devc, group, data);
1130                 }
1131
1132                 devc->num_samples = data->len / devc->pod_count;
1133                 g_byte_array_free(data, TRUE);
1134                 data = NULL;
1135                 break;
1136         default:
1137                 sr_err("Invalid channel type.");
1138                 break;
1139         }
1140
1141         /*
1142          * Advance to the next enabled channel. When data for all enabled
1143          * channels was received, then flush potentially queued logic data,
1144          * and send the "frame end" packet.
1145          */
1146         if (devc->current_channel->next) {
1147                 devc->current_channel = devc->current_channel->next;
1148                 hmo_request_data(sdi);
1149                 return TRUE;
1150         }
1151         hmo_send_logic_packet(sdi, devc);
1152
1153         /*
1154          * Release the logic data storage after each frame. This copes
1155          * with sample counts that differ in length per frame. -- Is
1156          * this a real constraint when acquiring multiple frames with
1157          * identical device settings?
1158          */
1159         hmo_cleanup_logic_data(devc);
1160
1161         packet.type = SR_DF_FRAME_END;
1162         sr_session_send(sdi, &packet);
1163
1164         /*
1165          * End of frame was reached. Stop acquisition after the specified
1166          * number of frames or after the specified number of samples, or
1167          * continue reception by starting over at the first enabled channel.
1168          */
1169         if (++devc->num_frames >= devc->frame_limit || devc->num_samples >= devc->samples_limit) {
1170                 sr_dev_acquisition_stop(sdi);
1171                 hmo_cleanup_logic_data(devc);
1172         } else {
1173                 devc->current_channel = devc->enabled_channels;
1174                 hmo_request_data(sdi);
1175         }
1176
1177         return TRUE;
1178 }