From: Uwe Hermann Date: Mon, 22 Jul 2013 17:48:15 +0000 (+0200) Subject: center-3xx: Initial Center 309 / Voltcraft K204 support. X-Git-Tag: libsigrok-0.2.2~98 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4a8bbed76d127663e62d83f6a3ac5b7e315e5f00;p=libsigrok.git center-3xx: Initial Center 309 / Voltcraft K204 support. --- diff --git a/hardware/center-3xx/api.c b/hardware/center-3xx/api.c index 5f61f9bd..6c01f4ca 100644 --- a/hardware/center-3xx/api.c +++ b/hardware/center-3xx/api.c @@ -20,41 +20,148 @@ #include "protocol.h" -SR_PRIV struct sr_dev_driver center_3xx_driver_info; -static struct sr_dev_driver *di = ¢er_3xx_driver_info; +static const int32_t hwopts[] = { + SR_CONF_CONN, + SR_CONF_SERIALCOMM, +}; + +static const int32_t hwcaps[] = { + SR_CONF_THERMOMETER, + SR_CONF_LIMIT_SAMPLES, + SR_CONF_LIMIT_MSEC, + SR_CONF_CONTINUOUS, +}; + +static const char *probe_names[] = { + "T1", "T2", "T3", "T4", + NULL, +}; + +SR_PRIV struct sr_dev_driver center_309_driver_info; +SR_PRIV struct sr_dev_driver voltcraft_k204_driver_info; + +SR_PRIV const struct center_dev_info center_devs[] = { + { + "Center", "309", "9600/8n1", 4, 32000, 45, + center_3xx_packet_valid, + ¢er_309_driver_info, receive_data_CENTER_309, + }, + { + "Voltcraft", "K204", "9600/8n1", 4, 32000, 45, + center_3xx_packet_valid, + &voltcraft_k204_driver_info, receive_data_VOLTCRAFT_K204, + }, +}; -static int init(struct sr_context *sr_ctx) +static int dev_clear(int idx) { - return std_init(sr_ctx, di, LOG_PREFIX); + return std_dev_clear(center_devs[idx].di, NULL); } -static GSList *scan(GSList *options) +static int init(struct sr_context *sr_ctx, int idx) { + sr_dbg("Selected '%s' subdriver.", center_devs[idx].di->name); + + return std_init(sr_ctx, center_devs[idx].di, LOG_PREFIX); +} + +static GSList *center_scan(const char *conn, const char *serialcomm, int idx) +{ + int i; + struct sr_dev_inst *sdi; struct drv_context *drvc; + struct dev_context *devc; + struct sr_probe *probe; + struct sr_serial_dev_inst *serial; GSList *devices; - (void)options; + if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) + return NULL; + + if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) + return NULL; + drvc = center_devs[idx].di->priv; devices = NULL; - drvc = di->priv; - drvc->instances = NULL; + serial_flush(serial); + + sr_info("Found device on port %s.", conn); + + if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, center_devs[idx].vendor, + center_devs[idx].device, NULL))) + goto scan_cleanup; + + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { + sr_err("Device context malloc failed."); + goto scan_cleanup; + } + + sdi->inst_type = SR_INST_SERIAL; + sdi->conn = serial; + + sdi->priv = devc; + sdi->driver = center_devs[idx].di; + + for (i = 0; i < center_devs[idx].num_channels; i++) { + if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, + TRUE, probe_names[i]))) + goto scan_cleanup; + sdi->probes = g_slist_append(sdi->probes, probe); + } + + drvc->instances = g_slist_append(drvc->instances, sdi); + devices = g_slist_append(devices, sdi); + +scan_cleanup: + serial_close(serial); return devices; } -static GSList *dev_list(void) +static GSList *scan(GSList *options, int idx) { - return ((struct drv_context *)(di->priv))->instances; + struct sr_config *src; + GSList *l, *devices; + const char *conn, *serialcomm; + + conn = serialcomm = NULL; + 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; + case SR_CONF_SERIALCOMM: + serialcomm = g_variant_get_string(src->data, NULL); + break; + } + } + if (!conn) + return NULL; + + if (serialcomm) { + /* Use the provided comm specs. */ + devices = center_scan(conn, serialcomm, idx); + } else { + /* Try the default. */ + devices = center_scan(conn, center_devs[idx].conn, idx); + } + + return devices; } -static int dev_clear(void) +static GSList *dev_list(int idx) { - return std_dev_clear(di, NULL); + return ((struct drv_context *)(center_devs[idx].di->priv))->instances; } static int dev_open(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_serial_dev_inst *serial; + + serial = sdi->conn; + if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) + return SR_ERR; sdi->status = SR_ST_ACTIVE; @@ -63,105 +170,141 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_serial_dev_inst *serial; - sdi->status = SR_ST_INACTIVE; + serial = sdi->conn; + if (serial && serial->fd != -1) { + serial_close(serial); + sdi->status = SR_ST_INACTIVE; + } return SR_OK; } -static int cleanup(void) -{ - return dev_clear(); -} - -static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi) +static int cleanup(int idx) { - int ret; - - (void)sdi; - (void)data; - - ret = SR_OK; - switch (key) { - default: - return SR_ERR_NA; - } - - return ret; + return dev_clear(idx); } -static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi) +static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) { - int ret; - - (void)data; + struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - ret = SR_OK; - switch (key) { + devc = sdi->priv; + + switch (id) { + case SR_CONF_LIMIT_SAMPLES: + if (g_variant_get_uint64(data) == 0) + return SR_ERR_ARG; + devc->limit_samples = g_variant_get_uint64(data); + break; + case SR_CONF_LIMIT_MSEC: + if (g_variant_get_uint64(data) == 0) + return SR_ERR_ARG; + devc->limit_msec = g_variant_get_uint64(data); + 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) { - int ret; - (void)sdi; - (void)data; - ret = SR_OK; switch (key) { + 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 *cb_data, int idx) { - (void)sdi; - (void)cb_data; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; + devc = sdi->priv; + devc->cb_data = cb_data; + devc->num_samples = 0; + devc->starttime = g_get_monotonic_time(); + + /* Send header packet to the session bus. */ + std_session_send_df_header(cb_data, LOG_PREFIX); + + /* Poll every 500ms, or whenever some data comes in. */ + serial = sdi->conn; + sr_source_add(serial->fd, G_IO_IN, 500, + center_devs[idx].receive_data, (void *)sdi); + 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; - - return SR_OK; + return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, + sdi->conn, LOG_PREFIX); } -SR_PRIV struct sr_dev_driver center_3xx_driver_info = { - .name = "center-3xx", - .longname = "Center 3xx", - .api_version = 1, - .init = init, - .cleanup = cleanup, - .scan = scan, - .dev_list = dev_list, - .dev_clear = dev_clear, - .config_get = config_get, - .config_set = config_set, - .config_list = config_list, - .dev_open = dev_open, - .dev_close = dev_close, - .dev_acquisition_start = dev_acquisition_start, - .dev_acquisition_stop = dev_acquisition_stop, - .priv = NULL, +/* Driver-specific API function wrappers */ +#define HW_INIT(X) \ +static int init_##X(struct sr_context *sr_ctx) { return init(sr_ctx, X); } +#define HW_CLEANUP(X) \ +static int cleanup_##X(void) { return cleanup(X); } +#define HW_SCAN(X) \ +static GSList *scan_##X(GSList *options) { return scan(options, X); } +#define HW_DEV_LIST(X) \ +static GSList *dev_list_##X(void) { return dev_list(X); } +#define HW_DEV_CLEAR(X) \ +static int dev_clear_##X(void) { return dev_clear(X); } +#define HW_DEV_ACQUISITION_START(X) \ +static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \ +void *cb_data) { return dev_acquisition_start(sdi, cb_data, X); } + +/* Driver structs and API function wrappers */ +#define DRV(ID, ID_UPPER, NAME, LONGNAME) \ +HW_INIT(ID_UPPER) \ +HW_CLEANUP(ID_UPPER) \ +HW_SCAN(ID_UPPER) \ +HW_DEV_LIST(ID_UPPER) \ +HW_DEV_CLEAR(ID_UPPER) \ +HW_DEV_ACQUISITION_START(ID_UPPER) \ +SR_PRIV struct sr_dev_driver ID##_driver_info = { \ + .name = NAME, \ + .longname = LONGNAME, \ + .api_version = 1, \ + .init = init_##ID_UPPER, \ + .cleanup = cleanup_##ID_UPPER, \ + .scan = scan_##ID_UPPER, \ + .dev_list = dev_list_##ID_UPPER, \ + .dev_clear = dev_clear_##ID_UPPER, \ + .config_get = NULL, \ + .config_set = config_set, \ + .config_list = config_list, \ + .dev_open = dev_open, \ + .dev_close = dev_close, \ + .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \ + .dev_acquisition_stop = dev_acquisition_stop, \ + .priv = NULL, \ }; + +DRV(center_309, CENTER_309, "center-309", "Center 309") +DRV(voltcraft_k204, VOLTCRAFT_K204, "voltcraft-k204", "Voltcraft K204") diff --git a/hardware/center-3xx/protocol.c b/hardware/center-3xx/protocol.c index 77427807..f9e5e815 100644 --- a/hardware/center-3xx/protocol.c +++ b/hardware/center-3xx/protocol.c @@ -20,12 +20,192 @@ #include "protocol.h" -SR_PRIV int center_3xx_receive_data(int fd, int revents, void *cb_data) +struct center_info { + float temp[4]; + gboolean rec, std, max, min, maxmin, t1t2, rel, hold, lowbat, celsius; + gboolean memfull, autooff; + gboolean mode_std, mode_rel, mode_max, mode_min, mode_maxmin; +}; + +static int center_send(struct sr_serial_dev_inst *serial, const char *cmd) { - (void)fd; + int ret; + + if ((ret = serial_write(serial, cmd, strlen(cmd))) < 0) { + sr_err("Error sending '%s' command: %d.", cmd, ret); + return SR_ERR; + } + + return SR_OK; +} + +SR_PRIV gboolean center_3xx_packet_valid(const uint8_t *buf) +{ + return (buf[0] == 0x02 && buf[44] == 0x03); +} + +static void log_packet(const uint8_t *buf, int idx) +{ + int i; + GString *s; + + s = g_string_sized_new(100); + g_string_printf(s, "Packet: "); + for (i = 0; i < center_devs[idx].packet_size; i++) + g_string_append_printf(s, "%02x ", buf[i]); + sr_spew("%s", s->str); + g_string_free(s, TRUE); +} + +static int packet_parse(const uint8_t *buf, int idx, struct center_info *info) +{ + int i; + uint16_t temp_u16; + + log_packet(buf, idx); + + /* Byte 0: Always 0x02. */ + + /* Byte 1: Various status bits. */ + info->rec = (buf[1] & (1 << 0)) != 0; + info->mode_std = (((buf[1] >> 1) & 0x3) == 0); + info->mode_max = (((buf[1] >> 1) & 0x3) == 1); + info->mode_min = (((buf[1] >> 1) & 0x3) == 2); + info->mode_maxmin = (((buf[1] >> 1) & 0x3) == 3); + /* TODO: Rel. Not available on all models. */ + info->t1t2 = (buf[1] & (1 << 3)) != 0; + info->rel = (buf[1] & (1 << 4)) != 0; + info->hold = (buf[1] & (1 << 5)) != 0; + info->lowbat = (buf[1] & (1 << 6)) != 0; + info->celsius = (buf[1] & (1 << 7)) != 0; + + /* Byte 2: Further status bits. */ + info->memfull = (buf[2] & (1 << 0)) != 0; + info->autooff = (buf[2] & (1 << 7)) != 0; + + /* Byte 7+8/9+10/11+12/13+14: channel T1/T2/T3/T4 temperature. */ + for (i = 0; i < 4; i++) { + temp_u16 = buf[8 + (i * 2)]; + temp_u16 |= ((uint16_t)buf[7 + (i * 2)] << 8); + info->temp[i] = (float)temp_u16; + } + + /* Byte 43: Specifies whether we need to divide the value(s) by 10. */ + for (i = 0; i < 4; i++) { + /* Bit = 0: Divide by 10. Bit = 1: Don't divide by 10. */ + if ((buf[43] & (1 << i)) == 0) + info->temp[i] /= 10; + } + + /* Bytes 39-42: Overflow/overlimit bits, depending on mode. */ + for (i = 0; i < 4; i++) { + if (info->mode_std && ((buf[39] & (1 << i)) != 0)) + info->temp[i] = INFINITY; + /* TODO: Rel. Not available on all models. */ + // if (info->mode_rel && ((buf[40] & (1 << i)) != 0)) + // info->temp[i] = INFINITY; + if (info->mode_max && ((buf[41] & (1 << i)) != 0)) + info->temp[i] = INFINITY; + if (info->mode_min && ((buf[42] & (1 << i)) != 0)) + info->temp[i] = INFINITY; + /* TODO: Minmax? */ + } + + /* Byte 44: Always 0x03. */ + + return SR_OK; +} + +static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx) +{ + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + struct dev_context *devc; + struct center_info info; + GSList *l; + int i, ret; + + devc = sdi->priv; - const struct sr_dev_inst *sdi; + memset(&analog, 0, sizeof(struct sr_datafeed_analog)); + memset(&info, 0, sizeof(struct center_info)); + + ret = packet_parse(buf, idx, &info); + if (ret < 0) { + sr_err("Failed to parse packet."); + return SR_ERR; + } + + /* Common values for all 4 probes. */ + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + analog.mq = SR_MQ_TEMPERATURE; + analog.unit = (info.celsius) ? SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT; + analog.num_samples = 1; + + /* Send the values for T1 - T4. */ + for (i = 0; i < 4; i++) { + l = NULL; + l = g_slist_append(l, g_slist_nth_data(sdi->probes, i)); + analog.probes = l; + analog.data = &(info.temp[i]); + sr_session_send(devc->cb_data, &packet); + g_slist_free(l); + } + + devc->num_samples++; + + return SR_OK; +} + +/* Return TRUE if a full packet was parsed, FALSE otherwise. */ +static gboolean handle_new_data(struct sr_dev_inst *sdi, int idx) +{ + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + int len, i, offset = 0, ret = FALSE; + + devc = sdi->priv; + serial = sdi->conn; + + /* Try to get as much data as the buffer can hold. */ + len = SERIAL_BUFSIZE - devc->buflen; + len = serial_read(serial, devc->buf + devc->buflen, len); + if (len < 1) { + sr_err("Serial port read error: %d.", len); + return FALSE; + } + + devc->buflen += len; + + /* Now look for packets in that data. */ + while ((devc->buflen - offset) >= center_devs[idx].packet_size) { + if (center_devs[idx].packet_valid(devc->buf + offset)) { + handle_packet(devc->buf + offset, sdi, idx); + offset += center_devs[idx].packet_size; + ret = TRUE; + } else { + offset++; + } + } + + /* If we have any data left, move it to the beginning of our buffer. */ + for (i = 0; i < devc->buflen - offset; i++) + devc->buf[i] = devc->buf[offset + i]; + devc->buflen -= offset; + + return ret; +} + +static int receive_data(int fd, int revents, int idx, void *cb_data) +{ + struct sr_dev_inst *sdi; struct dev_context *devc; + int64_t t; + static gboolean request_new_packet = TRUE; + struct sr_serial_dev_inst *serial; + + (void)fd; if (!(sdi = cb_data)) return TRUE; @@ -33,9 +213,44 @@ SR_PRIV int center_3xx_receive_data(int fd, int revents, void *cb_data) if (!(devc = sdi->priv)) return TRUE; + serial = sdi->conn; + if (revents == G_IO_IN) { - /* TODO */ + /* New data arrived. */ + request_new_packet = handle_new_data(sdi, idx); + } else { + /* + * Timeout. Send "A" to request a packet, but then don't send + * further "A" commands until we received a full packet first. + */ + if (request_new_packet) { + center_send(serial, "A"); + request_new_packet = FALSE; + } + } + + if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { + sr_info("Requested number of samples reached."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } + + if (devc->limit_msec) { + t = (g_get_monotonic_time() - devc->starttime) / 1000; + if (t > (int64_t)devc->limit_msec) { + sr_info("Requested time limit reached."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } } return TRUE; } + +#define RECEIVE_DATA(ID_UPPER) \ +SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \ + return receive_data(fd, revents, ID_UPPER, cb_data); } + +/* Driver-specific receive_data() wrappers */ +RECEIVE_DATA(CENTER_309) +RECEIVE_DATA(VOLTCRAFT_K204) diff --git a/hardware/center-3xx/protocol.h b/hardware/center-3xx/protocol.h index f3bda566..10179db3 100644 --- a/hardware/center-3xx/protocol.h +++ b/hardware/center-3xx/protocol.h @@ -22,6 +22,9 @@ #define LIBSIGROK_HARDWARE_CENTER_3XX_PROTOCOL_H #include +#include +#include +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" @@ -35,18 +38,54 @@ #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) +/* Note: When adding entries here, don't forget to update CENTER_DEV_COUNT. */ +enum { + CENTER_309, + VOLTCRAFT_K204, +}; + +#define CENTER_DEV_COUNT 2 + +struct center_dev_info { + char *vendor; + char *device; + char *conn; + int num_channels; + uint32_t max_sample_points; + uint8_t packet_size; + gboolean (*packet_valid)(const uint8_t *); + struct sr_dev_driver *di; + int (*receive_data)(int, int, void *); +}; + +extern SR_PRIV const struct center_dev_info center_devs[CENTER_DEV_COUNT]; + +#define SERIAL_BUFSIZE 256 + /** Private, per-device-instance driver context. */ struct dev_context { - /* Model-specific information */ + /** The current sampling limit (in number of samples). */ + uint64_t limit_samples; - /* Acquisition settings */ + /** The current sampling limit (in ms). */ + uint64_t limit_msec; - /* Operational state */ + /** Opaque pointer passed in by the frontend. */ + void *cb_data; - /* Temporary state across callbacks */ + /** The current number of already received samples. */ + uint64_t num_samples; + int64_t starttime; + + uint8_t buf[SERIAL_BUFSIZE]; + int bufoffset; + int buflen; }; -SR_PRIV int center_3xx_receive_data(int fd, int revents, void *cb_data); +SR_PRIV gboolean center_3xx_packet_valid(const uint8_t *buf); + +SR_PRIV int receive_data_CENTER_309(int fd, int revents, void *cb_data); +SR_PRIV int receive_data_VOLTCRAFT_K204(int fd, int revents, void *cb_data); #endif diff --git a/hwdriver.c b/hwdriver.c index 01f3c6d9..9d68a1fc 100644 --- a/hwdriver.c +++ b/hwdriver.c @@ -108,7 +108,8 @@ extern SR_PRIV struct sr_dev_driver brymen_bm857_driver_info; extern SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info; #endif #ifdef HAVE_HW_CENTER_3XX -extern SR_PRIV struct sr_dev_driver center_3xx_driver_info; +extern SR_PRIV struct sr_dev_driver center_309_driver_info; +extern SR_PRIV struct sr_dev_driver voltcraft_k204_driver_info; #endif #ifdef HAVE_HW_COLEAD_SLM extern SR_PRIV struct sr_dev_driver colead_slm_driver_info; @@ -214,7 +215,8 @@ static struct sr_dev_driver *drivers_list[] = { &cem_dt_885x_driver_info, #endif #ifdef HAVE_HW_CENTER_3XX - ¢er_3xx_driver_info, + ¢er_309_driver_info, + &voltcraft_k204_driver_info, #endif #ifdef HAVE_HW_COLEAD_SLM &colead_slm_driver_info,