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 #define SERIALCOMM "115200/8n1/flow=1"
27 static struct sr_dev_driver hameg_hmo_driver_info;
29 static const char *manufacturers[] = {
34 static const uint32_t scanopts[] = {
39 static const uint32_t drvopts[] = {
50 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
52 struct sr_dev_inst *sdi;
53 struct dev_context *devc;
54 struct sr_scpi_hw_info *hw_info;
60 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
61 sr_info("Couldn't get IDN response.");
65 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
68 sdi = g_malloc0(sizeof(struct sr_dev_inst));
69 sdi->vendor = g_strdup(hw_info->manufacturer);
70 sdi->model = g_strdup(hw_info->model);
71 sdi->version = g_strdup(hw_info->firmware_version);
72 sdi->serial_num = g_strdup(hw_info->serial_number);
73 sdi->driver = &hameg_hmo_driver_info;
74 sdi->inst_type = SR_INST_SCPI;
77 sr_scpi_hw_info_free(hw_info);
80 devc = g_malloc0(sizeof(struct dev_context));
84 if (hmo_init_device(sdi) != SR_OK)
90 sr_scpi_hw_info_free(hw_info);
91 sr_dev_inst_free(sdi);
97 static GSList *scan(struct sr_dev_driver *di, GSList *options)
99 return sr_scpi_scan(di->context, options, probe_device);
102 static void clear_helper(struct dev_context *devc)
104 hmo_scope_state_free(devc->model_state);
105 g_free(devc->analog_groups);
106 g_free(devc->digital_groups);
109 static int dev_clear(const struct sr_dev_driver *di)
111 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
114 static int dev_open(struct sr_dev_inst *sdi)
116 if (sr_scpi_open(sdi->conn) != SR_OK)
119 if (hmo_scope_state_get(sdi) != SR_OK)
125 static int dev_close(struct sr_dev_inst *sdi)
127 return sr_scpi_close(sdi->conn);
130 static int check_channel_group(struct dev_context *devc,
131 const struct sr_channel_group *cg)
134 const struct scope_config *model;
136 model = devc->model_config;
141 for (i = 0; i < model->analog_channels; i++)
142 if (cg == devc->analog_groups[i])
145 for (i = 0; i < model->digital_pods; i++)
146 if (cg == devc->digital_groups[i])
149 sr_err("Invalid channel group specified.");
154 static int config_get(uint32_t key, GVariant **data,
155 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
159 struct dev_context *devc;
160 const struct scope_config *model;
161 struct scope_state *state;
168 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
171 model = devc->model_config;
172 state = devc->model_state;
175 case SR_CONF_NUM_HDIV:
176 *data = g_variant_new_int32(model->num_xdivs);
178 case SR_CONF_TIMEBASE:
179 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
180 (*model->timebases)[state->timebase][1]);
182 case SR_CONF_NUM_VDIV:
183 if (cg_type == CG_NONE) {
184 return SR_ERR_CHANNEL_GROUP;
185 } else if (cg_type == CG_ANALOG) {
186 for (i = 0; i < model->analog_channels; i++) {
187 if (cg != devc->analog_groups[i])
189 *data = g_variant_new_int32(model->num_ydivs);
197 if (cg_type == CG_NONE) {
198 return SR_ERR_CHANNEL_GROUP;
199 } else if (cg_type == CG_ANALOG) {
200 for (i = 0; i < model->analog_channels; i++) {
201 if (cg != devc->analog_groups[i])
203 *data = g_variant_new("(tt)",
204 (*model->vdivs)[state->analog_channels[i].vdiv][0],
205 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
212 case SR_CONF_TRIGGER_SOURCE:
213 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
215 case SR_CONF_TRIGGER_SLOPE:
216 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
218 case SR_CONF_HORIZ_TRIGGERPOS:
219 *data = g_variant_new_double(state->horiz_triggerpos);
221 case SR_CONF_COUPLING:
222 if (cg_type == CG_NONE) {
223 return SR_ERR_CHANNEL_GROUP;
224 } else if (cg_type == CG_ANALOG) {
225 for (i = 0; i < model->analog_channels; i++) {
226 if (cg != devc->analog_groups[i])
228 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
235 case SR_CONF_SAMPLERATE:
236 *data = g_variant_new_uint64(state->sample_rate);
245 static int config_set(uint32_t key, GVariant *data,
246 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
248 int ret, cg_type, idx;
250 char command[MAX_COMMAND_SIZE], float_str[30];
251 struct dev_context *devc;
252 const struct scope_config *model;
253 struct scope_state *state;
255 gboolean update_sample_rate;
262 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
265 model = devc->model_config;
266 state = devc->model_state;
267 update_sample_rate = FALSE;
272 case SR_CONF_LIMIT_FRAMES:
273 devc->frame_limit = g_variant_get_uint64(data);
276 case SR_CONF_TRIGGER_SOURCE:
277 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
279 state->trigger_source = idx;
280 g_snprintf(command, sizeof(command),
281 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
282 (*model->trigger_sources)[idx]);
283 ret = sr_scpi_send(sdi->conn, command);
286 if (cg_type == CG_NONE)
287 return SR_ERR_CHANNEL_GROUP;
288 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
290 for (j = 1; j <= model->analog_channels; j++) {
291 if (cg != devc->analog_groups[j - 1])
293 state->analog_channels[j - 1].vdiv = idx;
294 g_ascii_formatd(float_str, sizeof(float_str), "%E",
295 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
296 g_snprintf(command, sizeof(command),
297 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
299 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
300 sr_scpi_get_opc(sdi->conn) != SR_OK)
306 case SR_CONF_TIMEBASE:
307 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
309 state->timebase = idx;
310 g_ascii_formatd(float_str, sizeof(float_str), "%E",
311 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
312 g_snprintf(command, sizeof(command),
313 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
315 ret = sr_scpi_send(sdi->conn, command);
316 update_sample_rate = TRUE;
318 case SR_CONF_HORIZ_TRIGGERPOS:
319 tmp_d = g_variant_get_double(data);
320 if (tmp_d < 0.0 || tmp_d > 1.0)
322 state->horiz_triggerpos = tmp_d;
323 tmp_d = -(tmp_d - 0.5) *
324 ((double) (*model->timebases)[state->timebase][0] /
325 (*model->timebases)[state->timebase][1])
327 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
328 g_snprintf(command, sizeof(command),
329 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
331 ret = sr_scpi_send(sdi->conn, command);
333 case SR_CONF_TRIGGER_SLOPE:
334 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
336 state->trigger_slope = idx;
337 g_snprintf(command, sizeof(command),
338 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
339 (*model->trigger_slopes)[idx]);
340 ret = sr_scpi_send(sdi->conn, command);
342 case SR_CONF_COUPLING:
343 if (cg_type == CG_NONE)
344 return SR_ERR_CHANNEL_GROUP;
345 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
347 for (j = 1; j <= model->analog_channels; j++) {
348 if (cg != devc->analog_groups[j - 1])
350 state->analog_channels[j - 1].coupling = idx;
351 g_snprintf(command, sizeof(command),
352 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
353 j, (*model->coupling_options)[idx]);
354 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
355 sr_scpi_get_opc(sdi->conn) != SR_OK)
367 ret = sr_scpi_get_opc(sdi->conn);
369 if (ret == SR_OK && update_sample_rate)
370 ret = hmo_update_sample_rate(sdi);
375 static int config_list(uint32_t key, GVariant **data,
376 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
378 int cg_type = CG_NONE;
379 struct dev_context *devc = NULL;
380 const struct scope_config *model = NULL;
384 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
387 model = devc->model_config;
391 case SR_CONF_SCAN_OPTIONS:
392 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
394 case SR_CONF_DEVICE_OPTIONS:
395 if (cg_type == CG_NONE) {
397 *data = std_gvar_array_u32((const uint32_t *)model->devopts, model->num_devopts);
399 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
400 } else if (cg_type == CG_ANALOG) {
401 *data = std_gvar_array_u32((const uint32_t *)model->devopts_cg_analog, model->num_devopts_cg_analog);
403 *data = std_gvar_array_u32(NULL, 0);
406 case SR_CONF_COUPLING:
407 if (cg_type == CG_NONE)
408 return SR_ERR_CHANNEL_GROUP;
409 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
411 case SR_CONF_TRIGGER_SOURCE:
414 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
416 case SR_CONF_TRIGGER_SLOPE:
419 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
421 case SR_CONF_TIMEBASE:
424 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
427 if (cg_type == CG_NONE)
428 return SR_ERR_CHANNEL_GROUP;
429 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
438 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
440 char command[MAX_COMMAND_SIZE];
441 struct sr_channel *ch;
442 struct dev_context *devc;
443 const struct scope_config *model;
446 model = devc->model_config;
448 ch = devc->current_channel->data;
451 case SR_CHANNEL_ANALOG:
452 g_snprintf(command, sizeof(command),
453 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
454 #ifdef WORDS_BIGENDIAN
461 case SR_CHANNEL_LOGIC:
462 g_snprintf(command, sizeof(command),
463 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
464 ch->index < 8 ? 1 : 2);
467 sr_err("Invalid channel type.");
471 return sr_scpi_send(sdi->conn, command);
474 static int hmo_check_channels(GSList *channels)
477 struct sr_channel *ch;
478 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
479 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
482 /* Preset "not enabled" for all channels / pods. */
483 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
484 enabled_chan[idx] = FALSE;
485 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
486 enabled_pod[idx] = FALSE;
489 * Determine which channels / pods are required for the caller's
490 * specified configuration.
492 for (l = channels; l; l = l->next) {
495 case SR_CHANNEL_ANALOG:
497 if (idx < ARRAY_SIZE(enabled_chan))
498 enabled_chan[idx] = TRUE;
500 case SR_CHANNEL_LOGIC:
502 if (idx < ARRAY_SIZE(enabled_pod))
503 enabled_pod[idx] = TRUE;
511 * Check for resource conflicts. Some channels can be either
512 * analog or digital, but never both at the same time.
514 * Note that the constraints might depend on the specific model.
515 * These tests might need some adjustment when support for more
516 * models gets added to the driver.
518 if (enabled_pod[0] && enabled_chan[2])
520 if (enabled_pod[1] && enabled_chan[3])
525 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
529 gboolean *pod_enabled, setup_changed;
530 char command[MAX_COMMAND_SIZE];
531 struct scope_state *state;
532 const struct scope_config *model;
533 struct sr_channel *ch;
534 struct dev_context *devc;
535 struct sr_scpi_dev_inst *scpi;
539 state = devc->model_state;
540 model = devc->model_config;
541 setup_changed = FALSE;
543 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
545 for (l = sdi->channels; l; l = l->next) {
548 case SR_CHANNEL_ANALOG:
549 if (ch->enabled == state->analog_channels[ch->index].state)
551 g_snprintf(command, sizeof(command),
552 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
553 ch->index + 1, ch->enabled);
555 if (sr_scpi_send(scpi, command) != SR_OK)
557 state->analog_channels[ch->index].state = ch->enabled;
558 setup_changed = TRUE;
560 case SR_CHANNEL_LOGIC:
562 * A digital POD needs to be enabled for every group of
566 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
568 if (ch->enabled == state->digital_channels[ch->index])
570 g_snprintf(command, sizeof(command),
571 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
572 ch->index, ch->enabled);
574 if (sr_scpi_send(scpi, command) != SR_OK)
577 state->digital_channels[ch->index] = ch->enabled;
578 setup_changed = TRUE;
585 for (i = 1; i <= model->digital_pods; i++) {
586 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
588 g_snprintf(command, sizeof(command),
589 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
590 i, pod_enabled[i - 1]);
591 if (sr_scpi_send(scpi, command) != SR_OK)
593 state->digital_pods[i - 1] = pod_enabled[i - 1];
594 setup_changed = TRUE;
599 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
605 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
608 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
609 size_t group, pod_count;
610 struct sr_channel *ch;
611 struct dev_context *devc;
612 struct sr_scpi_dev_inst *scpi;
618 /* Preset empty results. */
619 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
620 digital_added[group] = FALSE;
621 g_slist_free(devc->enabled_channels);
622 devc->enabled_channels = NULL;
625 * Contruct the list of enabled channels. Determine the highest
626 * number of digital pods involved in the acquisition.
629 for (l = sdi->channels; l; l = l->next) {
633 /* Only add a single digital channel per group (pod). */
634 group = ch->index / 8;
635 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
636 devc->enabled_channels = g_slist_append(
637 devc->enabled_channels, ch);
638 if (ch->type == SR_CHANNEL_LOGIC) {
639 digital_added[group] = TRUE;
640 if (pod_count < group + 1)
641 pod_count = group + 1;
645 if (!devc->enabled_channels)
647 devc->pod_count = pod_count;
648 devc->logic_data = NULL;
651 * Check constraints. Some channels can be either analog or
652 * digital, but not both at the same time.
654 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
655 sr_err("Invalid channel configuration specified!");
661 * Configure the analog and digital channels and the
662 * corresponding digital pods.
664 if (hmo_setup_channels(sdi) != SR_OK) {
665 sr_err("Failed to setup channel configuration!");
671 * Start acquisition on the first enabled channel. The
672 * receive routine will continue driving the acquisition.
674 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
675 hmo_receive_data, (void *)sdi);
677 std_session_send_df_header(sdi);
679 devc->current_channel = devc->enabled_channels;
681 return hmo_request_data(sdi);
684 g_slist_free(devc->enabled_channels);
685 devc->enabled_channels = NULL;
689 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
691 struct dev_context *devc;
692 struct sr_scpi_dev_inst *scpi;
694 std_session_send_df_end(sdi);
698 devc->num_frames = 0;
699 g_slist_free(devc->enabled_channels);
700 devc->enabled_channels = NULL;
702 sr_scpi_source_remove(sdi->session, scpi);
707 static struct sr_dev_driver hameg_hmo_driver_info = {
709 .longname = "Hameg HMO",
712 .cleanup = std_cleanup,
714 .dev_list = std_dev_list,
715 .dev_clear = dev_clear,
716 .config_get = config_get,
717 .config_set = config_set,
718 .config_list = config_list,
719 .dev_open = dev_open,
720 .dev_close = dev_close,
721 .dev_acquisition_start = dev_acquisition_start,
722 .dev_acquisition_stop = dev_acquisition_stop,
725 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);