zeroplus: Use message logging helpers.

This commit is contained in:
Uwe Hermann 2013-01-28 17:43:04 +01:00
parent c4227fc637
commit 6d1161142d
4 changed files with 84 additions and 50 deletions

View File

@ -30,9 +30,11 @@
*/ */
#include <assert.h> #include <assert.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
#include "analyzer.h" #include "analyzer.h"
#include "gl_usb.h" #include "gl_usb.h"
#include "libsigrok-internal.h" #include "protocol.h"
enum { enum {
HARD_DATA_CHECK_SUM = 0x00, HARD_DATA_CHECK_SUM = 0x00,
@ -244,7 +246,7 @@ static int __analyzer_set_freq(libusb_device_handle *devh, int freq, int scale)
break; break;
} }
sr_dbg("zp: Setting samplerate regs (freq=%d, scale=%d): " sr_dbg("Setting samplerate regs (freq=%d, scale=%d): "
"reg0: %d, reg1: %d, reg2: %d, reg3: %d.", "reg0: %d, reg1: %d, reg2: %d, reg3: %d.",
freq, scale, divisor, reg0, 0x02, reg2); freq, scale, divisor, reg0, 0x02, reg2);
@ -318,7 +320,7 @@ static int __analyzer_set_freq(libusb_device_handle *devh, int freq, int scale)
if (!f[i].freq) if (!f[i].freq)
return -1; return -1;
sr_dbg("zp: Setting samplerate regs (freq=%d, scale=%d): " sr_dbg("Setting samplerate regs (freq=%d, scale=%d): "
"reg0: %d, reg1: %d, reg2: %d, reg3: %d.", "reg0: %d, reg1: %d, reg2: %d, reg3: %d.",
freq, scale, f[i].div, f[i].mul, 0x02, f[i].sel); freq, scale, f[i].div, f[i].mul, 0x02, f[i].sel);
@ -399,6 +401,7 @@ SR_PRIV void analyzer_initialize(libusb_device_handle *devh)
SR_PRIV void analyzer_wait(libusb_device_handle *devh, int set, int unset) SR_PRIV void analyzer_wait(libusb_device_handle *devh, int set, int unset)
{ {
int status; int status;
while (1) { while (1) {
status = gl_reg_read(devh, DEV_STATUS); status = gl_reg_read(devh, DEV_STATUS);
if ((status & set) && ((status & unset) == 0)) if ((status & set) && ((status & unset) == 0))

View File

@ -29,11 +29,12 @@
* THE POSSIBILITY OF SUCH DAMAGE. * THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <libusb.h>
#include <stdio.h> #include <stdio.h>
#include <libusb.h>
#include "libsigrok.h" #include "libsigrok.h"
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include "gl_usb.h" #include "gl_usb.h"
#include "protocol.h"
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \ #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \
LIBUSB_RECIPIENT_INTERFACE) LIBUSB_RECIPIENT_INTERFACE)
@ -58,7 +59,7 @@ static int gl_write_address(libusb_device_handle *devh, unsigned int address)
ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR, ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR,
0, packet, 1, TIMEOUT); 0, packet, 1, TIMEOUT);
if (ret != 1) if (ret != 1)
sr_err("zp: %s: %s.", __func__, libusb_error_name(ret)); sr_err("%s: %s.", __func__, libusb_error_name(ret));
return ret; return ret;
} }
@ -70,7 +71,7 @@ static int gl_write_data(libusb_device_handle *devh, unsigned int val)
ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA, ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
0, packet, 1, TIMEOUT); 0, packet, 1, TIMEOUT);
if (ret != 1) if (ret != 1)
sr_err("zp: %s: %s.", __func__, libusb_error_name(ret)); sr_err("%s: %s.", __func__, libusb_error_name(ret));
return ret; return ret;
} }
@ -82,7 +83,7 @@ static int gl_read_data(libusb_device_handle *devh)
ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA, ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
0, packet, 1, TIMEOUT); 0, packet, 1, TIMEOUT);
if (ret != 1) if (ret != 1)
sr_err("zp: %s: %s, val=%hhx.", __func__, sr_err("%s: %s, val=%hhx.", __func__,
libusb_error_name(ret), packet[0]); libusb_error_name(ret), packet[0]);
return (ret == 1) ? packet[0] : ret; return (ret == 1) ? packet[0] : ret;
} }
@ -98,13 +99,13 @@ SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK, ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
0, packet, 8, TIMEOUT); 0, packet, 8, TIMEOUT);
if (ret != 8) if (ret != 8)
sr_err("zp: %s: libusb_control_transfer: %s.", __func__, sr_err("%s: libusb_control_transfer: %s.", __func__,
libusb_error_name(ret)); libusb_error_name(ret));
ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size, ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
&transferred, TIMEOUT); &transferred, TIMEOUT);
if (ret < 0) if (ret < 0)
sr_err("zp: %s: libusb_bulk_transfer: %s.", __func__, sr_err("%s: libusb_bulk_transfer: %s.", __func__,
libusb_error_name(ret)); libusb_error_name(ret));
return transferred; return transferred;
} }

