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