X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Frdtech-dps%2Fprotocol.c;h=83090d86ac7c2253c6662a305b7beab10015df67;hp=9f45fc4f6d8fa974f37b78b807859faabd5f3e9b;hb=a7e919a3a6b7fd511acbe1a280536b76c70c28d2;hpb=884ae8c02167e9575c84f09c7eee427e0c7353b7 diff --git a/src/hardware/rdtech-dps/protocol.c b/src/hardware/rdtech-dps/protocol.c index 9f45fc4f..83090d86 100644 --- a/src/hardware/rdtech-dps/protocol.c +++ b/src/hardware/rdtech-dps/protocol.c @@ -132,7 +132,7 @@ static int rdtech_dps_set_reg(const struct sr_dev_inst *sdi, modbus = sdi->conn; wrptr = (void *)registers; - write_u16le(wrptr, value); + write_u16be(wrptr, value); g_mutex_lock(&devc->rw_mutex); ret = sr_modbus_write_multiple_registers(modbus, address, @@ -187,8 +187,8 @@ SR_PRIV int rdtech_dps_get_model_version(struct sr_modbus_dev_inst *modbus, if (ret != SR_OK) return ret; rdptr = (void *)registers; - *model = read_u16le_inc(&rdptr); - *version = read_u16le_inc(&rdptr); + *model = read_u16be_inc(&rdptr); + *version = read_u16be_inc(&rdptr); *serno = 0; sr_info("RDTech DPS/DPH model: %u version: %u", *model, *version); @@ -254,10 +254,11 @@ static int send_value(const struct sr_dev_inst *sdi, * when the UART bitrate is only 9600bps? */ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, - struct rdtech_dps_state *state) + struct rdtech_dps_state *state, enum rdtech_dps_state_context reason) { struct dev_context *devc; struct sr_modbus_dev_inst *modbus; + gboolean get_config, get_init_state, get_curr_meas; uint16_t registers[12]; int ret; const uint8_t *rdptr; @@ -276,6 +277,37 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, if (!state) return SR_ERR_ARG; + /* Determine the requested level of response detail. */ + get_config = FALSE; + get_init_state = FALSE; + get_curr_meas = FALSE; + switch (reason) { + case ST_CTX_CONFIG: + get_config = TRUE; + get_init_state = TRUE; + get_curr_meas = TRUE; + break; + case ST_CTX_PRE_ACQ: + get_init_state = TRUE; + get_curr_meas = TRUE; + break; + case ST_CTX_IN_ACQ: + get_curr_meas = TRUE; + break; + default: + /* EMPTY */ + break; + } + /* + * TODO Make use of this information to reduce the transfer + * volume, especially on low bitrate serial connections. Though + * the device firmware's samplerate is probably more limiting + * than communication bandwidth is. + */ + (void)get_config; + (void)get_init_state; + (void)get_curr_meas; + switch (devc->model->model_type) { case MODEL_DPS: /* @@ -285,9 +317,6 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, * and the sequence of the registers and how to interpret * their bit fields. But then this is not too unusual for * a hardware specific device driver ... - * - * TODO Optionally reduce the transfer volume depending - * on the caller specified state query context. */ g_mutex_lock(&devc->rw_mutex); ret = rdtech_dps_read_holding_registers(modbus, @@ -298,25 +327,25 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, /* Interpret the registers' values. */ rdptr = (const void *)registers; - uset_raw = read_u16le_inc(&rdptr); + uset_raw = read_u16be_inc(&rdptr); volt_target = uset_raw / devc->voltage_multiplier; - iset_raw = read_u16le_inc(&rdptr); + iset_raw = read_u16be_inc(&rdptr); curr_limit = iset_raw / devc->current_multiplier; - uout_raw = read_u16le_inc(&rdptr); + uout_raw = read_u16be_inc(&rdptr); curr_voltage = uout_raw / devc->voltage_multiplier; - iout_raw = read_u16le_inc(&rdptr); + iout_raw = read_u16be_inc(&rdptr); curr_current = iout_raw / devc->current_multiplier; - power_raw = read_u16le_inc(&rdptr); + power_raw = read_u16be_inc(&rdptr); curr_power = power_raw / 100.0f; - (void)read_u16le_inc(&rdptr); /* UIN */ - reg_val = read_u16le_inc(&rdptr); /* LOCK */ + (void)read_u16be_inc(&rdptr); /* UIN */ + reg_val = read_u16be_inc(&rdptr); /* LOCK */ is_lock = reg_val != 0; - reg_val = read_u16le_inc(&rdptr); /* PROTECT */ + reg_val = read_u16be_inc(&rdptr); /* PROTECT */ uses_ovp = reg_val == STATE_OVP; uses_ocp = reg_val == STATE_OCP; - reg_state = read_u16le_inc(&rdptr); /* CV_CC */ + reg_state = read_u16be_inc(&rdptr); /* CV_CC */ is_reg_cc = reg_state == MODE_CC; - out_state = read_u16le_inc(&rdptr); /* ENABLE */ + out_state = read_u16be_inc(&rdptr); /* ENABLE */ is_out_enabled = out_state != 0; /* Transfer another chunk of registers in a single call. */ @@ -329,9 +358,9 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, /* Interpret the second registers chunk's values. */ rdptr = (const void *)registers; - ovpset_raw = read_u16le_inc(&rdptr); /* PRE OVPSET */ + ovpset_raw = read_u16be_inc(&rdptr); /* PRE OVPSET */ ovp_threshold = ovpset_raw * devc->voltage_multiplier; - ocpset_raw = read_u16le_inc(&rdptr); /* PRE OCPSET */ + ocpset_raw = read_u16be_inc(&rdptr); /* PRE OCPSET */ ocp_threshold = ocpset_raw * devc->current_multiplier; break; @@ -393,7 +422,13 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, return SR_ERR_ARG; } - /* Store gathered details in the high level container. */ + /* + * Store gathered details in the high level container. + * + * TODO Make use of the caller's context. The register access + * code path above need not have gathered every detail in every + * invocation. + */ memset(state, 0, sizeof(*state)); state->lock = is_lock; state->mask |= STATE_LOCK; @@ -551,7 +586,11 @@ SR_PRIV int rdtech_dps_seed_receive(const struct sr_dev_inst *sdi) struct rdtech_dps_state state; int ret; - ret = rdtech_dps_get_state(sdi, &state); + if (!sdi || !sdi->priv) + return SR_ERR_ARG; + devc = sdi->priv; + + ret = rdtech_dps_get_state(sdi, &state, ST_CTX_PRE_ACQ); if (ret != SR_OK) return ret; @@ -586,7 +625,7 @@ SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data) devc = sdi->priv; /* Get the device's current state. */ - ret = rdtech_dps_get_state(sdi, &state); + ret = rdtech_dps_get_state(sdi, &state, ST_CTX_IN_ACQ); if (ret != SR_OK) return ret;