hantek-dso: Use message logging helpers.

This commit is contained in:
Uwe Hermann 2012-11-10 18:19:30 +01:00
parent 47f4f073e0
commit e98b7f1be0
3 changed files with 191 additions and 176 deletions

View File

@ -33,10 +33,9 @@
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include "dso.h" #include "dso.h"
/* Max time in ms before we want to check on USB events */ /* Max time in ms before we want to check on USB events */
/* TODO tune this properly */ /* TODO tune this properly */
#define TICK 1 #define TICK 1
static const int hwcaps[] = { static const int hwcaps[] = {
SR_HWCAP_OSCILLOSCOPE, SR_HWCAP_OSCILLOSCOPE,
@ -145,6 +144,7 @@ static const char *coupling[] = {
SR_PRIV struct sr_dev_driver hantek_dso_driver_info; SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
static struct sr_dev_driver *hdi = &hantek_dso_driver_info; static struct sr_dev_driver *hdi = &hantek_dso_driver_info;
static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof) static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
@ -161,7 +161,8 @@ static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof
return NULL; return NULL;
sdi->driver = hdi; sdi->driver = hdi;
/* Add only the real probes -- EXT isn't a source of data, only /*
* Add only the real probes -- EXT isn't a source of data, only
* a trigger source internal to the device. * a trigger source internal to the device.
*/ */
for (i = 0; probe_names[i]; i++) { for (i = 0; probe_names[i]; i++) {
@ -172,9 +173,10 @@ static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof
} }
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
sr_err("hantek-dso: devc malloc failed"); sr_err("Device context malloc failed.");
return NULL; return NULL;
} }
devc->profile = prof; devc->profile = prof;
devc->dev_state = IDLE; devc->dev_state = IDLE;
devc->timebase = DEFAULT_TIMEBASE; devc->timebase = DEFAULT_TIMEBASE;
@ -230,12 +232,12 @@ static int clear_instances(void)
for (l = drvc->instances; l; l = l->next) { for (l = drvc->instances; l; l = l->next) {
if (!(sdi = l->data)) { if (!(sdi = l->data)) {
/* Log error, but continue cleaning up the rest. */ /* Log error, but continue cleaning up the rest. */
sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__); sr_err("%s: sdi was NULL, continuing", __func__);
continue; continue;
} }
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("hantek-dso: %s: sdi->priv was NULL, continuing", __func__); sr_err("%s: sdi->priv was NULL, continuing", __func__);
continue; continue;
} }
dso_close(sdi); dso_close(sdi);
@ -256,13 +258,13 @@ static int hw_init(void)
struct drv_context *drvc; struct drv_context *drvc;
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("hantek-dso: driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
if (libusb_init(NULL) != 0) { if (libusb_init(NULL) != 0) {
g_free(drvc); g_free(drvc);
sr_err("hantek-dso: Failed to initialize USB."); sr_err("Failed to initialize USB.");
return SR_ERR; return SR_ERR;
} }
@ -283,6 +285,7 @@ static GSList *hw_scan(GSList *options)
int devcnt, ret, i, j; int devcnt, ret, i, j;
(void)options; (void)options;
devcnt = 0; devcnt = 0;
devices = 0; devices = 0;
drvc = hdi->priv; drvc = hdi->priv;
@ -294,7 +297,7 @@ static GSList *hw_scan(GSList *options)
libusb_get_device_list(NULL, &devlist); libusb_get_device_list(NULL, &devlist);
for (i = 0; devlist[i]; i++) { for (i = 0; devlist[i]; i++) {
if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
sr_err("hantek-dso: failed to get device descriptor: %d", ret); sr_err("Failed to get device descriptor: %d.", ret);
continue; continue;
} }
@ -304,7 +307,7 @@ static GSList *hw_scan(GSList *options)
&& des.idProduct == dev_profiles[j].orig_pid) { && des.idProduct == dev_profiles[j].orig_pid) {
/* Device matches the pre-firmware profile. */ /* Device matches the pre-firmware profile. */
prof = &dev_profiles[j]; prof = &dev_profiles[j];
sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model); sr_dbg("Found a %s %s.", prof->vendor, prof->model);
sdi = dso_dev_new(devcnt, prof); sdi = dso_dev_new(devcnt, prof);
devices = g_slist_append(devices, sdi); devices = g_slist_append(devices, sdi);
devc = sdi->priv; devc = sdi->priv;
@ -313,8 +316,8 @@ static GSList *hw_scan(GSList *options)
/* Remember when the firmware on this device was updated */ /* Remember when the firmware on this device was updated */
devc->fw_updated = g_get_monotonic_time(); devc->fw_updated = g_get_monotonic_time();
else else
sr_err("hantek-dso: firmware upload failed for " sr_err("Firmware upload failed for "
"device %d", devcnt); "device %d.", devcnt);
/* Dummy USB address of 0xff will get overwritten later. */ /* Dummy USB address of 0xff will get overwritten later. */
devc->usb = sr_usb_dev_inst_new( devc->usb = sr_usb_dev_inst_new(
libusb_get_bus_number(devlist[i]), 0xff, NULL); libusb_get_bus_number(devlist[i]), 0xff, NULL);
@ -324,7 +327,7 @@ static GSList *hw_scan(GSList *options)
&& des.idProduct == dev_profiles[j].fw_pid) { && des.idProduct == dev_profiles[j].fw_pid) {
/* Device matches the post-firmware profile. */ /* Device matches the post-firmware profile. */
prof = &dev_profiles[j]; prof = &dev_profiles[j];
sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model); sr_dbg("Found a %s %s.", prof->vendor, prof->model);
sdi = dso_dev_new(devcnt, prof); sdi = dso_dev_new(devcnt, prof);
sdi->status = SR_ST_INACTIVE; sdi->status = SR_ST_INACTIVE;
devices = g_slist_append(devices, sdi); devices = g_slist_append(devices, sdi);
@ -363,13 +366,13 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
devc = sdi->priv; devc = sdi->priv;
/* /*
* if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
* for the FX2 to renumerate * for the FX2 to renumerate.
*/ */
err = SR_ERR; err = SR_ERR;
if (devc->fw_updated > 0) { if (devc->fw_updated > 0) {
sr_info("hantek-dso: waiting for device to reset"); sr_info("Waiting for device to reset.");
/* takes at least 300ms for the FX2 to be gone from the USB bus */ /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
g_usleep(300 * 1000); g_usleep(300 * 1000);
timediff_ms = 0; timediff_ms = 0;
while (timediff_ms < MAX_RENUM_DELAY_MS) { while (timediff_ms < MAX_RENUM_DELAY_MS) {
@ -378,21 +381,21 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
g_usleep(100 * 1000); g_usleep(100 * 1000);
timediff_us = g_get_monotonic_time() - devc->fw_updated; timediff_us = g_get_monotonic_time() - devc->fw_updated;
timediff_ms = timediff_us / 1000; timediff_ms = timediff_us / 1000;
sr_spew("hantek-dso: waited %" PRIi64 " ms", timediff_ms); sr_spew("Waited %" PRIi64 " ms.", timediff_ms);
} }
sr_info("hantek-dso: device came back after %d ms", timediff_ms); sr_info("Device came back after %d ms.", timediff_ms);
} else { } else {
err = dso_open(sdi); err = dso_open(sdi);
} }
if (err != SR_OK) { if (err != SR_OK) {
sr_err("hantek-dso: unable to open device"); sr_err("Unable to open device.");
return SR_ERR; return SR_ERR;
} }
err = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE); err = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
if (err != 0) { if (err != 0) {
sr_err("hantek-dso: Unable to claim interface: %d", err); sr_err("Unable to claim interface: %d.", err);
return SR_ERR; return SR_ERR;
} }
@ -401,7 +404,6 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
static int hw_dev_close(struct sr_dev_inst *sdi) static int hw_dev_close(struct sr_dev_inst *sdi)
{ {
dso_close(sdi); dso_close(sdi);
return SR_OK; return SR_OK;
@ -422,7 +424,7 @@ static int hw_cleanup(void)
} }
static int hw_info_get(int info_id, const void **data, static int hw_info_get(int info_id, const void **data,
const struct sr_dev_inst *sdi) const struct sr_dev_inst *sdi)
{ {
uint64_t tmp; uint64_t tmp;
@ -468,7 +470,7 @@ static int hw_info_get(int info_id, const void **data,
} }
static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
const void *value) const void *value)
{ {
struct dev_context *devc; struct dev_context *devc;
struct sr_rational tmp_rat; struct sr_rational tmp_rat;
@ -495,7 +497,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
case SR_HWCAP_HORIZ_TRIGGERPOS: case SR_HWCAP_HORIZ_TRIGGERPOS:
tmp_float = *(const float *)value; tmp_float = *(const float *)value;
if (tmp_float < 0.0 || tmp_float > 1.0) { if (tmp_float < 0.0 || tmp_float > 1.0) {
sr_err("hantek-dso: trigger position should be between 0.0 and 1.0"); sr_err("Trigger position should be between 0.0 and 1.0.");
ret = SR_ERR_ARG; ret = SR_ERR_ARG;
} else } else
devc->triggerposition = tmp_float; devc->triggerposition = tmp_float;
@ -547,14 +549,14 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
else if (!strcmp(targets[i], "TRIGGER")) else if (!strcmp(targets[i], "TRIGGER"))
devc->filter_trigger = TRUE; devc->filter_trigger = TRUE;
else { else {
sr_err("invalid filter target %s", targets[i]); sr_err("Invalid filter target %s.", targets[i]);
ret = SR_ERR_ARG; ret = SR_ERR_ARG;
} }
} }
g_strfreev(targets); g_strfreev(targets);
break; break;
case SR_HWCAP_VDIV: case SR_HWCAP_VDIV:
/* TODO not supporting vdiv per channel yet */ /* TODO: Not supporting vdiv per channel yet. */
tmp_rat = *(const struct sr_rational *)value; tmp_rat = *(const struct sr_rational *)value;
for (i = 0; vdivs[i].p && vdivs[i].q; i++) { for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
if (vdivs[i].p == tmp_rat.p if (vdivs[i].p == tmp_rat.p
@ -568,7 +570,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
ret = SR_ERR_ARG; ret = SR_ERR_ARG;
break; break;
case SR_HWCAP_COUPLING: case SR_HWCAP_COUPLING:
/* TODO not supporting coupling per channel yet */ /* TODO: Not supporting coupling per channel yet. */
for (i = 0; coupling[i]; i++) { for (i = 0; coupling[i]; i++) {
if (!strcmp(value, coupling[i])) { if (!strcmp(value, coupling[i])) {
devc->coupling_ch1 = i; devc->coupling_ch1 = i;
@ -581,6 +583,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
break; break;
default: default:
ret = SR_ERR_ARG; ret = SR_ERR_ARG;
break;
} }
return ret; return ret;
@ -605,16 +608,19 @@ static void send_chunk(struct dev_context *devc, unsigned char *buf,
analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes); analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
data_offset = 0; data_offset = 0;
for (i = 0; i < analog.num_samples; i++) { for (i = 0; i < analog.num_samples; i++) {
/* The device always sends data for both channels. If a channel /*
* The device always sends data for both channels. If a channel
* is disabled, it contains a copy of the enabled channel's * is disabled, it contains a copy of the enabled channel's
* data. However, we only send the requested channels to the bus. * data. However, we only send the requested channels to
* the bus.
* *
* Voltage values are encoded as a value 0-255 (0-512 on the 5200*), * Voltage values are encoded as a value 0-255 (0-512 on the
* where the value is a point in the range represented by the vdiv * DSO-5200*), where the value is a point in the range
* setting. There are 8 vertical divs, so e.g. 500mV/div represents * represented by the vdiv setting. There are 8 vertical divs,
* 4V peak-to-peak where 0 = -2V and 255 = +2V. * so e.g. 500mV/div represents 4V peak-to-peak where 0 = -2V
* and 255 = +2V.
*/ */
/* TODO: support for 5xxx series 9-bit samples */ /* TODO: Support for DSO-5xxx series 9-bit samples. */
if (devc->ch1_enabled) { if (devc->ch1_enabled) {
range = ((float)vdivs[devc->voltage_ch1].p / vdivs[devc->voltage_ch1].q) * 8; range = ((float)vdivs[devc->voltage_ch1].p / vdivs[devc->voltage_ch1].q) * 8;
ch1 = range / 255 * *(buf + i * 2 + 1); ch1 = range / 255 * *(buf + i * 2 + 1);
@ -630,12 +636,12 @@ static void send_chunk(struct dev_context *devc, unsigned char *buf,
} }
} }
sr_session_send(devc->cb_data, &packet); sr_session_send(devc->cb_data, &packet);
} }
/* Called by libusb (as triggered by handle_event()) when a transfer comes in. /*
* Called by libusb (as triggered by handle_event()) when a transfer comes in.
* Only channel data comes in asynchronously, and all transfers for this are * Only channel data comes in asynchronously, and all transfers for this are
* queued up beforehand, so this just needs so chuck the incoming data onto * queued up beforehand, so this just needs to chuck the incoming data onto
* the libsigrok session bus. * the libsigrok session bus.
*/ */
static void receive_transfer(struct libusb_transfer *transfer) static void receive_transfer(struct libusb_transfer *transfer)
@ -645,8 +651,8 @@ static void receive_transfer(struct libusb_transfer *transfer)
int num_samples, pre; int num_samples, pre;
devc = transfer->user_data; devc = transfer->user_data;
sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes", sr_dbg("receive_transfer(): status %d received %d bytes.",
transfer->status, transfer->actual_length); transfer->status, transfer->actual_length);
if (transfer->actual_length == 0) if (transfer->actual_length == 0)
/* Nothing to send to the bus. */ /* Nothing to send to the bus. */
@ -654,15 +660,17 @@ static void receive_transfer(struct libusb_transfer *transfer)
num_samples = transfer->actual_length / 2; num_samples = transfer->actual_length / 2;
sr_dbg("hantek-dso: got %d-%d/%d samples in frame", devc->samp_received + 1, sr_dbg("Got %d-%d/%d samples in frame.", devc->samp_received + 1,
devc->samp_received + num_samples, devc->framesize); devc->samp_received + num_samples, devc->framesize);
/* The device always sends a full frame, but the beginning of the frame /*
* The device always sends a full frame, but the beginning of the frame
* doesn't represent the trigger point. The offset at which the trigger * doesn't represent the trigger point. The offset at which the trigger
* happened came in with the capture state, so we need to start sending * happened came in with the capture state, so we need to start sending
* from there up the session bus. The samples in the frame buffer before * from there up the session bus. The samples in the frame buffer
* that trigger point came after the end of the device's frame buffer was * before that trigger point came after the end of the device's frame
* reached, and it wrapped around to overwrite up until the trigger point. * buffer was reached, and it wrapped around to overwrite up until the
* trigger point.
*/ */
if (devc->samp_received < devc->trigger_offset) { if (devc->samp_received < devc->trigger_offset) {
/* Trigger point not yet reached. */ /* Trigger point not yet reached. */
@ -672,17 +680,19 @@ static void receive_transfer(struct libusb_transfer *transfer)
transfer->buffer, num_samples * 2); transfer->buffer, num_samples * 2);
devc->samp_buffered += num_samples; devc->samp_buffered += num_samples;
} else { } else {
/* This chunk hits or overruns the trigger point. /*
* This chunk hits or overruns the trigger point.
* Store the part before the trigger fired, and * Store the part before the trigger fired, and
* send the rest up to the session bus. */ * send the rest up to the session bus.
*/
pre = devc->trigger_offset - devc->samp_received; pre = devc->trigger_offset - devc->samp_received;
memcpy(devc->framebuf + devc->samp_buffered * 2, memcpy(devc->framebuf + devc->samp_buffered * 2,
transfer->buffer, pre * 2); transfer->buffer, pre * 2);
devc->samp_buffered += pre; devc->samp_buffered += pre;
/* The rest of this chunk starts with the trigger point. */ /* The rest of this chunk starts with the trigger point. */
sr_dbg("hantek-dso: reached trigger point, %d samples buffered", sr_dbg("Reached trigger point, %d samples buffered.",
devc->samp_buffered); devc->samp_buffered);
/* Avoid the corner case where the chunk ended at /* Avoid the corner case where the chunk ended at
* exactly the trigger point. */ * exactly the trigger point. */
@ -706,8 +716,8 @@ static void receive_transfer(struct libusb_transfer *transfer)
if (devc->samp_received >= devc->framesize) { if (devc->samp_received >= devc->framesize) {
/* That was the last chunk in this frame. Send the buffered /* That was the last chunk in this frame. Send the buffered
* pre-trigger samples out now, in one big chunk. */ * pre-trigger samples out now, in one big chunk. */
sr_dbg("hantek-dso: end of frame, sending %d pre-trigger buffered samples", sr_dbg("End of frame, sending %d pre-trigger buffered samples.",
devc->samp_buffered); devc->samp_buffered);
send_chunk(devc, devc->framebuf, devc->samp_buffered); send_chunk(devc, devc->framebuf, devc->samp_buffered);
/* Mark the end of this frame. */ /* Mark the end of this frame. */
@ -721,7 +731,6 @@ static void receive_transfer(struct libusb_transfer *transfer)
devc->dev_state = NEW_CAPTURE; devc->dev_state = NEW_CAPTURE;
} }
} }
} }
static int handle_event(int fd, int revents, void *cb_data) static int handle_event(int fd, int revents, void *cb_data)
@ -735,7 +744,6 @@ static int handle_event(int fd, int revents, void *cb_data)
uint32_t trigger_offset; uint32_t trigger_offset;
uint8_t capturestate; uint8_t capturestate;
/* Avoid compiler warnings. */
(void)fd; (void)fd;
(void)revents; (void)revents;
@ -743,9 +751,11 @@ static int handle_event(int fd, int revents, void *cb_data)
devc = sdi->priv; devc = sdi->priv;
if (devc->dev_state == STOPPING) { if (devc->dev_state == STOPPING) {
/* We've been told to wind up the acquisition. */ /* We've been told to wind up the acquisition. */
sr_dbg("hantek-dso: stopping acquisition"); sr_dbg("Stopping acquisition.");
/* TODO: doesn't really cancel pending transfers so they might /*
* come in after SR_DF_END is sent. */ * TODO: Doesn't really cancel pending transfers so they might
* come in after SR_DF_END is sent.
*/
lupfd = libusb_get_pollfds(NULL); lupfd = libusb_get_pollfds(NULL);
for (i = 0; lupfd[i]; i++) for (i = 0; lupfd[i]; i++)
sr_source_remove(lupfd[i]->fd); sr_source_remove(lupfd[i]->fd);
@ -771,7 +781,7 @@ static int handle_event(int fd, int revents, void *cb_data)
return TRUE; return TRUE;
// if (dso_force_trigger(devc) != SR_OK) // if (dso_force_trigger(devc) != SR_OK)
// return TRUE; // return TRUE;
sr_dbg("hantek-dso: successfully requested next chunk"); sr_dbg("Successfully requested next chunk.");
devc->dev_state = CAPTURE; devc->dev_state = CAPTURE;
return TRUE; return TRUE;
} }
@ -781,8 +791,8 @@ static int handle_event(int fd, int revents, void *cb_data)
if ((dso_get_capturestate(devc, &capturestate, &trigger_offset)) != SR_OK) if ((dso_get_capturestate(devc, &capturestate, &trigger_offset)) != SR_OK)
return TRUE; return TRUE;
sr_dbg("hantek-dso: capturestate %d", capturestate); sr_dbg("Capturestate %d.", capturestate);
sr_dbg("hantek-dso: trigger offset 0x%.6x", trigger_offset); sr_dbg("Trigger offset 0x%.6x.", trigger_offset);
switch (capturestate) { switch (capturestate) {
case CAPTURE_EMPTY: case CAPTURE_EMPTY:
if (++devc->capture_empty_count >= MAX_CAPTURE_EMPTY) { if (++devc->capture_empty_count >= MAX_CAPTURE_EMPTY) {
@ -793,11 +803,11 @@ static int handle_event(int fd, int revents, void *cb_data)
break; break;
// if (dso_force_trigger(devc) != SR_OK) // if (dso_force_trigger(devc) != SR_OK)
// break; // break;
sr_dbg("hantek-dso: successfully requested next chunk"); sr_dbg("Successfully requested next chunk.");
} }
break; break;
case CAPTURE_FILLING: case CAPTURE_FILLING:
/* no data yet */ /* No data yet. */
break; break;
case CAPTURE_READY_8BIT: case CAPTURE_READY_8BIT:
/* Remember where in the captured frame the trigger is. */ /* Remember where in the captured frame the trigger is. */
@ -812,7 +822,8 @@ static int handle_event(int fd, int revents, void *cb_data)
if (dso_get_channeldata(devc, receive_transfer) != SR_OK) if (dso_get_channeldata(devc, receive_transfer) != SR_OK)
break; break;
/* Don't hit the state machine again until we're done fetching /*
* Don't hit the state machine again until we're done fetching
* the data we just told the scope to send. * the data we just told the scope to send.
*/ */
devc->dev_state = FETCH_DATA; devc->dev_state = FETCH_DATA;
@ -823,20 +834,21 @@ static int handle_event(int fd, int revents, void *cb_data)
break; break;
case CAPTURE_READY_9BIT: case CAPTURE_READY_9BIT:
/* TODO */ /* TODO */
sr_err("not yet supported"); sr_err("Not yet supported.");
break; break;
case CAPTURE_TIMEOUT: case CAPTURE_TIMEOUT:
/* Doesn't matter, we'll try again next time. */ /* Doesn't matter, we'll try again next time. */
break; break;
default: default:
sr_dbg("unknown capture state"); sr_dbg("Unknown capture state: %d.", capturestate);
break;
} }
return TRUE; return TRUE;
} }
static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
void *cb_data) void *cb_data)
{ {
const struct libusb_pollfd **lupfd; const struct libusb_pollfd **lupfd;
struct sr_datafeed_packet packet; struct sr_datafeed_packet packet;
@ -852,7 +864,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
devc->cb_data = cb_data; devc->cb_data = cb_data;
if (configure_probes(sdi) != SR_OK) { if (configure_probes(sdi) != SR_OK) {
sr_err("hantek-dso: failed to configured probes"); sr_err("Failed to configure probes.");
return SR_ERR; return SR_ERR;
} }
@ -865,8 +877,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
devc->dev_state = CAPTURE; devc->dev_state = CAPTURE;
lupfd = libusb_get_pollfds(NULL); lupfd = libusb_get_pollfds(NULL);
for (i = 0; lupfd[i]; i++) for (i = 0; lupfd[i]; i++)
sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event, sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK,
(void *)sdi); handle_event, (void *)sdi);
free(lupfd); free(lupfd);
/* Send header packet to the session bus. */ /* Send header packet to the session bus. */

