]> sigrok.org Git - libsigrok.git/blobdiff - hardware/hameg-hmo/api.c
hameg-hmo: Change the way vdiv and timebase are saved.
[libsigrok.git] / hardware / hameg-hmo / api.c
index 1663a5f339a4da89658966561df637d21ba16367..33800c7345867024b467d7fbe9a7e2453720af19 100644 (file)
 
 #define SERIALCOMM "115200/8n1/flow=1"
 
+SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
+static struct sr_dev_driver *di = &hameg_hmo_driver_info;
+
+static const char *manufacturers[] = {
+       "HAMEG",
+};
+
 static const int32_t hwopts[] = {
        SR_CONF_CONN,
        SR_CONF_SERIALCOMM,
@@ -186,6 +193,83 @@ skip_device:
 #endif
 }
 
+static int check_manufacturer(const char *manufacturer)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
+               if (!strcmp(manufacturer, manufacturers[i]))
+                       return SR_OK;
+
+       return SR_ERR;
+}
+
+static struct sr_dev_inst *hmo_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_scpi_dev_inst *scpi;
+
+       sdi = NULL;
+       devc = NULL;
+       scpi = NULL;
+       hw_info = NULL;
+
+       if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
+               goto fail;
+
+       sr_info("Probing %s.", serial_device);
+       if (sr_scpi_open(scpi) != SR_OK)
+               goto fail;
+
+       if (sr_scpi_get_hw_id(scpi, &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_SCPI;
+       sdi->conn = scpi;
+
+       if (hmo_init_device(sdi) != SR_OK)
+               goto fail;
+
+       sr_scpi_close(sdi->conn);
+
+       sdi->status = SR_ST_INACTIVE;
+
+       return sdi;
+
+fail:
+       if (hw_info)
+               sr_scpi_hw_info_free(hw_info);
+       if (scpi)
+               sr_scpi_free(scpi);
+       if (sdi)
+               sr_dev_inst_free(sdi);
+       if (devc)
+               g_free(devc);
+
+       return NULL;
+}
+
 static GSList *scan(GSList *options)
 {
        GSList *devices;
@@ -270,8 +354,7 @@ static int dev_clear(void)
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       if (sdi->status != SR_ST_ACTIVE &&
-           serial_open(sdi->conn, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
+       if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
                return SR_ERR;
 
        if (hmo_scope_state_get(sdi) != SR_OK)
@@ -287,7 +370,7 @@ static int dev_close(struct sr_dev_inst *sdi)
        if (sdi->status == SR_ST_INACTIVE)
                return SR_OK;
 
-       serial_close(sdi->conn);
+       sr_scpi_close(sdi->conn);
 
        sdi->status = SR_ST_INACTIVE;
 
@@ -353,11 +436,11 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
                        return SR_ERR_PROBE_GROUP;
                } else if (pg_type == PG_ANALOG) {
                        for (i = 0; i < model->analog_channels; ++i) {
-                               if (probe_group == &devc->analog_groups[i]) {
-                                       *data = g_variant_new_int32(model->num_ydivs);
-                                       ret = SR_OK;
-                                       break;
-                               }
+                               if (probe_group != &devc->analog_groups[i])
+                                       continue;
+                               *data = g_variant_new_int32(model->num_ydivs);
+                               ret = SR_OK;
+                               break;
                        }
 
                } else {
@@ -395,7 +478,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
 {
        int ret, pg_type;
        unsigned int i, j;
-       char command[MAX_COMMAND_SIZE];
+       char command[MAX_COMMAND_SIZE], float_str[30];
        struct dev_context *devc;
        struct scope_config *model;
        struct scope_state *state;
@@ -422,15 +505,15 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
        case SR_CONF_TRIGGER_SOURCE:
                tmp = g_variant_get_string(data, NULL);
                for (i = 0; (*model->trigger_sources)[i]; i++) {
-                       if (!g_strcmp0(tmp, (*model->trigger_sources)[i])) {
-                               state->trigger_source = i;
-                               g_snprintf(command, sizeof(command),
-                                          (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
-                                          (*model->trigger_sources)[i]);
+                       if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
+                               continue;
+                       state->trigger_source = i;
+                       g_snprintf(command, sizeof(command),
+                                  (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
+                                  (*model->trigger_sources)[i]);
 
-                               ret = sr_scpi_send(sdi->conn, command);
-                               break;
-                       }
+                       ret = sr_scpi_send(sdi->conn, command);
+                       break;
                }
                break;
        case SR_CONF_VDIV:
@@ -442,42 +525,44 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                g_variant_get(data, "(tt)", &p, &q);
 
                for (i = 0; i < model->num_vdivs; i++) {
-                       if (p == (*model->vdivs)[i][0] &&
-                           q == (*model->vdivs)[i][1]) {
-                               for (j = 1; j <= model->analog_channels; ++j) {
-                                       if (probe_group == &devc->analog_groups[j - 1]) {
-                                               state->analog_channels[j - 1].vdiv = (float) p / q;
-                                               g_snprintf(command, sizeof(command),
-                                                          (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
-                                                          j, state->analog_channels[j-1].vdiv);
-
-                                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
-                                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
-                                                       return SR_ERR;
-
-                                               break;
-                                       }
-                               }
+                       if (p != (*model->vdivs)[i][0] ||
+                           q != (*model->vdivs)[i][1])
+                               continue;
+                       for (j = 1; j <= model->analog_channels; ++j) {
+                               if (probe_group != &devc->analog_groups[j - 1])
+                                       continue;
+                               state->analog_channels[j - 1].vdiv = i;
+                               g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
+                               g_snprintf(command, sizeof(command),
+                                          (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
+                                          j, float_str);
+
+                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
+                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
+                                       return SR_ERR;
 
-                               ret = SR_OK;
                                break;
                        }
+
+                       ret = SR_OK;
+                       break;
                }
                break;
        case SR_CONF_TIMEBASE:
                g_variant_get(data, "(tt)", &p, &q);
 
                for (i = 0; i < model->num_timebases; i++) {
-                       if (p == (*model->timebases)[i][0] &&
-                           q == (*model->timebases)[i][1]) {
-                               state->timebase = (float) p / q;
-                               g_snprintf(command, sizeof(command),
-                                          (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
-                                          state->timebase);
+                       if (p != (*model->timebases)[i][0] ||
+                           q != (*model->timebases)[i][1])
+                               continue;
+                       state->timebase = i;
+                       g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
+                       g_snprintf(command, sizeof(command),
+                                  (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
+                                  float_str);
 
-                               ret = sr_scpi_send(sdi->conn, command);
-                               break;
-                       }
+                       ret = sr_scpi_send(sdi->conn, command);
+                       break;
                }
                break;
        case SR_CONF_HORIZ_TRIGGERPOS:
@@ -516,25 +601,25 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                tmp = g_variant_get_string(data, NULL);
 
                for (i = 0; (*model->coupling_options)[i]; i++) {
-                       if (!strcmp(tmp, (*model->coupling_options)[i])) {
-                               for (j = 1; j <= model->analog_channels; ++j) {
-                                       if (probe_group == &devc->analog_groups[j - 1]) {
-                                               state->analog_channels[j-1].coupling = i;
-
-                                               g_snprintf(command, sizeof(command),
-                                                          (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
-                                                          j, tmp);
-
-                                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
-                                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
-                                                       return SR_ERR;
-                                               break;
-                                       }
-                               }
+                       if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
+                               continue;
+                       for (j = 1; j <= model->analog_channels; ++j) {
+                               if (probe_group != &devc->analog_groups[j - 1])
+                                       continue;
+                               state->analog_channels[j-1].coupling = i;
 
-                               ret = SR_OK;
+                               g_snprintf(command, sizeof(command),
+                                          (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
+                                          j, tmp);
+
+                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
+                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
+                                       return SR_ERR;
                                break;
                        }
+
+                       ret = SR_OK;
+                       break;
                }
                break;
        default:
@@ -679,10 +764,10 @@ static int hmo_setup_probes(const struct sr_dev_inst *sdi)
        struct scope_config *model;
        struct sr_probe *probe;
        struct dev_context *devc;
-       struct sr_serial_dev_inst *serial;
+       struct sr_scpi_dev_inst *scpi;
 
        devc = sdi->priv;
-       serial = sdi->conn;
+       scpi = sdi->conn;
        state = devc->model_state;
        model = devc->model_config;
 
@@ -692,15 +777,15 @@ static int hmo_setup_probes(const struct sr_dev_inst *sdi)
                probe = l->data;
                switch (probe->type) {
                case SR_PROBE_ANALOG:
-                       if (probe->enabled != state->analog_channels[probe->index].state) {
-                               g_snprintf(command, sizeof(command),
-                                          (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
-                                          probe->index + 1, probe->enabled);
+                       if (probe->enabled == state->analog_channels[probe->index].state)
+                               break;
+                       g_snprintf(command, sizeof(command),
+                                  (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
+                                  probe->index + 1, probe->enabled);
 
-                               if (sr_scpi_send(serial, command) != SR_OK)
-                                       return SR_ERR;
-                               state->analog_channels[probe->index].state = probe->enabled;
-                       }
+                       if (sr_scpi_send(scpi, command) != SR_OK)
+                               return SR_ERR;
+                       state->analog_channels[probe->index].state = probe->enabled;
                        break;
                case SR_PROBE_LOGIC:
                        /*
@@ -710,16 +795,16 @@ static int hmo_setup_probes(const struct sr_dev_inst *sdi)
                        if (probe->enabled)
                                pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
 
-                       if (probe->enabled != state->digital_channels[probe->index]) {
-                               g_snprintf(command, sizeof(command),
-                                          (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
-                                          probe->index, probe->enabled);
+                       if (probe->enabled == state->digital_channels[probe->index])
+                               break;
+                       g_snprintf(command, sizeof(command),
+                                  (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
+                                  probe->index, probe->enabled);
 
-                               if (sr_scpi_send(serial, command) != SR_OK)
-                                       return SR_ERR;
+                       if (sr_scpi_send(scpi, command) != SR_OK)
+                               return SR_ERR;
 
-                               state->digital_channels[probe->index] = probe->enabled;
-                       }
+                       state->digital_channels[probe->index] = probe->enabled;
                        break;
                default:
                        return SR_ERR;
@@ -727,16 +812,14 @@ static int hmo_setup_probes(const struct sr_dev_inst *sdi)
        }
 
        for (i = 1; i <= model->digital_pods; ++i) {
-               if (state->digital_pods[i - 1] != pod_enabled[i - 1]) {
-                       g_snprintf(command, sizeof(command),
-                                  (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
-                                  i, pod_enabled[i - 1]);
-
-                       if (sr_scpi_send(serial, command) != SR_OK)
-                               return SR_ERR;
-
-                       state->digital_pods[i - 1] = pod_enabled[i - 1];
-               }
+               if (state->digital_pods[i - 1] == pod_enabled[i - 1])
+                       continue;
+               g_snprintf(command, sizeof(command),
+                          (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
+                          i, pod_enabled[i - 1]);
+               if (sr_scpi_send(scpi, command) != SR_OK)
+                       return SR_ERR;
+               state->digital_pods[i - 1] = pod_enabled[i - 1];
        }
 
        g_free(pod_enabled);
@@ -750,25 +833,25 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        gboolean digital_added;
        struct sr_probe *probe;
        struct dev_context *devc;
-       struct sr_serial_dev_inst *serial;
+       struct sr_scpi_dev_inst *scpi;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       serial = sdi->conn;
+       scpi = sdi->conn;
        devc = sdi->priv;
        digital_added = FALSE;
 
        for (l = sdi->probes; l; l = l->next) {
                probe = l->data;
-               if (probe->enabled) {
-                       /* Only add a single digital probe. */
-                       if (probe->type != SR_PROBE_LOGIC || !digital_added) {
-                               devc->enabled_probes = g_slist_append(
-                                               devc->enabled_probes, probe);
-                               if (probe->type == SR_PROBE_LOGIC)
-                                       digital_added = TRUE;
-                       }
+               if (!probe->enabled)
+                       continue;
+               /* Only add a single digital probe. */
+               if (probe->type != SR_PROBE_LOGIC || !digital_added) {
+                       devc->enabled_probes = g_slist_append(
+                                       devc->enabled_probes, probe);
+                       if (probe->type == SR_PROBE_LOGIC)
+                               digital_added = TRUE;
                }
        }
 
@@ -785,7 +868,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                return SR_ERR;
        }
 
-       sr_source_add(serial->fd, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
+       sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
 
        /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
@@ -798,10 +881,15 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_serial_dev_inst *serial;
+       struct sr_scpi_dev_inst *scpi;
+       struct sr_datafeed_packet packet;
 
        (void)cb_data;
 
+       packet.type = SR_DF_END;
+       packet.payload = NULL;
+       sr_session_send(sdi, &packet);
+
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
@@ -809,8 +897,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 
        g_slist_free(devc->enabled_probes);
        devc->enabled_probes = NULL;
-       serial = sdi->conn;
-       sr_source_remove(serial->fd);
+       scpi = sdi->conn;
+       sr_scpi_source_remove(scpi);
 
        return SR_OK;
 }