]>
Commit | Line | Data |
---|---|---|
10763937 SA |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net> | |
5 | * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <stdlib.h> | |
22 | #include "protocol.h" | |
23 | ||
8ab929d6 SA |
24 | SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info; |
25 | static struct sr_dev_driver *di = &yokogawa_dlm_driver_info; | |
26 | ||
27 | static char *MANUFACTURER_ID = "YOKOGAWA"; | |
28 | static char *MANUFACTURER_NAME = "Yokogawa"; | |
29 | ||
cf0280fa AJ |
30 | static const uint32_t drvopts[] = { |
31 | SR_CONF_LOGIC_ANALYZER, | |
32 | SR_CONF_OSCILLOSCOPE, | |
33 | }; | |
34 | ||
8ab929d6 SA |
35 | enum { |
36 | CG_INVALID = -1, | |
37 | CG_NONE, | |
38 | CG_ANALOG, | |
39 | CG_DIGITAL, | |
40 | }; | |
41 | ||
42 | static int init(struct sr_context *sr_ctx) | |
43 | { | |
44 | return std_init(sr_ctx, di, LOG_PREFIX); | |
45 | } | |
46 | ||
47 | static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi) | |
48 | { | |
49 | struct sr_dev_inst *sdi; | |
50 | struct dev_context *devc; | |
51 | struct sr_scpi_hw_info *hw_info; | |
52 | char *model_name; | |
53 | int model_index; | |
54 | ||
55 | sdi = NULL; | |
56 | devc = NULL; | |
57 | hw_info = NULL; | |
58 | ||
59 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { | |
60 | sr_info("Couldn't get IDN response."); | |
61 | goto fail; | |
62 | } | |
63 | ||
64 | if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0) | |
65 | goto fail; | |
66 | ||
67 | if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK) | |
68 | goto fail; | |
69 | ||
aac29cc1 | 70 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
71 | sdi->status = SR_ST_ACTIVE; |
72 | sdi->vendor = g_strdup(MANUFACTURER_NAME); | |
73 | sdi->model = g_strdup(model_name); | |
74 | sdi->version = g_strdup(hw_info->firmware_version); | |
8ab929d6 | 75 | |
d1314831 SA |
76 | sdi->serial_num = g_strdup(hw_info->serial_number); |
77 | ||
8ab929d6 SA |
78 | sr_scpi_hw_info_free(hw_info); |
79 | hw_info = NULL; | |
80 | ||
ac10a927 | 81 | devc = g_malloc0(sizeof(struct dev_context)); |
8ab929d6 SA |
82 | |
83 | sdi->driver = di; | |
84 | sdi->priv = devc; | |
85 | sdi->inst_type = SR_INST_SCPI; | |
86 | sdi->conn = scpi; | |
87 | ||
88 | if (dlm_device_init(sdi, model_index) != SR_OK) | |
89 | goto fail; | |
90 | ||
91 | sr_scpi_close(sdi->conn); | |
92 | ||
93 | sdi->status = SR_ST_INACTIVE; | |
94 | return sdi; | |
95 | ||
96 | fail: | |
97 | if (hw_info) | |
98 | sr_scpi_hw_info_free(hw_info); | |
99 | if (sdi) | |
100 | sr_dev_inst_free(sdi); | |
101 | if (devc) | |
102 | g_free(devc); | |
103 | ||
104 | return NULL; | |
105 | } | |
106 | ||
107 | static GSList *scan(GSList *options) | |
108 | { | |
109 | return sr_scpi_scan(di->priv, options, probe_usbtmc_device); | |
110 | } | |
111 | ||
112 | static GSList *dev_list(void) | |
113 | { | |
114 | return ((struct drv_context *)(di->priv))->instances; | |
115 | } | |
116 | ||
117 | static void clear_helper(void *priv) | |
118 | { | |
119 | struct dev_context *devc; | |
120 | ||
121 | devc = priv; | |
122 | ||
123 | dlm_scope_state_destroy(devc->model_state); | |
124 | ||
125 | g_free(devc->analog_groups); | |
126 | g_free(devc->digital_groups); | |
127 | g_free(devc); | |
128 | } | |
129 | ||
130 | static int dev_clear(void) | |
131 | { | |
132 | return std_dev_clear(di, clear_helper); | |
133 | } | |
134 | ||
135 | static int dev_open(struct sr_dev_inst *sdi) | |
136 | { | |
137 | if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) | |
138 | return SR_ERR; | |
139 | ||
140 | if (dlm_scope_state_query(sdi) != SR_OK) | |
141 | return SR_ERR; | |
142 | ||
143 | sdi->status = SR_ST_ACTIVE; | |
144 | ||
145 | return SR_OK; | |
146 | } | |
147 | ||
148 | static int dev_close(struct sr_dev_inst *sdi) | |
149 | { | |
150 | if (sdi->status == SR_ST_INACTIVE) | |
151 | return SR_OK; | |
152 | ||
153 | sr_scpi_close(sdi->conn); | |
154 | ||
155 | sdi->status = SR_ST_INACTIVE; | |
156 | ||
157 | return SR_OK; | |
158 | } | |
159 | ||
160 | static int cleanup(void) | |
161 | { | |
162 | dev_clear(); | |
163 | ||
164 | return SR_OK; | |
165 | } | |
166 | ||
167 | /** | |
168 | * Check which category a given channel group belongs to. | |
169 | * | |
170 | * @param devc Our internal device context. | |
171 | * @param cg The channel group to check. | |
172 | * | |
173 | * @retval CG_NONE cg is NULL | |
174 | * @retval CG_ANALOG cg is an analog group | |
175 | * @retval CG_DIGITAL cg is a digital group | |
176 | * @retval CG_INVALID cg is something else | |
177 | */ | |
178 | static int check_channel_group(struct dev_context *devc, | |
ac10a927 | 179 | const struct sr_channel_group *cg) |
8ab929d6 SA |
180 | { |
181 | unsigned int i; | |
182 | struct scope_config *model; | |
183 | ||
184 | model = devc->model_config; | |
185 | ||
186 | if (!cg) | |
187 | return CG_NONE; | |
188 | ||
189 | for (i = 0; i < model->analog_channels; ++i) | |
190 | if (cg == devc->analog_groups[i]) | |
191 | return CG_ANALOG; | |
192 | ||
193 | for (i = 0; i < model->pods; ++i) | |
194 | if (cg == devc->digital_groups[i]) | |
195 | return CG_DIGITAL; | |
196 | ||
197 | sr_err("Invalid channel group specified."); | |
198 | return CG_INVALID; | |
199 | } | |
200 | ||
584560f1 | 201 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
ac10a927 | 202 | const struct sr_channel_group *cg) |
8ab929d6 SA |
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) { | |
bf622e6d | 221 | case SR_CONF_NUM_HDIV: |
8ab929d6 SA |
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)", | |
ac10a927 SA |
227 | (*model->timebases)[state->timebase][0], |
228 | (*model->timebases)[state->timebase][1]); | |
8ab929d6 SA |
229 | ret = SR_OK; |
230 | break; | |
231 | case SR_CONF_NUM_VDIV: | |
232 | if (cg_type == CG_NONE) { | |
233 | sr_err("No channel group specified."); | |
234 | return SR_ERR_CHANNEL_GROUP; | |
235 | } else if (cg_type == CG_ANALOG) { | |
236 | *data = g_variant_new_int32(model->num_ydivs); | |
237 | ret = SR_OK; | |
238 | break; | |
239 | } else { | |
240 | ret = SR_ERR_NA; | |
241 | } | |
242 | break; | |
243 | case SR_CONF_VDIV: | |
244 | ret = SR_ERR_NA; | |
245 | if (cg_type == CG_NONE) { | |
246 | sr_err("No channel group specified."); | |
247 | return SR_ERR_CHANNEL_GROUP; | |
248 | } else if (cg_type != CG_ANALOG) | |
249 | break; | |
250 | ||
251 | for (i = 0; i < model->analog_channels; ++i) { | |
252 | if (cg != devc->analog_groups[i]) | |
253 | continue; | |
254 | *data = g_variant_new("(tt)", | |
ac10a927 SA |
255 | (*model->vdivs)[state->analog_states[i].vdiv][0], |
256 | (*model->vdivs)[state->analog_states[i].vdiv][1]); | |
8ab929d6 SA |
257 | ret = SR_OK; |
258 | break; | |
259 | } | |
260 | break; | |
261 | case SR_CONF_TRIGGER_SOURCE: | |
262 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
263 | ret = SR_OK; | |
264 | break; | |
265 | case SR_CONF_TRIGGER_SLOPE: | |
266 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
267 | ret = SR_OK; | |
268 | break; | |
269 | case SR_CONF_HORIZ_TRIGGERPOS: | |
270 | *data = g_variant_new_double(state->horiz_triggerpos); | |
271 | ret = SR_OK; | |
272 | break; | |
273 | case SR_CONF_COUPLING: | |
274 | ret = SR_ERR_NA; | |
275 | if (cg_type == CG_NONE) { | |
276 | sr_err("No channel group specified."); | |
277 | return SR_ERR_CHANNEL_GROUP; | |
278 | } else if (cg_type != CG_ANALOG) | |
279 | break; | |
280 | ||
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_states[i].coupling]); | |
285 | ret = SR_OK; | |
286 | break; | |
287 | } | |
288 | break; | |
289 | case SR_CONF_SAMPLERATE: | |
290 | *data = g_variant_new_uint64(state->sample_rate); | |
291 | ret = SR_OK; | |
292 | break; | |
293 | default: | |
294 | ret = SR_ERR_NA; | |
295 | } | |
296 | ||
297 | return ret; | |
298 | } | |
299 | ||
300 | static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n) | |
301 | { | |
302 | unsigned int i; | |
303 | GVariant *rational[2]; | |
304 | GVariantBuilder gvb; | |
305 | ||
306 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
307 | ||
308 | for (i = 0; i < n; i++) { | |
309 | rational[0] = g_variant_new_uint64((*array)[i][0]); | |
310 | rational[1] = g_variant_new_uint64((*array)[i][1]); | |
311 | ||
312 | /* FIXME: Valgrind reports a memory leak here. */ | |
313 | g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); | |
314 | } | |
315 | ||
316 | return g_variant_builder_end(&gvb); | |
317 | } | |
318 | ||
584560f1 | 319 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
ac10a927 | 320 | const struct sr_channel_group *cg) |
8ab929d6 SA |
321 | { |
322 | int ret, cg_type; | |
323 | unsigned int i, j; | |
324 | char float_str[30]; | |
325 | struct dev_context *devc; | |
326 | struct scope_config *model; | |
327 | struct scope_state *state; | |
328 | const char *tmp; | |
329 | uint64_t p, q; | |
330 | double tmp_d; | |
331 | gboolean update_sample_rate; | |
332 | ||
333 | if (!sdi || !(devc = sdi->priv)) | |
334 | return SR_ERR_ARG; | |
335 | ||
336 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
337 | return SR_ERR; | |
338 | ||
339 | model = devc->model_config; | |
340 | state = devc->model_state; | |
341 | update_sample_rate = FALSE; | |
342 | ||
343 | ret = SR_ERR_NA; | |
344 | ||
345 | switch (key) { | |
346 | case SR_CONF_LIMIT_FRAMES: | |
347 | devc->frame_limit = g_variant_get_uint64(data); | |
348 | ret = SR_OK; | |
349 | break; | |
350 | case SR_CONF_TRIGGER_SOURCE: | |
351 | tmp = g_variant_get_string(data, NULL); | |
352 | for (i = 0; (*model->trigger_sources)[i]; i++) { | |
353 | if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0) | |
354 | continue; | |
355 | state->trigger_source = i; | |
356 | /* TODO: A and B trigger support possible? */ | |
357 | ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]); | |
358 | break; | |
359 | } | |
360 | break; | |
361 | case SR_CONF_VDIV: | |
362 | if (cg_type == CG_NONE) { | |
363 | sr_err("No channel group specified."); | |
364 | return SR_ERR_CHANNEL_GROUP; | |
365 | } | |
366 | ||
367 | g_variant_get(data, "(tt)", &p, &q); | |
368 | ||
369 | for (i = 0; i < model->num_vdivs; i++) { | |
370 | if (p != (*model->vdivs)[i][0] || | |
ac10a927 | 371 | q != (*model->vdivs)[i][1]) |
8ab929d6 SA |
372 | continue; |
373 | for (j = 1; j <= model->analog_channels; ++j) { | |
374 | if (cg != devc->analog_groups[j - 1]) | |
375 | continue; | |
376 | state->analog_states[j - 1].vdiv = i; | |
377 | g_ascii_formatd(float_str, sizeof(float_str), | |
378 | "%E", (float) p / q); | |
379 | if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK || | |
ac10a927 | 380 | sr_scpi_get_opc(sdi->conn) != SR_OK) |
8ab929d6 SA |
381 | return SR_ERR; |
382 | ||
383 | break; | |
384 | } | |
385 | ||
386 | ret = SR_OK; | |
387 | break; | |
388 | } | |
389 | break; | |
390 | case SR_CONF_TIMEBASE: | |
391 | g_variant_get(data, "(tt)", &p, &q); | |
392 | ||
393 | for (i = 0; i < model->num_timebases; i++) { | |
394 | if (p != (*model->timebases)[i][0] || | |
ac10a927 | 395 | q != (*model->timebases)[i][1]) |
8ab929d6 SA |
396 | continue; |
397 | state->timebase = i; | |
398 | g_ascii_formatd(float_str, sizeof(float_str), | |
399 | "%E", (float) p / q); | |
400 | ret = dlm_timebase_set(sdi->conn, float_str); | |
401 | update_sample_rate = TRUE; | |
402 | break; | |
403 | } | |
404 | break; | |
405 | case SR_CONF_HORIZ_TRIGGERPOS: | |
406 | tmp_d = g_variant_get_double(data); | |
407 | ||
408 | /* TODO: Check if the calculation makes sense for the DLM. */ | |
409 | if (tmp_d < 0.0 || tmp_d > 1.0) | |
410 | return SR_ERR; | |
411 | ||
412 | state->horiz_triggerpos = tmp_d; | |
413 | tmp_d = -(tmp_d - 0.5) * | |
ac10a927 SA |
414 | ((double) (*model->timebases)[state->timebase][0] / |
415 | (*model->timebases)[state->timebase][1]) | |
416 | * model->num_xdivs; | |
8ab929d6 SA |
417 | |
418 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); | |
419 | ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str); | |
420 | break; | |
421 | case SR_CONF_TRIGGER_SLOPE: | |
422 | tmp = g_variant_get_string(data, NULL); | |
423 | ||
424 | if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r')) | |
425 | return SR_ERR_ARG; | |
426 | ||
427 | /* Note: See dlm_trigger_slopes[] in protocol.c. */ | |
428 | state->trigger_slope = (tmp[0] == 'r') ? | |
429 | SLOPE_POSITIVE : SLOPE_NEGATIVE; | |
430 | ||
431 | ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope); | |
432 | break; | |
433 | case SR_CONF_COUPLING: | |
434 | if (cg_type == CG_NONE) { | |
435 | sr_err("No channel group specified."); | |
436 | return SR_ERR_CHANNEL_GROUP; | |
437 | } | |
438 | ||
439 | tmp = g_variant_get_string(data, NULL); | |
440 | ||
441 | for (i = 0; (*model->coupling_options)[i]; i++) { | |
442 | if (strcmp(tmp, (*model->coupling_options)[i]) != 0) | |
443 | continue; | |
444 | for (j = 1; j <= model->analog_channels; ++j) { | |
445 | if (cg != devc->analog_groups[j - 1]) | |
446 | continue; | |
447 | state->analog_states[j-1].coupling = i; | |
448 | ||
449 | if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK || | |
ac10a927 | 450 | sr_scpi_get_opc(sdi->conn) != SR_OK) |
8ab929d6 SA |
451 | return SR_ERR; |
452 | break; | |
453 | } | |
454 | ||
455 | ret = SR_OK; | |
456 | break; | |
457 | } | |
458 | break; | |
459 | default: | |
460 | ret = SR_ERR_NA; | |
461 | break; | |
462 | } | |
463 | ||
464 | if (ret == SR_OK) | |
465 | ret = sr_scpi_get_opc(sdi->conn); | |
466 | ||
467 | if (ret == SR_OK && update_sample_rate) | |
468 | ret = dlm_sample_rate_query(sdi); | |
469 | ||
470 | return ret; | |
471 | } | |
472 | ||
584560f1 | 473 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
ac10a927 | 474 | const struct sr_channel_group *cg) |
8ab929d6 | 475 | { |
cf0280fa AJ |
476 | int cg_type = CG_NONE; |
477 | struct dev_context *devc = NULL; | |
478 | struct scope_config *model = NULL; | |
8ab929d6 | 479 | |
cf0280fa AJ |
480 | if (sdi && (devc = sdi->priv)) { |
481 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) | |
482 | return SR_ERR; | |
8ab929d6 | 483 | |
cf0280fa AJ |
484 | model = devc->model_config; |
485 | } | |
8ab929d6 SA |
486 | |
487 | switch (key) { | |
8ab929d6 SA |
488 | case SR_CONF_DEVICE_OPTIONS: |
489 | if (cg_type == CG_NONE) { | |
cf0280fa AJ |
490 | if (model) |
491 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
492 | model->devopts, model->num_devopts, sizeof(uint32_t)); | |
493 | else | |
494 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
495 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
8ab929d6 | 496 | } else if (cg_type == CG_ANALOG) { |
584560f1 | 497 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
af945a66 | 498 | model->analog_devopts, model->num_analog_devopts, sizeof(uint32_t)); |
8ab929d6 | 499 | } else { |
584560f1 BV |
500 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
501 | NULL, 0, sizeof(uint32_t)); | |
8ab929d6 SA |
502 | } |
503 | break; | |
504 | case SR_CONF_COUPLING: | |
505 | if (cg_type == CG_NONE) | |
506 | return SR_ERR_CHANNEL_GROUP; | |
507 | *data = g_variant_new_strv(*model->coupling_options, | |
ac10a927 | 508 | g_strv_length((char **)*model->coupling_options)); |
8ab929d6 SA |
509 | break; |
510 | case SR_CONF_TRIGGER_SOURCE: | |
cf0280fa AJ |
511 | if (!model) |
512 | return SR_ERR_ARG; | |
8ab929d6 | 513 | *data = g_variant_new_strv(*model->trigger_sources, |
ac10a927 | 514 | g_strv_length((char **)*model->trigger_sources)); |
8ab929d6 SA |
515 | break; |
516 | case SR_CONF_TRIGGER_SLOPE: | |
cf0280fa AJ |
517 | if (!model) |
518 | return SR_ERR_ARG; | |
8ab929d6 | 519 | *data = g_variant_new_strv(*model->trigger_slopes, |
ac10a927 | 520 | g_strv_length((char **)*model->trigger_slopes)); |
8ab929d6 SA |
521 | break; |
522 | case SR_CONF_TIMEBASE: | |
cf0280fa AJ |
523 | if (!model) |
524 | return SR_ERR_ARG; | |
8ab929d6 SA |
525 | *data = build_tuples(model->timebases, model->num_timebases); |
526 | break; | |
527 | case SR_CONF_VDIV: | |
528 | if (cg_type == CG_NONE) | |
529 | return SR_ERR_CHANNEL_GROUP; | |
530 | *data = build_tuples(model->vdivs, model->num_vdivs); | |
531 | break; | |
532 | default: | |
533 | return SR_ERR_NA; | |
534 | } | |
535 | ||
536 | return SR_OK; | |
537 | } | |
538 | ||
539 | static int dlm_check_channels(GSList *channels) | |
540 | { | |
541 | GSList *l; | |
542 | struct sr_channel *ch; | |
543 | gboolean enabled_pod1, enabled_chan4; | |
544 | ||
545 | enabled_pod1 = enabled_chan4 = FALSE; | |
546 | ||
547 | /* Note: On the DLM2000, CH4 and Logic are shared. */ | |
548 | /* TODO Handle non-DLM2000 models. */ | |
549 | for (l = channels; l; l = l->next) { | |
550 | ch = l->data; | |
551 | switch (ch->type) { | |
552 | case SR_CHANNEL_ANALOG: | |
553 | if (ch->index == 3) | |
554 | enabled_chan4 = TRUE; | |
555 | break; | |
556 | case SR_CHANNEL_LOGIC: | |
557 | enabled_pod1 = TRUE; | |
558 | break; | |
559 | default: | |
560 | return SR_ERR; | |
561 | } | |
562 | } | |
563 | ||
564 | if (enabled_pod1 && enabled_chan4) | |
565 | return SR_ERR; | |
566 | ||
567 | return SR_OK; | |
568 | } | |
569 | ||
570 | static int dlm_setup_channels(const struct sr_dev_inst *sdi) | |
571 | { | |
572 | GSList *l; | |
573 | unsigned int i; | |
574 | gboolean *pod_enabled, setup_changed; | |
575 | struct scope_state *state; | |
576 | struct scope_config *model; | |
577 | struct sr_channel *ch; | |
578 | struct dev_context *devc; | |
579 | struct sr_scpi_dev_inst *scpi; | |
580 | ||
581 | devc = sdi->priv; | |
582 | scpi = sdi->conn; | |
583 | state = devc->model_state; | |
584 | model = devc->model_config; | |
585 | setup_changed = FALSE; | |
586 | ||
ac10a927 | 587 | pod_enabled = g_malloc0(sizeof(gboolean) * model->pods); |
8ab929d6 SA |
588 | |
589 | for (l = sdi->channels; l; l = l->next) { | |
590 | ch = l->data; | |
591 | switch (ch->type) { | |
592 | case SR_CHANNEL_ANALOG: | |
593 | if (ch->enabled == state->analog_states[ch->index].state) | |
594 | break; | |
595 | ||
596 | if (dlm_analog_chan_state_set(scpi, ch->index + 1, | |
ac10a927 | 597 | ch->enabled) != SR_OK) |
8ab929d6 SA |
598 | return SR_ERR; |
599 | ||
600 | state->analog_states[ch->index].state = ch->enabled; | |
601 | setup_changed = TRUE; | |
602 | break; | |
603 | case SR_CHANNEL_LOGIC: | |
604 | if (ch->enabled) | |
605 | pod_enabled[ch->index / 8] = TRUE; | |
606 | ||
607 | if (ch->enabled == state->digital_states[ch->index]) | |
608 | break; | |
609 | ||
610 | if (dlm_digital_chan_state_set(scpi, ch->index + 1, | |
ac10a927 | 611 | ch->enabled) != SR_OK) |
8ab929d6 SA |
612 | return SR_ERR; |
613 | ||
614 | state->digital_states[ch->index] = ch->enabled; | |
615 | setup_changed = TRUE; | |
616 | break; | |
617 | default: | |
618 | return SR_ERR; | |
619 | } | |
620 | } | |
621 | ||
622 | for (i = 1; i <= model->pods; ++i) { | |
623 | if (state->pod_states[i - 1] == pod_enabled[i - 1]) | |
624 | continue; | |
625 | ||
626 | if (dlm_digital_pod_state_set(scpi, i, | |
ac10a927 | 627 | pod_enabled[i - 1]) != SR_OK) |
8ab929d6 SA |
628 | return SR_ERR; |
629 | ||
630 | state->pod_states[i - 1] = pod_enabled[i - 1]; | |
631 | setup_changed = TRUE; | |
632 | } | |
633 | ||
634 | g_free(pod_enabled); | |
635 | ||
636 | if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK) | |
637 | return SR_ERR; | |
638 | ||
639 | return SR_OK; | |
640 | } | |
641 | ||
642 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
643 | { | |
644 | GSList *l; | |
645 | gboolean digital_added; | |
646 | struct sr_channel *ch; | |
647 | struct dev_context *devc; | |
648 | struct sr_scpi_dev_inst *scpi; | |
649 | ||
650 | (void)cb_data; | |
651 | ||
ac10a927 SA |
652 | if (sdi->status != SR_ST_ACTIVE) |
653 | return SR_ERR_DEV_CLOSED; | |
8ab929d6 SA |
654 | |
655 | scpi = sdi->conn; | |
656 | devc = sdi->priv; | |
657 | digital_added = FALSE; | |
658 | ||
659 | g_slist_free(devc->enabled_channels); | |
660 | devc->enabled_channels = NULL; | |
661 | ||
662 | for (l = sdi->channels; l; l = l->next) { | |
663 | ch = l->data; | |
664 | if (!ch->enabled) | |
665 | continue; | |
666 | /* Only add a single digital channel. */ | |
667 | if (ch->type != SR_CHANNEL_LOGIC || !digital_added) { | |
668 | devc->enabled_channels = g_slist_append( | |
ac10a927 SA |
669 | devc->enabled_channels, ch); |
670 | if (ch->type == SR_CHANNEL_LOGIC) | |
671 | digital_added = TRUE; | |
8ab929d6 SA |
672 | } |
673 | } | |
674 | ||
675 | if (!devc->enabled_channels) | |
676 | return SR_ERR; | |
677 | ||
678 | if (dlm_check_channels(devc->enabled_channels) != SR_OK) { | |
679 | sr_err("Invalid channel configuration specified!"); | |
680 | return SR_ERR_NA; | |
681 | } | |
682 | ||
683 | if (dlm_setup_channels(sdi) != SR_OK) { | |
684 | sr_err("Failed to setup channel configuration!"); | |
685 | return SR_ERR; | |
686 | } | |
687 | ||
af3487ec SA |
688 | /* Request data for the first enabled channel. */ |
689 | devc->current_channel = devc->enabled_channels; | |
690 | dlm_channel_data_request(sdi); | |
691 | ||
0028d5a1 SA |
692 | /* Call our callback when data comes in or after 5ms. */ |
693 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5, | |
8ab929d6 SA |
694 | dlm_data_receive, (void *)sdi); |
695 | ||
696 | return SR_OK; | |
697 | } | |
698 | ||
699 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
700 | { | |
701 | struct dev_context *devc; | |
8ab929d6 SA |
702 | struct sr_datafeed_packet packet; |
703 | ||
704 | (void)cb_data; | |
705 | ||
706 | packet.type = SR_DF_END; | |
707 | packet.payload = NULL; | |
708 | sr_session_send(sdi, &packet); | |
709 | ||
710 | if (sdi->status != SR_ST_ACTIVE) | |
711 | return SR_ERR_DEV_CLOSED; | |
712 | ||
713 | devc = sdi->priv; | |
714 | ||
715 | devc->num_frames = 0; | |
716 | g_slist_free(devc->enabled_channels); | |
717 | devc->enabled_channels = NULL; | |
af3487ec SA |
718 | |
719 | sr_scpi_source_remove(sdi->session, sdi->conn); | |
8ab929d6 SA |
720 | |
721 | return SR_OK; | |
722 | } | |
10763937 SA |
723 | |
724 | SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = { | |
725 | .name = "yokogawa-dlm", | |
ac10a927 | 726 | .longname = "Yokogawa DL/DLM", |
10763937 | 727 | .api_version = 1, |
8ab929d6 SA |
728 | .init = init, |
729 | .cleanup = cleanup, | |
730 | .scan = scan, | |
731 | .dev_list = dev_list, | |
732 | .dev_clear = dev_clear, | |
733 | .config_get = config_get, | |
734 | .config_set = config_set, | |
735 | .config_list = config_list, | |
736 | .dev_open = dev_open, | |
737 | .dev_close = dev_close, | |
738 | .dev_acquisition_start = dev_acquisition_start, | |
739 | .dev_acquisition_stop = dev_acquisition_stop, | |
10763937 SA |
740 | .priv = NULL, |
741 | }; |