From: Aurelien Jacobs Date: Wed, 19 Feb 2014 14:09:03 +0000 (+0100) Subject: brymen-bm86x: actual driver implementation X-Git-Tag: libsigrok-0.3.0~133 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=ecaa89af0e01f9c3f585e82eb849bf616bff22cf;p=libsigrok.git brymen-bm86x: actual driver implementation --- diff --git a/README.devices b/README.devices index 136db62c..30f8e474 100644 --- a/README.devices +++ b/README.devices @@ -52,6 +52,7 @@ The following drivers/devices do not need any firmware upload: - agilent-dmm - alsa - appa-55ii + - brymen-bm86x - brymen-dmm - cem-dt-885x - center-3xx (including all subdrivers) @@ -117,6 +118,7 @@ The following drivers/devices do not require a serial port specification: - alsa - asix-sigma + - brymen-bm86x - chronovu-la8 - demo - fx2lafw diff --git a/hardware/brymen-bm86x/api.c b/hardware/brymen-bm86x/api.c index feb877d1..9ab60dff 100644 --- a/hardware/brymen-bm86x/api.c +++ b/hardware/brymen-bm86x/api.c @@ -19,6 +19,19 @@ #include "protocol.h" +#define BRYMEN_BC86X "0820.0001" + +static const int32_t hwopts[] = { + SR_CONF_CONN, +}; + +static const int32_t hwcaps[] = { + SR_CONF_MULTIMETER, + SR_CONF_LIMIT_SAMPLES, + SR_CONF_LIMIT_MSEC, + SR_CONF_CONTINUOUS, +}; + SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info; static struct sr_dev_driver *di = &brymen_bm86x_driver_info; @@ -29,17 +42,63 @@ static int init(struct sr_context *sr_ctx) static GSList *scan(GSList *options) { + GSList *usb_devices, *devices, *l; struct drv_context *drvc; - GSList *devices; - - (void)options; + struct dev_context *devc; + struct sr_dev_inst *sdi; + struct sr_usb_dev_inst *usb; + struct sr_config *src; + struct sr_probe *probe; + const char *conn; - devices = NULL; drvc = di->priv; drvc->instances = NULL; - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ + conn = BRYMEN_BC86X; + for (l = options; l; l = l->next) { + src = l->data; + switch (src->key) { + case SR_CONF_CONN: + conn = g_variant_get_string(src->data, NULL); + break; + } + } + + devices = NULL; + if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { + g_slist_free_full(usb_devices, g_free); + return NULL; + } + + for (l = usb_devices; l; l = l->next) { + usb = l->data; + + if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, + "Brymen", "BM869", NULL))) { + sr_err("sr_dev_inst_new returned NULL."); + return NULL; + } + + if (!(devc = g_try_malloc0(sizeof(*devc)))) { + sr_err("Device context malloc failed."); + return NULL; + } + + sdi->priv = devc; + sdi->driver = di; + if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P2"))) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + + sdi->inst_type = SR_INST_USB; + sdi->conn = usb; + + drvc->instances = g_slist_append(drvc->instances, sdi); + devices = g_slist_append(devices, sdi); + } return devices; } @@ -56,117 +115,184 @@ static int dev_clear(void) static int dev_open(struct sr_dev_inst *sdi) { - (void)sdi; + struct drv_context *drvc = di->priv; + struct sr_usb_dev_inst *usb; + struct dev_context *devc; + int ret; - /* TODO: get handle from sdi->conn and open it. */ + usb = sdi->conn; + devc = sdi->priv; + + if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) + sdi->status = SR_ST_ACTIVE; + + /* Detach kernel drivers which grabbed this device (if any). */ + if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { + ret = libusb_detach_kernel_driver(usb->devhdl, 0); + if (ret < 0) { + sr_err("Failed to detach kernel driver: %s.", + libusb_error_name(ret)); + return SR_ERR; + } + devc->detached_kernel_driver = 1; + sr_dbg("Successfully detached kernel driver."); + } else { + sr_dbg("No need to detach a kernel driver."); + } - sdi->status = SR_ST_ACTIVE; + /* Claim interface 0. */ + if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) { + sr_err("Failed to claim interface 0: %s.", + libusb_error_name(ret)); + return SR_ERR; + } + sr_dbg("Successfully claimed interface 0."); - return SR_OK; + return ret; } static int dev_close(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_usb_dev_inst *usb; + struct dev_context *devc; + int ret; + + usb = sdi->conn; + devc = sdi->priv; + + if ((ret = libusb_release_interface(usb->devhdl, 0))) + sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret)); + else + sr_dbg("Successfully released interface 0.\n"); + + if (!ret && devc->detached_kernel_driver) { + if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) { + sr_err("Failed to attach kernel driver: %s.\n", + libusb_error_name(ret)); + } else { + devc->detached_kernel_driver = 0; + sr_dbg("Successfully attached kernel driver.\n"); + } + } - /* TODO: get handle from sdi->conn and close it. */ + libusb_close(usb->devhdl); sdi->status = SR_ST_INACTIVE; - return SR_OK; + return ret; } static int cleanup(void) { - dev_clear(); - - /* TODO: free other driver resources, if any. */ - - return SR_OK; + return dev_clear(); } static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_probe_group *probe_group) { - int ret; + struct dev_context *devc = sdi->priv; - (void)sdi; - (void)data; (void)probe_group; - ret = SR_OK; switch (key) { - /* TODO */ + case SR_CONF_LIMIT_SAMPLES: + *data = g_variant_new_uint64(devc->limit_samples); + break; + case SR_CONF_LIMIT_MSEC: + *data = g_variant_new_uint64(devc->limit_msec); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_probe_group *probe_group) { - int ret; + struct dev_context *devc; - (void)data; (void)probe_group; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - ret = SR_OK; + if (!(devc = sdi->priv)) { + sr_err("sdi->priv was NULL."); + return SR_ERR_BUG; + } + switch (key) { - /* TODO */ + case SR_CONF_LIMIT_SAMPLES: + devc->limit_samples = g_variant_get_uint64(data); + sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples); + break; + case SR_CONF_LIMIT_MSEC: + devc->limit_msec = g_variant_get_uint64(data); + sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec); + break; default: - ret = SR_ERR_NA; + return SR_ERR_NA; } - return ret; + return SR_OK; } static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_probe_group *probe_group) { - int ret; - (void)sdi; - (void)data; (void)probe_group; - ret = SR_OK; switch (key) { - /* TODO */ + case SR_CONF_SCAN_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); + break; + case SR_CONF_DEVICE_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { - (void)sdi; - (void)cb_data; + struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - /* TODO: configure hardware, reset acquisition state, set up - * callbacks and send header packet. */ + devc = sdi->priv; + devc->session_cb_data = cb_data; + devc->start_time = g_get_monotonic_time(); + + /* Send header packet to the session bus. */ + std_session_send_df_header(cb_data, LOG_PREFIX); + + sr_source_add(0, 0, 10, brymen_bm86x_receive_data, (void *)sdi); return SR_OK; } static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - (void)cb_data; + struct sr_datafeed_packet packet; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - /* TODO: stop acquisition. */ + /* Send end packet to the session bus. */ + packet.type = SR_DF_END; + sr_session_send(cb_data, &packet); + + sr_source_remove(0); return SR_OK; } diff --git a/hardware/brymen-bm86x/protocol.c b/hardware/brymen-bm86x/protocol.c index 7c0a9a1f..85329ac4 100644 --- a/hardware/brymen-bm86x/protocol.c +++ b/hardware/brymen-bm86x/protocol.c @@ -17,14 +17,297 @@ * along with this program. If not, see . */ +#include +#include #include "protocol.h" +#define USB_TIMEOUT 500 + +static char char_map[128] = { + [0x20] = '-', + [0x5F] = '0', + [0x50] = '1', + [0x6D] = '2', + [0x7C] = '3', + [0x72] = '4', + [0x3E] = '5', + [0x3F] = '6', + [0x54] = '7', + [0x7F] = '8', + [0x7E] = '9', + [0x0F] = 'C', + [0x27] = 'F', + [0x0B] = 'L', + [0x79] = 'd', + [0x10] = 'i', + [0x39] = 'o', +}; + +static int brymen_bm86x_parse_digits(const unsigned char *buf, int length, + char *str, float *floatval, + char *temp_unit, int flag) +{ + char c, *p = str; + int i, ret; + + if (buf[0] & flag) + *p++ = '-'; + for (i = 0; i < length; i++) { + if (i && i < 5 && buf[i+1] & 0x01) + *p++ = '.'; + c = char_map[buf[i+1] >> 1]; + if (i == 5 && (c == 'C' || c == 'F')) + *temp_unit = c; + else if (c) + *p++ = c; + } + *p = 0; + + if ((ret = sr_atof_ascii(str, floatval))) { + sr_dbg("invalid float string: '%s'", str); + return ret; + } + + return SR_OK; +} + +static void brymen_bm86x_parse(unsigned char *buf, float *floatval, + struct sr_datafeed_analog *analog) +{ + char str[16], temp_unit; + int ret1, ret2, over_limit; + + ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0], + &temp_unit, 0x80); + over_limit = strstr(str, "0L") || strstr(str, "0.L"); + ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1], + &temp_unit, 0x10); + + /* main display */ + if (ret1 == SR_OK || over_limit) { + /* SI unit */ + if (buf[8] & 0x01) { + analog[0].mq = SR_MQ_VOLTAGE; + analog[0].unit = SR_UNIT_VOLT; + if (!strcmp(str, "diod")) + analog[0].mqflags |= SR_MQFLAG_DIODE; + } else if (buf[14] & 0x80) { + analog[0].mq = SR_MQ_CURRENT; + analog[0].unit = SR_UNIT_AMPERE; + } else if (buf[14] & 0x20) { + analog[0].mq = SR_MQ_CAPACITANCE; + analog[0].unit = SR_UNIT_FARAD; + } else if (buf[14] & 0x10) { + analog[0].mq = SR_MQ_CONDUCTANCE; + analog[0].unit = SR_UNIT_SIEMENS; + } else if (buf[15] & 0x01) { + analog[0].mq = SR_MQ_FREQUENCY; + analog[0].unit = SR_UNIT_HERTZ; + } else if (buf[10] & 0x01) { + analog[0].mq = SR_MQ_CONTINUITY; + analog[0].unit = SR_UNIT_OHM; + } else if (buf[15] & 0x10) { + analog[0].mq = SR_MQ_RESISTANCE; + analog[0].unit = SR_UNIT_OHM; + } else if (buf[15] & 0x02) { + analog[0].mq = SR_MQ_POWER; + analog[0].unit = SR_UNIT_DECIBEL_MW; + } else if (buf[15] & 0x80) { + analog[0].mq = SR_MQ_DUTY_CYCLE; + analog[0].unit = SR_UNIT_PERCENTAGE; + } else if (buf[ 2] & 0x0A) { + analog[0].mq = SR_MQ_TEMPERATURE; + if (temp_unit == 'F') + analog[0].unit = SR_UNIT_FAHRENHEIT; + else + analog[0].unit = SR_UNIT_CELSIUS; + } + + /* when MIN MAX and AVG are displayed at the same time, remove them */ + if ((buf[1] & 0xE0) == 0xE0) + buf[1] &= ~0xE0; + + /* AC/DC/Auto flags */ + if (buf[1] & 0x10) analog[0].mqflags |= SR_MQFLAG_DC; + if (buf[2] & 0x01) analog[0].mqflags |= SR_MQFLAG_AC; + if (buf[1] & 0x01) analog[0].mqflags |= SR_MQFLAG_AUTORANGE; + if (buf[1] & 0x08) analog[0].mqflags |= SR_MQFLAG_HOLD; + if (buf[1] & 0x20) analog[0].mqflags |= SR_MQFLAG_MAX; + if (buf[1] & 0x40) analog[0].mqflags |= SR_MQFLAG_MIN; + if (buf[1] & 0x80) analog[0].mqflags |= SR_MQFLAG_AVG; + if (buf[3] & 0x01) analog[0].mqflags |= SR_MQFLAG_RELATIVE; + + /* when dBm is displayed, remove the m suffix so that it is + not considered as the 10e-3 SI prefix */ + if (buf[15] & 0x02) + buf[15] &= ~0x04; + + /* SI prefix */ + if (buf[14] & 0x40) floatval[0] *= 1e-9; /* n */ + if (buf[15] & 0x08) floatval[0] *= 1e-6; /* µ */ + if (buf[15] & 0x04) floatval[0] *= 1e-3; /* m */ + if (buf[15] & 0x40) floatval[0] *= 1e3; /* k */ + if (buf[15] & 0x20) floatval[0] *= 1e6; /* M */ + + if (over_limit) floatval[0] = INFINITY; + } + + /* secondary display */ + if (ret2 == SR_OK) { + /* SI unit */ + if (buf[14] & 0x08) { + analog[1].mq = SR_MQ_VOLTAGE; + analog[1].unit = SR_UNIT_VOLT; + } else if (buf[9] & 0x04) { + analog[1].mq = SR_MQ_CURRENT; + analog[1].unit = SR_UNIT_AMPERE; + } else if (buf[14] & 0x04) { + analog[1].mq = SR_MQ_FREQUENCY; + analog[1].unit = SR_UNIT_HERTZ; + } else if (buf[9] & 0x40) { + analog[1].mq = SR_MQ_TEMPERATURE; + if (temp_unit == 'F') + analog[1].unit = SR_UNIT_FAHRENHEIT; + else + analog[1].unit = SR_UNIT_CELSIUS; + } + + /* AC flag */ + if (buf[9] & 0x20) analog[1].mqflags |= SR_MQFLAG_AC; + + /* SI prefix */ + if (buf[ 9] & 0x01) floatval[1] *= 1e-6; /* µ */ + if (buf[ 9] & 0x02) floatval[1] *= 1e-3; /* m */ + if (buf[14] & 0x02) floatval[1] *= 1e3; /* k */ + if (buf[14] & 0x01) floatval[1] *= 1e6; /* M */ + } + + if (buf[9] & 0x80) + sr_spew("Battery is low."); +} + +static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi, + unsigned char *buf) +{ + struct dev_context *devc; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog[2]; + float floatval[2]; + + devc = sdi->priv; + + analog[0].mq = -1; + analog[0].mqflags = 0; + + analog[1].mq = -1; + analog[1].mqflags = 0; + + brymen_bm86x_parse(buf, floatval, analog); + + if (analog[0].mq != -1) { + /* Got a measurement. */ + analog[0].num_samples = 1; + analog[0].data = &floatval[0]; + analog[0].probes = g_slist_append(NULL, sdi->probes->data); + packet.type = SR_DF_ANALOG; + packet.payload = &analog[0]; + sr_session_send(devc->session_cb_data, &packet); + g_slist_free(analog[0].probes); + } + + if (analog[1].mq != -1) { + /* Got a measurement. */ + analog[1].num_samples = 1; + analog[1].data = &floatval[1]; + analog[1].probes = g_slist_append(NULL, sdi->probes->next->data); + packet.type = SR_DF_ANALOG; + packet.payload = &analog[1]; + sr_session_send(devc->session_cb_data, &packet); + g_slist_free(analog[1].probes); + } + + if (analog[0].mq != -1 || analog[1].mq != -1) + devc->num_samples++; +} + +static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi) +{ + struct sr_usb_dev_inst *usb; + unsigned char buf[] = { 0x00, 0x86, 0x66 }; + int ret; + + usb = sdi->conn; + + sr_dbg("Sending HID set report."); + ret = libusb_control_transfer(usb->devhdl, + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE | + LIBUSB_ENDPOINT_OUT, + 9, /* bRequest: HID set_report */ + 0x300, /* wValue: HID feature, report num 0 */ + 0, /* wIndex: interface 0 */ + buf, sizeof(buf), USB_TIMEOUT); + + if (ret < 0) { + sr_err("HID feature report error: %s.", libusb_error_name(ret)); + return SR_ERR; + } + + if (ret != sizeof(buf)) { + sr_err("Short packet: sent %d/%ld bytes.", ret, sizeof(buf)); + return SR_ERR; + } + + return SR_OK; +} + +static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + struct sr_usb_dev_inst *usb; + unsigned char buf[24]; + int ret, transferred; + + devc = sdi->priv; + usb = sdi->conn; + + sr_dbg("Reading HID interrupt report."); + /* Get data from EP1 using an interrupt transfer. */ + ret = libusb_interrupt_transfer(usb->devhdl, + LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */ + buf, sizeof(buf), + &transferred, USB_TIMEOUT); + + if (ret == LIBUSB_ERROR_TIMEOUT) { + if (++devc->interrupt_pending > 3) + devc->interrupt_pending = 0; + return SR_OK; + } + + if (ret < 0) { + sr_err("USB receive error: %s.", libusb_error_name(ret)); + return SR_ERR; + } + + if (transferred != sizeof(buf)) { + sr_err("Short packet: received %d/%d bytes.", transferred, sizeof(buf)); + return SR_ERR; + } + + devc->interrupt_pending = 0; + brymen_bm86x_handle_packet(sdi, buf); + + return SR_OK; +} + SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; + struct sr_dev_inst *sdi; struct dev_context *devc; + int64_t time; (void)fd; + (void)revents; if (!(sdi = cb_data)) return TRUE; @@ -32,8 +315,28 @@ SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data) if (!(devc = sdi->priv)) return TRUE; - if (revents == G_IO_IN) { - /* TODO */ + if (!devc->interrupt_pending) { + if (brymen_bm86x_send_command(sdi)) + return FALSE; + devc->interrupt_pending = 1; + } + + if (brymen_bm86x_read_interrupt(sdi)) + return FALSE; + + if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { + sr_info("Requested number of samples reached, stopping."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } + + if (devc->limit_msec) { + time = (g_get_monotonic_time() - devc->start_time) / 1000; + if (time > (int64_t)devc->limit_msec) { + sr_info("Requested time limit reached, stopping."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } } return TRUE; diff --git a/hardware/brymen-bm86x/protocol.h b/hardware/brymen-bm86x/protocol.h index db6f7d9b..5590d4e3 100644 --- a/hardware/brymen-bm86x/protocol.h +++ b/hardware/brymen-bm86x/protocol.h @@ -30,14 +30,18 @@ /** Private, per-device-instance driver context. */ struct dev_context { - /* Model-specific information */ - /* Acquisition settings */ + uint64_t limit_samples; /**< The sampling limit (in number of samples).*/ + uint64_t limit_msec; /**< The time limit (in milliseconds). */ + void *session_cb_data; /**< Opaque pointer passed in by the frontend. */ /* Operational state */ + int detached_kernel_driver;/**< Whether kernel driver was detached or not */ + uint64_t num_samples; /**< The number of already received samples. */ + int64_t start_time; /**< The time at which sampling started. */ /* Temporary state across callbacks */ - + int interrupt_pending; }; SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data);