uni-t-dmm: Do proper detection of packets in the stream.

Also, various fixups in the driver.
This commit is contained in:
Uwe Hermann 2013-01-26 23:39:24 +01:00
parent c885268777
commit bc653a5678
3 changed files with 72 additions and 65 deletions

View File

@ -1,7 +1,7 @@
/* /*
* This file is part of the libsigrok project. * This file is part of the libsigrok project.
* *
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -42,14 +42,14 @@ SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
SR_PRIV struct dmm_info udmms[] = { SR_PRIV struct dmm_info udmms[] = {
{ {
"UNI-T", "UT61D", 19230 /* TODO */, "UNI-T", "UT61D", 19230,
FS9922_PACKET_SIZE, NULL, FS9922_PACKET_SIZE, NULL,
sr_fs9922_packet_valid, sr_fs9922_parse, sr_fs9922_packet_valid, sr_fs9922_parse,
NULL, NULL,
&uni_t_ut61d_driver_info, receive_data_UNI_T_UT61D, &uni_t_ut61d_driver_info, receive_data_UNI_T_UT61D,
}, },
{ {
"Voltcraft VC-820", "VC-820", 19200 /* TODO */, "Voltcraft", "VC-820", 2400,
FS9721_PACKET_SIZE, NULL, FS9721_PACKET_SIZE, NULL,
sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_packet_valid, sr_fs9721_parse,
NULL, NULL,
@ -70,7 +70,7 @@ static int hw_init(struct sr_context *sr_ctx, int dmm)
{ {
sr_dbg("Selected '%s' subdriver.", udmms[dmm].di->name); sr_dbg("Selected '%s' subdriver.", udmms[dmm].di->name);
return std_hw_init(sr_ctx, uddms[dmm].di, DRIVER_LOG_DOMAIN); return std_hw_init(sr_ctx, udmms[dmm].di, DRIVER_LOG_DOMAIN);
} }
static GSList *hw_scan(GSList *options, int dmm) static GSList *hw_scan(GSList *options, int dmm)
@ -84,8 +84,6 @@ static GSList *hw_scan(GSList *options, int dmm)
struct sr_probe *probe; struct sr_probe *probe;
const char *conn; const char *conn;
(void)options;
drvc = udmms[dmm].di->priv; drvc = udmms[dmm].di->priv;
/* USB scan is always authoritative. */ /* USB scan is always authoritative. */
@ -117,6 +115,8 @@ static GSList *hw_scan(GSList *options, int dmm)
return NULL; return NULL;
} }
devc->first_run = TRUE;
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
udmms[dmm].vendor, udmms[dmm].device, NULL))) { udmms[dmm].vendor, udmms[dmm].device, NULL))) {
sr_err("sr_dev_inst_new returned NULL."); sr_err("sr_dev_inst_new returned NULL.");
@ -139,7 +139,7 @@ static GSList *hw_scan(GSList *options, int dmm)
static GSList *hw_dev_list(int dmm) static GSList *hw_dev_list(int dmm)
{ {
return ((struct drv_context *)(udmms[dmm]->priv))->instances; return ((struct drv_context *)(udmms[dmm].di->priv))->instances;
} }
static int hw_dev_open(struct sr_dev_inst *sdi, int dmm) static int hw_dev_open(struct sr_dev_inst *sdi, int dmm)
@ -210,7 +210,6 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
{ {
(void)sdi; (void)sdi;
switch (key) { switch (key) {

View File

@ -1,7 +1,7 @@
/* /*
* This file is part of the libsigrok project. * This file is part of the libsigrok project.
* *
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -70,9 +70,9 @@
static void decode_packet(struct sr_dev_inst *sdi, int dmm, const uint8_t *buf, static void decode_packet(struct sr_dev_inst *sdi, int dmm, const uint8_t *buf,
void *info) void *info)
{ {
struct dev_context *devc;
struct sr_datafeed_packet packet; struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog; struct sr_datafeed_analog analog;
/// struct fs9721_info info;
float floatval; float floatval;
int ret; int ret;
@ -80,9 +80,9 @@ static void decode_packet(struct sr_dev_inst *sdi, int dmm, const uint8_t *buf,
memset(&analog, 0, sizeof(struct sr_datafeed_analog)); memset(&analog, 0, sizeof(struct sr_datafeed_analog));
/* Parse the protocol packet. */ /* Parse the protocol packet. */
ret = udmms[dmm].packet_parse(buf, &floatval, &analog, &info); ret = udmms[dmm].packet_parse(buf, &floatval, &analog, info);
if (ret != SR_OK) { if (ret != SR_OK) {
sr_err("Invalid DMM packet, ignoring."); sr_dbg("Invalid DMM packet, ignoring.");
return; return;
} }
@ -128,7 +128,7 @@ static int hid_chip_init(struct dev_context *devc, uint16_t baudrate)
} }
sr_dbg("Successfully claimed interface 0."); sr_dbg("Successfully claimed interface 0.");
/* Baudrate example: 19230 baud -> HEX(19230) == 0x4b1e */ /* Set data for the HID feature report (e.g. baudrate). */
buf[0] = baudrate & 0xff; /* Baudrate, LSB */ buf[0] = baudrate & 0xff; /* Baudrate, LSB */
buf[1] = (baudrate >> 8) & 0xff; /* Baudrate, MSB */ buf[1] = (baudrate >> 8) & 0xff; /* Baudrate, MSB */
buf[2] = 0x00; /* Unknown/unused (?) */ buf[2] = 0x00; /* Unknown/unused (?) */
@ -182,33 +182,23 @@ static void log_dmm_packet(const uint8_t *buf)
buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]); buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
} }
static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data) static int get_and_handle_data(struct sr_dev_inst *sdi, int dmm, void *info)
{ {
struct sr_dev_inst *sdi;
struct dev_context *devc; struct dev_context *devc;
uint8_t buf[CHUNK_SIZE], *pbuf;
int i, ret, len, num_databytes_in_chunk; int i, ret, len, num_databytes_in_chunk;
uint8_t buf[CHUNK_SIZE];
uint8_t *pbuf;
static gboolean first_run = TRUE, synced_on_first_packet = FALSE;
static uint64_t data_byte_counter = 0;
(void)fd;
(void)revents;
sdi = cb_data;
devc = sdi->priv; devc = sdi->priv;
pbuf = devc->protocol_buf; pbuf = devc->protocol_buf;
/* On the first run, we need to init the HID chip. */ /* On the first run, we need to init the HID chip. */
if (first_run) { if (devc->first_run) {
/* Note: The baudrate is DMM-specific (UT61D: 19230). */
if ((ret = hid_chip_init(devc, udmms[dmm].baudrate)) != SR_OK) { if ((ret = hid_chip_init(devc, udmms[dmm].baudrate)) != SR_OK) {
sr_err("HID chip init failed: %d.", ret); sr_err("HID chip init failed: %d.", ret);
return FALSE; return SR_ERR;
} }
memset(pbuf, 0x00, NUM_DATA_BYTES); memset(pbuf, 0x00, DMM_BUFSIZE);
first_run = FALSE; devc->first_run = FALSE;
} }
memset(&buf, 0x00, CHUNK_SIZE); memset(&buf, 0x00, CHUNK_SIZE);
@ -224,46 +214,62 @@ static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
if (ret < 0) { if (ret < 0) {
sr_err("USB receive error: %s.", libusb_error_name(ret)); sr_err("USB receive error: %s.", libusb_error_name(ret));
return FALSE; return SR_ERR;
} }
if (len != CHUNK_SIZE) { if (len != CHUNK_SIZE) {
sr_err("Short packet: received %d/%d bytes.", len, CHUNK_SIZE); sr_err("Short packet: received %d/%d bytes.", len, CHUNK_SIZE);
/* TODO: Print the bytes? */ /* TODO: Print the bytes? */
return FALSE; return SR_ERR;
} }
log_8byte_chunk((const uint8_t *)&buf); log_8byte_chunk((const uint8_t *)&buf);
if (buf[0] != 0xf0) { /* If there are no data bytes just return (without error). */
/* First time: Synchronize to the start of a packet. */ if (buf[0] == 0xf0)
if (!synced_on_first_packet) { return SR_OK;
if (!udmms[dmm].packet_valid(buf))
return TRUE;
synced_on_first_packet = TRUE;
sr_spew("Successfully synchronized on first packet.");
}
devc->bufoffset = 0;
/* Append the 1-7 data bytes of this chunk to pbuf. */
num_databytes_in_chunk = buf[0] & 0x0f; num_databytes_in_chunk = buf[0] & 0x0f;
for (i = 0; i < num_databytes_in_chunk; i++) for (i = 0; i < num_databytes_in_chunk; i++)
pbuf[data_byte_counter++] = buf[1 + i]; pbuf[devc->buflen++] = buf[1 + i];
/* TODO: Handle > 14 bytes in pbuf? Can this happen? */ /* Now look for packets in that data. */
if (data_byte_counter == NUM_DATA_BYTES) { while ((devc->buflen - devc->bufoffset) >= udmms[dmm].packet_size) {
log_dmm_packet(pbuf); if (udmms[dmm].packet_valid(pbuf + devc->bufoffset)) {
data_byte_counter = 0; log_dmm_packet(pbuf + devc->bufoffset);
decode_packet(sdi, dmm, pbuf + devc->bufoffset, info);
if (!udmms[dmm].packet_valid(pbuf)) { devc->bufoffset += udmms[dmm].packet_size;
sr_err("Invalid packet."); } else {
return TRUE; devc->bufoffset++;
}
decode_packet(sdi, dmm, pbuf, info);
memset(pbuf, 0x00, NUM_DATA_BYTES);
} }
} }
/* Move remaining bytes to beginning of buffer. */
for (i = 0; i < devc->buflen - devc->bufoffset; i++)
pbuf[i] = pbuf[devc->bufoffset + i];
devc->buflen -= devc->bufoffset;
return SR_OK;
}
static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
{
int ret;
struct sr_dev_inst *sdi;
struct dev_context *devc;
(void)fd;
(void)revents;
sdi = cb_data;
devc = sdi->priv;
if ((ret = get_and_handle_data(sdi, dmm, info)) != SR_OK)
return FALSE;
/* Abort acquisition if we acquired enough samples. */ /* Abort acquisition if we acquired enough samples. */
if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
sr_info("Requested number of samples reached."); sr_info("Requested number of samples reached.");

View File

@ -1,7 +1,7 @@
/* /*
* This file is part of the libsigrok project. * This file is part of the libsigrok project.
* *
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -60,11 +60,9 @@ struct dmm_info {
extern SR_PRIV struct dmm_info udmms[DMM_COUNT]; extern SR_PRIV struct dmm_info udmms[DMM_COUNT];
#define UT_D04_CABLE_USB_VID 0x1a86
#define UT_D04_CABLE_USB_DID 0xe008
#define CHUNK_SIZE 8 #define CHUNK_SIZE 8
#define NUM_DATA_BYTES 14
#define DMM_BUFSIZE 256
/** Private, per-device-instance driver context. */ /** Private, per-device-instance driver context. */
struct dev_context { struct dev_context {
@ -82,7 +80,11 @@ struct dev_context {
struct sr_usb_dev_inst *usb; struct sr_usb_dev_inst *usb;
uint8_t protocol_buf[14]; gboolean first_run;
uint8_t protocol_buf[DMM_BUFSIZE];
uint8_t bufoffset;
uint8_t buflen;
}; };
SR_PRIV int receive_data_UNI_T_UT61D(int fd, int revents, void *cb_data); SR_PRIV int receive_data_UNI_T_UT61D(int fd, int revents, void *cb_data);