X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=hardware%2Flascar-el-usb%2Fprotocol.c;h=d63027443c3964585ab67315e9eebd8a225a1b93;hb=361d15110ce355dd69099d95ac8c9716e45b3173;hp=508727adbab795938cdf0be3d2b8be3a0a921a74;hpb=6aa1eb4ea33e1aeeef772cd234f140464eea3fe2;p=libsigrok.git diff --git a/hardware/lascar-el-usb/protocol.c b/hardware/lascar-el-usb/protocol.c index 508727ad..d6302744 100644 --- a/hardware/lascar-el-usb/protocol.c +++ b/hardware/lascar-el-usb/protocol.c @@ -40,12 +40,12 @@ static const struct elusb_profile profiles[] = { { 8, "EL-USB-LITE", LOG_UNSUPPORTED }, { 9, "EL-USB-CO", LOG_CO }, { 10, "EL-USB-TC", LOG_UNSUPPORTED }, - { 11, "EL-USB-CO300", LOG_UNSUPPORTED }, - { 12, "EL-USB-2-LCD", LOG_UNSUPPORTED }, - { 13, "EL-USB-2+", LOG_UNSUPPORTED }, + { 11, "EL-USB-CO300", LOG_CO }, + { 12, "EL-USB-2-LCD", LOG_TEMP_RH }, + { 13, "EL-USB-2+", LOG_TEMP_RH }, { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED }, { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED }, - { 16, "EL-USB-2-LCD+", LOG_UNSUPPORTED }, + { 16, "EL-USB-2-LCD+", LOG_TEMP_RH }, { 17, "EL-USB-5", LOG_UNSUPPORTED }, { 18, "EL-USB-1-RCG", LOG_UNSUPPORTED }, { 19, "EL-USB-1-LCD", LOG_UNSUPPORTED }, @@ -85,21 +85,24 @@ static void mark_xfer(struct libusb_transfer *xfer) } -SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl) +SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl, + unsigned char *configblock, int *configlen) { struct drv_context *drvc; struct libusb_transfer *xfer_in, *xfer_out; struct timeval tv; int64_t start; int buflen; - unsigned char cmd[3], buf[256], *config; + unsigned char cmd[3], buf[MAX_CONFIGBLOCK_SIZE]; + + sr_spew("Reading config block."); drvc = di->priv; - config = NULL; + *configlen = 0; if (!(xfer_in = libusb_alloc_transfer(0)) || !(xfer_out = libusb_alloc_transfer(0))) - return NULL; + return SR_ERR; /* Flush anything the F321 still has queued. */ while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen, @@ -143,10 +146,10 @@ SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl) } /* Got configuration structure header. */ - sr_dbg("response to config request: 0x%.2x 0x%.2x 0x%.2x ", + sr_spew("Response to config request: 0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]); buflen = buf[1] | (buf[2] << 8); - if (buf[0] != 0x02 || buflen > 256) { + if (buf[0] != 0x02 || buflen > MAX_CONFIGBLOCK_SIZE) { sr_dbg("Invalid response to config request: " "0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]); libusb_close(dev_hdl); @@ -176,9 +179,8 @@ SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl) goto cleanup; } - if (!(config = g_try_malloc(256))) - return NULL; - memcpy(config, buf, buflen); + memcpy(configblock, buf, buflen); + *configlen = buflen; cleanup: if (!xfer_in->user_data || !xfer_in->user_data) { @@ -197,7 +199,93 @@ cleanup: libusb_free_transfer(xfer_in); libusb_free_transfer(xfer_out); - return config; + return *configlen ? SR_OK : SR_ERR; +} + +SR_PRIV int lascar_save_config(libusb_device_handle *dev_hdl, + unsigned char *config, int configlen) +{ + struct drv_context *drvc; + struct libusb_transfer *xfer_in, *xfer_out; + struct timeval tv; + int64_t start; + int buflen, ret; + unsigned char cmd[3], buf[256]; + + sr_spew("Writing config block."); + + drvc = di->priv; + + if (!(xfer_in = libusb_alloc_transfer(0)) || + !(xfer_out = libusb_alloc_transfer(0))) + return SR_ERR; + + /* Flush anything the F321 still has queued. */ + while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen, + 5) == 0 && buflen > 0) + ; + ret = SR_OK; + + /* Keep a read request waiting in the wings, ready to pounce + * the moment the device sends something. */ + libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN, + buf, 256, mark_xfer, 0, 10000); + if (libusb_submit_transfer(xfer_in) != 0) { + ret = SR_ERR; + goto cleanup; + } + + /* Request device configuration structure. */ + cmd[0] = 0x01; + cmd[1] = configlen & 0xff; + cmd[2] = (configlen >> 8) & 0xff; + libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, + cmd, 3, mark_xfer, 0, 100); + if (libusb_submit_transfer(xfer_out) != 0) { + ret = SR_ERR; + goto cleanup; + } + tv.tv_sec = 0; + tv.tv_usec = 0; + while (!xfer_out->user_data) { + g_usleep(5000); + libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); + } + + libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, + config, configlen, mark_xfer, 0, 100); + if (libusb_submit_transfer(xfer_out) != 0) { + ret = SR_ERR; + goto cleanup; + } + while (!xfer_in->user_data || !xfer_out->user_data) { + g_usleep(5000); + libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); + } + + if (xfer_in->actual_length != 1 || buf[0] != 0xff) { + sr_dbg("unexpected response after transfer"); + ret = SR_ERR; + } + +cleanup: + if (!xfer_in->user_data || !xfer_in->user_data) { + if (!xfer_in->user_data) + libusb_cancel_transfer(xfer_in); + if (!xfer_out->user_data) + libusb_cancel_transfer(xfer_out); + start = g_get_monotonic_time(); + while (!xfer_in->user_data || !xfer_out->user_data) { + if (g_get_monotonic_time() - start > 10000) + break; + g_usleep(1000); + libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); + } + } + libusb_free_transfer(xfer_in); + libusb_free_transfer(xfer_out); + + return ret; } static struct sr_dev_inst *lascar_identify(unsigned char *config) @@ -260,8 +348,8 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address) struct libusb_device **devlist; struct libusb_device_descriptor des; libusb_device_handle *dev_hdl; - int ret, i; - unsigned char *config; + int dummy, ret, i; + unsigned char config[MAX_CONFIGBLOCK_SIZE]; drvc = di->priv; sdi = NULL; @@ -280,12 +368,11 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address) if (!(dev_hdl = lascar_open(devlist[i]))) continue; - if (!(config = lascar_get_config(dev_hdl))) + if (lascar_get_config(dev_hdl, config, &dummy) != SR_OK) continue; libusb_close(dev_hdl); sdi = lascar_identify(config); - g_free(config); } return sdi; @@ -297,30 +384,70 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf, struct dev_context *devc; struct sr_datafeed_packet packet; struct sr_datafeed_analog analog; + float *temp, *rh; uint16_t s; - int samples, samples_left, i; + int samples, samples_left, i, j; devc = sdi->priv; + analog.probes = sdi->probes; + samples = buflen / devc->sample_size; samples_left = devc->logged_samples - devc->rcvd_samples; if (samples_left < samples) samples = samples_left; switch (devc->profile->logformat) { case LOG_TEMP_RH: - /* TODO */ + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + analog.mqflags = 0; + if (!(temp = g_try_malloc(sizeof(float) * samples))) + break; + if (!(rh = g_try_malloc(sizeof(float) * samples))) + break; + for (i = 0, j = 0; i < samples; i++) { + /* Both Celcius and Fahrenheit stored at base -40. */ + if (devc->temp_unit == 0) + /* Celcius is stored in half-degree increments. */ + temp[j] = buf[i * 2] / 2 - 40; + else + temp[j] = buf[i * 2] - 40; + + rh[j] = buf[i * 2 + 1] / 2; + + if (temp[j] == 0.0 && rh[j] == 0.0) + /* Skip invalid measurement. */ + continue; + j++; + } + analog.num_samples = j; + + analog.mq = SR_MQ_TEMPERATURE; + if (devc->temp_unit == 1) + analog.unit = SR_UNIT_FAHRENHEIT; + else + analog.unit = SR_UNIT_CELSIUS; + analog.data = temp; + sr_session_send(devc->cb_data, &packet); + + analog.mq = SR_MQ_RELATIVE_HUMIDITY; + analog.unit = SR_UNIT_PERCENTAGE; + analog.data = rh; + sr_session_send(devc->cb_data, &packet); + g_free(temp); + g_free(rh); break; case LOG_CO: packet.type = SR_DF_ANALOG; packet.payload = &analog; analog.num_samples = samples; - analog.mq = 0; - analog.unit = 0; + analog.mq = SR_MQ_CARBON_MONOXIDE; + analog.unit = SR_UNIT_CONCENTRATION; analog.mqflags = 0; if (!(analog.data = g_try_malloc(sizeof(float) * samples))) break; for (i = 0; i < samples; i++) { s = (buf[i * 2] << 8) | buf[i * 2 + 1]; - analog.data[i] = s * devc->co_high + devc->co_low; + analog.data[i] = (s * devc->co_high + devc->co_low) / 1000000; if (analog.data[i] < 0.0) analog.data[i] = 0.0; } @@ -420,3 +547,84 @@ SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer) } } + +static int get_flags(unsigned char *configblock) +{ + int flags; + + flags = (configblock[32] | (configblock[33] << 8)) & 0x1fff; + sr_spew("Read flags (0x%.4x).", flags); + + return flags; +} + +static int set_flags(unsigned char *configblock, int flags) +{ + + sr_spew("Setting flags to 0x%.4x.", flags); + configblock[32] = flags & 0xff; + configblock[33] = (flags >> 8) & 0x1f; + + return flags; +} + +SR_PRIV int lascar_is_logging(const struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + int dummy, flags, ret; + + devc = sdi->priv; + if (lascar_get_config(devc->usb->devhdl, devc->config, &dummy) != SR_OK) + return -1; + + flags = get_flags(devc->config); + if (flags & 0x0100) + ret = 1; + else + ret = 0; + + return ret; +} + +SR_PRIV int lascar_start_logging(const struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + int len, flags, ret; + + devc = sdi->priv; + if (lascar_get_config(devc->usb->devhdl, devc->config, &len) != SR_OK) + return SR_ERR; + + /* Turn on logging. */ + flags = get_flags(devc->config); + flags |= 0x0100; + set_flags(devc->config, flags); + + /* Start logging in 0 seconds. */ + memset(devc->config + 24, 0, 4); + + ret = lascar_save_config(devc->usb->devhdl, devc->config, len); + sr_info("Started internal logging."); + + return ret; +} + +SR_PRIV int lascar_stop_logging(const struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + int len, flags, ret; + + devc = sdi->priv; + if (lascar_get_config(devc->usb->devhdl, devc->config, &len) != SR_OK) + return SR_ERR; + + flags = get_flags(devc->config); + flags &= ~0x0100; + set_flags(devc->config, flags); + + ret = lascar_save_config(devc->usb->devhdl, devc->config, len); + sr_info("Stopped internal logging."); + + return ret; +} +