2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com>
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.
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.
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/>.
26 static struct sr_dev_driver yokogawa_dlm_driver_info;
28 static const char *MANUFACTURER_ID = "YOKOGAWA";
30 static const uint32_t scanopts[] = {
34 static const uint32_t drvopts[] = {
35 SR_CONF_LOGIC_ANALYZER,
39 static const uint32_t devopts[] = {
40 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
41 SR_CONF_SAMPLERATE | SR_CONF_GET,
42 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_HDIV | SR_CONF_GET,
44 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 static const uint32_t devopts_cg_analog[] = {
50 SR_CONF_NUM_VDIV | SR_CONF_GET,
51 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 static const uint32_t devopts_cg_digital[] = {
65 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
67 struct sr_dev_inst *sdi;
68 struct dev_context *devc;
69 struct sr_scpi_hw_info *hw_info;
77 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
78 sr_info("Couldn't get IDN response.");
82 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
85 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
88 sdi = g_malloc0(sizeof(struct sr_dev_inst));
89 sdi->vendor = g_strdup("Yokogawa");
90 sdi->model = g_strdup(model_name);
91 sdi->version = g_strdup(hw_info->firmware_version);
93 sdi->serial_num = g_strdup(hw_info->serial_number);
95 sr_scpi_hw_info_free(hw_info);
98 devc = g_malloc0(sizeof(struct dev_context));
100 sdi->driver = &yokogawa_dlm_driver_info;
102 sdi->inst_type = SR_INST_SCPI;
105 if (dlm_device_init(sdi, model_index) != SR_OK)
111 sr_scpi_hw_info_free(hw_info);
112 sr_dev_inst_free(sdi);
118 static GSList *scan(struct sr_dev_driver *di, GSList *options)
120 return sr_scpi_scan(di->context, options, probe_device);
123 static void clear_helper(struct dev_context *devc)
125 dlm_scope_state_destroy(devc->model_state);
126 g_free(devc->analog_groups);
127 g_free(devc->digital_groups);
130 static int dev_clear(const struct sr_dev_driver *di)
132 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
135 static int dev_open(struct sr_dev_inst *sdi)
137 if (sr_scpi_open(sdi->conn) != SR_OK)
140 if (dlm_scope_state_query(sdi) != SR_OK)
146 static int dev_close(struct sr_dev_inst *sdi)
148 return sr_scpi_close(sdi->conn);
152 * Check which category a given channel group belongs to.
154 * @param devc Our internal device context.
155 * @param cg The channel group to check.
157 * @retval CG_NONE cg is NULL
158 * @retval CG_ANALOG cg is an analog group
159 * @retval CG_DIGITAL cg is a digital group
160 * @retval CG_INVALID cg is something else
162 static int check_channel_group(struct dev_context *devc,
163 const struct sr_channel_group *cg)
166 const struct scope_config *model;
168 model = devc->model_config;
173 for (i = 0; i < model->analog_channels; i++)
174 if (cg == devc->analog_groups[i])
177 for (i = 0; i < model->pods; i++)
178 if (cg == devc->digital_groups[i])
181 sr_err("Invalid channel group specified.");
185 static int config_get(uint32_t key, GVariant **data,
186 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
190 struct dev_context *devc;
191 const struct scope_config *model;
192 struct scope_state *state;
199 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
202 model = devc->model_config;
203 state = devc->model_state;
206 case SR_CONF_NUM_HDIV:
207 *data = g_variant_new_int32(model->num_xdivs);
210 case SR_CONF_TIMEBASE:
211 *data = g_variant_new("(tt)",
212 dlm_timebases[state->timebase][0],
213 dlm_timebases[state->timebase][1]);
216 case SR_CONF_NUM_VDIV:
217 if (cg_type == CG_NONE) {
218 return SR_ERR_CHANNEL_GROUP;
219 } else if (cg_type == CG_ANALOG) {
220 *data = g_variant_new_int32(model->num_ydivs);
229 if (cg_type == CG_NONE) {
230 return SR_ERR_CHANNEL_GROUP;
231 } else if (cg_type != CG_ANALOG)
234 for (i = 0; i < model->analog_channels; i++) {
235 if (cg != devc->analog_groups[i])
237 *data = g_variant_new("(tt)",
238 dlm_vdivs[state->analog_states[i].vdiv][0],
239 dlm_vdivs[state->analog_states[i].vdiv][1]);
244 case SR_CONF_TRIGGER_SOURCE:
245 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
248 case SR_CONF_TRIGGER_SLOPE:
249 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
252 case SR_CONF_HORIZ_TRIGGERPOS:
253 *data = g_variant_new_double(state->horiz_triggerpos);
256 case SR_CONF_COUPLING:
258 if (cg_type == CG_NONE) {
259 return SR_ERR_CHANNEL_GROUP;
260 } else if (cg_type != CG_ANALOG)
263 for (i = 0; i < model->analog_channels; i++) {
264 if (cg != devc->analog_groups[i])
266 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
271 case SR_CONF_SAMPLERATE:
272 *data = g_variant_new_uint64(state->sample_rate);
282 static int config_set(uint32_t key, GVariant *data,
283 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
285 int ret, cg_type, idx;
288 struct dev_context *devc;
289 const struct scope_config *model;
290 struct scope_state *state;
292 gboolean update_sample_rate;
294 if (!sdi || !(devc = sdi->priv))
297 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
300 model = devc->model_config;
301 state = devc->model_state;
302 update_sample_rate = FALSE;
307 case SR_CONF_LIMIT_FRAMES:
308 devc->frame_limit = g_variant_get_uint64(data);
311 case SR_CONF_TRIGGER_SOURCE:
312 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
314 state->trigger_source = idx;
315 /* TODO: A and B trigger support possible? */
316 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[idx]);
319 if (cg_type == CG_NONE)
320 return SR_ERR_CHANNEL_GROUP;
321 if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(dlm_vdivs))) < 0)
323 for (j = 1; j <= model->analog_channels; j++) {
324 if (cg != devc->analog_groups[j - 1])
326 state->analog_states[j - 1].vdiv = idx;
327 g_ascii_formatd(float_str, sizeof(float_str),
328 "%E", (float) dlm_vdivs[idx][0] / dlm_vdivs[idx][1]);
329 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
330 sr_scpi_get_opc(sdi->conn) != SR_OK)
336 case SR_CONF_TIMEBASE:
337 if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(dlm_timebases))) < 0)
339 state->timebase = idx;
340 g_ascii_formatd(float_str, sizeof(float_str),
341 "%E", (float) dlm_timebases[idx][0] / dlm_timebases[idx][1]);
342 ret = dlm_timebase_set(sdi->conn, float_str);
343 update_sample_rate = TRUE;
345 case SR_CONF_HORIZ_TRIGGERPOS:
346 tmp_d = g_variant_get_double(data);
348 /* TODO: Check if the calculation makes sense for the DLM. */
349 if (tmp_d < 0.0 || tmp_d > 1.0)
352 state->horiz_triggerpos = tmp_d;
353 tmp_d = -(tmp_d - 0.5) *
354 ((double) dlm_timebases[state->timebase][0] /
355 dlm_timebases[state->timebase][1])
358 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
359 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
361 case SR_CONF_TRIGGER_SLOPE:
362 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(dlm_trigger_slopes))) < 0)
364 /* Note: See dlm_trigger_slopes[] in protocol.c. */
365 state->trigger_slope = idx;
366 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
368 case SR_CONF_COUPLING:
369 if (cg_type == CG_NONE)
370 return SR_ERR_CHANNEL_GROUP;
371 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
373 for (j = 1; j <= model->analog_channels; j++) {
374 if (cg != devc->analog_groups[j - 1])
376 state->analog_states[j - 1].coupling = idx;
377 if (dlm_analog_chan_coupl_set(sdi->conn, j, (*model->coupling_options)[idx]) != SR_OK ||
378 sr_scpi_get_opc(sdi->conn) != SR_OK)
390 ret = sr_scpi_get_opc(sdi->conn);
392 if (ret == SR_OK && update_sample_rate)
393 ret = dlm_sample_rate_query(sdi);
398 static int config_channel_set(const struct sr_dev_inst *sdi,
399 struct sr_channel *ch, unsigned int changes)
401 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
402 if (changes != SR_CHANNEL_SET_ENABLED)
405 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
408 static int config_list(uint32_t key, GVariant **data,
409 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
411 int cg_type = CG_NONE;
412 struct dev_context *devc;
413 const struct scope_config *model;
415 devc = (sdi) ? sdi->priv : NULL;
416 model = (devc) ? devc->model_config : NULL;
420 case SR_CONF_SCAN_OPTIONS:
421 case SR_CONF_DEVICE_OPTIONS:
422 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
423 case SR_CONF_TIMEBASE:
424 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(dlm_timebases));
426 case SR_CONF_TRIGGER_SOURCE:
429 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
431 case SR_CONF_TRIGGER_SLOPE:
432 *data = g_variant_new_strv(ARRAY_AND_SIZE(dlm_trigger_slopes));
434 case SR_CONF_NUM_HDIV:
435 *data = g_variant_new_uint32(model->num_xdivs);
442 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
446 case SR_CONF_DEVICE_OPTIONS:
447 if (cg_type == CG_ANALOG)
448 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
449 else if (cg_type == CG_DIGITAL)
450 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_digital));
452 *data = std_gvar_array_u32(NULL, 0);
454 case SR_CONF_COUPLING:
455 if (cg_type == CG_NONE)
456 return SR_ERR_CHANNEL_GROUP;
457 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
460 if (cg_type == CG_NONE)
461 return SR_ERR_CHANNEL_GROUP;
462 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(dlm_vdivs));
471 static int dlm_check_channels(GSList *channels)
474 struct sr_channel *ch;
475 gboolean enabled_pod1, enabled_chan4;
477 enabled_pod1 = enabled_chan4 = FALSE;
479 /* Note: On the DLM2000, CH4 and Logic are shared. */
480 /* TODO Handle non-DLM2000 models. */
481 for (l = channels; l; l = l->next) {
484 case SR_CHANNEL_ANALOG:
486 enabled_chan4 = TRUE;
488 case SR_CHANNEL_LOGIC:
496 if (enabled_pod1 && enabled_chan4)
502 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
505 gboolean digital_added;
506 struct sr_channel *ch;
507 struct dev_context *devc;
508 struct sr_scpi_dev_inst *scpi;
512 digital_added = FALSE;
514 g_slist_free(devc->enabled_channels);
515 devc->enabled_channels = NULL;
517 for (l = sdi->channels; l; l = l->next) {
521 /* Only add a single digital channel. */
522 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
523 devc->enabled_channels = g_slist_append(
524 devc->enabled_channels, ch);
525 if (ch->type == SR_CHANNEL_LOGIC)
526 digital_added = TRUE;
530 if (!devc->enabled_channels)
533 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
534 sr_err("Invalid channel configuration specified!");
538 /* Request data for the first enabled channel. */
539 devc->current_channel = devc->enabled_channels;
540 dlm_channel_data_request(sdi);
542 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
543 dlm_data_receive, (void *)sdi);
548 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
550 struct dev_context *devc;
552 std_session_send_df_end(sdi);
556 devc->num_frames = 0;
557 g_slist_free(devc->enabled_channels);
558 devc->enabled_channels = NULL;
560 sr_scpi_source_remove(sdi->session, sdi->conn);
565 static struct sr_dev_driver yokogawa_dlm_driver_info = {
566 .name = "yokogawa-dlm",
567 .longname = "Yokogawa DL/DLM",
570 .cleanup = std_cleanup,
572 .dev_list = std_dev_list,
573 .dev_clear = dev_clear,
574 .config_get = config_get,
575 .config_set = config_set,
576 .config_channel_set = config_channel_set,
577 .config_list = config_list,
578 .dev_open = dev_open,
579 .dev_close = dev_close,
580 .dev_acquisition_start = dev_acquisition_start,
581 .dev_acquisition_stop = dev_acquisition_stop,
584 SR_REGISTER_DEV_DRIVER(yokogawa_dlm_driver_info);