]> sigrok.org Git - libsigrok.git/blobdiff - hardware/zeroplus-logic-cube/zeroplus.c
Change all sigrok_ prefixes to sr_.
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
index 12de15b1533c179115bdd39fbe99bffc695c7eef..ab48aa55cc3440fd775d8703bfdb50252b8f4103 100644 (file)
@@ -72,7 +72,7 @@ static int capabilities[] = {
        0,
 };
 
-/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
+/* List of struct sr_device_instance, maintained by opendev()/closedev(). */
 static GSList *device_instances = NULL;
 
 static libusb_context *usb_context = NULL;
@@ -140,15 +140,63 @@ static unsigned int get_memory_size(int type)
                return 0;
 }
 
-struct sigrok_device_instance *zp_open_device(int device_index)
+static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
+                   struct libusb_device_descriptor *des)
 {
-       struct sigrok_device_instance *sdi;
+       unsigned int i;
+       int err;
+
+       if ((err = libusb_get_device_descriptor(dev, des))) {
+               g_warning("failed to get device descriptor: %d", err);
+               return -1;
+       }
+
+       if (des->idVendor != USB_VENDOR)
+               return 0;
+
+       if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
+           && libusb_get_device_address(dev) == (*sdi)->usb->address) {
+
+               for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
+                       if (!(des->idProduct == zeroplus_models[i].pid))
+                               continue;
+
+                       g_message("Found PID=%04X (%s)", des->idProduct,
+                                 zeroplus_models[i].model_name);
+                       num_channels = zeroplus_models[i].channels;
+                       memory_size = zeroplus_models[i].sample_depth * 1024;
+                       break;
+               }
+
+               if (num_channels == 0) {
+                       g_warning("Unknown ZeroPlus device %04X",
+                                 des->idProduct);
+                       return -2;
+               }
+
+               /* Found it. */
+               if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
+                       (*sdi)->status = ST_ACTIVE;
+                       g_message("opened device %d on %d.%d interface %d",
+                            (*sdi)->index, (*sdi)->usb->bus,
+                            (*sdi)->usb->address, USB_INTERFACE);
+               } else {
+                       g_warning("failed to open device: %d", err);
+                       *sdi = NULL;
+               }
+       }
+
+       return 0;
+}
+
+struct sr_device_instance *zp_open_device(int device_index)
+{
+       struct sr_device_instance *sdi;
        libusb_device **devlist;
        struct libusb_device_descriptor des;
-       unsigned int j;
        int err, i;
 
-       if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
+       if (!(sdi = get_sr_device_instance(device_instances, device_index)))
                return NULL;
 
        libusb_get_device_list(usb_context, &devlist);
@@ -156,59 +204,8 @@ struct sigrok_device_instance *zp_open_device(int device_index)
                /* Find the device by vendor, product, bus and address. */
                libusb_get_device_list(usb_context, &devlist);
                for (i = 0; devlist[i]; i++) {
-                       if ((err = libusb_get_device_descriptor(devlist[i],
-                            &des))) {
-                               g_warning("failed to get device descriptor:"
-                                         "%d", err);
-                               continue;
-                       }
-
-                       if (des.idVendor == USB_VENDOR) {
-                               if (libusb_get_bus_number(devlist[i]) ==
-                                   sdi->usb->bus
-                                   && libusb_get_device_address(devlist[i]) ==
-                                   sdi->usb->address) {
-                                       for (j = 0;
-                                            j < ARRAY_SIZE(zeroplus_models);
-                                            j++) {
-                                               if (des.idProduct ==
-                                                   zeroplus_models[j].pid) {
-                                                       g_message
-                                                           ("Found PID=%04X (%s)",
-                                                            des.idProduct,
-                                                            zeroplus_models[j].
-                                                            model_name);
-                                                       num_channels =
-                                                           zeroplus_models[j].
-                                                           channels;
-                                                       memory_size =
-                                                           zeroplus_models[j].
-                                                           sample_depth * 1024;
-                                                       break;
-                                               }
-                                       }
-                                       if (num_channels == 0) {
-                                               g_warning
-                                                   ("Unknown ZeroPlus device %04X",
-                                                    des.idProduct);
-                                               continue;
-                                       }
-                                       /* Found it */
-                                       if (!(err = libusb_open(devlist[i],
-                                                &(sdi->usb->devhdl)))) {
-                                               sdi->status = ST_ACTIVE;
-                                               g_message("opened device %d on"
-                                                    " %d.%d interface %d",
-                                                    sdi->index, sdi->usb->bus,
-                                                    sdi->usb->address,
-                                                    USB_INTERFACE);
-                                       } else {
-                                               g_warning("failed to open "
-                                                         "device: %d", err);
-                                               sdi = NULL;
-                                       }
-                               }
-                       }
+                       /* TODO: Error handling. */
+                       err = opendev4(&sdi, devlist[i], &des);
                }
        } else {
                /* Status must be ST_ACTIVE, i.e. already in use... */
@@ -222,16 +219,17 @@ struct sigrok_device_instance *zp_open_device(int device_index)
        return sdi;
 }
 
