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