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