]>
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 | #define SERIALCOMM "115200/8n1/flow=1" | |
26 | ||
27 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info; | |
28 | ||
29 | static const char *manufacturers[] = { | |
30 | "HAMEG", | |
31 | "Rohde&Schwarz", | |
32 | }; | |
33 | ||
34 | static const uint32_t drvopts[] = { | |
35 | SR_CONF_OSCILLOSCOPE, | |
36 | }; | |
37 | ||
38 | static const uint32_t scanopts[] = { | |
39 | SR_CONF_CONN, | |
40 | SR_CONF_SERIALCOMM, | |
41 | }; | |
42 | ||
43 | enum { | |
44 | CG_INVALID = -1, | |
45 | CG_NONE, | |
46 | CG_ANALOG, | |
47 | CG_DIGITAL, | |
48 | }; | |
49 | ||
50 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
51 | { | |
52 | return std_init(sr_ctx, di, LOG_PREFIX); | |
53 | } | |
54 | ||
55 | static int check_manufacturer(const char *manufacturer) | |
56 | { | |
57 | unsigned int i; | |
58 | ||
59 | for (i = 0; i < ARRAY_SIZE(manufacturers); i++) | |
60 | if (!strcmp(manufacturer, manufacturers[i])) | |
61 | return SR_OK; | |
62 | ||
63 | return SR_ERR; | |
64 | } | |
65 | ||
66 | static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi) | |
67 | { | |
68 | struct sr_dev_inst *sdi; | |
69 | struct dev_context *devc; | |
70 | struct sr_scpi_hw_info *hw_info; | |
71 | ||
72 | sdi = NULL; | |
73 | devc = NULL; | |
74 | hw_info = NULL; | |
75 | ||
76 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { | |
77 | sr_info("Couldn't get IDN response."); | |
78 | goto fail; | |
79 | } | |
80 | ||
81 | if (check_manufacturer(hw_info->manufacturer) != SR_OK) | |
82 | goto fail; | |
83 | ||
84 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
85 | sdi->vendor = g_strdup(hw_info->manufacturer); | |
86 | sdi->model = g_strdup(hw_info->model); | |
87 | sdi->version = g_strdup(hw_info->firmware_version); | |
88 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
89 | sdi->driver = &hameg_hmo_driver_info; | |
90 | sdi->inst_type = SR_INST_SCPI; | |
91 | sdi->conn = scpi; | |
92 | ||
93 | sr_scpi_hw_info_free(hw_info); | |
94 | hw_info = NULL; | |
95 | ||
96 | devc = g_malloc0(sizeof(struct dev_context)); | |
97 | ||
98 | sdi->priv = devc; | |
99 | ||
100 | if (hmo_init_device(sdi) != SR_OK) | |
101 | goto fail; | |
102 | ||
103 | return sdi; | |
104 | ||
105 | fail: | |
106 | if (hw_info) | |
107 | sr_scpi_hw_info_free(hw_info); | |
108 | if (sdi) | |
109 | sr_dev_inst_free(sdi); | |
110 | g_free(devc); | |
111 | ||
112 | return NULL; | |
113 | } | |
114 | ||
115 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
116 | { | |
117 | return sr_scpi_scan(di->context, options, hmo_probe_serial_device); | |
118 | } | |
119 | ||
120 | static GSList *dev_list(const struct sr_dev_driver *di) | |
121 | { | |
122 | return ((struct drv_context *)(di->context))->instances; | |
123 | } | |
124 | ||
125 | static void clear_helper(void *priv) | |
126 | { | |
127 | struct dev_context *devc; | |
128 | ||
129 | devc = priv; | |
130 | ||
131 | hmo_scope_state_free(devc->model_state); | |
132 | ||
133 | g_free(devc->analog_groups); | |
134 | g_free(devc->digital_groups); | |
135 | ||
136 | g_free(devc); | |
137 | } | |
138 | ||
139 | static int dev_clear(const struct sr_dev_driver *di) | |
140 | { | |
141 | return std_dev_clear(di, clear_helper); | |
142 | } | |
143 | ||
144 | static int dev_open(struct sr_dev_inst *sdi) | |
145 | { | |
146 | if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) | |
147 | return SR_ERR; | |
148 | ||
149 | if (hmo_scope_state_get(sdi) != SR_OK) | |
150 | return SR_ERR; | |
151 | ||
152 | sdi->status = SR_ST_ACTIVE; | |
153 | ||
154 | return SR_OK; | |
155 | } | |
156 | ||
157 | static int dev_close(struct sr_dev_inst *sdi) | |
158 | { | |
159 | if (sdi->status == SR_ST_INACTIVE) | |
160 | return SR_OK; | |
161 | ||
162 | sr_scpi_close(sdi->conn); | |
163 | ||
164 | sdi->status = SR_ST_INACTIVE; | |
165 | ||
166 | return SR_OK; | |
167 | } | |
168 | ||
169 | static int cleanup(const struct sr_dev_driver *di) | |
170 | { | |
171 | dev_clear(di); | |
172 | ||
173 | return SR_OK; | |
174 | } | |
175 | ||
176 | static int check_channel_group(struct dev_context *devc, | |
177 | const struct sr_channel_group *cg) | |
178 | { | |
179 | unsigned int i; | |
180 | const struct scope_config *model; | |
181 | ||
182 | model = devc->model_config; | |
183 | ||
184 | if (!cg) | |
185 | return CG_NONE; | |
186 | ||
187 | for (i = 0; i < model->analog_channels; i++) | |
188 | if (cg == devc->analog_groups[i]) | |
189 | return CG_ANALOG; | |
190 | ||
191 | for (i = 0; i < model->digital_pods; i++) | |
192 | if (cg == devc->digital_groups[i]) | |
193 | return CG_DIGITAL; | |
194 | ||
195 | sr_err("Invalid channel group specified."); | |
196 | ||
197 | return CG_INVALID; | |
198 | } | |
199 | ||
200 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
201 | const struct sr_channel_group *cg) | |
202 | { | |
203 | int ret, cg_type; | |
204 | unsigned int i; | |
205 | struct dev_context *devc; | |
206 | const struct scope_config *model; | |
207 | struct scope_state *state; | |
208 | ||
209 | if (!sdi || !(devc = sdi->priv)) | |
210 | return SR_ERR_ARG; | |
211 | ||
212 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
213 | return SR_ERR; | |
214 | ||
215 | ret = SR_ERR_NA; | |
216 | model = devc->model_config; | |
217 | state = devc->model_state; | |
218 | ||
219 | switch (key) { | |
220 | case SR_CONF_NUM_HDIV: | |
221 | *data = g_variant_new_int32(model->num_xdivs); | |
222 | ret = SR_OK; | |
223 | break; | |
224 | case SR_CONF_TIMEBASE: | |
225 | *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0], | |
226 | (*model->timebases)[state->timebase][1]); | |
227 | ret = SR_OK; | |
228 | break; | |
229 | case SR_CONF_NUM_VDIV: | |
230 | if (cg_type == CG_NONE) { | |
231 | sr_err("No channel group specified."); | |
232 | return SR_ERR_CHANNEL_GROUP; | |
233 | } else if (cg_type == CG_ANALOG) { | |
234 | for (i = 0; i < model->analog_channels; i++) { | |
235 | if (cg != devc->analog_groups[i]) | |
236 | continue; | |
237 | *data = g_variant_new_int32(model->num_ydivs); | |
238 | ret = SR_OK; | |
239 | break; | |
240 | } | |
241 | ||
242 | } else { | |
243 | ret = SR_ERR_NA; | |
244 | } | |
245 | break; | |
246 | case SR_CONF_VDIV: | |
247 | if (cg_type == CG_NONE) { | |
248 | sr_err("No channel group specified."); | |
249 | return SR_ERR_CHANNEL_GROUP; | |
250 | } else if (cg_type == CG_ANALOG) { | |
251 | for (i = 0; i < model->analog_channels; i++) { | |
252 | if (cg != devc->analog_groups[i]) | |
253 | continue; | |
254 | *data = g_variant_new("(tt)", | |
255 | (*model->vdivs)[state->analog_channels[i].vdiv][0], | |
256 | (*model->vdivs)[state->analog_channels[i].vdiv][1]); | |
257 | ret = SR_OK; | |
258 | break; | |
259 | } | |
260 | ||
261 | } else { | |
262 | ret = SR_ERR_NA; | |
263 | } | |
264 | break; | |
265 | case SR_CONF_TRIGGER_SOURCE: | |
266 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
267 | ret = SR_OK; | |
268 | break; | |
269 | case SR_CONF_TRIGGER_SLOPE: | |
270 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
271 | ret = SR_OK; | |
272 | break; | |
273 | case SR_CONF_HORIZ_TRIGGERPOS: | |
274 | *data = g_variant_new_double(state->horiz_triggerpos); | |
275 | ret = SR_OK; | |
276 | break; | |
277 | case SR_CONF_COUPLING: | |
278 | if (cg_type == CG_NONE) { | |
279 | sr_err("No channel group specified."); | |
280 | return SR_ERR_CHANNEL_GROUP; | |
281 | } else if (cg_type == CG_ANALOG) { | |
282 | for (i = 0; i < model->analog_channels; i++) { | |
283 | if (cg != devc->analog_groups[i]) | |
284 | continue; | |
285 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]); | |
286 | ret = SR_OK; | |
287 | break; | |
288 | } | |
289 | ||
290 | } else { | |
291 | ret = SR_ERR_NA; | |
292 | } | |
293 | break; | |
294 | case SR_CONF_SAMPLERATE: | |
295 | *data = g_variant_new_uint64(state->sample_rate); | |
296 | ret = SR_OK; | |
297 | break; | |
298 | default: | |
299 | ret = SR_ERR_NA; | |
300 | } | |
301 | ||
302 | return ret; | |
303 | } | |
304 | ||
305 | static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n) | |
306 | { | |
307 | unsigned int i; | |
308 | GVariant *rational[2]; | |
309 | GVariantBuilder gvb; | |
310 | ||
311 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
312 | ||
313 | for (i = 0; i < n; i++) { | |
314 | rational[0] = g_variant_new_uint64((*array)[i][0]); | |
315 | rational[1] = g_variant_new_uint64((*array)[i][1]); | |
316 | ||
317 | /* FIXME: Valgrind reports a memory leak here. */ | |
318 | g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); | |
319 | } | |
320 | ||
321 | return g_variant_builder_end(&gvb); | |
322 | } | |
323 | ||
324 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
325 | const struct sr_channel_group *cg) | |
326 | { | |
327 | int ret, cg_type; | |
328 | unsigned int i, j; | |
329 | char command[MAX_COMMAND_SIZE], float_str[30]; | |
330 | struct dev_context *devc; | |
331 | const struct scope_config *model; | |
332 | struct scope_state *state; | |
333 | const char *tmp; | |
334 | uint64_t p, q; | |
335 | double tmp_d; | |
336 | gboolean update_sample_rate; | |
337 | ||
338 | if (!sdi || !(devc = sdi->priv)) | |
339 | return SR_ERR_ARG; | |
340 | ||
341 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
342 | return SR_ERR; | |
343 | ||
344 | model = devc->model_config; | |
345 | state = devc->model_state; | |
346 | update_sample_rate = FALSE; | |
347 | ||
348 | ret = SR_ERR_NA; | |
349 | ||
350 | switch (key) { | |
351 | case SR_CONF_LIMIT_FRAMES: | |
352 | devc->frame_limit = g_variant_get_uint64(data); | |
353 | ret = SR_OK; | |
354 | break; | |
355 | case SR_CONF_TRIGGER_SOURCE: | |
356 | tmp = g_variant_get_string(data, NULL); | |
357 | for (i = 0; (*model->trigger_sources)[i]; i++) { | |
358 | if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0) | |
359 | continue; | |
360 | state->trigger_source = i; | |
361 | g_snprintf(command, sizeof(command), | |
362 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
363 | (*model->trigger_sources)[i]); | |
364 | ||
365 | ret = sr_scpi_send(sdi->conn, command); | |
366 | break; | |
367 | } | |
368 | break; | |
369 | case SR_CONF_VDIV: | |
370 | if (cg_type == CG_NONE) { | |
371 | sr_err("No channel group specified."); | |
372 | return SR_ERR_CHANNEL_GROUP; | |
373 | } | |
374 | ||
375 | g_variant_get(data, "(tt)", &p, &q); | |
376 | ||
377 | for (i = 0; i < model->num_vdivs; i++) { | |
378 | if (p != (*model->vdivs)[i][0] || | |
379 | q != (*model->vdivs)[i][1]) | |
380 | continue; | |
381 | for (j = 1; j <= model->analog_channels; j++) { | |
382 | if (cg != devc->analog_groups[j - 1]) | |
383 | continue; | |
384 | state->analog_channels[j - 1].vdiv = i; | |
385 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); | |
386 | g_snprintf(command, sizeof(command), | |
387 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
388 | j, float_str); | |
389 | ||
390 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
391 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
392 | return SR_ERR; | |
393 | ||
394 | break; | |
395 | } | |
396 | ||
397 | ret = SR_OK; | |
398 | break; | |
399 | } | |
400 | break; | |
401 | case SR_CONF_TIMEBASE: | |
402 | g_variant_get(data, "(tt)", &p, &q); | |
403 | ||
404 | for (i = 0; i < model->num_timebases; i++) { | |
405 | if (p != (*model->timebases)[i][0] || | |
406 | q != (*model->timebases)[i][1]) | |
407 | continue; | |
408 | state->timebase = i; | |
409 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); | |
410 | g_snprintf(command, sizeof(command), | |
411 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
412 | float_str); | |
413 | ||
414 | ret = sr_scpi_send(sdi->conn, command); | |
415 | update_sample_rate = TRUE; | |
416 | break; | |
417 | } | |
418 | break; | |
419 | case SR_CONF_HORIZ_TRIGGERPOS: | |
420 | tmp_d = g_variant_get_double(data); | |
421 | ||
422 | if (tmp_d < 0.0 || tmp_d > 1.0) | |
423 | return SR_ERR; | |
424 | ||
425 | state->horiz_triggerpos = tmp_d; | |
426 | tmp_d = -(tmp_d - 0.5) * | |
427 | ((double) (*model->timebases)[state->timebase][0] / | |
428 | (*model->timebases)[state->timebase][1]) | |
429 | * model->num_xdivs; | |
430 | ||
431 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); | |
432 | g_snprintf(command, sizeof(command), | |
433 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
434 | float_str); | |
435 | ||
436 | ret = sr_scpi_send(sdi->conn, command); | |
437 | break; | |
438 | case SR_CONF_TRIGGER_SLOPE: | |
439 | tmp = g_variant_get_string(data, NULL); | |
440 | for (i = 0; (*model->trigger_slopes)[i]; i++) { | |
441 | if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0) | |
442 | continue; | |
443 | state->trigger_slope = i; | |
444 | g_snprintf(command, sizeof(command), | |
445 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
446 | (*model->trigger_slopes)[i]); | |
447 | ||
448 | ret = sr_scpi_send(sdi->conn, command); | |
449 | break; | |
450 | } | |
451 | break; | |
452 | case SR_CONF_COUPLING: | |
453 | if (cg_type == CG_NONE) { | |
454 | sr_err("No channel group specified."); | |
455 | return SR_ERR_CHANNEL_GROUP; | |
456 | } | |
457 | ||
458 | tmp = g_variant_get_string(data, NULL); | |
459 | ||
460 | for (i = 0; (*model->coupling_options)[i]; i++) { | |
461 | if (strcmp(tmp, (*model->coupling_options)[i]) != 0) | |
462 | continue; | |
463 | for (j = 1; j <= model->analog_channels; j++) { | |
464 | if (cg != devc->analog_groups[j - 1]) | |
465 | continue; | |
466 | state->analog_channels[j-1].coupling = i; | |
467 | ||
468 | g_snprintf(command, sizeof(command), | |
469 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
470 | j, tmp); | |
471 | ||
472 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
473 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
474 | return SR_ERR; | |
475 | break; | |
476 | } | |
477 | ||
478 | ret = SR_OK; | |
479 | break; | |
480 | } | |
481 | break; | |
482 | default: | |
483 | ret = SR_ERR_NA; | |
484 | break; | |
485 | } | |
486 | ||
487 | if (ret == SR_OK) | |
488 | ret = sr_scpi_get_opc(sdi->conn); | |
489 | ||
490 | if (ret == SR_OK && update_sample_rate) | |
491 | ret = hmo_update_sample_rate(sdi); | |
492 | ||
493 | return ret; | |
494 | } | |
495 | ||
496 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
497 | const struct sr_channel_group *cg) | |
498 | { | |
499 | int cg_type = CG_NONE; | |
500 | struct dev_context *devc = NULL; | |
501 | const struct scope_config *model = NULL; | |
502 | ||
503 | if (sdi && (devc = sdi->priv)) { | |
504 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
505 | return SR_ERR; | |
506 | ||
507 | model = devc->model_config; | |
508 | } | |
509 | ||
510 | switch (key) { | |
511 | case SR_CONF_SCAN_OPTIONS: | |
512 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
513 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
514 | break; | |
515 | case SR_CONF_DEVICE_OPTIONS: | |
516 | if (cg_type == CG_NONE) { | |
517 | if (model) | |
518 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
519 | model->devopts, model->num_devopts, sizeof(uint32_t)); | |
520 | else | |
521 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
522 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
523 | } else if (cg_type == CG_ANALOG) { | |
524 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
525 | model->analog_devopts, model->num_analog_devopts, | |
526 | sizeof(uint32_t)); | |
527 | } else { | |
528 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
529 | NULL, 0, sizeof(uint32_t)); | |
530 | } | |
531 | break; | |
532 | case SR_CONF_COUPLING: | |
533 | if (cg_type == CG_NONE) | |
534 | return SR_ERR_CHANNEL_GROUP; | |
535 | *data = g_variant_new_strv(*model->coupling_options, | |
536 | g_strv_length((char **)*model->coupling_options)); | |
537 | break; | |
538 | case SR_CONF_TRIGGER_SOURCE: | |
539 | if (!model) | |
540 | return SR_ERR_ARG; | |
541 | *data = g_variant_new_strv(*model->trigger_sources, | |
542 | g_strv_length((char **)*model->trigger_sources)); | |
543 | break; | |
544 | case SR_CONF_TRIGGER_SLOPE: | |
545 | if (!model) | |
546 | return SR_ERR_ARG; | |
547 | *data = g_variant_new_strv(*model->trigger_slopes, | |
548 | g_strv_length((char **)*model->trigger_slopes)); | |
549 | break; | |
550 | case SR_CONF_TIMEBASE: | |
551 | if (!model) | |
552 | return SR_ERR_ARG; | |
553 | *data = build_tuples(model->timebases, model->num_timebases); | |
554 | break; | |
555 | case SR_CONF_VDIV: | |
556 | if (cg_type == CG_NONE) | |
557 | return SR_ERR_CHANNEL_GROUP; | |
558 | *data = build_tuples(model->vdivs, model->num_vdivs); | |
559 | break; | |
560 | default: | |
561 | return SR_ERR_NA; | |
562 | } | |
563 | ||
564 | return SR_OK; | |
565 | } | |
566 | ||
567 | SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi) | |
568 | { | |
569 | char command[MAX_COMMAND_SIZE]; | |
570 | struct sr_channel *ch; | |
571 | struct dev_context *devc; | |
572 | const struct scope_config *model; | |
573 | ||
574 | devc = sdi->priv; | |
575 | model = devc->model_config; | |
576 | ||
577 | ch = devc->current_channel->data; | |
578 | ||
579 | switch (ch->type) { | |
580 | case SR_CHANNEL_ANALOG: | |
581 | g_snprintf(command, sizeof(command), | |
582 | (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA], | |
583 | ch->index + 1); | |
584 | break; | |
585 | case SR_CHANNEL_LOGIC: | |
586 | g_snprintf(command, sizeof(command), | |
587 | (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA], | |
588 | ch->index < 8 ? 1 : 2); | |
589 | break; | |
590 | default: | |
591 | sr_err("Invalid channel type."); | |
592 | break; | |
593 | } | |
594 | ||
595 | return sr_scpi_send(sdi->conn, command); | |
596 | } | |
597 | ||
598 | static int hmo_check_channels(GSList *channels) | |
599 | { | |
600 | GSList *l; | |
601 | struct sr_channel *ch; | |
602 | gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4; | |
603 | ||
604 | enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE; | |
605 | ||
606 | for (l = channels; l; l = l->next) { | |
607 | ch = l->data; | |
608 | switch (ch->type) { | |
609 | case SR_CHANNEL_ANALOG: | |
610 | if (ch->index == 2) | |
611 | enabled_chan3 = TRUE; | |
612 | else if (ch->index == 3) | |
613 | enabled_chan4 = TRUE; | |
614 | break; | |
615 | case SR_CHANNEL_LOGIC: | |
616 | if (ch->index < 8) | |
617 | enabled_pod1 = TRUE; | |
618 | else | |
619 | enabled_pod2 = TRUE; | |
620 | break; | |
621 | default: | |
622 | return SR_ERR; | |
623 | } | |
624 | } | |
625 | ||
626 | if ((enabled_pod1 && enabled_chan3) || | |
627 | (enabled_pod2 && enabled_chan4)) | |
628 | return SR_ERR; | |
629 | ||
630 | return SR_OK; | |
631 | } | |
632 | ||
633 | static int hmo_setup_channels(const struct sr_dev_inst *sdi) | |
634 | { | |
635 | GSList *l; | |
636 | unsigned int i; | |
637 | gboolean *pod_enabled, setup_changed; | |
638 | char command[MAX_COMMAND_SIZE]; | |
639 | struct scope_state *state; | |
640 | const struct scope_config *model; | |
641 | struct sr_channel *ch; | |
642 | struct dev_context *devc; | |
643 | struct sr_scpi_dev_inst *scpi; | |
644 | ||
645 | devc = sdi->priv; | |
646 | scpi = sdi->conn; | |
647 | state = devc->model_state; | |
648 | model = devc->model_config; | |
649 | setup_changed = FALSE; | |
650 | ||
651 | pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods); | |
652 | ||
653 | for (l = sdi->channels; l; l = l->next) { | |
654 | ch = l->data; | |
655 | switch (ch->type) { | |
656 | case SR_CHANNEL_ANALOG: | |
657 | if (ch->enabled == state->analog_channels[ch->index].state) | |
658 | break; | |
659 | g_snprintf(command, sizeof(command), | |
660 | (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE], | |
661 | ch->index + 1, ch->enabled); | |
662 | ||
663 | if (sr_scpi_send(scpi, command) != SR_OK) | |
664 | return SR_ERR; | |
665 | state->analog_channels[ch->index].state = ch->enabled; | |
666 | setup_changed = TRUE; | |
667 | break; | |
668 | case SR_CHANNEL_LOGIC: | |
669 | /* | |
670 | * A digital POD needs to be enabled for every group of | |
671 | * 8 channels. | |
672 | */ | |
673 | if (ch->enabled) | |
674 | pod_enabled[ch->index < 8 ? 0 : 1] = TRUE; | |
675 | ||
676 | if (ch->enabled == state->digital_channels[ch->index]) | |
677 | break; | |
678 | g_snprintf(command, sizeof(command), | |
679 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE], | |
680 | ch->index, ch->enabled); | |
681 | ||
682 | if (sr_scpi_send(scpi, command) != SR_OK) | |
683 | return SR_ERR; | |
684 | ||
685 | state->digital_channels[ch->index] = ch->enabled; | |
686 | setup_changed = TRUE; | |
687 | break; | |
688 | default: | |
689 | return SR_ERR; | |
690 | } | |
691 | } | |
692 | ||
693 | for (i = 1; i <= model->digital_pods; i++) { | |
694 | if (state->digital_pods[i - 1] == pod_enabled[i - 1]) | |
695 | continue; | |
696 | g_snprintf(command, sizeof(command), | |
697 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE], | |
698 | i, pod_enabled[i - 1]); | |
699 | if (sr_scpi_send(scpi, command) != SR_OK) | |
700 | return SR_ERR; | |
701 | state->digital_pods[i - 1] = pod_enabled[i - 1]; | |
702 | setup_changed = TRUE; | |
703 | } | |
704 | ||
705 | g_free(pod_enabled); | |
706 | ||
707 | if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK) | |
708 | return SR_ERR; | |
709 | ||
710 | return SR_OK; | |
711 | } | |
712 | ||
713 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
714 | { | |
715 | GSList *l; | |
716 | gboolean digital_added; | |
717 | struct sr_channel *ch; | |
718 | struct dev_context *devc; | |
719 | struct sr_scpi_dev_inst *scpi; | |
720 | ||
721 | if (sdi->status != SR_ST_ACTIVE) | |
722 | return SR_ERR_DEV_CLOSED; | |
723 | ||
724 | scpi = sdi->conn; | |
725 | devc = sdi->priv; | |
726 | digital_added = FALSE; | |
727 | ||
728 | g_slist_free(devc->enabled_channels); | |
729 | devc->enabled_channels = NULL; | |
730 | ||
731 | for (l = sdi->channels; l; l = l->next) { | |
732 | ch = l->data; | |
733 | if (!ch->enabled) | |
734 | continue; | |
735 | /* Only add a single digital channel. */ | |
736 | if (ch->type != SR_CHANNEL_LOGIC || !digital_added) { | |
737 | devc->enabled_channels = g_slist_append( | |
738 | devc->enabled_channels, ch); | |
739 | if (ch->type == SR_CHANNEL_LOGIC) | |
740 | digital_added = TRUE; | |
741 | } | |
742 | } | |
743 | ||
744 | if (!devc->enabled_channels) | |
745 | return SR_ERR; | |
746 | ||
747 | if (hmo_check_channels(devc->enabled_channels) != SR_OK) { | |
748 | sr_err("Invalid channel configuration specified!"); | |
749 | return SR_ERR_NA; | |
750 | } | |
751 | ||
752 | if (hmo_setup_channels(sdi) != SR_OK) { | |
753 | sr_err("Failed to setup channel configuration!"); | |
754 | return SR_ERR; | |
755 | } | |
756 | ||
757 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, | |
758 | hmo_receive_data, (void *)sdi); | |
759 | ||
760 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
761 | ||
762 | devc->current_channel = devc->enabled_channels; | |
763 | ||
764 | return hmo_request_data(sdi); | |
765 | } | |
766 | ||
767 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
768 | { | |
769 | struct dev_context *devc; | |
770 | struct sr_scpi_dev_inst *scpi; | |
771 | ||
772 | (void)cb_data; | |
773 | ||
774 | std_session_send_df_end(sdi, LOG_PREFIX); | |
775 | ||
776 | if (sdi->status != SR_ST_ACTIVE) | |
777 | return SR_ERR_DEV_CLOSED; | |
778 | ||
779 | devc = sdi->priv; | |
780 | ||
781 | devc->num_frames = 0; | |
782 | g_slist_free(devc->enabled_channels); | |
783 | devc->enabled_channels = NULL; | |
784 | scpi = sdi->conn; | |
785 | sr_scpi_source_remove(sdi->session, scpi); | |
786 | ||
787 | return SR_OK; | |
788 | } | |
789 | ||
790 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = { | |
791 | .name = "hameg-hmo", | |
792 | .longname = "Hameg HMO", | |
793 | .api_version = 1, | |
794 | .init = init, | |
795 | .cleanup = cleanup, | |
796 | .scan = scan, | |
797 | .dev_list = dev_list, | |
798 | .dev_clear = dev_clear, | |
799 | .config_get = config_get, | |
800 | .config_set = config_set, | |
801 | .config_list = config_list, | |
802 | .dev_open = dev_open, | |
803 | .dev_close = dev_close, | |
804 | .dev_acquisition_start = dev_acquisition_start, | |
805 | .dev_acquisition_stop = dev_acquisition_stop, | |
806 | .context = NULL, | |
807 | }; |