tekpower-dmm: Generalize & first-class TP4000ZC driver.

Prepare the tekpower-dmm driver to be able to support various simple
serial port based DMMs.

Also, make a 'tekpower-tp4000zc' "first-class" driver which is currently
the only user of this generic driver.
This commit is contained in:
Uwe Hermann 2012-12-01 19:28:36 +01:00
parent 1fbab46626
commit 729b01f988
4 changed files with 106 additions and 43 deletions

View File

@ -3,6 +3,7 @@
*
* Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
*
* 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
@ -28,8 +29,6 @@
#include "libsigrok-internal.h"
#include "protocol.h"
#define SERIALCOMM "2400/8n1"
static const int hwopts[] = {
SR_HWOPT_CONN,
SR_HWOPT_SERIALCOMM,
@ -48,8 +47,22 @@ static const char *probe_names[] = {
NULL,
};
SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info;
static struct sr_dev_driver *di = &tekpower_dmm_driver_info;
SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
static struct sr_dev_driver *di_tekpower_tp4000zc = &tekpower_tp4000zc_driver_info;
/* After hw_init() this will point to a device-specific entry (see above). */
static struct sr_dev_driver *di = NULL;
SR_PRIV struct dmm_info dmms[] = {
{
"TekPower", "TP4000ZC",
"2400/8n1", 2400,
FS9721_PACKET_SIZE,
sr_fs9721_packet_valid,
sr_fs9721_parse,
dmm_details_tp4000zc,
},
};
/* Properly close and free all devices. */
static int clear_instances(void)
@ -77,7 +90,7 @@ static int clear_instances(void)
return SR_OK;
}
static int hw_init(void)
static int hw_init(int dmm)
{
struct drv_context *drvc;
@ -86,12 +99,21 @@ static int hw_init(void)
return SR_ERR_MALLOC;
}
if (dmm == TEKPOWER_TP4000ZC)
di = di_tekpower_tp4000zc;
sr_dbg("Selected '%s' subdriver.", di->name);
di->priv = drvc;
return SR_OK;
}
static GSList *lcd14_scan(const char *conn, const char *serialcomm)
static int hw_init_tekpower_tp4000zc(void)
{
return hw_init(TEKPOWER_TP4000ZC);
}
static GSList *scan(const char *conn, const char *serialcomm, int dmm)
{
struct sr_dev_inst *sdi;
struct drv_context *drvc;
@ -124,8 +146,9 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
/* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf);
ret = serial_stream_detect(serial, buf, &len, FS9721_PACKET_SIZE,
sr_fs9721_packet_valid, 1000, 2400);
ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
dmms[dmm].packet_valid, 1000,
dmms[dmm].baudrate);
if (ret != SR_OK)
goto scan_cleanup;
@ -136,14 +159,14 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
* combination of the nonstandard cable that ships with this device and
* the serial port or USB to serial adapter.
*/
dropped = len - FS9721_PACKET_SIZE;
if (dropped > 2 * FS9721_PACKET_SIZE)
dropped = len - dmms[dmm].packet_size;
if (dropped > 2 * dmms[dmm].packet_size)
sr_warn("Had to drop too much data.");
sr_info("Found device on port %s.", conn);
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
"TP4000ZC", "")))
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor,
dmms[dmm].device, "")))
goto scan_cleanup;
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
@ -172,6 +195,7 @@ static GSList *hw_scan(GSList *options)
struct sr_hwopt *opt;
GSList *l, *devices;
const char *conn, *serialcomm;
int dmm;
conn = serialcomm = NULL;
for (l = options; l; l = l->next) {
@ -188,12 +212,15 @@ static GSList *hw_scan(GSList *options)
if (!conn)
return NULL;
if (!strcmp(di->name, "tekpower-tp4000zc"))
dmm = 0;
if (serialcomm) {
/* Use the provided comm specs. */
devices = lcd14_scan(conn, serialcomm);
devices = scan(conn, serialcomm, dmm);
} else {
/* Try the default. */
devices = lcd14_scan(conn, SERIALCOMM);
devices = scan(conn, dmms[dmm].conn, dmm);
}
return devices;
@ -309,6 +336,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
struct sr_datafeed_header header;
struct sr_datafeed_meta_analog meta;
struct dev_context *devc;
int (*receive_data)(int, int, void *) = NULL;
if (!(devc = sdi->priv)) {
sr_err("sdi->priv was NULL.");
@ -341,9 +369,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
meta.num_probes = 1;
sr_session_send(devc->cb_data, &packet);
if (!strcmp(di->name, "tekpower-tp4000zc"))
receive_data = tekpower_tp4000zc_receive_data;
/* Poll every 50ms, or whenever some data comes in. */
sr_source_add(devc->serial->fd, G_IO_IN, 50,
tekpower_dmm_receive_data, (void *)sdi);
receive_data, (void *)sdi);
return SR_OK;
}
@ -374,11 +405,11 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
return SR_OK;
}
SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info = {
.name = "tekpower-dmm",
.longname = "TekPower/Digitek TP4000ZC/DT4000ZC DMM",
SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = {
.name = "tekpower-tp4000zc",
.longname = "TekPower TP4000ZC",
.api_version = 1,
.init = hw_init,
.init = hw_init_tekpower_tp4000zc,
.cleanup = hw_cleanup,
.scan = hw_scan,
.dev_list = hw_dev_list,

View File

@ -2,6 +2,7 @@
* This file is part of the sigrok project.
*
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
*
* 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
@ -26,9 +27,6 @@
#include "libsigrok-internal.h"
#include "protocol.h"
/* User-defined FS9721_LP3 flag 'c2c1_10' means temperature on this DMM. */
#define is_temperature info.is_c2c1_10
static void log_dmm_packet(const uint8_t *buf)
{
sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
@ -37,14 +35,26 @@ static void log_dmm_packet(const uint8_t *buf)
buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
}
/* Now see what the value means, and pass that on. */
static void fs9721_serial_handle_packet(const uint8_t *buf,
struct dev_context *devc)
SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info)
{
struct fs9721_info *info_local;
info_local = (struct fs9721_info *)info;
/* User-defined FS9721_LP3 flag 'c2c1_10' means temperature. */
if (info_local->is_c2c1_10) {
analog->mq = SR_MQ_TEMPERATURE;
/* No Kelvin or Fahrenheit from the device, just Celsius. */
analog->unit = SR_UNIT_CELSIUS;
}
}
static void handle_packet(const uint8_t *buf, struct dev_context *devc,
int dmm, void *info)
{
float floatval;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog *analog;
struct fs9721_info info;
log_dmm_packet(buf);
@ -56,14 +66,10 @@ static void fs9721_serial_handle_packet(const uint8_t *buf,
analog->num_samples = 1;
analog->mq = -1;
sr_fs9721_parse(buf, &floatval, analog, &info);
dmms[dmm].packet_parse(buf, &floatval, analog, info);
analog->data = &floatval;
if (is_temperature) {
analog->mq = SR_MQ_TEMPERATURE;
/* No Kelvin or Fahrenheit from the device, just Celsius. */
analog->unit = SR_UNIT_CELSIUS;
}
dmms[dmm].dmm_details(analog, info);
if (analog->mq != -1) {
/* Got a measurement. */
@ -76,7 +82,7 @@ static void fs9721_serial_handle_packet(const uint8_t *buf,
g_free(analog);
}
static void handle_new_data(struct dev_context *devc)
static void handle_new_data(struct dev_context *devc, int dmm, void *info)
{
int len, i, offset = 0;
@ -90,10 +96,10 @@ static void handle_new_data(struct dev_context *devc)
devc->buflen += len;
/* Now look for packets in that data. */
while ((devc->buflen - offset) >= FS9721_PACKET_SIZE) {
if (sr_fs9721_packet_valid(devc->buf + offset)) {
fs9721_serial_handle_packet(devc->buf + offset, devc);
offset += FS9721_PACKET_SIZE;
while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
if (dmms[dmm].packet_valid(devc->buf + offset)) {
handle_packet(devc->buf + offset, devc, dmm, info);
offset += dmms[dmm].packet_size;
} else {
offset++;
}
@ -105,7 +111,7 @@ static void handle_new_data(struct dev_context *devc)
devc->buflen -= offset;
}
SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
{
struct sr_dev_inst *sdi;
struct dev_context *devc;
@ -120,7 +126,7 @@ SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
if (revents == G_IO_IN) {
/* Serial data arrived. */
handle_new_data(devc);
handle_new_data(devc, dmm, info);
}
if (devc->num_samples >= devc->limit_samples) {
@ -131,3 +137,10 @@ SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
return TRUE;
}
SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data)
{
struct fs9721_info info;
return receive_data(fd, revents, TEKPOWER_TP4000ZC, &info, cb_data);
}

View File

@ -29,7 +29,25 @@
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
#define DMM_BUFSIZE 256
enum {
TEKPOWER_TP4000ZC,
};
struct dmm_info {
char *vendor;
char *device;
char *conn;
uint32_t baudrate;
int packet_size;
gboolean (*packet_valid)(const uint8_t *);
int (*packet_parse)(const uint8_t *, float *,
struct sr_datafeed_analog *, void *);
void (*dmm_details)(struct sr_datafeed_analog *, void *);
};
SR_PRIV struct dmm_info dmms[1];
#define DMM_BUFSIZE 256
/** Private, per-device-instance driver context. */
struct dev_context {
@ -49,6 +67,7 @@ struct dev_context {
int buflen;
};
SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data);
SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data);
SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info);
#endif

View File

@ -114,7 +114,7 @@ extern SR_PRIV struct sr_dev_driver flukedmm_driver_info;
extern SR_PRIV struct sr_dev_driver radioshackdmm_driver_info;
#endif
#ifdef HAVE_HW_TEKPOWER_DMM
extern SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info;
extern SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
#endif
#ifdef HAVE_HW_UNI_T_DMM
extern SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
@ -169,7 +169,7 @@ static struct sr_dev_driver *drivers_list[] = {
&radioshackdmm_driver_info,
#endif
#ifdef HAVE_HW_TEKPOWER_DMM
&tekpower_dmm_driver_info,
&tekpower_tp4000zc_driver_info,
#endif
#ifdef HAVE_HW_UNI_T_DMM
&uni_t_ut61d_driver_info,