From: Matthias Heidbrink Date: Thu, 21 Nov 2013 14:47:09 +0000 (+0100) Subject: gmc_mh_1x_2x: Completed driver for Metrahit 1x/2x X-Git-Tag: libsigrok-0.3.0~541 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=f57924179d08dee9d24099233ffbee2123613a22;p=libsigrok.git gmc_mh_1x_2x: Completed driver for Metrahit 1x/2x This driver supports devices with “RS232“ interface (Metrahit 16I, 18S, Siemens B1105 tested, 29S incomplete). --- diff --git a/hardware/gmc-mh-1x-2x/api.c b/hardware/gmc-mh-1x-2x/api.c index 8647d7fa..42969412 100644 --- a/hardware/gmc-mh-1x-2x/api.c +++ b/hardware/gmc-mh-1x-2x/api.c @@ -17,48 +17,215 @@ * along with this program. If not, see . */ +#include + #include "protocol.h" +/* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */ +#define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */ +#define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0" +#define VENDOR_GMC "Gossen Metrawatt" + SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info; -static struct sr_dev_driver *di = &gmc_mh_1x_2x_rs232_driver_info; -static int init(struct sr_context *sr_ctx) +static const int32_t hwopts[] = { + SR_CONF_CONN, + SR_CONF_SERIALCOMM, +}; + +static const int32_t hwcaps[] = { + SR_CONF_MULTIMETER, + SR_CONF_LIMIT_SAMPLES, + SR_CONF_LIMIT_MSEC, + SR_CONF_CONTINUOUS, +}; + +/** Driver init function */ +static int init_1x_2x_rs232(struct sr_context *sr_ctx) { - return std_init(sr_ctx, di, LOG_PREFIX); + return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX); } -static GSList *scan(GSList *options) + +/** Read single byte from serial port. + * \retval -1 Timeout or error. + * \retval other Byte. + */ +static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout) { - struct drv_context *drvc; - GSList *devices; + guint8 result = 0; + int rc = 0; + + for (;;) { + rc = serial_read(serial, &result, 1); + if (rc == 1) { + sr_spew("read: 0x%02x/%d", result, result); + return result; + } + if (g_get_monotonic_time() > timeout) + return -1; + g_usleep(2000); + } +} + +/** Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second. + * \param serial Configured, open serial port. + * + * \retval NULL Detection failed. + * \retval other Model code. + */ +static enum model scan_model_sm(struct sr_serial_dev_inst *serial) +{ + int byte, bytecnt, cnt; + enum model model; + gint64 timeout_us; + + model = SR_METRAHIT_NONE; + timeout_us = g_get_monotonic_time() + 1*1000*1000; + + /* Try to find message consisting of device code and several + * (at least 4) data bytes. */ + for (bytecnt = 0; bytecnt < 100; bytecnt++) { + byte = read_byte(serial, timeout_us); + if ((byte == -1) || (timeout_us < g_get_monotonic_time())) + break; + if ((byte & MSGID_MASK) == MSGID_INF) { + if (!(model = sr_gmc_decode_model_sm(byte & MSGC_MASK))) + break; + /* Now expect (at least) 4 data bytes */ + for (cnt = 0; cnt < 4; cnt++) { + byte = read_byte(serial, timeout_us); + if ((byte == -1) || + ((byte & MSGID_MASK) != MSGID_DATA)) + { + model = SR_METRAHIT_NONE; + bytecnt = 100; + break; + } + } + break; + } + } - (void)options; + return model; +} + +/** Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt + * 'RS232' interface. The older 1x models use 8192 and the newer 2x 9600 baud. + * The DMM usually sends up to about 20 messages per second. However, depending + * on configuration and measurement mode the intervals can be much larger and + * then the detection might not work. + */ +static GSList *scan_1x_2x_rs232(GSList *options) +{ + struct sr_dev_inst *sdi; + struct drv_context *drvc; + struct dev_context *devc; + struct sr_config *src; + struct sr_probe *probe; + struct sr_serial_dev_inst *serial; + GSList *l, *devices; + const char *conn, *serialcomm; + enum model model; + gboolean serialcomm_given; + int cnt; devices = NULL; - drvc = di->priv; + drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv; drvc->instances = NULL; + conn = serialcomm = NULL; + model = SR_METRAHIT_NONE; + serialcomm_given = FALSE; + + sr_spew("scan_1x_2x_rs232() called!"); + + 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); + serialcomm_given = TRUE; + break; + } + } + if (!conn) + return NULL; + if (!serialcomm) + serialcomm = SERIALCOMM_2X_RS232; + + if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) + return NULL; + + if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) { + sr_serial_dev_inst_free(serial); + return NULL; + } - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ + serial_flush(serial); + + model = scan_model_sm(serial); + + /* If detection failed and no user-supplied parameters, + * try second baud rate. */ + if ((model == SR_METRAHIT_NONE) && !serialcomm_given) { + serialcomm = SERIALCOMM_1X_RS232; + g_free(serial->serialcomm); + serial->serialcomm = g_strdup(serialcomm); + if (serial_set_paramstr(serial, serialcomm) == SR_OK) { + serial_flush(serial); + model = scan_model_sm(serial); + } + } + + if (model != SR_METRAHIT_NONE) { + sr_spew("%s %s detected!", VENDOR_GMC, sr_gmc_model_str(model)); + if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC, + sr_gmc_model_str(model), ""))) + return NULL; + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { + sr_err("Device context malloc failed."); + return NULL; + } + devc->model = model; + devc->limit_samples = 0; + devc->limit_msec = 0; + devc->num_samples = 0; + devc->elapsed_msec = g_timer_new(); + devc->settings_ok = FALSE; + + sdi->conn = serial; + sdi->priv = devc; + sdi->driver = &gmc_mh_1x_2x_rs232_driver_info; + if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + drvc->instances = g_slist_append(drvc->instances, sdi); + devices = g_slist_append(devices, sdi); + } return devices; } -static GSList *dev_list(void) +/** Driver device list function */ +static GSList *dev_list_1x_2x_rs232(void) { - return ((struct drv_context *)(di->priv))->instances; + return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv)) + ->instances; } -static int dev_clear(void) +static int dev_clear_1x_2x_rs232(void) { - return std_dev_clear(di, NULL); + return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL); } static int dev_open(struct sr_dev_inst *sdi) { - (void)sdi; - - /* TODO: get handle from sdi->conn and open it. */ + struct sr_serial_dev_inst *serial = sdi->conn; + if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) + return SR_ERR; sdi->status = SR_ST_ACTIVE; @@ -67,24 +234,33 @@ 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; + struct dev_context *devc; - /* TODO: get handle from sdi->conn and close it. */ + serial = sdi->conn; + if (serial && serial->fd != -1) { + serial_close(serial); + sdi->status = SR_ST_INACTIVE; + } sdi->status = SR_ST_INACTIVE; + /* Free dynamically allocated resources. */ + if ((devc = sdi->priv) && devc->elapsed_msec) { + g_timer_destroy(devc->elapsed_msec); + devc->elapsed_msec = NULL; + devc->model = SR_METRAHIT_NONE; + } + return SR_OK; } -static int cleanup(void) +static int cleanup_sm_rs232(void) { - dev_clear(); - - /* TODO: free other driver resources, if any. */ - - return SR_OK; + return dev_clear_1x_2x_rs232(); } +/** TODO */ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_probe_group *probe_group) { @@ -107,68 +283,102 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, 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_MSEC: + if (g_variant_get_uint64(data) == 0) { + sr_err("LIMIT_MSEC can't be 0."); + return SR_ERR; + } + devc->limit_msec = g_variant_get_uint64(data); + sr_dbg("Setting time limit to %" PRIu64 "ms.", + devc->limit_msec); + break; + case SR_CONF_LIMIT_SAMPLES: + devc->limit_samples = g_variant_get_uint64(data); + sr_dbg("Setting sample limit to %" PRIu64 ".", + devc->limit_samples); + 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, +static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi, void *cb_data) { - (void)sdi; - (void)cb_data; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + + if (!sdi || !cb_data || !(devc = sdi->priv)) + return SR_ERR_BUG; 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->cb_data = cb_data; + devc->settings_ok = FALSE; + devc->buflen = 0; + + /* Send header packet to the session bus. */ + std_session_send_df_header(cb_data, LOG_PREFIX); + + /* Start timer, if required. */ + if (devc->limit_msec) + g_timer_start(devc->elapsed_msec); + + /* Poll every 40ms, or whenever some data comes in. */ + serial = sdi->conn; + sr_source_add(serial->fd, G_IO_IN, 40, gmc_mh_1x_2x_receive_data, + (void *)sdi); return SR_OK; } -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data) { - (void)cb_data; + struct dev_context *devc; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; + /* Stop timer, if required. */ + if (sdi && (devc = sdi->priv) && devc->limit_msec) + g_timer_stop(devc->elapsed_msec); - /* TODO: stop acquisition. */ - - return SR_OK; + return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, + sdi->conn, LOG_PREFIX); } SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { @@ -176,17 +386,17 @@ SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { .longname = "Gossen Metrawatt Metrahit 1x/2x DMMs, 'RS232' Interface", .api_version = 1, - .init = init, - .cleanup = cleanup, - .scan = scan, - .dev_list = dev_list, - .dev_clear = dev_clear, + .init = init_1x_2x_rs232, + .cleanup = cleanup_sm_rs232, + .scan = scan_1x_2x_rs232, + .dev_list = dev_list_1x_2x_rs232, + .dev_clear = dev_clear_1x_2x_rs232, .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, + .dev_acquisition_start = dev_acq_start_1x_2x_rs232, + .dev_acquisition_stop = dev_acq_stop, .priv = NULL, }; diff --git a/hardware/gmc-mh-1x-2x/protocol.c b/hardware/gmc-mh-1x-2x/protocol.c index ef7427fe..0583b2b2 100644 --- a/hardware/gmc-mh-1x-2x/protocol.c +++ b/hardware/gmc-mh-1x-2x/protocol.c @@ -17,12 +17,762 @@ * along with this program. If not, see . */ +#include +#include + #include "protocol.h" +/** Set or clear flags in devc->mqflags. */ +static void setmqf(struct dev_context *devc, uint64_t flags, gboolean set) +{ + if (set) + devc->mqflags |= flags; + else + devc->mqflags &= ~flags; +} + +/** Decode current type and measured value, Metrahit 12-16. + */ +static void decode_ctmv_16(guchar ctmv, struct dev_context *devc) +{ + devc->mq = 0; + devc->unit = 0; + devc->mqflags = 0; + + switch (ctmv) { + case 0x00: /* 0000 - */ + break; + case 0x01: /* 0001 mV DC */ + devc->scale1000 = -1; /* Fall through */ + case 0x02: /* 0010 V DC */ + case 0x03: /* 0011 V AC+DC */ + case 0x04: /* 0100 V AC */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + if (ctmv <= 0x03) + devc->mqflags |= SR_MQFLAG_DC; + if (ctmv >= 0x03) { + devc->mqflags |= SR_MQFLAG_AC; + if (devc->model >= SR_METRAHIT_16S) + devc->mqflags |= SR_MQFLAG_RMS; + } + break; + case 0x05: /* 0101 Hz (15S/16S only) */ + case 0x06: /* 0110 kHz (15S/16S only) */ + devc->mq = SR_MQ_FREQUENCY; + devc->unit = SR_UNIT_HERTZ; + if (ctmv == 0x06) + devc->scale1000 = 1; + break; + case 0x07: /* 0111 % (15S/16S only) */ + devc->mq = SR_MQ_DUTY_CYCLE; + devc->unit = SR_UNIT_PERCENTAGE; + break; + case 0x08: /* 1000 Diode */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + devc->mqflags |= SR_MQFLAG_DIODE; + break; + case 0x09: /* 1001 Ohm, °C */ + case 0x0a: /* 1010 kOhm */ + case 0x0b: /* 1011 MOhm */ + devc->mq = SR_MQ_RESISTANCE; /* Changed to temp. later if req.*/ + devc->unit = SR_UNIT_OHM; + devc->scale1000 = ctmv - 0x09; + break; + case 0x0c: /* 1100 nF (15S/16S only) */ + case 0x0d: /* 1101 µF (15S/16S only) */ + devc->mq = SR_MQ_CAPACITANCE; + devc->unit = SR_UNIT_FARAD; + if (ctmv == 0x0c) + devc->scale1000 = -3; + else + devc->scale1000 = -2; + break; + case 0x0e: /* mA, µA */ + devc->scale1000 = -1; /* Fall through. */ + case 0x0f: /* A */ + devc->mq = SR_MQ_CURRENT; + devc->unit = SR_UNIT_AMPERE; + if (devc->model == SR_METRAHIT_16S) + devc->mqflags |= SR_MQFLAG_RMS; + /* 16I A only with clamp, RMS questionable. */ + break; + } +} + +/** Decode range/sign/acdc byte special chars (Metrahit 12-16). + * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). + */ +static void decode_rs_16(guchar rs, struct dev_context *devc) +{ + sr_spew("decode_rs_16(%d) scale = %f", rs, devc->scale); + + if (rs & 0x08) /* Sign */ + devc->scale *= -1.0; + + if (devc->mq == SR_MQ_CURRENT) { + if (rs & 0x04) /* Current is AC */ + devc->mqflags |= SR_MQFLAG_AC; + else + devc->mqflags |= SR_MQFLAG_DC; + } + + switch (rs & 0x03) { + case 0: + if (devc->mq == SR_MQ_VOLTAGE) /* V */ + devc->scale *= 0.1; + else if (devc->mq == SR_MQ_CURRENT) /* 000.0 µA */ + devc->scale *= 0.0000001; /* Untested! */ + else if (devc->mq == SR_MQ_RESISTANCE) { + if (devc->buflen >= 10) { + /* °C with 10 byte msg type, otherwise GOhm. */ + devc->mq = SR_MQ_TEMPERATURE; + devc->unit = SR_UNIT_CELSIUS; + devc->scale *= 0.01; + } + else if ((devc->scale1000 == 2)) { + /* 16I Iso 500/1000V 3 GOhm */ + devc->scale *= 0.1; + } + } + break; + case 1: + devc->scale *= 0.0001; + break; + case 2: + devc->scale *= 0.001; + break; + case 3: + devc->scale *= 0.01; + break; + } +} + +/** Decode special chars, Metrahit 12-16. + * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). + */ +static void decode_spc_16(guchar spc, struct dev_context *devc) +{ + /* xxxx1xxx ON */ + /* TODO: What does that mean? Power on? The 16I sets this. */ + /* xxxxx1xx BEEP */ + /* xxxxxx1x Low battery */ + /* xxxxxxx1 FUSE */ + /* 1xxxxxxx MIN */ + setmqf(devc, SR_MQFLAG_MIN, spc & 0x80); + + /* x1xxxxxx MAN */ + setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x40)); + + /* xx1xxxxx DATA */ + setmqf(devc, SR_MQFLAG_HOLD, spc & 0x20); + + /* xxx1xxxx MAX */ + setmqf(devc, SR_MQFLAG_MAX, spc & 0x10); +} + +/** Decode current type and measured value, Metrahit 18. + */ +static void decode_ctmv_18(guchar ctmv, struct dev_context *devc) +{ + devc->mq = 0; + devc->unit = 0; + devc->mqflags = 0; + + switch (ctmv) { + case 0x00: /* 0000 - */ + break; + case 0x01: /* 0001 V AC */ + case 0x02: /* 0010 V AC+DC */ + case 0x03: /* 0011 V DC */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + if (ctmv <= 0x02) + devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); + if (ctmv >= 0x02) + devc->mqflags |= SR_MQFLAG_DC; + break; + case 0x04: /* 0100 Ohm/Ohm with buzzer */ + devc->mq = SR_MQ_RESISTANCE; + devc->unit = SR_UNIT_OHM; + break; + case 0x05: /* 0101 Diode/Diode with buzzer*/ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + devc->mqflags |= SR_MQFLAG_DIODE; + break; + case 0x06: /* 0110 °C */ + devc->mq = SR_MQ_TEMPERATURE; + devc->unit = SR_UNIT_CELSIUS; + break; + case 0x07: /* 0111 F */ + devc->mq = SR_MQ_CAPACITANCE; + devc->unit = SR_UNIT_FARAD; + break; + case 0x08: /* 1000 mA DC */ + case 0x09: /* 1001 A DC */ + case 0x0a: /* 1010 mA AC+DC */ + case 0x0b: /* 1011 A AC+DC */ + devc->mq = SR_MQ_CURRENT; + devc->unit = SR_UNIT_AMPERE; + devc->mqflags |= SR_MQFLAG_DC; + if (ctmv >= 0x0a) + devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); + if ((ctmv == 0x08) || (ctmv == 0x0a)) + devc->scale1000 = -1; + break; + case 0x0c: /* 1100 Hz */ + devc->mq = SR_MQ_FREQUENCY; + devc->unit = SR_UNIT_HERTZ; + break; + case 0x0d: /* 1101 dB */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_DECIBEL_VOLT; + devc->mqflags |= SR_MQFLAG_AC; /* dB available for AC only */ + break; + case 0x0e: /* 1110 Events AC, Events AC+DC. Actually delivers just + * current voltage via IR, nothing more. */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + devc->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; + break; + case 0x0f: /* 1111 Clock */ + devc->mq = SR_MQ_TIME; + devc->unit = SR_UNIT_SECOND; + devc->mqflags |= SR_MQFLAG_DURATION; + break; + } +} + +/** Decode range/sign/acdc byte special chars, Metrahit 18. + * \param[in] rs Rance/sign byte + */ +static void decode_rs_18(guchar rs, struct dev_context *devc) +{ + int range; + + /* Sign */ + if (((devc->scale > 0) && (rs & 0x08)) || + ((devc->scale < 0) && !(rs & 0x08))) + devc->scale *= -1.0; + + /* Range */ + range = rs & 0x07; + switch (devc->mq) { + case SR_MQ_VOLTAGE: + if (devc->unit == SR_UNIT_DECIBEL_VOLT) { + devc->scale *= pow(10.0, -2); + /* When entering relative mode, the device switches + * from 10 byte to 6 byte msg format. Unfortunately + * it switches back to 10 byte when the second value + * is measured, so that's not sufficient to + * identify relative mode. + */ + } + else if (devc->vmains_29S) + devc->scale *= pow(10.0, range - 2); + else + devc->scale *= pow(10.0, range - 5); + break; + case SR_MQ_CURRENT: + if (devc->scale1000 == -1) + devc->scale *= pow(10.0, range - 5); + else + devc->scale *= pow(10.0, range - 4); + break; + case SR_MQ_RESISTANCE: + devc->scale *= pow(10.0, range - 2); + break; + case SR_MQ_FREQUENCY: + devc->scale *= pow(10.0, range - 3); + break; + case SR_MQ_TEMPERATURE: + devc->scale *= pow(10.0, range - 2); + break; + case SR_MQ_CAPACITANCE: + devc->scale *= pow(10.0, range - 14); + break; + /* TODO: 29S Mains measurements. */ + } +} + +/** Decode special chars, Metrahit 18. + * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). + */ +static void decode_spc_18(guchar spc, struct dev_context *devc) +{ + /* xxxx1xxx ZERO */ + /* xxxxx1xx BEEP */ + /* xxxxxx1x Low battery */ + /* xxxxxxx1 Fuse */ + + if (devc->mq == SR_MQ_TIME) { + /* xxx1xxxx Clock running: 1; stop: 0 */ + sr_spew("Clock running: %d", spc >> 4); + } + else { + /* 1xxxxxxx MAN */ + setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); + + /* x1xxxxxx MIN */ + setmqf(devc, SR_MQFLAG_MIN, spc & 0x40); + + /* xx1xxxxx MAX */ + setmqf(devc, SR_MQFLAG_MAX, spc & 0x20); + + /* xxx1xxxx DATA */ + setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); + } +} + +/** Decode current type and measured value, Metrahit 2x. + * \param[in] ctmv Current type and measured value (v1 | (v2<<4)). + */ +static void decode_ctmv_2x(guchar ctmv, struct dev_context *devc) +{ + if ((ctmv > 0x1c) || (!devc)) { + sr_err("decode_ct_mv_2x(%d): invalid param(s)!", ctmv); + return; + } + + devc->mq = 0; + devc->unit = 0; + devc->mqflags = 0; + + switch (ctmv) { + /* 00000 unused */ + case 0x01: /* 00001 V DC */ + case 0x02: /* 00010 V AC+DC */ + case 0x03: /* 00011 V AC */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + if (ctmv <= 0x02) + devc->mqflags |= SR_MQFLAG_DC; + if (ctmv >= 0x02) { + devc->mqflags |= SR_MQFLAG_AC; + if (devc->model >= SR_METRAHIT_24S) + devc->model |= SR_MQFLAG_RMS; + } + break; + case 0x04: /* 00100 mA DC */ + case 0x05: /* 00101 mA AC+DC */ + devc->scale1000 = -1; + case 0x06: /* 00110 A DC */ + case 0x07: /* 00111 A AC+DC */ + devc->mq = SR_MQ_CURRENT; + devc->unit = SR_UNIT_AMPERE; + devc->mqflags |= SR_MQFLAG_DC; + if ((ctmv == 0x05) || (ctmv == 0x07)) { + devc->mqflags |= SR_MQFLAG_AC; + if (devc->model >= SR_METRAHIT_24S) + devc->mqflags |= SR_MQFLAG_RMS; + } + break; + case 0x08: /* 01000 Ohm */ + devc->mq = SR_MQ_RESISTANCE; + devc->unit = SR_UNIT_OHM; + break; + case 0x09: /* 01001 F */ + devc->mq = SR_MQ_CAPACITANCE; + devc->unit = SR_UNIT_FARAD; + devc->scale *= 0.1; + break; + case 0x0a: /* 01010 dB */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_DECIBEL_VOLT; + devc->mqflags |= SR_MQFLAG_AC; + break; + case 0x0b: /* 01011 Hz U ACDC */ + case 0x0c: /* 01100 Hz U AC */ + devc->mq = SR_MQ_FREQUENCY; + devc->unit = SR_UNIT_HERTZ; + devc->mqflags |= SR_MQFLAG_AC; + if (ctmv <= 0x0b) + devc->mqflags |= SR_MQFLAG_DC; + break; + case 0x0d: /* 01101 W on power, mA range (29S only) */ + case 0x0e: /* 01110 W on power, A range (29S only) */ + /* TODO: Differences between Send Mode and bidir protocol here */ + devc->mq = SR_MQ_POWER; + devc->unit = SR_UNIT_WATT; + break; + case 0x0f: /* 01111 Diode */ + case 0x10: /* 10000 Diode with buzzer (actually cont. with voltage) */ + devc->unit = SR_UNIT_VOLT; + if (ctmv == 0x0f) { + devc->mq = SR_MQ_VOLTAGE; + devc->mqflags |= SR_MQFLAG_DIODE; + devc->scale *= 0.1; + } + else { + devc->mq = SR_MQ_CONTINUITY; + devc->scale *= 0.00001; + } + devc->unit = SR_UNIT_VOLT; + break; + case 0x11: /* 10001 Ohm with buzzer */ + devc->mq = SR_MQ_CONTINUITY; + devc->unit = SR_UNIT_OHM; + devc->scale1000 = -1; + break; + case 0x12: /* 10010 Temperature */ + devc->mq = SR_MQ_TEMPERATURE; + devc->unit = SR_UNIT_CELSIUS; + /* This can be Fahrenheit. That is detected by range=4 later. */ + break; + /* 0x13 10011, 0x14 10100 unsed */ + case 0x15: /* 10101 Press (29S only) */ + /* TODO: What does that mean? Possibly phase shift? + Then we need a unit/flag for it. */ + devc->mq = SR_MQ_GAIN; + devc->unit = SR_UNIT_PERCENTAGE; + break; + case 0x16: /* 10110 Pulse W (29S only) */ + /* TODO: Own unit and flag for this! */ + devc->mq = SR_MQ_POWER; + devc->unit = SR_UNIT_WATT; + break; + case 0x17: /* 10111 TRMS V on mains (29S only) */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_VOLT; + devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); + devc->vmains_29S = TRUE; + break; + case 0x18: /* 11000 Counter (zero crossings of a signal) */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_UNITLESS; + break; + case 0x19: /* 11001 Events U ACDC */ + case 0x1a: /* 11010 Events U AC */ + /* TODO: No unit or flags for this yet! */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_UNITLESS; + devc->mqflags |= SR_MQFLAG_AC; + if (ctmv <= 0x19) + devc->mqflags |= SR_MQFLAG_DC; + break; + case 0x1b: /* 11011 pulse on mains (29S only) */ + /* TODO: No unit or flags for this yet! */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_UNITLESS; + devc->mqflags |= SR_MQFLAG_AC; + break; + case 0x1c: /* 11100 dropout on mains (29S only) */ + /* TODO: No unit or flags for this yet! */ + devc->mq = SR_MQ_VOLTAGE; + devc->unit = SR_UNIT_UNITLESS; + devc->mqflags |= SR_MQFLAG_AC; + break; + default: + sr_err("decode_ctmv_2x(%d, ...): Unknown ctmv!"); + } +} + +/** Decode range/sign/acdc byte special chars, Metrahit 2x. + * \param[in] rs Rance/sign byte + */ +static void decode_rs_2x(guchar rs, struct dev_context *devc) +{ + int range; + + /* Sign */ + if (((devc->scale > 0) && (rs & 0x08)) || + ((devc->scale < 0) && !(rs & 0x08))) + devc->scale *= -1.0; + + /* Range */ + range = rs & 0x07; + switch (devc->mq) { + case SR_MQ_VOLTAGE: + if (devc->unit == SR_UNIT_DECIBEL_VOLT) { + devc->scale *= pow(10.0, -3); + } + else if (devc->vmains_29S) + devc->scale *= pow(10.0, range - 2); + else if(devc->mqflags & SR_MQFLAG_AC) + devc->scale *= pow(10.0, range - 6); + else /* "Undocumented feature": Between AC and DC + scaling differs by 1. */ + devc->scale *= pow(10.0, range - 5); + break; + case SR_MQ_CURRENT: + if (devc->scale1000 == -1) + devc->scale *= pow(10.0, range - 5); + else + devc->scale *= pow(10.0, range - 4); + break; + case SR_MQ_RESISTANCE: + devc->scale *= pow(10.0, range - 3); + break; + case SR_MQ_FREQUENCY: + devc->scale *= pow(10.0, range - 3); + break; + case SR_MQ_TEMPERATURE: + if (range == 4) { /* Indicator for °F */ + devc->unit = SR_UNIT_FAHRENHEIT; + } + + devc->scale *= pow(10.0, - 2); + break; + case SR_MQ_CAPACITANCE: + devc->scale *= pow(10.0, range - 13); + break; + /* TODO: 29S Mains measurements. */ + } +} + +/** Decode special chars (Metrahit 2x). + * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). + */ +static void decode_spc_2x(guchar spc, struct dev_context *devc) +{ + /* xxxxxxx1 Fuse */ + + /* xxxxxx1x Low battery */ + + /* xxxxx1xx BEEP */ + + /* xxxx1xxx ZERO */ + + /* xxx1xxxx DATA */ + setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); + + /* x11xxxxx unused */ + /* 1xxxxxxx MAN */ + setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); +} + +/** Clean range and sign. */ +static void clean_rs_v(struct dev_context *devc) +{ + devc->value = 0.0; + devc->scale = 1.0; +} + +/** Clean current type, measured variable, range and sign. */ +static void clean_ctmv_rs_v(struct dev_context *devc) +{ + devc->mq = 0; + devc->unit = 0; + devc->mqflags = 0; + devc->scale1000 = 0; + devc->vmains_29S = FALSE; + clean_rs_v(devc); +} + +/** Send prepared value. */ +static void send_value(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + struct sr_datafeed_analog analog; + struct sr_datafeed_packet packet; + + devc = sdi->priv; + + memset(&analog, 0, sizeof(analog)); + analog.probes = sdi->probes; + analog.num_samples = 1; + analog.mq = devc->mq; + analog.unit = devc->unit; + analog.mqflags = devc->mqflags; + analog.data = &devc->value; + + memset(&packet, 0, sizeof(packet)); + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + sr_session_send(devc->cb_data, &packet); + + devc->num_samples++; +} + +/** Process 6-byte data message, Metrahit 1x/2x. */ +static void process_msg_dta_6(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + int cnt; + guchar dgt; + + devc = sdi->priv; + clean_rs_v(devc); + + /* Byte 0, range and sign */ + if (devc->model <= SR_METRAHIT_16X) { + decode_rs_16(bc(devc->buf[0]), devc); + } + else if (devc->model < SR_METRAHIT_2X){ + decode_rs_18(bc(devc->buf[0]), devc); + } else { + decode_rs_2x(bc(devc->buf[0]), devc); + } + + /* Bytes 1-5, digits (ls first). */ + for (cnt = 0; cnt < 5; cnt++) { + dgt = bc(devc->buf[1 + cnt]); + if (dgt >= 10) { + /* 10 Overload; on model <= 16X also 11 possible. */ + devc->value = NAN; + devc->scale = 1.0; + break; + } + devc->value += pow(10.0, cnt) * dgt; + } + sr_spew("process_msg_dta_6() value=%f scale=%f scalet=%d", + devc->value, devc->scale, devc->scale1000); + if (devc->value != NAN) + devc->value *= devc->scale * pow(1000.0, devc->scale1000); + + /* Create and send packet. */ + send_value(sdi); +} + +/** Process 5-byte info message, Metrahit 1x/2x. */ +static void process_msg_inf_5(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + enum model model; + + devc = sdi->priv; + + clean_ctmv_rs_v(devc); + + /* Process byte 0 */ + model = sr_gmc_decode_model_sm(bc(devc->buf[0])); + if (model != devc->model) { + sr_warn("Model mismatch in data: Detected %s, now %s", + sr_gmc_model_str(devc->model), + sr_gmc_model_str(model)); + } + + /* Process bytes 1-4 */ + if (devc->model <= SR_METRAHIT_16X) { + decode_ctmv_16(bc(devc->buf[1]), devc); + decode_spc_16(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); + decode_rs_16(bc(devc->buf[4]), devc); + } + else if (devc->model <= SR_METRAHIT_18S) { + decode_ctmv_18(bc(devc->buf[1]), devc); + decode_spc_18(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); + decode_rs_18(bc(devc->buf[4]), devc); + } + else { /* Must be Metrahit 2x */ + decode_ctmv_2x(bc(devc->buf[1]), devc); + decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); + decode_rs_2x(bc(devc->buf[4]), devc); + } +} + +/** Process 10-byte info/data message, Metrahit 15+. */ +static void process_msg_inf_10(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + int cnt; + guchar dgt; + + devc = sdi->priv; + + process_msg_inf_5(sdi); + + /* Now decode numbers */ + for (cnt = 0; cnt < 5; cnt++) { + dgt = bc(devc->buf[5 + cnt]); + if (dgt >= 10) { /* Overload */ + devc->value = NAN; + devc->scale = 1.0; + break; + } + devc->value += pow(10.0, cnt) * dgt; + } + sr_spew("process_msg_inf_10() value=%f scale=%f scalet=%d", + devc->value, devc->scale, devc->scale1000); + + if (devc->value != NAN) + devc->value *= devc->scale * pow(1000.0, devc->scale1000); + + /* Create and send packet. */ + send_value(sdi); +} + +/** Decode send interval (Metrahit 2x only). */ +static const char* decode_send_interval(guchar si) +{ + switch (si) { + case 0x00: return "0.05"; + case 0x01: return "0.1"; + case 0x02: return "0.2"; + case 0x03: return "0.5"; + case 0x04: return "00:01"; + case 0x05: return "00:02"; + case 0x06: return "00:05"; + case 0x07: return "00:10"; + case 0x08: return "00:20"; + case 0x09: return "00:30"; + case 0x0a: return "01:00"; + case 0x0b: return "02:00"; + case 0x0c: return "05:00"; + case 0x0d: return "10:00"; + case 0x0e: return "----"; + case 0x0f: return "data"; + default: return "Unknown value"; + } +} + +/** Process 13-byte info/data message, Metrahit 2x. */ +static void process_msg_inf_13(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + enum model model; + int cnt; + guchar dgt; + + devc = sdi->priv; + + clean_ctmv_rs_v(devc); + + /* Byte 0, model. */ + model = sr_gmc_decode_model_sm(bc(devc->buf[0])); + if (model != devc->model) { + sr_warn("Model mismatch in data: Detected %s, now %s", + sr_gmc_model_str(devc->model), + sr_gmc_model_str(model)); + } + + /* Bytes 1-4, 11. */ + decode_ctmv_2x(bc(devc->buf[1]) | (bc(devc->buf[11]) << 4), devc); + decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); + decode_rs_2x(bc(devc->buf[4]), devc); + + /* Bytes 5-10, digits (ls first). */ + for (cnt = 0; cnt < 6; cnt++) { + dgt = bc(devc->buf[5 + cnt]); + if (dgt == 10) { /* Overload */ + devc->value = NAN; + devc->scale = 1.0; + break; + } + devc->value += pow(10.0, cnt) * dgt; + } + sr_spew("process_msg_inf_13() value=%f scale=%f scale1000=%d mq=%d " + "unit=%d mqflags=0x%02llx", devc->value, devc->scale, + devc->scale1000, devc->mq, devc->unit, devc->mqflags); + if (devc->value != NAN) + devc->value *= devc->scale * pow(1000.0, devc->scale1000); + + /* Byte 12, Send Interval */ + sr_spew("Send interval: %s", decode_send_interval(bc(devc->buf[12]))); + + /* Create and send packet. */ + send_value(sdi); +} + + SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; + struct sr_dev_inst *sdi; struct dev_context *devc; + struct sr_serial_dev_inst *serial; + unsigned char buf, msgt; + int len; (void)fd; @@ -32,9 +782,180 @@ SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data) if (!(devc = sdi->priv)) return TRUE; - if (revents == G_IO_IN) { - /* TODO */ + serial = sdi->conn; + + if (revents == G_IO_IN) { /* Serial data arrived. */ + while (GMC_BUFSIZE - devc->buflen - 1 > 0) { + len = serial_read(serial, devc->buf + devc->buflen, 1); + if (len < 1) + break; + buf = *(devc->buf + devc->buflen); + sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MSGC_MASK); + devc->buflen += len; + if (!devc->settings_ok) { + /* If no device type/settings record processed + * yet, wait for one. */ + if ((devc->buf[0] & MSGID_MASK) != MSGID_INF) { + devc->buflen = 0; + continue; + } + devc->settings_ok = TRUE; + } + + msgt = devc->buf[0] & MSGID_MASK; + switch (msgt) { + case MSGID_INF: + if (devc->buflen == 13) { + process_msg_inf_13(sdi); + devc->buflen = 0; + continue; + } + else if ((devc->buflen == 10) && + (devc->model <= SR_METRAHIT_18S)) { + process_msg_inf_10(sdi); + devc->buflen = 0; + continue; + } + else if ((devc->buflen >= 5) && + (devc->buf[devc->buflen-1] & + MSGID_MASK) != MSGID_DATA) { + /* Char just received is beginning + * of next message */ + process_msg_inf_5(sdi); + devc->buf[0] =devc->buf[devc->buflen-1]; + devc->buflen = 1; + continue; + } + break; + case MSGID_DTA: + case MSGID_D10: + if (devc->buflen == 6) { + process_msg_dta_6(sdi); + devc->buflen = 0; + } + break; + case MSGID_DATA: + sr_err("Comm error, unexpected data byte!"); + devc->buflen = 0; + break; + } + } + } + + /* If number of samples or time limit reached, stop aquisition. */ + if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) { + sdi->driver->dev_acquisition_stop(sdi, cb_data); + } + + if (devc->limit_msec) { + gdouble elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); + if ((elapsed_s * 1000) >= devc->limit_msec) { + sdi->driver->dev_acquisition_stop(sdi, cb_data); + } } return TRUE; } + + +/** Decode model in "send mode" */ +SR_PRIV int sr_gmc_decode_model_sm(guchar mcode) +{ + if (mcode > 0xf) { + sr_err("decode_model(%d): Model code 0..15 expected!", mcode); + return SR_METRAHIT_NONE; + } + + switch(mcode) { + case 0x04: /* 0100b */ + return SR_METRAHIT_12S; + case 0x08: /* 1000b */ + return SR_METRAHIT_13S14A; + case 0x09: /* 1001b */ + return SR_METRAHIT_14S; + case 0x0A: /* 1010b */ + return SR_METRAHIT_15S; + case 0x0B: /* 1011b */ + return SR_METRAHIT_16S; + case 0x06: /* 0110b (undocumented by GMC!) */ + return SR_METRAHIT_16I; + case 0x0D: /* 1101b */ + return SR_METRAHIT_18S; + case 0x02: /* 0010b */ + return SR_METRAHIT_22SM; + case 0x03: /* 0011b */ + return SR_METRAHIT_23S; + case 0x0f: /* 1111b */ + return SR_METRAHIT_24S; + case 0x05: /* 0101b */ + return SR_METRAHIT_25SM; + case 0x01: /* 0001b */ + return SR_METRAHIT_26S; + case 0x0c: /* 1100b */ + return SR_METRAHIT_28S; + case 0x0e: /* 1110b */ + return SR_METRAHIT_29S; + default: + sr_err("Unknown model code %d!", mcode); + return SR_METRAHIT_NONE; + } +} + +/** Decode model in bidirectional mode. + \param[in] mcode Model code. + + \return Model code +*/ +SR_PRIV int sr_gmc_decode_model_bidi(guchar mcode) +{ + switch(mcode) { + case 2: return SR_METRAHIT_22SM; + case 3: return SR_METRAHIT_23S; + case 4: return SR_METRAHIT_24S; + case 5: return SR_METRAHIT_25SM; + case 1: return SR_METRAHIT_26S; + case 12: return SR_METRAHIT_28S; + case 14: return SR_METRAHIT_29S; + default: + sr_err("Unknown model code %d!", mcode); + return SR_METRAHIT_NONE; + } +} + +SR_PRIV const char* sr_gmc_model_str(enum model mcode) +{ + switch (mcode) { + case SR_METRAHIT_NONE: + return "-uninitialized model variable-"; + case SR_METRAHIT_12S: + return "METRAHit 12S"; + case SR_METRAHIT_13S14A: + return "METRAHit 13S/14A"; + case SR_METRAHIT_14S: + return "METRAHit 14S"; + case SR_METRAHIT_15S: + return "METRAHit 15S"; + case SR_METRAHIT_16S: + return "METRAHit 16S"; + case SR_METRAHIT_16I: + return "METRAHit 16I"; + case SR_METRAHIT_18S: + return "METRAHit 18S"; + case SR_METRAHIT_22SM: + return "METRAHit 22S/M"; + case SR_METRAHIT_23S: + return "METRAHit 23S"; + case SR_METRAHIT_24S: + return "METRAHit 24S"; + case SR_METRAHIT_25SM: + return "METRAHit 25S/M"; + case SR_METRAHIT_26S: + return "METRAHit 26S"; + case SR_METRAHIT_28S: + return "METRAHit 28S"; + case SR_METRAHIT_29S: + return "METRAHit 29S"; + default: + return "Unknown model code"; + } +} diff --git a/hardware/gmc-mh-1x-2x/protocol.h b/hardware/gmc-mh-1x-2x/protocol.h index 799421d0..711dd1c0 100644 --- a/hardware/gmc-mh-1x-2x/protocol.h +++ b/hardware/gmc-mh-1x-2x/protocol.h @@ -34,16 +34,99 @@ #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) +#define GMC_BUFSIZE 266 + +/** Message ID bits 4, 5 */ +#define MSGID_MASK 0x30 /**< Mask to get message ID bits */ +#define MSGID_INF 0x00 /**< Start of message with device info */ +#define MSGID_D10 0x10 /**< Start of data message, non-displayed intermediate */ +#define MSGID_DTA 0x20 /**< Start of data message, displayed, averaged */ +#define MSGID_DATA 0x30 /**< Data byte in message */ + +#define MSGC_MASK 0x0f /**< Mask to get message byte contents */ + +#define MSGSRC_MASK 0xc0 /**< Mask to get bits related to message source */ + +#define bc(x) (x & MSGC_MASK) /**< Get contents from a byte */ + +#define MASK_6BITS 0x3f /**< Mask lower six bits. */ + +/** Internal multimeter model codes. In opposite to the multimeter models from + * protocol (see decode_model()), these codes allow working with ranges. + */ +enum model { + SR_METRAHIT_NONE = 0, /**< Value for uninitialized variable */ + SR_METRAHIT_12S = 12, + SR_METRAHIT_13S14A = 13, + SR_METRAHIT_14S = 14, + SR_METRAHIT_15S = 15, + SR_METRAHIT_16S = 16, + SR_METRAHIT_16I = 17, + SR_METRAHIT_16X = SR_METRAHIT_16I, /**< All Metrahit 16 */ + /* A Metrahit 17 exists, but seems not to have an IR interface. */ + SR_METRAHIT_18S = 18, + SR_METRAHIT_2X = 20, /**< For model type comparisons */ + SR_METRAHIT_22SM = 22, + SR_METRAHIT_23S = 23, + SR_METRAHIT_24S = 24, + SR_METRAHIT_25SM = 25, + SR_METRAHIT_26S = 26, + SR_METRAHIT_28S = 28, + SR_METRAHIT_29S = 29, +}; + +/** Convert GMC model code in send mode to sigrok-internal one. */ +SR_PRIV int sr_gmc_decode_model_sm(guchar mcode); + +/** Convert GMC model code in bidirectional mode to sigrok-internal one. + \param[in] mcode Model code. + + \return Model code +*/ +SR_PRIV int sr_gmc_decode_model_bidi(guchar mcode); + +/** Create 14-bytes (42 byte) message used in bidirectional mode. + * \param[in] addr Target address. 0..15, 0=Broadcast. + * \param[in] func Function code. + * \param[in] params Further parameters, &char[9]. Unused bytes must be 0. + * \param[out] buf &guchar[42] Output buffer (3*14). + * + */ +void create_msg_14(guchar addr, guchar func, guchar* params, guchar* buf); + +/** Get model string from sigrok-internal model code. + */ +SR_PRIV const char* sr_gmc_model_str(enum model mcode); + /** Private, per-device-instance driver context. */ struct dev_context { /* Model-specific information */ + enum model model; /**< Model code. */ /* Acquisition settings */ + uint64_t limit_samples; /**< Target number of samples */ + uint64_t limit_msec; /**< Target sampling time */ + + /* Opaque pointer passed in by frontend. */ + void *cb_data; /* Operational state */ + gboolean settings_ok; /**< Settings msg received yet. */ + int msg_type; /**< Message type (MSGID_INF, ...). */ + int msg_len; /**< Message lengh (valid when msg, curr. type known).*/ + int mq; /**< Measured quantity */ + int unit; /**< Measured unit */ + uint64_t mqflags; /**< Measured quantity flags */ + float value; /**< Measured value */ + float scale; /**< Scale for value. */ + int8_t scale1000; /**< Additional scale factor 1000^x. */ + gboolean vmains_29S; /**< Measured ctmv is V mains (29S only). */ /* Temporary state across callbacks */ - + uint64_t num_samples; /**< Current #samples for limit_samples */ + GTimer* elapsed_msec; /**< Used for sampling with limit_msec */ + unsigned char buf[GMC_BUFSIZE]; /**< Buffer for read callback */ + int buflen; /**< Data len in buf */ }; SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data);