From: Frank Stettner Date: Mon, 2 Oct 2017 10:50:04 +0000 (+0200) Subject: hp-3478a: Initial HP 3478A diver X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d2c1730a281ed4982f2ea8be0cd137d0e47135a0;p=libsigrok.git hp-3478a: Initial HP 3478A diver --- diff --git a/configure.ac b/configure.ac index e01d7200..8299e4d0 100644 --- a/configure.ac +++ b/configure.ac @@ -244,7 +244,7 @@ SR_DRIVER([Hantek 4032L], [hantek-4032l], [libusb]) SR_DRIVER([Hantek 6xxx], [hantek-6xxx], [libusb]) SR_DRIVER([Hantek DSO], [hantek-dso], [libusb]) SR_DRIVER([HP 3457A], [hp-3457a]) -SR_DRIVER([HP 3478A], [hp-3478a]) +SR_DRIVER([HP 3478A], [hp-3478a], [libgpib]) SR_DRIVER([Hung-Chang DSO-2100], [hung-chang-dso-2100], [libieee1284]) SR_DRIVER([Ikalogic Scanalogic-2], [ikalogic-scanalogic2], [libusb]) SR_DRIVER([Ikalogic Scanaplus], [ikalogic-scanaplus], [libftdi]) diff --git a/src/hardware/hp-3478a/api.c b/src/hardware/hp-3478a/api.c index b97a21e0..84db3180 100644 --- a/src/hardware/hp-3478a/api.c +++ b/src/hardware/hp-3478a/api.c @@ -18,39 +18,109 @@ */ #include +#include "scpi.h" #include "protocol.h" -static GSList *scan(struct sr_dev_driver *di, GSList *options) +static const uint32_t scanopts[] = { + SR_CONF_CONN, +}; + +static const uint32_t drvopts[] = { + SR_CONF_MULTIMETER, +}; + +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_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, +}; + +static const struct { + enum sr_mq mq; + enum sr_mqflag mqflag; +} mqopts[] = { + {SR_MQ_VOLTAGE, SR_MQFLAG_DC}, + {SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_AUTORANGE}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC | SR_MQFLAG_RMS}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC | SR_MQFLAG_RMS | SR_MQFLAG_AUTORANGE}, + {SR_MQ_CURRENT, SR_MQFLAG_DC}, + {SR_MQ_CURRENT, SR_MQFLAG_DC | SR_MQFLAG_AUTORANGE}, + {SR_MQ_CURRENT, SR_MQFLAG_AC | SR_MQFLAG_RMS}, + {SR_MQ_CURRENT, SR_MQFLAG_AC | SR_MQFLAG_RMS | SR_MQFLAG_AUTORANGE}, + {SR_MQ_RESISTANCE, 0}, + {SR_MQ_RESISTANCE, 0 | SR_MQFLAG_AUTORANGE}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE | SR_MQFLAG_AUTORANGE}, +}; + +SR_PRIV struct sr_dev_driver hp_3478a_driver_info; + +static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx) { - struct drv_context *drvc; - GSList *devices; + struct sr_channel *channel; + struct channel_context *chanc; - (void)options; + chanc = g_malloc(sizeof(*chanc)); + chanc->location = TERMINAL_FRONT; - devices = NULL; - drvc = di->context; - drvc->instances = NULL; + channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG, TRUE, "P1"); + channel->priv = chanc; - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ + return chan_idx; +} - return devices; +static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) +{ + int ret; + struct sr_dev_inst *sdi; + struct dev_context *devc; + + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->vendor = g_strdup("Hewlett-Packard"); + sdi->model = g_strdup("3478A"); + sdi->conn = scpi; + sdi->driver = &hp_3478a_driver_info; + sdi->inst_type = SR_INST_SCPI; + + devc = g_malloc0(sizeof(struct dev_context)); + sr_sw_limits_init(&devc->limits); + sdi->priv = devc; + + /* Get actual status (function, digits, ...). */ + ret = hp_3478a_get_status_bytes(sdi); + if (ret != SR_OK) + return NULL; + + create_front_channel(sdi, 0); + + return sdi; +} + +static GSList *scan(struct sr_dev_driver *di, GSList *options) +{ + return sr_scpi_scan(di->context, options, probe_device); } static int dev_open(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_scpi_dev_inst *scpi; + + scpi = sdi->conn; - /* TODO: get handle from sdi->conn and open it. */ + if (sr_scpi_open(scpi) != SR_OK) + return SR_ERR; return SR_OK; } static int dev_close(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_scpi_dev_inst *scpi; + + scpi = sdi->conn; - /* TODO: get handle from sdi->conn and close it. */ + sr_scpi_close(scpi); return SR_OK; } @@ -58,75 +128,140 @@ static int dev_close(struct sr_dev_inst *sdi) 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; int ret; + GVariant *arr[2]; - (void)sdi; - (void)data; (void)cg; - ret = SR_OK; + devc = sdi->priv; + switch (key) { - /* TODO */ + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_LIMIT_MSEC: + return sr_sw_limits_config_get(&devc->limits, key, data); + case SR_CONF_MEASURED_QUANTITY: + ret = hp_3478a_get_status_bytes(sdi); + if (ret != SR_OK) + return ret; + arr[0] = g_variant_new_uint32(devc->measurement_mq); + arr[1] = g_variant_new_uint64(devc->measurement_mq_flags); + *data = g_variant_new_tuple(arr, 2); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + struct dev_context *devc; + enum sr_mq mq; + enum sr_mqflag mq_flags; + GVariant *tuple_child; - (void)sdi; - (void)data; (void)cg; - ret = SR_OK; + devc = sdi->priv; + switch (key) { - /* TODO */ + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_LIMIT_MSEC: + return sr_sw_limits_config_set(&devc->limits, key, data); + case SR_CONF_MEASURED_QUANTITY: + tuple_child = g_variant_get_child_value(data, 0); + mq = g_variant_get_uint32(tuple_child); + tuple_child = g_variant_get_child_value(data, 1); + mq_flags = g_variant_get_uint64(tuple_child); + g_variant_unref(tuple_child); + return hp_3478a_set_mq(sdi, mq, mq_flags); default: - ret = SR_ERR_NA; + return SR_ERR_NA; } - return ret; + return SR_OK; } static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + unsigned int i; + GVariant *gvar, *arr[2]; + GVariantBuilder gvb; - (void)sdi; - (void)data; - (void)cg; - - ret = SR_OK; switch (key) { - /* TODO */ + case SR_CONF_SCAN_OPTIONS: + case SR_CONF_DEVICE_OPTIONS: + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); + case SR_CONF_MEASURED_QUANTITY: + /* + * TODO: move to std.c as + * SR_PRIV GVariant *std_gvar_measured_quantities() + */ + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + for (i = 0; i < ARRAY_SIZE(mqopts); i++) { + arr[0] = g_variant_new_uint32(mqopts[i].mq); + arr[1] = g_variant_new_uint64(mqopts[i].mqflag); + gvar = g_variant_new_tuple(arr, 2); + g_variant_builder_add_value(&gvb, gvar); + } + *data = g_variant_builder_end(&gvb); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int dev_acquisition_start(const struct sr_dev_inst *sdi) { - /* TODO: configure hardware, reset acquisition state, set up - * callbacks and send header packet. */ + int ret; + struct sr_scpi_dev_inst *scpi; + struct dev_context *devc; + + scpi = sdi->conn; + devc = sdi->priv; - (void)sdi; + sr_sw_limits_acquisition_start(&devc->limits); + std_session_send_df_header(sdi); + + /* + * NOTE: For faster readings, there are some things one can do: + * - Turn off the display: sr_scpi_send(scpi, "D3SIGROK"). + * - Set the line frequency to 60Hz via switch (back of the unit). + * - Set to 3.5 digits measurement (add config key SR_CONF_DIGITS). + */ + + /* Set to internal trigger. */ + sr_scpi_send(scpi, "T1"); + /* Get device status. */ + hp_3478a_get_status_bytes(sdi); + + ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100, + hp_3478a_receive_data, (void *)sdi); + if (ret != SR_OK) + return ret; return SR_OK; } static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - /* TODO: stop acquisition. */ + struct sr_scpi_dev_inst *scpi; + + scpi = sdi->conn; + + sr_scpi_source_remove(sdi->session, scpi); + std_session_send_df_end(sdi); - (void)sdi; + /* Set to internal trigger. */ + sr_scpi_send(scpi, "T1"); + /* Turn on display. */ + sr_scpi_send(scpi, "D1"); return SR_OK; } diff --git a/src/hardware/hp-3478a/protocol.c b/src/hardware/hp-3478a/protocol.c index a794d562..e9cf6677 100644 --- a/src/hardware/hp-3478a/protocol.c +++ b/src/hardware/hp-3478a/protocol.c @@ -1,7 +1,7 @@ /* * This file is part of the libsigrok project. * - * Copyright (C) 2017 Frank Stettner + * Copyright (C) 2017-2018 Frank Stettner * * 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 @@ -18,24 +18,448 @@ */ #include +#include +#include +#include "scpi.h" #include "protocol.h" +static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); +static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); +static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); + +static const struct { + enum sr_mq mq; + int (*set_mode)(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); +} sr_mq_to_cmd_map[] = { + { SR_MQ_VOLTAGE, set_mq_volt }, + { SR_MQ_CURRENT, set_mq_amp }, + { SR_MQ_RESISTANCE, set_mq_ohm }, +}; + +static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) +{ + const char *cmd; + + if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC && + (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC) + return SR_ERR_NA; + + if ((flags & SR_MQFLAG_AC) == SR_MQFLAG_AC) + cmd = "F2"; + else + cmd = "F1"; + + return sr_scpi_send(scpi, "%s", cmd); +} + +static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) +{ + const char *cmd; + + if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC && + (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC) + return SR_ERR_NA; + + if (flags & SR_MQFLAG_AC) + cmd = "F6"; + else + cmd = "F5"; + + return sr_scpi_send(scpi, "%s", cmd); +} + +static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) +{ + const char *cmd; + + if (flags & SR_MQFLAG_FOUR_WIRE) + cmd = "F4"; + else + cmd = "F3"; + + return sr_scpi_send(scpi, "%s", cmd); +} + +SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, + enum sr_mqflag mq_flags) +{ + int ret; + size_t i; + struct sr_scpi_dev_inst *scpi = sdi->conn; + struct dev_context *devc = sdi->priv; + + /* No need to send command if we're not changing measurement type. */ + if (devc->measurement_mq == mq && + ((devc->measurement_mq_flags & mq_flags) == mq_flags)) + return SR_OK; + + for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) { + if (sr_mq_to_cmd_map[i].mq != mq) + continue; + + ret = sr_mq_to_cmd_map[i].set_mode(scpi, mq_flags); + if (ret != SR_OK) + return ret; + + ret = hp_3478a_get_status_bytes(sdi); + return ret; + } + + return SR_ERR_NA; +} + +static int parse_range_vdc(struct dev_context *devc, uint8_t range_byte) +{ + if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30MV) { + devc->enc_digits = devc->spec_digits - 2; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300MV) { + devc->enc_digits = devc->spec_digits - 3; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_3V) { + devc->enc_digits = devc->spec_digits - 1; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30V) { + devc->enc_digits = devc->spec_digits - 2; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300V) { + devc->enc_digits = devc->spec_digits - 3; + } else { + return SR_ERR_DATA; + } + + return SR_OK; +} + +static int parse_range_vac(struct dev_context *devc, uint8_t range_byte) +{ + if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300MV) { + devc->enc_digits = devc->spec_digits - 3; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_3V) { + devc->enc_digits = devc->spec_digits - 1; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_30V) { + devc->enc_digits = devc->spec_digits - 2; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300V) { + devc->enc_digits = devc->spec_digits - 3; + } else { + return SR_ERR_DATA; + } + + return SR_OK; +} + +static int parse_range_a(struct dev_context *devc, uint8_t range_byte) +{ + if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_300MA) { + devc->enc_digits = devc->spec_digits - 3; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_3A) { + devc->enc_digits = devc->spec_digits - 1; + } else { + return SR_ERR_DATA; + } + + return SR_OK; +} + +static int parse_range_ohm(struct dev_context *devc, uint8_t range_byte) +{ + if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30R) { + devc->enc_digits = devc->spec_digits - 2; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300R) { + devc->enc_digits = devc->spec_digits - 3; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3KR) { + devc->enc_digits = devc->spec_digits - 1; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30KR) { + devc->enc_digits = devc->spec_digits - 2; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300KR) { + devc->enc_digits = devc->spec_digits - 3; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3MR) { + devc->enc_digits = devc->spec_digits - 1; + } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30MR) { + devc->enc_digits = devc->spec_digits - 2; + } else { + return SR_ERR_DATA; + } + + return SR_OK; +} + +static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) +{ + devc->measurement_mq_flags = 0; + + /* Function + Range */ + if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VDC) { + devc->measurement_mq = SR_MQ_VOLTAGE; + devc->measurement_mq_flags |= SR_MQFLAG_DC; + devc->measurement_unit = SR_UNIT_VOLT; + parse_range_vdc(devc, function_byte); + } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VAC) { + devc->measurement_mq = SR_MQ_VOLTAGE; + devc->measurement_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; + devc->measurement_unit = SR_UNIT_VOLT; + parse_range_vac(devc, function_byte); + } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_2WR) { + devc->measurement_mq = SR_MQ_RESISTANCE; + devc->measurement_unit = SR_UNIT_OHM; + parse_range_ohm(devc, function_byte); + } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_4WR) { + devc->measurement_mq = SR_MQ_RESISTANCE; + devc->measurement_mq_flags |= SR_MQFLAG_FOUR_WIRE; + devc->measurement_unit = SR_UNIT_OHM; + parse_range_ohm(devc, function_byte); + } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_ADC) { + devc->measurement_mq = SR_MQ_CURRENT; + devc->measurement_mq_flags |= SR_MQFLAG_DC; + devc->measurement_unit = SR_UNIT_AMPERE; + parse_range_a(devc, function_byte); + } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_AAC) { + devc->measurement_mq = SR_MQ_CURRENT; + devc->measurement_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; + devc->measurement_unit = SR_UNIT_AMPERE; + parse_range_a(devc, function_byte); + } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_EXR) { + devc->measurement_mq = SR_MQ_RESISTANCE; + devc->measurement_unit = SR_UNIT_OHM; + parse_range_ohm(devc, function_byte); + } + + /* Digits / Resolution */ + if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_5_5) { + devc->spec_digits = 5; + } else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_4_5) { + devc->spec_digits = 4; + } else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_3_5) { + devc->spec_digits = 3; + } + + return SR_OK; +} + +static int parse_status_byte(struct dev_context *devc, uint8_t status_byte) +{ + devc->trigger = TRIGGER_UNDEFINED; + + /* External Trigger */ + if ((status_byte & STATUS_EXT_TRIGGER) == STATUS_EXT_TRIGGER) + devc->trigger = TRIGGER_EXTERNAL; + + /* Cal RAM */ + if ((status_byte & STATUS_CAL_RAM) == STATUS_CAL_RAM) + devc->calibration = TRUE; + else + devc->calibration = FALSE; + + /* Front/Rear terminals */ + if ((status_byte & STATUS_FRONT_TERMINAL) == STATUS_FRONT_TERMINAL) + devc->terminal = TERMINAL_FRONT; + else + devc->terminal = TERMINAL_REAR; + + /* 50Hz / 60Hz */ + if ((status_byte & STATUS_50HZ) == STATUS_50HZ) + devc->line = LINE_50HZ; + else + devc->line = LINE_60HZ; + + /* Auto-Zero */ + if ((status_byte & STATUS_AUTO_ZERO) == STATUS_AUTO_ZERO) + devc->auto_zero = TRUE; + else + devc->auto_zero = FALSE; + + /* Auto-Range */ + if ((status_byte & STATUS_AUTO_RANGE) == STATUS_AUTO_RANGE) + devc->measurement_mq_flags |= SR_MQFLAG_AUTORANGE; + else + devc->measurement_mq_flags &= ~SR_MQFLAG_AUTORANGE; + + /* Internal trigger */ + if ((status_byte & STATUS_INT_TRIGGER) == STATUS_INT_TRIGGER) + devc->trigger = TRIGGER_INTERNAL; + + return SR_OK; +} + +static int parse_srq_byte(uint8_t sqr_byte) +{ + (void)sqr_byte; + +#if 0 + /* The ServiceReQuest register isn't used at the moment. */ + + /* PON SRQ */ + if ((sqr_byte & SRQ_POWER_ON) == SRQ_POWER_ON) + sr_spew("hp_3478a_get_status_bytes: Power On SRQ or clear " + "msg received"); + + /* Cal failed SRQ */ + if ((sqr_byte & SRQ_CAL_FAILED) == SRQ_CAL_FAILED) + sr_spew("hp_3478a_get_status_bytes: CAL failed SRQ"); + + /* Keyboard SRQ */ + if ((sqr_byte & SRQ_KEYBORD) == SRQ_KEYBORD) + sr_spew("hp_3478a_get_status_bytes: Keyboard SRQ"); + + /* Hardware error SRQ */ + if ((sqr_byte & SRQ_HARDWARE_ERR) == SRQ_HARDWARE_ERR) + sr_spew("hp_3478a_get_status_bytes: Hardware error SRQ"); + + /* Syntax error SRQ */ + if ((sqr_byte & SRQ_SYNTAX_ERR) == SRQ_SYNTAX_ERR) + sr_spew("hp_3478a_get_status_bytes: Syntax error SRQ"); + + /* Every reading is available to the bus SRQ */ + if ((sqr_byte & SRQ_BUS_AVAIL) == SRQ_BUS_AVAIL) + sr_spew("hp_3478a_get_status_bytes: Every reading is available to " + "the bus SRQ"); +#endif + + return SR_OK; +} + +static int parse_error_byte(uint8_t error_byte) +{ + int ret; + + ret = SR_OK; + + /* A/D link */ + if ((error_byte & ERROR_AD_LINK) == ERROR_AD_LINK) { + sr_err("hp_3478a: Failure in the A/D link"); + ret = SR_ERR; + } + + /* A/D Self Test */ + if ((error_byte & ERROR_AD_SELF_TEST) == ERROR_AD_SELF_TEST) { + sr_err("hp_3478a: A/D has failed its internal Self Test"); + ret = SR_ERR; + } + + /* A/D slope error */ + if ((error_byte & ERROR_AD_SLOPE) == ERROR_AD_SLOPE) { + sr_err("hp_3478a: There has been an A/D slope error"); + ret = SR_ERR; + } + + /* ROM Selt Test */ + if ((error_byte & ERROR_ROM_SELF_TEST) == ERROR_ROM_SELF_TEST) { + sr_err("hp_3478a: The ROM Self Test has failed"); + ret = SR_ERR; + } + + /* RAM Selt Test */ + if ((error_byte & ERROR_RAM_SELF_TEST) == ERROR_RAM_SELF_TEST) { + sr_err("hp_3478a: The RAM Self Test has failed"); + ret = SR_ERR; + } + + /* Selt Test */ + if ((error_byte & ERROR_SELF_TEST) == ERROR_SELF_TEST) { + sr_err("hp_3478a: Self Test: Any of the CAL RAM locations have bad " + "checksums, or a range with a bad checksum is selected"); + ret = SR_ERR; + } + + return ret; +} + +SR_PRIV int hp_3478a_get_status_bytes(const struct sr_dev_inst *sdi) +{ + int ret; + char *response; + uint8_t function_byte, status_byte, srq_byte, error_byte; + struct sr_scpi_dev_inst *scpi = sdi->conn; + struct dev_context *devc = sdi->priv; + + ret = sr_scpi_get_string(scpi, "B", &response); + if (ret != SR_OK) + return ret; + + if (!response) + return SR_ERR; + + function_byte = (uint8_t)response[0]; + status_byte = (uint8_t)response[1]; + srq_byte = (uint8_t)response[2]; + error_byte = (uint8_t)response[3]; + + g_free(response); + + parse_function_byte(devc, function_byte); + parse_status_byte(devc, status_byte); + parse_srq_byte(srq_byte); + ret = parse_error_byte(error_byte); + + return ret; +} + +static void acq_send_measurement(struct sr_dev_inst *sdi) +{ + 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 dev_context *devc; + float f; + + devc = sdi->priv; + + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + + sr_analog_init(&analog, &encoding, &meaning, &spec, devc->enc_digits); + + /* TODO: Implement NAN, depending on counts, range and value. */ + f = devc->measurement; + analog.num_samples = 1; + analog.data = &f; + + encoding.unitsize = sizeof(float); + encoding.is_float = TRUE; + encoding.digits = devc->enc_digits; + + meaning.mq = devc->measurement_mq; + meaning.mqflags = devc->measurement_mq_flags; + meaning.unit = devc->measurement_unit; + meaning.channels = sdi->channels; + + spec.spec_digits = devc->spec_digits; + + sr_session_send(sdi, &packet); +} + SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; + struct sr_scpi_dev_inst *scpi; + struct sr_dev_inst *sdi; struct dev_context *devc; (void)fd; + (void)revents; - if (!(sdi = cb_data)) + if (!(sdi = cb_data) || !(devc = sdi->priv)) return TRUE; - if (!(devc = sdi->priv)) - return TRUE; + scpi = sdi->conn; - if (revents == G_IO_IN) { - /* TODO */ - } + /* + * This is necessary to get the actual range for the encoding digits. + * When SPoll is implemmented, this can be done via SPoll. + */ + if (hp_3478a_get_status_bytes(sdi) != SR_OK) + return FALSE; + + /* + * TODO: Implement GPIB-SPoll, to get notified by a SRQ when a new + * measurement is available. This is necessary, because when + * switching ranges, there could be a timeout. + */ + if (sr_scpi_get_double(scpi, NULL, &devc->measurement) != SR_OK) + return FALSE; + + acq_send_measurement(sdi); + sr_sw_limits_update_samples_read(&devc->limits, 1); + + if (sr_sw_limits_check(&devc->limits)) + sr_dev_acquisition_stop(sdi); return TRUE; } diff --git a/src/hardware/hp-3478a/protocol.h b/src/hardware/hp-3478a/protocol.h index a36af719..baf622bd 100644 --- a/src/hardware/hp-3478a/protocol.h +++ b/src/hardware/hp-3478a/protocol.h @@ -27,9 +27,137 @@ #define LOG_PREFIX "hp-3478a" +#define SB1_FUNCTION_BLOCK 0b11100000 +#define SB1_RANGE_BLOCK 0b00011100 +#define SB1_DIGITS_BLOCK 0b00000011 + +/* Status Byte 1 (Function) */ +enum sb1_function { + FUNCTION_VDC = 0b00100000, + FUNCTION_VAC = 0b01000000, + FUNCTION_2WR = 0b01100000, + FUNCTION_4WR = 0b10000000, + FUNCTION_ADC = 0b10100000, + FUNCTION_AAC = 0b11000000, + FUNCTION_EXR = 0b11100000, +}; + +/* Status Byte 1 (Range V DC) */ +enum sb1_range_vdc { + RANGE_VDC_30MV = 0b00000100, + RANGE_VDC_300MV = 0b00001000, + RANGE_VDC_3V = 0b00001100, + RANGE_VDC_30V = 0b00010000, + RANGE_VDC_300V = 0b00010100, +}; + +/* Status Byte 1 (Range V AC) */ +enum sb1_range_vac { + RANGE_VAC_300MV = 0b00000100, + RANGE_VAC_3V = 0b00001000, + RANGE_VAC_30V = 0b00001100, + RANGE_VAC_300V = 0b00010000, +}; + +/* Status Byte 1 (Range A) */ +enum sb1_range_a { + RANGE_A_300MA = 0b00000100, + RANGE_A_3A = 0b00001000, +}; + +/* Status Byte 1 (Range Ohm) */ +enum sb1_range_ohm { + RANGE_OHM_30R = 0b00000100, + RANGE_OHM_300R = 0b00001000, + RANGE_OHM_3KR = 0b00001100, + RANGE_OHM_30KR = 0b00010000, + RANGE_OHM_300KR = 0b00010100, + RANGE_OHM_3MR = 0b00011000, + RANGE_OHM_30MR = 0b00011100, +}; + +/* Status Byte 1 (Digits) */ +enum sb1_digits { + DIGITS_5_5 = 0b00000001, + DIGITS_4_5 = 0b00000010, + DIGITS_3_5 = 0b00000011, +}; + +/* Status Byte 2 */ +enum sb2_status { + STATUS_INT_TRIGGER = (1 << 0), + STATUS_AUTO_RANGE = (1 << 1), + STATUS_AUTO_ZERO = (1 << 2), + STATUS_50HZ = (1 << 3), + STATUS_FRONT_TERMINAL = (1 << 4), + STATUS_CAL_RAM = (1 << 5), + STATUS_EXT_TRIGGER = (1 << 6), +}; + +/* Status Byte 3 (Serial Poll Mask) */ +enum sb3_srq { + SRQ_BUS_AVAIL = (1 << 0), + SRQ_SYNTAX_ERR = (1 << 2), + SRQ_HARDWARE_ERR = (1 << 3), + SRQ_KEYBORD = (1 << 4), + SRQ_CAL_FAILED = (1 << 5), + SRQ_POWER_ON = (1 << 7), +}; + +/* Status Byte 4 (Error) */ +enum sb4_error { + ERROR_SELF_TEST = (1 << 0), + ERROR_RAM_SELF_TEST = (1 << 1), + ERROR_ROM_SELF_TEST = (1 << 2), + ERROR_AD_SLOPE = (1 << 3), + ERROR_AD_SELF_TEST = (1 << 4), + ERROR_AD_LINK = (1 << 5), +}; + +/* Channel connector (front terminals or rear terminals. */ +enum terminal_connector { + TERMINAL_FRONT, + TERMINAL_REAR, +}; + +/* Possible triggers */ +enum trigger_state { + TRIGGER_UNDEFINED, + TRIGGER_EXTERNAL, + TRIGGER_INTERNAL, +}; + +/* Possible line frequencies */ +enum line_freq { + LINE_50HZ, + LINE_60HZ, +}; + struct dev_context { + struct sr_sw_limits limits; + + double measurement; + enum sr_mq measurement_mq; + enum sr_mqflag measurement_mq_flags; + enum sr_unit measurement_unit; + uint8_t enc_digits; + uint8_t spec_digits; + + enum terminal_connector terminal; + enum trigger_state trigger; + enum line_freq line; + gboolean auto_zero; + gboolean calibration; +}; + +struct channel_context { + int index; + enum terminal_connector location; }; +SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, + enum sr_mqflag mq_flags); +SR_PRIV int hp_3478a_get_status_bytes(const struct sr_dev_inst *sdi); SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data); #endif