2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
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.
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.
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/>.
25 static struct sr_dev_driver hameg_hmo_driver_info;
27 static const char *manufacturers[] = {
32 static const uint32_t scanopts[] = {
37 static const uint32_t drvopts[] = {
48 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
50 struct sr_dev_inst *sdi;
51 struct dev_context *devc;
52 struct sr_scpi_hw_info *hw_info;
58 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
59 sr_info("Couldn't get IDN response.");
63 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
66 sdi = g_malloc0(sizeof(struct sr_dev_inst));
67 sdi->vendor = g_strdup(hw_info->manufacturer);
68 sdi->model = g_strdup(hw_info->model);
69 sdi->version = g_strdup(hw_info->firmware_version);
70 sdi->serial_num = g_strdup(hw_info->serial_number);
71 sdi->driver = &hameg_hmo_driver_info;
72 sdi->inst_type = SR_INST_SCPI;
75 sr_scpi_hw_info_free(hw_info);
78 devc = g_malloc0(sizeof(struct dev_context));
82 if (hmo_init_device(sdi) != SR_OK)
88 sr_scpi_hw_info_free(hw_info);
89 sr_dev_inst_free(sdi);
95 static GSList *scan(struct sr_dev_driver *di, GSList *options)
97 return sr_scpi_scan(di->context, options, probe_device);
100 static void clear_helper(struct dev_context *devc)
102 hmo_scope_state_free(devc->model_state);
103 g_free(devc->analog_groups);
104 g_free(devc->digital_groups);
107 static int dev_clear(const struct sr_dev_driver *di)
109 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
112 static int dev_open(struct sr_dev_inst *sdi)
114 if (sr_scpi_open(sdi->conn) != SR_OK)
117 if (hmo_scope_state_get(sdi) != SR_OK)
123 static int dev_close(struct sr_dev_inst *sdi)
125 return sr_scpi_close(sdi->conn);
128 static int check_channel_group(struct dev_context *devc,
129 const struct sr_channel_group *cg)
131 const struct scope_config *model;
133 model = devc->model_config;
138 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
141 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
144 sr_err("Invalid channel group specified.");
149 static int config_get(uint32_t key, GVariant **data,
150 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
153 struct dev_context *devc;
154 const struct scope_config *model;
155 struct scope_state *state;
162 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
165 model = devc->model_config;
166 state = devc->model_state;
169 case SR_CONF_NUM_HDIV:
170 *data = g_variant_new_int32(model->num_xdivs);
172 case SR_CONF_TIMEBASE:
173 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
174 (*model->timebases)[state->timebase][1]);
176 case SR_CONF_NUM_VDIV:
178 return SR_ERR_CHANNEL_GROUP;
179 if (cg_type != CG_ANALOG)
181 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
183 *data = g_variant_new_int32(model->num_ydivs);
187 return SR_ERR_CHANNEL_GROUP;
188 if (cg_type != CG_ANALOG)
190 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
192 *data = g_variant_new("(tt)",
193 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
194 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
196 case SR_CONF_TRIGGER_SOURCE:
197 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
199 case SR_CONF_TRIGGER_SLOPE:
200 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
202 case SR_CONF_HORIZ_TRIGGERPOS:
203 *data = g_variant_new_double(state->horiz_triggerpos);
205 case SR_CONF_COUPLING:
207 return SR_ERR_CHANNEL_GROUP;
208 if (cg_type != CG_ANALOG)
210 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
212 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
214 case SR_CONF_SAMPLERATE:
215 *data = g_variant_new_uint64(state->sample_rate);
224 static int config_set(uint32_t key, GVariant *data,
225 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
227 int ret, cg_type, idx, j;
228 char command[MAX_COMMAND_SIZE], float_str[30];
229 struct dev_context *devc;
230 const struct scope_config *model;
231 struct scope_state *state;
233 gboolean update_sample_rate;
240 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
243 model = devc->model_config;
244 state = devc->model_state;
245 update_sample_rate = FALSE;
248 case SR_CONF_LIMIT_SAMPLES:
249 devc->samples_limit = g_variant_get_uint64(data);
252 case SR_CONF_LIMIT_FRAMES:
253 devc->frame_limit = g_variant_get_uint64(data);
256 case SR_CONF_TRIGGER_SOURCE:
257 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
259 state->trigger_source = idx;
260 g_snprintf(command, sizeof(command),
261 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
262 (*model->trigger_sources)[idx]);
263 ret = sr_scpi_send(sdi->conn, command);
267 return SR_ERR_CHANNEL_GROUP;
268 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
270 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
272 state->analog_channels[j].vdiv = idx;
273 g_ascii_formatd(float_str, sizeof(float_str), "%E",
274 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
275 g_snprintf(command, sizeof(command),
276 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
278 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
279 sr_scpi_get_opc(sdi->conn) != SR_OK)
283 case SR_CONF_TIMEBASE:
284 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
286 state->timebase = idx;
287 g_ascii_formatd(float_str, sizeof(float_str), "%E",
288 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
289 g_snprintf(command, sizeof(command),
290 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
292 ret = sr_scpi_send(sdi->conn, command);
293 update_sample_rate = TRUE;
295 case SR_CONF_HORIZ_TRIGGERPOS:
296 tmp_d = g_variant_get_double(data);
297 if (tmp_d < 0.0 || tmp_d > 1.0)
299 state->horiz_triggerpos = tmp_d;
300 tmp_d = -(tmp_d - 0.5) *
301 ((double) (*model->timebases)[state->timebase][0] /
302 (*model->timebases)[state->timebase][1])
304 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
305 g_snprintf(command, sizeof(command),
306 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
308 ret = sr_scpi_send(sdi->conn, command);
310 case SR_CONF_TRIGGER_SLOPE:
311 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
313 state->trigger_slope = idx;
314 g_snprintf(command, sizeof(command),
315 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
316 (*model->trigger_slopes)[idx]);
317 ret = sr_scpi_send(sdi->conn, command);
319 case SR_CONF_COUPLING:
321 return SR_ERR_CHANNEL_GROUP;
322 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
324 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
326 state->analog_channels[j].coupling = idx;
327 g_snprintf(command, sizeof(command),
328 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
329 j + 1, (*model->coupling_options)[idx]);
330 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
331 sr_scpi_get_opc(sdi->conn) != SR_OK)
341 ret = sr_scpi_get_opc(sdi->conn);
343 if (ret == SR_OK && update_sample_rate)
344 ret = hmo_update_sample_rate(sdi);
349 static int config_list(uint32_t key, GVariant **data,
350 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
352 int cg_type = CG_NONE;
353 struct dev_context *devc = NULL;
354 const struct scope_config *model = NULL;
358 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
361 model = devc->model_config;
365 case SR_CONF_SCAN_OPTIONS:
366 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
368 case SR_CONF_DEVICE_OPTIONS:
371 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
373 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
374 } else if (cg_type == CG_ANALOG) {
375 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
377 *data = std_gvar_array_u32(NULL, 0);
380 case SR_CONF_COUPLING:
382 return SR_ERR_CHANNEL_GROUP;
385 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
387 case SR_CONF_TRIGGER_SOURCE:
390 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
392 case SR_CONF_TRIGGER_SLOPE:
395 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
397 case SR_CONF_TIMEBASE:
400 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
404 return SR_ERR_CHANNEL_GROUP;
407 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
416 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
418 char command[MAX_COMMAND_SIZE];
419 struct sr_channel *ch;
420 struct dev_context *devc;
421 const struct scope_config *model;
424 model = devc->model_config;
426 ch = devc->current_channel->data;
429 case SR_CHANNEL_ANALOG:
430 g_snprintf(command, sizeof(command),
431 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
432 #ifdef WORDS_BIGENDIAN
439 case SR_CHANNEL_LOGIC:
440 g_snprintf(command, sizeof(command),
441 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
442 ch->index < 8 ? 1 : 2);
445 sr_err("Invalid channel type.");
449 return sr_scpi_send(sdi->conn, command);
452 static int hmo_check_channels(GSList *channels)
455 struct sr_channel *ch;
456 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
457 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
460 /* Preset "not enabled" for all channels / pods. */
461 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
462 enabled_chan[idx] = FALSE;
463 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
464 enabled_pod[idx] = FALSE;
467 * Determine which channels / pods are required for the caller's
468 * specified configuration.
470 for (l = channels; l; l = l->next) {
473 case SR_CHANNEL_ANALOG:
475 if (idx < ARRAY_SIZE(enabled_chan))
476 enabled_chan[idx] = TRUE;
478 case SR_CHANNEL_LOGIC:
480 if (idx < ARRAY_SIZE(enabled_pod))
481 enabled_pod[idx] = TRUE;
489 * Check for resource conflicts. Some channels can be either
490 * analog or digital, but never both at the same time.
492 * Note that the constraints might depend on the specific model.
493 * These tests might need some adjustment when support for more
494 * models gets added to the driver.
496 if (enabled_pod[0] && enabled_chan[2])
498 if (enabled_pod[1] && enabled_chan[3])
503 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
507 gboolean *pod_enabled, setup_changed;
508 char command[MAX_COMMAND_SIZE];
509 struct scope_state *state;
510 const struct scope_config *model;
511 struct sr_channel *ch;
512 struct dev_context *devc;
513 struct sr_scpi_dev_inst *scpi;
518 state = devc->model_state;
519 model = devc->model_config;
520 setup_changed = FALSE;
522 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
524 for (l = sdi->channels; l; l = l->next) {
527 case SR_CHANNEL_ANALOG:
528 if (ch->enabled == state->analog_channels[ch->index].state)
530 g_snprintf(command, sizeof(command),
531 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
532 ch->index + 1, ch->enabled);
534 if (sr_scpi_send(scpi, command) != SR_OK) {
538 state->analog_channels[ch->index].state = ch->enabled;
539 setup_changed = TRUE;
541 case SR_CHANNEL_LOGIC:
543 * A digital POD needs to be enabled for every group of
547 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
549 if (ch->enabled == state->digital_channels[ch->index])
551 g_snprintf(command, sizeof(command),
552 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
553 ch->index, ch->enabled);
555 if (sr_scpi_send(scpi, command) != SR_OK) {
560 state->digital_channels[ch->index] = ch->enabled;
561 setup_changed = TRUE;
570 for (i = 0; i < model->digital_pods; i++) {
571 if (state->digital_pods[i] == pod_enabled[i])
573 g_snprintf(command, sizeof(command),
574 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
575 i + 1, pod_enabled[i]);
576 if (sr_scpi_send(scpi, command) != SR_OK) {
580 state->digital_pods[i] = pod_enabled[i];
581 setup_changed = TRUE;
587 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
593 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
596 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
597 size_t group, pod_count;
598 struct sr_channel *ch;
599 struct dev_context *devc;
600 struct sr_scpi_dev_inst *scpi;
606 devc->num_samples = 0;
607 devc->num_frames = 0;
609 /* Preset empty results. */
610 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
611 digital_added[group] = FALSE;
612 g_slist_free(devc->enabled_channels);
613 devc->enabled_channels = NULL;
616 * Contruct the list of enabled channels. Determine the highest
617 * number of digital pods involved in the acquisition.
620 for (l = sdi->channels; l; l = l->next) {
624 /* Only add a single digital channel per group (pod). */
625 group = ch->index / 8;
626 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
627 devc->enabled_channels = g_slist_append(
628 devc->enabled_channels, ch);
629 if (ch->type == SR_CHANNEL_LOGIC) {
630 digital_added[group] = TRUE;
631 if (pod_count < group + 1)
632 pod_count = group + 1;
636 if (!devc->enabled_channels)
638 devc->pod_count = pod_count;
639 devc->logic_data = NULL;
642 * Check constraints. Some channels can be either analog or
643 * digital, but not both at the same time.
645 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
646 sr_err("Invalid channel configuration specified!");
652 * Configure the analog and digital channels and the
653 * corresponding digital pods.
655 if (hmo_setup_channels(sdi) != SR_OK) {
656 sr_err("Failed to setup channel configuration!");
662 * Start acquisition on the first enabled channel. The
663 * receive routine will continue driving the acquisition.
665 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
666 hmo_receive_data, (void *)sdi);
668 std_session_send_df_header(sdi);
670 devc->current_channel = devc->enabled_channels;
672 return hmo_request_data(sdi);
675 g_slist_free(devc->enabled_channels);
676 devc->enabled_channels = NULL;
680 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
682 struct dev_context *devc;
683 struct sr_scpi_dev_inst *scpi;
685 std_session_send_df_end(sdi);
689 devc->num_samples = 0;
690 devc->num_frames = 0;
691 g_slist_free(devc->enabled_channels);
692 devc->enabled_channels = NULL;
694 sr_scpi_source_remove(sdi->session, scpi);
699 static struct sr_dev_driver hameg_hmo_driver_info = {
701 .longname = "Hameg HMO",
704 .cleanup = std_cleanup,
706 .dev_list = std_dev_list,
707 .dev_clear = dev_clear,
708 .config_get = config_get,
709 .config_set = config_set,
710 .config_list = config_list,
711 .dev_open = dev_open,
712 .dev_close = dev_close,
713 .dev_acquisition_start = dev_acquisition_start,
714 .dev_acquisition_stop = dev_acquisition_stop,
717 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);