]>
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 <stdlib.h> | |
22 | #include "scpi.h" | |
23 | #include "protocol.h" | |
24 | ||
25 | static struct sr_dev_driver hameg_hmo_driver_info; | |
26 | ||
27 | static const char *manufacturers[] = { | |
28 | "HAMEG", | |
29 | "Rohde&Schwarz", | |
30 | }; | |
31 | ||
32 | static const uint32_t scanopts[] = { | |
33 | SR_CONF_CONN, | |
34 | SR_CONF_SERIALCOMM, | |
35 | }; | |
36 | ||
37 | static const uint32_t drvopts[] = { | |
38 | SR_CONF_OSCILLOSCOPE, | |
39 | }; | |
40 | ||
41 | enum { | |
42 | CG_INVALID = -1, | |
43 | CG_NONE, | |
44 | CG_ANALOG, | |
45 | CG_DIGITAL, | |
46 | }; | |
47 | ||
48 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) | |
49 | { | |
50 | struct sr_dev_inst *sdi; | |
51 | struct dev_context *devc; | |
52 | struct sr_scpi_hw_info *hw_info; | |
53 | ||
54 | sdi = NULL; | |
55 | devc = NULL; | |
56 | hw_info = NULL; | |
57 | ||
58 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { | |
59 | sr_info("Couldn't get IDN response."); | |
60 | goto fail; | |
61 | } | |
62 | ||
63 | if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0) | |
64 | goto fail; | |
65 | ||
66 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
67 | sdi->vendor = g_strdup(hw_info->manufacturer); | |
68 | sdi->model = g_strdup(hw_info->model); | |
69 | sdi->version = g_strdup(hw_info->firmware_version); | |
70 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
71 | sdi->driver = &hameg_hmo_driver_info; | |
72 | sdi->inst_type = SR_INST_SCPI; | |
73 | sdi->conn = scpi; | |
74 | ||
75 | sr_scpi_hw_info_free(hw_info); | |
76 | hw_info = NULL; | |
77 | ||
78 | devc = g_malloc0(sizeof(struct dev_context)); | |
79 | ||
80 | sdi->priv = devc; | |
81 | ||
82 | if (hmo_init_device(sdi) != SR_OK) | |
83 | goto fail; | |
84 | ||
85 | return sdi; | |
86 | ||
87 | fail: | |
88 | sr_scpi_hw_info_free(hw_info); | |
89 | sr_dev_inst_free(sdi); | |
90 | g_free(devc); | |
91 | ||
92 | return NULL; | |
93 | } | |
94 | ||
95 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
96 | { | |
97 | return sr_scpi_scan(di->context, options, probe_device); | |
98 | } | |
99 | ||
100 | static void clear_helper(struct dev_context *devc) | |
101 | { | |
102 | hmo_scope_state_free(devc->model_state); | |
103 | g_free(devc->analog_groups); | |
104 | g_free(devc->digital_groups); | |
105 | } | |
106 | ||
107 | static int dev_clear(const struct sr_dev_driver *di) | |
108 | { | |
109 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); | |
110 | } | |
111 | ||
112 | static int dev_open(struct sr_dev_inst *sdi) | |
113 | { | |
114 | if (sr_scpi_open(sdi->conn) != SR_OK) | |
115 | return SR_ERR; | |
116 | ||
117 | if (hmo_scope_state_get(sdi) != SR_OK) | |
118 | return SR_ERR; | |
119 | ||
120 | return SR_OK; | |
121 | } | |
122 | ||
123 | static int dev_close(struct sr_dev_inst *sdi) | |
124 | { | |
125 | return sr_scpi_close(sdi->conn); | |
126 | } | |
127 | ||
128 | static int check_channel_group(struct dev_context *devc, | |
129 | const struct sr_channel_group *cg) | |
130 | { | |
131 | const struct scope_config *model; | |
132 | ||
133 | model = devc->model_config; | |
134 | ||
135 | if (!cg) | |
136 | return CG_NONE; | |
137 | ||
138 | if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0) | |
139 | return CG_ANALOG; | |
140 | ||
141 | if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0) | |
142 | return CG_DIGITAL; | |
143 | ||
144 | sr_err("Invalid channel group specified."); | |
145 | ||
146 | return CG_INVALID; | |
147 | } | |
148 | ||
149 | static int config_get(uint32_t key, GVariant **data, | |
150 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
151 | { | |
152 | int cg_type, idx; | |
153 | struct dev_context *devc; | |
154 | const struct scope_config *model; | |
155 | struct scope_state *state; | |
156 | ||
157 | if (!sdi) | |
158 | return SR_ERR_ARG; | |
159 | ||
160 | devc = sdi->priv; | |
161 | ||
162 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
163 | return SR_ERR; | |
164 | ||
165 | model = devc->model_config; | |
166 | state = devc->model_state; | |
167 | ||
168 | switch (key) { | |
169 | case SR_CONF_NUM_HDIV: | |
170 | *data = g_variant_new_int32(model->num_xdivs); | |
171 | break; | |
172 | case SR_CONF_TIMEBASE: | |
173 | *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0], | |
174 | (*model->timebases)[state->timebase][1]); | |
175 | break; | |
176 | case SR_CONF_NUM_VDIV: | |
177 | if (!cg) | |
178 | return SR_ERR_CHANNEL_GROUP; | |
179 | if (cg_type != CG_ANALOG) | |
180 | return SR_ERR_NA; | |
181 | if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0) | |
182 | return SR_ERR_ARG; | |
183 | *data = g_variant_new_int32(model->num_ydivs); | |
184 | break; | |
185 | case SR_CONF_VDIV: | |
186 | if (!cg) | |
187 | return SR_ERR_CHANNEL_GROUP; | |
188 | if (cg_type != CG_ANALOG) | |
189 | return SR_ERR_NA; | |
190 | if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) | |
191 | return SR_ERR_ARG; | |
192 | *data = g_variant_new("(tt)", | |
193 | (*model->vdivs)[state->analog_channels[idx].vdiv][0], | |
194 | (*model->vdivs)[state->analog_channels[idx].vdiv][1]); | |
195 | break; | |
196 | case SR_CONF_TRIGGER_SOURCE: | |
197 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
198 | break; | |
199 | case SR_CONF_TRIGGER_SLOPE: | |
200 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
201 | break; | |
202 | case SR_CONF_HORIZ_TRIGGERPOS: | |
203 | *data = g_variant_new_double(state->horiz_triggerpos); | |
204 | break; | |
205 | case SR_CONF_COUPLING: | |
206 | if (!cg) | |
207 | return SR_ERR_CHANNEL_GROUP; | |
208 | if (cg_type != CG_ANALOG) | |
209 | return SR_ERR_NA; | |
210 | if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) | |
211 | return SR_ERR_ARG; | |
212 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]); | |
213 | break; | |
214 | case SR_CONF_SAMPLERATE: | |
215 | *data = g_variant_new_uint64(state->sample_rate); | |
216 | break; | |
217 | default: | |
218 | return SR_ERR_NA; | |
219 | } | |
220 | ||
221 | return SR_OK; | |
222 | } | |
223 | ||
224 | static int config_set(uint32_t key, GVariant *data, | |
225 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
226 | { | |
227 | int ret, cg_type, idx, j; | |
228 | char command[MAX_COMMAND_SIZE], float_str[30]; | |
229 | struct dev_context *devc; | |
230 | const struct scope_config *model; | |
231 | struct scope_state *state; | |
232 | double tmp_d; | |
233 | gboolean update_sample_rate; | |
234 | ||
235 | if (!sdi) | |
236 | return SR_ERR_ARG; | |
237 | ||
238 | devc = sdi->priv; | |
239 | ||
240 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
241 | return SR_ERR; | |
242 | ||
243 | model = devc->model_config; | |
244 | state = devc->model_state; | |
245 | update_sample_rate = FALSE; | |
246 | ||
247 | switch (key) { | |
248 | case SR_CONF_LIMIT_SAMPLES: | |
249 | devc->samples_limit = g_variant_get_uint64(data); | |
250 | ret = SR_OK; | |
251 | break; | |
252 | case SR_CONF_LIMIT_FRAMES: | |
253 | devc->frame_limit = g_variant_get_uint64(data); | |
254 | ret = SR_OK; | |
255 | break; | |
256 | case SR_CONF_TRIGGER_SOURCE: | |
257 | if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0) | |
258 | return SR_ERR_ARG; | |
259 | state->trigger_source = idx; | |
260 | g_snprintf(command, sizeof(command), | |
261 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
262 | (*model->trigger_sources)[idx]); | |
263 | ret = sr_scpi_send(sdi->conn, command); | |
264 | break; | |
265 | case SR_CONF_VDIV: | |
266 | if (!cg) | |
267 | return SR_ERR_CHANNEL_GROUP; | |
268 | if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0) | |
269 | return SR_ERR_ARG; | |
270 | if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) | |
271 | return SR_ERR_ARG; | |
272 | state->analog_channels[j].vdiv = idx; | |
273 | g_ascii_formatd(float_str, sizeof(float_str), "%E", | |
274 | (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]); | |
275 | g_snprintf(command, sizeof(command), | |
276 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
277 | j + 1, float_str); | |
278 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
279 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
280 | return SR_ERR; | |
281 | ret = SR_OK; | |
282 | break; | |
283 | case SR_CONF_TIMEBASE: | |
284 | if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0) | |
285 | return SR_ERR_ARG; | |
286 | state->timebase = idx; | |
287 | g_ascii_formatd(float_str, sizeof(float_str), "%E", | |
288 | (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]); | |
289 | g_snprintf(command, sizeof(command), | |
290 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
291 | float_str); | |
292 | ret = sr_scpi_send(sdi->conn, command); | |
293 | update_sample_rate = TRUE; | |
294 | break; | |
295 | case SR_CONF_HORIZ_TRIGGERPOS: | |
296 | tmp_d = g_variant_get_double(data); | |
297 | if (tmp_d < 0.0 || tmp_d > 1.0) | |
298 | return SR_ERR; | |
299 | state->horiz_triggerpos = tmp_d; | |
300 | tmp_d = -(tmp_d - 0.5) * | |
301 | ((double) (*model->timebases)[state->timebase][0] / | |
302 | (*model->timebases)[state->timebase][1]) | |
303 | * model->num_xdivs; | |
304 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); | |
305 | g_snprintf(command, sizeof(command), | |
306 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
307 | float_str); | |
308 | ret = sr_scpi_send(sdi->conn, command); | |
309 | break; | |
310 | case SR_CONF_TRIGGER_SLOPE: | |
311 | if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0) | |
312 | return SR_ERR_ARG; | |
313 | state->trigger_slope = idx; | |
314 | g_snprintf(command, sizeof(command), | |
315 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
316 | (*model->trigger_slopes)[idx]); | |
317 | ret = sr_scpi_send(sdi->conn, command); | |
318 | break; | |
319 | case SR_CONF_COUPLING: | |
320 | if (!cg) | |
321 | return SR_ERR_CHANNEL_GROUP; | |
322 | if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0) | |
323 | return SR_ERR_ARG; | |
324 | if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) | |
325 | return SR_ERR_ARG; | |
326 | state->analog_channels[j].coupling = idx; | |
327 | g_snprintf(command, sizeof(command), | |
328 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
329 | j + 1, (*model->coupling_options)[idx]); | |
330 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
331 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
332 | return SR_ERR; | |
333 | ret = SR_OK; | |
334 | break; | |
335 | default: | |
336 | ret = SR_ERR_NA; | |
337 | break; | |
338 | } | |
339 | ||
340 | if (ret == SR_OK) | |
341 | ret = sr_scpi_get_opc(sdi->conn); | |
342 | ||
343 | if (ret == SR_OK && update_sample_rate) | |
344 | ret = hmo_update_sample_rate(sdi); | |
345 | ||
346 | return ret; | |
347 | } | |
348 | ||
349 | static int config_list(uint32_t key, GVariant **data, | |
350 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
351 | { | |
352 | int cg_type = CG_NONE; | |
353 | struct dev_context *devc = NULL; | |
354 | const struct scope_config *model = NULL; | |
355 | ||
356 | if (sdi) { | |
357 | devc = sdi->priv; | |
358 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
359 | return SR_ERR; | |
360 | ||
361 | model = devc->model_config; | |
362 | } | |
363 | ||
364 | switch (key) { | |
365 | case SR_CONF_SCAN_OPTIONS: | |
366 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts)); | |
367 | break; | |
368 | case SR_CONF_DEVICE_OPTIONS: | |
369 | if (!cg) { | |
370 | if (model) | |
371 | *data = std_gvar_array_u32(*model->devopts, model->num_devopts); | |
372 | else | |
373 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts)); | |
374 | } else if (cg_type == CG_ANALOG) { | |
375 | *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog); | |
376 | } else { | |
377 | *data = std_gvar_array_u32(NULL, 0); | |
378 | } | |
379 | break; | |
380 | case SR_CONF_COUPLING: | |
381 | if (!cg) | |
382 | return SR_ERR_CHANNEL_GROUP; | |
383 | if (!model) | |
384 | return SR_ERR_ARG; | |
385 | *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options); | |
386 | break; | |
387 | case SR_CONF_TRIGGER_SOURCE: | |
388 | if (!model) | |
389 | return SR_ERR_ARG; | |
390 | *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources); | |
391 | break; | |
392 | case SR_CONF_TRIGGER_SLOPE: | |
393 | if (!model) | |
394 | return SR_ERR_ARG; | |
395 | *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes); | |
396 | break; | |
397 | case SR_CONF_TIMEBASE: | |
398 | if (!model) | |
399 | return SR_ERR_ARG; | |
400 | *data = std_gvar_tuple_array(*model->timebases, model->num_timebases); | |
401 | break; | |
402 | case SR_CONF_VDIV: | |
403 | if (!cg) | |
404 | return SR_ERR_CHANNEL_GROUP; | |
405 | if (!model) | |
406 | return SR_ERR_ARG; | |
407 | *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs); | |
408 | break; | |
409 | default: | |
410 | return SR_ERR_NA; | |
411 | } | |
412 | ||
413 | return SR_OK; | |
414 | } | |
415 | ||
416 | SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi) | |
417 | { | |
418 | char command[MAX_COMMAND_SIZE]; | |
419 | struct sr_channel *ch; | |
420 | struct dev_context *devc; | |
421 | const struct scope_config *model; | |
422 | ||
423 | devc = sdi->priv; | |
424 | model = devc->model_config; | |
425 | ||
426 | ch = devc->current_channel->data; | |
427 | ||
428 | switch (ch->type) { | |
429 | case SR_CHANNEL_ANALOG: | |
430 | g_snprintf(command, sizeof(command), | |
431 | (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA], | |
432 | #ifdef WORDS_BIGENDIAN | |
433 | "MSBF", | |
434 | #else | |
435 | "LSBF", | |
436 | #endif | |
437 | ch->index + 1); | |
438 | break; | |
439 | case SR_CHANNEL_LOGIC: | |
440 | g_snprintf(command, sizeof(command), | |
441 | (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA], | |
442 | ch->index < 8 ? 1 : 2); | |
443 | break; | |
444 | default: | |
445 | sr_err("Invalid channel type."); | |
446 | break; | |
447 | } | |
448 | ||
449 | return sr_scpi_send(sdi->conn, command); | |
450 | } | |
451 | ||
452 | static int hmo_check_channels(GSList *channels) | |
453 | { | |
454 | GSList *l; | |
455 | struct sr_channel *ch; | |
456 | gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT]; | |
457 | gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT]; | |
458 | size_t idx; | |
459 | ||
460 | /* Preset "not enabled" for all channels / pods. */ | |
461 | for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++) | |
462 | enabled_chan[idx] = FALSE; | |
463 | for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++) | |
464 | enabled_pod[idx] = FALSE; | |
465 | ||
466 | /* | |
467 | * Determine which channels / pods are required for the caller's | |
468 | * specified configuration. | |
469 | */ | |
470 | for (l = channels; l; l = l->next) { | |
471 | ch = l->data; | |
472 | switch (ch->type) { | |
473 | case SR_CHANNEL_ANALOG: | |
474 | idx = ch->index; | |
475 | if (idx < ARRAY_SIZE(enabled_chan)) | |
476 | enabled_chan[idx] = TRUE; | |
477 | break; | |
478 | case SR_CHANNEL_LOGIC: | |
479 | idx = ch->index / 8; | |
480 | if (idx < ARRAY_SIZE(enabled_pod)) | |
481 | enabled_pod[idx] = TRUE; | |
482 | break; | |
483 | default: | |
484 | return SR_ERR; | |
485 | } | |
486 | } | |
487 | ||
488 | /* | |
489 | * Check for resource conflicts. Some channels can be either | |
490 | * analog or digital, but never both at the same time. | |
491 | * | |
492 | * Note that the constraints might depend on the specific model. | |
493 | * These tests might need some adjustment when support for more | |
494 | * models gets added to the driver. | |
495 | */ | |
496 | if (enabled_pod[0] && enabled_chan[2]) | |
497 | return SR_ERR; | |
498 | if (enabled_pod[1] && enabled_chan[3]) | |
499 | return SR_ERR; | |
500 | return SR_OK; | |
501 | } | |
502 | ||
503 | static int hmo_setup_channels(const struct sr_dev_inst *sdi) | |
504 | { | |
505 | GSList *l; | |
506 | unsigned int i; | |
507 | gboolean *pod_enabled, setup_changed; | |
508 | char command[MAX_COMMAND_SIZE]; | |
509 | struct scope_state *state; | |
510 | const struct scope_config *model; | |
511 | struct sr_channel *ch; | |
512 | struct dev_context *devc; | |
513 | struct sr_scpi_dev_inst *scpi; | |
514 | int ret; | |
515 | ||
516 | devc = sdi->priv; | |
517 | scpi = sdi->conn; | |
518 | state = devc->model_state; | |
519 | model = devc->model_config; | |
520 | setup_changed = FALSE; | |
521 | ||
522 | pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods); | |
523 | ||
524 | for (l = sdi->channels; l; l = l->next) { | |
525 | ch = l->data; | |
526 | switch (ch->type) { | |
527 | case SR_CHANNEL_ANALOG: | |
528 | if (ch->enabled == state->analog_channels[ch->index].state) | |
529 | break; | |
530 | g_snprintf(command, sizeof(command), | |
531 | (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE], | |
532 | ch->index + 1, ch->enabled); | |
533 | ||
534 | if (sr_scpi_send(scpi, command) != SR_OK) { | |
535 | g_free(pod_enabled); | |
536 | return SR_ERR; | |
537 | } | |
538 | state->analog_channels[ch->index].state = ch->enabled; | |
539 | setup_changed = TRUE; | |
540 | break; | |
541 | case SR_CHANNEL_LOGIC: | |
542 | /* | |
543 | * A digital POD needs to be enabled for every group of | |
544 | * 8 channels. | |
545 | */ | |
546 | if (ch->enabled) | |
547 | pod_enabled[ch->index < 8 ? 0 : 1] = TRUE; | |
548 | ||
549 | if (ch->enabled == state->digital_channels[ch->index]) | |
550 | break; | |
551 | g_snprintf(command, sizeof(command), | |
552 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE], | |
553 | ch->index, ch->enabled); | |
554 | ||
555 | if (sr_scpi_send(scpi, command) != SR_OK) { | |
556 | g_free(pod_enabled); | |
557 | return SR_ERR; | |
558 | } | |
559 | ||
560 | state->digital_channels[ch->index] = ch->enabled; | |
561 | setup_changed = TRUE; | |
562 | break; | |
563 | default: | |
564 | g_free(pod_enabled); | |
565 | return SR_ERR; | |
566 | } | |
567 | } | |
568 | ||
569 | ret = SR_OK; | |
570 | for (i = 0; i < model->digital_pods; i++) { | |
571 | if (state->digital_pods[i] == pod_enabled[i]) | |
572 | continue; | |
573 | g_snprintf(command, sizeof(command), | |
574 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE], | |
575 | i + 1, pod_enabled[i]); | |
576 | if (sr_scpi_send(scpi, command) != SR_OK) { | |
577 | ret = SR_ERR; | |
578 | break; | |
579 | } | |
580 | state->digital_pods[i] = pod_enabled[i]; | |
581 | setup_changed = TRUE; | |
582 | } | |
583 | g_free(pod_enabled); | |
584 | if (ret != SR_OK) | |
585 | return ret; | |
586 | ||
587 | if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK) | |
588 | return SR_ERR; | |
589 | ||
590 | return SR_OK; | |
591 | } | |
592 | ||
593 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
594 | { | |
595 | GSList *l; | |
596 | gboolean digital_added[MAX_DIGITAL_GROUP_COUNT]; | |
597 | size_t group, pod_count; | |
598 | struct sr_channel *ch; | |
599 | struct dev_context *devc; | |
600 | struct sr_scpi_dev_inst *scpi; | |
601 | int ret; | |
602 | ||
603 | scpi = sdi->conn; | |
604 | devc = sdi->priv; | |
605 | ||
606 | devc->num_samples = 0; | |
607 | devc->num_frames = 0; | |
608 | ||
609 | /* Preset empty results. */ | |
610 | for (group = 0; group < ARRAY_SIZE(digital_added); group++) | |
611 | digital_added[group] = FALSE; | |
612 | g_slist_free(devc->enabled_channels); | |
613 | devc->enabled_channels = NULL; | |
614 | ||
615 | /* | |
616 | * Contruct the list of enabled channels. Determine the highest | |
617 | * number of digital pods involved in the acquisition. | |
618 | */ | |
619 | pod_count = 0; | |
620 | for (l = sdi->channels; l; l = l->next) { | |
621 | ch = l->data; | |
622 | if (!ch->enabled) | |
623 | continue; | |
624 | /* Only add a single digital channel per group (pod). */ | |
625 | group = ch->index / 8; | |
626 | if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) { | |
627 | devc->enabled_channels = g_slist_append( | |
628 | devc->enabled_channels, ch); | |
629 | if (ch->type == SR_CHANNEL_LOGIC) { | |
630 | digital_added[group] = TRUE; | |
631 | if (pod_count < group + 1) | |
632 | pod_count = group + 1; | |
633 | } | |
634 | } | |
635 | } | |
636 | if (!devc->enabled_channels) | |
637 | return SR_ERR; | |
638 | devc->pod_count = pod_count; | |
639 | devc->logic_data = NULL; | |
640 | ||
641 | /* | |
642 | * Check constraints. Some channels can be either analog or | |
643 | * digital, but not both at the same time. | |
644 | */ | |
645 | if (hmo_check_channels(devc->enabled_channels) != SR_OK) { | |
646 | sr_err("Invalid channel configuration specified!"); | |
647 | ret = SR_ERR_NA; | |
648 | goto free_enabled; | |
649 | } | |
650 | ||
651 | /* | |
652 | * Configure the analog and digital channels and the | |
653 | * corresponding digital pods. | |
654 | */ | |
655 | if (hmo_setup_channels(sdi) != SR_OK) { | |
656 | sr_err("Failed to setup channel configuration!"); | |
657 | ret = SR_ERR; | |
658 | goto free_enabled; | |
659 | } | |
660 | ||
661 | /* | |
662 | * Start acquisition on the first enabled channel. The | |
663 | * receive routine will continue driving the acquisition. | |
664 | */ | |
665 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, | |
666 | hmo_receive_data, (void *)sdi); | |
667 | ||
668 | std_session_send_df_header(sdi); | |
669 | ||
670 | devc->current_channel = devc->enabled_channels; | |
671 | ||
672 | return hmo_request_data(sdi); | |
673 | ||
674 | free_enabled: | |
675 | g_slist_free(devc->enabled_channels); | |
676 | devc->enabled_channels = NULL; | |
677 | return ret; | |
678 | } | |
679 | ||
680 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
681 | { | |
682 | struct dev_context *devc; | |
683 | struct sr_scpi_dev_inst *scpi; | |
684 | ||
685 | std_session_send_df_end(sdi); | |
686 | ||
687 | devc = sdi->priv; | |
688 | ||
689 | devc->num_samples = 0; | |
690 | devc->num_frames = 0; | |
691 | g_slist_free(devc->enabled_channels); | |
692 | devc->enabled_channels = NULL; | |
693 | scpi = sdi->conn; | |
694 | sr_scpi_source_remove(sdi->session, scpi); | |
695 | ||
696 | return SR_OK; | |
697 | } | |
698 | ||
699 | static struct sr_dev_driver hameg_hmo_driver_info = { | |
700 | .name = "hameg-hmo", | |
701 | .longname = "Hameg HMO", | |
702 | .api_version = 1, | |
703 | .init = std_init, | |
704 | .cleanup = std_cleanup, | |
705 | .scan = scan, | |
706 | .dev_list = std_dev_list, | |
707 | .dev_clear = dev_clear, | |
708 | .config_get = config_get, | |
709 | .config_set = config_set, | |
710 | .config_list = config_list, | |
711 | .dev_open = dev_open, | |
712 | .dev_close = dev_close, | |
713 | .dev_acquisition_start = dev_acquisition_start, | |
714 | .dev_acquisition_stop = dev_acquisition_stop, | |
715 | .context = NULL, | |
716 | }; | |
717 | SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info); |