View File

@ -0,0 +1,33 @@
/*
* This file is part of the sigrok project.
*
* Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LIBSIGROK_HARDWARE_ZEROPLUS_LOGIC_CUBE_PROTOCOL_H
#define LIBSIGROK_HARDWARE_ZEROPLUS_LOGIC_CUBE_PROTOCOL_H
/* Message logging helpers with driver-specific prefix string. */
#define DRIVER_LOG_DOMAIN "zeroplus: "
#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
#endif

View File

@ -27,6 +27,7 @@
#include "libsigrok.h" #include "libsigrok.h"
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include "analyzer.h" #include "analyzer.h"
#include "protocol.h"
#define USB_VENDOR 0x0c12 #define USB_VENDOR 0x0c12
@ -268,7 +269,7 @@ static int clear_instances(void)
sdi = l->data; sdi = l->data;
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
/* Log error, but continue cleaning up the rest. */ /* Log error, but continue cleaning up the rest. */
sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__); sr_err("%s: sdi->priv was NULL, continuing", __func__);
continue; continue;
} }
sr_usb_dev_inst_free(devc->usb); sr_usb_dev_inst_free(devc->usb);
@ -283,10 +284,6 @@ static int clear_instances(void)
return SR_OK; return SR_OK;
} }
/*
* API callbacks
*/
static int hw_init(struct sr_context *sr_ctx) static int hw_init(struct sr_context *sr_ctx)
{ {
return std_hw_init(sr_ctx, di, "zeroplus: "); return std_hw_init(sr_ctx, di, "zeroplus: ");
@ -319,7 +316,7 @@ static GSList *hw_scan(GSList *options)
for (i = 0; devlist[i]; i++) { for (i = 0; devlist[i]; i++) {
ret = libusb_get_device_descriptor(devlist[i], &des); ret = libusb_get_device_descriptor(devlist[i], &des);
if (ret != 0) { if (ret != 0) {
sr_err("zp: failed to get device descriptor: %s", sr_err("Failed to get device descriptor: %s.",
libusb_error_name(ret)); libusb_error_name(ret));
continue; continue;
} }
@ -334,19 +331,19 @@ static GSList *hw_scan(GSList *options)
/* Skip if the device was not found */ /* Skip if the device was not found */
if (!prof) if (!prof)
continue; continue;
sr_info("zp: Found ZEROPLUS model %s", prof->model_name); sr_info("Found ZEROPLUS model %s.", prof->model_name);
/* Register the device with libsigrok. */ /* Register the device with libsigrok. */
if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
VENDOR_NAME, prof->model_name, NULL))) { VENDOR_NAME, prof->model_name, NULL))) {
sr_err("zp: %s: sr_dev_inst_new failed", __func__); sr_err("%s: sr_dev_inst_new failed", __func__);
return NULL; return NULL;
} }
sdi->driver = di; sdi->driver = di;
/* Allocate memory for our private driver context. */ /* Allocate memory for our private driver context. */
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
sr_err("zp: %s: devc malloc failed", __func__); sr_err("Device context malloc failed.");
return NULL; return NULL;
} }
sdi->priv = devc; sdi->priv = devc;
@ -401,21 +398,21 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
int device_count, ret, i; int device_count, ret, i;
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
sr_err("zp: %s: sdi->priv was NULL", __func__); sr_err("%s: sdi->priv was NULL", __func__);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
&devlist); &devlist);
if (device_count < 0) { if (device_count < 0) {
sr_err("zp: Failed to retrieve device list"); sr_err("Failed to retrieve device list.");
return SR_ERR; return SR_ERR;
} }
dev = NULL; dev = NULL;
for (i = 0; i < device_count; i++) { for (i = 0; i < device_count; i++) {
if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
sr_err("zp: Failed to get device descriptor: %s.", sr_err("Failed to get device descriptor: %s.",
libusb_error_name(ret)); libusb_error_name(ret));
continue; continue;
} }
@ -426,31 +423,31 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
} }
} }
if (!dev) { if (!dev) {
sr_err("device on bus %d address %d disappeared!", sr_err("Device on bus %d address %d disappeared!",
devc->usb->bus, devc->usb->address); devc->usb->bus, devc->usb->address);
return SR_ERR; return SR_ERR;
} }
if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) { if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
sdi->status = SR_ST_ACTIVE; sdi->status = SR_ST_ACTIVE;
sr_info("zp: opened device %d on %d.%d interface %d", sr_info("Opened device %d on %d.%d interface %d.",
sdi->index, devc->usb->bus, sdi->index, devc->usb->bus,
devc->usb->address, USB_INTERFACE); devc->usb->address, USB_INTERFACE);
} else { } else {
sr_err("zp: failed to open device: %s", libusb_error_name(ret)); sr_err("Failed to open device: %s.", libusb_error_name(ret));
return SR_ERR; return SR_ERR;
} }
ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION); ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
if (ret < 0) { if (ret < 0) {
sr_err("zp: Unable to set USB configuration %d: %s", sr_err("Unable to set USB configuration %d: %s.",
USB_CONFIGURATION, libusb_error_name(ret)); USB_CONFIGURATION, libusb_error_name(ret));
return SR_ERR; return SR_ERR;
} }
ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE); ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
if (ret != 0) { if (ret != 0) {
sr_err("zp: Unable to claim interface: %s", sr_err("Unable to claim interface: %s.",
libusb_error_name(ret)); libusb_error_name(ret));
return SR_ERR; return SR_ERR;
} }
@ -491,14 +488,14 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
struct dev_context *devc; struct dev_context *devc;
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
sr_err("zp: %s: sdi->priv was NULL", __func__); sr_err("%s: sdi->priv was NULL", __func__);
return SR_ERR; return SR_ERR;
} }
if (!devc->usb->devhdl) if (!devc->usb->devhdl)
return SR_ERR; return SR_ERR;
sr_info("zp: closing device %d on %d.%d interface %d", sdi->index, sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
devc->usb->bus, devc->usb->address, USB_INTERFACE); devc->usb->bus, devc->usb->address, USB_INTERFACE);
libusb_release_interface(devc->usb->devhdl, USB_INTERFACE); libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
libusb_reset_device(devc->usb->devhdl); libusb_reset_device(devc->usb->devhdl);
@ -530,8 +527,8 @@ static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
if (sdi) { if (sdi) {
devc = sdi->priv; devc = sdi->priv;
*data = &devc->cur_samplerate; *data = &devc->cur_samplerate;
sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.", sr_spew("Returning samplerate: %" PRIu64 "Hz.",
__func__, devc->cur_samplerate); devc->cur_samplerate);
} else } else
return SR_ERR; return SR_ERR;
break; break;
@ -549,12 +546,13 @@ static int set_samplerate(struct dev_context *devc, uint64_t samplerate)
for (i = 0; supported_samplerates[i]; i++) for (i = 0; supported_samplerates[i]; i++)
if (samplerate == supported_samplerates[i]) if (samplerate == supported_samplerates[i])
break; break;
if (!supported_samplerates[i] || samplerate > devc->max_samplerate) { if (!supported_samplerates[i] || samplerate > devc->max_samplerate) {
sr_err("zp: %s: unsupported samplerate", __func__); sr_err("Unsupported samplerate.");
return SR_ERR_ARG; return SR_ERR_ARG;
} }
sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate); sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
if (samplerate >= SR_MHZ(1)) if (samplerate >= SR_MHZ(1))
analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ); analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
@ -582,7 +580,7 @@ static int set_limit_samples(struct dev_context *devc, uint64_t samples)
else else
devc->memory_size = MEMORY_SIZE_512K; devc->memory_size = MEMORY_SIZE_512K;
sr_info("zp: Setting memory size to %dK.", sr_info("Setting memory size to %dK.",
get_memory_size(devc->memory_size) / 1024); get_memory_size(devc->memory_size) / 1024);
analyzer_set_memory_size(devc->memory_size); analyzer_set_memory_size(devc->memory_size);
@ -593,13 +591,13 @@ static int set_limit_samples(struct dev_context *devc, uint64_t samples)
static int set_capture_ratio(struct dev_context *devc, uint64_t ratio) static int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
{ {
if (ratio > 100) { if (ratio > 100) {
sr_err("zp: %s: invalid capture ratio", __func__); sr_err("Invalid capture ratio: %" PRIu64 ".", ratio);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
devc->capture_ratio = ratio; devc->capture_ratio = ratio;
sr_info("zp: Setting capture ratio to %d%%.", devc->capture_ratio); sr_info("Setting capture ratio to %d%%.", devc->capture_ratio);
return SR_OK; return SR_OK;
} }
@ -609,12 +607,12 @@ static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
struct dev_context *devc; struct dev_context *devc;
if (!sdi) { if (!sdi) {
sr_err("zp: %s: sdi was NULL", __func__); sr_err("%s: sdi was NULL", __func__);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
sr_err("zp: %s: sdi->priv was NULL", __func__); sr_err("%s: sdi->priv was NULL", __func__);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
@ -632,7 +630,6 @@ static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
{ {
(void)sdi; (void)sdi;
switch (key) { switch (key) {
@ -676,8 +673,8 @@ static void set_triggerbar(struct dev_context *devc)
analyzer_set_triggerbar_address(triggerbar); analyzer_set_triggerbar_address(triggerbar);
analyzer_set_ramsize_trigger_address(ramsize - triggerbar); analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
sr_dbg("zp: triggerbar_address = %d(0x%x)", triggerbar, triggerbar); sr_dbg("triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
sr_dbg("zp: ramsize_triggerbar_address = %d(0x%x)", sr_dbg("ramsize_triggerbar_address = %d(0x%x)",
ramsize - triggerbar, ramsize - triggerbar); ramsize - triggerbar, ramsize - triggerbar);
} }
@ -695,12 +692,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
struct dev_context *devc; struct dev_context *devc;
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
sr_err("zp: %s: sdi->priv was NULL", __func__); sr_err("%s: sdi->priv was NULL", __func__);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
if (configure_probes(sdi) != SR_OK) { if (configure_probes(sdi) != SR_OK) {
sr_err("zp: failed to configured probes"); sr_err("Failed to configure probes.");
return SR_ERR; return SR_ERR;
} }
@ -710,14 +707,14 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
analyzer_configure(devc->usb->devhdl); analyzer_configure(devc->usb->devhdl);
analyzer_start(devc->usb->devhdl); analyzer_start(devc->usb->devhdl);
sr_info("zp: Waiting for data"); sr_info("Waiting for data.");
analyzer_wait_data(devc->usb->devhdl); analyzer_wait_data(devc->usb->devhdl);
sr_info("zp: Stop address = 0x%x", sr_info("Stop address = 0x%x.",
analyzer_get_stop_address(devc->usb->devhdl)); analyzer_get_stop_address(devc->usb->devhdl));
sr_info("zp: Now address = 0x%x", sr_info("Now address = 0x%x.",
analyzer_get_now_address(devc->usb->devhdl)); analyzer_get_now_address(devc->usb->devhdl));
sr_info("zp: Trigger address = 0x%x", sr_info("Trigger address = 0x%x.",
analyzer_get_trigger_address(devc->usb->devhdl)); analyzer_get_trigger_address(devc->usb->devhdl));
packet.type = SR_DF_HEADER; packet.type = SR_DF_HEADER;
@ -727,7 +724,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
sr_session_send(cb_data, &packet); sr_session_send(cb_data, &packet);
if (!(buf = g_try_malloc(PACKET_SIZE))) { if (!(buf = g_try_malloc(PACKET_SIZE))) {
sr_err("zp: %s: buf malloc failed", __func__); sr_err("Packet buffer malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
@ -739,7 +736,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
n = devc->max_memory_size * 4; n = devc->max_memory_size * 4;
for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) { for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE); res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
sr_info("zp: Tried to read %d bytes, actually read %d bytes", sr_info("Tried to read %d bytes, actually read %d bytes.",
PACKET_SIZE, res); PACKET_SIZE, res);
packet.type = SR_DF_LOGIC; packet.type = SR_DF_LOGIC;
@ -769,7 +766,7 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
sr_session_send(cb_data, &packet); sr_session_send(cb_data, &packet);
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
sr_err("zp: %s: sdi->priv was NULL", __func__); sr_err("%s: sdi->priv was NULL", __func__);
return SR_ERR_BUG; return SR_ERR_BUG;
} }