Refactoring and code simplifications.

This commit is contained in:
Uwe Hermann 2010-04-15 23:21:30 +02:00
parent fed16f06e2
commit 408e719989
4 changed files with 72 additions and 71 deletions

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "sigrok.h" #include <sigrok.h>
static int hw_init(char *deviceinfo) static int hw_init(char *deviceinfo)
{ {

View File

@ -23,8 +23,8 @@
#include <inttypes.h> #include <inttypes.h>
#include <glib.h> #include <glib.h>
#include <libusb.h> #include <libusb.h>
#include <sigrok.h>
#include "config.h" #include "config.h"
#include "sigrok.h"
#define USB_VENDOR 0x0925 #define USB_VENDOR 0x0925
#define USB_PRODUCT 0x3881 #define USB_PRODUCT 0x3881

View File

@ -140,6 +140,54 @@ static unsigned int get_memory_size(int type)
return 0; return 0;
} }
static int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
struct libusb_device_descriptor *des)
{
int j, 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 (j = 0; j < ARRAY_SIZE(zeroplus_models); j++) {
if (!(des.idProduct == zeroplus_models[j].pid))
continue;
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);
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 sigrok_device_instance *zp_open_device(int device_index) struct sigrok_device_instance *zp_open_device(int device_index)
{ {
struct sigrok_device_instance *sdi; struct sigrok_device_instance *sdi;
@ -156,59 +204,8 @@ struct sigrok_device_instance *zp_open_device(int device_index)
/* Find the device by vendor, product, bus and address. */ /* Find the device by vendor, product, bus and address. */
libusb_get_device_list(usb_context, &devlist); libusb_get_device_list(usb_context, &devlist);
for (i = 0; devlist[i]; i++) { for (i = 0; devlist[i]; i++) {
if ((err = libusb_get_device_descriptor(devlist[i], /* TODO: Error handling. */
&des))) { err = opendev3(&sdi, 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;
}
}
}
} }
} else { } else {
/* Status must be ST_ACTIVE, i.e. already in use... */ /* Status must be ST_ACTIVE, i.e. already in use... */
@ -224,14 +221,15 @@ struct sigrok_device_instance *zp_open_device(int device_index)
static void close_device(struct sigrok_device_instance *sdi) static void close_device(struct sigrok_device_instance *sdi)
{ {
if (sdi->usb->devhdl) { if (!sdi->usb->devhdl)
g_message("closing device %d on %d.%d interface %d", sdi->index, return;
sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE); g_message("closing device %d on %d.%d interface %d", sdi->index,
libusb_close(sdi->usb->devhdl); sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
sdi->usb->devhdl = NULL; libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
sdi->status = ST_INACTIVE; libusb_close(sdi->usb->devhdl);
} sdi->usb->devhdl = NULL;
sdi->status = ST_INACTIVE;
} }
static int configure_probes(GSList *probes) static int configure_probes(GSList *probes)
@ -347,7 +345,8 @@ static int hw_opendev(int device_index)
analyzer_set_memory_size(MEMORY_SIZE_512K); analyzer_set_memory_size(MEMORY_SIZE_512K);
// analyzer_set_freq(g_freq, g_freq_scale); // analyzer_set_freq(g_freq, g_freq_scale);
analyzer_set_trigger_count(1); 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( analyzer_set_ramsize_trigger_address(
(100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2); (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
@ -361,7 +360,7 @@ static int hw_opendev(int device_index)
analyzer_set_compression(COMPRESSION_NONE); analyzer_set_compression(COMPRESSION_NONE);
if (cur_samplerate == 0) { 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, if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
&samplerates.low) == SIGROK_ERR) &samplerates.low) == SIGROK_ERR)
return SIGROK_ERR; return SIGROK_ERR;
@ -382,11 +381,11 @@ static void hw_cleanup(void)
{ {
GSList *l; GSList *l;
/* Properly close all devices. */ /* Properly close all devices... */
for (l = device_instances; l; l = l->next) for (l = device_instances; l; l = l->next)
close_device((struct sigrok_device_instance *)l->data); close_device((struct sigrok_device_instance *)l->data);
/* And free all their memory. */ /* ...and free all their memory. */
for (l = device_instances; l; l = l->next) for (l = device_instances; l; l = l->next)
g_free(l->data); g_free(l->data);
g_slist_free(device_instances); g_slist_free(device_instances);
@ -400,12 +399,11 @@ static void hw_cleanup(void)
static void *hw_get_device_info(int device_index, int device_info_id) static void *hw_get_device_info(int device_index, int device_info_id)
{ {
struct sigrok_device_instance *sdi; struct sigrok_device_instance *sdi;
void *info; void *info = NULL;
if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
return NULL; return NULL;
info = NULL;
switch (device_info_id) { switch (device_info_id) {
case DI_INSTANCE: case DI_INSTANCE:
info = sdi; info = sdi;
@ -522,7 +520,10 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE); for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
packet_num++) { packet_num++) {
res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE); 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_LOGIC32;
packet.length = PACKET_SIZE; packet.length = PACKET_SIZE;

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "sigrok.h" #include <sigrok.h>
extern struct output_format output_text_bits; extern struct output_format output_text_bits;
extern struct output_format output_text_hex; extern struct output_format output_text_hex;