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