From: Bert Vermeulen Date: Sun, 11 Aug 2013 08:44:03 +0000 (+0200) Subject: uni-t-ut32x: Full acquisition support X-Git-Tag: libsigrok-0.2.2~95 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d6ff054ae58f1423282d52c37ac9e76296163504;p=libsigrok.git uni-t-ut32x: Full acquisition support --- diff --git a/hardware/uni-t-ut32x/api.c b/hardware/uni-t-ut32x/api.c index f1843524..f97adcda 100644 --- a/hardware/uni-t-ut32x/api.c +++ b/hardware/uni-t-ut32x/api.c @@ -21,13 +21,6 @@ #include -#define USB_CONN "1a86.e008" -#define VENDOR "UNI-T" -#define MODEL "UT32x" -#define USB_INTERFACE 0 -#define EP_IN 0x80 | 2 -#define EP_OUT 2 - static const int32_t hwcaps[] = { SR_CONF_THERMOMETER, SR_CONF_LIMIT_SAMPLES, @@ -153,8 +146,8 @@ static int dev_open(struct sr_dev_inst *sdi) * driver being active, but detaching it always returns an error. */ #if !defined(__APPLE__) - if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { - if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { + if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { + if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) { sr_err("failed to detach kernel driver: %s", libusb_error_name(ret)); return SR_ERR; @@ -162,7 +155,7 @@ static int dev_open(struct sr_dev_inst *sdi) } #endif - if ((ret = libusb_set_configuration(usb->devhdl, 1))) { + if ((ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION))) { sr_err("Failed to set configuration: %s.", libusb_error_name(ret)); return SR_ERR; } @@ -313,6 +306,18 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, devc->cb_data = cb_data; devc->num_samples = 0; + devc->packet_len = 0; + + /* Configure serial port parameters on USB-UART interface + * chip inside the device (just baudrate 2400 actually). */ + cmd[0] = 0x09; + cmd[1] = 0x60; + ret = libusb_control_transfer(usb->devhdl, 0x21, 0x09, 0x0300, 0x00, + cmd, 2, 5); + if (ret != 2) { + sr_dbg("Failed to configure CH9325: %s", libusb_error_name(ret)); + return SR_ERR; + } /* Send header packet to the session bus. */ std_session_send_df_header(cb_data, LOG_PREFIX); @@ -320,16 +325,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, if (!(devc->xfer = libusb_alloc_transfer(0))) return SR_ERR; - pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx); - for (i = 0; pfd[i]; i++) { - /* Handle USB events every 10ms. */ - sr_source_add(pfd[i]->fd, pfd[i]->events, 10, - uni_t_ut32x_handle_events, (void *)sdi); - /* We'll need to remove this fd later. */ - devc->usbfd[i] = pfd[i]->fd; - } - devc->usbfd[i] = -1; - /* Length of payload to follow. */ cmd[0] = 0x01; if (devc->data_source == DATA_SOURCE_LIVE) @@ -345,23 +340,35 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, } libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf, - 128, uni_t_ut32x_receive_transfer, (void *)sdi, 15); + 8, uni_t_ut32x_receive_transfer, (void *)sdi, 15); if (libusb_submit_transfer(devc->xfer) != 0) { libusb_free_transfer(devc->xfer); return SR_ERR; } + pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx); + for (i = 0; pfd[i]; i++) { + /* Handle USB events every 10ms. */ + sr_source_add(pfd[i]->fd, pfd[i]->events, 10, + uni_t_ut32x_handle_events, (void *)sdi); + /* We'll need to remove this fd later. */ + devc->usbfd[i] = pfd[i]->fd; + } + devc->usbfd[i] = -1; + return SR_OK; } static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { + (void)cb_data; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - /* TODO: stop acquisition. */ + /* Signal USB transfer handler to clean up and stop. */ + sdi->status = SR_ST_STOPPING; return SR_OK; } diff --git a/hardware/uni-t-ut32x/protocol.c b/hardware/uni-t-ut32x/protocol.c index 3614eda7..95f512b3 100644 --- a/hardware/uni-t-ut32x/protocol.c +++ b/hardware/uni-t-ut32x/protocol.c @@ -19,28 +19,222 @@ #include "protocol.h" -SR_PRIV int uni_t_ut32x_handle_events(int fd, int revents, void *cb_data) +#include +#include + +extern struct sr_dev_driver uni_t_ut32x_driver_info; +static struct sr_dev_driver *di = &uni_t_ut32x_driver_info; + +static float parse_temperature(unsigned char *buf) { - (void)fd; + float temp; + int i; + gboolean negative; + + negative = FALSE; + temp = 0.0; + for (i = 0; i < 4; i++) { + if (buf[i] == 0x3a) + continue; + if (buf[i] == 0x3b) { + if (negative) { + sr_dbg("Double negative sign!"); + return NAN; + } else { + negative = TRUE; + continue; + } + } + if (buf[i] < 0x30 || buf[i] > 0x39) { + sr_dbg("Invalid digit '%.2x'!", buf[i]); + return NAN; + } + temp *= 10; + temp += (buf[i] - 0x30); + } + temp /= 10; + if (negative) + temp = -temp; - const struct sr_dev_inst *sdi; + return temp; +} + +static void process_packet(struct sr_dev_inst *sdi) +{ struct dev_context *devc; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + GString *spew; + float temp; + int i; + gboolean is_valid; - if (!(sdi = cb_data)) - return TRUE; + devc = sdi->priv; + sr_dbg("Received full 19-byte packet."); + if (sr_log_loglevel_get() >= SR_LOG_SPEW) { + spew = g_string_sized_new(60); + for (i = 0; i < devc->packet_len; i++) + g_string_append_printf(spew, "%.2x ", devc->packet[i]); + sr_spew("%s", spew->str); + g_string_free(spew, TRUE); + } - if (!(devc = sdi->priv)) - return TRUE; + is_valid = TRUE; + if (devc->packet[1] == 0x3b && devc->packet[2] == 0x3b + && devc->packet[3] == 0x3b && devc->packet[4] == 0x3b) + /* No measurement: missing probe, empty storage location, ... */ + is_valid = FALSE; - if (revents == G_IO_IN) { - /* TODO */ + temp = parse_temperature(devc->packet + 1); + if (isnan(temp)) + is_valid = FALSE; + + if (is_valid) { + memset(&analog, 0, sizeof(struct sr_datafeed_analog)); + analog.mq = SR_MQ_TEMPERATURE; + analog.mqflags = 0; + switch (devc->packet[5] - 0x30) { + case 1: + analog.unit = SR_UNIT_CELSIUS; + break; + case 2: + analog.unit = SR_UNIT_FAHRENHEIT; + break; + case 3: + analog.unit = SR_UNIT_KELVIN; + break; + default: + /* We can still pass on the measurement, whatever it is. */ + sr_dbg("Unknown unit 0x%.2x.", devc->packet[5]); + } + switch (devc->packet[13] - 0x30) { + case 0: + /* Probe T1. */ + analog.probes = g_slist_append(NULL, g_slist_nth_data(sdi->probes, 0)); + break; + case 1: + /* Probe T2. */ + analog.probes = g_slist_append(NULL, g_slist_nth_data(sdi->probes, 1)); + break; + case 2: + case 3: + /* Probe T1-T2. */ + analog.probes = g_slist_append(NULL, g_slist_nth_data(sdi->probes, 2)); + analog.mqflags |= SR_MQFLAG_RELATIVE; + break; + default: + sr_err("Unknown probe 0x%.2x.", devc->packet[13]); + is_valid = FALSE; + } + if (is_valid) { + analog.num_samples = 1; + analog.data = &temp; + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + sr_session_send(devc->cb_data, &packet); + g_slist_free(analog.probes); + } + } + + /* We count packets even if the temperature was invalid. This way + * a sample limit on "Memory" data source still works: unused + * memory slots come through as "----" measurements. */ + devc->num_samples++; + if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { + sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi, + devc->cb_data); } - return TRUE; } SR_PRIV void uni_t_ut32x_receive_transfer(struct libusb_transfer *transfer) { + struct dev_context *devc; + struct sr_dev_inst *sdi; + int hid_payload_len, ret; + + sdi = transfer->user_data; + devc = sdi->priv; + if (transfer->actual_length == 8) { + /* CH9325 encodes length in low nibble of first byte, with + * bytes 1-7 being the (padded) payload. */ + hid_payload_len = transfer->buffer[0] & 0x0f; + memcpy(devc->packet + devc->packet_len, transfer->buffer + 1, + hid_payload_len); + devc->packet_len += hid_payload_len; + if (devc->packet_len >= 2 + && devc->packet[devc->packet_len - 2] == 0x0d + && devc->packet[devc->packet_len - 1] == 0x0a) { + /* Got end of packet, but do we have a complete packet? */ + if (devc->packet_len == 19) + process_packet(sdi); + /* Either way, done with it. */ + devc->packet_len = 0; + } else if (devc->packet_len > 19) { + /* Guard against garbage from the device overrunning + * our packet buffer. */ + sr_dbg("Buffer overrun!"); + devc->packet_len = 0; + } + } + + /* Get the next transfer (unless we're shutting down). */ + if (sdi->status != SR_ST_STOPPING) { + if ((ret = libusb_submit_transfer(devc->xfer)) != 0) { + sr_dbg("Failed to resubmit transfer: %s", libusb_error_name(ret)); + sdi->status = SR_ST_STOPPING; + libusb_free_transfer(devc->xfer); + } + } else + libusb_free_transfer(devc->xfer); } +SR_PRIV int uni_t_ut32x_handle_events(int fd, int revents, void *cb_data) +{ + struct drv_context *drvc; + struct dev_context *devc; + struct sr_dev_inst *sdi; + struct sr_datafeed_packet packet; + struct sr_usb_dev_inst *usb; + struct timeval tv; + int len, ret, i; + unsigned char cmd[2]; + + (void)fd; + (void)revents; + drvc = di->priv; + + if (!(sdi = cb_data)) + return TRUE; + + if (!(devc = sdi->priv)) + return TRUE; + + memset(&tv, 0, sizeof(struct timeval)); + libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, + NULL); + + if (sdi->status == SR_ST_STOPPING) { + for (i = 0; devc->usbfd[i] != -1; i++) + sr_source_remove(devc->usbfd[i]); + packet.type = SR_DF_END; + sr_session_send(cb_data, &packet); + + /* Tell the device to stop sending USB packets. */ + usb = sdi->conn; + cmd[0] = 0x01; + cmd[1] = CMD_STOP; + ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5); + if (ret != 0 || len != 2) { + /* Warning only, doesn't matter. */ + sr_dbg("Failed to send stop command: %s", libusb_error_name(ret)); + } + + sdi->status = SR_ST_ACTIVE; + return TRUE; + } + + return TRUE; +} + diff --git a/hardware/uni-t-ut32x/protocol.h b/hardware/uni-t-ut32x/protocol.h index e09bedce..b070eb43 100644 --- a/hardware/uni-t-ut32x/protocol.h +++ b/hardware/uni-t-ut32x/protocol.h @@ -35,6 +35,14 @@ #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) #define DEFAULT_DATA_SOURCE DATA_SOURCE_LIVE +#define USB_CONN "1a86.e008" +#define VENDOR "UNI-T" +#define MODEL "UT32x" +#define USB_INTERFACE 0 +#define USB_CONFIGURATION 1 + +#define EP_IN 0x80 | 2 +#define EP_OUT 2 enum { DATA_SOURCE_LIVE, @@ -49,8 +57,6 @@ enum { /** Private, per-device-instance driver context. */ struct dev_context { - /* Model-specific information */ - /* Acquisition settings */ uint64_t limit_samples; gboolean data_source; @@ -58,12 +64,13 @@ struct dev_context { /* Operational state */ uint64_t num_samples; int usbfd[10]; - unsigned char buf[128]; + unsigned char buf[8]; struct libusb_transfer *xfer; void *cb_data; /* Temporary state across callbacks */ - + unsigned char packet[32]; + int packet_len; }; SR_PRIV int uni_t_ut32x_handle_events(int fd, int revents, void *cb_data);