From: Uwe Hermann Date: Sat, 10 Nov 2012 17:19:30 +0000 (+0100) Subject: hantek-dso: Use message logging helpers. X-Git-Tag: dsupstream~569 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e98b7f1be0a97207b9a0b4d7c9a8d397fb542eb1;p=libsigrok.git hantek-dso: Use message logging helpers. --- diff --git a/hardware/hantek-dso/api.c b/hardware/hantek-dso/api.c index 0847da55..c819fbbe 100644 --- a/hardware/hantek-dso/api.c +++ b/hardware/hantek-dso/api.c @@ -33,10 +33,9 @@ #include "libsigrok-internal.h" #include "dso.h" - /* Max time in ms before we want to check on USB events */ /* TODO tune this properly */ -#define TICK 1 +#define TICK 1 static const int hwcaps[] = { SR_HWCAP_OSCILLOSCOPE, @@ -145,6 +144,7 @@ static const char *coupling[] = { SR_PRIV struct sr_dev_driver 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 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; 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. */ 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)))) { - sr_err("hantek-dso: devc malloc failed"); + sr_err("Device context malloc failed."); return NULL; } + devc->profile = prof; devc->dev_state = IDLE; devc->timebase = DEFAULT_TIMEBASE; @@ -230,12 +232,12 @@ static int clear_instances(void) for (l = drvc->instances; l; l = l->next) { if (!(sdi = l->data)) { /* 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; } if (!(devc = sdi->priv)) { /* 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; } dso_close(sdi); @@ -256,13 +258,13 @@ static int hw_init(void) struct drv_context *drvc; 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; } if (libusb_init(NULL) != 0) { g_free(drvc); - sr_err("hantek-dso: Failed to initialize USB."); + sr_err("Failed to initialize USB."); return SR_ERR; } @@ -283,6 +285,7 @@ static GSList *hw_scan(GSList *options) int devcnt, ret, i, j; (void)options; + devcnt = 0; devices = 0; drvc = hdi->priv; @@ -294,7 +297,7 @@ static GSList *hw_scan(GSList *options) libusb_get_device_list(NULL, &devlist); for (i = 0; devlist[i]; i++) { 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; } @@ -304,7 +307,7 @@ static GSList *hw_scan(GSList *options) && des.idProduct == dev_profiles[j].orig_pid) { /* Device matches the pre-firmware profile. */ 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); devices = g_slist_append(devices, sdi); devc = sdi->priv; @@ -313,8 +316,8 @@ static GSList *hw_scan(GSList *options) /* Remember when the firmware on this device was updated */ devc->fw_updated = g_get_monotonic_time(); else - sr_err("hantek-dso: firmware upload failed for " - "device %d", devcnt); + sr_err("Firmware upload failed for " + "device %d.", devcnt); /* Dummy USB address of 0xff will get overwritten later. */ devc->usb = sr_usb_dev_inst_new( 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) { /* Device matches the post-firmware profile. */ 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->status = SR_ST_INACTIVE; devices = g_slist_append(devices, sdi); @@ -363,13 +366,13 @@ static int hw_dev_open(struct sr_dev_inst *sdi) devc = sdi->priv; /* - * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS - * for the FX2 to renumerate + * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS + * for the FX2 to renumerate. */ err = SR_ERR; if (devc->fw_updated > 0) { - sr_info("hantek-dso: waiting for device to reset"); - /* takes at least 300ms for the FX2 to be gone from the USB bus */ + sr_info("Waiting for device to reset."); + /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ g_usleep(300 * 1000); timediff_ms = 0; 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); timediff_us = g_get_monotonic_time() - devc->fw_updated; 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 { err = dso_open(sdi); } if (err != SR_OK) { - sr_err("hantek-dso: unable to open device"); + sr_err("Unable to open device."); return SR_ERR; } err = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE); if (err != 0) { - sr_err("hantek-dso: Unable to claim interface: %d", err); + sr_err("Unable to claim interface: %d.", 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) { - dso_close(sdi); return SR_OK; @@ -422,7 +424,7 @@ static int hw_cleanup(void) } 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; @@ -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, - const void *value) + const void *value) { struct dev_context *devc; 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: tmp_float = *(const float *)value; 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; } else 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")) devc->filter_trigger = TRUE; else { - sr_err("invalid filter target %s", targets[i]); + sr_err("Invalid filter target %s.", targets[i]); ret = SR_ERR_ARG; } } g_strfreev(targets); break; 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; for (i = 0; vdivs[i].p && vdivs[i].q; i++) { 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; break; case SR_HWCAP_COUPLING: - /* TODO not supporting coupling per channel yet */ + /* TODO: Not supporting coupling per channel yet. */ for (i = 0; coupling[i]; i++) { if (!strcmp(value, coupling[i])) { devc->coupling_ch1 = i; @@ -581,6 +583,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, break; default: ret = SR_ERR_ARG; + break; } 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); data_offset = 0; 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 - * 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*), - * where the value is a point in the range represented by the vdiv - * setting. There are 8 vertical divs, so e.g. 500mV/div represents - * 4V peak-to-peak where 0 = -2V and 255 = +2V. + * Voltage values are encoded as a value 0-255 (0-512 on the + * DSO-5200*), where the value is a point in the range + * represented by the vdiv setting. There are 8 vertical divs, + * 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) { range = ((float)vdivs[devc->voltage_ch1].p / vdivs[devc->voltage_ch1].q) * 8; 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); - } -/* 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 - * 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. */ static void receive_transfer(struct libusb_transfer *transfer) @@ -645,8 +651,8 @@ static void receive_transfer(struct libusb_transfer *transfer) int num_samples, pre; devc = transfer->user_data; - sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes", - transfer->status, transfer->actual_length); + sr_dbg("receive_transfer(): status %d received %d bytes.", + transfer->status, transfer->actual_length); if (transfer->actual_length == 0) /* Nothing to send to the bus. */ @@ -654,15 +660,17 @@ static void receive_transfer(struct libusb_transfer *transfer) num_samples = transfer->actual_length / 2; - sr_dbg("hantek-dso: got %d-%d/%d samples in frame", devc->samp_received + 1, - devc->samp_received + num_samples, devc->framesize); + sr_dbg("Got %d-%d/%d samples in frame.", devc->samp_received + 1, + 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 * 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 - * that trigger point came after the end of the device's frame buffer was - * reached, and it wrapped around to overwrite up until the trigger point. + * from there up the session bus. The samples in the frame buffer + * before that trigger point came after the end of the device's frame + * buffer was reached, and it wrapped around to overwrite up until the + * trigger point. */ if (devc->samp_received < devc->trigger_offset) { /* Trigger point not yet reached. */ @@ -672,17 +680,19 @@ static void receive_transfer(struct libusb_transfer *transfer) transfer->buffer, num_samples * 2); devc->samp_buffered += num_samples; } 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 - * send the rest up to the session bus. */ + * send the rest up to the session bus. + */ pre = devc->trigger_offset - devc->samp_received; memcpy(devc->framebuf + devc->samp_buffered * 2, transfer->buffer, pre * 2); devc->samp_buffered += pre; /* The rest of this chunk starts with the trigger point. */ - sr_dbg("hantek-dso: reached trigger point, %d samples buffered", - devc->samp_buffered); + sr_dbg("Reached trigger point, %d samples buffered.", + devc->samp_buffered); /* Avoid the corner case where the chunk ended at * exactly the trigger point. */ @@ -706,8 +716,8 @@ static void receive_transfer(struct libusb_transfer *transfer) if (devc->samp_received >= devc->framesize) { /* That was the last chunk in this frame. Send the buffered * pre-trigger samples out now, in one big chunk. */ - sr_dbg("hantek-dso: end of frame, sending %d pre-trigger buffered samples", - devc->samp_buffered); + sr_dbg("End of frame, sending %d pre-trigger buffered samples.", + devc->samp_buffered); send_chunk(devc, devc->framebuf, devc->samp_buffered); /* Mark the end of this frame. */ @@ -721,7 +731,6 @@ static void receive_transfer(struct libusb_transfer *transfer) devc->dev_state = NEW_CAPTURE; } } - } 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; uint8_t capturestate; - /* Avoid compiler warnings. */ (void)fd; (void)revents; @@ -743,9 +751,11 @@ static int handle_event(int fd, int revents, void *cb_data) devc = sdi->priv; if (devc->dev_state == STOPPING) { /* We've been told to wind up the acquisition. */ - sr_dbg("hantek-dso: stopping acquisition"); - /* TODO: doesn't really cancel pending transfers so they might - * come in after SR_DF_END is sent. */ + sr_dbg("Stopping acquisition."); + /* + * TODO: Doesn't really cancel pending transfers so they might + * come in after SR_DF_END is sent. + */ lupfd = libusb_get_pollfds(NULL); for (i = 0; lupfd[i]; i++) sr_source_remove(lupfd[i]->fd); @@ -771,7 +781,7 @@ static int handle_event(int fd, int revents, void *cb_data) return TRUE; // if (dso_force_trigger(devc) != SR_OK) // return TRUE; - sr_dbg("hantek-dso: successfully requested next chunk"); + sr_dbg("Successfully requested next chunk."); devc->dev_state = CAPTURE; 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) return TRUE; - sr_dbg("hantek-dso: capturestate %d", capturestate); - sr_dbg("hantek-dso: trigger offset 0x%.6x", trigger_offset); + sr_dbg("Capturestate %d.", capturestate); + sr_dbg("Trigger offset 0x%.6x.", trigger_offset); switch (capturestate) { case 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; // if (dso_force_trigger(devc) != SR_OK) // break; - sr_dbg("hantek-dso: successfully requested next chunk"); + sr_dbg("Successfully requested next chunk."); } break; case CAPTURE_FILLING: - /* no data yet */ + /* No data yet. */ break; case CAPTURE_READY_8BIT: /* 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) 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. */ devc->dev_state = FETCH_DATA; @@ -823,20 +834,21 @@ static int handle_event(int fd, int revents, void *cb_data) break; case CAPTURE_READY_9BIT: /* TODO */ - sr_err("not yet supported"); + sr_err("Not yet supported."); break; case CAPTURE_TIMEOUT: /* Doesn't matter, we'll try again next time. */ break; default: - sr_dbg("unknown capture state"); + sr_dbg("Unknown capture state: %d.", capturestate); + break; } return TRUE; } static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, - void *cb_data) + void *cb_data) { const struct libusb_pollfd **lupfd; 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; if (configure_probes(sdi) != SR_OK) { - sr_err("hantek-dso: failed to configured probes"); + sr_err("Failed to configure probes."); return SR_ERR; } @@ -865,8 +877,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, devc->dev_state = CAPTURE; lupfd = libusb_get_pollfds(NULL); for (i = 0; lupfd[i]; i++) - sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event, - (void *)sdi); + sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, + handle_event, (void *)sdi); free(lupfd); /* Send header packet to the session bus. */ diff --git a/hardware/hantek-dso/dso.c b/hardware/hantek-dso/dso.c index 3fea9a58..0f1285a3 100644 --- a/hardware/hantek-dso/dso.c +++ b/hardware/hantek-dso/dso.c @@ -28,19 +28,18 @@ extern struct sr_dev_driver hantek_dso_driver_info; - static int send_begin(struct dev_context *devc) { int ret; unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe, 0x00, 0x01, 0x00}; - sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND"); + sr_dbg("Sending CTRL_BEGINCOMMAND."); if ((ret = libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND, 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; } @@ -119,7 +118,7 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi) libusb_get_device_list(NULL, &devlist); for (i = 0; devlist[i]; i++) { 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; } @@ -152,19 +151,19 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi) */ devc->usb->address = libusb_get_device_address(devlist[i]); - if(!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) - sr_err("hantek-dso: wrong endpoint profile"); + if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) + sr_err("Wrong endpoint profile."); else { 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, devc->usb->address, USB_INTERFACE); } } 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; } 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) 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); libusb_release_interface(devc->usb->devhdl, USB_INTERFACE); libusb_close(devc->usb->devhdl); @@ -198,7 +197,7 @@ static int get_channel_offsets(struct dev_context *devc) GString *gs; int chan, v, ret; - sr_dbg("hantek-dso: getting channel offsets"); + sr_dbg("Getting channel offsets."); ret = libusb_control_transfer(devc->usb->devhdl, 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, sizeof(devc->channel_levels), 200); 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; } @@ -217,19 +216,21 @@ static int get_channel_offsets(struct dev_context *devc) */ for (chan = 0; chan < 2; chan++) { 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][1] = g_ntohs(devc->channel_levels[chan][v][1]); + devc->channel_levels[chan][v][0] = + 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) { gs = g_string_sized_new(128); 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++) { g_string_append_printf(gs, " %.4x-%.4x", - devc->channel_levels[chan][v][0], - devc->channel_levels[chan][v][1]); + devc->channel_levels[chan][v][0], + devc->channel_levels[chan][v][1]); } sr_dbg(gs->str); } @@ -244,18 +245,18 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc) int ret, tmp; uint8_t cmdstring[12]; 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, - 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)); /* Command */ cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; /* Trigger source */ - sr_dbg("hantek-dso: trigger source %s", devc->triggersource); + sr_dbg("Trigger source %s.", devc->triggersource); if (!strcmp("CH2", devc->triggersource)) tmp = 0; 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)) tmp = 2; else { - sr_err("hantek-dso: invalid trigger source %s", devc->triggersource); + sr_err("Invalid trigger source: '%s'.", devc->triggersource); return SR_ERR_ARG; } cmdstring[2] = tmp; /* 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; /* Timebase fast */ - sr_dbg("hantek-dso: time base index %d", devc->timebase); + sr_dbg("Time base index: %d.", devc->timebase); switch (devc->framesize) { case FRAMESIZE_SMALL: if (devc->timebase < TIME_20us) @@ -289,7 +290,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc) break; case FRAMESIZE_LARGE: 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; } 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; /* 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; cmdstring[3] = tmp; /* Fast rates channel */ - /* TODO: is this right? */ + /* TODO: Is this right? */ tmp = devc->timebase < TIME_10us ? 1 : 0; cmdstring[3] |= tmp << 2; /* Trigger slope: 0=positive 1=negative */ - /* TODO: does this work? */ - sr_dbg("hantek-dso: trigger slope %d", devc->triggerslope); + /* TODO: Does this work? */ + sr_dbg("Trigger slope: %d.", devc->triggerslope); cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; /* Timebase slow */ @@ -334,7 +335,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc) cmdstring[5] = (tmp >> 8) & 0xff; /* 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; cmdstring[6] = tmp & 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, cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { - sr_err("Failed to set trigger/samplerate: %d", ret); + sr_err("Failed to set trigger/samplerate: %d.", ret); return SR_ERR; } - sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE"); + sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE."); return SR_OK; } @@ -360,22 +361,22 @@ SR_PRIV int dso_set_filters(struct dev_context *devc) int ret, tmp; uint8_t cmdstring[8]; - sr_dbg("hantek-dso: preparing CMD_SET_FILTERS"); + sr_dbg("Preparing CMD_SET_FILTERS."); memset(cmdstring, 0, sizeof(cmdstring)); cmdstring[0] = CMD_SET_FILTERS; cmdstring[1] = 0x0f; if (devc->filter_ch1) { - sr_dbg("hantek-dso: turning on CH1 filter"); + sr_dbg("Turning on CH1 filter."); cmdstring[2] |= 0x80; } if (devc->filter_ch2) { - sr_dbg("hantek-dso: turning on CH2 filter"); + sr_dbg("Turning on CH2 filter."); cmdstring[2] |= 0x40; } if (devc->filter_trigger) { /* TODO: supported on the DSO-2090? */ - sr_dbg("hantek-dso: turning on trigger filter"); + sr_dbg("Turning on trigger filter."); 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); return SR_ERR; } - sr_dbg("hantek-dso: sent CMD_SET_FILTERS"); + sr_dbg("Sent CMD_SET_FILTERS."); return SR_OK; } @@ -399,7 +400,7 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc) int ret, tmp; uint8_t cmdstring[8]; - sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE"); + sr_dbg("Preparing CMD_SET_VOLTAGE."); memset(cmdstring, 0, sizeof(cmdstring)); cmdstring[0] = CMD_SET_VOLTAGE; @@ -407,7 +408,7 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc) cmdstring[2] = 0x30; /* 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) { case VDIV_1V: 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 */ - sr_dbg("hantek-dso: CH2 vdiv index %d", devc->voltage_ch2); + sr_dbg("CH2 vdiv index: %d.", devc->voltage_ch2); switch (devc->voltage_ch2) { case VDIV_1V: case VDIV_100MV: @@ -453,10 +454,10 @@ SR_PRIV int dso_set_voltage(struct dev_context *devc) DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { - sr_err("Failed to set voltage: %d", ret); + sr_err("Failed to set voltage: %d.", ret); return SR_ERR; } - sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE"); + sr_dbg("Sent CMD_SET_VOLTAGE."); 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, 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) relays[1] = ~relays[1]; @@ -476,7 +477,7 @@ SR_PRIV int dso_set_relays(struct dev_context *devc) if (devc->voltage_ch1 < VDIV_100MV) 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) relays[3] = ~relays[3]; @@ -486,7 +487,7 @@ SR_PRIV int dso_set_relays(struct dev_context *devc) if (devc->voltage_ch2 < VDIV_100MV) 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) 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) { gs = g_string_sized_new(128); - g_string_printf(gs, "hantek-dso: relays:"); + g_string_printf(gs, "Relays:"); for (i = 0; i < 17; i++) g_string_append_printf(gs, " %.2x", relays[i]); 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, LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, 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; } - sr_dbg("hantek-dso: sent CTRL_SETRELAYS"); + sr_dbg("Sent CTRL_SETRELAYS."); return SR_OK; } @@ -519,7 +520,7 @@ SR_PRIV int dso_set_voffsets(struct dev_context *devc) uint16_t *ch_levels; uint8_t offsets[17]; - sr_dbg("hantek-dso: preparing CTRL_SETOFFSET"); + sr_dbg("Preparing CTRL_SETOFFSET."); memset(offsets, 0, sizeof(offsets)); /* 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]; offsets[0] = (offset >> 8) | 0x20; offsets[1] = offset & 0xff; - sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", devc->voffset_ch1, - offsets[0], offsets[1]); + sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1, + offsets[0], offsets[1]); /* Channel 2 */ ch_levels = devc->channel_levels[1][devc->voltage_ch2]; offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0]; offsets[2] = (offset >> 8) | 0x20; offsets[3] = offset & 0xff; - sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", devc->voffset_ch2, - offsets[2], offsets[3]); + sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2, + offsets[2], offsets[3]); /* Trigger */ offset = MAX_VERT_TRIGGER * devc->voffset_trigger; offsets[4] = (offset >> 8) | 0x20; 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]); if ((ret = libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, 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; } - sr_dbg("hantek-dso: sent CTRL_SETOFFSET"); + sr_dbg("Sent CTRL_SETOFFSET."); return SR_OK; } @@ -561,7 +562,7 @@ SR_PRIV int dso_enable_trigger(struct dev_context *devc) int ret, tmp; uint8_t cmdstring[2]; - sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER"); + sr_dbg("Sending CMD_ENABLE_TRIGGER."); memset(cmdstring, 0, sizeof(cmdstring)); 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, cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { - sr_err("Failed to enable trigger: %d", ret); + sr_err("Failed to enable trigger: %d.", ret); return SR_ERR; } @@ -586,7 +587,7 @@ SR_PRIV int dso_force_trigger(struct dev_context *devc) int ret, tmp; uint8_t cmdstring[2]; - sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER"); + sr_dbg("Sending CMD_FORCE_TRIGGER."); memset(cmdstring, 0, sizeof(cmdstring)); 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, cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { - sr_err("Failed to force trigger: %d", ret); + sr_err("Failed to force trigger: %d.", ret); 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_dbg("hantek-dso: initializing dso"); + sr_dbg("Initializing DSO."); if (get_channel_offsets(devc) != SR_OK) return SR_ERR; @@ -635,33 +636,35 @@ SR_PRIV int dso_init(struct dev_context *devc) return SR_OK; } -SR_PRIV int dso_get_capturestate(struct dev_context *devc, uint8_t *capturestate, - uint32_t *trigger_offset) +SR_PRIV int dso_get_capturestate(struct dev_context *devc, + uint8_t *capturestate, + uint32_t *trigger_offset) { int ret, tmp, i; unsigned int bitvalue, toff; 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[1] = 0; 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; } if ((ret = libusb_bulk_transfer(devc->usb->devhdl, DSO_EP_IN | LIBUSB_ENDPOINT_IN, inbuf, 512, &tmp, 100)) != 0) { - sr_dbg("Failed to get capturestate: %d", ret); + sr_dbg("Failed to get capturestate: %d.", ret); return SR_ERR; } *capturestate = inbuf[0]; 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 * 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; uint8_t cmdstring[2]; - sr_dbg("hantek-dso: sending CMD_CAPTURE_START"); + sr_dbg("Sending CMD_CAPTURE_START."); cmdstring[0] = CMD_CAPTURE_START; cmdstring[1] = 0; 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; } @@ -702,22 +705,23 @@ SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn uint8_t cmdstring[2]; unsigned char *buf; - sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA"); + sr_dbg("Sending CMD_GET_CHANNELDATA."); cmdstring[0] = CMD_GET_CHANNELDATA; cmdstring[1] = 0; 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; } - /* TODO: dso-2xxx only */ - num_transfers = devc->framesize * sizeof(unsigned short) / devc->epin_maxpacketsize; - sr_dbg("hantek-dso: queueing up %d transfers", num_transfers); + /* TODO: DSO-2xxx only. */ + num_transfers = devc->framesize * + sizeof(unsigned short) / devc->epin_maxpacketsize; + sr_dbg("Queueing up %d transfers.", num_transfers); for (i = 0; i < num_transfers; i++) { 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; } 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, devc->epin_maxpacketsize, cb, devc, 40); 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. */ libusb_free_transfer(transfer); g_free(buf); @@ -735,4 +739,3 @@ SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn return SR_OK; } - diff --git a/hardware/hantek-dso/dso.h b/hardware/hantek-dso/dso.h index 5ea970a9..bafa077d 100644 --- a/hardware/hantek-dso/dso.h +++ b/hardware/hantek-dso/dso.h @@ -22,41 +22,40 @@ #ifndef LIBSIGROK_HARDWARE_HANTEK_DSO_H #define LIBSIGROK_HARDWARE_HANTEK_DSO_H -#define USB_INTERFACE 0 -#define USB_CONFIGURATION 1 -#define DSO_EP_IN 0x86 -#define DSO_EP_OUT 0x02 -#define NUM_PROBES 2 +#define USB_INTERFACE 0 +#define USB_CONFIGURATION 1 +#define DSO_EP_IN 0x86 +#define DSO_EP_OUT 0x02 +#define NUM_PROBES 2 /* 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_FRAMESIZE FRAMESIZE_SMALL -#define DEFAULT_TIMEBASE TIME_100us -#define DEFAULT_TRIGGER_SOURCE "CH1" -#define DEFAULT_COUPLING COUPLING_DC -#define DEFAULT_HORIZ_TRIGGERPOS 0.5 -#define DEFAULT_VERT_OFFSET 0.5 -#define DEFAULT_VERT_TRIGGERPOS 0.5 +#define DEFAULT_VOLTAGE VDIV_500MV +#define DEFAULT_FRAMESIZE FRAMESIZE_SMALL +#define DEFAULT_TIMEBASE TIME_100us +#define DEFAULT_TRIGGER_SOURCE "CH1" +#define DEFAULT_COUPLING COUPLING_DC +#define DEFAULT_HORIZ_TRIGGERPOS 0.5 +#define DEFAULT_VERT_OFFSET 0.5 +#define DEFAULT_VERT_TRIGGERPOS 0.5 -#define MAX_VERT_TRIGGER 0xfe +#define MAX_VERT_TRIGGER 0xfe /* Hantek DSO-specific protocol values */ -#define EEPROM_CHANNEL_OFFSETS 0x08 - -#define FRAMESIZE_SMALL 10240 -#define FRAMESIZE_LARGE 32768 +#define EEPROM_CHANNEL_OFFSETS 0x08 +#define FRAMESIZE_SMALL 10240 +#define FRAMESIZE_LARGE 32768 enum control_requests { CTRL_READ_EEPROM = 0xa2, CTRL_GETSPEED = 0xb2, CTRL_BEGINCOMMAND = 0xb3, CTRL_SETOFFSET = 0xb4, - CTRL_SETRELAYS = 0xb5 + CTRL_SETRELAYS = 0xb5, }; enum dso_commands { @@ -70,7 +69,7 @@ enum dso_commands { CMD_SET_VOLTAGE, /* unused */ cmdSetLogicalData, - cmdGetLogicalData + cmdGetLogicalData, }; /* Must match the coupling table. */ @@ -78,7 +77,7 @@ enum couplings { COUPLING_AC = 0, COUPLING_DC, /* TODO not used, how to enable? */ - COUPLING_GND + COUPLING_GND, }; /* Must match the timebases table. */ @@ -97,7 +96,7 @@ enum time_bases { TIME_40ms, TIME_100ms, TIME_200ms, - TIME_400ms + TIME_400ms, }; /* Must match the vdivs table. */ @@ -115,7 +114,7 @@ enum { enum trigger_slopes { SLOPE_POSITIVE = 0, - SLOPE_NEGATIVE + SLOPE_NEGATIVE, }; enum trigger_sources { @@ -130,13 +129,13 @@ enum capturestates { CAPTURE_READY_8BIT = 2, CAPTURE_READY_9BIT = 7, CAPTURE_TIMEOUT = 127, - CAPTURE_UNKNOWN = 255 + CAPTURE_UNKNOWN = 255, }; enum triggermodes { TRIGGERMODE_AUTO, TRIGGERMODE_NORMAL, - TRIGGERMODE_SINGLE + TRIGGERMODE_SINGLE, }; 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_force_trigger(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, - uint32_t *trigger_offset); +SR_PRIV int dso_get_capturestate(struct dev_context *devc, + uint8_t *capturestate, uint32_t *trigger_offset); 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