X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fhameg-hmo%2Fprotocol.c;h=ff9cf29ff2b84407499f21f8f52df2df890b5aa5;hb=9d156555a557aa079056d42fbc9ddd12338e842e;hp=cd94f0ca1bb9393fec76f0065bf0805a9c914cd6;hpb=13f2b9d789fdc7130c0f31b88319bd0f3445109a;p=libsigrok.git diff --git a/hardware/hameg-hmo/protocol.c b/hardware/hameg-hmo/protocol.c index cd94f0ca..ff9cf29f 100644 --- a/hardware/hameg-hmo/protocol.c +++ b/hardware/hameg-hmo/protocol.c @@ -19,19 +19,17 @@ #include "protocol.h" -static const char *manufacturers[] = { - "HAMEG", -}; - static const char *hameg_scpi_dialect[] = { [SCPI_CMD_GET_DIG_DATA] = ":POD%d:DATA?", [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?", - [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %E", + [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s", [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?", [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s", + [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?", + [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?", [SCPI_CMD_GET_ANALOG_DATA] = ":CHAN%d:DATA?", [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?", - [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %E", + [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s", [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?", [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d", [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?", @@ -54,6 +52,8 @@ static const int32_t hmo_hwcaps[] = { SR_CONF_NUM_TIMEBASE, SR_CONF_TRIGGER_SLOPE, SR_CONF_HORIZ_TRIGGERPOS, + SR_CONF_SAMPLERATE, + SR_CONF_LIMIT_FRAMES, }; static const int32_t hmo_analog_caps[] = { @@ -66,6 +66,7 @@ static const char *hmo_coupling_options[] = { "AC", "ACL", "DC", + "DCL", "GND", NULL, }; @@ -256,27 +257,19 @@ static struct scope_config scope_models[] = { }, }; -static int check_manufacturer(const char *manufacturer) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(manufacturers); ++i) - if (strcmp(manufacturer, manufacturers[i]) == 0) - return SR_OK; - - return SR_ERR; -} - static void scope_state_dump(struct scope_config *config, struct scope_state *state) { unsigned int i; + char *tmp; for (i = 0; i < config->analog_channels; ++i) { - sr_info("State of analog channel %d -> %s : %s %.3eV %.3e offset", i+1, - state->analog_channels[i].state ? "On" : "Off", + tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0], + (*config->vdivs)[state->analog_channels[i].vdiv][1]); + sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)", + i + 1, state->analog_channels[i].state ? "On" : "Off", (*config->coupling_options)[state->analog_channels[i].coupling], - state->analog_channels[i].vdiv, state->analog_channels[i].vertical_offset); + tmp, state->analog_channels[i].vertical_offset); } for (i = 0; i < config->digital_channels; ++i) { @@ -289,23 +282,29 @@ static void scope_state_dump(struct scope_config *config, state->digital_pods[i] ? "On" : "Off"); } - sr_info("Current timebase: %.2es", state->timebase); - sr_info("Current trigger: %s (source), %s (slope) %.2e (offset)", + tmp = sr_period_string((*config->timebases)[state->timebase][0] * + (*config->timebases)[state->timebase][1]); + sr_info("Current timebase: %s", tmp); + g_free(tmp); + + tmp = sr_samplerate_string(state->sample_rate); + sr_info("Current samplerate: %s", tmp); + g_free(tmp); + + sr_info("Current trigger: %s (source), %s (slope) %2.2e (offset)", (*config->trigger_sources)[state->trigger_source], (*config->trigger_slopes)[state->trigger_slope], state->horiz_triggerpos); } -static int scope_state_get_array_option(struct sr_serial_dev_inst *serial, - const char *command, const char *(*array)[], - int *result) +static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi, + const char *command, const char *(*array)[], int *result) { char *tmp; unsigned int i; - if (sr_scpi_get_string(serial, command, &tmp) != SR_OK) { - if (tmp) - g_free(tmp); + if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) { + g_free(tmp); return SR_ERR; } @@ -326,11 +325,12 @@ static int scope_state_get_array_option(struct sr_serial_dev_inst *serial, return SR_OK; } -static int analog_channel_state_get(struct sr_serial_dev_inst *serial, +static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi, struct scope_config *config, struct scope_state *state) { - unsigned int i; + unsigned int i, j; + float tmp_float; char command[MAX_COMMAND_SIZE]; for (i = 0; i < config->analog_channels; ++i) { @@ -338,7 +338,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE], i + 1); - if (sr_scpi_get_bool(serial, command, + if (sr_scpi_get_bool(scpi, command, &state->analog_channels[i].state) != SR_OK) return SR_ERR; @@ -346,15 +346,23 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV], i + 1); - if (sr_scpi_get_float(serial, command, - &state->analog_channels[i].vdiv) != SR_OK) + if (sr_scpi_get_float(scpi, command, &tmp_float) != SR_OK) + return SR_ERR; + for (j = 0; j < config->num_vdivs; j++) { + if (tmp_float == ((float) (*config->vdivs)[j][0] / + (*config->vdivs)[j][1])) { + state->analog_channels[i].vdiv = j; + break; + } + } + if (i == config->num_vdivs) return SR_ERR; g_snprintf(command, sizeof(command), (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET], i + 1); - if (sr_scpi_get_float(serial, command, + if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK) return SR_ERR; @@ -362,7 +370,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING], i + 1); - if (scope_state_get_array_option(serial, command, config->coupling_options, + if (scope_state_get_array_option(scpi, command, config->coupling_options, &state->analog_channels[i].coupling) != SR_OK) return SR_ERR; } @@ -370,7 +378,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, return SR_OK; } -static int digital_channel_state_get(struct sr_serial_dev_inst *serial, +static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi, struct scope_config *config, struct scope_state *state) { @@ -382,7 +390,7 @@ static int digital_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE], i); - if (sr_scpi_get_bool(serial, command, + if (sr_scpi_get_bool(scpi, command, &state->digital_channels[i]) != SR_OK) return SR_ERR; } @@ -392,7 +400,7 @@ static int digital_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE], i + 1); - if (sr_scpi_get_bool(serial, command, + if (sr_scpi_get_bool(scpi, command, &state->digital_pods[i]) != SR_OK) return SR_ERR; } @@ -400,15 +408,81 @@ static int digital_channel_state_get(struct sr_serial_dev_inst *serial, return SR_OK; } -SR_PRIV int scope_state_get(struct sr_dev_inst *sdi) +SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi) { struct dev_context *devc; struct scope_state *state; struct scope_config *config; + int tmp; + unsigned int i; + float tmp_float; + gboolean channel_found; + char tmp_str[MAX_COMMAND_SIZE]; + char chan_name[20]; + devc = sdi->priv; config = devc->model_config; state = devc->model_state; + channel_found = FALSE; + + for (i = 0; i < config->analog_channels; ++i) { + if (state->analog_channels[i].state) { + g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1); + g_snprintf(tmp_str, sizeof(tmp_str), + (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE], + chan_name); + channel_found = TRUE; + break; + } + } + + if (!channel_found) { + for (i = 0; i < config->digital_pods; i++) { + if (state->digital_pods[i]) { + g_snprintf(chan_name, sizeof(chan_name), "POD%d", i); + g_snprintf(tmp_str, sizeof(tmp_str), + (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE], + chan_name); + channel_found = TRUE; + break; + } + } + } + + /* No channel is active, ask the instrument for the sample rate + * in single shot mode */ + if (!channel_found) { + if (sr_scpi_get_float(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE], + &tmp_float) != SR_OK) + return SR_ERR; + + state->sample_rate = tmp_float; + } else { + if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK) + return SR_ERR; + state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] / + (*config->timebases)[state->timebase][1]) * + config->num_xdivs); + } + + return SR_OK; +} + +SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + struct scope_state *state; + struct scope_config *config; + float tmp_float; + unsigned int i; + + devc = sdi->priv; + config = devc->model_config; + state = devc->model_state; + + sr_info("Fetching scope state"); if (analog_channel_state_get(sdi->conn, config, state) != SR_OK) return SR_ERR; @@ -416,29 +490,47 @@ SR_PRIV int scope_state_get(struct sr_dev_inst *sdi) if (digital_channel_state_get(sdi->conn, config, state) != SR_OK) return SR_ERR; - /* TODO check if value is sensible */ - if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE], - &state->timebase) != SR_OK) + if (sr_scpi_get_float(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE], + &tmp_float) != SR_OK) + return SR_ERR; + + for (i = 0; i < config->num_timebases; i++) { + if (tmp_float == ((float) (*config->timebases)[i][0] / + (*config->timebases)[i][1])) { + state->timebase = i; + break; + } + } + if (i == config->num_timebases) + return SR_ERR; + + if (sr_scpi_get_float(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS], + &state->horiz_triggerpos) != SR_OK) return SR_ERR; - if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS], - &state->horiz_triggerpos) != SR_OK) + if (scope_state_get_array_option(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE], + config->trigger_sources, &state->trigger_source) != SR_OK) return SR_ERR; - if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE], - config->trigger_sources, &state->trigger_source) != SR_OK) + if (scope_state_get_array_option(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE], + config->trigger_slopes, &state->trigger_slope) != SR_OK) return SR_ERR; - if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE], - config->trigger_slopes, &state->trigger_slope) != SR_OK) + if (hmo_update_sample_rate(sdi) != SR_OK) return SR_ERR; + sr_info("Fetching finished."); + scope_state_dump(config, state); return SR_OK; } -SR_PRIV struct scope_state *scope_state_new(struct scope_config *config) +static struct scope_state *scope_state_new(struct scope_config *config) { struct scope_state *state; @@ -446,15 +538,15 @@ SR_PRIV struct scope_state *scope_state_new(struct scope_config *config) return NULL; if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels, - sizeof(struct analog_channel_state)))) + sizeof(struct analog_channel_state)))) goto fail; - if (!(state->digital_channels = g_try_malloc0_n(config->digital_channels, - sizeof(gboolean)))) + if (!(state->digital_channels = g_try_malloc0_n( + config->digital_channels, sizeof(gboolean)))) goto fail; if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods, - sizeof(gboolean)))) + sizeof(gboolean)))) goto fail; return state; @@ -471,7 +563,7 @@ fail: return NULL; } -SR_PRIV void scope_state_free(struct scope_state *state) +SR_PRIV void hmo_scope_state_free(struct scope_state *state) { g_free(state->analog_channels); g_free(state->digital_channels); @@ -483,16 +575,14 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) { char tmp[25]; int model_index; - unsigned int i; - unsigned int j; - + unsigned int i, j; struct sr_probe *probe; struct dev_context *devc; devc = sdi->priv; model_index = -1; - /* Find the exact model */ + /* Find the exact model. */ for (i = 0; i < ARRAY_SIZE(scope_models); i++) { for (j = 0; scope_models[i].name[j]; j++) { if (!strcmp(sdi->model, scope_models[i].name[j])) { @@ -505,7 +595,7 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) } if (model_index == -1) { - sr_dbg("Unsupported HMO device"); + sr_dbg("Unsupported HMO device."); return SR_ERR_NA; } @@ -517,37 +607,38 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) scope_models[model_index].digital_pods))) return SR_ERR_MALLOC; - /* Add analog channels */ + /* Add analog channels. */ for (i = 0; i < scope_models[model_index].analog_channels; i++) { if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, - (*scope_models[model_index].analog_names)[i]))) + (*scope_models[model_index].analog_names)[i]))) return SR_ERR_MALLOC; sdi->probes = g_slist_append(sdi->probes, probe); - devc->analog_groups[i].name = (char *) (*scope_models[model_index].analog_names)[i]; + devc->analog_groups[i].name = + (char *)(*scope_models[model_index].analog_names)[i]; devc->analog_groups[i].probes = g_slist_append(NULL, probe); sdi->probe_groups = g_slist_append(sdi->probe_groups, &devc->analog_groups[i]); } - /* Add digital probe groups */ + /* Add digital probe groups. */ for (i = 0; i < scope_models[model_index].digital_pods; ++i) { g_snprintf(tmp, 25, "POD%d", i); devc->digital_groups[i].name = g_strdup(tmp); sdi->probe_groups = g_slist_append(sdi->probe_groups, - &devc->digital_groups[i < 8 ? 0 : 1]); + &devc->digital_groups[i < 8 ? 0 : 1]); } - /* Add digital channels */ + /* Add digital channels. */ for (i = 0; i < scope_models[model_index].digital_channels; i++) { if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, - (*scope_models[model_index].digital_names)[i]))) + (*scope_models[model_index].digital_names)[i]))) return SR_ERR_MALLOC; sdi->probes = g_slist_append(sdi->probes, probe); - devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(devc->digital_groups[i < 8 ? 0 : 1].probes, - probe); + devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append( + devc->digital_groups[i < 8 ? 0 : 1].probes, probe); } devc->model_config = &scope_models[model_index]; @@ -559,74 +650,15 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) return SR_OK; } -SR_PRIV struct sr_dev_inst *hameg_probe_serial_device(const char *serial_device, - const char *serial_options) -{ - struct sr_dev_inst *sdi; - struct dev_context *devc; - struct sr_scpi_hw_info *hw_info; - struct sr_serial_dev_inst *serial; - - sdi = NULL; - devc = NULL; - serial = NULL; - hw_info = NULL; - - if (!(serial = sr_serial_dev_inst_new(serial_device, serial_options))) - goto fail; - - sr_info("Probing %s.", serial_device); - if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) - goto fail; - - if (sr_scpi_get_hw_id(serial, &hw_info) != SR_OK) { - sr_info("Couldn't get IDN response"); - goto fail; - } - - if (check_manufacturer(hw_info->manufacturer) != SR_OK) - goto fail; - - if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, - hw_info->manufacturer, hw_info->model, - hw_info->firmware_version))) { - goto fail; - } - sr_scpi_hw_info_free(hw_info); - hw_info = NULL; - - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) - goto fail; - - sdi->driver = di; - sdi->priv = devc; - sdi->inst_type = SR_INST_SERIAL; - sdi->conn = serial; - - if (hmo_init_device(sdi) != SR_OK) - goto fail; - - return sdi; - -fail: - if (hw_info) - sr_scpi_hw_info_free(hw_info); - if (serial) - sr_serial_dev_inst_free(serial); - if (sdi) - sr_dev_inst_free(sdi); - if (devc) - g_free(devc); - - return NULL; -} - -SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) +SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data) { struct sr_probe *probe; struct sr_dev_inst *sdi; struct dev_context *devc; struct sr_datafeed_packet packet; + GArray *data; + struct sr_datafeed_analog analog; + struct sr_datafeed_logic logic; (void)fd; @@ -641,10 +673,6 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) switch (probe->type) { case SR_PROBE_ANALOG: - { - GArray *data; - struct sr_datafeed_analog analog; - if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) { if (data) g_array_free(data, TRUE); @@ -666,14 +694,8 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) sr_session_send(cb_data, &packet); g_slist_free(analog.probes); g_array_free(data, TRUE); - } - break; - + break; case SR_PROBE_LOGIC: - { - GArray *data; - struct sr_datafeed_logic logic; - if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) { if (data) g_free(data); @@ -690,11 +712,9 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) packet.payload = &logic; sr_session_send(cb_data, &packet); g_array_free(data, TRUE); - } - break; - + break; default: - sr_err("Invalid probe type"); + sr_err("Invalid probe type."); break; } @@ -705,8 +725,6 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) devc->current_probe = devc->current_probe->next; hmo_request_data(sdi); } else if (++devc->num_frames == devc->frame_limit) { - packet.type = SR_DF_END; - sr_session_send(sdi, &packet); sdi->driver->dev_acquisition_stop(sdi, cb_data); } else { devc->current_probe = devc->enabled_probes;