]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/beaglelogic/api.c
Pass driver struct pointer to driver callbacks.
[libsigrok.git] / src / hardware / beaglelogic / api.c
index 9abbbefc972e059b0aeaed04e13518182c789392..d10d071bff0b54eca7e1aa109742950b8be758ee 100644 (file)
@@ -21,7 +21,6 @@
 #include "beaglelogic.h"
 
 SR_PRIV struct sr_dev_driver beaglelogic_driver_info;
-static struct sr_dev_driver *di = &beaglelogic_driver_info;
 
 /* Scan options */
 static const uint32_t scanopts[] = {
@@ -35,6 +34,7 @@ static const uint32_t devopts[] = {
        SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
+       SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_GET,
 };
 
@@ -60,17 +60,16 @@ static const uint64_t samplerates[] = {
        SR_HZ(1),
 };
 
-static int init(struct sr_context *sr_ctx)
+static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static struct dev_context * beaglelogic_devc_alloc(void)
+static struct dev_context *beaglelogic_devc_alloc(void)
 {
        struct dev_context *devc;
 
-       /* Allocate zeroed structure */
-       devc = g_try_malloc0(sizeof(*devc));
+       devc = g_malloc0(sizeof(struct dev_context));
 
        /* Default non-zero values (if any) */
        devc->fd = -1;
@@ -79,14 +78,13 @@ static struct dev_context * beaglelogic_devc_alloc(void)
        return devc;
 }
 
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        struct drv_context *drvc;
        GSList *devices, *l;
        struct sr_config *src;
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
-       struct sr_channel *ch;
        int i, maxch;
 
        devices = NULL;
@@ -97,7 +95,10 @@ static GSList *scan(GSList *options)
        if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
                return NULL;
 
-       sdi = sr_dev_inst_new(SR_ST_INACTIVE, NULL, "BeagleLogic", "1.0");
+       sdi = g_malloc0(sizeof(struct sr_dev_inst));
+       sdi->status = SR_ST_INACTIVE;
+       sdi->model = g_strdup("BeagleLogic");
+       sdi->version = g_strdup("1.0");
        sdi->driver = di;
 
        /* Unless explicitly specified, keep max channels to 8 only */
@@ -133,12 +134,9 @@ static GSList *scan(GSList *options)
        sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);
 
        /* Fill the channels */
-       for (i = 0; i < maxch; i++) {
-               if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
-                               beaglelogic_channel_names[i])))
-                       return NULL;
-               sdi->channels = g_slist_append(sdi->channels, ch);
-       }
+       for (i = 0; i < maxch; i++)
+               sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
+                               beaglelogic_channel_names[i]);
 
        sdi->priv = devc;
        drvc->instances = g_slist_append(drvc->instances, sdi);
@@ -147,12 +145,12 @@ static GSList *scan(GSList *options)
        return devices;
 }
 
-static GSList *dev_list(void)
+static GSList *dev_list(const struct sr_dev_driver *di)
 {
        return ((struct drv_context *)(di->priv))->instances;
 }
 
-static int dev_clear(void)
+static int dev_clear(const struct sr_dev_driver *di)
 {
        return std_dev_clear(di, NULL);
 }
@@ -201,7 +199,7 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
+static int cleanup(const struct sr_dev_driver *di)
 {
        struct drv_context *drvc;
        struct sr_dev_inst *sdi;
@@ -221,8 +219,6 @@ static int cleanup(void)
        g_slist_free(drvc->instances);
        drvc->instances = NULL;
 
-       di->priv = NULL;
-
        return SR_OK;
 }
 
@@ -241,6 +237,10 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                *data = g_variant_new_uint64(devc->cur_samplerate);
                break;
 
+       case SR_CONF_CAPTURE_RATIO:
+               *data = g_variant_new_uint64(devc->capture_ratio);
+               break;
+
        case SR_CONF_NUM_LOGIC_CHANNELS:
                *data = g_variant_new_uint32(g_slist_length(sdi->channels));
                break;
@@ -285,6 +285,14 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                }
                return beaglelogic_set_triggerflags(devc);
 
+       case SR_CONF_CAPTURE_RATIO:
+               devc->capture_ratio = g_variant_get_uint64(data);
+               if (devc->capture_ratio > 100) {
+                       devc->capture_ratio = 0;
+                       return SR_ERR;
+               }
+               return SR_OK;
+
        default:
                return SR_ERR_NA;
        }
@@ -360,7 +368,12 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
 
        /* Configure triggers & send header packet */
        if ((trigger = sr_session_trigger_get(sdi->session))) {
-               devc->stl = soft_trigger_logic_new(sdi, trigger);
+               int pre_trigger_samples = 0;
+               if (devc->limit_samples > 0)
+                       pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
+               devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
+               if (devc->stl == NULL)
+                       return SR_ERR_MALLOC;
                devc->trigger_fired = FALSE;
        } else
                devc->trigger_fired = TRUE;