-static void close_device(struct sigrok_device_instance *sdi)
+static void close_device(struct sr_device_instance *sdi)
 {
-       if (sdi->usb->devhdl) {
-               g_message("closing device %d on %d.%d interface %d", sdi->index,
-                         sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
-               libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
-               libusb_close(sdi->usb->devhdl);
-               sdi->usb->devhdl = NULL;
-               sdi->status = ST_INACTIVE;
-       }
+       if (!sdi->usb->devhdl)
+               return;
+
+       g_message("closing device %d on %d.%d interface %d", sdi->index,
+                 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
+       libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
+       libusb_close(sdi->usb->devhdl);
+       sdi->usb->devhdl = NULL;
+       sdi->status = ST_INACTIVE;
 }
 
 static int configure_probes(GSList *probes)
@@ -263,12 +261,12 @@ static int configure_probes(GSList *probes)
                                        trigger_value[stage] |= probe_bit;
                                stage++;
                                if (stage > NUM_TRIGGER_STAGES)
-                                       return SIGROK_ERR;
+                                       return SR_ERR;
                        }
                }
        }
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
 /*
@@ -277,12 +275,12 @@ static int configure_probes(GSList *probes)
 
 static int hw_init(char *deviceinfo)
 {
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
        struct libusb_device_descriptor des;
        libusb_device **devlist;
        int err, devcnt, i;
 
-       /* QUICK HACK */
+       /* Avoid compiler warnings. */
        deviceinfo = deviceinfo;
 
        if (libusb_init(&usb_context) != 0) {
@@ -307,7 +305,7 @@ static int hw_init(char *deviceinfo)
                         * TODO: Any way to detect specific model/version in
                         * the zeroplus range?
                         */
-                       sdi = sigrok_device_instance_new(devcnt,
+                       sdi = sr_device_instance_new(devcnt,
                                        ST_INACTIVE, USB_VENDOR_NAME,
                                        USB_MODEL_NAME, USB_MODEL_VERSION);
                        if (!sdi)
@@ -327,27 +325,27 @@ static int hw_init(char *deviceinfo)
 
 static int hw_opendev(int device_index)
 {
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
        int err;
 
        if (!(sdi = zp_open_device(device_index))) {
                g_warning("unable to open device");
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
        if (err != 0) {
                g_warning("Unable to claim interface: %d", err);
-               return SIGROK_ERR;
+               return SR_ERR;
        }
        analyzer_reset(sdi->usb->devhdl);
        analyzer_initialize(sdi->usb->devhdl);
-       analyzer_configure(sdi->usb->devhdl);
 
        analyzer_set_memory_size(MEMORY_SIZE_512K);
        // analyzer_set_freq(g_freq, g_freq_scale);
        analyzer_set_trigger_count(1);
-       // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger) * get_memory_size(g_memory_size)) / 100) >> 2);
+       // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
+       // * get_memory_size(g_memory_size)) / 100) >> 2);
        analyzer_set_ramsize_trigger_address(
                (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
 
@@ -361,20 +359,20 @@ static int hw_opendev(int device_index)
        analyzer_set_compression(COMPRESSION_NONE);
 
        if (cur_samplerate == 0) {
-               /* Sample rate hasn't been set. Default to the slowest one. */
+               /* Samplerate hasn't been set. Default to the slowest one. */
                if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
-                    &samplerates.low) == SIGROK_ERR)
-                       return SIGROK_ERR;
+                    &samplerates.low) == SR_ERR)
+                       return SR_ERR;
        }
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
 static void hw_closedev(int device_index)
 {
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
 
-       if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
+       if ((sdi = get_sr_device_instance(device_instances, device_index)))
                close_device(sdi);
 }
 
@@ -382,11 +380,11 @@ static void hw_cleanup(void)
 {
        GSList *l;
 
-       /* Properly close all devices. */
+       /* Properly close all devices... */
        for (l = device_instances; l; l = l->next)
-               close_device((struct sigrok_device_instance *)l->data);
+               close_device((struct sr_device_instance *)l->data);
 
-       /* And free all their memory. */
+       /* ...and free all their memory. */
        for (l = device_instances; l; l = l->next)
                g_free(l->data);
        g_slist_free(device_instances);
@@ -399,13 +397,12 @@ static void hw_cleanup(void)
 
 static void *hw_get_device_info(int device_index, int device_info_id)
 {
-       struct sigrok_device_instance *sdi;
-       void *info;
+       struct sr_device_instance *sdi;
+       void *info = NULL;
 
-       if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
+       if (!(sdi = get_sr_device_instance(device_instances, device_index)))
                return NULL;
 
-       info = NULL;
        switch (device_info_id) {
        case DI_INSTANCE:
                info = sdi;
@@ -429,9 +426,9 @@ static void *hw_get_device_info(int device_index, int device_info_id)
 
 static int hw_get_status(int device_index)
 {
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
 
-       sdi = get_sigrok_device_instance(device_instances, device_index);
+       sdi = get_sr_device_instance(device_instances, device_index);
        if (sdi)
                return sdi->status;
        else
@@ -446,7 +443,7 @@ static int *hw_get_capabilities(void)
 /* TODO: This will set the same samplerate for all devices. */
 static int set_configuration_samplerate(uint64_t samplerate)
 {
-       g_message("%s(%llu)", __FUNCTION__, samplerate);
+       g_message("%s(%" PRIu64 ")", __FUNCTION__, samplerate);
        if (samplerate > MHZ(1))
                analyzer_set_freq(samplerate / MHZ(1), FREQ_SCALE_MHZ);
        else if (samplerate > KHZ(1))
@@ -456,16 +453,16 @@ static int set_configuration_samplerate(uint64_t samplerate)
 
        cur_samplerate = samplerate;
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
 static int hw_set_configuration(int device_index, int capability, void *value)
 {
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
        uint64_t *tmp_u64;
 
-       if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
-               return SIGROK_ERR;
+       if (!(sdi = get_sr_device_instance(device_instances, device_index)))
+               return SR_ERR;
 
        switch (capability) {
        case HWCAP_SAMPLERATE:
@@ -474,24 +471,28 @@ static int hw_set_configuration(int device_index, int capability, void *value)
        case HWCAP_PROBECONFIG:
                return configure_probes((GSList *) value);
        case HWCAP_LIMIT_SAMPLES:
-               limit_samples = strtoull(value, NULL, 10);
-               return SIGROK_OK;
+               tmp_u64 = value;
+               limit_samples = *tmp_u64;
+               return SR_OK;
        default:
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 }
 
 static int hw_start_acquisition(int device_index, gpointer session_device_id)
 {
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
        struct datafeed_packet packet;
        struct datafeed_header header;
        int res;
        unsigned int packet_num;
        unsigned char *buf;
 
-       if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
-               return SIGROK_ERR;
+       if (!(sdi = get_sr_device_instance(device_instances, device_index)))
+               return SR_ERR;
+
+       /* push configured settings to device */
+       analyzer_configure(sdi->usb->devhdl);
 
        analyzer_start(sdi->usb->devhdl);
        g_message("Waiting for data");
@@ -511,21 +512,26 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        gettimeofday(&header.starttime, NULL);
        header.samplerate = cur_samplerate;
        header.protocol_id = PROTO_RAW;
-       header.num_probes = num_channels;
+       header.num_logic_probes = num_channels;
+       header.num_analog_probes = 0;
        session_bus(session_device_id, &packet);
 
        buf = g_malloc(PACKET_SIZE);
        if (!buf)
-               return SIGROK_ERR;
+               return SR_ERR;
        analyzer_read_start(sdi->usb->devhdl);
        /* Send the incoming transfer to the session bus. */
        for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
             packet_num++) {
                res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
-               // g_message("Tried to read %llx bytes, actually read %x bytes", PACKET_SIZE, res);
+#if 0
+               g_message("Tried to read %llx bytes, actually read %x bytes",
+                         PACKET_SIZE, res);
+#endif
 
-               packet.type = DF_LOGIC32;
+               packet.type = DF_LOGIC;
                packet.length = PACKET_SIZE;
+               packet.unitsize = 4;
                packet.payload = buf;
                session_bus(session_device_id, &packet);
        }
@@ -535,19 +541,19 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        packet.type = DF_END;
        session_bus(session_device_id, &packet);
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
 /* This stops acquisition on ALL devices, ignoring device_index. */
 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 {
        struct datafeed_packet packet;
-       struct sigrok_device_instance *sdi;
+       struct sr_device_instance *sdi;
 
        packet.type = DF_END;
        session_bus(session_device_id, &packet);
 
-       if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
+       if (!(sdi = get_sr_device_instance(device_instances, device_index)))
                return; /* TODO: Cry? */
 
        analyzer_reset(sdi->usb->devhdl);