From: Dave Buechi Date: Sat, 17 Aug 2019 21:36:14 +0000 (+0200) Subject: Initial support for MASTECH MS6514 thermometer X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=23669c3df33a3ea884afabf4b7a8cd6fe4a75824;p=libsigrok.git Initial support for MASTECH MS6514 thermometer --- diff --git a/Makefile.am b/Makefile.am index b9594883..07bc68c6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -446,6 +446,12 @@ src_libdrivers_la_SOURCES += \ src/hardware/manson-hcs-3xxx/protocol.c \ src/hardware/manson-hcs-3xxx/api.c endif +if HW_MASTECH_MS6514 +src_libdrivers_la_SOURCES += \ + src/hardware/mastech-ms6514/protocol.h \ + src/hardware/mastech-ms6514/protocol.c \ + src/hardware/mastech-ms6514/api.c +endif if HW_MAYNUO_M97 src_libdrivers_la_SOURCES += \ src/hardware/maynuo-m97/protocol.h \ diff --git a/configure.ac b/configure.ac index cd3346c8..6b2d36af 100644 --- a/configure.ac +++ b/configure.ac @@ -291,6 +291,7 @@ SR_DRIVER([Lascar EL-USB], [lascar-el-usb], [libusb]) SR_DRIVER([LeCroy LogicStudio], [lecroy-logicstudio], [libusb]) SR_DRIVER([LeCroy X-Stream], [lecroy-xstream]) SR_DRIVER([Manson HCS-3xxx], [manson-hcs-3xxx], [serial_comm]) +SR_DRIVER([Mastech MS6514], [mastech-ms6514]) SR_DRIVER([maynuo-m97], [maynuo-m97]) SR_DRIVER([MIC 985xx], [mic-985xx], [serial_comm]) SR_DRIVER([Microchip PICkit2], [microchip-pickit2], [libusb]) diff --git a/src/hardware/mastech-ms6514/api.c b/src/hardware/mastech-ms6514/api.c new file mode 100644 index 00000000..7173cb3c --- /dev/null +++ b/src/hardware/mastech-ms6514/api.c @@ -0,0 +1,219 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2019 Dave Buechi + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include "protocol.h" + +static const uint32_t scanopts[] = { + SR_CONF_CONN, + SR_CONF_SERIALCOMM, +}; + +static const uint32_t drvopts[] = { + SR_CONF_THERMOMETER, +}; + +static const uint32_t devopts[] = { + SR_CONF_CONTINUOUS, + SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, + SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, + SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, +}; + +static const char *channel_names[] = { + "T1", "T2", "T1-T2", +}; + +static const char *data_sources[] = { + "Live", "Memory", +}; + +static GSList *scan(struct sr_dev_driver *di, GSList *options) +{ + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + struct sr_dev_inst *sdi; + struct sr_config *src; + GSList *devices, *l; + const char *conn, *serialcomm; + uint8_t buf[2 * MASTECH_MS6514_FRAME_SIZE]; + size_t len, i; + + len = sizeof(buf); + devices = NULL; + 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) + serialcomm = "9600/8n1"; + + serial = sr_serial_dev_inst_new(conn, serialcomm); + + if (serial_open(serial, SERIAL_RDONLY) != SR_OK) + return NULL; + + sr_info("Probing serial port %s.", conn); + + serial_flush(serial); + + /* Let's get a bit of data and see if we can find a packet. */ + if (serial_stream_detect(serial, buf, &len, (2 * MASTECH_MS6514_FRAME_SIZE), + mastech_ms6514_packet_valid, 500) != SR_OK) + goto scan_cleanup; + + sr_info("Found device on port %s.", conn); + + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->status = SR_ST_INACTIVE; + sdi->vendor = g_strdup("MASTECH"); + sdi->model = g_strdup("MS6514"); + devc = g_malloc0(sizeof(struct dev_context)); + devc->data_source = DEFAULT_DATA_SOURCE; + sdi->inst_type = SR_INST_SERIAL; + sdi->conn = serial; + sdi->priv = devc; + + for (i = 0; i < ARRAY_SIZE(channel_names); i++) + sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]); + + devices = g_slist_append(devices, sdi); + +scan_cleanup: + serial_close(serial); + + return std_scan_complete(di, devices); +} + +static int config_get(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) +{ + struct dev_context *devc = sdi->priv; + + (void)cg; + + switch (key) { + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_LIMIT_MSEC: + return sr_sw_limits_config_get(&devc->limits, key, data); + case SR_CONF_DATA_SOURCE: + *data = g_variant_new_string(data_sources[devc->data_source]); + break; + default: + return SR_ERR_NA; + } + + return SR_OK; +} + +static int config_set(uint32_t key, GVariant *data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) +{ + struct dev_context *devc; + int idx; + + (void)cg; + + devc = sdi->priv; + + switch (key) { + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_LIMIT_MSEC: + return sr_sw_limits_config_set(&devc->limits, key, data); + case SR_CONF_DATA_SOURCE: + if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0) + return SR_ERR_ARG; + devc->data_source = idx; + break; + default: + return SR_ERR_NA; + } + + return SR_OK; +} + +static int config_list(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) +{ + switch (key) { + case SR_CONF_SCAN_OPTIONS: + case SR_CONF_DEVICE_OPTIONS: + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); + case SR_CONF_DATA_SOURCE: + *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources)); + break; + default: + return SR_ERR_NA; + } + + return SR_OK; +} + +static int dev_acquisition_start(const struct sr_dev_inst *sdi) +{ + struct sr_serial_dev_inst *serial; + struct dev_context *devc; + uint8_t command; + + serial = sdi->conn; + devc = sdi->priv; + + sr_sw_limits_acquisition_start(&devc->limits); + std_session_send_df_header(sdi); + + if (devc->data_source == DATA_SOURCE_MEMORY) { + command = CMD_GET_STORED; + serial_write_blocking(serial, &command, sizeof(command), 0); + } + + serial_source_add(sdi->session, serial, G_IO_IN, MASTECH_MS6514_BUF_SIZE, + mastech_ms6514_receive_data, (void *)sdi); + + return SR_OK; +} + +static struct sr_dev_driver mastech_ms6514_driver_info = { + .name = "mastech-ms6514", + .longname = "MASTECH MS6514", + .api_version = 1, + .init = std_init, + .cleanup = std_cleanup, + .scan = scan, + .dev_list = std_dev_list, + .dev_clear = std_dev_clear, + .config_get = config_get, + .config_set = config_set, + .config_list = config_list, + .dev_open = std_serial_dev_open, + .dev_close = std_serial_dev_close, + .dev_acquisition_start = dev_acquisition_start, + .dev_acquisition_stop = std_serial_dev_acquisition_stop, + .context = NULL, +}; +SR_REGISTER_DEV_DRIVER(mastech_ms6514_driver_info); diff --git a/src/hardware/mastech-ms6514/protocol.c b/src/hardware/mastech-ms6514/protocol.c new file mode 100644 index 00000000..ae001933 --- /dev/null +++ b/src/hardware/mastech-ms6514/protocol.c @@ -0,0 +1,249 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2019 Dave Buechi + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "protocol.h" + +static const uint8_t channel_assignment[16][2] = { + /* MAIN AUX */ + {0, 1}, /* T1 T2 */ + {1, 0}, /* T2 T1 */ + {2, 0}, /* T1-T2 T1 */ + {2, 1}, /* T1-T2 T2 */ + {0, 0}, /* T1 T1 MAX */ + {1, 1}, /* T2 T2 MAX */ + {2, 2}, /* T1-T2 T1-T2 MAX */ + {2, 2}, /* T1-T2 T1-T2 MAX */ + {0, 0}, /* T1 T1 MIN */ + {1, 1}, /* T2 T2 MIN */ + {2, 2}, /* T1-T2 T1-T2 MIN */ + {2, 2}, /* T1-T2 T1-T2 MIN */ + {0, 0}, /* T1 T1 AVG */ + {1, 1}, /* T2 T2 AVG */ + {2, 2}, /* T1-T2 T1-T2 AVG */ + {2, 2}, /* T1-T2 T1-T2 AVG */ +}; + +SR_PRIV gboolean mastech_ms6514_packet_valid(const uint8_t *buf) +{ + if ((buf[0] == 0x65) && (buf[1] == 0x14) && + (buf[16] == 0x0D) && (buf[17] == 0x0A)) + return TRUE; + + return FALSE; +} + +static uint64_t mastech_ms6514_flags(const uint8_t *buf, const uint8_t channel_index) +{ + uint64_t flags; + + flags = 0; + if ((buf[10] & 0x40) == 0x40) + flags |= SR_MQFLAG_HOLD; + + if (channel_index == 0) { + if ((buf[11] & 0x03) > 0x01) + flags |= SR_MQFLAG_RELATIVE; + } + + if (channel_index == 1) { + switch (buf[12] & 0x03) { + case 0x01: + flags |= SR_MQFLAG_MAX; + break; + case 0x02: + flags |= SR_MQFLAG_MIN; + break; + case 0x03: + flags |= SR_MQFLAG_AVG; + break; + } + } + + return flags; +} + +static enum sr_unit mastech_ms6514_unit(const uint8_t *buf) +{ + enum sr_unit unit; + + switch (buf[10] & 0x03) { + case 0x01: + unit = SR_UNIT_CELSIUS; + break; + case 0x02: + unit = SR_UNIT_FAHRENHEIT; + break; + case 0x03: + unit = SR_UNIT_KELVIN; + break; + default: + unit = SR_UNIT_UNITLESS; + break; + } + + return unit; +} + +static uint8_t mastech_ms6514_channel_assignment(const uint8_t *buf, const uint8_t index) +{ + return channel_assignment[((buf[12] & 0x03) << 2) + (buf[11] & 0x03)][index]; +} + +static uint8_t mastech_ms6514_data_source(const uint8_t *buf) +{ + if ((buf[2] & 0x01) == 0x01) + return DATA_SOURCE_MEMORY; + else + return DATA_SOURCE_LIVE; +} + +static float mastech_ms6514_temperature(const uint8_t *buf, const uint8_t channel_index, int *digits) +{ + float value; + uint8_t modifiers; + + *digits = 0; + value = (buf[5 + channel_index * 2] << 8) + buf[6 + channel_index * 2]; + modifiers = buf[11 + channel_index]; + + if ((modifiers & 0x80) == 0x80) + value = -value; + + if ((modifiers & 0x08) == 0x08) { + value /= 10.0; + *digits = 1; + } + + if ((modifiers & 0x40) == 0x40) + value = INFINITY; + + return value; +} + +static void mastech_ms6514_data(struct sr_dev_inst *sdi, const uint8_t *buf) +{ + struct dev_context *devc; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + struct sr_analog_encoding encoding; + struct sr_analog_meaning meaning; + struct sr_analog_spec spec; + struct sr_channel *ch; + float value; + int i, digits; + + devc = sdi->priv; + + if ((devc->data_source == DATA_SOURCE_MEMORY) && \ + (mastech_ms6514_data_source(buf) == DATA_SOURCE_LIVE)) { + sr_dev_acquisition_stop(sdi); + return; + } + + for (i = 0; i < MASTECH_MS6514_NUM_CHANNELS; i++) { + ch = g_slist_nth_data(sdi->channels, i); + if (!ch->enabled) + continue; + + value = mastech_ms6514_temperature(buf, i, &digits); + sr_analog_init(&analog, &encoding, &meaning, &spec, digits); + analog.num_samples = 1; + analog.data = &value; + analog.meaning->mq = SR_MQ_TEMPERATURE; + analog.meaning->unit = mastech_ms6514_unit(buf); + analog.meaning->mqflags = mastech_ms6514_flags(buf, i); + + analog.meaning->channels = g_slist_append(NULL, + g_slist_nth_data(sdi->channels, + mastech_ms6514_channel_assignment(buf, i))); + + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + sr_session_send(sdi, &packet); + g_slist_free(analog.meaning->channels); + } + + sr_sw_limits_update_samples_read(&devc->limits, 1); +} + +static const uint8_t *mastech_ms6514_parse_data(struct sr_dev_inst *sdi, + const uint8_t *buf, int len) +{ + if (len < MASTECH_MS6514_FRAME_SIZE) + return NULL; /* Not enough data for a full packet. */ + + if (buf[0] != 0x65 || buf[1] != 0x14) + return buf + 1; /* Try to re-synchronize on a packet start. */ + + if (buf[16] != 0x0D || buf[17] != 0x0A) + return buf + MASTECH_MS6514_FRAME_SIZE; /* Valid start but no valid end -> skip. */ + + mastech_ms6514_data(sdi, buf); + + return buf + MASTECH_MS6514_FRAME_SIZE; +} + +SR_PRIV int mastech_ms6514_receive_data(int fd, int revents, void *cb_data) +{ + struct sr_dev_inst *sdi; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + const uint8_t *ptr, *next_ptr, *end_ptr; + int len; + + (void)fd; + + if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN) + return TRUE; + serial = sdi->conn; + + /* Try to get as much data as the buffer can hold. */ + len = sizeof(devc->buf) - devc->buf_len; + len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len); + if (len < 1) { + sr_err("Serial port read error: %d.", len); + return FALSE; + } + devc->buf_len += len; + + /* Now look for packets in that data. */ + ptr = devc->buf; + end_ptr = ptr + devc->buf_len; + while ((next_ptr = mastech_ms6514_parse_data(sdi, ptr, end_ptr - ptr))) + ptr = next_ptr; + + /* If we have any data left, move it to the beginning of our buffer. */ + memmove(devc->buf, ptr, end_ptr - ptr); + devc->buf_len -= ptr - devc->buf; + + /* If buffer is full and no valid packet was found, wipe buffer. */ + if (devc->buf_len >= sizeof(devc->buf)) { + devc->buf_len = 0; + return FALSE; + } + + if (sr_sw_limits_check(&devc->limits)) { + sr_dev_acquisition_stop(sdi); + return TRUE; + } + + return TRUE; +} diff --git a/src/hardware/mastech-ms6514/protocol.h b/src/hardware/mastech-ms6514/protocol.h new file mode 100644 index 00000000..35660b42 --- /dev/null +++ b/src/hardware/mastech-ms6514/protocol.h @@ -0,0 +1,55 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2019 Dave Buechi + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBSIGROK_HARDWARE_MASTECH_MS6514_PROTOCOL_H +#define LIBSIGROK_HARDWARE_MASTECH_MS6514_PROTOCOL_H + +#include +#include +#include +#include "libsigrok-internal.h" + +#define LOG_PREFIX "mastech-ms6514" + +#define MASTECH_MS6514_NUM_CHANNELS 2 +#define MASTECH_MS6514_BUF_SIZE (3 * 18) +#define MASTECH_MS6514_FRAME_SIZE 18 +#define DEFAULT_DATA_SOURCE DATA_SOURCE_LIVE + +enum mastech_ms6614_data_source { + DATA_SOURCE_LIVE, + DATA_SOURCE_MEMORY, +}; + +enum mastech_ms6614_command { + CMD_GET_STORED = 0xA1 +}; + +struct dev_context { + struct sr_sw_limits limits; + enum mastech_ms6614_data_source data_source; + unsigned int buf_len; + uint8_t buf[MASTECH_MS6514_BUF_SIZE]; + unsigned int log_buf_len; +}; + +SR_PRIV int mastech_ms6514_receive_data(int fd, int revents, void *cb_data); +SR_PRIV gboolean mastech_ms6514_packet_valid(const uint8_t *buf); + +#endif