View File

@ -28,19 +28,18 @@
extern struct sr_dev_driver hantek_dso_driver_info; extern struct sr_dev_driver hantek_dso_driver_info;
static int send_begin(struct dev_context *devc) static int send_begin(struct dev_context *devc)
{ {
int ret; int ret;
unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe, unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
0x00, 0x01, 0x00}; 0x00, 0x01, 0x00};
sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND"); sr_dbg("Sending CTRL_BEGINCOMMAND.");
if ((ret = libusb_control_transfer(devc->usb->devhdl, if ((ret = libusb_control_transfer(devc->usb->devhdl,
LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND, LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) { 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
sr_err("failed to send begincommand: %d", ret); sr_err("Failed to send begincommand: %d.", ret);
return SR_ERR; return SR_ERR;
} }
@ -119,7 +118,7 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
libusb_get_device_list(NULL, &devlist); libusb_get_device_list(NULL, &devlist);
for (i = 0; devlist[i]; i++) { for (i = 0; devlist[i]; i++) {
if ((err = libusb_get_device_descriptor(devlist[i], &des))) { if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
sr_err("hantek-dso: failed to get device descriptor: %d", err); sr_err("Failed to get device descriptor: %d.", err);
continue; continue;
} }
@ -152,19 +151,19 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
*/ */
devc->usb->address = libusb_get_device_address(devlist[i]); devc->usb->address = libusb_get_device_address(devlist[i]);
if(!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
sr_err("hantek-dso: wrong endpoint profile"); sr_err("Wrong endpoint profile.");
else { else {
sdi->status = SR_ST_ACTIVE; sdi->status = SR_ST_ACTIVE;
sr_info("hantek-dso: 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("hantek-dso: failed to open device: %d", err); sr_err("Failed to open device: %d.", err);
} }
/* if we made it here, we handled the device one way or another */ /* If we made it here, we handled the device (somehow). */
break; break;
} }
libusb_free_device_list(devlist, 1); libusb_free_device_list(devlist, 1);
@ -184,7 +183,7 @@ SR_PRIV void dso_close(struct sr_dev_inst *sdi)
if (devc->usb->devhdl == NULL) if (devc->usb->devhdl == NULL)
return; return;
sr_info("hantek-dso: 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_close(devc->usb->devhdl); libusb_close(devc->usb->devhdl);
@ -198,7 +197,7 @@ static int get_channel_offsets(struct dev_context *devc)
GString *gs; GString *gs;
int chan, v, ret; int chan, v, ret;
sr_dbg("hantek-dso: getting channel offsets"); sr_dbg("Getting channel offsets.");
ret = libusb_control_transfer(devc->usb->devhdl, ret = libusb_control_transfer(devc->usb->devhdl,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
@ -206,7 +205,7 @@ static int get_channel_offsets(struct dev_context *devc)
(unsigned char *)&devc->channel_levels, (unsigned char *)&devc->channel_levels,
sizeof(devc->channel_levels), 200); sizeof(devc->channel_levels), 200);
if (ret != sizeof(devc->channel_levels)) { if (ret != sizeof(devc->channel_levels)) {
sr_err("failed to get channel offsets: %d", ret); sr_err("Failed to get channel offsets: %d.", ret);
return SR_ERR; return SR_ERR;
} }
@ -217,19 +216,21 @@ static int get_channel_offsets(struct dev_context *devc)
*/ */
for (chan = 0; chan < 2; chan++) { for (chan = 0; chan < 2; chan++) {
for (v = 0; v < 9; v++) { for (v = 0; v < 9; v++) {
devc->channel_levels[chan][v][0] = g_ntohs(devc->channel_levels[chan][v][0]); devc->channel_levels[chan][v][0] =
devc->channel_levels[chan][v][1] = g_ntohs(devc->channel_levels[chan][v][1]); g_ntohs(devc->channel_levels[chan][v][0]);
devc->channel_levels[chan][v][1] =
g_ntohs(devc->channel_levels[chan][v][1]);
} }
} }
if (sr_log_loglevel_get() >= SR_LOG_DBG) { if (sr_log_loglevel_get() >= SR_LOG_DBG) {
gs = g_string_sized_new(128); gs = g_string_sized_new(128);
for (chan = 0; chan < 2; chan++) { for (chan = 0; chan < 2; chan++) {
g_string_printf(gs, "hantek-dso: CH%d:", chan + 1); g_string_printf(gs, "CH%d:", chan + 1);
for (v = 0; v < 9; v++) { for (v = 0; v < 9; v++) {
g_string_append_printf(gs, " %.4x-%.4x", g_string_append_printf(gs, " %.4x-%.4x",
devc->channel_levels[chan][v][0], devc->channel_levels[chan][v][0],
devc->channel_levels[chan][v][1]); devc->channel_levels[chan][v][1]);
} }
sr_dbg(gs->str); sr_dbg(gs->str);
} }
@ -244,18 +245,18 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
int ret, tmp; int ret, tmp;
uint8_t cmdstring[12]; uint8_t cmdstring[12];
uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce, uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 }; 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE"); sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
memset(cmdstring, 0, sizeof(cmdstring)); memset(cmdstring, 0, sizeof(cmdstring));
/* Command */ /* Command */
cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
/* Trigger source */ /* Trigger source */
sr_dbg("hantek-dso: trigger source %s", devc->triggersource); sr_dbg("Trigger source %s.", devc->triggersource);
if (!strcmp("CH2", devc->triggersource)) if (!strcmp("CH2", devc->triggersource))
tmp = 0; tmp = 0;
else if (!strcmp("CH1", devc->triggersource)) else if (!strcmp("CH1", devc->triggersource))
@ -263,17 +264,17 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
else if (!strcmp("EXT", devc->triggersource)) else if (!strcmp("EXT", devc->triggersource))
tmp = 2; tmp = 2;
else { else {
sr_err("hantek-dso: invalid trigger source %s", devc->triggersource); sr_err("Invalid trigger source: '%s'.", devc->triggersource);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
cmdstring[2] = tmp; cmdstring[2] = tmp;
/* Frame size */ /* Frame size */
sr_dbg("hantek-dso: frame size %d", devc->framesize); sr_dbg("Frame size: %d.", devc->framesize);
cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
/* Timebase fast */ /* Timebase fast */
sr_dbg("hantek-dso: time base index %d", devc->timebase); sr_dbg("Time base index: %d.", devc->timebase);
switch (devc->framesize) { switch (devc->framesize) {
case FRAMESIZE_SMALL: case FRAMESIZE_SMALL:
if (devc->timebase < TIME_20us) if (devc->timebase < TIME_20us)
@ -289,7 +290,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
break; break;
case FRAMESIZE_LARGE: case FRAMESIZE_LARGE:
if (devc->timebase < TIME_40us) { if (devc->timebase < TIME_40us) {
sr_err("hantek-dso: timebase < 40us only supported with 10K buffer"); sr_err("Timebase < 40us only supported with 10K buffer.");
return SR_ERR_ARG; return SR_ERR_ARG;
} }
else if (devc->timebase == TIME_40us) else if (devc->timebase == TIME_40us)
@ -305,18 +306,18 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
cmdstring[2] |= (tmp & 0x07) << 5; cmdstring[2] |= (tmp & 0x07) << 5;
/* Enabled channels: 00=CH1 01=CH2 10=both */ /* Enabled channels: 00=CH1 01=CH2 10=both */
sr_dbg("hantek-dso: channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled); sr_dbg("Channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled);
tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1; tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1;
cmdstring[3] = tmp; cmdstring[3] = tmp;
/* Fast rates channel */ /* Fast rates channel */
/* TODO: is this right? */ /* TODO: Is this right? */
tmp = devc->timebase < TIME_10us ? 1 : 0; tmp = devc->timebase < TIME_10us ? 1 : 0;
cmdstring[3] |= tmp << 2; cmdstring[3] |= tmp << 2;
/* Trigger slope: 0=positive 1=negative */ /* Trigger slope: 0=positive 1=negative */
/* TODO: does this work? */ /* TODO: Does this work? */
sr_dbg("hantek-dso: trigger slope %d", devc->triggerslope); sr_dbg("Trigger slope: %d.", devc->triggerslope);
cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
/* Timebase slow */ /* Timebase slow */
@ -334,7 +335,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
cmdstring[5] = (tmp >> 8) & 0xff; cmdstring[5] = (tmp >> 8) & 0xff;
/* Horizontal trigger position */ /* Horizontal trigger position */
sr_dbg("hantek-dso: trigger position %3.2f", devc->triggerposition); sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
tmp = 0x77fff + 0x8000 * devc->triggerposition; tmp = 0x77fff + 0x8000 * devc->triggerposition;
cmdstring[6] = tmp & 0xff; cmdstring[6] = tmp & 0xff;
cmdstring[7] = (tmp >> 8) & 0xff; cmdstring[7] = (tmp >> 8) & 0xff;
@ -347,10 +348,10 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
cmdstring, sizeof(cmdstring), cmdstring, sizeof(cmdstring),
&tmp, 100)) != 0) { &tmp, 100)) != 0) {
sr_err("Failed to set trigger/samplerate: %d", ret); sr_err("Failed to set trigger/samplerate: %d.", ret);
return SR_ERR; return SR_ERR;
} }
sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE"); sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
return SR_OK; return SR_OK;
} }
@ -360,22 +361,22 @@ SR_PRIV int dso_set_filters(struct dev_context *devc)
int ret, tmp; int ret, tmp;
uint8_t cmdstring[8]; uint8_t cmdstring[8];
sr_dbg("hantek-dso: preparing CMD_SET_FILTERS"); sr_dbg("Preparing CMD_SET_FILTERS.");
memset(cmdstring, 0, sizeof(cmdstring)); memset(cmdstring, 0, sizeof(cmdstring));
cmdstring[0] = CMD_SET_FILTERS; cmdstring[0] = CMD_SET_FILTERS;
cmdstring[1] = 0x0f; cmdstring[1] = 0x0f;
if (devc->filter_ch1) { if (devc->filter_ch1) {
sr_dbg("hantek-dso: turning on CH1 filter"); sr_dbg("Turning on CH1 filter.");
cmdstring[2] |= 0x80; cmdstring[2] |= 0x80;
} }
if (devc->filter_ch2) { if (devc->filter_ch2) {
sr_dbg("hantek-dso: turning on CH2 filter"); sr_dbg("Turning on CH2 filter.");
cmdstring[2] |= 0x40; cmdstring[2] |= 0x40;
} }
if (devc->filter_trigger) { if (devc->filter_trigger) {
/* TODO: supported on the DSO-2090? */ /* TODO: supported on the DSO-2090? */
sr_dbg("hantek-dso: turning on trigger filter"); sr_dbg("Turning on trigger filter.");
cmdstring[2] |= 0x20; cmdstring[2] |= 0x20;
} }
@ -389,7 +390,7 @@ SR_PRIV int dso_set_filters(struct dev_context *devc)
sr_err("Failed to set filters: %d", ret); sr_err("Failed to set filters: %d", ret);
return SR_ERR; return SR_ERR;
} }
sr_dbg("hantek-dso: sent CMD_SET_FILTERS"); sr_dbg("Sent CMD_SET_FILTERS.");
return SR_OK; return SR_OK;
} }
@ -399,7 +400,7 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc)
int ret, tmp; int ret, tmp;
uint8_t cmdstring[8]; uint8_t cmdstring[8];
sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE"); sr_dbg("Preparing CMD_SET_VOLTAGE.");
memset(cmdstring, 0, sizeof(cmdstring)); memset(cmdstring, 0, sizeof(cmdstring));
cmdstring[0] = CMD_SET_VOLTAGE; cmdstring[0] = CMD_SET_VOLTAGE;
@ -407,7 +408,7 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc)
cmdstring[2] = 0x30; cmdstring[2] = 0x30;
/* CH1 volts/div is encoded in bits 0-1 */ /* CH1 volts/div is encoded in bits 0-1 */
sr_dbg("hantek-dso: CH1 vdiv index %d", devc->voltage_ch1); sr_dbg("CH1 vdiv index: %d.", devc->voltage_ch1);
switch (devc->voltage_ch1) { switch (devc->voltage_ch1) {
case VDIV_1V: case VDIV_1V:
case VDIV_100MV: case VDIV_100MV:
@ -427,7 +428,7 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc)
} }
/* CH2 volts/div is encoded in bits 2-3 */ /* CH2 volts/div is encoded in bits 2-3 */
sr_dbg("hantek-dso: CH2 vdiv index %d", devc->voltage_ch2); sr_dbg("CH2 vdiv index: %d.", devc->voltage_ch2);
switch (devc->voltage_ch2) { switch (devc->voltage_ch2) {
case VDIV_1V: case VDIV_1V:
case VDIV_100MV: case VDIV_100MV:
@ -453,10 +454,10 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc)
DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
cmdstring, sizeof(cmdstring), cmdstring, sizeof(cmdstring),
&tmp, 100)) != 0) { &tmp, 100)) != 0) {
sr_err("Failed to set voltage: %d", ret); sr_err("Failed to set voltage: %d.", ret);
return SR_ERR; return SR_ERR;
} }
sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE"); sr_dbg("Sent CMD_SET_VOLTAGE.");
return SR_OK; return SR_OK;
} }
@ -468,7 +469,7 @@ SR_PRIV int dso_set_relays(struct dev_context *devc)
uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01, uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
sr_dbg("hantek-dso: preparing CTRL_SETRELAYS"); sr_dbg("Preparing CTRL_SETRELAYS.");
if (devc->voltage_ch1 < VDIV_1V) if (devc->voltage_ch1 < VDIV_1V)
relays[1] = ~relays[1]; relays[1] = ~relays[1];
@ -476,7 +477,7 @@ SR_PRIV int dso_set_relays(struct dev_context *devc)
if (devc->voltage_ch1 < VDIV_100MV) if (devc->voltage_ch1 < VDIV_100MV)
relays[2] = ~relays[2]; relays[2] = ~relays[2];
sr_dbg("hantek-dso: CH1 coupling %d", devc->coupling_ch1); sr_dbg("CH1 coupling: %d.", devc->coupling_ch1);
if (devc->coupling_ch1 != COUPLING_AC) if (devc->coupling_ch1 != COUPLING_AC)
relays[3] = ~relays[3]; relays[3] = ~relays[3];
@ -486,7 +487,7 @@ SR_PRIV int dso_set_relays(struct dev_context *devc)
if (devc->voltage_ch2 < VDIV_100MV) if (devc->voltage_ch2 < VDIV_100MV)
relays[5] = ~relays[5]; relays[5] = ~relays[5];
sr_dbg("hantek-dso: CH2 coupling %d", devc->coupling_ch1); sr_dbg("CH2 coupling: %d.", devc->coupling_ch1);
if (devc->coupling_ch2 != COUPLING_AC) if (devc->coupling_ch2 != COUPLING_AC)
relays[6] = ~relays[6]; relays[6] = ~relays[6];
@ -495,7 +496,7 @@ SR_PRIV int dso_set_relays(struct dev_context *devc)
if (sr_log_loglevel_get() >= SR_LOG_DBG) { if (sr_log_loglevel_get() >= SR_LOG_DBG) {
gs = g_string_sized_new(128); gs = g_string_sized_new(128);
g_string_printf(gs, "hantek-dso: relays:"); g_string_printf(gs, "Relays:");
for (i = 0; i < 17; i++) for (i = 0; i < 17; i++)
g_string_append_printf(gs, " %.2x", relays[i]); g_string_append_printf(gs, " %.2x", relays[i]);
sr_dbg(gs->str); sr_dbg(gs->str);
@ -505,10 +506,10 @@ SR_PRIV int dso_set_relays(struct dev_context *devc)
if ((ret = libusb_control_transfer(devc->usb->devhdl, if ((ret = libusb_control_transfer(devc->usb->devhdl,
LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
0, 0, relays, 17, 100)) != sizeof(relays)) { 0, 0, relays, 17, 100)) != sizeof(relays)) {
sr_err("failed to set relays: %d", ret); sr_err("Failed to set relays: %d.", ret);
return SR_ERR; return SR_ERR;
} }
sr_dbg("hantek-dso: sent CTRL_SETRELAYS"); sr_dbg("Sent CTRL_SETRELAYS.");
return SR_OK; return SR_OK;
} }
@ -519,7 +520,7 @@ SR_PRIV int dso_set_voffsets(struct dev_context *devc)
uint16_t *ch_levels; uint16_t *ch_levels;
uint8_t offsets[17]; uint8_t offsets[17];
sr_dbg("hantek-dso: preparing CTRL_SETOFFSET"); sr_dbg("Preparing CTRL_SETOFFSET.");
memset(offsets, 0, sizeof(offsets)); memset(offsets, 0, sizeof(offsets));
/* Channel 1 */ /* Channel 1 */
@ -527,31 +528,31 @@ SR_PRIV int dso_set_voffsets(struct dev_context *devc)
offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0]; offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
offsets[0] = (offset >> 8) | 0x20; offsets[0] = (offset >> 8) | 0x20;
offsets[1] = offset & 0xff; offsets[1] = offset & 0xff;
sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", devc->voffset_ch1, sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
offsets[0], offsets[1]); offsets[0], offsets[1]);
/* Channel 2 */ /* Channel 2 */
ch_levels = devc->channel_levels[1][devc->voltage_ch2]; ch_levels = devc->channel_levels[1][devc->voltage_ch2];
offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0]; offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
offsets[2] = (offset >> 8) | 0x20; offsets[2] = (offset >> 8) | 0x20;
offsets[3] = offset & 0xff; offsets[3] = offset & 0xff;
sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", devc->voffset_ch2, sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
offsets[2], offsets[3]); offsets[2], offsets[3]);
/* Trigger */ /* Trigger */
offset = MAX_VERT_TRIGGER * devc->voffset_trigger; offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
offsets[4] = (offset >> 8) | 0x20; offsets[4] = (offset >> 8) | 0x20;
offsets[5] = offset & 0xff; offsets[5] = offset & 0xff;
sr_dbg("hantek-dso: trigger offset %3.2f (%.2x%.2x)", devc->voffset_trigger, sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
offsets[4], offsets[5]); offsets[4], offsets[5]);
if ((ret = libusb_control_transfer(devc->usb->devhdl, if ((ret = libusb_control_transfer(devc->usb->devhdl,
LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) { 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
sr_err("failed to set offsets: %d", ret); sr_err("Failed to set offsets: %d.", ret);
return SR_ERR; return SR_ERR;
} }
sr_dbg("hantek-dso: sent CTRL_SETOFFSET"); sr_dbg("Sent CTRL_SETOFFSET.");
return SR_OK; return SR_OK;
} }
@ -561,7 +562,7 @@ SR_PRIV int dso_enable_trigger(struct dev_context *devc)
int ret, tmp; int ret, tmp;
uint8_t cmdstring[2]; uint8_t cmdstring[2];
sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER"); sr_dbg("Sending CMD_ENABLE_TRIGGER.");
memset(cmdstring, 0, sizeof(cmdstring)); memset(cmdstring, 0, sizeof(cmdstring));
cmdstring[0] = CMD_ENABLE_TRIGGER; cmdstring[0] = CMD_ENABLE_TRIGGER;
@ -574,7 +575,7 @@ SR_PRIV int dso_enable_trigger(struct dev_context *devc)
DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
cmdstring, sizeof(cmdstring), cmdstring, sizeof(cmdstring),
&tmp, 100)) != 0) { &tmp, 100)) != 0) {
sr_err("Failed to enable trigger: %d", ret); sr_err("Failed to enable trigger: %d.", ret);
return SR_ERR; return SR_ERR;
} }
@ -586,7 +587,7 @@ SR_PRIV int dso_force_trigger(struct dev_context *devc)
int ret, tmp; int ret, tmp;
uint8_t cmdstring[2]; uint8_t cmdstring[2];
sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER"); sr_dbg("Sending CMD_FORCE_TRIGGER.");
memset(cmdstring, 0, sizeof(cmdstring)); memset(cmdstring, 0, sizeof(cmdstring));
cmdstring[0] = CMD_FORCE_TRIGGER; cmdstring[0] = CMD_FORCE_TRIGGER;
@ -599,7 +600,7 @@ SR_PRIV int dso_force_trigger(struct dev_context *devc)
DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
cmdstring, sizeof(cmdstring), cmdstring, sizeof(cmdstring),
&tmp, 100)) != 0) { &tmp, 100)) != 0) {
sr_err("Failed to force trigger: %d", ret); sr_err("Failed to force trigger: %d.", ret);
return SR_ERR; return SR_ERR;
} }
@ -609,7 +610,7 @@ SR_PRIV int dso_force_trigger(struct dev_context *devc)
SR_PRIV int dso_init(struct dev_context *devc) SR_PRIV int dso_init(struct dev_context *devc)
{ {
sr_dbg("hantek-dso: initializing dso"); sr_dbg("Initializing DSO.");
if (get_channel_offsets(devc) != SR_OK) if (get_channel_offsets(devc) != SR_OK)
return SR_ERR; return SR_ERR;
@ -635,33 +636,35 @@ SR_PRIV int dso_init(struct dev_context *devc)
return SR_OK; return SR_OK;
} }
SR_PRIV int dso_get_capturestate(struct dev_context *devc, uint8_t *capturestate, SR_PRIV int dso_get_capturestate(struct dev_context *devc,
uint32_t *trigger_offset) uint8_t *capturestate,
uint32_t *trigger_offset)
{ {
int ret, tmp, i; int ret, tmp, i;
unsigned int bitvalue, toff; unsigned int bitvalue, toff;
uint8_t cmdstring[2], inbuf[512]; uint8_t cmdstring[2], inbuf[512];
sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE"); sr_dbg("Sending CMD_GET_CAPTURESTATE.");
cmdstring[0] = CMD_GET_CAPTURESTATE; cmdstring[0] = CMD_GET_CAPTURESTATE;
cmdstring[1] = 0; cmdstring[1] = 0;
if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) {
sr_dbg("Failed to send get_capturestate command: %d", ret); sr_dbg("Failed to send get_capturestate command: %d.", ret);
return SR_ERR; return SR_ERR;
} }
if ((ret = libusb_bulk_transfer(devc->usb->devhdl, if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
DSO_EP_IN | LIBUSB_ENDPOINT_IN, DSO_EP_IN | LIBUSB_ENDPOINT_IN,
inbuf, 512, &tmp, 100)) != 0) { inbuf, 512, &tmp, 100)) != 0) {
sr_dbg("Failed to get capturestate: %d", ret); sr_dbg("Failed to get capturestate: %d.", ret);
return SR_ERR; return SR_ERR;
} }
*capturestate = inbuf[0]; *capturestate = inbuf[0];
toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2]; toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
/* This conversion comes from the openhantek project. /*
* This conversion comes from the openhantek project.
* Each set bit in the 24-bit value inverts all bits with a lower * Each set bit in the 24-bit value inverts all bits with a lower
* value. No idea why the device reports the trigger point this way. * value. No idea why the device reports the trigger point this way.
*/ */
@ -682,13 +685,13 @@ SR_PRIV int dso_capture_start(struct dev_context *devc)
int ret; int ret;
uint8_t cmdstring[2]; uint8_t cmdstring[2];
sr_dbg("hantek-dso: sending CMD_CAPTURE_START"); sr_dbg("Sending CMD_CAPTURE_START.");
cmdstring[0] = CMD_CAPTURE_START; cmdstring[0] = CMD_CAPTURE_START;
cmdstring[1] = 0; cmdstring[1] = 0;
if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) {
sr_err("Failed to send capture_start command: %d", ret); sr_err("Failed to send capture_start command: %d.", ret);
return SR_ERR; return SR_ERR;
} }
@ -702,22 +705,23 @@ SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn
uint8_t cmdstring[2]; uint8_t cmdstring[2];
unsigned char *buf; unsigned char *buf;
sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA"); sr_dbg("Sending CMD_GET_CHANNELDATA.");
cmdstring[0] = CMD_GET_CHANNELDATA; cmdstring[0] = CMD_GET_CHANNELDATA;
cmdstring[1] = 0; cmdstring[1] = 0;
if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) {
sr_err("Failed to get channel data: %d", ret); sr_err("Failed to get channel data: %d.", ret);
return SR_ERR; return SR_ERR;
} }
/* TODO: dso-2xxx only */ /* TODO: DSO-2xxx only. */
num_transfers = devc->framesize * sizeof(unsigned short) / devc->epin_maxpacketsize; num_transfers = devc->framesize *
sr_dbg("hantek-dso: queueing up %d transfers", num_transfers); sizeof(unsigned short) / devc->epin_maxpacketsize;
sr_dbg("Queueing up %d transfers.", num_transfers);
for (i = 0; i < num_transfers; i++) { for (i = 0; i < num_transfers; i++) {
if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) { if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
sr_err("hantek-dso: %s: buf malloc failed", __func__); sr_err("Failed to malloc USB endpoint buffer.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
transfer = libusb_alloc_transfer(0); transfer = libusb_alloc_transfer(0);
@ -725,7 +729,7 @@ SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn
DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf, DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
devc->epin_maxpacketsize, cb, devc, 40); devc->epin_maxpacketsize, cb, devc, 40);
if ((ret = libusb_submit_transfer(transfer)) != 0) { if ((ret = libusb_submit_transfer(transfer)) != 0) {
sr_err("failed to submit transfer: %d", ret); sr_err("Failed to submit transfer: %d.", ret);
/* TODO: Free them all. */ /* TODO: Free them all. */
libusb_free_transfer(transfer); libusb_free_transfer(transfer);
g_free(buf); g_free(buf);
@ -735,4 +739,3 @@ SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn
return SR_OK; return SR_OK;
} }

View File

@ -22,41 +22,40 @@
#ifndef LIBSIGROK_HARDWARE_HANTEK_DSO_H #ifndef LIBSIGROK_HARDWARE_HANTEK_DSO_H
#define LIBSIGROK_HARDWARE_HANTEK_DSO_H #define LIBSIGROK_HARDWARE_HANTEK_DSO_H
#define USB_INTERFACE 0 #define USB_INTERFACE 0
#define USB_CONFIGURATION 1 #define USB_CONFIGURATION 1
#define DSO_EP_IN 0x86 #define DSO_EP_IN 0x86
#define DSO_EP_OUT 0x02 #define DSO_EP_OUT 0x02
#define NUM_PROBES 2 #define NUM_PROBES 2
/* FX2 renumeration delay in ms */ /* FX2 renumeration delay in ms */
#define MAX_RENUM_DELAY_MS 3000 #define MAX_RENUM_DELAY_MS 3000
#define MAX_CAPTURE_EMPTY 3 #define MAX_CAPTURE_EMPTY 3
#define DEFAULT_VOLTAGE VDIV_500MV #define DEFAULT_VOLTAGE VDIV_500MV
#define DEFAULT_FRAMESIZE FRAMESIZE_SMALL #define DEFAULT_FRAMESIZE FRAMESIZE_SMALL
#define DEFAULT_TIMEBASE TIME_100us #define DEFAULT_TIMEBASE TIME_100us
#define DEFAULT_TRIGGER_SOURCE "CH1" #define DEFAULT_TRIGGER_SOURCE "CH1"
#define DEFAULT_COUPLING COUPLING_DC #define DEFAULT_COUPLING COUPLING_DC
#define DEFAULT_HORIZ_TRIGGERPOS 0.5 #define DEFAULT_HORIZ_TRIGGERPOS 0.5
#define DEFAULT_VERT_OFFSET 0.5 #define DEFAULT_VERT_OFFSET 0.5
#define DEFAULT_VERT_TRIGGERPOS 0.5 #define DEFAULT_VERT_TRIGGERPOS 0.5
#define MAX_VERT_TRIGGER 0xfe #define MAX_VERT_TRIGGER 0xfe
/* Hantek DSO-specific protocol values */ /* Hantek DSO-specific protocol values */
#define EEPROM_CHANNEL_OFFSETS 0x08 #define EEPROM_CHANNEL_OFFSETS 0x08
#define FRAMESIZE_SMALL 10240
#define FRAMESIZE_LARGE 32768
#define FRAMESIZE_SMALL 10240
#define FRAMESIZE_LARGE 32768
enum control_requests { enum control_requests {
CTRL_READ_EEPROM = 0xa2, CTRL_READ_EEPROM = 0xa2,
CTRL_GETSPEED = 0xb2, CTRL_GETSPEED = 0xb2,
CTRL_BEGINCOMMAND = 0xb3, CTRL_BEGINCOMMAND = 0xb3,
CTRL_SETOFFSET = 0xb4, CTRL_SETOFFSET = 0xb4,
CTRL_SETRELAYS = 0xb5 CTRL_SETRELAYS = 0xb5,
}; };
enum dso_commands { enum dso_commands {
@ -70,7 +69,7 @@ enum dso_commands {
CMD_SET_VOLTAGE, CMD_SET_VOLTAGE,
/* unused */ /* unused */
cmdSetLogicalData, cmdSetLogicalData,
cmdGetLogicalData cmdGetLogicalData,
}; };
/* Must match the coupling table. */ /* Must match the coupling table. */
@ -78,7 +77,7 @@ enum couplings {
COUPLING_AC = 0, COUPLING_AC = 0,
COUPLING_DC, COUPLING_DC,
/* TODO not used, how to enable? */ /* TODO not used, how to enable? */
COUPLING_GND COUPLING_GND,
}; };
/* Must match the timebases table. */ /* Must match the timebases table. */
@ -97,7 +96,7 @@ enum time_bases {
TIME_40ms, TIME_40ms,
TIME_100ms, TIME_100ms,
TIME_200ms, TIME_200ms,
TIME_400ms TIME_400ms,
}; };
/* Must match the vdivs table. */ /* Must match the vdivs table. */
@ -115,7 +114,7 @@ enum {
enum trigger_slopes { enum trigger_slopes {
SLOPE_POSITIVE = 0, SLOPE_POSITIVE = 0,
SLOPE_NEGATIVE SLOPE_NEGATIVE,
}; };
enum trigger_sources { enum trigger_sources {
@ -130,13 +129,13 @@ enum capturestates {
CAPTURE_READY_8BIT = 2, CAPTURE_READY_8BIT = 2,
CAPTURE_READY_9BIT = 7, CAPTURE_READY_9BIT = 7,
CAPTURE_TIMEOUT = 127, CAPTURE_TIMEOUT = 127,
CAPTURE_UNKNOWN = 255 CAPTURE_UNKNOWN = 255,
}; };
enum triggermodes { enum triggermodes {
TRIGGERMODE_AUTO, TRIGGERMODE_AUTO,
TRIGGERMODE_NORMAL, TRIGGERMODE_NORMAL,
TRIGGERMODE_SINGLE TRIGGERMODE_SINGLE,
}; };
enum states { enum states {
@ -209,9 +208,10 @@ SR_PRIV void dso_close(struct sr_dev_inst *sdi);
SR_PRIV int dso_enable_trigger(struct dev_context *devc); SR_PRIV int dso_enable_trigger(struct dev_context *devc);
SR_PRIV int dso_force_trigger(struct dev_context *devc); SR_PRIV int dso_force_trigger(struct dev_context *devc);
SR_PRIV int dso_init(struct dev_context *devc); SR_PRIV int dso_init(struct dev_context *devc);
SR_PRIV int dso_get_capturestate(struct dev_context *devc, uint8_t *capturestate, SR_PRIV int dso_get_capturestate(struct dev_context *devc,
uint32_t *trigger_offset); uint8_t *capturestate, uint32_t *trigger_offset);
SR_PRIV int dso_capture_start(struct dev_context *devc); SR_PRIV int dso_capture_start(struct dev_context *devc);
SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn cb); SR_PRIV int dso_get_channeldata(struct dev_context *devc,
libusb_transfer_cb_fn cb);
#endif #endif